Add track recovery, fix Arctic Land bridge & 9-map sim/render, harden security & optimize runtime

- Add manual track recovery (R / Backspace / Gamepad Back), document controls in README.md, fix Track 8 (Arctic Land) bridge jump/clearance physics, and fix Track 5 respawn invincibility & Track 6 off-track AI recovery across all 9 maps
- Verify 9-map sector morphing, combat (swept projectiles/LOS), pitstop chargers, and overpass/bridge surface sampling
- Fix Overpass Bottom (-Y) diagonal split and AO, add TerrainChunk frustum culling, O(1) 3D Classic uPaletteLut, atlas gutter/bleed UVs, cached uniform locations, and reusable render scratch buffers
- Harden VFS path traversal, RNC decompression bombs/truncated headers, asset/WAV parsers, GameConfig, save loaders, CLI args, and FLIC lifecycle
- Optimize 48 kHz OPL3/SoundFont synthesis (trig LUTs, cached envelopes/filters), 64-bit XMI ticks, and audio parameter/thread safety
- Add security, rendering, physics, and gameplay regression tests across all test suites
This commit is contained in:
Luxferre committed 2026-12-01 18:50:29 +02:00
1 parent 5c2ac8e70f
commit db788a2739
50 files changed
+3522 -1100

No files matched your search

+216 -117
View File
@@ -25,6 +25,14 @@ float fx_hash(uint32_t x) {
return static_cast<float>(x & 0x00FFFFFFu) / 16777216.0f;
}
float closest_segment_t(const Vec3& a, const Vec3& b, const Vec3& p, Vec3& out_pt) {
Vec3 ab = b - a;
float len_sq = ab.len_sq();
float t = (len_sq > 1e-8f) ? std::clamp((p - a).dot(ab) / len_sq, 0.0f, 1.0f) : 1.0f;
out_pt = a + ab * t;
return t;
}
void apply_damage(CraftState& victim, int attacker_id, const std::vector<CraftState>& crafts,
const RaceSetup& setup, std::vector<ParticleBurst>& particles,
std::vector<SimEvent>& events, bool& out_race_over, int& out_winner_id) {
@@ -39,8 +47,12 @@ void apply_damage(CraftState& victim, int attacker_id, const std::vector<CraftSt
victim.deaths += 1;
victim.vel = Vec3(0, 0, 0);
victim.recovery_state = RecoveryState::CallingRepair;
victim.recovery_refill = true;
victim.recovery_duration = 3.2f;
victim.recovery_timer = 3.2f;
victim.recovery_pos = victim.curr_pose.pos;
victim.pit_stuck_timer = 0.0f;
victim.stuck_in_pit_hint = false;
std::string killer_name = "TRACK";
if (attacker_id >= 0 && attacker_id < static_cast<int>(crafts.size())) {
@@ -142,6 +154,27 @@ void apply_pickup(CraftState& c, uint8_t subtype, std::vector<SimEvent>& events)
} // namespace
bool has_line_of_sight(const assets::LevelData& level, const Vec3& a, const Vec3& b) {
Vec3 d = b - a;
float len = d.len();
if (len < 0.5f) return true;
int steps = std::max(1, static_cast<int>(std::ceil(len / 0.70f)));
for (int s = 1; s < steps; ++s) {
float t = static_cast<float>(s) / static_cast<float>(steps);
Vec3 p = a + d * t;
float gy = level.sample_height_world(p.x, p.z);
if (p.y <= gy - 0.10f) {
return false;
}
int ix = static_cast<int>(std::floor(p.x));
int iz = static_cast<int>(std::floor(p.z));
if (level.is_cell_solid_at_point(ix, iz, p.x, p.z, p.y, 0.10f)) {
return false;
}
}
return true;
}
void spawn_smoke_puff(std::vector<ParticleBurst>& particles, const Vec3& pos, const Vec3& vel,
float start_size, float end_size, const Vec4& color, float life,
float drag, float gravity, float seed) {
@@ -273,7 +306,10 @@ void step_combat_and_world(
bool& out_race_over,
int& out_winner_id,
float dt,
std::vector<ProjectileTrace>* out_projectile_traces) {
std::vector<ProjectileTrace>* out_projectile_traces,
uint32_t* io_next_projectile_uid) {
uint32_t fallback_uid = 1;
uint32_t& uid_counter = io_next_projectile_uid ? *io_next_projectile_uid : fallback_uid;
// 1. Recovery craft state machine & weapon firing per craft
for (size_t i = 0; i < crafts.size(); ++i) {
@@ -281,60 +317,36 @@ void step_combat_and_world(
if (c.recovery_state != RecoveryState::Active) {
c.recovery_timer -= dt;
if (c.recovery_state == RecoveryState::CallingRepair && c.recovery_timer < 2.2f) {
float rec_dur = std::max(c.recovery_duration, 0.1f);
float u = c.recovery_timer / rec_dur;
if (c.recovery_state == RecoveryState::CallingRepair && u < 0.68f) {
c.recovery_state = RecoveryState::AwaitingRepair;
if (c.is_human) {
events.push_back({SimSoundCue::None, c.curr_pose.pos, c.player_index, "AWAITING REPAIR", 0.0f});
events.push_back({SimSoundCue::None, c.curr_pose.pos, c.player_index,
c.recovery_refill ? "AWAITING REPAIR" : "AWAITING RECOVERY", 0.0f});
}
} else if (c.recovery_state == RecoveryState::AwaitingRepair && c.recovery_timer < 1.1f) {
} else if (c.recovery_state == RecoveryState::AwaitingRepair && u < 0.34f) {
c.recovery_state = RecoveryState::Repairing;
if (c.is_human) {
events.push_back({SimSoundCue::Refuel, c.curr_pose.pos, c.player_index, "REPAIRING", 0.0f});
events.push_back({c.recovery_refill ? SimSoundCue::Refuel : SimSoundCue::SwitchTrigger,
c.curr_pose.pos, c.player_index,
c.recovery_refill ? "REPAIRING" : "RECOVERING TO TRACK", 0.0f});
}
} else if (c.recovery_timer <= 0.0f) {
c.recovery_state = RecoveryState::Active;
c.shield = c.max_shield;
c.fuel = c.max_fuel;
c.ammo = std::max(c.ammo, c.max_ammo * 0.5f);
// Place cleanly at nearest clear main waypoint and resolve against morphed walls
if (!level.main_waypoints.empty()) {
size_t best_wp = 0;
float best_d2 = 1e18f;
for (size_t wi = 0; wi < level.main_waypoints.size(); ++wi) {
const auto& w = level.main_waypoints[wi];
float dx = w.x - c.curr_pose.pos.x;
float dz = w.z - c.curr_pose.pos.z;
float d2 = dx * dx + dz * dz;
if (d2 < best_d2) {
best_d2 = d2;
best_wp = wi;
}
}
const auto& wp = level.main_waypoints[best_wp];
const auto& wpn = level.main_waypoints[(best_wp + 1) % level.main_waypoints.size()];
const VehicleSpec& sp_rec = kVehicleSpecs[std::clamp(c.vehicle_idx, 0, 5)];
float fy = 0.0f, cy = 1e6f;
level.sample_surface_and_ceiling(wp.x, wp.z, c.curr_pose.pos.y, fy, cy);
c.curr_pose.pos = Vec3(wp.x, fy + sp_rec.hover_offset, wp.z);
c.curr_pose.yaw = std::atan2(wpn.x - wp.x, wpn.z - wp.z);
c.curr_pose.pitch = 0.0f;
c.curr_pose.roll = 0.0f;
c.vel = Vec3(0, 0, 0);
float dummy_imp = 0.0f;
bool dummy_scr = false;
resolve_world_collisions(c, level, dummy_imp, dummy_scr);
c.prev_pose = c.curr_pose;
c.current_wp = static_cast<int>(best_wp);
c.ai_target_wp = static_cast<int>((best_wp + 1) % level.main_waypoints.size());
if (c.recovery_refill) {
c.shield = c.max_shield;
c.fuel = c.max_fuel;
c.ammo = std::max(c.ammo, c.max_ammo * 0.5f);
c.invincible_timer = std::max(c.invincible_timer, 2.0f);
} else if (setup.fuel_enabled && c.fuel < 5.0f) {
c.fuel = 8.0f;
}
c.ai_stuck_timer = 0.0f;
c.ai_unstuck_timer = 0.0f;
c.ai_progress_timer = 0.0f;
c.ai_pit_stay_timer = 0.0f;
place_craft_on_track(c, level, crafts, setup.mode != GameMode::DeathMatch);
c.ai_pit_cooldown = 0.0f;
c.ai_seeking_pit = false;
c.ai_progress_anchor = c.curr_pose.pos;
c.ai_anchor_init = true;
c.recovery_cooldown = 1.5f;
c.recovery_refill = true;
c.recovery_duration = 3.2f;
} else if (c.shield <= 0.1f) {
int rec_ticks = static_cast<int>(std::lround(c.recovery_timer * 60.0f));
if (rec_ticks % 4 == 0) {
@@ -359,7 +371,12 @@ void step_combat_and_world(
// Trigger repair if fuel completely depleted and stopped
if (setup.fuel_enabled && c.fuel <= 0.05f && c.speed_horizontal() < 0.8f) {
c.recovery_state = RecoveryState::CallingRepair;
c.recovery_refill = true;
c.recovery_duration = 3.0f;
c.recovery_timer = 3.0f;
c.recovery_pos = c.curr_pose.pos;
c.pit_stuck_timer = 0.0f;
c.stuck_in_pit_hint = false;
if (c.is_human) {
events.push_back({SimSoundCue::Warning, c.curr_pose.pos, c.player_index, "CALLING REPAIR", 0.2f});
}
@@ -372,8 +389,9 @@ void step_combat_and_world(
continue;
}
// Update missile lock-on cone
// Update missile lock-on cone (requires line-of-sight)
Vec3 fwd = c.forward_dir();
const Vec3 eye_a = c.curr_pose.pos + Vec3(0.0f, 0.25f, 0.0f);
int best_tgt = -1;
float best_score = 0.72f; // ~44 deg half-cone
for (size_t j = 0; j < crafts.size(); ++j) {
@@ -385,7 +403,8 @@ void step_combat_and_world(
float dist = to_tgt.len();
if (dist < 1.0f || dist > 38.0f) continue;
float align = (to_tgt / dist).dot(fwd);
if (align > best_score) {
if (align > best_score &&
has_line_of_sight(level, eye_a, crafts[j].curr_pose.pos + Vec3(0.0f, 0.25f, 0.0f))) {
best_score = align;
best_tgt = static_cast<int>(j);
}
@@ -417,6 +436,7 @@ void step_combat_and_world(
float dmg = sp.minigun_damage * (1.0f + 0.25f * static_cast<float>(c.minigun_level - 1));
Projectile p;
p.uid = uid_counter++;
p.active = true;
p.is_missile = false;
p.owner_id = c.id;
@@ -438,6 +458,7 @@ void step_combat_and_world(
float dmg = sp.missile_damage * (1.0f + 0.22f * static_cast<float>(c.missile_level - 1));
Projectile p;
p.uid = uid_counter++;
p.active = true;
p.is_missile = true;
p.owner_id = c.id;
@@ -491,22 +512,132 @@ void step_combat_and_world(
Vec3 prev_pos = pr.pos;
pr.pos += pr.vel * dt;
// Swept segment intersection against world, crafts, and destructible props
bool hit_world = false;
float world_t = 2.0f;
Vec3 world_pos = pr.pos;
{
float seg_len = (pr.pos - prev_pos).len();
int sub = std::clamp(static_cast<int>(std::ceil(seg_len / 0.35f)), 1, 4);
for (int s = 1; s <= sub; ++s) {
float t = static_cast<float>(s) / static_cast<float>(sub);
Vec3 sp = lerp(prev_pos, pr.pos, t);
float gy = level.sample_height_world(sp.x, sp.z);
int ix = static_cast<int>(std::floor(sp.x));
int iz = static_cast<int>(std::floor(sp.z));
if (sp.y <= gy ||
level.is_cell_solid_at_point(ix, iz, sp.x, sp.z, sp.y, 0.10f)) {
hit_world = true;
world_t = t;
world_pos = sp;
break;
}
}
}
int hit_craft_idx = -1;
float craft_t = 2.0f;
Vec3 craft_hit_pos = pr.pos;
for (size_t j = 0; j < crafts.size(); ++j) {
const CraftState& victim = crafts[j];
if (victim.id == pr.owner_id || victim.recovery_state != RecoveryState::Active) continue;
const VehicleSpec& vspec = kVehicleSpecs[std::clamp(victim.vehicle_idx, 0, 5)];
float hit_r = vspec.collision_radius + 0.12f;
Vec3 cpt;
float t = closest_segment_t(prev_pos, pr.pos, victim.curr_pose.pos, cpt);
if ((victim.curr_pose.pos - cpt).len_sq() < hit_r * hit_r && t < craft_t) {
craft_t = t;
hit_craft_idx = static_cast<int>(j);
craft_hit_pos = cpt;
}
}
int hit_prop_idx = -1;
float prop_t = 2.0f;
Vec3 prop_hit_pos = pr.pos;
for (size_t pi = 0; pi < props.size(); ++pi) {
const auto& prop = props[pi];
if (!prop.alive) continue;
Vec3 center = prop.pos + Vec3(0.0f, 0.25f, 0.0f);
Vec3 cpt;
float t = closest_segment_t(prev_pos, pr.pos, center, cpt);
if ((center - cpt).len_sq() < 0.60f * 0.60f && t < prop_t) {
prop_t = t;
hit_prop_idx = static_cast<int>(pi);
prop_hit_pos = cpt;
}
}
if (out_projectile_traces != nullptr) {
int owner_player = -1;
if (pr.owner_id >= 0 && pr.owner_id < static_cast<int>(crafts.size()) &&
crafts[pr.owner_id].is_human) {
owner_player = crafts[pr.owner_id].player_index;
}
out_projectile_traces->push_back({prev_pos, pr.pos, owner_player, pr.is_missile});
Vec3 trace_end = pr.pos;
if (hit_craft_idx >= 0 && craft_t <= prop_t && (!hit_world || craft_t <= world_t)) {
trace_end = craft_hit_pos;
} else if (hit_prop_idx >= 0 && (!hit_world || prop_t <= world_t)) {
trace_end = prop_hit_pos;
}
out_projectile_traces->push_back({prev_pos, trace_end, owner_player, pr.is_missile, pr.uid});
}
// World/Column/Ground impact
float gy = level.sample_height_world(pr.pos.x, pr.pos.z);
int ix = static_cast<int>(std::floor(pr.pos.x));
int iz = static_cast<int>(std::floor(pr.pos.z));
if (pr.pos.y <= gy ||
level.is_cell_solid_at_point(ix, iz, pr.pos.x, pr.pos.z, pr.pos.y, 0.10f)) {
if (hit_craft_idx >= 0 && craft_t <= prop_t && (!hit_world || craft_t <= world_t)) {
CraftState& victim = crafts[static_cast<size_t>(hit_craft_idx)];
pr.active = false;
pr.pos = craft_hit_pos;
if (pr.owner_id >= 0 && pr.owner_id < static_cast<int>(crafts.size())) {
crafts[pr.owner_id].shots_hit += 1;
}
if (pr.is_missile) {
spawn_explosion_fx(particles, pr.pos, 1.15f);
events.push_back({SimSoundCue::Explosion, pr.pos,
victim.is_human ? victim.player_index : -1, "", 0.65f});
} else {
spawn_impact_fx(particles, pr.pos, true);
events.push_back({SimSoundCue::Impact, pr.pos,
victim.is_human ? victim.player_index : -1, "", 0.20f});
}
if (setup.damage_enabled && victim.invincible_timer <= 0.0f) {
victim.shield -= pr.damage;
victim.armour_used += pr.damage;
victim.damage_flash = 0.22f;
if (victim.shield <= 0.0f) {
if (pr.owner_id >= 0 && pr.owner_id < static_cast<int>(crafts.size())) {
crafts[pr.owner_id].kills += 1;
if (setup.mode == GameMode::DeathMatch &&
crafts[pr.owner_id].kills >= setup.deathmatch_kill_limit) {
out_race_over = true;
out_winner_id = crafts[pr.owner_id].id;
}
}
apply_damage(victim, pr.owner_id, crafts, setup, particles, events,
out_race_over, out_winner_id);
}
}
continue;
}
if (hit_prop_idx >= 0 && (!hit_world || prop_t <= world_t)) {
PropObject& prop = props[static_cast<size_t>(hit_prop_idx)];
pr.active = false;
pr.pos = prop_hit_pos;
prop.hp -= pr.damage;
if (prop.hp <= 0.0f) {
prop.alive = false;
prop.respawn_timer = 25.0f;
spawn_explosion_fx(particles, prop.pos, 0.95f);
events.push_back({SimSoundCue::Explosion, prop.pos, -1, "", 0.25f});
}
continue;
}
if (hit_world) {
pr.active = false;
pr.pos = world_pos;
if (pr.is_missile) {
spawn_explosion_fx(particles, pr.pos, 0.95f);
events.push_back({SimSoundCue::Explosion, pr.pos, -1, "", 0.0f});
@@ -515,65 +646,6 @@ void step_combat_and_world(
}
continue;
}
// Hit Crafts
for (size_t j = 0; j < crafts.size(); ++j) {
CraftState& victim = crafts[j];
if (victim.id == pr.owner_id || victim.recovery_state != RecoveryState::Active) continue;
const VehicleSpec& vspec = kVehicleSpecs[std::clamp(victim.vehicle_idx, 0, 5)];
float hit_r = vspec.collision_radius + 0.12f;
Vec3 d = victim.curr_pose.pos - pr.pos;
if (d.len_sq() < hit_r * hit_r) {
pr.active = false;
if (pr.owner_id >= 0 && pr.owner_id < static_cast<int>(crafts.size())) {
crafts[pr.owner_id].shots_hit += 1;
}
if (pr.is_missile) {
spawn_explosion_fx(particles, pr.pos, 1.15f);
events.push_back({SimSoundCue::Explosion, pr.pos,
victim.is_human ? victim.player_index : -1, "", 0.65f});
} else {
spawn_impact_fx(particles, pr.pos, true);
events.push_back({SimSoundCue::Impact, pr.pos,
victim.is_human ? victim.player_index : -1, "", 0.20f});
}
if (setup.damage_enabled && victim.invincible_timer <= 0.0f) {
victim.shield -= pr.damage;
victim.armour_used += pr.damage;
if (victim.shield <= 0.0f) {
if (pr.owner_id >= 0 && pr.owner_id < static_cast<int>(crafts.size())) {
crafts[pr.owner_id].kills += 1;
if (setup.mode == GameMode::DeathMatch &&
crafts[pr.owner_id].kills >= setup.deathmatch_kill_limit) {
out_race_over = true;
out_winner_id = crafts[pr.owner_id].id;
}
}
apply_damage(victim, pr.owner_id, crafts, setup, particles, events,
out_race_over, out_winner_id);
}
}
break;
}
}
if (!pr.active) continue;
// Hit Destructible Props
for (auto& prop : props) {
if (!prop.alive) continue;
if ((prop.pos - pr.pos).len_sq() < 0.60f * 0.60f) {
pr.active = false;
prop.hp -= pr.damage;
if (prop.hp <= 0.0f) {
prop.alive = false;
prop.respawn_timer = 25.0f;
spawn_explosion_fx(particles, prop.pos, 0.95f);
events.push_back({SimSoundCue::Explosion, prop.pos, -1, "", 0.25f});
}
break;
}
}
}
projectiles.erase(
std::remove_if(projectiles.begin(), projectiles.end(),
@@ -623,8 +695,13 @@ void step_combat_and_world(
prop.alive = false;
prop.respawn_timer = 25.0f;
if (prop.model_key.rfind("BAREL", 0) == 0) {
if (setup.damage_enabled) {
c.shield = std::max(5.0f, c.shield - 12.0f);
if (setup.damage_enabled && c.invincible_timer <= 0.0f) {
float dmg = std::min(12.0f, std::max(0.0f, c.shield - 5.0f));
if (dmg > 0.0f) {
c.shield -= dmg;
c.armour_used += dmg;
}
c.damage_flash = 0.22f;
}
spawn_explosion_fx(particles, prop.pos, 1.0f);
events.push_back({SimSoundCue::Explosion, prop.pos,
@@ -647,12 +724,14 @@ std::vector<RecoveryVisual> get_recovery_visuals(const std::vector<CraftState>&
c.recovery_state == RecoveryState::Repairing) {
RecoveryVisual rv;
rv.active = true;
float rec_dur = std::max(c.recovery_duration, 0.1f);
float u = std::clamp(c.recovery_timer / rec_dur, 0.0f, 1.0f);
float hover_y = 0.0f;
if (c.recovery_state == RecoveryState::CallingRepair) {
float t = std::clamp((c.recovery_timer - 2.2f) / 1.0f, 0.0f, 1.0f);
float t = std::clamp((u - 0.68f) / 0.32f, 0.0f, 1.0f);
hover_y = 1.6f + t * 8.0f;
} else if (c.recovery_state == RecoveryState::AwaitingRepair) {
float t = std::clamp((c.recovery_timer - 1.1f) / 1.1f, 0.0f, 1.0f);
float t = std::clamp((u - 0.34f) / 0.34f, 0.0f, 1.0f);
hover_y = 1.05f + t * 0.55f;
} else {
hover_y = 1.05f;
@@ -665,4 +744,24 @@ std::vector<RecoveryVisual> get_recovery_visuals(const std::vector<CraftState>&
return out;
}
bool trigger_track_recovery(CraftState& craft, std::vector<SimEvent>& events) {
if (craft.recovery_state != RecoveryState::Active || craft.finished ||
craft.recovery_cooldown > 0.0f) {
return false;
}
craft.vel = Vec3(0.0f, 0.0f, 0.0f);
craft.recovery_state = RecoveryState::CallingRepair;
craft.recovery_refill = false;
craft.recovery_duration = 1.6f;
craft.recovery_timer = 1.6f;
craft.recovery_pos = craft.curr_pose.pos;
craft.pit_stuck_timer = 0.0f;
craft.stuck_in_pit_hint = false;
if (craft.is_human) {
events.push_back({SimSoundCue::Warning, craft.curr_pose.pos, craft.player_index,
"CALLING RECOVERY", 0.18f});
}
return true;
}
} // namespace top_octane::sim