From 8e6bb5540bac535f206bc37d722b0707c05de115 Mon Sep 17 00:00:00 2001 From: Luxferre Date: Tue, 1 Dec 2026 21:39:10 +0200 Subject: [PATCH] Rebalance Medium AI difficulty pacing and combat aggression --- src/sim/ai_driver.cpp | 97 ++++++++++++++++++++++++++++++++--------- tests/test_gameplay.cpp | 85 ++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 21 deletions(-) diff --git a/src/sim/ai_driver.cpp b/src/sim/ai_driver.cpp index 468ad97..ae22dbb 100644 --- a/src/sim/ai_driver.cpp +++ b/src/sim/ai_driver.cpp @@ -201,14 +201,20 @@ CraftInput compute_ai_input(CraftState& ai, const assets::LevelData& level, return out; } - // Portal-safe forward waypoint advance + // Portal-safe forward waypoint advance (tighter apex adherence on sharp hairpin turns) + int prev_wp_idx = (wp_idx + n_wp - 1) % n_wp; int next_wp_idx = (wp_idx + 1) % n_wp; + Vec2 prev_wp_pos(level.main_waypoints[prev_wp_idx].x, level.main_waypoints[prev_wp_idx].z); Vec2 next_wp_pos(level.main_waypoints[next_wp_idx].x, level.main_waypoints[next_wp_idx].z); + Vec2 seg_from_prev = cur_wp_pos - prev_wp_pos; Vec2 seg_to_next = next_wp_pos - cur_wp_pos; + const bool sharp_hairpin = + (seg_from_prev.len_sq() > 0.25f && seg_to_next.len_sq() > 0.25f && + seg_from_prev.normalized().dot(seg_to_next.normalized()) < 0.25f); float dist_to_next = (next_wp_pos - cur_pos).len(); bool passed_geom = ((cur_pos - cur_wp_pos).dot(seg_to_next) > 0.0f) && (dist_to_next < dist_to_wp); - bool advance_wp = (dist_to_wp < 3.4f); - if (!advance_wp && (dist_to_wp < 5.2f || passed_geom)) { + bool advance_wp = (dist_to_wp < (sharp_hairpin ? 1.8f : 3.4f)); + if (!advance_wp && ((!sharp_hairpin && dist_to_wp < 5.2f) || passed_geom)) { advance_wp = has_clear_corridor(cur_pos, next_wp_pos, ai.curr_pose.pos.y); } @@ -330,8 +336,27 @@ CraftInput compute_ai_input(CraftState& ai, const assets::LevelData& level, out.steer = std::clamp(-err * 1.85f, -1.0f, 1.0f); - float diff_pace = (difficulty == 0) ? 0.78f : (difficulty == 2 ? 1.0f : 0.90f); - bool is_corner = (level.main_waypoints[wp_idx].subtype == 1); + struct DifficultyProfile { + float pace; + float speed_cap; + float corner_frac; + float min_ammo; + float gun_range; + float gun_cone; + }; + static constexpr DifficultyProfile kDifficultyProfiles[3] = { + {0.72f, 0.74f, 0.58f, 20.0f, 14.0f, 0.965f}, // Easy + {0.82f, 0.83f, 0.65f, 15.0f, 17.5f, 0.955f}, // Medium + {1.00f, 1.00f, 0.74f, 5.0f, 22.0f, 0.930f}, // Hard + }; + const DifficultyProfile& prof = kDifficultyProfiles[std::clamp(difficulty, 0, 2)]; + + const bool is_corner = (level.main_waypoints[wp_idx].subtype == 1); + const Vec3 fwd_u = ai.forward_dir(); + const Vec3 surf_n = level.sample_normal_world(ai.curr_pose.pos.x, ai.curr_pose.pos.z); + const bool steep_straight_ramp = + !ai.airborne && !is_corner && std::abs(err) < 0.22f && + ((surf_n.x * fwd_u.x + surf_n.z * fwd_u.z) < -0.18f); if (charging_pit) { out.throttle = (spd > 3.5f) ? 0.0f : 0.18f; @@ -340,21 +365,40 @@ CraftInput compute_ai_input(CraftState& ai, const assets::LevelData& level, out.throttle = 0.0f; out.brake = 0.65f; } else if (std::abs(err) > 0.65f && spd > spec.top_speed * 0.52f) { - out.throttle = 0.25f * diff_pace; + out.throttle = 0.25f * prof.pace; out.brake = 0.35f; - } else if (is_corner && spd > spec.top_speed * 0.72f * diff_pace) { - out.throttle = 0.50f * diff_pace; + } else if (is_corner && spd > spec.top_speed * prof.corner_frac) { + out.throttle = 0.50f * prof.pace; } else { - out.throttle = diff_pace; - if (difficulty >= 1 && std::abs(err) < 0.18f && ai.fuel > ai.max_fuel * 0.45f && - (ai.id + static_cast(ai.race_time)) % 3 == 0) { - out.boost = true; + out.throttle = steep_straight_ramp ? 1.0f : prof.pace; + if (difficulty >= 2) { + if (std::abs(err) < 0.18f && ai.fuel > ai.max_fuel * 0.45f && + (ai.id + static_cast(ai.race_time)) % 3 == 0) { + out.boost = true; + } + } else if (difficulty == 1) { + if (std::abs(err) < 0.10f && ai.fuel > ai.max_fuel * 0.72f && + spd < spec.top_speed * 0.86f && + static_cast(ai.race_time * 3.0f) % 24 == ((ai.id * 3) % 24)) { + out.boost = true; + } } } - // Combat AI: shoot visible opponents in sights! - if (ai.ammo > 5.0f) { - Vec3 fwd = ai.forward_dir(); + // Smooth cruise speed ceiling so Easy and Medium AI race clearly below Hard pace + // while preserving full thrust on steep straight jump ramps (e.g. Arctic Land bridge). + if (prof.speed_cap < 0.99f && !out.boost && !steep_straight_ramp && out.throttle > 0.08f) { + const float target_spd = spec.top_speed * prof.speed_cap; + if (spd > target_spd) { + const float hold_throttle = + std::clamp((0.55f * target_spd) / std::max(spec.accel, 1.0f), 0.10f, prof.pace); + const float over = std::clamp((spd - target_spd) / 2.5f, 0.0f, 1.0f); + out.throttle = std::min(out.throttle, lerp(hold_throttle, 0.08f, over)); + } + } + + // Combat AI: shoot visible opponents in sights (tiered by difficulty) + if (ai.ammo > prof.min_ammo) { const Vec3 eye_a = ai.curr_pose.pos + Vec3(0.0f, 0.25f, 0.0f); for (const auto& other : all_crafts) { if (other.id == ai.id || other.recovery_state != RecoveryState::Active || @@ -363,16 +407,27 @@ CraftInput compute_ai_input(CraftState& ai, const assets::LevelData& level, } Vec3 to_e = other.curr_pose.pos - ai.curr_pose.pos; float dist = to_e.len(); - if (dist < 1.2f || dist > 22.0f) continue; - float cos_ang = (to_e / dist).dot(fwd); - if (cos_ang > 0.93f && difficulty >= 0 && + if (dist < 1.2f || dist > prof.gun_range) continue; + float cos_ang = (to_e / dist).dot(fwd_u); + if (cos_ang > prof.gun_cone && has_line_of_sight(level, eye_a, other.curr_pose.pos + Vec3(0.0f, 0.25f, 0.0f))) { - out.fire_minigun = (difficulty == 2 || (static_cast(ai.race_time * 10.0f) % 2 == 0)); + if (difficulty >= 2) { + out.fire_minigun = true; + } else if (difficulty == 1) { + out.fire_minigun = (static_cast(ai.race_time * 4.0f) % 4 == 0); + } else { + out.fire_minigun = (static_cast(ai.race_time * 3.0f) % 5 == 0); + } if (out.fire_minigun) break; } } - if (ai.lock_progress >= 1.0f && difficulty >= 1 && ai.ammo >= 20.0f) { - out.fire_missile = true; + if (ai.lock_progress >= 1.0f) { + if (difficulty >= 2 && ai.ammo >= 20.0f) { + out.fire_missile = true; + } else if (difficulty == 1 && ai.ammo >= 36.0f && + static_cast(ai.race_time * 2.0f) % 14 == (ai.id % 14)) { + out.fire_missile = true; + } } } diff --git a/tests/test_gameplay.cpp b/tests/test_gameplay.cpp index 5c6bf06..613c12a 100644 --- a/tests/test_gameplay.cpp +++ b/tests/test_gameplay.cpp @@ -2,6 +2,7 @@ #include "core/clock.hpp" #include "core/vfs.hpp" #include "game/game_modes.hpp" +#include "sim/ai_driver.hpp" #include "sim/combat.hpp" #include "sim/race_sim.hpp" @@ -619,6 +620,7 @@ int main() { sp.total_laps = 1; sp.difficulty = 1; sp.num_ai = 2; + sp.p2_vehicle = sp.p1_vehicle; sim::RaceSim sim_sp; CHECK(sim_sp.start_race(lvl3, sp)); @@ -1676,5 +1678,88 @@ int main() { std::cout << "[PASS] Championship completion leaderboard default page & tie-break ranking validated\n"; } + // 17. Difficulty pacing & combat aggressiveness ordering (Easy / Medium / Hard) + { + struct PaceResult { + bool finished = false; + float race_time = 0.0f; + }; + auto run_solo_2lap = [&](int difficulty) -> PaceResult { + sim::RaceSetup setup; + setup.mode = sim::GameMode::SingleRace; + setup.track_number = 1; + setup.total_laps = 2; + setup.num_ai = 0; + setup.difficulty = difficulty; + setup.p1_vehicle = 0; // KD-1 + setup.weapons_enabled = false; + setup.damage_enabled = false; + + sim::RaceSim sim; + if (!sim.start_race(lvl1, setup)) return {}; + const int max_ticks = static_cast(120.0f / dt); + for (int t = 0; t < max_ticks && !sim.race_finished(); ++t) { + CraftInput p1 = sim::compute_ai_input(sim.crafts_mut()[0], sim.level(), + sim.crafts(), setup.difficulty, dt); + sim.step(p1, CraftInput{}, dt); + } + return {sim.race_finished(), sim.crafts()[0].race_time}; + }; + + const PaceResult easy = run_solo_2lap(0); + const PaceResult med = run_solo_2lap(1); + const PaceResult hard = run_solo_2lap(2); + CHECK(easy.finished); + CHECK(med.finished); + CHECK(hard.finished); + std::cout << " Solo 2-lap KD-1 (Track 1): Easy=" << easy.race_time + << "s, Medium=" << med.race_time << "s, Hard=" << hard.race_time << "s\n"; + CHECK(easy.race_time > med.race_time + 1.5f); + CHECK(med.race_time > hard.race_time + 2.5f); + + struct CombatResult { + bool finished = false; + int ai_shots = 0; + }; + auto run_combat_2lap_shots = [&](int difficulty) -> CombatResult { + sim::RaceSetup setup; + setup.mode = sim::GameMode::SingleRace; + setup.track_number = 1; + setup.total_laps = 2; + setup.num_ai = 7; + setup.difficulty = difficulty; + setup.p1_vehicle = 0; // KD-1 + setup.weapons_enabled = true; + setup.damage_enabled = true; + + sim::RaceSim sim; + if (!sim.start_race(lvl1, setup)) return {}; + const int max_ticks = static_cast(120.0f / dt); + for (int t = 0; t < max_ticks && !sim.race_finished(); ++t) { + CraftInput p1 = sim::compute_ai_input(sim.crafts_mut()[0], sim.level(), + sim.crafts(), setup.difficulty, dt); + sim.step(p1, CraftInput{}, dt); + } + int total_ai_shots = 0; + for (size_t i = 1; i < sim.crafts().size(); ++i) { + total_ai_shots += sim.crafts()[i].shots_fired; + } + return {sim.race_finished(), total_ai_shots}; + }; + + const CombatResult easy_c = run_combat_2lap_shots(0); + const CombatResult med_c = run_combat_2lap_shots(1); + const CombatResult hard_c = run_combat_2lap_shots(2); + CHECK(easy_c.finished); + CHECK(med_c.finished); + CHECK(hard_c.finished); + std::cout << " 8-racer 2-lap AI shots (Track 1): Easy=" << easy_c.ai_shots + << ", Medium=" << med_c.ai_shots << ", Hard=" << hard_c.ai_shots << "\n"; + CHECK(static_cast(med_c.ai_shots) < static_cast(hard_c.ai_shots) * 0.75f); + CHECK(easy_c.ai_shots <= hard_c.ai_shots); + + std::cout << "[PASS] Difficulty pacing & combat aggression cleanly separated (Easy / Medium / Hard)\n"; + } + return 0; }