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

440 lines
18 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>
namespace top_octane::sim {
namespace {
void spawn_explosion(std::vector<ParticleBurst>& particles, const Vec3& pos, float scale = 1.0f) {
for (int k = 0; k < 10; ++k) {
float ang = kTwoPi * (static_cast<float>(k) / 10.0f);
float spd = (1.5f + static_cast<float>((k * 37) % 10) * 0.28f) * scale;
Vec3 v(std::cos(ang) * spd, (1.2f + static_cast<float>(k % 3) * 0.9f) * scale,
std::sin(ang) * spd);
Vec4 col = (k % 2 == 0) ? Vec4(1.0f, 0.6f, 0.15f, 1.0f) : Vec4(1.0f, 0.25f, 0.05f, 1.0f);
particles.push_back({pos, v, 0.55f * scale, col, 0.45f, 0.45f});
}
}
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(particles, victim.curr_pose.pos, 1.5f);
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) {
// Map Bullfrog type-5 subtypes to authentic Hi-Octane powerups
std::string msg;
SimSoundCue cue = SimSoundCue::Pickup;
switch (subtype) {
case 0:
case 1:
c.fuel = std::min(c.max_fuel, c.fuel + 35.0f);
msg = "FUEL";
break;
case 2:
c.max_fuel = std::min(220.0f, c.max_fuel + 25.0f);
c.fuel = c.max_fuel;
msg = "EXTRA FUEL";
cue = SimSoundCue::Powerup;
break;
case 3:
c.shield = std::min(c.max_shield, c.shield + 40.0f);
msg = "SHIELD";
break;
case 4:
case 5:
c.max_shield = std::min(260.0f, c.max_shield + 30.0f);
c.shield = c.max_shield;
msg = "EXTRA SHIELD";
cue = SimSoundCue::Powerup;
break;
case 6:
case 7:
c.ammo = std::min(c.max_ammo, c.ammo + 45.0f);
msg = "AMMO";
break;
case 8:
case 9:
c.max_ammo = std::min(250.0f, c.max_ammo + 30.0f);
c.ammo = c.max_ammo;
msg = "EXTRA AMMO";
cue = SimSoundCue::Powerup;
break;
case 10:
c.booster_level = std::min(3, c.booster_level + 1);
c.fuel = std::min(c.max_fuel, c.fuel + 30.0f);
msg = "BOOSTER UPGRADED";
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:
case 13:
default:
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;
}
events.push_back({cue, c.curr_pose.pos, c.is_human ? c.player_index : -1,
c.is_human ? msg : "", 0.25f});
}
} // namespace
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) {
// 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 gently at nearest main waypoint if available
if (!level.main_waypoints.empty()) {
const auto& wp = level.main_waypoints[c.current_wp % level.main_waypoints.size()];
const auto& wpn = level.main_waypoints[(c.current_wp + 1) % level.main_waypoints.size()];
c.curr_pose.pos = Vec3(wp.x, level.sample_height_world(wp.x, wp.z) + 0.45f, wp.z);
c.curr_pose.yaw = std::atan2(wpn.x - wp.x, wpn.z - wp.z);
c.prev_pose = c.curr_pose;
c.vel = Vec3(0, 0, 0);
}
}
continue;
}
// Trigger repair if fuel completely depleted and stopped
if (c.fuel <= 0.05f && c.speed_horizontal() < 0.8f) {
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;
}
// 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) continue;
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);
}
particles.push_back({pr.pos, Vec3(0, 0.3f, 0), 0.24f, Vec4(0.95f, 0.8f, 0.45f, 0.8f), 0.18f, 0.18f});
} else if (!pr.is_missile) {
particles.push_back({pr.pos, Vec3(0, 0, 0), 0.14f, Vec4(1.0f, 0.95f, 0.4f, 0.9f), 0.06f, 0.06f});
}
pr.pos += pr.vel * dt;
// World/Column/Ground impact
float gy = level.sample_height_world(pr.pos.x, pr.pos.z);
if (pr.pos.y <= gy ||
level.is_cell_solid_at_y(static_cast<int>(std::floor(pr.pos.x)),
static_cast<int>(std::floor(pr.pos.z)), pr.pos.y)) {
pr.active = false;
if (pr.is_missile) {
spawn_explosion(particles, pr.pos, 0.9f);
events.push_back({SimSoundCue::Explosion, pr.pos, -1, "", 0.0f});
} else {
particles.push_back({pr.pos, Vec3(0, 1.0f, 0), 0.22f, Vec4(1.0f, 0.8f, 0.3f, 0.9f), 0.16f, 0.16f});
}
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;
victim.shield -= pr.damage;
victim.armour_used += pr.damage;
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(particles, pr.pos, 1.15f);
events.push_back({SimSoundCue::Explosion, pr.pos,
victim.is_human ? victim.player_index : -1, "", 0.65f});
} else {
particles.push_back({pr.pos, Vec3(0, 1.2f, 0), 0.28f, Vec4(1.0f, 0.6f, 0.2f, 1.0f), 0.20f, 0.20f});
events.push_back({SimSoundCue::Impact, pr.pos,
victim.is_human ? victim.player_index : -1, "", 0.20f});
}
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(particles, prop.pos, 0.85f);
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.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) {
c.shield = std::max(5.0f, c.shield - 12.0f);
spawn_explosion(particles, prop.pos, 0.9f);
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