diff --git a/src/assets/level.cpp b/src/assets/level.cpp index 8c34d36..8349e67 100644 --- a/src/assets/level.cpp +++ b/src/assets/level.cpp @@ -271,25 +271,63 @@ bool LevelData::load(const Vfs& vfs, int level_num) { main_waypoints.push_back(node); } - // Compute approximate forward vector for each checkpoint from nearest waypoint direction + // Compute exact perpendicular forward unit normal (and bidirectional flag) for each checkpoint if (!main_waypoints.empty()) { for (auto& gate : checkpoints) { - size_t best_wp = 0; - float best_d2 = 1e18f; - for (size_t i = 0; i < main_waypoints.size(); ++i) { - float dx = main_waypoints[i].x - gate.cx; - float dz = main_waypoints[i].z - gate.cz; - float d2 = dx * dx + dz * dz; - if (d2 < best_d2) { - best_d2 = d2; - best_wp = i; + Vec2 g0(gate.x0, gate.z0); + Vec2 seg(gate.x1 - gate.x0, gate.z1 - gate.z0); + float slen = seg.len(); + if (slen <= 1e-4f) continue; + + Vec2 n1(-seg.y / slen, seg.x / slen); + int pos_cross = 0; + int neg_cross = 0; + for (size_t wi = 0; wi < main_waypoints.size(); ++wi) { + const auto& w0 = main_waypoints[wi]; + const auto& w1 = main_waypoints[(wi + 1) % main_waypoints.size()]; + Vec2 p0(w0.x, w0.z); + Vec2 p1(w1.x, w1.z); + float d0 = (p0 - g0).dot(n1); + float d1 = (p1 - g0).dot(n1); + if ((d0 <= 0.0f && d1 >= 0.0f) || (d0 >= 0.0f && d1 <= 0.0f)) { + float denom = std::abs(d1 - d0); + if (denom <= 1e-5f) continue; + float frac = std::abs(d0) / denom; + Vec2 pc = p0 + (p1 - p0) * frac; + float t = (pc - g0).dot(seg) / (slen * slen); + if (t >= -0.25f && t <= 1.25f) { + if ((p1 - p0).dot(n1) >= 0.0f) { + ++pos_cross; + } else { + ++neg_cross; + } + } } } - const WaypointNode& cur_wp = main_waypoints[best_wp]; - const WaypointNode& nxt_wp = main_waypoints[(best_wp + 1) % main_waypoints.size()]; - Vec2 dir = Vec2(nxt_wp.x - cur_wp.x, nxt_wp.z - cur_wp.z).normalized(); - if (dir.len_sq() > 0.01f) { - gate.dir = dir; + + if (pos_cross > 0 && neg_cross == 0) { + gate.dir = n1; + gate.bidirectional = false; + } else if (neg_cross > 0 && pos_cross == 0) { + gate.dir = n1 * -1.0f; + gate.bidirectional = false; + } else { + gate.bidirectional = (pos_cross > 0 && neg_cross > 0); + size_t best_wp = 0; + float best_d2 = 1e18f; + for (size_t i = 0; i < main_waypoints.size(); ++i) { + float dx = main_waypoints[i].x - gate.cx; + float dz = main_waypoints[i].z - gate.cz; + float d2 = dx * dx + dz * dz; + if (d2 < best_d2) { + best_d2 = d2; + best_wp = i; + } + } + const WaypointNode& cur_wp = main_waypoints[best_wp]; + const WaypointNode& nxt_wp = main_waypoints[(best_wp + 1) % main_waypoints.size()]; + Vec2 wp_dir(nxt_wp.x - cur_wp.x, nxt_wp.z - cur_wp.z); + gate.dir = (wp_dir.dot(n1) < 0.0f) ? (n1 * -1.0f) : n1; } } } diff --git a/src/assets/level.hpp b/src/assets/level.hpp index 7221686..9515725 100644 --- a/src/assets/level.hpp +++ b/src/assets/level.hpp @@ -72,7 +72,8 @@ struct CheckpointGate { float x0 = 0.0f, z0 = 0.0f; float x1 = 0.0f, z1 = 0.0f; float cx = 0.0f, cz = 0.0f; - Vec2 dir{0.0f, -1.0f}; // approximate track forward direction + Vec2 dir{0.0f, -1.0f}; // unit normal perpendicular to gate segment pointing along track forward direction + bool bidirectional = false; // true when main racing circuit crosses gate in both directions (e.g. hairpins, DeathMatch corridor) }; struct WaypointNode { diff --git a/src/game/game_app.cpp b/src/game/game_app.cpp index 717ad9a..beb359d 100644 --- a/src/game/game_app.cpp +++ b/src/game/game_app.cpp @@ -575,6 +575,7 @@ void GameApp::enter_state(ScreenState s) { } } else if (s == ScreenState::PostRace) { in_race_session_ = false; + post_race_page_ = initial_post_race_page(selected_mode_, champ_.is_complete()); audio_.set_engine_state(false, 0.0f, false, 0.0f); audio_.set_pitstop_state(false, false, 1.0f); audio_.stop_intro_sfx(-1); @@ -1222,8 +1223,12 @@ void GameApp::update_menu(float dt) { } if (state_ == ScreenState::PostRace) { - if (m.left || m.right) { - post_race_page_ = (post_race_page_ + 1) % (selected_mode_ == sim::GameMode::Championship ? 3 : 2); + const int page_count = (selected_mode_ == sim::GameMode::Championship) ? 3 : 2; + if (m.left) { + post_race_page_ = (post_race_page_ + page_count - 1) % page_count; + audio_.play_sfx("CHANGE.WAV", 0.32f); + } else if (m.right) { + post_race_page_ = (post_race_page_ + 1) % page_count; audio_.play_sfx("CHANGE.WAV", 0.32f); } if (m.confirm || m.back) { @@ -2405,7 +2410,10 @@ void GameApp::render_menus() { } if (state_ == ScreenState::PostRace) { - ui_ren_.draw_text_centered(cx, oy + 76.0f, 2.65f, str.race_over, Vec4(0.35f, 1.0f, 0.55f, 1)); + const bool champ_done = (selected_mode_ == sim::GameMode::Championship && champ_.is_complete()); + ui_ren_.draw_text_centered(cx, oy + 76.0f, 2.65f, + champ_done ? "CHAMPIONSHIP COMPLETE" : str.race_over, + champ_done ? Vec4(1.0f, 0.88f, 0.25f, 1.0f) : Vec4(0.35f, 1.0f, 0.55f, 1.0f)); if (post_race_record_flags_ & 1) { ui_ren_.draw_text_centered(cx, oy + 114.0f, 1.9f, "NEW FASTEST LAP!", Vec4(1, 0.9f, 0.2f, 1)); } @@ -2493,30 +2501,46 @@ void GameApp::render_menus() { ui_ren_.draw_text(cx - 240.0f, oy + 370.0f, 2.0f, "RATING: " + champ_.player_rating_title(), Vec4(1, 0.9f, 0.2f, 1)); } } else if (selected_mode_ == sim::GameMode::Championship) { - ui_ren_.draw_text_centered(cx, oy + 152.0f, 2.25f, "CHAMPIONSHIP TABLE", Vec4(0.4f, 0.85f, 1, 1)); - auto ds = champ_.drivers; - std::sort(ds.begin(), ds.end(), [](const auto& a, const auto& b) { return a.points > b.points; }); + ui_ren_.draw_text_centered(cx, oy + 144.0f, 2.15f, + champ_done ? "FINAL CHAMPIONSHIP STANDINGS" : "CHAMPIONSHIP TABLE", + Vec4(0.4f, 0.85f, 1, 1)); + const auto order = champ_.sorted_driver_indices(); float tbl_w = std::min(720.0f, vw - 48.0f); float c0 = cx - tbl_w * 0.5f; float c1 = c0 + tbl_w * 0.10f; float c2 = c0 + tbl_w * 0.40f; float c3 = c0 + tbl_w * 0.70f; float c4 = c0 + tbl_w * 0.84f; - for (size_t i = 0; i < ds.size(); ++i) { - float ry = oy + 200.0f + static_cast(i) * 32.0f; - Vec4 col = (ds[i].name == cfg_.player_name) - ? Vec4(1, 0.95f, 0.25f, 1) - : Vec4(0.9f, 0.95f, 1, 1); + ui_ren_.draw_text_fit(c0, oy + 178.0f, tbl_w * 0.09f, 1.65f, str.position, Vec4(0.4f, 0.85f, 1, 1)); + ui_ren_.draw_text_fit(c1, oy + 178.0f, tbl_w * 0.29f, 1.65f, str.driver, Vec4(0.4f, 0.85f, 1, 1)); + ui_ren_.draw_text_fit(c2, oy + 178.0f, tbl_w * 0.29f, 1.65f, "TEAM", Vec4(0.4f, 0.85f, 1, 1)); + ui_ren_.draw_text_fit(c3, oy + 178.0f, tbl_w * 0.13f, 1.65f, "POINTS", Vec4(0.4f, 0.85f, 1, 1)); + ui_ren_.draw_text_fit(c4, oy + 178.0f, tbl_w * 0.16f, 1.65f, "WINS", Vec4(0.4f, 0.85f, 1, 1)); + for (size_t i = 0; i < order.size(); ++i) { + const size_t idx = order[i]; + const auto& d = champ_.drivers[idx]; + float ry = oy + 208.0f + static_cast(i) * 30.0f; + Vec4 col = (idx == 0) ? Vec4(1, 0.95f, 0.25f, 1) : Vec4(0.9f, 0.95f, 1, 1); ui_ren_.draw_text_fit(c0, ry, tbl_w * 0.09f, 1.75f, std::to_string(i + 1) + ".", col); - ui_ren_.draw_text_fit(c1, ry, tbl_w * 0.29f, 1.75f, ds[i].name, col); + ui_ren_.draw_text_fit(c1, ry, tbl_w * 0.29f, 1.75f, d.name, col); ui_ren_.draw_text_fit(c2, ry, tbl_w * 0.29f, 1.75f, - sim::kTeamNames[std::clamp(ds[i].team_idx, 0, 7)], col); - ui_ren_.draw_text_fit(c3, ry, tbl_w * 0.13f, 1.75f, std::to_string(ds[i].points) + " PTS", col); - ui_ren_.draw_text_fit(c4, ry, tbl_w * 0.16f, 1.75f, std::to_string(ds[i].wins) + " W", col); + sim::kTeamNames[std::clamp(d.team_idx, 0, 7)], col); + ui_ren_.draw_text_fit(c3, ry, tbl_w * 0.13f, 1.75f, std::to_string(d.points) + " PTS", col); + ui_ren_.draw_text_fit(c4, ry, tbl_w * 0.16f, 1.75f, std::to_string(d.wins) + " W", col); + } + if (champ_done) { + const size_t winner_idx = order[0]; + std::string summary = (winner_idx == 0) + ? ("CHAMPIONSHIP WINNER! RATING: " + champ_.player_rating_title()) + : ("CHAMPION: " + champ_.drivers[winner_idx].name + " RATING: " + champ_.player_rating_title()); + ui_ren_.draw_text_centered_fit(cx, oy + 458.0f, vw - 48.0f, 1.85f, summary, + Vec4(1.0f, 0.90f, 0.25f, 1.0f)); } } ui_ren_.draw_text_centered_fit(cx, oy + 505.0f, vw - 48.0f, 1.9f, - "LEFT/RIGHT STATS PAGE ENTER CONTINUE", Vec4(1, 0.85f, 0.2f, 1)); + champ_done ? "LEFT/RIGHT RACE RESULTS & STATS ENTER MAIN MENU" + : "LEFT/RIGHT STATS PAGE ENTER CONTINUE", + Vec4(1, 0.85f, 0.2f, 1)); } } diff --git a/src/game/game_modes.cpp b/src/game/game_modes.cpp index 66966b9..85b78ee 100644 --- a/src/game/game_modes.cpp +++ b/src/game/game_modes.cpp @@ -75,6 +75,17 @@ std::string ChampionshipState::player_rating_title() const { return sim::rating_for_score(overall_rating_pts); } +std::array ChampionshipState::sorted_driver_indices() const { + std::array order{0, 1, 2, 3, 4, 5, 6, 7}; + std::sort(order.begin(), order.end(), [&](size_t a, size_t b) { + if (drivers[a].points != drivers[b].points) return drivers[a].points > drivers[b].points; + if (drivers[a].wins != drivers[b].wins) return drivers[a].wins > drivers[b].wins; + if (drivers[a].kills != drivers[b].kills) return drivers[a].kills > drivers[b].kills; + return a < b; + }); + return order; +} + bool ChampionshipState::save_slot(const std::filesystem::path& pref_dir, int slot) const { std::error_code ec; std::filesystem::create_directories(pref_dir, ec); diff --git a/src/game/game_modes.hpp b/src/game/game_modes.hpp index d9b2c61..095cd1e 100644 --- a/src/game/game_modes.hpp +++ b/src/game/game_modes.hpp @@ -38,12 +38,17 @@ struct ChampionshipState { void record_race_results(const std::vector& crafts); bool is_complete() const { return current_track > total_tracks; } std::string player_rating_title() const; + std::array sorted_driver_indices() const; bool save_slot(const std::filesystem::path& pref_dir, int slot) const; bool load_slot(const std::filesystem::path& pref_dir, int slot); static bool slot_exists(const std::filesystem::path& pref_dir, int slot); }; +inline int initial_post_race_page(sim::GameMode mode, bool championship_complete) { + return (mode == sim::GameMode::Championship && championship_complete) ? 2 : 0; +} + struct TrackRecord { float best_lap = 0.0f; std::string best_lap_driver = "---"; diff --git a/src/sim/race_sim.cpp b/src/sim/race_sim.cpp index 91d9a20..05cda80 100644 --- a/src/sim/race_sim.cpp +++ b/src/sim/race_sim.cpp @@ -14,12 +14,13 @@ bool segments_intersect_or_near(Vec2 p0, Vec2 p1, Vec2 g0, Vec2 g1, float margin float* out_frac = nullptr) { // Check if craft crossed near checkpoint gate line (segment g0..g1 or center proximity) Vec2 mid = (g0 + g1) * 0.5f; - float gate_half = std::max((g1 - g0).len() * 0.5f, 3.0f) + margin; + Vec2 seg = g1 - g0; + float slen = seg.len(); + float gate_half = std::max(slen * 0.5f, 3.0f) + margin; if ((p1 - mid).len_sq() > gate_half * gate_half && (p0 - mid).len_sq() > gate_half * gate_half) { return false; } - Vec2 seg = g1 - g0; - if (seg.len_sq() < 0.25f) { + if (slen < 0.5f) { if ((p1 - mid).len() <= 3.5f) { if (out_frac) *out_frac = 1.0f; return true; @@ -33,8 +34,9 @@ bool segments_intersect_or_near(Vec2 p0, Vec2 p1, Vec2 g0, Vec2 g1, float margin float denom = std::abs(d1 - d0); float frac = (denom > 1e-5f) ? std::clamp(std::abs(d0) / denom, 0.0f, 1.0f) : 1.0f; Vec2 p_cross = p0 + (p1 - p0) * frac; - float t = (p_cross - g0).dot(seg) / seg.len_sq(); - if (t >= -0.35f && t <= 1.35f) { + float t = (p_cross - g0).dot(seg) / (slen * slen); + float ext = margin / slen; + if (t >= -ext && t <= 1.0f + ext) { if (out_frac) *out_frac = frac; return true; } @@ -42,27 +44,38 @@ bool segments_intersect_or_near(Vec2 p0, Vec2 p1, Vec2 g0, Vec2 g1, float margin return false; } +bool clip_segment_slab(float origin, float delta, float bmin, float bmax, + float& t0, float& t1) { + if (std::abs(delta) < 1e-6f) { + return origin >= bmin && origin <= bmax; + } + float inv = 1.0f / delta; + float tn = (bmin - origin) * inv; + float tf = (bmax - origin) * inv; + if (tn > tf) std::swap(tn, tf); + t0 = std::max(t0, tn); + t1 = std::min(t1, tf); + return t0 <= t1; +} + bool segment_intersects_aabb(const Vec3& p0, const Vec3& p1, float min_x, float max_x, float min_y, float max_y, float min_z, float max_z) { float t0 = 0.0f; float t1 = 1.0f; - auto clip = [&](float origin, float delta, float bmin, float bmax) -> bool { - if (std::abs(delta) < 1e-6f) { - return origin >= bmin && origin <= bmax; - } - float inv = 1.0f / delta; - float tn = (bmin - origin) * inv; - float tf = (bmax - origin) * inv; - if (tn > tf) std::swap(tn, tf); - t0 = std::max(t0, tn); - t1 = std::min(t1, tf); - return t0 <= t1; - }; - return clip(p0.x, p1.x - p0.x, min_x, max_x) && - clip(p0.y, p1.y - p0.y, min_y, max_y) && - clip(p0.z, p1.z - p0.z, min_z, max_z); + return clip_segment_slab(p0.x, p1.x - p0.x, min_x, max_x, t0, t1) && + clip_segment_slab(p0.y, p1.y - p0.y, min_y, max_y, t0, t1) && + clip_segment_slab(p0.z, p1.z - p0.z, min_z, max_z, t0, t1); +} + +bool segment_intersects_aabb_2d(Vec2 p0, Vec2 p1, + float min_x, float max_x, + float min_z, float max_z) { + float t0 = 0.0f; + float t1 = 1.0f; + return clip_segment_slab(p0.x, p1.x - p0.x, min_x, max_x, t0, t1) && + clip_segment_slab(p0.y, p1.y - p0.y, min_z, max_z, t0, t1); } } // namespace @@ -556,11 +569,23 @@ void RaceSim::update_checkpoints_and_laps(CraftState& c, float dt) { const auto& gate = level_.checkpoints[expected]; Vec2 move = p1 - p0; float mlen = move.len(); - bool moving_forward = (mlen <= 1e-4f || move.dot(gate.dir) >= -0.25f * mlen); + bool moving_forward = gate.bidirectional || (mlen <= 1e-4f) || (move.dot(gate.dir) >= -0.25f * mlen); float cross_frac = 1.0f; - if (moving_forward && - segments_intersect_or_near(p0, p1, Vec2(gate.x0, gate.z0), Vec2(gate.x1, gate.z1), - 2.5f, &cross_frac)) { + bool crossed = false; + if (moving_forward) { + crossed = segments_intersect_or_near(p0, p1, Vec2(gate.x0, gate.z0), Vec2(gate.x1, gate.z1), + 2.5f, &cross_frac); + if (!crossed && gate.bidirectional) { + // Authentic Bullfrog checkpoint strip spans inclusive grid cells [min..max+1] + float gx0 = std::min(gate.x0, gate.x1); + float gx1 = std::max(gate.x0, gate.x1) + 1.0f; + float gz0 = std::min(gate.z0, gate.z1); + float gz1 = std::max(gate.z0, gate.z1) + 1.0f; + crossed = segment_intersects_aabb_2d(p0, p1, gx0, gx1, gz0, gz1); + cross_frac = 1.0f; + } + } + if (crossed) { ++c.checkpoints_passed; c.next_checkpoint = (expected + 1) % num_cps; c.wrong_way = false; @@ -884,9 +909,34 @@ void RaceSim::activate_group(uint16_t group, int actor_player, bool allow_banner const bool in_dst = (pk.pos.x >= dx0 && pk.pos.x < dx1 && pk.pos.z >= dz0 && pk.pos.z < dz1); if (in_src && !in_dst) { - pk.pos.x += shift_x; - pk.pos.z += shift_z; - pk.pos.y = level_.sample_entity_surface_y(pk.pos.x, pk.pos.z) + 0.45f; + const float nx = pk.pos.x + shift_x; + const float nz = pk.pos.z + shift_z; + PickupItem* conflict = nullptr; + for (auto& other : pickups_) { + if (&other == &pk || !other.group_unlocked) continue; + float dx = other.pos.x - nx; + float dz = other.pos.z - nz; + if (dx * dx + dz * dz <= 0.25f) { + conflict = &other; + break; + } + } + if (conflict != nullptr) { + if (pk.group == m.group && conflict->group != m.group) { + conflict->active = false; + conflict->group_unlocked = false; + pk.pos.x = nx; + pk.pos.z = nz; + pk.pos.y = level_.sample_entity_surface_y(nx, nz) + 0.45f; + } else { + pk.active = false; + pk.group_unlocked = false; + } + } else { + pk.pos.x = nx; + pk.pos.z = nz; + pk.pos.y = level_.sample_entity_surface_y(nx, nz) + 0.45f; + } } } } @@ -897,10 +947,12 @@ void RaceSim::activate_group(uint16_t group, int actor_player, bool allow_banner if (am.duration <= 0.0f) { apply_morph_fraction(am, 1.0f); - morph_touched_.clear(); - morph_touched_.push_back({m.dst_x, m.dst_z, m.dst_x + m.size_x, m.dst_z + m.size_z}); - morph_touched_.push_back({m.src_x, m.src_z, m.src_x + m.size_x, m.src_z + m.size_z}); - resnap_entities_in_rects(morph_touched_); + if (height_differs || tex_or_col_differs) { + morph_touched_.clear(); + morph_touched_.push_back({m.dst_x, m.dst_z, m.dst_x + m.size_x, m.dst_z + m.size_z}); + morph_touched_.push_back({m.src_x, m.src_z, m.src_x + m.size_x, m.src_z + m.size_z}); + resnap_entities_in_rects(morph_touched_); + } } else { active_morphs_.push_back(std::move(am)); } diff --git a/tests/test_assets.cpp b/tests/test_assets.cpp index a8fbe46..c8d77b6 100644 --- a/tests/test_assets.cpp +++ b/tests/test_assets.cpp @@ -69,10 +69,34 @@ int main() { CHECK(lvl.checkpoints[0].index == 0); for (const auto& cp : lvl.checkpoints) { CHECK(cp.x0 != cp.x1 || cp.z0 != cp.z1); + Vec2 seg(cp.x1 - cp.x0, cp.z1 - cp.z0); + float slen = seg.len(); + CHECK(slen > 0.5f); + CHECK(std::abs(cp.dir.len() - 1.0f) < 1e-3f); + CHECK(std::abs(cp.dir.dot(seg) / slen) < 1e-3f); + } + if (l == 3) { + CHECK(lvl.checkpoints[1].dir.y < -0.99f && !lvl.checkpoints[1].bidirectional); + } + if (l == 4) { + CHECK(lvl.checkpoints[6].dir.x > 0.99f && !lvl.checkpoints[6].bidirectional); + CHECK(lvl.checkpoints[9].dir.x < -0.99f && !lvl.checkpoints[9].bidirectional); + } + if (l == 6) { + CHECK(lvl.checkpoints[9].dir.x > 0.99f && !lvl.checkpoints[9].bidirectional); } if (l == 7) { CHECK(lvl.checkpoints.size() == 6); } + if (l == 8) { + CHECK(lvl.checkpoints[1].bidirectional); + CHECK(lvl.checkpoints[4].bidirectional); + } + if (l == 9) { + for (const auto& cp : lvl.checkpoints) { + CHECK(cp.bidirectional); + } + } // Verify all referenced columns & floor tiles are within valid bounds int col_cells = 0; diff --git a/tests/test_gameplay.cpp b/tests/test_gameplay.cpp index 454c69d..5c6bf06 100644 --- a/tests/test_gameplay.cpp +++ b/tests/test_gameplay.cpp @@ -1434,5 +1434,247 @@ int main() { std::cout << "[PASS] DeathMatch Final Lap & Wrong Way suppression validated\n"; } + // 15. Perpendicular Checkpoint Normals, Bidirectional Gates, Morphed Shortcuts & Pickup Deduplication + { + auto test_gate_crossing = [&](int track_num, int cp_idx, Vec2 start_xz, Vec2 dir_xz, + int expected_after, int steps = 8) -> bool { + assets::LevelData lvl; + if (!lvl.load(vfs, track_num)) return false; + sim::RaceSetup setup; + setup.mode = sim::GameMode::SingleRace; + setup.track_number = track_num; + setup.num_ai = 0; + setup.total_laps = 2; + setup.weapons_enabled = false; + setup.fuel_enabled = false; + setup.damage_enabled = false; + sim::RaceSim sim_obj; + if (!sim_obj.start_race(lvl, setup)) return false; + for (int t = 0; t < 200; ++t) sim_obj.step(CraftInput{}, CraftInput{}, dt); + auto& p1 = sim_obj.crafts_mut()[0]; + p1.autopilot = false; + p1.next_checkpoint = cp_idx; + p1.checkpoints_passed = cp_idx; + Vec2 d = dir_xz.normalized(); + p1.curr_pose.pos = Vec3(start_xz.x, sim_obj.level().sample_height_world(start_xz.x, start_xz.y) + 0.30f, + start_xz.y); + p1.curr_pose.yaw = std::atan2(d.x, d.y); + p1.prev_pose = p1.curr_pose; + for (int t = 0; t < steps; ++t) { + p1.vel.x = d.x * 18.0f; + p1.vel.z = d.y * 18.0f; + sim_obj.step(CraftInput{}, CraftInput{}, dt); + } + return sim_obj.crafts()[0].next_checkpoint == expected_after; + }; + + // 15a. Previously failing forward crossings & reverse-crossing rejection on fixed gates + CHECK(test_gate_crossing(6, 9, Vec2(9.2f, 89.6f), Vec2(0.8f, 0.5f), 10)); // L6 cp[9] ESE waypoint crossing + CHECK(test_gate_crossing(6, 9, Vec2(10.8f, 90.0f), Vec2(-1.0f, 0.0f), 9)); // L6 cp[9] reverse West rejected + CHECK(test_gate_crossing(3, 1, Vec2(18.5f, 57.6f), Vec2(-0.7f, -0.7f), 2)); // L3 cp[1] NW diagonal exit + CHECK(test_gate_crossing(4, 6, Vec2(44.4f, 123.8f), Vec2(0.5f, 0.85f), 7)); // L4 cp[6] angled SE crossing + CHECK(test_gate_crossing(4, 9, Vec2(24.6f, 79.5f), Vec2(-0.5f, 0.85f), 0)); // L4 cp[9] angled SW crossing + CHECK(test_gate_crossing(8, 1, Vec2(97.5f, 87.2f), Vec2(0.0f, 1.0f), 2)); // L8 cp[1] Southbound hairpin entry + CHECK(test_gate_crossing(8, 4, Vec2(17.4f, 88.6f), Vec2(0.7f, -0.7f), 5)); // L8 cp[4] NE hairpin exit + CHECK(test_gate_crossing(8, 4, Vec2(19.3f, 85.0f), Vec2(-1.0f, 0.0f), 5, 3)); // L8 cp[4] apex tile strip graze + CHECK(test_gate_crossing(8, 4, Vec2(22.0f, 85.0f), Vec2(0.0f, 1.0f), 4, 4)); // L8 cp[4] East of apex strip does not fire + + // 15b. Level 7 chained mine shortcut (trig#278 -> trig#279 -> trig#282) hits cp[2] and cp[3] + { + assets::LevelData lvl7; + CHECK(lvl7.load(vfs, 7)); + sim::RaceSetup ms; + ms.track_number = 7; + ms.num_ai = 0; + ms.total_laps = 2; + ms.fuel_enabled = false; + ms.damage_enabled = false; + sim::RaceSim msim; + CHECK(msim.start_race(lvl7, ms)); + for (int t = 0; t < 200; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + + const uint16_t ramp_before = msim.level().cell(33, 42).height; + const uint16_t ramp_src = msim.level().cell(39, 49).height; + const uint16_t ridge_before = msim.level().cell(45, 40).height; + const uint16_t ridge_src = msim.level().cell(45, 49).height; + CHECK(ramp_before > ramp_src); + CHECK(ridge_before > 2048u && ridge_src < 2048u); + + auto& p1 = msim.crafts_mut()[0]; + p1.autopilot = false; + // Visiting trig#279 before trig#278 arms group 5 must NOT morph M[3] + p1.curr_pose.pos = Vec3(32.0f, msim.level().sample_height_world(32.0f, 42.5f) + 0.30f, 42.5f); + p1.prev_pose = p1.curr_pose; + for (int t = 0; t < 40; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + CHECK(msim.level().cell(33, 42).height == ramp_before); + + // Crossing trig#278 (94..99, 56..57) arms trig#279 + p1.curr_pose.pos = Vec3(95.5f, msim.level().sample_height_world(95.5f, 56.5f) + 0.30f, 56.5f); + p1.prev_pose = p1.curr_pose; + for (int t = 0; t < 10; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + + // Now trig#279 fires on Lap 2 and flattens M[3] step at (33, 42) + p1.curr_pose.pos = Vec3(32.0f, msim.level().sample_height_world(32.0f, 42.5f) + 0.30f, 42.5f); + p1.prev_pose = p1.curr_pose; + for (int t = 0; t < 45; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + CHECK(msim.level().cell(33, 42).height == ramp_src); + + // Missile hit on trig#282 blasts ridge M[4] (44..47, 39..42) + p1.curr_pose.pos = Vec3(45.5f, msim.level().sample_height_world(45.5f, 43.0f) + 0.30f, 43.0f); + p1.curr_pose.yaw = kPi; + p1.prev_pose = p1.curr_pose; + p1.vel = Vec3(0, 0, 0); + p1.missile_cd = 0.0f; + for (int t = 0; t < 45; ++t) { + CraftInput fire; + fire.fire_missile = (t == 0); + msim.step(fire, CraftInput{}, dt); + } + CHECK(msim.level().cell(45, 40).height == ridge_src); + + // Driving into mine entry crosses cp[2] (z=46) and exiting north rejoins before cp[3] (x=52) + p1.next_checkpoint = 2; + p1.checkpoints_passed = 2; + p1.curr_pose.pos = Vec3(22.0f, msim.level().sample_height_world(22.0f, 46.8f) + 0.30f, 46.8f); + p1.curr_pose.yaw = kPi; + p1.prev_pose = p1.curr_pose; + for (int t = 0; t < 10; ++t) { + p1.vel = Vec3(0.0f, 0.0f, -18.0f); + msim.step(CraftInput{}, CraftInput{}, dt); + } + CHECK(msim.crafts()[0].next_checkpoint == 3); + + p1.curr_pose.pos = Vec3(51.2f, msim.level().sample_height_world(51.2f, 24.5f) + 0.30f, 24.5f); + p1.curr_pose.yaw = kPi * 0.5f; + p1.prev_pose = p1.curr_pose; + for (int t = 0; t < 10; ++t) { + p1.vel = Vec3(18.0f, 0.0f, 0.0f); + msim.step(CraftInput{}, CraftInput{}, dt); + } + CHECK(msim.crafts()[0].next_checkpoint == 4); + } + + // 15c. Morph pickup relocation deduplicates overlapping pickups (L4 M[1] & L5 M[2]) + auto check_no_stacked_pickups = [&](const sim::RaceSim& s) -> bool { + for (size_t i = 0; i < s.pickups().size(); ++i) { + const auto& a = s.pickups()[i]; + if (!a.group_unlocked) continue; + for (size_t j = i + 1; j < s.pickups().size(); ++j) { + const auto& b = s.pickups()[j]; + if (!b.group_unlocked) continue; + float dx = a.pos.x - b.pos.x; + float dz = a.pos.z - b.pos.z; + if (dx * dx + dz * dz < 0.25f) return false; + } + } + return true; + }; + + { + assets::LevelData lvl4; + CHECK(lvl4.load(vfs, 4)); + sim::RaceSetup ms4; + ms4.track_number = 4; + ms4.num_ai = 0; + sim::RaceSim sim4; + CHECK(sim4.start_race(lvl4, ms4)); + for (int t = 0; t < 200; ++t) sim4.step(CraftInput{}, CraftInput{}, dt); + auto& p1 = sim4.crafts_mut()[0]; + p1.curr_pose.pos = Vec3(69.5f, sim4.level().sample_height_world(69.5f, 90.0f) + 0.30f, 90.0f); + p1.curr_pose.yaw = kPi; + p1.prev_pose = p1.curr_pose; + p1.vel = Vec3(0, 0, 0); + for (int t = 0; t < 60; ++t) { + CraftInput fire; + fire.fire_missile = (t == 0); + sim4.step(fire, CraftInput{}, dt); + } + CHECK(check_no_stacked_pickups(sim4)); + int vault_unlocked = 0; + for (const auto& pk : sim4.pickups()) { + if (pk.group_unlocked && pk.pos.x >= 64.0f && pk.pos.x <= 77.0f && + pk.pos.z >= 71.0f && pk.pos.z <= 89.0f) { + ++vault_unlocked; + } + } + CHECK(vault_unlocked == 8); + } + + { + assets::LevelData lvl5; + CHECK(lvl5.load(vfs, 5)); + sim::RaceSetup ms5; + ms5.track_number = 5; + ms5.num_ai = 0; + sim::RaceSim sim5; + CHECK(sim5.start_race(lvl5, ms5)); + for (int t = 0; t < 200; ++t) sim5.step(CraftInput{}, CraftInput{}, dt); + auto& p1 = sim5.crafts_mut()[0]; + p1.curr_pose.pos = Vec3(17.0f, sim5.level().sample_height_world(17.0f, 134.0f) + 0.30f, 134.0f); + p1.prev_pose = p1.curr_pose; + for (int t = 0; t < 45; ++t) sim5.step(CraftInput{}, CraftInput{}, dt); + CHECK(check_no_stacked_pickups(sim5)); + int alcove_unlocked = 0; + for (const auto& pk : sim5.pickups()) { + if (pk.group_unlocked && pk.pos.x >= 15.0f && pk.pos.x <= 20.0f && + pk.pos.z >= 133.0f && pk.pos.z <= 137.0f) { + ++alcove_unlocked; + } + } + CHECK(alcove_unlocked == 4); + } + + std::cout << "[PASS] Perpendicular checkpoints, bidirectional hairpins, L7 mine morph & pickup deduplication validated\n"; + } + + // 16. Championship completion opens PostRace directly on overall championship leaderboard with exact tie-breakers + { + CHECK(game::initial_post_race_page(sim::GameMode::Championship, false) == 0); + CHECK(game::initial_post_race_page(sim::GameMode::Championship, true) == 2); + CHECK(game::initial_post_race_page(sim::GameMode::SingleRace, true) == 0); + CHECK(game::initial_post_race_page(sim::GameMode::DeathMatch, true) == 0); + + game::ChampionshipState ch; + ch.start_new("ACE", 0, 0, 1, 2); + CHECK(!ch.is_complete()); + CHECK(game::initial_post_race_page(sim::GameMode::Championship, ch.is_complete()) == 0); + + auto make_results = [&](const std::array& places, const std::array& kills) { + std::vector out(8); + for (int i = 0; i < 8; ++i) { + out[static_cast(i)].id = i; + out[static_cast(i)].is_human = (i == 0); + out[static_cast(i)].player_index = (i == 0) ? 0 : -1; + out[static_cast(i)].placement = places[static_cast(i)]; + out[static_cast(i)].kills = kills[static_cast(i)]; + } + return out; + }; + + // Race 1 (places for slots 0..7): slot 1=1st(10,1W), slot 2=2nd(6), slot 0=3rd(4), slot 3=4th(3), slot 4=5th(2), slot 5=6th(1), 6=7th, 7=8th + ch.record_race_results(make_results({3, 1, 2, 4, 5, 6, 7, 8}, {1, 2, 6, 2, 0, 0, 3, 1})); + CHECK(ch.current_track == 2); + CHECK(!ch.is_complete()); + CHECK(game::initial_post_race_page(sim::GameMode::Championship, ch.is_complete()) == 0); + + // Race 2 creates controlled ties: + // - Slot 1: 12 pts, 1 win, 2 kills + // - Slot 4: 12 pts, 1 win, 0 kills (loses to Slot 1 on kills, beats Slot 2 on wins) + // - Slot 2: 12 pts, 0 wins, 6 kills + // - Slot 3: 7 pts, 0 wins, 6 kills + // - Slot 0: 7 pts, 0 wins, 2 kills (loses to Slot 3 on kills) + ch.record_race_results(make_results({4, 5, 2, 3, 1, 6, 7, 8}, {1, 0, 0, 4, 0, 0, 0, 0})); + CHECK(ch.is_complete()); + CHECK(game::initial_post_race_page(sim::GameMode::Championship, ch.is_complete()) == 2); + + const std::array expected_order{1, 4, 2, 3, 0, 5, 6, 7}; + const auto order = ch.sorted_driver_indices(); + for (size_t i = 0; i < order.size(); ++i) { + CHECK(order[i] == expected_order[i]); + } + + std::cout << "[PASS] Championship completion leaderboard default page & tie-break ranking validated\n"; + } + return 0; }