Files
top-octane/src/sim/combat.cpp
T

968 lines
44 KiB
C++
Raw Normal View History

2026-11-30 22:31:51 +02:00
#include "sim/combat.hpp"
#include "sim/vehicle_defs.hpp"
#include <algorithm>
#include <cmath>
#include <cstdint>
2026-11-30 22:31:51 +02:00
namespace top_octane::sim {
namespace {
uint32_t pos_seed_u32(const Vec3& p, uint32_t salt) {
uint32_t hx = static_cast<uint32_t>(std::lround(p.x * 16.0f)) * 73856093u;
uint32_t hy = static_cast<uint32_t>(std::lround(p.y * 16.0f)) * 19349663u;
uint32_t hz = static_cast<uint32_t>(std::lround(p.z * 16.0f)) * 83492791u;
return hx ^ hy ^ hz ^ (salt * 2654435761u);
}
float fx_hash(uint32_t x) {
x ^= x >> 16;
x *= 0x7feb352du;
x ^= x >> 15;
x *= 0x846ca68bu;
x ^= x >> 16;
return static_cast<float>(x & 0x00FFFFFFu) / 16777216.0f;
2026-11-30 22:31:51 +02:00
}
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;
}
float smoothstep01(float t) {
t = std::clamp(t, 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}
void begin_recovery_sequence(CraftState& c, bool refill, float duration) {
c.vel = Vec3(0.0f, 0.0f, 0.0f);
c.yaw_rate = 0.0f;
c.recovery_state = RecoveryState::CallingRepair;
c.recovery_refill = refill;
c.recovery_duration = duration;
c.recovery_timer = duration;
c.recovery_pos = c.curr_pose.pos;
c.recovery_start_yaw = c.curr_pose.yaw;
c.recovery_start_pitch = c.curr_pose.pitch;
c.recovery_start_roll = c.curr_pose.roll;
c.recovery_target_valid = false;
c.recovery_depart_timer = 0.0f;
c.pit_stuck_timer = 0.0f;
c.stuck_in_pit_hint = false;
c.wrong_way = false;
const Vec3 fwd(std::sin(c.recovery_start_yaw), 0.0f, std::cos(c.recovery_start_yaw));
const Vec3 start_drone = c.recovery_pos - fwd * 2.5f + Vec3(0.0f, 7.5f, 0.0f);
c.recovery_drone_curr_pose = {start_drone, c.recovery_start_yaw, 0.18f, 0.0f};
c.recovery_drone_prev_pose = c.recovery_drone_curr_pose;
}
void init_recovery_flight_plan(CraftState& c, const assets::LevelData& level,
const std::vector<CraftState>& crafts, bool restrict_arc) {
TrackRecoveryTarget tgt = compute_track_recovery_target(c, level, crafts, restrict_arc);
c.recovery_target_pos = tgt.valid ? tgt.pos : c.recovery_pos;
c.recovery_target_yaw = tgt.valid ? tgt.yaw : c.recovery_start_yaw;
c.recovery_target_wp = tgt.valid ? tgt.waypoint_idx : c.current_wp;
c.recovery_target_valid = true;
const Vec3& start = c.recovery_pos;
const Vec3& target = c.recovery_target_pos;
bool all_under_ceiling = true;
float min_ceil = 1e6f;
float max_floor = std::max(start.y, target.y);
float max_obstacle_y = std::max(start.y, target.y);
constexpr int kSamples = 16;
for (int k = 0; k < kSamples; ++k) {
const float t = static_cast<float>(k) / static_cast<float>(kSamples - 1);
const Vec3 sp = lerp(start, target, t);
float fy = 0.0f, cy = 1e6f;
level.sample_surface_and_ceiling(sp.x, sp.z, sp.y, fy, cy);
if (cy >= 1e5f) {
all_under_ceiling = false;
} else {
min_ceil = std::min(min_ceil, cy);
}
max_floor = std::max(max_floor, fy);
const float gy = level.sample_height_world(sp.x, sp.z);
max_obstacle_y = std::max(max_obstacle_y, gy);
const int ix = std::clamp(static_cast<int>(std::floor(sp.x)), 0, assets::kMapWidth - 1);
const int iz = std::clamp(static_cast<int>(std::floor(sp.z)), 0, assets::kMapHeight - 1);
const assets::MapEntry& cell = level.cell(ix, iz);
if (cell.tex_or_col < 0) {
int col_idx = -cell.tex_or_col;
if (col_idx > 0 && col_idx < assets::kMaxColumns) {
uint8_t mask = level.columns[col_idx].shape_mask;
for (int s_hi = 7; s_hi >= 0; --s_hi) {
if ((mask & (1u << s_hi)) != 0) {
max_obstacle_y = std::max(max_obstacle_y, gy + static_cast<float>(s_hi) + 1.0f);
break;
}
}
}
}
}
const float max_y = std::max(start.y, target.y);
const float dx = target.x - start.x;
const float dz = target.z - start.z;
const float dist_2d = std::sqrt(dx * dx + dz * dz);
if (all_under_ceiling && min_ceil < 1e5f) {
const float band_lo = max_y;
const float band_hi = std::max(max_y, min_ceil - 0.65f);
c.recovery_cruise_y = std::clamp(std::max(max_floor + 0.55f, max_y), band_lo, band_hi);
} else {
const float base_lift = (dist_2d < 0.75f)
? (max_y + 0.55f)
: (max_y + std::clamp(1.1f + dist_2d * 0.14f, 1.2f, 3.0f));
c.recovery_cruise_y = std::min(std::max(base_lift, max_obstacle_y + 0.85f), max_y + 5.8f);
}
}
2026-11-30 22:31:51 +02:00
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) {
(void)setup;
if (victim.recovery_state != RecoveryState::Active) return;
victim.damage_flash = 0.22f;
if (victim.shield > 0.0f) return;
// Craft destroyed!
victim.shield = 0.0f;
victim.deaths += 1;
begin_recovery_sequence(victim, true, 3.2f);
2026-11-30 22:31:51 +02:00
std::string killer_name = "TRACK";
if (attacker_id >= 0 && attacker_id < static_cast<int>(crafts.size())) {
killer_name = crafts[attacker_id].pilot_name;
}
victim.killed_by = killer_name;
spawn_explosion_fx(particles, victim.curr_pose.pos, 1.55f);
2026-11-30 22:31:51 +02:00
events.push_back({SimSoundCue::Explosion, victim.curr_pose.pos,
victim.is_human ? victim.player_index : -1,
victim.is_human ? ("KILLED BY " + killer_name) : "", 0.95f});
// Credit kill to attacker (const_cast avoided by updating in caller if needed, see below)
(void)out_race_over;
(void)out_winner_id;
}
void apply_pickup(CraftState& c, uint8_t subtype, std::vector<SimEvent>& events) {
// Authentic HIOCTANE.EXE Type-5 subtype table (0..13 -> TMAPS 29..42)
const VehicleSpec& sp = kVehicleSpecs[std::clamp(c.vehicle_idx, 0, 5)];
2026-11-30 22:31:51 +02:00
std::string msg;
SimSoundCue cue = SimSoundCue::Pickup;
switch (subtype) {
case 0:
c.invincible_timer = 15.0f;
msg = "INVINCIBLE";
cue = SimSoundCue::Powerup;
break;
2026-11-30 22:31:51 +02:00
case 1:
c.invisible_timer = 15.0f;
msg = "INVISIBLE";
cue = SimSoundCue::Powerup;
2026-11-30 22:31:51 +02:00
break;
case 2:
c.shield = std::min(c.max_shield, c.shield + std::max(25.0f, c.max_shield * 0.30f));
msg = "SHIELD EXTRA";
2026-11-30 22:31:51 +02:00
break;
case 3:
c.shield = std::max(c.shield, c.max_shield);
msg = "SHIELD FULL";
2026-11-30 22:31:51 +02:00
break;
case 4:
c.max_shield = std::max(c.max_shield, sp.max_shield * 2.0f);
2026-11-30 22:31:51 +02:00
c.shield = c.max_shield;
msg = "DOUBLE SHIELD";
2026-11-30 22:31:51 +02:00
cue = SimSoundCue::Powerup;
break;
case 5:
c.ammo = std::min(c.max_ammo, c.ammo + std::max(25.0f, c.max_ammo * 0.30f));
msg = "AMMO EXTRA";
break;
2026-11-30 22:31:51 +02:00
case 6:
c.ammo = std::max(c.ammo, c.max_ammo);
msg = "AMMO FULL";
2026-11-30 22:31:51 +02:00
break;
case 7:
c.max_ammo = std::max(c.max_ammo, sp.max_ammo * 2.0f);
2026-11-30 22:31:51 +02:00
c.ammo = c.max_ammo;
msg = "DOUBLE AMMO";
2026-11-30 22:31:51 +02:00
cue = SimSoundCue::Powerup;
break;
case 8:
c.fuel = std::min(c.max_fuel, c.fuel + std::max(25.0f, c.max_fuel * 0.30f));
msg = "FUEL EXTRA";
break;
case 9:
c.fuel = std::max(c.fuel, c.max_fuel);
msg = "FUEL FULL";
break;
2026-11-30 22:31:51 +02:00
case 10:
c.max_fuel = std::max(c.max_fuel, sp.max_fuel * 2.0f);
c.fuel = c.max_fuel;
msg = "DOUBLE FUEL";
2026-11-30 22:31:51 +02:00
cue = SimSoundCue::Powerup;
break;
case 11:
c.minigun_level = std::min(3, c.minigun_level + 1);
c.ammo = std::min(c.max_ammo, c.ammo + 35.0f);
msg = "MINIGUN UPGRADED";
cue = SimSoundCue::Powerup;
break;
case 12:
c.missile_level = std::min(3, c.missile_level + 1);
c.ammo = std::min(c.max_ammo, c.ammo + 35.0f);
msg = "MISSILE UPGRADED";
cue = SimSoundCue::Powerup;
break;
case 13:
default:
c.booster_level = std::min(3, c.booster_level + 1);
c.fuel = std::min(c.max_fuel, c.fuel + 35.0f);
msg = "BOOSTER UPGRADED";
cue = SimSoundCue::Powerup;
break;
2026-11-30 22:31:51 +02:00
}
events.push_back({cue, c.curr_pose.pos, c.is_human ? c.player_index : -1,
c.is_human ? msg : "", 0.25f});
}
} // 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) {
particles.push_back({pos, vel, start_size, color, life, life,
ParticleKind::Smoke, start_size, end_size,
drag, gravity, seed, color.w});
}
void spawn_explosion_fx(std::vector<ParticleBurst>& particles, const Vec3& pos, float scale) {
// 1. 2 blazing white-yellow core flash Fireball puffs
for (int k = 0; k < 2; ++k) {
float h0 = fx_hash(pos_seed_u32(pos, static_cast<uint32_t>(k + 1)));
float life = 0.22f + 0.06f * h0;
Vec3 jitter((h0 - 0.5f) * 0.25f * scale, 0.20f * scale,
(fx_hash(pos_seed_u32(pos, static_cast<uint32_t>(k + 20))) - 0.5f) * 0.25f * scale);
particles.push_back({pos + jitter, Vec3(0.0f, 0.9f * scale, 0.0f),
0.55f * scale, Vec4(1.0f, 0.96f, 0.78f, 1.0f), life, life,
ParticleKind::Fireball, 0.55f * scale, 2.25f * scale,
3.0f, 0.8f, h0, 1.0f});
}
// 2. 10 expanding turbulent Fireball puffs bursting spherically/upward
for (int k = 0; k < 10; ++k) {
uint32_t sbase = static_cast<uint32_t>(100 + k * 7);
float h_ang = fx_hash(pos_seed_u32(pos, sbase + 0u));
float h_spd = fx_hash(pos_seed_u32(pos, sbase + 1u));
float h_vy = fx_hash(pos_seed_u32(pos, sbase + 2u));
float h_life = fx_hash(pos_seed_u32(pos, sbase + 3u));
float ang = kTwoPi * (static_cast<float>(k) / 10.0f) + (h_ang - 0.5f) * 0.45f;
float spd = (2.0f + h_spd * 3.2f) * scale;
float vy = (1.2f + h_vy * 3.0f) * scale;
float life = 0.45f + h_life * 0.30f;
Vec3 off(std::cos(ang) * 0.28f * scale, 0.12f * scale, std::sin(ang) * 0.28f * scale);
Vec3 v(std::cos(ang) * spd, vy, std::sin(ang) * spd);
particles.push_back({pos + off, v,
0.45f * scale, Vec4(1.0f, 0.82f, 0.32f, 0.95f), life, life,
ParticleKind::Fireball, 0.45f * scale, 1.85f * scale,
2.8f, 1.6f, h_ang, 0.95f});
}
// 3. 12 thick dark charcoal/black Smoke puffs bursting outward & rising buoyantly
for (int k = 0; k < 12; ++k) {
uint32_t sbase = static_cast<uint32_t>(200 + k * 7);
float h_ang = fx_hash(pos_seed_u32(pos, sbase + 0u));
float h_spd = fx_hash(pos_seed_u32(pos, sbase + 1u));
float h_vy = fx_hash(pos_seed_u32(pos, sbase + 2u));
float h_life = fx_hash(pos_seed_u32(pos, sbase + 3u));
float h_col = fx_hash(pos_seed_u32(pos, sbase + 4u));
float ang = kTwoPi * (static_cast<float>(k) / 12.0f) + (h_ang - 0.5f) * 0.40f;
float spd = (1.4f + h_spd * 2.4f) * scale;
float vy = (1.4f + h_vy * 2.2f) * scale;
float life = 1.15f + h_life * 0.70f;
float g = 0.13f + h_col * 0.11f;
Vec3 off(std::cos(ang) * 0.35f * scale, 0.20f * scale, std::sin(ang) * 0.35f * scale);
Vec3 v(std::cos(ang) * spd, vy, std::sin(ang) * spd);
spawn_smoke_puff(particles, pos + off, v, 0.55f * scale, 2.80f * scale,
Vec4(g, g, g * 1.04f, 0.84f), life, 2.2f, 1.7f, h_ang);
}
// 4. 4 bright hot orange-yellow AdditiveGlow sparks
for (int k = 0; k < 4; ++k) {
uint32_t sbase = static_cast<uint32_t>(300 + k * 7);
float h_ang = fx_hash(pos_seed_u32(pos, sbase + 0u));
float h_spd = fx_hash(pos_seed_u32(pos, sbase + 1u));
float h_vy = fx_hash(pos_seed_u32(pos, sbase + 2u));
float h_life = fx_hash(pos_seed_u32(pos, sbase + 3u));
float ang = kTwoPi * (static_cast<float>(k) / 4.0f) + h_ang * 0.9f;
float spd = (5.5f + h_spd * 4.0f) * scale;
float vy = (3.5f + h_vy * 4.0f) * scale;
float life = 0.45f + h_life * 0.30f;
particles.push_back({pos, Vec3(std::cos(ang) * spd, vy, std::sin(ang) * spd),
0.18f * scale, Vec4(1.0f, 0.82f, 0.28f, 1.0f), life, life,
ParticleKind::AdditiveGlow, 0.18f * scale, 0.06f * scale,
0.8f, -12.0f, h_ang, 1.0f});
}
// 5. 4 charred rotating Debris chunks under strong downward gravity
for (int k = 0; k < 4; ++k) {
uint32_t sbase = static_cast<uint32_t>(400 + k * 7);
float h_ang = fx_hash(pos_seed_u32(pos, sbase + 0u));
float h_spd = fx_hash(pos_seed_u32(pos, sbase + 1u));
float h_vy = fx_hash(pos_seed_u32(pos, sbase + 2u));
float h_life = fx_hash(pos_seed_u32(pos, sbase + 3u));
float ang = kTwoPi * (static_cast<float>(k) / 4.0f + 0.125f) + (h_ang - 0.5f) * 0.6f;
float spd = (3.8f + h_spd * 3.7f) * scale;
float vy = (4.0f + h_vy * 4.2f) * scale;
float life = 0.65f + h_life * 0.40f;
particles.push_back({pos, Vec3(std::cos(ang) * spd, vy, std::sin(ang) * spd),
0.26f * scale, Vec4(0.15f, 0.14f, 0.13f, 0.95f), life, life,
ParticleKind::Debris, 0.26f * scale, 0.18f * scale,
0.4f, -13.5f, h_ang, 0.95f});
}
}
void spawn_impact_fx(std::vector<ParticleBurst>& particles, const Vec3& pos, bool hit_craft) {
const int n_sparks = hit_craft ? 4 : 3;
for (int k = 0; k < n_sparks; ++k) {
uint32_t sbase = static_cast<uint32_t>(500 + k * 5);
float h0 = fx_hash(pos_seed_u32(pos, sbase + 0u));
float h1 = fx_hash(pos_seed_u32(pos, sbase + 1u));
float ang = kTwoPi * (static_cast<float>(k) / static_cast<float>(n_sparks)) + h0;
float spd = 2.6f + h1 * 2.8f;
float life = 0.18f + 0.10f * h0;
Vec4 scol = hit_craft ? Vec4(1.0f, 0.65f, 0.20f, 1.0f) : Vec4(1.0f, 0.85f, 0.35f, 0.95f);
particles.push_back({pos, Vec3(std::cos(ang) * spd, 1.8f + h1 * 2.2f, std::sin(ang) * spd),
0.18f, scol, life, life,
ParticleKind::AdditiveGlow, 0.18f, 0.05f,
1.2f, -10.0f, h0, scol.w});
}
for (int k = 0; k < 2; ++k) {
float h0 = fx_hash(pos_seed_u32(pos, static_cast<uint32_t>(550 + k * 5)));
float ang = h0 * kTwoPi;
Vec3 v(std::cos(ang) * 0.9f, 1.2f + 0.6f * h0, std::sin(ang) * 0.9f);
spawn_smoke_puff(particles, pos, v, 0.18f, 0.62f,
Vec4(0.48f, 0.48f, 0.50f, 0.55f), 0.45f, 2.5f, 0.9f, h0);
}
}
2026-11-30 22:31:51 +02:00
void step_combat_and_world(
RaceSetup& setup,
const assets::LevelData& level,
std::vector<CraftState>& crafts,
const std::vector<CraftInput>& inputs,
std::vector<Projectile>& projectiles,
std::vector<PickupItem>& pickups,
std::vector<PropObject>& props,
std::vector<ParticleBurst>& particles,
std::vector<SimEvent>& events,
bool& out_race_over,
int& out_winner_id,
float dt,
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;
2026-11-30 22:31:51 +02:00
// 1. Recovery craft state machine & weapon firing per craft
for (size_t i = 0; i < crafts.size(); ++i) {
CraftState& c = crafts[i];
if (c.recovery_state == RecoveryState::Active && c.recovery_depart_timer > 0.0f) {
c.recovery_depart_timer = std::max(0.0f, c.recovery_depart_timer - dt);
const float dep_dur = std::max(c.recovery_depart_duration, 0.1f);
const float dp = std::clamp(1.0f - (c.recovery_depart_timer / dep_dur), 0.0f, 1.0f);
const float de = dp * dp;
const Vec3 dfwd(std::sin(c.recovery_depart_yaw), 0.0f, std::cos(c.recovery_depart_yaw));
c.recovery_drone_curr_pose.pos =
c.recovery_depart_start + dfwd * (de * 5.5f) + Vec3(0.0f, de * 8.0f, 0.0f);
c.recovery_drone_curr_pose.yaw = c.recovery_depart_yaw;
c.recovery_drone_curr_pose.pitch = 0.22f * dp;
c.recovery_drone_curr_pose.roll = 0.0f;
}
2026-11-30 22:31:51 +02:00
if (c.recovery_state != RecoveryState::Active) {
c.recovery_timer -= dt;
const float rec_dur = std::max(c.recovery_duration, 0.1f);
const float u = std::clamp(c.recovery_timer / rec_dur, 0.0f, 1.0f);
const float approach_end_u = 0.72f;
const float carry_start_u = c.recovery_refill ? 0.48f : 0.62f;
if (c.recovery_state == RecoveryState::CallingRepair &&
u < approach_end_u && c.recovery_timer > 0.0f) {
2026-11-30 22:31:51 +02:00
c.recovery_state = RecoveryState::AwaitingRepair;
if (c.is_human) {
events.push_back({SimSoundCue::None, c.curr_pose.pos, c.player_index,
c.recovery_refill ? "AWAITING REPAIR" : "AWAITING RECOVERY", 0.0f});
2026-11-30 22:31:51 +02:00
}
}
if (c.recovery_state == RecoveryState::AwaitingRepair &&
u < carry_start_u && c.recovery_timer > 0.0f) {
2026-11-30 22:31:51 +02:00
c.recovery_state = RecoveryState::Repairing;
if (c.is_human) {
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});
2026-11-30 22:31:51 +02:00
}
}
// Keep landing elevation synchronized with any dynamic terrain morphs under the target
const VehicleSpec& sp_rec = kVehicleSpecs[std::clamp(c.vehicle_idx, 0, 5)];
if (c.recovery_target_valid) {
const float tgt_gy = level.sample_entity_surface_y(c.recovery_target_pos.x, c.recovery_target_pos.z);
float tgt_fy = 0.0f, tgt_cy = 1e6f;
level.sample_surface_and_ceiling(c.recovery_target_pos.x, c.recovery_target_pos.z,
tgt_gy + sp_rec.hover_offset, tgt_fy, tgt_cy);
c.recovery_target_pos.y = tgt_fy + sp_rec.hover_offset;
}
if (c.recovery_timer <= 0.0f) {
2026-11-30 22:31:51 +02:00
c.recovery_state = RecoveryState::Active;
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;
2026-11-30 22:31:51 +02:00
}
float live_y = 0.0f;
if (c.recovery_target_valid &&
is_track_recovery_spot_clear(c, level, crafts, c.recovery_target_pos.x,
c.recovery_target_pos.z, live_y, false)) {
Vec3 land_pos(c.recovery_target_pos.x, live_y, c.recovery_target_pos.z);
finalize_craft_recovery_pose(c, level, land_pos, c.recovery_target_yaw,
c.recovery_target_wp, false);
} else {
place_craft_on_track(c, level, crafts, setup.mode != GameMode::DeathMatch);
}
c.recovery_depart_duration = 0.75f;
c.recovery_depart_timer = 0.75f;
c.recovery_depart_start = c.curr_pose.pos + Vec3(0.0f, 1.05f, 0.0f);
c.recovery_depart_yaw = c.curr_pose.yaw;
c.recovery_drone_curr_pose = {c.recovery_depart_start, c.recovery_depart_yaw, 0.0f, 0.0f};
c.ai_pit_cooldown = 0.0f;
c.recovery_cooldown = 1.5f;
c.recovery_refill = true;
c.recovery_duration = 3.2f;
c.recovery_target_valid = false;
} else if (c.recovery_state == RecoveryState::CallingRepair) {
c.curr_pose.pos = c.recovery_pos;
c.curr_pose.yaw = c.recovery_start_yaw;
c.curr_pose.pitch = c.recovery_start_pitch;
c.curr_pose.roll = c.recovery_start_roll;
const Vec3 sfwd(std::sin(c.recovery_start_yaw), 0.0f, std::cos(c.recovery_start_yaw));
Vec3 approach_start = c.recovery_pos - sfwd * 2.5f + Vec3(0.0f, 7.5f, 0.0f);
Vec3 approach_end = c.recovery_pos + Vec3(0.0f, 1.45f, 0.0f);
float sf = 0.0f, sc = 1e6f;
level.sample_surface_and_ceiling(c.recovery_pos.x, c.recovery_pos.z,
c.recovery_pos.y, sf, sc);
if (sc < 1e5f) {
const float max_drone_y = std::max(c.recovery_pos.y + 1.15f, sc - 0.45f);
approach_start.y = std::min(approach_start.y, max_drone_y);
if (c.recovery_drone_prev_pose.pos.y > max_drone_y) {
c.recovery_drone_prev_pose.pos.y = max_drone_y;
}
}
const float p = std::clamp((1.0f - u) / std::max(1.0f - approach_end_u, 1e-4f), 0.0f, 1.0f);
const float ease_out = 1.0f - (1.0f - p) * (1.0f - p);
Vec3 dpos = lerp(approach_start, approach_end, ease_out);
if (sc < 1e5f) {
dpos.y = std::min(dpos.y, std::max(approach_end.y, sc - 0.45f));
}
c.recovery_drone_curr_pose = {dpos, c.recovery_start_yaw, 0.18f * (1.0f - ease_out), 0.0f};
} else if (c.recovery_state == RecoveryState::AwaitingRepair) {
c.curr_pose.pos = c.recovery_pos;
c.curr_pose.yaw = c.recovery_start_yaw;
c.curr_pose.pitch = c.recovery_start_pitch;
c.curr_pose.roll = c.recovery_start_roll;
const float q = std::clamp((approach_end_u - u) /
std::max(approach_end_u - carry_start_u, 1e-4f), 0.0f, 1.0f);
const float hover_y = lerp(1.45f, 1.05f, q);
c.recovery_drone_curr_pose = {c.curr_pose.pos + Vec3(0.0f, hover_y, 0.0f),
c.curr_pose.yaw, 0.0f, 0.0f};
} else {
// Smoothly airlift craft horizontally & vertically from breakdown site to track
if (!c.recovery_target_valid) {
init_recovery_flight_plan(c, level, crafts, setup.mode != GameMode::DeathMatch);
}
const Vec3 prev_pos = c.curr_pose.pos;
const float s = std::clamp(1.0f - (u / std::max(carry_start_u, 1e-4f)), 0.0f, 1.0f);
const float h = smoothstep01((s - 0.08f) / 0.84f);
float lift = 1.0f;
if (s < 0.25f) {
lift = smoothstep01(s / 0.25f);
} else if (s > 0.75f) {
lift = smoothstep01((1.0f - s) / 0.25f);
}
Vec3 next_pos;
next_pos.x = lerp(c.recovery_pos.x, c.recovery_target_pos.x, h);
next_pos.z = lerp(c.recovery_pos.z, c.recovery_target_pos.z, h);
const float base_y = lerp(c.recovery_pos.y, c.recovery_target_pos.y, h);
next_pos.y = lerp(base_y, c.recovery_cruise_y, lift);
float live_floor = 0.0f, live_ceil = 1e6f;
level.sample_surface_and_ceiling(next_pos.x, next_pos.z, next_pos.y, live_floor, live_ceil);
next_pos.y = std::max(next_pos.y, live_floor + sp_rec.hover_offset);
if (live_ceil < 1e5f && next_pos.y < live_ceil + 0.5f) {
const float max_under = std::max(live_floor + 0.14f, live_ceil - 0.55f);
next_pos.y = std::min(next_pos.y, max_under);
}
const float yaw_s = smoothstep01((s - 0.10f) / 0.78f);
const float lvl_s = smoothstep01(s / 0.25f);
c.curr_pose.pos = next_pos;
c.curr_pose.yaw = lerp_angle(c.recovery_start_yaw, c.recovery_target_yaw, yaw_s);
c.curr_pose.pitch = lerp(c.recovery_start_pitch, 0.0f, lvl_s);
c.curr_pose.roll = lerp(c.recovery_start_roll, 0.0f, lvl_s);
const Vec3 dfwd(std::sin(c.curr_pose.yaw), 0.0f, std::cos(c.curr_pose.yaw));
const Vec3 delta = c.curr_pose.pos - prev_pos;
const float fwd_spd = (dt > 1e-5f)
? ((delta.x * dfwd.x + delta.z * dfwd.z) / dt)
: 0.0f;
const float tilt_fade = std::clamp((1.0f - s) / 0.15f, 0.0f, 1.0f);
c.recovery_drone_curr_pose = {
c.curr_pose.pos + Vec3(0.0f, 1.05f, 0.0f),
c.curr_pose.yaw,
std::clamp(fwd_spd * 0.012f, -0.18f, 0.18f) * tilt_fade,
0.0f,
};
}
if (c.recovery_timer > 0.0f && c.shield <= 0.1f &&
(c.recovery_state != RecoveryState::Repairing || u > 0.25f)) {
int rec_ticks = static_cast<int>(std::lround(c.recovery_timer * 60.0f));
if (rec_ticks % 4 == 0) {
float h0 = fx_hash(pos_seed_u32(c.curr_pose.pos, static_cast<uint32_t>(rec_ticks)));
float ang = h0 * kTwoPi;
Vec3 p = c.curr_pose.pos + Vec3(std::cos(ang) * 0.22f, 0.25f, std::sin(ang) * 0.22f);
Vec3 v(std::cos(ang) * 0.45f, 2.4f + 0.6f * h0, std::sin(ang) * 0.45f);
spawn_smoke_puff(particles, p, v, 0.42f, 2.15f,
Vec4(0.14f, 0.14f, 0.16f, 0.78f), 1.25f, 1.6f, 1.8f, h0);
}
if (rec_ticks % 7 == 0) {
float h1 = fx_hash(pos_seed_u32(c.curr_pose.pos, static_cast<uint32_t>(rec_ticks + 91)));
particles.push_back({c.curr_pose.pos + Vec3(0.0f, 0.25f, 0.0f),
Vec3((h1 - 0.5f) * 0.6f, 1.8f, (h1 - 0.5f) * 0.6f),
0.32f, Vec4(1.0f, 0.78f, 0.30f, 0.95f), 0.34f, 0.34f,
ParticleKind::Fireball, 0.32f, 0.80f, 1.5f, 1.4f, h1, 0.95f});
}
2026-11-30 22:31:51 +02:00
}
continue;
}
// Trigger repair if fuel completely depleted and stopped
if (setup.fuel_enabled && c.fuel <= 0.05f && c.speed_horizontal() < 0.8f) {
begin_recovery_sequence(c, true, 3.0f);
2026-11-30 22:31:51 +02:00
if (c.is_human) {
events.push_back({SimSoundCue::Warning, c.curr_pose.pos, c.player_index, "CALLING REPAIR", 0.2f});
}
continue;
}
if (!setup.weapons_enabled) {
c.lock_target_id = -1;
c.lock_progress = 0.0f;
continue;
}
// Update missile lock-on cone (requires line-of-sight)
2026-11-30 22:31:51 +02:00
Vec3 fwd = c.forward_dir();
const Vec3 eye_a = c.curr_pose.pos + Vec3(0.0f, 0.25f, 0.0f);
2026-11-30 22:31:51 +02:00
int best_tgt = -1;
float best_score = 0.72f; // ~44 deg half-cone
for (size_t j = 0; j < crafts.size(); ++j) {
if (i == j || crafts[j].recovery_state != RecoveryState::Active ||
crafts[j].invisible_timer > 0.0f) {
continue;
}
2026-11-30 22:31:51 +02:00
Vec3 to_tgt = crafts[j].curr_pose.pos - c.curr_pose.pos;
float dist = to_tgt.len();
if (dist < 1.0f || dist > 38.0f) continue;
float align = (to_tgt / dist).dot(fwd);
if (align > best_score &&
has_line_of_sight(level, eye_a, crafts[j].curr_pose.pos + Vec3(0.0f, 0.25f, 0.0f))) {
2026-11-30 22:31:51 +02:00
best_score = align;
best_tgt = static_cast<int>(j);
}
}
if (best_tgt >= 0) {
if (c.lock_target_id != best_tgt) {
c.lock_target_id = best_tgt;
c.lock_progress = 0.0f;
}
float prev_lock = c.lock_progress;
float lock_speed = 1.4f + 0.4f * static_cast<float>(c.missile_level - 1);
c.lock_progress = std::min(1.0f, c.lock_progress + lock_speed * dt);
if (prev_lock < 1.0f && c.lock_progress >= 1.0f && c.is_human) {
events.push_back({SimSoundCue::LockOn, c.curr_pose.pos, c.player_index, "", 0.1f});
}
} else {
c.lock_target_id = -1;
c.lock_progress = 0.0f;
}
const CraftInput& in = (i < inputs.size()) ? inputs[i] : CraftInput{};
const VehicleSpec& sp = kVehicleSpecs[std::clamp(c.vehicle_idx, 0, 5)];
// Fire Minigun
if (in.fire_minigun && c.minigun_cd <= 0.0f && c.ammo >= 1.0f) {
c.ammo -= 1.0f;
c.minigun_cd = std::max(0.06f, 0.13f - 0.025f * static_cast<float>(c.minigun_level - 1));
c.shots_fired += 1;
float dmg = sp.minigun_damage * (1.0f + 0.25f * static_cast<float>(c.minigun_level - 1));
Projectile p;
p.uid = uid_counter++;
2026-11-30 22:31:51 +02:00
p.active = true;
p.is_missile = false;
p.owner_id = c.id;
p.pos = c.curr_pose.pos + fwd * 0.7f + Vec3(0, 0.1f, 0);
p.vel = fwd * 48.0f + c.vel * 0.35f;
p.yaw = c.curr_pose.yaw;
p.damage = dmg;
p.life = 1.1f;
projectiles.push_back(p);
events.push_back({SimSoundCue::MinigunShot, c.curr_pose.pos, c.player_index, "", 0.08f});
}
// Fire Homing Missile
if (in.fire_missile && c.missile_cd <= 0.0f && c.ammo >= 12.0f) {
c.ammo -= 12.0f;
c.missile_cd = std::max(0.45f, 0.85f - 0.14f * static_cast<float>(c.missile_level - 1));
c.shots_fired += 1;
float dmg = sp.missile_damage * (1.0f + 0.22f * static_cast<float>(c.missile_level - 1));
Projectile p;
p.uid = uid_counter++;
2026-11-30 22:31:51 +02:00
p.active = true;
p.is_missile = true;
p.owner_id = c.id;
p.target_id = (c.lock_progress >= 0.75f) ? c.lock_target_id : -1;
p.pos = c.curr_pose.pos + fwd * 0.85f + Vec3(0, 0.15f, 0);
p.vel = fwd * 30.0f + c.vel * 0.3f;
p.yaw = c.curr_pose.yaw;
p.damage = dmg;
p.life = 2.8f;
projectiles.push_back(p);
events.push_back({SimSoundCue::MissileLaunch, c.curr_pose.pos, c.player_index, "", 0.30f});
}
}
// 2. Step Projectiles & Hit Detection
for (auto& pr : projectiles) {
if (!pr.active) continue;
pr.life -= dt;
if (pr.life <= 0.0f) {
pr.active = false;
continue;
}
if (pr.is_missile && pr.target_id >= 0 && pr.target_id < static_cast<int>(crafts.size())) {
const CraftState& tgt = crafts[pr.target_id];
if (tgt.recovery_state == RecoveryState::Active) {
Vec3 to_tgt = (tgt.curr_pose.pos - pr.pos).normalized();
Vec3 dir = pr.vel.normalized();
float spd = pr.vel.len();
Vec3 steered = lerp(dir, to_tgt, std::min(1.0f, dt * 6.5f)).normalized();
pr.vel = steered * std::max(spd, 28.0f);
pr.yaw = std::atan2(steered.x, steered.z);
}
}
if (pr.is_missile) {
Vec3 mdir = pr.vel.normalized();
float seed = fx_hash(pos_seed_u32(pr.pos, static_cast<uint32_t>(pr.owner_id + 17)));
spawn_smoke_puff(particles, pr.pos - mdir * 0.22f,
-mdir * 1.4f + Vec3((seed - 0.5f) * 0.4f, 0.45f, (seed - 0.5f) * 0.4f),
0.20f, 0.90f, Vec4(0.76f, 0.77f, 0.79f, 0.64f), 0.62f,
3.5f, 0.75f, seed);
particles.push_back({pr.pos - mdir * 0.14f, Vec3(0, 0.1f, 0),
0.24f, Vec4(1.0f, 0.78f, 0.30f, 0.95f), 0.08f, 0.08f,
ParticleKind::AdditiveGlow, 0.24f, 0.07f, 0.0f, 0.0f, seed, 0.95f});
} else {
particles.push_back({pr.pos, Vec3(0, 0, 0), 0.14f, Vec4(1.0f, 0.95f, 0.4f, 0.9f),
0.06f, 0.06f, ParticleKind::AdditiveGlow, 0.14f, 0.06f,
0.0f, 0.0f, 0.0f, 0.9f});
2026-11-30 22:31:51 +02:00
}
Vec3 prev_pos = pr.pos;
2026-11-30 22:31:51 +02:00
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;
}
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});
}
2026-11-30 22:31:51 +02:00
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)];
2026-11-30 22:31:51 +02:00
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;
2026-11-30 22:31:51 +02:00
if (pr.is_missile) {
spawn_explosion_fx(particles, pr.pos, 0.95f);
2026-11-30 22:31:51 +02:00
events.push_back({SimSoundCue::Explosion, pr.pos, -1, "", 0.0f});
} else {
spawn_impact_fx(particles, pr.pos, false);
2026-11-30 22:31:51 +02:00
}
continue;
}
}
projectiles.erase(
std::remove_if(projectiles.begin(), projectiles.end(),
[](const Projectile& p) { return !p.active; }),
projectiles.end());
// 3. Pickups collection & respawn
for (auto& pk : pickups) {
if (!pk.group_unlocked) {
continue;
}
2026-11-30 22:31:51 +02:00
if (!pk.active) {
pk.respawn_timer -= dt;
if (pk.respawn_timer <= 0.0f) pk.active = true;
continue;
}
for (auto& c : crafts) {
if (c.recovery_state != RecoveryState::Active) continue;
const VehicleSpec& sp = kVehicleSpecs[std::clamp(c.vehicle_idx, 0, 5)];
float pick_r = sp.collision_radius + 0.45f;
Vec3 d = c.curr_pose.pos - pk.pos;
if (d.x * d.x + d.z * d.z < pick_r * pick_r && std::abs(d.y) < 1.5f) {
pk.active = false;
pk.respawn_timer = 15.0f;
apply_pickup(c, pk.subtype, events);
break;
}
}
}
// 4. Prop collision & respawn
for (auto& prop : props) {
if (!prop.alive) {
prop.respawn_timer -= dt;
if (prop.respawn_timer <= 0.0f) {
prop.alive = true;
prop.hp = 25.0f;
}
continue;
}
for (auto& c : crafts) {
if (c.recovery_state != RecoveryState::Active) continue;
const VehicleSpec& sp = kVehicleSpecs[std::clamp(c.vehicle_idx, 0, 5)];
float prop_r = sp.collision_radius + 0.24f;
Vec3 d = c.curr_pose.pos - prop.pos;
if (d.x * d.x + d.z * d.z < prop_r * prop_r && std::abs(d.y) < 1.2f) {
prop.alive = false;
prop.respawn_timer = 25.0f;
if (prop.model_key.rfind("BAREL", 0) == 0) {
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);
2026-11-30 22:31:51 +02:00
events.push_back({SimSoundCue::Explosion, prop.pos,
c.is_human ? c.player_index : -1, "", 0.45f});
} else {
events.push_back({SimSoundCue::Impact, prop.pos,
c.is_human ? c.player_index : -1, "", 0.12f});
}
break;
}
}
}
}
std::vector<RecoveryVisual> get_recovery_visuals(const std::vector<CraftState>& crafts, float alpha) {
2026-11-30 22:31:51 +02:00
std::vector<RecoveryVisual> out;
const float t = std::clamp(alpha, 0.0f, 1.0f);
2026-11-30 22:31:51 +02:00
for (const auto& c : crafts) {
const bool recovering = (c.recovery_state == RecoveryState::CallingRepair ||
c.recovery_state == RecoveryState::AwaitingRepair ||
c.recovery_state == RecoveryState::Repairing);
const bool departing = (c.recovery_state == RecoveryState::Active && c.recovery_depart_timer > 0.0f);
if (recovering || departing) {
2026-11-30 22:31:51 +02:00
RecoveryVisual rv;
rv.active = true;
rv.pos = lerp(c.recovery_drone_prev_pose.pos, c.recovery_drone_curr_pose.pos, t);
rv.yaw = lerp_angle(c.recovery_drone_prev_pose.yaw, c.recovery_drone_curr_pose.yaw, t);
rv.pitch = lerp(c.recovery_drone_prev_pose.pitch, c.recovery_drone_curr_pose.pitch, t);
rv.roll = lerp(c.recovery_drone_prev_pose.roll, c.recovery_drone_curr_pose.roll, t);
2026-11-30 22:31:51 +02:00
out.push_back(rv);
}
}
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;
}
begin_recovery_sequence(craft, false, 1.6f);
if (craft.is_human) {
events.push_back({SimSoundCue::Warning, craft.curr_pose.pos, craft.player_index,
"CALLING RECOVERY", 0.18f});
}
return true;
}
2026-11-30 22:31:51 +02:00
} // namespace top_octane::sim