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

668 lines
29 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
}
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;
victim.vel = Vec3(0, 0, 0);
victim.recovery_state = RecoveryState::CallingRepair;
victim.recovery_timer = 3.2f;
victim.recovery_pos = victim.curr_pose.pos;
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
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) {
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_timer -= dt;
if (c.recovery_state == RecoveryState::CallingRepair && c.recovery_timer < 2.2f) {
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});
}
} else if (c.recovery_state == RecoveryState::AwaitingRepair && c.recovery_timer < 1.1f) {
c.recovery_state = RecoveryState::Repairing;
if (c.is_human) {
events.push_back({SimSoundCue::Refuel, c.curr_pose.pos, c.player_index, "REPAIRING", 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
2026-11-30 22:31:51 +02:00
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);
2026-11-30 22:31:51 +02:00
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;
2026-11-30 22:31:51 +02:00
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());
2026-11-30 22:31:51 +02:00
}
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;
c.ai_pit_cooldown = 0.0f;
c.ai_seeking_pit = false;
c.ai_progress_anchor = c.curr_pose.pos;
c.ai_anchor_init = true;
} else if (c.shield <= 0.1f) {
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) {
2026-11-30 22:31:51 +02:00
c.recovery_state = RecoveryState::CallingRepair;
c.recovery_timer = 3.0f;
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;
}
2026-11-30 22:31:51 +02:00
// Update missile lock-on cone
Vec3 fwd = c.forward_dir();
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) {
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.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.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;
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});
}
2026-11-30 22:31:51 +02:00
// 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));
2026-11-30 22:31:51 +02:00
if (pr.pos.y <= gy ||
level.is_cell_solid_at_point(ix, iz, pr.pos.x, pr.pos.z, pr.pos.y, 0.10f)) {
2026-11-30 22:31:51 +02:00
pr.active = false;
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;
}
// 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);
2026-11-30 22:31:51 +02:00
events.push_back({SimSoundCue::Explosion, pr.pos,
victim.is_human ? victim.player_index : -1, "", 0.65f});
} else {
spawn_impact_fx(particles, pr.pos, true);
2026-11-30 22:31:51 +02:00
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;
}
2026-11-30 22:31:51 +02:00
}
apply_damage(victim, pr.owner_id, crafts, setup, particles, events,
out_race_over, out_winner_id);
2026-11-30 22:31:51 +02:00
}
}
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);
2026-11-30 22:31:51 +02:00
events.push_back({SimSoundCue::Explosion, prop.pos, -1, "", 0.25f});
}
break;
}
}
}
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.shield = std::max(5.0f, c.shield - 12.0f);
}
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) {
std::vector<RecoveryVisual> out;
for (const auto& c : crafts) {
if (c.recovery_state == RecoveryState::CallingRepair ||
c.recovery_state == RecoveryState::AwaitingRepair ||
c.recovery_state == RecoveryState::Repairing) {
RecoveryVisual rv;
rv.active = true;
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);
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);
hover_y = 1.05f + t * 0.55f;
} else {
hover_y = 1.05f;
}
rv.pos = c.curr_pose.pos + Vec3(0.0f, hover_y, 0.0f);
rv.yaw = c.curr_pose.yaw;
out.push_back(rv);
}
}
return out;
}
} // namespace top_octane::sim