diff --git a/src/game/game_app.cpp b/src/game/game_app.cpp index d9b5164..717ad9a 100644 --- a/src/game/game_app.cpp +++ b/src/game/game_app.cpp @@ -1421,8 +1421,8 @@ void GameApp::render_frame(float alpha) { frame_models_.push_back({sim::model_key_for(rec.vehicle_idx, rec.team_idx), gp.pos, gp.yaw, gp.pitch, gp.roll, gscale, Vec4(0.45f, 0.85f, 1.0f, 0.45f)}); } - for (const auto& rv : sim::get_recovery_visuals(race_sim_.crafts())) { - frame_models_.push_back({"RECOV0-0", rv.pos, rv.yaw, 0.0f, 0.0f, + for (const auto& rv : sim::get_recovery_visuals(race_sim_.crafts(), alpha)) { + frame_models_.push_back({"RECOV0-0", rv.pos, rv.yaw, rv.pitch, rv.roll, sim::kRecoveryVisualScale, Vec4(1, 1, 1, 1)}); } for (const auto& pr : race_sim_.props()) { @@ -1895,7 +1895,7 @@ void GameApp::render_hud_viewport(const sim::CraftState& craft, const render::Re float cy = oy + 126.0f * hs; if (race_sim_.countdown_active() || (!race_sim_.countdown_active() && craft.race_time < 0.85f)) { render_countdown_lights(cx, cy, hs); - } else if (craft.wrong_way) { + } else if (craft.wrong_way && selected_mode_ != sim::GameMode::DeathMatch) { ui_ren_.draw_text_centered_fit(cx, cy + 28.0f * s, static_cast(vw) * 0.75f, 2.6f * s, str.wrong_way, Vec4(1.0f, 0.22f, 0.20f, 1.0f), true, render::UiFont::MenuLarge); } else { diff --git a/src/sim/combat.cpp b/src/sim/combat.cpp index 26450df..80a3bdb 100644 --- a/src/sim/combat.cpp +++ b/src/sim/combat.cpp @@ -33,6 +33,97 @@ float closest_segment_t(const Vec3& a, const Vec3& b, const Vec3& p, Vec3& out_p 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& 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(k) / static_cast(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(std::floor(sp.x)), 0, assets::kMapWidth - 1); + const int iz = std::clamp(static_cast(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(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); + } +} + void apply_damage(CraftState& victim, int attacker_id, const std::vector& crafts, const RaceSetup& setup, std::vector& particles, std::vector& events, bool& out_race_over, int& out_winner_id) { @@ -45,14 +136,7 @@ void apply_damage(CraftState& victim, int attacker_id, const std::vector= 0 && attacker_id < static_cast(crafts.size())) { @@ -315,24 +399,55 @@ void step_combat_and_world( 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; + } + if (c.recovery_state != RecoveryState::Active) { c.recovery_timer -= dt; - 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) { + 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) { 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}); } - } else if (c.recovery_state == RecoveryState::AwaitingRepair && u < 0.34f) { + } + if (c.recovery_state == RecoveryState::AwaitingRepair && + u < carry_start_u && c.recovery_timer > 0.0f) { 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}); } - } else if (c.recovery_timer <= 0.0f) { + } + + // 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) { c.recovery_state = RecoveryState::Active; if (c.recovery_refill) { c.shield = c.max_shield; @@ -342,12 +457,118 @@ void step_combat_and_world( } else if (setup.fuel_enabled && c.fuel < 5.0f) { c.fuel = 8.0f; } - place_craft_on_track(c, level, crafts, setup.mode != GameMode::DeathMatch); + + 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; - } else if (c.shield <= 0.1f) { + 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(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(rec_ticks))); @@ -370,13 +591,7 @@ 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; + begin_recovery_sequence(c, true, 3.0f); if (c.is_human) { events.push_back({SimSoundCue::Warning, c.curr_pose.pos, c.player_index, "CALLING REPAIR", 0.2f}); } @@ -716,28 +931,21 @@ void step_combat_and_world( } } -std::vector get_recovery_visuals(const std::vector& crafts) { +std::vector get_recovery_visuals(const std::vector& crafts, float alpha) { std::vector out; + const float t = std::clamp(alpha, 0.0f, 1.0f); for (const auto& c : crafts) { - if (c.recovery_state == RecoveryState::CallingRepair || - c.recovery_state == RecoveryState::AwaitingRepair || - c.recovery_state == RecoveryState::Repairing) { + 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) { 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((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((u - 0.34f) / 0.34f, 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; + 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); out.push_back(rv); } } @@ -749,14 +957,7 @@ bool trigger_track_recovery(CraftState& craft, std::vector& events) { 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; + 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}); diff --git a/src/sim/combat.hpp b/src/sim/combat.hpp index a1dfcbf..b9716b4 100644 --- a/src/sim/combat.hpp +++ b/src/sim/combat.hpp @@ -10,6 +10,8 @@ struct RecoveryVisual { bool active = false; Vec3 pos{0, 0, 0}; float yaw = 0.0f; + float pitch = 0.0f; + float roll = 0.0f; }; // Shared deterministic explosion, billowing smoke & spark VFX helpers @@ -39,8 +41,9 @@ void step_combat_and_world( std::vector* out_projectile_traces = nullptr, uint32_t* io_next_projectile_uid = nullptr); -// Computes visual Recovery Helicopter (RECOV0-0) positions for any craft under repair -std::vector get_recovery_visuals(const std::vector& crafts); +// Computes visual Recovery Helicopter (RECOV0-0) positions for any craft under repair or departing +std::vector get_recovery_visuals(const std::vector& crafts, + float alpha = 1.0f); // Triggers manual track recovery for an active craft without refilling resources bool trigger_track_recovery(CraftState& craft, std::vector& events); diff --git a/src/sim/physics.cpp b/src/sim/physics.cpp index 161655e..fe761cd 100644 --- a/src/sim/physics.cpp +++ b/src/sim/physics.cpp @@ -408,6 +408,7 @@ CraftStepFeedback step_craft_physics(CraftState& craft, const CraftInput& input, bool fuel_enabled) { CraftStepFeedback fb; craft.prev_pose = craft.curr_pose; + craft.recovery_drone_prev_pose = craft.recovery_drone_curr_pose; if (!std::isfinite(dt) || dt <= 1e-6f) { return fb; } @@ -724,40 +725,115 @@ bool resolve_craft_pair_collision(CraftState& a, CraftState& b, float& out_rel_s return true; } -bool place_craft_on_track(CraftState& craft, const assets::LevelData& level, - const std::vector& all_crafts, - bool restrict_to_checkpoint_arc) { +bool is_track_recovery_spot_clear(const CraftState& craft, const assets::LevelData& level, + const std::vector& all_crafts, + float wx, float wz, float& out_y, bool check_crafts) { + if (wx < 2.0f || wx > static_cast(assets::kMapWidth - 3) || + wz < 2.0f || wz > static_cast(assets::kMapHeight - 3)) { + return false; + } const VehicleSpec& spec = kVehicleSpecs[std::clamp(craft.vehicle_idx, 0, 5)]; + const float probe_r = spec.collision_radius + 0.08f; - auto finalize_pose = [&](const Vec3& pos, float yaw) { - craft.curr_pose.pos = pos; - craft.curr_pose.yaw = yaw; - craft.curr_pose.pitch = 0.0f; - craft.curr_pose.roll = 0.0f; - craft.vel = Vec3(0.0f, 0.0f, 0.0f); - craft.yaw_rate = 0.0f; - craft.airborne = false; - craft.airtime = 0.0f; - craft.air_spin_accum = 0.0f; - craft.wrong_way = false; - craft.scrape_intensity = 0.0f; - craft.boost_timer = 0.0f; - float dummy_imp = 0.0f; - bool dummy_scr = false; - resolve_world_collisions(craft, level, dummy_imp, dummy_scr); + float raw_gy = level.sample_height_world(wx, wz); + float gy = level.sample_entity_surface_y(wx, wz); + float fy = 0.0f, cy = 1e6f; + level.sample_surface_and_ceiling(wx, wz, gy + spec.hover_offset, fy, cy); + if (cy - fy < 0.70f) { + return false; + } + bool on_bridge = (fy > raw_gy + 0.5f); + if (!on_bridge && level.sample_normal_world(wx, wz).y < 0.50f) { + return false; + } + const float probe_y = fy + spec.hover_offset; + static constexpr float kOffX[5] = {0.0f, 1.0f, -1.0f, 0.0f, 0.0f}; + static constexpr float kOffZ[5] = {0.0f, 0.0f, 0.0f, 1.0f, -1.0f}; + for (int k = 0; k < 5; ++k) { + float px = wx + kOffX[k] * probe_r; + float pz = wz + kOffZ[k] * probe_r; + if (px < 1.5f || px > static_cast(assets::kMapWidth) - 2.5f || + pz < 1.5f || pz > static_cast(assets::kMapHeight) - 2.5f) { + return false; + } + int ix = static_cast(std::floor(px)); + int iz = static_cast(std::floor(pz)); + if (level.is_cell_solid_at_point(ix, iz, px, pz, probe_y) || + level.is_cliff_blocking_at_y(ix, iz, probe_y)) { + return false; + } + } + if (check_crafts) { + for (const auto& o : all_crafts) { + if (o.id == craft.id) continue; + Vec3 other_pos; + if (o.recovery_state == RecoveryState::Active) { + other_pos = o.curr_pose.pos; + } else if (o.recovery_state == RecoveryState::Repairing && + o.recovery_target_valid && + std::abs(o.recovery_timer - craft.recovery_timer) < 0.45f) { + other_pos = o.recovery_target_pos; + } else { + continue; + } + float dx = other_pos.x - wx; + float dz = other_pos.z - wz; + if (dx * dx + dz * dz < 1.6f * 1.6f && + std::abs(other_pos.y - probe_y) < 1.5f) { + return false; + } + } + } + out_y = probe_y; + return true; +} + +void finalize_craft_recovery_pose(CraftState& craft, const assets::LevelData& level, + const Vec3& pos, float yaw, int waypoint_idx, + bool snap_prev_pose) { + craft.curr_pose.pos = pos; + craft.curr_pose.yaw = yaw; + craft.curr_pose.pitch = 0.0f; + craft.curr_pose.roll = 0.0f; + craft.vel = Vec3(0.0f, 0.0f, 0.0f); + craft.yaw_rate = 0.0f; + craft.airborne = false; + craft.airtime = 0.0f; + craft.air_spin_accum = 0.0f; + craft.wrong_way = false; + craft.scrape_intensity = 0.0f; + craft.boost_timer = 0.0f; + float dummy_imp = 0.0f; + bool dummy_scr = false; + resolve_world_collisions(craft, level, dummy_imp, dummy_scr); + if (snap_prev_pose) { craft.prev_pose = craft.curr_pose; - craft.pit_stuck_timer = 0.0f; - craft.stuck_in_pit_hint = false; - craft.ai_stuck_timer = 0.0f; - craft.ai_unstuck_timer = 0.0f; - craft.ai_progress_timer = 0.0f; - craft.ai_pit_stay_timer = 0.0f; - craft.ai_seeking_pit = false; - craft.ai_progress_anchor = craft.curr_pose.pos; - craft.ai_anchor_init = true; - craft.ai_wp_best_dist = 1e9f; - craft.ai_wp_stall_timer = 0.0f; - }; + } + craft.pit_stuck_timer = 0.0f; + craft.stuck_in_pit_hint = false; + craft.ai_stuck_timer = 0.0f; + craft.ai_unstuck_timer = 0.0f; + craft.ai_progress_timer = 0.0f; + craft.ai_pit_stay_timer = 0.0f; + craft.ai_seeking_pit = false; + craft.ai_progress_anchor = craft.curr_pose.pos; + craft.ai_anchor_init = true; + craft.ai_wp_best_dist = 1e9f; + craft.ai_wp_stall_timer = 0.0f; + if (!level.main_waypoints.empty()) { + const int n_wp = static_cast(level.main_waypoints.size()); + const int wi = ((waypoint_idx % n_wp) + n_wp) % n_wp; + craft.current_wp = wi; + craft.ai_target_wp = (wi + 1) % n_wp; + } +} + +TrackRecoveryTarget compute_track_recovery_target(const CraftState& craft, + const assets::LevelData& level, + const std::vector& all_crafts, + bool restrict_to_checkpoint_arc) { + TrackRecoveryTarget result; + const VehicleSpec& spec = kVehicleSpecs[std::clamp(craft.vehicle_idx, 0, 5)]; if (level.main_waypoints.empty()) { float gx = std::clamp(craft.curr_pose.pos.x, 2.0f, static_cast(assets::kMapWidth - 3)); @@ -765,61 +841,14 @@ bool place_craft_on_track(CraftState& craft, const assets::LevelData& level, float gy = level.sample_entity_surface_y(gx, gz); float fy = 0.0f, cy = 1e6f; level.sample_surface_and_ceiling(gx, gz, gy + spec.hover_offset, fy, cy); - finalize_pose(Vec3(gx, fy + spec.hover_offset, gz), craft.curr_pose.yaw); - return true; + result.pos = Vec3(gx, fy + spec.hover_offset, gz); + result.yaw = craft.curr_pose.yaw; + result.waypoint_idx = 0; + result.valid = true; + return result; } const int n_wp = static_cast(level.main_waypoints.size()); - const float probe_r = spec.collision_radius + 0.08f; - - auto is_spot_clear = [&](float wx, float wz, float& out_y, bool check_crafts) -> bool { - if (wx < 2.0f || wx > static_cast(assets::kMapWidth - 3) || - wz < 2.0f || wz > static_cast(assets::kMapHeight - 3)) { - return false; - } - float raw_gy = level.sample_height_world(wx, wz); - float gy = level.sample_entity_surface_y(wx, wz); - float fy = 0.0f, cy = 1e6f; - level.sample_surface_and_ceiling(wx, wz, gy + spec.hover_offset, fy, cy); - if (cy - fy < 0.70f) { - return false; - } - bool on_bridge = (fy > raw_gy + 0.5f); - if (!on_bridge && level.sample_normal_world(wx, wz).y < 0.50f) { - return false; - } - const float probe_y = fy + spec.hover_offset; - static constexpr float kOffX[5] = {0.0f, 1.0f, -1.0f, 0.0f, 0.0f}; - static constexpr float kOffZ[5] = {0.0f, 0.0f, 0.0f, 1.0f, -1.0f}; - for (int k = 0; k < 5; ++k) { - float px = wx + kOffX[k] * probe_r; - float pz = wz + kOffZ[k] * probe_r; - if (px < 1.5f || px > static_cast(assets::kMapWidth) - 2.5f || - pz < 1.5f || pz > static_cast(assets::kMapHeight) - 2.5f) { - return false; - } - int ix = static_cast(std::floor(px)); - int iz = static_cast(std::floor(pz)); - if (level.is_cell_solid_at_point(ix, iz, px, pz, probe_y) || - level.is_cliff_blocking_at_y(ix, iz, probe_y)) { - return false; - } - } - if (check_crafts) { - for (const auto& o : all_crafts) { - if (o.id == craft.id || o.recovery_state != RecoveryState::Active) continue; - float dx = o.curr_pose.pos.x - wx; - float dz = o.curr_pose.pos.z - wz; - if (dx * dx + dz * dz < 1.6f * 1.6f && - std::abs(o.curr_pose.pos.y - probe_y) < 1.5f) { - return false; - } - } - } - out_y = probe_y; - return true; - }; - auto dist_sq_to_wp = [&](int wi) -> float { float dx = level.main_waypoints[wi].x - craft.curr_pose.pos.x; float dz = level.main_waypoints[wi].z - craft.curr_pose.pos.z; @@ -860,7 +889,7 @@ bool place_craft_on_track(CraftState& craft, const assets::LevelData& level, int wi = (wp_start + step) % n_wp; const auto& w = level.main_waypoints[wi]; float dummy_y = 0.0f; - if (!is_spot_clear(w.x, w.z, dummy_y, false)) continue; + if (!is_track_recovery_spot_clear(craft, level, all_crafts, w.x, w.z, dummy_y, false)) continue; float d2 = dist_sq_to_wp(wi); if (d2 < best_d2) { best_d2 = d2; @@ -879,7 +908,7 @@ bool place_craft_on_track(CraftState& craft, const assets::LevelData& level, for (int wi = 0; wi < n_wp; ++wi) { const auto& w = level.main_waypoints[wi]; float dummy_y = 0.0f; - if (!is_spot_clear(w.x, w.z, dummy_y, false)) continue; + if (!is_track_recovery_spot_clear(craft, level, all_crafts, w.x, w.z, dummy_y, false)) continue; float d2 = dist_sq_to_wp(wi); if (d2 < best_d2) { best_d2 = d2; @@ -928,7 +957,7 @@ bool place_craft_on_track(CraftState& craft, const assets::LevelData& level, float tx = wp.x + off.x; float tz = wp.z + off.y; float ty = 0.0f; - if (is_spot_clear(tx, tz, ty, true)) { + if (is_track_recovery_spot_clear(craft, level, all_crafts, tx, tz, ty, true)) { chosen_x = tx; chosen_z = tz; chosen_y = ty; @@ -938,7 +967,7 @@ bool place_craft_on_track(CraftState& craft, const assets::LevelData& level, } if (!placed_clear) { float ty = 0.0f; - if (is_spot_clear(wp.x, wp.z, ty, false)) { + if (is_track_recovery_spot_clear(craft, level, all_crafts, wp.x, wp.z, ty, false)) { chosen_y = ty; } else { float gy = level.sample_entity_surface_y(wp.x, wp.z); @@ -948,9 +977,20 @@ bool place_craft_on_track(CraftState& craft, const assets::LevelData& level, } } - finalize_pose(Vec3(chosen_x, chosen_y, chosen_z), yaw); - craft.current_wp = best_wp; - craft.ai_target_wp = (best_wp + 1) % n_wp; + result.pos = Vec3(chosen_x, chosen_y, chosen_z); + result.yaw = yaw; + result.waypoint_idx = best_wp; + result.valid = true; + return result; +} + +bool place_craft_on_track(CraftState& craft, const assets::LevelData& level, + const std::vector& all_crafts, + bool restrict_to_checkpoint_arc) { + TrackRecoveryTarget tgt = compute_track_recovery_target( + craft, level, all_crafts, restrict_to_checkpoint_arc); + if (!tgt.valid) return false; + finalize_craft_recovery_pose(craft, level, tgt.pos, tgt.yaw, tgt.waypoint_idx, true); return true; } diff --git a/src/sim/physics.hpp b/src/sim/physics.hpp index 54cc293..b9e49f9 100644 --- a/src/sim/physics.hpp +++ b/src/sim/physics.hpp @@ -79,6 +79,20 @@ struct CraftState { float recovery_cooldown = 0.0f; float pit_stuck_timer = 0.0f; bool stuck_in_pit_hint = false; + float recovery_start_yaw = 0.0f; + float recovery_start_pitch = 0.0f; + float recovery_start_roll = 0.0f; + Vec3 recovery_target_pos{0.0f, 0.0f, 0.0f}; + float recovery_target_yaw = 0.0f; + int recovery_target_wp = 0; + float recovery_cruise_y = 0.0f; + bool recovery_target_valid = false; + CraftPose recovery_drone_prev_pose{}; + CraftPose recovery_drone_curr_pose{}; + float recovery_depart_timer = 0.0f; + float recovery_depart_duration = 0.0f; + Vec3 recovery_depart_start{0.0f, 0.0f, 0.0f}; + float recovery_depart_yaw = 0.0f; // Race / Deathmatch progression int lap = 1; @@ -133,6 +147,13 @@ struct CraftStepFeedback { bool out_of_fuel = false; }; +struct TrackRecoveryTarget { + Vec3 pos{0.0f, 0.0f, 0.0f}; + float yaw = 0.0f; + int waypoint_idx = 0; + bool valid = false; +}; + // Pure deterministic 60 Hz hover physics step for one active craft CraftStepFeedback step_craft_physics(CraftState& craft, const CraftInput& input, const assets::LevelData& level, float dt, @@ -145,6 +166,22 @@ bool resolve_world_collisions(CraftState& craft, const assets::LevelData& level, // Resolves sphere-sphere bounce between two active crafts bool resolve_craft_pair_collision(CraftState& a, CraftState& b, float& out_rel_speed); +// Checks whether a recovery landing spot is drivable, unobstructed, and clear of nearby crafts +bool is_track_recovery_spot_clear(const CraftState& craft, const assets::LevelData& level, + const std::vector& all_crafts, + float wx, float wz, float& out_y, bool check_crafts); + +// Finds the nearest clear, drivable main track waypoint for recovery or drone airlift +TrackRecoveryTarget compute_track_recovery_target(const CraftState& craft, + const assets::LevelData& level, + const std::vector& all_crafts, + bool restrict_to_checkpoint_arc = true); + +// Finalizes craft kinematics, collision projection, and AI/stuck timers at a recovery landing pose +void finalize_craft_recovery_pose(CraftState& craft, const assets::LevelData& level, + const Vec3& pos, float yaw, int waypoint_idx, + bool snap_prev_pose = true); + // Safely places a craft on the nearest clear, drivable main track waypoint bool place_craft_on_track(CraftState& craft, const assets::LevelData& level, const std::vector& all_crafts, diff --git a/src/sim/race_sim.cpp b/src/sim/race_sim.cpp index c1e1aa7..91d9a20 100644 --- a/src/sim/race_sim.cpp +++ b/src/sim/race_sim.cpp @@ -543,6 +543,10 @@ bool RaceSim::request_track_recovery(int player_index) { } void RaceSim::update_checkpoints_and_laps(CraftState& c, float dt) { + if (c.recovery_state != RecoveryState::Active) { + c.wrong_way = false; + return; + } if (level_.checkpoints.empty() || c.finished) return; int num_cps = static_cast(level_.checkpoints.size()); Vec2 p0(c.prev_pose.pos.x, c.prev_pose.pos.z); @@ -577,7 +581,7 @@ void RaceSim::update_checkpoints_and_laps(CraftState& c, float dt) { } else { c.current_lap_time = rem; ++c.lap; - if (c.lap == setup_.total_laps && c.is_human) { + if (setup_.mode != GameMode::DeathMatch && c.lap == setup_.total_laps && c.is_human) { events_.push_back({SimSoundCue::None, c.curr_pose.pos, c.player_index, "FINAL LAP", 0.0f}); } } @@ -598,7 +602,9 @@ void RaceSim::update_checkpoints_and_laps(CraftState& c, float dt) { } } c.current_wp = static_cast(best_wp); - if (c.speed_horizontal() > 3.0f) { + if (setup_.mode == GameMode::DeathMatch) { + c.wrong_way = false; + } else if (c.speed_horizontal() > 3.0f) { const auto& w0 = level_.main_waypoints[best_wp]; const auto& w1 = level_.main_waypoints[(best_wp + 1) % level_.main_waypoints.size()]; Vec2 track_dir = Vec2(w1.x - w0.x, w1.z - w0.z).normalized(); @@ -1016,12 +1022,21 @@ void RaceSim::resnap_entities_in_rects(const std::vector& rects) { for (auto& c : crafts_) { if (!touches_any(c.curr_pose.pos.x, c.curr_pose.pos.z, 1.5f)) continue; const float hover = kVehicleSpecs[std::clamp(c.vehicle_idx, 0, 5)].hover_offset; - if (c.recovery_state != RecoveryState::Active) { + if (c.recovery_state == RecoveryState::CallingRepair || + c.recovery_state == RecoveryState::AwaitingRepair) { float sy = level_.sample_entity_surface_y(c.curr_pose.pos.x, c.curr_pose.pos.z); c.curr_pose.pos.y = sy + hover; c.recovery_pos.y = c.curr_pose.pos.y; continue; } + if (c.recovery_state == RecoveryState::Repairing) { + float sy = level_.sample_entity_surface_y(c.curr_pose.pos.x, c.curr_pose.pos.z); + c.curr_pose.pos.y = std::max(c.curr_pose.pos.y, sy + hover); + continue; + } + if (c.recovery_state != RecoveryState::Active) { + continue; + } float imp = 0.0f; bool scr = false; resolve_world_collisions(c, level_, imp, scr); diff --git a/tests/test_gameplay.cpp b/tests/test_gameplay.cpp index d8aace7..454c69d 100644 --- a/tests/test_gameplay.cpp +++ b/tests/test_gameplay.cpp @@ -1134,14 +1134,53 @@ int main() { } CHECK(saw_recovery_banner); - // Wait ~1.75s for manual recovery sequence (1.6s) to complete - for (int t = 0; t < 105; ++t) rsim.step(CraftInput{}, CraftInput{}, dt); + // Wait ~1.75s for manual recovery sequence (1.6s) to airlift craft smoothly back to track + bool saw_repairing = false; + int repair_move_ticks = 0; + float max_repair_step = 0.0f; + float touchdown_step = -1.0f; + const int cp_before_rec = p1.checkpoints_passed; + sim::RecoveryState prev_rec_state = p1.recovery_state; + Vec3 prev_rec_pos = p1.curr_pose.pos; + for (int t = 0; t < 105; ++t) { + rsim.step(CraftInput{}, CraftInput{}, dt); + const float step_d = (p1.curr_pose.pos - prev_rec_pos).len(); + auto step_rvis = sim::get_recovery_visuals(rsim.crafts()); + if (p1.recovery_state != sim::RecoveryState::Active) { + CHECK(step_rvis.size() == 1); + } + if (p1.recovery_state == sim::RecoveryState::Repairing) { + saw_repairing = true; + CHECK((step_rvis[0].pos - (p1.curr_pose.pos + Vec3(0.0f, 1.05f, 0.0f))).len() < 0.15f); + if (prev_rec_state == sim::RecoveryState::Repairing) { + if (step_d > 0.02f) ++repair_move_ticks; + max_repair_step = std::max(max_repair_step, step_d); + } + } + if (prev_rec_state == sim::RecoveryState::Repairing && + p1.recovery_state == sim::RecoveryState::Active) { + touchdown_step = step_d; + } + prev_rec_state = p1.recovery_state; + prev_rec_pos = p1.curr_pose.pos; + } + CHECK(saw_repairing); + CHECK(repair_move_ticks >= 20); + CHECK(max_repair_step < 1.5f); + CHECK(touchdown_step >= 0.0f && touchdown_step < 0.5f); + CHECK(p1.checkpoints_passed == cp_before_rec); CHECK(p1.recovery_state == sim::RecoveryState::Active); CHECK(p1.deaths == 0); CHECK(p1.curr_pose.pos.y > 0.0f); CHECK(p1.curr_pose.pos.x < 25.0f); CHECK(p1.next_checkpoint == 1); CHECK(!p1.stuck_in_pit_hint); + auto dep_vis0 = sim::get_recovery_visuals(rsim.crafts()); + CHECK(dep_vis0.size() == 1); + CHECK(dep_vis0[0].pos.y > p1.curr_pose.pos.y + 1.05f); + for (int t = 0; t < 5; ++t) rsim.step(CraftInput{}, CraftInput{}, dt); + auto dep_vis1 = sim::get_recovery_visuals(rsim.crafts()); + CHECK(dep_vis1.size() == 1 && dep_vis1[0].pos.y > dep_vis0[0].pos.y); int px = static_cast(std::floor(p1.curr_pose.pos.x)); int pz = static_cast(std::floor(p1.curr_pose.pos.z)); CHECK(!lvl1.is_cell_solid_at_point(px, pz, p1.curr_pose.pos.x, @@ -1159,6 +1198,10 @@ int main() { rsim.step(rec_in, CraftInput{}, dt); CHECK(p1.recovery_state == sim::RecoveryState::Active); + // Departing drone finishes climbing away cleanly while craft races on + for (int t = 0; t < 50; ++t) rsim.step(CraftInput{}, CraftInput{}, dt); + CHECK(sim::get_recovery_visuals(rsim.crafts()).empty()); + // Finished craft cannot recover p1.finished = true; CHECK(!rsim.request_track_recovery(0)); @@ -1310,5 +1353,86 @@ int main() { std::cout << "[PASS] Gameplay dt/AI-basin/invincible/Level9-morph & save hardening validated\n"; } + // 14. DeathMatch suppresses "FINAL LAP" and "WRONG WAY" warnings while SingleRace keeps both + { + sim::RaceSetup dm; + dm.mode = sim::GameMode::DeathMatch; + dm.track_number = 1; + dm.num_ai = 0; + dm.total_laps = 2; + dm.deathmatch_kill_limit = 5; + dm.weapons_enabled = false; + dm.fuel_enabled = false; + sim::RaceSim dm_sim; + CHECK(dm_sim.start_race(lvl1, dm)); + for (int t = 0; t < 200; ++t) dm_sim.step(CraftInput{}, CraftInput{}, dt); + dm_sim.clear_events(); + dm_sim.crafts_mut()[0].autopilot = true; + + bool dm_saw_final_lap = false; + for (int t = 0; t < 2400 && dm_sim.crafts()[0].lap < 3; ++t) { + dm_sim.step(CraftInput{}, CraftInput{}, dt); + for (const auto& ev : dm_sim.events()) { + if (ev.banner_text == "FINAL LAP") dm_saw_final_lap = true; + } + dm_sim.clear_events(); + } + CHECK(dm_sim.crafts()[0].lap >= 3); + CHECK(!dm_sim.crafts()[0].finished); + CHECK(!dm_saw_final_lap); + + sim::RaceSetup sr; + sr.mode = sim::GameMode::SingleRace; + sr.track_number = 1; + sr.num_ai = 0; + sr.total_laps = 2; + sr.weapons_enabled = false; + sr.fuel_enabled = false; + sim::RaceSim sr_sim; + CHECK(sr_sim.start_race(lvl1, sr)); + for (int t = 0; t < 200; ++t) sr_sim.step(CraftInput{}, CraftInput{}, dt); + sr_sim.clear_events(); + sr_sim.crafts_mut()[0].autopilot = true; + + bool sr_saw_final_lap = false; + for (int t = 0; t < 1600 && !sr_saw_final_lap; ++t) { + sr_sim.step(CraftInput{}, CraftInput{}, dt); + for (const auto& ev : sr_sim.events()) { + if (ev.banner_text == "FINAL LAP") sr_saw_final_lap = true; + } + sr_sim.clear_events(); + } + CHECK(sr_sim.crafts()[0].lap >= 2); + CHECK(sr_saw_final_lap); + + auto drive_wrong_way = [&](sim::RaceSim& sim_obj) { + auto& c = sim_obj.crafts_mut()[0]; + c.autopilot = false; + const size_t n_wp = lvl1.main_waypoints.size(); + const size_t wp = static_cast(std::clamp(c.current_wp, 0, static_cast(n_wp - 1))); + const auto& w0 = lvl1.main_waypoints[wp]; + const auto& w1 = lvl1.main_waypoints[(wp + 1) % n_wp]; + Vec2 dir = Vec2(w1.x - w0.x, w1.z - w0.z).normalized(); + c.curr_pose.yaw = std::atan2(-dir.x, -dir.y); + c.vel = Vec3(-dir.x, 0.0f, -dir.y) * 10.0f; + c.prev_pose = c.curr_pose; + CraftInput rev_in; + rev_in.throttle = 1.0f; + for (int t = 0; t < 5; ++t) { + sim_obj.step(rev_in, CraftInput{}, dt); + } + }; + + drive_wrong_way(dm_sim); + CHECK(dm_sim.crafts()[0].speed_horizontal() > 3.0f); + CHECK(!dm_sim.crafts()[0].wrong_way); + + drive_wrong_way(sr_sim); + CHECK(sr_sim.crafts()[0].speed_horizontal() > 3.0f); + CHECK(sr_sim.crafts()[0].wrong_way); + + std::cout << "[PASS] DeathMatch Final Lap & Wrong Way suppression validated\n"; + } + return 0; }