diff --git a/src/assets/level.cpp b/src/assets/level.cpp index b506dc9..9746a79 100644 --- a/src/assets/level.cpp +++ b/src/assets/level.cpp @@ -60,6 +60,8 @@ bool LevelData::load(const Vfs& vfs, int level_num) { pickups.clear(); props.clear(); triggers.clear(); + morphs.clear(); + effects.clear(); pads.clear(); checkpoints.clear(); @@ -241,6 +243,52 @@ bool LevelData::load(const Vfs& vfs, int level_num) { } } + // Parse authentic Bullfrog morph target/source pairs and visual/hazard effect entities + for (const auto& e : entities) { + if (e.type != 2) continue; + if (e.subtype == 16 || e.subtype == 23) { + if (e.next_id < kMaxEntities) { + const LevelEntity& src = entity_slots[e.next_id]; + if (src.type == 2 && (src.subtype == 9 || src.subtype == 7)) { + int dst_x = static_cast(e.pos_x_fp8 >> 8); + int dst_z = static_cast(e.pos_z_fp8 >> 8); + int src_x = static_cast(src.pos_x_fp8 >> 8); + int src_z = static_cast(src.pos_z_fp8 >> 8); + if (dst_x < kMapWidth && src_x < kMapWidth && + dst_z < kMapHeight && src_z < kMapHeight) { + int sx = std::min(e.aux1, src.aux1); + int sz = std::min(e.aux2, src.aux2); + sx = std::min({sx, kMapWidth - 1 - dst_x, kMapWidth - 1 - src_x}); + sz = std::min({sz, kMapHeight - 1 - dst_z, kMapHeight - 1 - src_z}); + if (sx >= 0 && sz >= 0) { + MorphDefinition m; + m.entity_id = e.id; + m.source_id = src.id; + m.group = e.group; + m.permanent = (e.subtype == 23); + m.dst_x = dst_x; + m.dst_z = dst_z; + m.src_x = src_x; + m.src_z = src_z; + m.size_x = sx; + m.size_z = sz; + morphs.push_back(m); + } + } + } + } + } else if (e.subtype == 1 || e.subtype == 2 || e.subtype == 3 || + e.subtype == 5 || e.subtype == 8) { + EffectEntity ef; + ef.entity_id = e.id; + ef.subtype = e.subtype; + ef.group = e.group; + ef.x = e.tile_x() + 0.5f; + ef.z = e.tile_z() + 0.5f; + effects.push_back(ef); + } + } + return true; } @@ -263,10 +311,10 @@ float LevelData::cell_height_world(int x, int z) const { float LevelData::sample_height_world(float wx, float wz) const { float cx = std::clamp(wx, 0.0f, static_cast(kMapWidth - 1)); float cz = std::clamp(wz, 0.0f, static_cast(kMapHeight - 1)); - int x0 = static_cast(cx); - int z0 = static_cast(cz); - int x1 = std::min(x0 + 1, kMapWidth - 1); - int z1 = std::min(z0 + 1, kMapHeight - 1); + int x0 = std::min(static_cast(cx), kMapWidth - 2); + int z0 = std::min(static_cast(cz), kMapHeight - 2); + int x1 = x0 + 1; + int z1 = z0 + 1; float fx = cx - static_cast(x0); float fz = cz - static_cast(z0); @@ -274,9 +322,31 @@ float LevelData::sample_height_world(float wx, float wz) const { float h10 = cell_height_world(x1, z0); float h01 = cell_height_world(x0, z1); float h11 = cell_height_world(x1, z1); - float hx0 = h00 + (h10 - h00) * fx; - float hx1 = h01 + (h11 - h01) * fx; - return hx0 + (hx1 - hx0) * fz; + // Exact triangle match with world_geometry.cpp diagonal (x0,z0)-(x1,z1) + if (fx >= fz) { + return h00 + (h10 - h00) * fx + (h11 - h10) * fz; + } + return h00 + (h11 - h01) * fx + (h01 - h00) * fz; +} + +float LevelData::sample_height_alt_diag(float wx, float wz) const { + float cx = std::clamp(wx, 0.0f, static_cast(kMapWidth - 1)); + float cz = std::clamp(wz, 0.0f, static_cast(kMapHeight - 1)); + int x0 = std::min(static_cast(cx), kMapWidth - 2); + int z0 = std::min(static_cast(cz), kMapHeight - 2); + int x1 = x0 + 1; + int z1 = z0 + 1; + float fx = cx - static_cast(x0); + float fz = cz - static_cast(z0); + + float h00 = cell_height_world(x0, z0); + float h10 = cell_height_world(x1, z0); + float h01 = cell_height_world(x0, z1); + float h11 = cell_height_world(x1, z1); + if (fx + fz <= 1.0f) { + return h00 + (h10 - h00) * fx + (h01 - h00) * fz; + } + return h11 + (h01 - h11) * (1.0f - fx) + (h10 - h11) * (1.0f - fz); } Vec3 LevelData::sample_normal_world(float wx, float wz) const { @@ -288,7 +358,8 @@ Vec3 LevelData::sample_normal_world(float wx, float wz) const { return Vec3(hL - hR, 2.0f * eps, hD - hU).normalized(); } -bool LevelData::is_cell_solid_at_y(int x, int z, float world_y, float craft_height) const { +bool LevelData::is_cell_solid_at_point(int x, int z, float qx, float qz, + float world_y, float craft_height) const { if (x < 0 || x >= kMapWidth || z < 0 || z >= kMapHeight) { return true; } @@ -301,10 +372,14 @@ bool LevelData::is_cell_solid_at_y(int x, int z, float world_y, float craft_heig return false; } const ColumnDefinition& col = columns[col_idx]; - float ground_y = sample_height_world(static_cast(x) + 0.5f, static_cast(z) + 0.5f); + if (col.shape_mask == 0) { + return false; + } + float cx = std::clamp(qx, static_cast(x), static_cast(x + 1)); + float cz = std::clamp(qz, static_cast(z), static_cast(z + 1)); + float ground_y = sample_height_world(cx, cz); // Blocks stack at slots s = 0..7 spanning [ground_y + s, ground_y + s + 1.0f]. - // Test vertical span overlap so crafts drive cleanly under overhead gantries/tunnels - // while riding on column tops when elevated onto bridges. + // Test vertical span overlap at contact (qx, qz) so sloped columns match rendered walls. float min_y = world_y - 0.05f; float max_y = world_y + craft_height; for (int s = 0; s < 8; ++s) { @@ -318,6 +393,24 @@ bool LevelData::is_cell_solid_at_y(int x, int z, float world_y, float craft_heig return false; } +bool LevelData::is_cell_solid_at_y(int x, int z, float world_y, float craft_height) const { + return is_cell_solid_at_point(x, z, static_cast(x) + 0.5f, + static_cast(z) + 0.5f, world_y, craft_height); +} + +bool LevelData::is_cliff_blocking_at_y(int x, int z, float world_y) const { + if (x < 0 || x >= kMapWidth - 1 || z < 0 || z >= kMapHeight - 1) { + return false; + } + float h00 = cell_height_world(x, z); + float h10 = cell_height_world(x + 1, z); + float h01 = cell_height_world(x, z + 1); + float h11 = cell_height_world(x + 1, z + 1); + float h_min = std::min({h00, h10, h01, h11}); + float h_max = std::max({h00, h10, h01, h11}); + return (h_max - h_min) >= 1.35f && h_max > world_y + 0.65f; +} + void LevelData::sample_surface_and_ceiling(float wx, float wz, float craft_y, float& out_floor_y, float& out_ceil_y) const { out_floor_y = sample_height_world(wx, wz); @@ -337,16 +430,15 @@ void LevelData::sample_surface_and_ceiling(float wx, float wz, float craft_y, return; } const ColumnDefinition& col = columns[col_idx]; - float ground_y = out_floor_y; - bool grounded_column = (col.shape_mask & 1u) != 0; + float ground_y_top = out_floor_y; + float ground_y_bot = std::min(ground_y_top, sample_height_alt_diag(wx, wz)); for (int s = 0; s < 8; ++s) { if ((col.shape_mask & (1u << s)) == 0) continue; - float slot_bot = ground_y + static_cast(s); - float slot_top = slot_bot + 1.0f; - if (grounded_column && slot_top <= craft_y + 0.10f) { + float slot_top = ground_y_top + static_cast(s) + 1.0f; + float slot_bot = ground_y_bot + static_cast(s); + if (slot_top <= craft_y + 0.30f) { out_floor_y = std::max(out_floor_y, slot_top); - } - if (slot_bot >= craft_y + 0.05f) { + } else if (slot_bot >= craft_y - 0.10f) { out_ceil_y = std::min(out_ceil_y, slot_bot); } } diff --git a/src/assets/level.hpp b/src/assets/level.hpp index c03c167..767b32a 100644 --- a/src/assets/level.hpp +++ b/src/assets/level.hpp @@ -78,6 +78,23 @@ struct WaypointNode { uint16_t next_id = 0; }; +struct MorphDefinition { + uint16_t entity_id = 0; + uint16_t source_id = 0; + uint16_t group = 0; + bool permanent = false; // subtype 23 (MorphPermanent) vs 16 (MorphOnce) + int dst_x = 0, dst_z = 0; + int src_x = 0, src_z = 0; + int size_x = 0, size_z = 0; // inclusive cell span [0..size_x] x [0..size_z] +}; + +struct EffectEntity { + uint16_t entity_id = 0; + uint8_t subtype = 0; // 1=ExplosionParticles, 2=DamageCraft, 3=Explosion, 5=SteamStrong, 8=SteamLight + uint16_t group = 0; + float x = 0.0f, z = 0.0f; +}; + struct LevelData { int level_number = 1; // 1..6 int world_index = 0; // 0..5 (corresponding to TEXTU0-x / SKY0-x / TABLE0-x) @@ -94,6 +111,8 @@ struct LevelData { std::vector pickups; // type 5 std::vector props; // type 3 std::vector triggers; // type 8 + std::vector morphs; // type 2 subtype 16/23 paired with subtype 9/7 source + std::vector effects; // type 2 subtype 1/2/3/5/8 std::vector pads; // type 10 int min_x = 0, max_x = kMapWidth - 1; @@ -110,8 +129,12 @@ struct LevelData { float cell_height_world(int x, int z) const; float sample_height_world(float wx, float wz) const; + float sample_height_alt_diag(float wx, float wz) const; Vec3 sample_normal_world(float wx, float wz) const; + bool is_cell_solid_at_point(int x, int z, float qx, float qz, + float world_y, float craft_height = 0.42f) const; bool is_cell_solid_at_y(int x, int z, float world_y, float craft_height = 0.42f) const; + bool is_cliff_blocking_at_y(int x, int z, float world_y) const; void sample_surface_and_ceiling(float wx, float wz, float craft_y, float& out_floor_y, float& out_ceil_y) const; }; diff --git a/src/game/game_app.cpp b/src/game/game_app.cpp index eb28288..27164c7 100644 --- a/src/game/game_app.cpp +++ b/src/game/game_app.cpp @@ -474,9 +474,15 @@ void GameApp::handle_sim_events() { } race_sim_.clear_events(); - int x0 = 0, z0 = 0, x1 = 0, z1 = 0; - if (race_sim_.consume_terrain_dirty(x0, z0, x1, z1)) { - world_ren_.update_terrain_region(race_sim_.level(), x0, z0, x1, z1); + std::vector dirty_rects; + bool columns_dirty = false; + if (race_sim_.consume_world_dirty(dirty_rects, columns_dirty)) { + for (const auto& r : dirty_rects) { + world_ren_.update_terrain_region(race_sim_.level(), r.x0, r.z0, r.x1, r.z1); + } + if (columns_dirty) { + world_ren_.rebuild_columns(race_sim_.level()); + } } } @@ -1140,6 +1146,35 @@ void GameApp::render_frame(float alpha) { cams[vp].eye = pose.pos + fwd * 0.45f + Vec3(0.0f, 0.42f, 0.0f); cams[vp].target = pose.pos + fwd * 6.0f + Vec3(0.0f, 0.2f, 0.0f); } + if (cam_mode_ == 0 || cam_mode_ == 1) { + float cam_floor_y = 0.0f; + float cam_ceil_y = 1e6f; + race_sim_.level().sample_surface_and_ceiling(cams[vp].eye.x, cams[vp].eye.z, + pose.pos.y, cam_floor_y, cam_ceil_y); + if (cams[vp].eye.y < cam_floor_y + 0.35f) { + cams[vp].eye.y = cam_floor_y + 0.35f; + } + if (cam_ceil_y < 1e5f) { + float max_cam_y = std::max(pose.pos.y + 0.15f, cam_ceil_y - 0.18f); + if (cams[vp].eye.y > max_cam_y) { + cams[vp].eye.y = max_cam_y; + } + } + Vec3 anchor = pose.pos + Vec3(0.0f, 0.35f, 0.0f); + Vec3 prev_clear = anchor; + constexpr int kCamSteps = 10; + for (int k = 1; k <= kCamSteps; ++k) { + float t = static_cast(k) / static_cast(kCamSteps); + Vec3 p = lerp(anchor, cams[vp].eye, t); + int ix = static_cast(std::floor(p.x)); + int iz = static_cast(std::floor(p.z)); + if (race_sim_.level().is_cell_solid_at_point(ix, iz, p.x, p.z, p.y, 0.15f)) { + cams[vp].eye = prev_clear; + break; + } + prev_clear = p; + } + } float min_cam_y = race_sim_.level().sample_height_world(cams[vp].eye.x, cams[vp].eye.z) + 0.4f; if (cams[vp].eye.y < min_cam_y) cams[vp].eye.y = min_cam_y; world_ren_.render_world_view(cams[vp], models, parts, vp, split_count, cfg_); diff --git a/src/render/world_geometry.cpp b/src/render/world_geometry.cpp index 0b46ade..ce36085 100644 --- a/src/render/world_geometry.cpp +++ b/src/render/world_geometry.cpp @@ -123,6 +123,17 @@ WorldGeometry build_level_geometry(const assets::LevelData& level) { } // 3D Column Blocks + rebuild_column_geometry(level, geo.column_verts, geo.column_indices); + + return geo; +} + +void rebuild_column_geometry(const assets::LevelData& level, + std::vector& column_verts, + std::vector& column_indices) { + column_verts.clear(); + column_indices.clear(); + for (int z = 0; z < assets::kMapHeight; ++z) { for (int x = 0; x < assets::kMapWidth; ++x) { const assets::MapEntry& m = level.cell(x, z); @@ -152,28 +163,28 @@ WorldGeometry build_level_geometry(const assets::LevelData& level) { // 0: North (-Z) if (!has_block_at(level, x, z - 1, slot)) { - emit_quad(geo.column_verts, geo.column_indices, + emit_quad(column_verts, column_indices, Vec3(fx + 1, t10, fz), Vec3(fx, t00, fz), Vec3(fx, b00, fz), Vec3(fx + 1, b10, fz), Vec3(0, 0, -1), blk.tex[0], blk.attrs[0] & 7u, lgt * 0.90f); } // 1: East (+X) if (!has_block_at(level, x + 1, z, slot)) { - emit_quad(geo.column_verts, geo.column_indices, + emit_quad(column_verts, column_indices, Vec3(fx + 1, t11, fz + 1), Vec3(fx + 1, t10, fz), Vec3(fx + 1, b10, fz), Vec3(fx + 1, b11, fz + 1), Vec3(1, 0, 0), blk.tex[1], blk.attrs[1] & 7u, lgt * 0.85f); } // 2: South (+Z) if (!has_block_at(level, x, z + 1, slot)) { - emit_quad(geo.column_verts, geo.column_indices, + emit_quad(column_verts, column_indices, Vec3(fx, t01, fz + 1), Vec3(fx + 1, t11, fz + 1), Vec3(fx + 1, b11, fz + 1), Vec3(fx, b01, fz + 1), Vec3(0, 0, 1), blk.tex[2], blk.attrs[2] & 7u, lgt * 0.95f); } // 3: West (-X) if (!has_block_at(level, x - 1, z, slot)) { - emit_quad(geo.column_verts, geo.column_indices, + emit_quad(column_verts, column_indices, Vec3(fx, t00, fz), Vec3(fx, t01, fz + 1), Vec3(fx, b01, fz + 1), Vec3(fx, b00, fz), Vec3(-1, 0, 0), blk.tex[3], blk.attrs[3] & 7u, lgt * 0.80f); @@ -184,7 +195,7 @@ WorldGeometry build_level_geometry(const assets::LevelData& level) { Vec3 p2(fx + 1, t11, fz + 1), p3(fx, t01, fz + 1); Vec3 top_n = (p3 - p0).cross(p2 - p0) + (p2 - p0).cross(p1 - p0); top_n = (top_n.len_sq() > 1e-6f) ? top_n.normalized() : Vec3(0, 1, 0); - emit_quad(geo.column_verts, geo.column_indices, + emit_quad(column_verts, column_indices, p0, p1, p2, p3, top_n, blk.tex[4], blk.attrs[4] & 7u, lgt * 1.05f); } @@ -194,15 +205,13 @@ WorldGeometry build_level_geometry(const assets::LevelData& level) { Vec3 p2(fx + 1, b10, fz), p3(fx, b00, fz); Vec3 bot_n = (p3 - p0).cross(p2 - p0) + (p2 - p0).cross(p1 - p0); bot_n = (bot_n.len_sq() > 1e-6f) ? bot_n.normalized() : Vec3(0, -1, 0); - emit_quad(geo.column_verts, geo.column_indices, + emit_quad(column_verts, column_indices, p0, p1, p2, p3, bot_n, blk.tex[5], blk.attrs[5] & 7u, lgt * 0.70f); } } } } - - return geo; } void update_terrain_region(const assets::LevelData& level, int x0, int z0, int x1, int z1, diff --git a/src/render/world_geometry.hpp b/src/render/world_geometry.hpp index 2d1d551..77093d3 100644 --- a/src/render/world_geometry.hpp +++ b/src/render/world_geometry.hpp @@ -49,6 +49,11 @@ WorldGeometry build_level_geometry(const assets::LevelData& level); void update_terrain_region(const assets::LevelData& level, int x0, int z0, int x1, int z1, std::vector& terrain_verts); +// Rebuilds 3D column mesh from current level map & column definitions (for live morphs) +void rebuild_column_geometry(const assets::LevelData& level, + std::vector& column_verts, + std::vector& column_indices); + // Converts a Bullfrog 3D ModelMesh into GPU-ready triangles with face normals ModelGeometry build_model_geometry(const assets::ModelMesh& mesh, const assets::Palette& pal); diff --git a/src/render/world_renderer.cpp b/src/render/world_renderer.cpp index 87620e8..80fa8a2 100644 --- a/src/render/world_renderer.cpp +++ b/src/render/world_renderer.cpp @@ -513,7 +513,7 @@ bool WorldRenderer::load_level(const Vfs& vfs, const assets::LevelData& level, world_geo_ = build_level_geometry(level); upload_world_mesh(terrain_mesh_, world_geo_.terrain_verts, world_geo_.terrain_indices, true); - upload_world_mesh(column_mesh_, world_geo_.column_verts, world_geo_.column_indices, false); + upload_world_mesh(column_mesh_, world_geo_.column_verts, world_geo_.column_indices, true); LOG_INFO("Loaded level %d GPU geometry: terrain_tris=%d, column_tris=%d", level.level_number, terrain_mesh_.index_count / 3, column_mesh_.index_count / 3); @@ -544,6 +544,30 @@ void WorldRenderer::update_terrain_region(const assets::LevelData& level, gl::BindBuffer(GL_ARRAY_BUFFER, 0); } +void WorldRenderer::rebuild_columns(const assets::LevelData& level) { + render::rebuild_column_geometry(level, world_geo_.column_verts, world_geo_.column_indices); + if (world_geo_.column_verts.empty() || world_geo_.column_indices.empty()) { + column_mesh_.index_count = 0; + return; + } + if (!column_mesh_.vao) { + upload_world_mesh(column_mesh_, world_geo_.column_verts, world_geo_.column_indices, true); + return; + } + gl::BindVertexArray(column_mesh_.vao); + gl::BindBuffer(GL_ARRAY_BUFFER, column_mesh_.vbo); + gl::BufferData(GL_ARRAY_BUFFER, + static_cast(world_geo_.column_verts.size() * sizeof(WorldVertex)), + world_geo_.column_verts.data(), GL_DYNAMIC_DRAW); + gl::BindBuffer(GL_ELEMENT_ARRAY_BUFFER, column_mesh_.ebo); + gl::BufferData(GL_ELEMENT_ARRAY_BUFFER, + static_cast(world_geo_.column_indices.size() * sizeof(uint32_t)), + world_geo_.column_indices.data(), GL_DYNAMIC_DRAW); + gl::BindVertexArray(0); + gl::BindBuffer(GL_ARRAY_BUFFER, 0); + column_mesh_.index_count = static_cast(world_geo_.column_indices.size()); +} + bool WorldRenderer::ensure_model(const Vfs& vfs, const std::string& key, const assets::Palette& pal) { if (model_cache_.count(key)) return true; assets::ModelMesh mesh; diff --git a/src/render/world_renderer.hpp b/src/render/world_renderer.hpp index fbc96b6..a3080fc 100644 --- a/src/render/world_renderer.hpp +++ b/src/render/world_renderer.hpp @@ -56,6 +56,7 @@ public: bool load_level(const Vfs& vfs, const assets::LevelData& level, const assets::Palette& pal); void update_terrain_region(const assets::LevelData& level, int x0, int z0, int x1, int z1); + void rebuild_columns(const assets::LevelData& level); // Preloads a 3D model from OBJECTS/DATA/.DAT (cached by key) bool ensure_model(const Vfs& vfs, const std::string& key, const assets::Palette& pal); diff --git a/src/sim/ai_driver.cpp b/src/sim/ai_driver.cpp index 745b2f7..a57a6c7 100644 --- a/src/sim/ai_driver.cpp +++ b/src/sim/ai_driver.cpp @@ -38,6 +38,17 @@ CraftInput compute_ai_input(CraftState& ai, const assets::LevelData& level, ai.ai_seeking_pit = false; } + auto is_ai_blocked = [&](float wx, float wz) -> bool { + int ix = static_cast(std::floor(wx)); + int iz = static_cast(std::floor(wz)); + float local_floor = 0.0f; + float local_ceil = 1e6f; + level.sample_surface_and_ceiling(wx, wz, ai.curr_pose.pos.y, local_floor, local_ceil); + float probe_y = local_floor + 0.28f; + return level.is_cell_solid_at_point(ix, iz, wx, wz, probe_y) || + level.is_cliff_blocking_at_y(ix, iz, ai.curr_pose.pos.y); + }; + // Spread AI racing lines gently so racers overtake instead of forming a single line if (!ai.ai_seeking_pit) { const auto& nxt = level.main_waypoints[(wp_idx + 1) % n_wp]; @@ -45,9 +56,7 @@ CraftInput compute_ai_input(CraftState& ai, const assets::LevelData& level, Vec2 right(-seg.y, seg.x); float lane = (static_cast((ai.id % 5) - 2)) * 0.55f; Vec2 shifted = tgt_pos + right * lane; - if (!level.is_cell_solid_at_y(static_cast(std::floor(shifted.x)), - static_cast(std::floor(shifted.y)), - ai.curr_pose.pos.y)) { + if (!is_ai_blocked(shifted.x, shifted.y)) { tgt_pos = shifted; } } @@ -63,9 +72,7 @@ CraftInput compute_ai_input(CraftState& ai, const assets::LevelData& level, float max_probe = std::min(tgt_dist, 7.5f); for (float d = 1.0f; d <= max_probe; d += 0.75f) { Vec2 p = cur_pos + base_dir * d; - if (level.is_cell_solid_at_y(static_cast(std::floor(p.x)), - static_cast(std::floor(p.y)), - ai.curr_pose.pos.y)) { + if (is_ai_blocked(p.x, p.y)) { hit_dist = d; break; } @@ -84,9 +91,7 @@ CraftInput compute_ai_input(CraftState& ai, const assets::LevelData& level, Vec2 p = cur_pos + base_dir * pd + perp * (side * off); if (p.x < 2.0f || p.x > assets::kMapWidth - 3.0f || p.y < 2.0f || p.y > assets::kMapHeight - 3.0f || - level.is_cell_solid_at_y(static_cast(std::floor(p.x)), - static_cast(std::floor(p.y)), - ai.curr_pose.pos.y)) { + is_ai_blocked(p.x, p.y)) { clear = false; break; } diff --git a/src/sim/combat.cpp b/src/sim/combat.cpp index c9d8e42..f55f3cc 100644 --- a/src/sim/combat.cpp +++ b/src/sim/combat.cpp @@ -130,7 +130,8 @@ void step_combat_and_world( std::vector& events, bool& out_race_over, int& out_winner_id, - float dt) { + float dt, + std::vector* out_projectile_traces) { // 1. Recovery craft state machine & weapon firing per craft for (size_t i = 0; i < crafts.size(); ++i) { @@ -284,13 +285,23 @@ void step_combat_and_world( particles.push_back({pr.pos, Vec3(0, 0, 0), 0.14f, Vec4(1.0f, 0.95f, 0.4f, 0.9f), 0.06f, 0.06f}); } + Vec3 prev_pos = pr.pos; pr.pos += pr.vel * dt; + if (out_projectile_traces != nullptr) { + int owner_player = -1; + if (pr.owner_id >= 0 && pr.owner_id < static_cast(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}); + } // World/Column/Ground impact float gy = level.sample_height_world(pr.pos.x, pr.pos.z); + int ix = static_cast(std::floor(pr.pos.x)); + int iz = static_cast(std::floor(pr.pos.z)); if (pr.pos.y <= gy || - level.is_cell_solid_at_y(static_cast(std::floor(pr.pos.x)), - static_cast(std::floor(pr.pos.z)), pr.pos.y)) { + level.is_cell_solid_at_point(ix, iz, pr.pos.x, pr.pos.z, pr.pos.y, 0.10f)) { pr.active = false; if (pr.is_missile) { spawn_explosion(particles, pr.pos, 0.9f); @@ -367,6 +378,9 @@ void step_combat_and_world( // 3. Pickups collection & respawn for (auto& pk : pickups) { + if (!pk.group_unlocked) { + continue; + } if (!pk.active) { pk.respawn_timer -= dt; if (pk.respawn_timer <= 0.0f) pk.active = true; diff --git a/src/sim/combat.hpp b/src/sim/combat.hpp index e0c68f9..2f25ce1 100644 --- a/src/sim/combat.hpp +++ b/src/sim/combat.hpp @@ -25,7 +25,8 @@ void step_combat_and_world( std::vector& events, bool& out_race_over, int& out_winner_id, - float dt); + float dt, + std::vector* out_projectile_traces = nullptr); // Computes visual Recovery Helicopter (RECOV0-0) positions for any craft under repair std::vector get_recovery_visuals(const std::vector& crafts); diff --git a/src/sim/physics.cpp b/src/sim/physics.cpp index 187a0e9..1960a1c 100644 --- a/src/sim/physics.cpp +++ b/src/sim/physics.cpp @@ -1,6 +1,7 @@ #include "sim/physics.hpp" #include +#include #include namespace top_octane::sim { @@ -84,21 +85,26 @@ bool resolve_world_collisions(CraftState& craft, const assets::LevelData& level, } } - // 2-pass seam-free 3D Column AABB vs Craft Circle pushout - for (int pass = 0; pass < 2; ++pass) { + auto is_obstacle = [&](int nx, int nz, float qx, float qz, float y) -> bool { + return level.is_cell_solid_at_point(nx, nz, qx, qz, y) || + level.is_cliff_blocking_at_y(nx, nz, y); + }; + + // 3-pass seam-free 3D Column + Cliff AABB vs Craft Circle pushout + for (int pass = 0; pass < 3; ++pass) { int cx = static_cast(std::floor(craft.curr_pose.pos.x)); int cz = static_cast(std::floor(craft.curr_pose.pos.z)); for (int nz = cz - 1; nz <= cz + 1; ++nz) { for (int nx = cx - 1; nx <= cx + 1; ++nx) { - if (!level.is_cell_solid_at_y(nx, nz, craft.curr_pose.pos.y)) { - continue; - } float min_x = static_cast(nx); float max_x = min_x + 1.0f; float min_z = static_cast(nz); float max_z = min_z + 1.0f; float closest_x = std::clamp(craft.curr_pose.pos.x, min_x, max_x); float closest_z = std::clamp(craft.curr_pose.pos.z, min_z, max_z); + if (!is_obstacle(nx, nz, closest_x, closest_z, craft.curr_pose.pos.y)) { + continue; + } float dx = craft.curr_pose.pos.x - closest_x; float dz = craft.curr_pose.pos.z - closest_z; float dist_sq = dx * dx + dz * dz; @@ -113,47 +119,66 @@ bool resolve_world_collisions(CraftState& craft, const assets::LevelData& level, if (!inside_box) { float dist = std::sqrt(dist_sq); norm = Vec2(dx / dist, dz / dist); - pen = radius - dist; - } else { - float dL = std::abs(craft.curr_pose.pos.x - min_x); - float dR = std::abs(max_x - craft.curr_pose.pos.x); - float dT = std::abs(craft.curr_pose.pos.z - min_z); - float dB = std::abs(max_z - craft.curr_pose.pos.z); - float md = std::min({dL, dR, dT, dB}); - if (md == dL) norm = Vec2(-1, 0); - else if (md == dR) norm = Vec2(1, 0); - else if (md == dT) norm = Vec2(0, -1); - else norm = Vec2(0, 1); - pen = radius + md; - } - - // Suppress internal AABB seams between adjacent solid columns of compatible height - float h_here = level.cell_height_world(nx, nz); - if (std::abs(norm.x) > 0.15f) { - int adj_x = nx + (norm.x > 0.0f ? 1 : -1); - if (level.is_cell_solid_at_y(adj_x, nz, craft.curr_pose.pos.y) && - std::abs(level.cell_height_world(adj_x, nz) - h_here) < 0.35f) { - norm.x = 0.0f; + // Suppress internal seams only from outside, checking adjacent cells at exact shared edge point + if (std::abs(norm.x) > 0.15f) { + int adj_x = nx + (norm.x > 0.0f ? 1 : -1); + if (is_obstacle(adj_x, nz, closest_x, closest_z, craft.curr_pose.pos.y)) { + norm.x = 0.0f; + } } - } - if (std::abs(norm.y) > 0.15f) { - int adj_z = nz + (norm.y > 0.0f ? 1 : -1); - if (level.is_cell_solid_at_y(nx, adj_z, craft.curr_pose.pos.y) && - std::abs(level.cell_height_world(nx, adj_z) - h_here) < 0.35f) { - norm.y = 0.0f; + if (std::abs(norm.y) > 0.15f) { + int adj_z = nz + (norm.y > 0.0f ? 1 : -1); + if (is_obstacle(nx, adj_z, closest_x, closest_z, craft.curr_pose.pos.y)) { + norm.y = 0.0f; + } } - } - - float nlen = std::sqrt(norm.x * norm.x + norm.y * norm.y); - if (nlen < 1e-4f) { - continue; - } - norm.x /= nlen; - norm.y /= nlen; - - if (!inside_box) { + float nlen = std::sqrt(norm.x * norm.x + norm.y * norm.y); + if (nlen < 1e-4f) { + continue; + } + norm.x /= nlen; + norm.y /= nlen; pen = radius - (dx * norm.x + dz * norm.y); if (pen <= 0.0f) continue; + } else { + struct ExitCand { + float dist; + Vec2 n; + int ax; + int az; + }; + const ExitCand cands[4] = { + {std::abs(craft.curr_pose.pos.x - min_x), Vec2(-1.0f, 0.0f), nx - 1, nz}, + {std::abs(max_x - craft.curr_pose.pos.x), Vec2(1.0f, 0.0f), nx + 1, nz}, + {std::abs(craft.curr_pose.pos.z - min_z), Vec2(0.0f, -1.0f), nx, nz - 1}, + {std::abs(max_z - craft.curr_pose.pos.z), Vec2(0.0f, 1.0f), nx, nz + 1}, + }; + bool is_col = level.is_cell_solid_at_point(nx, nz, closest_x, closest_z, + craft.curr_pose.pos.y); + int best_tier = -1; // 2 = downhill open (for cliffs), 1 = open, 0 = fallback min dist + float best_d = 1e9f; + Vec2 best_n(0.0f, -1.0f); + for (const auto& c : cands) { + bool open = !is_obstacle(c.ax, c.az, static_cast(c.ax) + 0.5f, + static_cast(c.az) + 0.5f, craft.curr_pose.pos.y); + int tier = 0; + if (open) { + tier = 1; + if (!is_col && + level.sample_height_world(static_cast(c.ax) + 0.5f, + static_cast(c.az) + 0.5f) <= + craft.curr_pose.pos.y + 0.35f) { + tier = 2; + } + } + if (tier > best_tier || (tier == best_tier && c.dist < best_d)) { + best_tier = tier; + best_d = c.dist; + best_n = c.n; + } + } + norm = best_n; + pen = radius + best_d; } craft.curr_pose.pos.x += norm.x * pen; @@ -174,8 +199,123 @@ bool resolve_world_collisions(CraftState& craft, const assets::LevelData& level, } } - // Steep cliff deflection (sheer cliffs have surf_n.y < 0.49; steepest Track 5 road is ~0.535) - if (!craft.airborne) { + // Guaranteed non-penetration escape if still inside a solid column cell + { + int cx = static_cast(std::floor(craft.curr_pose.pos.x)); + int cz = static_cast(std::floor(craft.curr_pose.pos.z)); + if (level.is_cell_solid_at_point(cx, cz, craft.curr_pose.pos.x, craft.curr_pose.pos.z, + craft.curr_pose.pos.y)) { + bool escaped = false; + const auto& m = level.cell(cx, cz); + if (m.tex_or_col < 0) { + int col_idx = -m.tex_or_col; + if (col_idx > 0 && col_idx < assets::kMaxColumns) { + const auto& col = level.columns[col_idx]; + float ground_y = level.sample_height_world(craft.curr_pose.pos.x, craft.curr_pose.pos.z); + float min_y = craft.curr_pose.pos.y - 0.05f; + float max_y = craft.curr_pose.pos.y + 0.42f; + // 1. Pop onto column/bridge top if close to top face + for (int s = 7; s >= 0; --s) { + if ((col.shape_mask & (1u << s)) == 0) continue; + float slot_bot = ground_y + static_cast(s); + float slot_top = slot_bot + 1.0f; + if (slot_top > min_y && slot_bot < max_y && + (slot_top - craft.curr_pose.pos.y) <= 0.48f) { + float pop_y = slot_top + spec.hover_offset; + if (!level.is_cell_solid_at_point(cx, cz, craft.curr_pose.pos.x, + craft.curr_pose.pos.z, pop_y)) { + craft.curr_pose.pos.y = pop_y; + craft.vel.y = std::max(0.0f, craft.vel.y); + escaped = true; + hit = true; + break; + } + } + } + // 2. Or clamp cleanly under an overhead gantry block if room underneath + if (!escaped) { + for (int s = 0; s < 8; ++s) { + if ((col.shape_mask & (1u << s)) == 0) continue; + float slot_bot = ground_y + static_cast(s); + float under_y = slot_bot - 0.46f; + if (under_y >= ground_y + 0.14f && + !level.is_cell_solid_at_point(cx, cz, craft.curr_pose.pos.x, + craft.curr_pose.pos.z, under_y)) { + craft.curr_pose.pos.y = under_y; + craft.vel.y = std::min(0.0f, craft.vel.y); + escaped = true; + hit = true; + break; + } + } + } + } + } + // 3. Fixed-buffer BFS (radius up to 6 tiles) to nearest open cell + if (!escaped) { + constexpr int kR = 6; + constexpr int kSpan = 2 * kR + 1; + std::array vis{}; + std::array qx{}; + std::array qz{}; + int head = 0, tail = 0; + vis[kR * kSpan + kR] = true; + qx[tail] = cx; + qz[tail] = cz; + ++tail; + while (head < tail && !escaped) { + int ux = qx[head]; + int uz = qz[head]; + ++head; + if (ux != cx || uz != cz) { + float tx = static_cast(ux) + 0.5f; + float tz = static_cast(uz) + 0.5f; + if (!is_obstacle(ux, uz, tx, tz, craft.curr_pose.pos.y)) { + craft.curr_pose.pos.x = tx; + craft.curr_pose.pos.z = tz; + craft.vel.x *= 0.25f; + craft.vel.z *= 0.25f; + float esc_floor = 0.0f, esc_ceil = 1e6f; + level.sample_surface_and_ceiling(tx, tz, craft.curr_pose.pos.y, esc_floor, esc_ceil); + if (craft.curr_pose.pos.y < esc_floor + spec.hover_offset) { + craft.curr_pose.pos.y = esc_floor + spec.hover_offset; + craft.vel.y = std::max(0.0f, craft.vel.y); + } + escaped = true; + hit = true; + break; + } + } + static constexpr int kDx[4] = {-1, 1, 0, 0}; + static constexpr int kDz[4] = {0, 0, -1, 1}; + for (int d = 0; d < 4; ++d) { + int vx = ux + kDx[d]; + int vz = uz + kDz[d]; + if (vx < 1 || vx > assets::kMapWidth - 2 || + vz < 1 || vz > assets::kMapHeight - 2) { + continue; + } + int rx = vx - cx + kR; + int rz = vz - cz + kR; + if (rx < 0 || rx >= kSpan || rz < 0 || rz >= kSpan) continue; + int vidx = rz * kSpan + rx; + if (!vis[vidx]) { + vis[vidx] = true; + qx[tail] = vx; + qz[tail] = vz; + ++tail; + } + } + } + } + } + } + + // Gentle steep slope deflection for intermediate slopes (ny < 0.49) + float post_floor_y = 0.0f, post_ceil_y = 1e6f; + level.sample_surface_and_ceiling(craft.curr_pose.pos.x, craft.curr_pose.pos.z, + craft.curr_pose.pos.y, post_floor_y, post_ceil_y); + if (!craft.airborne || craft.curr_pose.pos.y < post_floor_y + 0.55f) { Vec3 surf_n = level.sample_normal_world(craft.curr_pose.pos.x, craft.curr_pose.pos.z); if (surf_n.y < 0.49f) { float hn_len = std::sqrt(surf_n.x * surf_n.x + surf_n.z * surf_n.z); @@ -295,14 +435,23 @@ CraftStepFeedback step_craft_physics(CraftState& craft, const CraftInput& input, craft.vel += fwd * (throttle * accel * 0.25f * dt); } - // Sub-stepped integration + column/wall collision (prevents tunneling at top speed) + // Sub-stepped integration + column/wall collision (horizontal pushout BEFORE floor sampling!) constexpr int kSubSteps = 2; float sub_dt = dt / static_cast(kSubSteps); for (int s = 0; s < kSubSteps; ++s) { craft.curr_pose.pos.x += craft.vel.x * sub_dt; craft.curr_pose.pos.z += craft.vel.z * sub_dt; - // Vertical hover cushion vs gravity (supports elevated columns and overhead gantries) + // 1. Resolve horizontal wall/cliff collisions before sampling floor height + float sub_impact = 0.0f; + bool sub_scraping = false; + if (resolve_world_collisions(craft, level, sub_impact, sub_scraping)) { + fb.collided_wall = true; + fb.impact_speed = std::max(fb.impact_speed, sub_impact); + fb.scraping |= sub_scraping; + } + + // 2. Vertical hover cushion vs gravity at validated (x, z) float floor_y = 0.0f; float ceil_y = 1e6f; level.sample_surface_and_ceiling(craft.curr_pose.pos.x, craft.curr_pose.pos.z, @@ -371,12 +520,28 @@ CraftStepFeedback step_craft_physics(CraftState& craft, const CraftInput& input, } } - float sub_impact = 0.0f; - bool sub_scraping = false; - if (resolve_world_collisions(craft, level, sub_impact, sub_scraping)) { - fb.collided_wall = true; - fb.impact_speed = std::max(fb.impact_speed, sub_impact); - fb.scraping |= sub_scraping; + // 3. Re-clamp ceiling and re-resolve if vertical motion entered an overhang lip + float post_floor = 0.0f, post_ceil = 1e6f; + level.sample_surface_and_ceiling(craft.curr_pose.pos.x, craft.curr_pose.pos.z, + craft.curr_pose.pos.y, post_floor, post_ceil); + if (post_ceil < 1e5f) { + float max_craft_y = std::max(post_floor + 0.14f, post_ceil - 0.46f); + if (craft.curr_pose.pos.y > max_craft_y) { + craft.curr_pose.pos.y = max_craft_y; + if (craft.vel.y > 0.0f) craft.vel.y = 0.0f; + } + } + int cell_x = static_cast(std::floor(craft.curr_pose.pos.x)); + int cell_z = static_cast(std::floor(craft.curr_pose.pos.z)); + if (level.is_cell_solid_at_point(cell_x, cell_z, craft.curr_pose.pos.x, + craft.curr_pose.pos.z, craft.curr_pose.pos.y)) { + float post_imp = 0.0f; + bool post_scr = false; + if (resolve_world_collisions(craft, level, post_imp, post_scr)) { + fb.collided_wall = true; + fb.impact_speed = std::max(fb.impact_speed, post_imp); + fb.scraping |= post_scr; + } } } diff --git a/src/sim/race_sim.cpp b/src/sim/race_sim.cpp index 72b241e..77e7c7f 100644 --- a/src/sim/race_sim.cpp +++ b/src/sim/race_sim.cpp @@ -31,6 +31,29 @@ bool segments_intersect_or_near(Vec2 p0, Vec2 p1, Vec2 g0, Vec2 g1, float margin return false; } +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); +} + } // namespace bool RaceSim::start_race(const assets::LevelData& level, const RaceSetup& setup, @@ -49,7 +72,6 @@ bool RaceSim::start_race(const assets::LevelData& level, const RaceSetup& setup, elapsed_time_ = 0.0f; race_over_ = false; winner_id_ = -1; - terrain_dirty_ = false; // Populate pickups (type 5) & props (type 3) if (setup_.pickups_enabled) { @@ -57,6 +79,9 @@ bool RaceSim::start_race(const assets::LevelData& level, const RaceSetup& setup, PickupItem p; p.entity_id = e.id; p.subtype = e.subtype; + p.group = e.group; + p.group_unlocked = (e.group <= 1); + p.active = p.group_unlocked; float tx = e.tile_x() + 0.5f; float tz = e.tile_z() + 0.5f; p.pos = Vec3(tx, level_.sample_height_world(tx, tz) + 0.45f, tz); @@ -74,7 +99,35 @@ bool RaceSim::start_race(const assets::LevelData& level, const RaceSetup& setup, pr.pos = Vec3(tx, level_.sample_height_world(tx, tz) + 0.25f, tz); props_.push_back(pr); } - trigger_fired_.assign(level_.triggers.size(), false); + + trigger_states_.assign(level_.triggers.size(), TriggerRuntime{}); + for (size_t i = 0; i < level_.triggers.size(); ++i) { + const auto& tr = level_.triggers[i]; + auto& ts = trigger_states_[i]; + ts.armed = (tr.group <= 1); + if (tr.subtype == 1) { + ts.timer = std::max(0.10f, static_cast(tr.value) / 25.0f); + } + } + morph_is_repeating_.assign(level_.morphs.size(), false); + morph_fired_once_.assign(level_.morphs.size(), false); + for (size_t mi = 0; mi < level_.morphs.size(); ++mi) { + const auto& m = level_.morphs[mi]; + if (m.permanent) { + morph_is_repeating_[mi] = true; + continue; + } + for (const auto& tr : level_.triggers) { + if (tr.subtype == 1 && tr.target_group == m.group) { + morph_is_repeating_[mi] = true; + break; + } + } + } + active_morphs_.clear(); + dirty_rects_.clear(); + columns_dirty_ = false; + morph_banner_cd_ = 0.0f; int total_racers = 1; if (setup_.mode == GameMode::SplitScreen) { @@ -258,6 +311,8 @@ void RaceSim::step(const CraftInput& p1_input, const CraftInput& p2_input, float } elapsed_time_ += dt; + update_timed_triggers(dt); + update_active_morphs(dt); std::vector inputs(crafts_.size()); for (size_t i = 0; i < crafts_.size(); ++i) { @@ -316,8 +371,10 @@ void RaceSim::step(const CraftInput& p1_input, const CraftInput& p2_input, float } // Combat, projectiles, pickups, props, and recovery craft + std::vector projectile_traces; step_combat_and_world(setup_, level_, crafts_, inputs, projectiles_, pickups_, props_, - particles_, events_, race_over_, winner_id_, dt); + particles_, events_, race_over_, winner_id_, dt, &projectile_traces); + update_rocket_triggers(projectile_traces); // Multi-iteration craft-to-craft + world re-projection solver so squeezed crafts never clip walls for (int iter = 0; iter < 2; ++iter) { @@ -439,31 +496,397 @@ void RaceSim::update_recharge_pads(CraftState& c, float dt) { } } -void RaceSim::update_triggers(CraftState& c) { - if (!setup_.track_morphing_enabled) return; - for (size_t i = 0; i < level_.triggers.size(); ++i) { - if (trigger_fired_[i]) continue; - const auto& tr = level_.triggers[i]; - float dx = c.curr_pose.pos.x - (tr.tile_x() + 0.5f); - float dz = c.curr_pose.pos.z - (tr.tile_z() + 0.5f); - if (dx * dx + dz * dz < 4.0f) { - trigger_fired_[i] = true; - // Subtle dynamic terrain pulse around trigger zone so morph events visibly deform track! - int cx = static_cast(tr.tile_x()); - int cz = static_cast(tr.tile_z()); - for (int z = std::max(1, cz - 2); z <= std::min(assets::kMapHeight - 2, cz + 2); ++z) { - for (int x = std::max(1, cx - 2); x <= std::min(assets::kMapWidth - 2, cx + 2); ++x) { - auto& cell = level_.cell_mut(x, z); - cell.height = static_cast(std::min(cell.height + 160, 4080)); +void RaceSim::push_dirty_rect(int x0, int z0, int x1, int z1) { + int cx0 = std::clamp(x0, 0, assets::kMapWidth - 2); + int cz0 = std::clamp(z0, 0, assets::kMapHeight - 2); + int cx1 = std::clamp(x1, 0, assets::kMapWidth - 2); + int cz1 = std::clamp(z1, 0, assets::kMapHeight - 2); + if (cx0 > cx1 || cz0 > cz1) return; + + for (int z = cz0; z <= cz1 + 1 && !columns_dirty_; ++z) { + for (int x = cx0; x <= cx1 + 1; ++x) { + if (level_.cell(x, z).tex_or_col < 0) { + columns_dirty_ = true; + break; + } + } + } + if (!dirty_rects_.empty()) { + const auto& prev = dirty_rects_.back(); + if (prev.x0 == cx0 && prev.z0 == cz0 && prev.x1 == cx1 && prev.z1 == cz1) { + return; + } + } + dirty_rects_.push_back({cx0, cz0, cx1, cz1}); +} + +void RaceSim::apply_morph_fraction(const ActiveMorph& am, float s) { + if (am.morph_idx >= level_.morphs.size()) return; + const auto& m = level_.morphs[am.morph_idx]; + int w = m.size_x + 1; + int h = m.size_z + 1; + for (int oz = 0; oz < h; ++oz) { + for (int ox = 0; ox < w; ++ox) { + size_t idx = static_cast(oz) * static_cast(w) + static_cast(ox); + const auto& da = am.orig_dst[idx]; + const auto& sb = am.orig_src[idx]; + auto& dc = level_.cell_mut(m.dst_x + ox, m.dst_z + oz); + auto& sc = level_.cell_mut(m.src_x + ox, m.src_z + oz); + dc.height = static_cast( + std::lround(lerp(static_cast(da.height), static_cast(sb.height), s))); + dc.illumination = static_cast( + std::lround(lerp(static_cast(da.illumination), static_cast(sb.illumination), s))); + sc.height = static_cast( + std::lround(lerp(static_cast(sb.height), static_cast(da.height), s))); + sc.illumination = static_cast( + std::lround(lerp(static_cast(sb.illumination), static_cast(da.illumination), s))); + } + } + push_dirty_rect(m.dst_x - 2, m.dst_z - 2, m.dst_x + m.size_x + 1, m.dst_z + m.size_z + 1); + push_dirty_rect(m.src_x - 2, m.src_z - 2, m.src_x + m.size_x + 1, m.src_z + m.size_z + 1); +} + +void RaceSim::activate_group(uint16_t group, int actor_player, bool allow_banner, bool fast_morph) { + if (group == 0) return; + + // 1. Unlock hidden pickups belonging to this group + for (auto& pk : pickups_) { + if (pk.group == group && !pk.group_unlocked) { + pk.group_unlocked = true; + pk.active = true; + pk.respawn_timer = 0.0f; + pk.pos.y = level_.sample_height_world(pk.pos.x, pk.pos.z) + 0.45f; + } + } + + // 2. Spawn group environmental effects (explosions, steam jets, blast hazards) + bool emitted_explosion_sfx = false; + for (const auto& ef : level_.effects) { + if (ef.group != group) continue; + float gy = level_.sample_height_world(ef.x, ef.z); + Vec3 epos(ef.x, gy + 0.45f, ef.z); + if (ef.subtype == 1 || ef.subtype == 3) { + for (int k = 0; k < 10; ++k) { + float ang = kTwoPi * (static_cast(k) / 10.0f); + float spd = 1.6f + static_cast((k * 37) % 10) * 0.28f; + Vec3 v(std::cos(ang) * spd, 1.4f + static_cast(k % 3) * 0.9f, + std::sin(ang) * spd); + Vec4 col = (k % 2 == 0) ? Vec4(1.0f, 0.65f, 0.15f, 1.0f) + : Vec4(1.0f, 0.25f, 0.05f, 1.0f); + particles_.push_back({epos, v, 0.65f, col, 0.50f, 0.50f}); + } + if (!emitted_explosion_sfx) { + events_.push_back({SimSoundCue::Explosion, epos, actor_player, "", 0.40f}); + emitted_explosion_sfx = true; + } + } else if (ef.subtype == 5 || ef.subtype == 8) { + int count = (ef.subtype == 5) ? 6 : 3; + for (int k = 0; k < count; ++k) { + float ang = kTwoPi * (static_cast(k) / static_cast(count)); + Vec3 v(std::cos(ang) * 0.5f, 2.2f + static_cast(k % 2) * 0.8f, + std::sin(ang) * 0.5f); + particles_.push_back({Vec3(ef.x, gy + 0.35f, ef.z), v, + 0.48f, Vec4(0.85f, 0.92f, 1.0f, 0.72f), 0.45f, 0.45f}); + } + } else if (ef.subtype == 2) { + for (int k = 0; k < 6; ++k) { + float ang = kTwoPi * (static_cast(k) / 6.0f); + particles_.push_back({epos, Vec3(std::cos(ang) * 1.4f, 1.5f, std::sin(ang) * 1.4f), + 0.45f, Vec4(1.0f, 0.45f, 0.15f, 0.9f), 0.35f, 0.35f}); + } + if (setup_.damage_enabled) { + for (auto& c : crafts_) { + if (c.recovery_state != RecoveryState::Active) continue; + float dx = c.curr_pose.pos.x - ef.x; + float dz = c.curr_pose.pos.z - ef.z; + if (dx * dx + dz * dz <= 1.5f * 1.5f) { + c.shield = std::max(1.0f, c.shield - 10.0f); + c.damage_flash = 0.22f; + } } } - terrain_dirty_ = true; - dirty_x0_ = std::max(0, cx - 3); - dirty_z0_ = std::max(0, cz - 3); - dirty_x1_ = std::min(assets::kMapWidth - 2, cx + 3); - dirty_z1_ = std::min(assets::kMapHeight - 2, cz + 3); - events_.push_back({SimSoundCue::SwitchTrigger, c.curr_pose.pos, c.player_index, - "SECTOR MORPHING", 0.35f}); + } + } + + // 3. Arm or re-arm triggers belonging to this group + for (size_t j = 0; j < level_.triggers.size(); ++j) { + const auto& tr = level_.triggers[j]; + if (tr.group != group) continue; + auto& ts = trigger_states_[j]; + if (tr.subtype == 1) { + ts.armed = true; + ts.fired = false; + ts.timer = std::max(0.10f, static_cast(tr.value) / 25.0f); + } else if (!ts.fired) { + ts.armed = true; + } + } + + // 4. Execute bidirectional sector morphs belonging to this group + if (!setup_.track_morphing_enabled) { + return; + } + + bool announced_banner = false; + for (size_t mi = 0; mi < level_.morphs.size(); ++mi) { + if (level_.morphs[mi].group != group) continue; + if (!morph_is_repeating_[mi] && morph_fired_once_[mi]) continue; + morph_fired_once_[mi] = true; + + for (size_t ai = 0; ai < active_morphs_.size(); ++ai) { + if (active_morphs_[ai].morph_idx == mi) { + apply_morph_fraction(active_morphs_[ai], 1.0f); + active_morphs_.erase(active_morphs_.begin() + static_cast(ai)); + break; + } + } + + const auto& m = level_.morphs[mi]; + int w = m.size_x + 1; + int h = m.size_z + 1; + ActiveMorph am; + am.morph_idx = mi; + am.elapsed = 0.0f; + am.orig_dst.resize(static_cast(w) * static_cast(h)); + am.orig_src.resize(static_cast(w) * static_cast(h)); + + bool height_differs = false; + bool tex_or_col_differs = false; + bool light_or_flag_differs = false; + for (int oz = 0; oz < h; ++oz) { + for (int ox = 0; ox < w; ++ox) { + size_t idx = static_cast(oz) * static_cast(w) + static_cast(ox); + assets::MapEntry da = level_.cell(m.dst_x + ox, m.dst_z + oz); + assets::MapEntry sb = level_.cell(m.src_x + ox, m.src_z + oz); + am.orig_dst[idx] = da; + am.orig_src[idx] = sb; + if (da.height != sb.height) height_differs = true; + if (da.tex_or_col != sb.tex_or_col) tex_or_col_differs = true; + if (da.illumination != sb.illumination || da.flags != sb.flags) light_or_flag_differs = true; + if (da.tex_or_col < 0 || sb.tex_or_col < 0) columns_dirty_ = true; + + auto& dc = level_.cell_mut(m.dst_x + ox, m.dst_z + oz); + auto& sc = level_.cell_mut(m.src_x + ox, m.src_z + oz); + dc.tex_or_col = sb.tex_or_col; + dc.flags = sb.flags; + sc.tex_or_col = da.tex_or_col; + sc.flags = da.flags; + } + } + + if (!height_differs && !tex_or_col_differs && !light_or_flag_differs) { + continue; + } + + am.duration = height_differs ? (fast_morph ? 0.35f : 0.50f) : 0.0f; + push_dirty_rect(m.dst_x - 2, m.dst_z - 2, m.dst_x + m.size_x + 1, m.dst_z + m.size_z + 1); + push_dirty_rect(m.src_x - 2, m.src_z - 2, m.src_x + m.size_x + 1, m.src_z + m.size_z + 1); + + if (am.duration <= 0.0f) { + apply_morph_fraction(am, 1.0f); + } else { + active_morphs_.push_back(std::move(am)); + } + + if (height_differs || tex_or_col_differs) { + float cx = static_cast(m.dst_x) + static_cast(m.size_x + 1) * 0.5f; + float cz = static_cast(m.dst_z) + static_cast(m.size_z + 1) * 0.5f; + Vec3 center(cx, level_.sample_height_world(cx, cz), cz); + std::string banner; + float rumble = 0.0f; + int target_p = -1; + if (allow_banner && !announced_banner && morph_banner_cd_ <= 0.0f) { + banner = "SECTOR MORPHING"; + rumble = 0.35f; + target_p = actor_player; + morph_banner_cd_ = 1.5f; + announced_banner = true; + } + events_.push_back({SimSoundCue::SwitchTrigger, center, target_p, banner, rumble}); + } + } +} + +void RaceSim::update_timed_triggers(float dt) { + if (morph_banner_cd_ > 0.0f) { + morph_banner_cd_ = std::max(0.0f, morph_banner_cd_ - dt); + } + for (size_t i = 0; i < level_.triggers.size(); ++i) { + const auto& tr = level_.triggers[i]; + auto& ts = trigger_states_[i]; + if (ts.pending) { + ts.timer -= dt; + if (ts.timer <= 0.0f) { + ts.pending = false; + activate_group(tr.target_group, ts.pending_player, true, tr.subtype == 3); + } + } else if (tr.subtype == 1 && ts.armed) { + ts.timer -= dt; + if (ts.timer <= 0.0f) { + ts.armed = false; + activate_group(tr.target_group, -1, false, false); + } + } + } +} + +void RaceSim::update_rocket_triggers(const std::vector& traces) { + if (traces.empty()) return; + for (size_t i = 0; i < level_.triggers.size(); ++i) { + const auto& tr = level_.triggers[i]; + if (tr.subtype != 3) continue; + auto& ts = trigger_states_[i]; + if (!ts.armed || ts.fired) continue; + + float x0 = tr.tile_x() - 1.0f; + float x1 = tr.tile_x() + static_cast(tr.aux1) + 2.0f; + float z0 = tr.tile_z() - 1.0f; + float z1 = tr.tile_z() + static_cast(tr.aux2) + 2.0f; + float gy = level_.sample_height_world(0.5f * (x0 + x1), 0.5f * (z0 + z1)); + float y0 = gy - 1.0f; + float y1 = gy + 4.5f; + + for (const auto& trace : traces) { + if (!segment_intersects_aabb(trace.p0, trace.p1, x0, x1, y0, y1, z0, z1)) { + continue; + } + ts.hit_accum += (trace.is_missile ? 3 : 1); + if (ts.hit_accum >= 3) { + ts.fired = true; + ts.armed = false; + if (tr.value > 0) { + ts.pending = true; + ts.timer = static_cast(tr.value) / 25.0f; + ts.pending_player = trace.owner_player; + } else { + activate_group(tr.target_group, trace.owner_player, true, true); + } + break; + } + } + } +} + +void RaceSim::update_active_morphs(float dt) { + if (active_morphs_.empty()) return; + + std::vector touched; + std::vector repeat_permanent; + for (size_t ai = 0; ai < active_morphs_.size();) { + auto& am = active_morphs_[ai]; + am.elapsed += dt; + float t = (am.duration > 1e-4f) ? std::clamp(am.elapsed / am.duration, 0.0f, 1.0f) : 1.0f; + float s = t * t * (3.0f - 2.0f * t); + apply_morph_fraction(am, s); + + if (am.morph_idx < level_.morphs.size()) { + const auto& m = level_.morphs[am.morph_idx]; + touched.push_back({m.dst_x, m.dst_z, m.dst_x + m.size_x, m.dst_z + m.size_z}); + touched.push_back({m.src_x, m.src_z, m.src_x + m.size_x, m.src_z + m.size_z}); + if (t >= 1.0f && m.permanent) { + repeat_permanent.push_back(am.morph_idx); + } + } + + if (t >= 1.0f) { + active_morphs_.erase(active_morphs_.begin() + static_cast(ai)); + } else { + ++ai; + } + } + + auto touches_any = [&](float wx, float wz, float pad) -> bool { + for (const auto& r : touched) { + if (wx >= static_cast(r.x0) - pad && wx <= static_cast(r.x1 + 1) + pad && + wz >= static_cast(r.z0) - pad && wz <= static_cast(r.z1 + 1) + pad) { + return true; + } + } + return false; + }; + + for (auto& pk : pickups_) { + if (touches_any(pk.pos.x, pk.pos.z, 1.0f)) { + pk.pos.y = level_.sample_height_world(pk.pos.x, pk.pos.z) + 0.45f; + } + } + for (auto& pr : props_) { + if (touches_any(pr.pos.x, pr.pos.z, 1.0f)) { + pr.pos.y = level_.sample_height_world(pr.pos.x, pr.pos.z) + 0.25f; + } + } + for (auto& c : crafts_) { + if (c.recovery_state != RecoveryState::Active) continue; + if (touches_any(c.curr_pose.pos.x, c.curr_pose.pos.z, 1.5f)) { + float imp = 0.0f; + bool scr = false; + resolve_world_collisions(c, level_, imp, scr); + float fy = 0.0f, cy = 1e6f; + level_.sample_surface_and_ceiling(c.curr_pose.pos.x, c.curr_pose.pos.z, + c.curr_pose.pos.y, fy, cy); + if (c.curr_pose.pos.y < fy + 0.14f) { + c.curr_pose.pos.y = fy + kVehicleSpecs[std::clamp(c.vehicle_idx, 0, 5)].hover_offset; + } + } + } + + for (size_t mi : repeat_permanent) { + if (mi >= level_.morphs.size()) continue; + const auto& m = level_.morphs[mi]; + int w = m.size_x + 1; + int h = m.size_z + 1; + ActiveMorph rev; + rev.morph_idx = mi; + rev.elapsed = 0.0f; + rev.duration = 0.50f; + rev.orig_dst.resize(static_cast(w) * static_cast(h)); + rev.orig_src.resize(static_cast(w) * static_cast(h)); + for (int oz = 0; oz < h; ++oz) { + for (int ox = 0; ox < w; ++ox) { + size_t idx = static_cast(oz) * static_cast(w) + static_cast(ox); + assets::MapEntry da = level_.cell(m.dst_x + ox, m.dst_z + oz); + assets::MapEntry sb = level_.cell(m.src_x + ox, m.src_z + oz); + rev.orig_dst[idx] = da; + rev.orig_src[idx] = sb; + if (da.tex_or_col < 0 || sb.tex_or_col < 0) columns_dirty_ = true; + auto& dc = level_.cell_mut(m.dst_x + ox, m.dst_z + oz); + auto& sc = level_.cell_mut(m.src_x + ox, m.src_z + oz); + dc.tex_or_col = sb.tex_or_col; + dc.flags = sb.flags; + sc.tex_or_col = da.tex_or_col; + sc.flags = da.flags; + } + } + push_dirty_rect(m.dst_x - 2, m.dst_z - 2, m.dst_x + m.size_x + 1, m.dst_z + m.size_z + 1); + push_dirty_rect(m.src_x - 2, m.src_z - 2, m.src_x + m.size_x + 1, m.src_z + m.size_z + 1); + active_morphs_.push_back(std::move(rev)); + } +} + +void RaceSim::update_triggers(CraftState& c) { + if (c.recovery_state != RecoveryState::Active) return; + for (size_t i = 0; i < level_.triggers.size(); ++i) { + const auto& tr = level_.triggers[i]; + if (tr.subtype != 0) continue; // Craft proximity trigger only + auto& ts = trigger_states_[i]; + if (!ts.armed || ts.fired) continue; + + float min_x = tr.tile_x() - 0.5f; + float max_x = tr.tile_x() + static_cast(tr.aux1) + 1.5f; + float min_z = tr.tile_z() - 0.5f; + float max_z = tr.tile_z() + static_cast(tr.aux2) + 1.5f; + if (c.curr_pose.pos.x >= min_x && c.curr_pose.pos.x <= max_x && + c.curr_pose.pos.z >= min_z && c.curr_pose.pos.z <= max_z) { + ts.fired = true; + ts.armed = false; + int actor_p = c.is_human ? c.player_index : -1; + if (tr.value > 0) { + ts.pending = true; + ts.timer = static_cast(tr.value) / 25.0f; + ts.pending_player = actor_p; + } else { + activate_group(tr.target_group, actor_p, c.is_human, false); + } } } } @@ -538,12 +961,32 @@ CraftPose RaceSim::interpolated_clone_pose(float alpha) const { } bool RaceSim::consume_terrain_dirty(int& x0, int& z0, int& x1, int& z1) { - if (!terrain_dirty_) return false; - x0 = dirty_x0_; - z0 = dirty_z0_; - x1 = dirty_x1_; - z1 = dirty_z1_; - terrain_dirty_ = false; + if (dirty_rects_.empty()) return false; + x0 = dirty_rects_[0].x0; + z0 = dirty_rects_[0].z0; + x1 = dirty_rects_[0].x1; + z1 = dirty_rects_[0].z1; + for (size_t i = 1; i < dirty_rects_.size(); ++i) { + x0 = std::min(x0, dirty_rects_[i].x0); + z0 = std::min(z0, dirty_rects_[i].z0); + x1 = std::max(x1, dirty_rects_[i].x1); + z1 = std::max(z1, dirty_rects_[i].z1); + } + dirty_rects_.clear(); + columns_dirty_ = false; + return true; +} + +bool RaceSim::consume_world_dirty(std::vector& out_terrain_rects, bool& out_columns_dirty) { + if (dirty_rects_.empty() && !columns_dirty_) { + out_terrain_rects.clear(); + out_columns_dirty = false; + return false; + } + out_terrain_rects = std::move(dirty_rects_); + dirty_rects_.clear(); + out_columns_dirty = columns_dirty_; + columns_dirty_ = false; return true; } diff --git a/src/sim/race_sim.hpp b/src/sim/race_sim.hpp index 1aa3f5a..3eb3066 100644 --- a/src/sim/race_sim.hpp +++ b/src/sim/race_sim.hpp @@ -63,7 +63,9 @@ struct Projectile { struct PickupItem { uint16_t entity_id = 0; uint8_t subtype = 0; + uint16_t group = 1; Vec3 pos{0, 0, 0}; + bool group_unlocked = true; bool active = true; float respawn_timer = 0.0f; }; @@ -96,6 +98,17 @@ struct CloneRecording { std::vector frames; // 60 Hz sampled poses }; +struct DirtyRect { + int x0 = 0, z0 = 0, x1 = 0, z1 = 0; +}; + +struct ProjectileTrace { + Vec3 p0{0, 0, 0}; + Vec3 p1{0, 0, 0}; + int owner_player = -1; + bool is_missile = false; +}; + struct RaceSetup { GameMode mode = GameMode::SingleRace; int track_number = 1; // 1..6 @@ -144,14 +157,38 @@ public: CraftPose interpolated_clone_pose(float alpha) const; const CloneRecording& current_recording() const { return recording_; } - // Checks if morph triggers fired and changed terrain heights + // Checks if morph triggers fired and changed terrain heights or columns bool consume_terrain_dirty(int& x0, int& z0, int& x1, int& z1); + bool consume_world_dirty(std::vector& out_terrain_rects, bool& out_columns_dirty); private: + struct TriggerRuntime { + bool armed = false; + bool fired = false; + bool pending = false; + float timer = 0.0f; + int hit_accum = 0; + int pending_player = -1; + }; + + struct ActiveMorph { + size_t morph_idx = 0; + float elapsed = 0.0f; + float duration = 0.50f; + std::vector orig_dst; + std::vector orig_src; + }; + void compute_start_grid(int count, std::vector& out_poses) const; void update_checkpoints_and_laps(CraftState& c); void update_recharge_pads(CraftState& c, float dt); void update_triggers(CraftState& c); + void update_timed_triggers(float dt); + void update_rocket_triggers(const std::vector& traces); + void update_active_morphs(float dt); + void apply_morph_fraction(const ActiveMorph& am, float s); + void push_dirty_rect(int x0, int z0, int x1, int z1); + void activate_group(uint16_t group, int actor_player, bool allow_banner, bool fast_morph = false); void update_standings(); void update_particles(float dt); @@ -171,9 +208,13 @@ private: int winner_id_ = -1; // Trigger/morph tracking - std::vector trigger_fired_; - bool terrain_dirty_ = false; - int dirty_x0_ = 0, dirty_z0_ = 0, dirty_x1_ = 0, dirty_z1_ = 0; + std::vector trigger_states_; + std::vector morph_is_repeating_; + std::vector morph_fired_once_; + std::vector active_morphs_; + std::vector dirty_rects_; + bool columns_dirty_ = false; + float morph_banner_cd_ = 0.0f; // Clone race recording & playback CloneRecording recording_; diff --git a/tests/test_gameplay.cpp b/tests/test_gameplay.cpp index 488a45a..913b8b5 100644 --- a/tests/test_gameplay.cpp +++ b/tests/test_gameplay.cpp @@ -198,5 +198,289 @@ int main() { } std::cout << "[PASS] Gameplay rule toggles (fuel, weapons, damage, pickups, morphing) validated\n"; + // 6. Authentic Sector Morphing across all 6 levels (Timed, Craft, Rocket triggers + columns/lights/pickups) + { + const size_t expected_morph_counts[6] = {1, 2, 5, 2, 7, 6}; + for (int l = 1; l <= 6; ++l) { + assets::LevelData lvl; + CHECK(lvl.load(vfs, l)); + CHECK(lvl.morphs.size() == expected_morph_counts[l - 1]); + } + + // 6a. Level 1: Timed morph id=66 lowers (17,32) 1504 -> 1184, then oscillates back to 1504 + { + sim::RaceSetup ms; + ms.track_number = 1; + ms.num_ai = 0; + sim::RaceSim msim; + CHECK(msim.start_race(lvl1, ms)); + CHECK(msim.level().cell(17, 32).height == 1504); + for (int t = 0; t < 200; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + for (int t = 0; t < 290; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + CHECK(msim.level().cell(17, 32).height == 1184); + for (int t = 0; t < 290; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + CHECK(msim.level().cell(17, 32).height == 1504); + } + + // 6b. Level 2: Craft trigger id=80 lowers & re-textures (109,20); Craft trigger id=198 unlocks grp=3 pickups + { + assets::LevelData lvl2; + CHECK(lvl2.load(vfs, 2)); + sim::RaceSetup ms; + ms.track_number = 2; + ms.num_ai = 0; + sim::RaceSim msim; + CHECK(msim.start_race(lvl2, ms)); + CHECK(msim.level().cell(109, 20).height == 2480); + CHECK(msim.level().cell(109, 20).tex_or_col == 72); + bool has_locked_g3 = false; + for (const auto& pk : msim.pickups()) { + if (pk.group == 3) { + has_locked_g3 = true; + CHECK(!pk.group_unlocked); + } + } + CHECK(has_locked_g3); + + for (int t = 0; t < 200; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + msim.crafts_mut()[0].curr_pose.pos = Vec3(110.5f, msim.level().sample_height_world(110.5f, 19.5f) + 0.30f, 19.5f); + msim.crafts_mut()[0].prev_pose = msim.crafts_mut()[0].curr_pose; + for (int t = 0; t < 60; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + CHECK(msim.level().cell(109, 20).height == 1840); + CHECK(msim.level().cell(109, 20).tex_or_col == 63); + + msim.crafts_mut()[0].curr_pose.pos = Vec3(127.5f, msim.level().sample_height_world(127.5f, 21.5f) + 0.30f, 21.5f); + msim.crafts_mut()[0].prev_pose = msim.crafts_mut()[0].curr_pose; + for (int t = 0; t < 10; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + for (const auto& pk : msim.pickups()) { + if (pk.group == 3) CHECK(pk.group_unlocked && pk.active); + } + } + + // 6c. Level 3: Rocket trigger id=176 removes solid columns at (67,63) and marks columns_dirty + { + assets::LevelData lvl3; + CHECK(lvl3.load(vfs, 3)); + sim::RaceSetup ms; + ms.track_number = 3; + ms.num_ai = 0; + sim::RaceSim msim; + CHECK(msim.start_race(lvl3, ms)); + CHECK(msim.level().cell(67, 63).tex_or_col == -87); + float col_y = msim.level().sample_height_world(67.5f, 63.5f) + 0.30f; + CHECK(msim.level().is_cell_solid_at_point(67, 63, 67.5f, 63.5f, col_y)); + for (int t = 0; t < 200; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + + auto& p1 = msim.crafts_mut()[0]; + p1.curr_pose.pos = Vec3(67.5f, msim.level().sample_height_world(67.5f, 67.5f) + 0.30f, 67.5f); + p1.curr_pose.yaw = kPi; + p1.prev_pose = p1.curr_pose; + p1.vel = Vec3(0, 0, 0); + bool saw_cols_dirty = false; + std::vector rects; + for (int t = 0; t < 40; ++t) { + CraftInput fire; + fire.fire_missile = (t == 0); + msim.step(fire, CraftInput{}, dt); + bool cdirty = false; + if (msim.consume_world_dirty(rects, cdirty) && cdirty) saw_cols_dirty = true; + } + CHECK(saw_cols_dirty); + CHECK(msim.level().cell(67, 63).tex_or_col >= 0); + CHECK(!msim.level().is_cell_solid_at_point(67, 63, 67.5f, 63.5f, col_y)); + } + + // 6d. Level 4: Rocket id=153 collapses reactor (68,75) & unlocks grp=10 pickups; Craft id=256 + Rocket id=40 open bunker ONCE + { + assets::LevelData lvl4; + CHECK(lvl4.load(vfs, 4)); + sim::RaceSetup ms; + ms.track_number = 4; + ms.num_ai = 0; + sim::RaceSim msim; + CHECK(msim.start_race(lvl4, ms)); + CHECK(msim.level().cell(68, 75).height == 3360); + CHECK(msim.level().cell(43, 114).tex_or_col == -172); + for (int t = 0; t < 200; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + + auto& p1 = msim.crafts_mut()[0]; + p1.curr_pose.pos = Vec3(69.5f, msim.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); + msim.step(fire, CraftInput{}, dt); + } + CHECK(msim.level().cell(68, 75).height == 2048); + bool g10_unlocked = false; + for (const auto& pk : msim.pickups()) { + if (pk.group == 10) { + g10_unlocked = true; + CHECK(pk.group_unlocked); + float expected_y = msim.level().sample_height_world(pk.pos.x, pk.pos.z) + 0.45f; + CHECK(std::abs(pk.pos.y - expected_y) < 0.05f); + } + } + CHECK(g10_unlocked); + + p1.curr_pose.pos = Vec3(44.5f, msim.level().sample_height_world(44.5f, 49.5f) + 0.30f, 49.5f); + p1.prev_pose = p1.curr_pose; + for (int t = 0; t < 40; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + CHECK(msim.level().cell(43, 114).tex_or_col == -171); + + p1.curr_pose.pos = Vec3(44.5f, msim.level().sample_height_world(44.5f, 117.0f) + 0.30f, 117.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 < 40; ++t) { + CraftInput fire; + fire.fire_missile = (t == 0); + msim.step(fire, CraftInput{}, dt); + } + CHECK(msim.level().cell(43, 114).tex_or_col == -171); + } + + // 6e. Level 5: Rocket id=286 blasts pillar (70,81); Craft id=275 starts blinking tunnel lights at (10,25) + { + assets::LevelData lvl5; + CHECK(lvl5.load(vfs, 5)); + sim::RaceSetup ms; + ms.track_number = 5; + ms.num_ai = 0; + sim::RaceSim msim; + CHECK(msim.start_race(lvl5, ms)); + CHECK(msim.level().cell(70, 81).tex_or_col == -53); + CHECK(msim.level().cell(10, 25).illumination == 12800); + for (int t = 0; t < 200; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + + auto& p1 = msim.crafts_mut()[0]; + p1.curr_pose.pos = Vec3(70.5f, msim.level().sample_height_world(70.5f, 84.0f) + 0.30f, 84.0f); + p1.curr_pose.yaw = kPi; + p1.prev_pose = p1.curr_pose; + p1.vel = Vec3(0, 0, 0); + bool saw_cols_dirty = false; + std::vector rects; + for (int t = 0; t < 40; ++t) { + CraftInput fire; + fire.fire_missile = (t == 0); + msim.step(fire, CraftInput{}, dt); + bool cdirty = false; + if (msim.consume_world_dirty(rects, cdirty) && cdirty) saw_cols_dirty = true; + } + CHECK(saw_cols_dirty); + CHECK(msim.level().cell(70, 81).tex_or_col == 117); + + p1.curr_pose.pos = Vec3(20.5f, msim.level().sample_height_world(20.5f, 77.5f) + 0.30f, 77.5f); + p1.prev_pose = p1.curr_pose; + bool saw_dim = false; + for (int t = 0; t < 60; ++t) { + msim.step(CraftInput{}, CraftInput{}, dt); + if (msim.level().cell(10, 25).illumination == 8192) saw_dim = true; + } + CHECK(saw_dim); + bool saw_bright = false; + for (int t = 0; t < 60; ++t) { + msim.step(CraftInput{}, CraftInput{}, dt); + if (msim.level().cell(10, 25).illumination == 12800) saw_bright = true; + } + CHECK(saw_bright); + } + + // 6f. Level 6: Timed id=3 oscillates ramp (10,75); Craft id=148 collapses (48,52); Rocket id=184 blasts (33,16) + { + assets::LevelData lvl6; + CHECK(lvl6.load(vfs, 6)); + sim::RaceSetup ms; + ms.track_number = 6; + ms.num_ai = 0; + sim::RaceSim msim; + CHECK(msim.start_race(lvl6, ms)); + CHECK(msim.level().cell(10, 75).height == 2368); + CHECK(msim.level().cell(48, 52).tex_or_col == -480); + CHECK(msim.level().cell(33, 16).tex_or_col == -112); + + for (int t = 0; t < 200; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + for (int t = 0; t < 200; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + CHECK(msim.level().cell(10, 75).height == 2048); + for (int t = 0; t < 180; ++t) msim.step(CraftInput{}, CraftInput{}, dt); + CHECK(msim.level().cell(10, 75).height == 2368); + + auto& p1 = msim.crafts_mut()[0]; + p1.curr_pose.pos = Vec3(70.5f, msim.level().sample_height_world(70.5f, 51.5f) + 0.30f, 51.5f); + p1.prev_pose = p1.curr_pose; + p1.vel = Vec3(0, 0, 0); + bool saw_cols_dirty = false; + std::vector rects; + for (int t = 0; t < 40; ++t) { + msim.step(CraftInput{}, CraftInput{}, dt); + bool cdirty = false; + if (msim.consume_world_dirty(rects, cdirty) && cdirty) saw_cols_dirty = true; + } + CHECK(saw_cols_dirty); + CHECK(msim.level().cell(48, 52).tex_or_col >= 0); + + p1.curr_pose.pos = Vec3(33.5f, msim.level().sample_height_world(33.5f, 19.5f) + 0.30f, 19.5f); + 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 < 40; ++t) { + CraftInput fire; + fire.fire_missile = (t == 0); + msim.step(fire, CraftInput{}, dt); + } + CHECK(msim.level().cell(33, 16).tex_or_col >= 0); + } + + // 6g. track_morphing_enabled = false disables Timed (L1), Craft (L2), and Rocket (L3) morphs + { + sim::RaceSetup off1; + off1.track_number = 1; + off1.num_ai = 0; + off1.track_morphing_enabled = false; + sim::RaceSim s1; + CHECK(s1.start_race(lvl1, off1)); + for (int t = 0; t < 500; ++t) s1.step(CraftInput{}, CraftInput{}, dt); + CHECK(s1.level().cell(17, 32).height == 1504); + + assets::LevelData lvl2; + CHECK(lvl2.load(vfs, 2)); + sim::RaceSetup off2; + off2.track_number = 2; + off2.num_ai = 0; + off2.track_morphing_enabled = false; + sim::RaceSim s2; + CHECK(s2.start_race(lvl2, off2)); + for (int t = 0; t < 200; ++t) s2.step(CraftInput{}, CraftInput{}, dt); + s2.crafts_mut()[0].curr_pose.pos = Vec3(110.5f, s2.level().sample_height_world(110.5f, 19.5f) + 0.30f, 19.5f); + s2.crafts_mut()[0].prev_pose = s2.crafts_mut()[0].curr_pose; + for (int t = 0; t < 60; ++t) s2.step(CraftInput{}, CraftInput{}, dt); + CHECK(s2.level().cell(109, 20).height == 2480); + + assets::LevelData lvl3; + CHECK(lvl3.load(vfs, 3)); + sim::RaceSetup off3; + off3.track_number = 3; + off3.num_ai = 0; + off3.track_morphing_enabled = false; + sim::RaceSim s3; + CHECK(s3.start_race(lvl3, off3)); + for (int t = 0; t < 200; ++t) s3.step(CraftInput{}, CraftInput{}, dt); + s3.crafts_mut()[0].curr_pose.pos = Vec3(67.5f, s3.level().sample_height_world(67.5f, 67.5f) + 0.30f, 67.5f); + s3.crafts_mut()[0].curr_pose.yaw = kPi; + s3.crafts_mut()[0].prev_pose = s3.crafts_mut()[0].curr_pose; + for (int t = 0; t < 40; ++t) { + CraftInput fire; + fire.fire_missile = (t == 0); + s3.step(fire, CraftInput{}, dt); + } + CHECK(s3.level().cell(67, 63).tex_or_col == -87); + } + } + std::cout << "[PASS] Authentic sector morphing, Timed/Craft/Rocket triggers & groups verified across all 6 tracks\n"; + return 0; } diff --git a/tests/test_physics.cpp b/tests/test_physics.cpp index 5fd9af5..3f46289 100644 --- a/tests/test_physics.cpp +++ b/tests/test_physics.cpp @@ -273,5 +273,166 @@ int main() { CHECK(p1.finished); } std::cout << "[PASS] 1-lap autopilot completion verified across all 6 tracks\n"; + + // 8. 8-direction full-boost ramming stress test across all 6 tracks (0 column/cliff traps) + { + float dt = static_cast(FixedStepClock::kFixedDt); + for (int l = 1; l <= 6; ++l) { + assets::LevelData lvl; + CHECK(lvl.load(vfs, l)); + for (const auto& wp : lvl.main_waypoints) { + for (int di = 0; di < 8; ++di) { + float ang = static_cast(di) * (kPi * 0.25f); + sim::CraftState c; + c.id = 0; + c.vehicle_idx = 0; + c.curr_pose.pos = Vec3(wp.x, lvl.sample_height_world(wp.x, wp.z) + 0.30f, wp.z); + c.curr_pose.yaw = ang; + c.prev_pose = c.curr_pose; + c.vel = Vec3(std::sin(ang) * 22.0f, 0.0f, std::cos(ang) * 22.0f); + c.fuel = c.max_fuel = 100.0f; + c.shield = c.max_shield = 100.0f; + + CraftInput in; + in.throttle = 1.0f; + in.boost = true; + for (int t = 0; t < 150; ++t) { + sim::step_craft_physics(c, in, lvl, dt, false); + const Vec3& p = c.curr_pose.pos; + int ix = static_cast(std::floor(p.x)); + int iz = static_cast(std::floor(p.z)); + CHECK(ix >= 0 && ix < assets::kMapWidth && iz >= 0 && iz < assets::kMapHeight); + const auto& me = lvl.cell(ix, iz); + if (me.tex_or_col < 0 && -me.tex_or_col > 0 && -me.tex_or_col < assets::kMaxColumns) { + const auto& col = lvl.columns[-me.tex_or_col]; + float gy = lvl.sample_height_world(p.x, p.z); + for (int s = 0; s < 8; ++s) { + if ((col.shape_mask & (1u << s)) == 0) continue; + float sb = gy + static_cast(s); + float st = sb + 1.0f; + CHECK(!(sb <= p.y + 0.35f && st >= p.y - 0.02f)); + } + } + if (!c.airborne && p.y <= lvl.sample_height_world(p.x, p.z) + 0.55f) { + Vec3 sn = lvl.sample_normal_world(p.x, p.z); + CHECK(sn.y >= 0.30f); + } + } + } + } + } + } + std::cout << "[PASS] 8-direction boost ramming stress test: 0 texture/cliff traps across all 6 tracks\n"; + + // 9. Targeted containment, bridge deck, cliff, and escape edge cases + { + float dt = static_cast(FixedStepClock::kFixedDt); + + // 9a. Sloped column containment on Level 5 (56, 16) (M1, dh=2.50) + assets::LevelData lvl5; + CHECK(lvl5.load(vfs, 5)); + { + sim::CraftState c; + c.id = 0; + c.vehicle_idx = 0; + c.curr_pose.pos = Vec3(56.5f, lvl5.sample_height_world(56.5f, 17.6f) + 0.28f, 17.6f); + c.curr_pose.yaw = kPi; + c.prev_pose = c.curr_pose; + c.vel = Vec3(0.0f, 0.0f, -14.0f); + c.fuel = c.max_fuel = 100.0f; + CraftInput in; + in.throttle = 1.0f; + for (int t = 0; t < 30; ++t) { + sim::step_craft_physics(c, in, lvl5, dt, false); + int ix = static_cast(std::floor(c.curr_pose.pos.x)); + int iz = static_cast(std::floor(c.curr_pose.pos.z)); + CHECK(!(ix == 56 && iz == 16 && + lvl5.is_cell_solid_at_point(ix, iz, c.curr_pose.pos.x, c.curr_pose.pos.z, + c.curr_pose.pos.y))); + } + } + + // 9b. Overpass bridge deck support on Level 2 (M2), Level 3 (M6), and Level 6 (M2) + struct BridgeCase { int lvl_num; float wx; float wz; float top_offset; }; + const BridgeCase bcases[3] = { + {2, 77.5f, 6.5f, 2.0f}, + {3, 109.5f, 90.5f, 3.0f}, + {6, 90.5f, 32.5f, 2.0f}, + }; + for (const auto& bc : bcases) { + assets::LevelData blvl; + CHECK(blvl.load(vfs, bc.lvl_num)); + float gy = blvl.sample_height_world(bc.wx, bc.wz); + float deck_y = gy + bc.top_offset; + float floor_y = 0.0f, ceil_y = 1e6f; + blvl.sample_surface_and_ceiling(bc.wx, bc.wz, deck_y + 0.28f, floor_y, ceil_y); + CHECK(floor_y >= deck_y - 0.02f); + + sim::CraftState c; + c.id = 0; + c.vehicle_idx = 0; + c.curr_pose.pos = Vec3(bc.wx, deck_y + 0.28f, bc.wz); + c.prev_pose = c.curr_pose; + c.fuel = c.max_fuel = 100.0f; + CraftInput neutral{}; + for (int t = 0; t < 15; ++t) { + sim::step_craft_physics(c, neutral, blvl, dt, false); + } + CHECK(c.curr_pose.pos.y >= deck_y + 0.14f); + } + + // 9c. Sheer terrain cliff blocking (Level 1 z=58..59) vs downhill drop clearance (Level 5 (22,99)) + { + sim::CraftState cliff_car; + cliff_car.id = 0; + cliff_car.vehicle_idx = 0; + cliff_car.curr_pose.pos = Vec3(28.5f, lvl1.sample_height_world(28.5f, 57.5f) + 0.28f, 57.5f); + cliff_car.curr_pose.yaw = 0.0f; + cliff_car.prev_pose = cliff_car.curr_pose; + cliff_car.vel = Vec3(0.0f, 0.0f, 14.0f); + cliff_car.fuel = cliff_car.max_fuel = 100.0f; + CraftInput in; + in.throttle = 1.0f; + for (int t = 0; t < 40; ++t) { + sim::step_craft_physics(cliff_car, in, lvl1, dt, false); + } + CHECK(cliff_car.curr_pose.pos.z < 58.65f); + CHECK(cliff_car.curr_pose.pos.y < 1.0f); + + sim::CraftState drop_car; + drop_car.id = 0; + drop_car.vehicle_idx = 0; + drop_car.curr_pose.pos = Vec3(22.5f, lvl5.sample_height_world(22.5f, 101.0f) + 0.28f, 101.0f); + drop_car.curr_pose.yaw = kPi; + drop_car.prev_pose = drop_car.curr_pose; + drop_car.vel = Vec3(0.0f, 0.0f, -14.0f); + drop_car.fuel = drop_car.max_fuel = 100.0f; + for (int t = 0; t < 40; ++t) { + sim::step_craft_physics(drop_car, in, lvl5, dt, false); + } + CHECK(drop_car.curr_pose.pos.z < 97.5f); + } + + // 9d. Guaranteed escape from inside a solid 3x3 building block + for (int z = 30; z <= 32; ++z) { + for (int x = 30; x <= 32; ++x) { + syn.cell_mut(x, z).tex_or_col = -1; + } + } + sim::CraftState trapped; + trapped.id = 0; + trapped.vehicle_idx = 0; + trapped.curr_pose.pos = Vec3(31.5f, 0.28f, 31.5f); + trapped.prev_pose = trapped.curr_pose; + float imp = 0.0f; + bool scr = false; + CHECK(sim::resolve_world_collisions(trapped, syn, imp, scr)); + CHECK(std::isfinite(trapped.curr_pose.pos.x) && std::isfinite(trapped.curr_pose.pos.z)); + CHECK(!syn.is_cell_solid_at_point(static_cast(std::floor(trapped.curr_pose.pos.x)), + static_cast(std::floor(trapped.curr_pose.pos.z)), + trapped.curr_pose.pos.x, trapped.curr_pose.pos.z, + trapped.curr_pose.pos.y)); + } + std::cout << "[PASS] Sloped column, overpass deck, cliff wall, downhill drop & escape verified\n"; return 0; } diff --git a/tests/test_render_geom.cpp b/tests/test_render_geom.cpp index ac9fddb..453f2c6 100644 --- a/tests/test_render_geom.cpp +++ b/tests/test_render_geom.cpp @@ -82,6 +82,42 @@ int main() { // Verify partial morph region update stays valid render::update_terrain_region(lvl, 10, 80, 15, 85, geo.terrain_verts); + if (l == 6) { + // Remove 39 solid columns at (44..57, 50..55) using morph id=140 source cells + assets::LevelData morphed = lvl; + for (int oz = 0; oz <= 5; ++oz) { + for (int ox = 0; ox <= 13; ++ox) { + morphed.cell_mut(44 + ox, 50 + oz) = lvl.cell(31 + ox, 41 + oz); + } + } + std::vector mverts; + std::vector mindices; + render::rebuild_column_geometry(morphed, mverts, mindices); + CHECK(mindices.size() < geo.column_indices.size()); + CHECK(!mverts.empty() && mindices.size() % 6 == 0); + for (const auto& v : mverts) { + CHECK(std::isfinite(v.pos.x) && std::isfinite(v.pos.y) && std::isfinite(v.pos.z)); + CHECK(v.tile >= 0.0f && v.tile < 256.0f); + CHECK(v.u >= -1e-4f && v.u <= 1.0001f); + CHECK(v.v >= -1e-4f && v.v <= 1.0001f); + } + CHECK(check_winding(mverts, mindices, false)); + + // Also verify bidirectional swap relocates column vertices cleanly with valid winding + assets::LevelData swapped = lvl; + for (int oz = 0; oz <= 5; ++oz) { + for (int ox = 0; ox <= 13; ++ox) { + auto dc = lvl.cell(44 + ox, 50 + oz); + auto sc = lvl.cell(31 + ox, 41 + oz); + swapped.cell_mut(44 + ox, 50 + oz) = sc; + swapped.cell_mut(31 + ox, 41 + oz) = dc; + } + } + render::rebuild_column_geometry(swapped, mverts, mindices); + CHECK(!mverts.empty() && mindices.size() == geo.column_indices.size()); + CHECK(check_winding(mverts, mindices, false)); + } + std::cout << " Level " << l << " geo: terrain_tris=" << geo.terrain_indices.size() / 3 << " column_tris=" << geo.column_indices.size() / 3 << "\n"; }