Fix geometry collision traps and implement authentic sector morphing

This commit is contained in:
Luxferre committed 2026-12-01 08:05:04 +02:00
1 parent cd63921ea4
commit b9353f552c
16 files changed
+1470 -131

No files matched your search

+110 -18
View File
@@ -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<int>(e.pos_x_fp8 >> 8);
int dst_z = static_cast<int>(e.pos_z_fp8 >> 8);
int src_x = static_cast<int>(src.pos_x_fp8 >> 8);
int src_z = static_cast<int>(src.pos_z_fp8 >> 8);
if (dst_x < kMapWidth && src_x < kMapWidth &&
dst_z < kMapHeight && src_z < kMapHeight) {
int sx = std::min<int>(e.aux1, src.aux1);
int sz = std::min<int>(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<float>(kMapWidth - 1));
float cz = std::clamp(wz, 0.0f, static_cast<float>(kMapHeight - 1));
int x0 = static_cast<int>(cx);
int z0 = static_cast<int>(cz);
int x1 = std::min(x0 + 1, kMapWidth - 1);
int z1 = std::min(z0 + 1, kMapHeight - 1);
int x0 = std::min(static_cast<int>(cx), kMapWidth - 2);
int z0 = std::min(static_cast<int>(cz), kMapHeight - 2);
int x1 = x0 + 1;
int z1 = z0 + 1;
float fx = cx - static_cast<float>(x0);
float fz = cz - static_cast<float>(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<float>(kMapWidth - 1));
float cz = std::clamp(wz, 0.0f, static_cast<float>(kMapHeight - 1));
int x0 = std::min(static_cast<int>(cx), kMapWidth - 2);
int z0 = std::min(static_cast<int>(cz), kMapHeight - 2);
int x1 = x0 + 1;
int z1 = z0 + 1;
float fx = cx - static_cast<float>(x0);
float fz = cz - static_cast<float>(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<float>(x) + 0.5f, static_cast<float>(z) + 0.5f);
if (col.shape_mask == 0) {
return false;
}
float cx = std::clamp(qx, static_cast<float>(x), static_cast<float>(x + 1));
float cz = std::clamp(qz, static_cast<float>(z), static_cast<float>(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<float>(x) + 0.5f,
static_cast<float>(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<float>(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<float>(s) + 1.0f;
float slot_bot = ground_y_bot + static_cast<float>(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);
}
}
+23
View File
@@ -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<LevelEntity> pickups; // type 5
std::vector<LevelEntity> props; // type 3
std::vector<LevelEntity> triggers; // type 8
std::vector<MorphDefinition> morphs; // type 2 subtype 16/23 paired with subtype 9/7 source
std::vector<EffectEntity> effects; // type 2 subtype 1/2/3/5/8
std::vector<LevelEntity> 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;
};
+38 -3
View File
@@ -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<sim::DirtyRect> 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<float>(k) / static_cast<float>(kCamSteps);
Vec3 p = lerp(anchor, cams[vp].eye, t);
int ix = static_cast<int>(std::floor(p.x));
int iz = static_cast<int>(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_);
+17 -8
View File
@@ -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<WorldVertex>& column_verts,
std::vector<uint32_t>& 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,
+5
View File
@@ -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<WorldVertex>& 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<WorldVertex>& column_verts,
std::vector<uint32_t>& 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);
+25 -1
View File
@@ -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<GLsizeiptr>(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<GLsizeiptr>(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<GLsizei>(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;
+1
View File
@@ -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/<key>.DAT (cached by key)
bool ensure_model(const Vfs& vfs, const std::string& key, const assets::Palette& pal);
+14 -9
View File
@@ -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<int>(std::floor(wx));
int iz = static_cast<int>(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<float>((ai.id % 5) - 2)) * 0.55f;
Vec2 shifted = tgt_pos + right * lane;
if (!level.is_cell_solid_at_y(static_cast<int>(std::floor(shifted.x)),
static_cast<int>(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<int>(std::floor(p.x)),
static_cast<int>(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<int>(std::floor(p.x)),
static_cast<int>(std::floor(p.y)),
ai.curr_pose.pos.y)) {
is_ai_blocked(p.x, p.y)) {
clear = false;
break;
}
+17 -3
View File
@@ -130,7 +130,8 @@ void step_combat_and_world(
std::vector<SimEvent>& events,
bool& out_race_over,
int& out_winner_id,
float dt) {
float dt,
std::vector<ProjectileTrace>* 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<int>(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<int>(std::floor(pr.pos.x));
int iz = static_cast<int>(std::floor(pr.pos.z));
if (pr.pos.y <= gy ||
level.is_cell_solid_at_y(static_cast<int>(std::floor(pr.pos.x)),
static_cast<int>(std::floor(pr.pos.z)), pr.pos.y)) {
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;
+2 -1
View File
@@ -25,7 +25,8 @@ void step_combat_and_world(
std::vector<SimEvent>& events,
bool& out_race_over,
int& out_winner_id,
float dt);
float dt,
std::vector<ProjectileTrace>* out_projectile_traces = nullptr);
// Computes visual Recovery Helicopter (RECOV0-0) positions for any craft under repair
std::vector<RecoveryVisual> get_recovery_visuals(const std::vector<CraftState>& crafts);
+217 -52
View File
@@ -1,6 +1,7 @@
#include "sim/physics.hpp"
#include <algorithm>
#include <array>
#include <cmath>
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<int>(std::floor(craft.curr_pose.pos.x));
int cz = static_cast<int>(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<float>(nx);
float max_x = min_x + 1.0f;
float min_z = static_cast<float>(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<float>(c.ax) + 0.5f,
static_cast<float>(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<float>(c.ax) + 0.5f,
static_cast<float>(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<int>(std::floor(craft.curr_pose.pos.x));
int cz = static_cast<int>(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<float>(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<float>(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<bool, kSpan * kSpan> vis{};
std::array<int, kSpan * kSpan> qx{};
std::array<int, kSpan * kSpan> 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<float>(ux) + 0.5f;
float tz = static_cast<float>(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<float>(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<int>(std::floor(craft.curr_pose.pos.x));
int cell_z = static_cast<int>(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;
}
}
}
+475 -32
View File
@@ -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<float>(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<CraftInput> 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<ProjectileTrace> 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<int>(tr.tile_x());
int cz = static_cast<int>(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<uint16_t>(std::min<int>(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<size_t>(oz) * static_cast<size_t>(w) + static_cast<size_t>(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<uint16_t>(
std::lround(lerp(static_cast<float>(da.height), static_cast<float>(sb.height), s)));
dc.illumination = static_cast<uint16_t>(
std::lround(lerp(static_cast<float>(da.illumination), static_cast<float>(sb.illumination), s)));
sc.height = static_cast<uint16_t>(
std::lround(lerp(static_cast<float>(sb.height), static_cast<float>(da.height), s)));
sc.illumination = static_cast<uint16_t>(
std::lround(lerp(static_cast<float>(sb.illumination), static_cast<float>(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<float>(k) / 10.0f);
float spd = 1.6f + static_cast<float>((k * 37) % 10) * 0.28f;
Vec3 v(std::cos(ang) * spd, 1.4f + static_cast<float>(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<float>(k) / static_cast<float>(count));
Vec3 v(std::cos(ang) * 0.5f, 2.2f + static_cast<float>(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<float>(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<float>(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<ptrdiff_t>(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<size_t>(w) * static_cast<size_t>(h));
am.orig_src.resize(static_cast<size_t>(w) * static_cast<size_t>(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<size_t>(oz) * static_cast<size_t>(w) + static_cast<size_t>(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<float>(m.dst_x) + static_cast<float>(m.size_x + 1) * 0.5f;
float cz = static_cast<float>(m.dst_z) + static_cast<float>(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<ProjectileTrace>& 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<float>(tr.aux1) + 2.0f;
float z0 = tr.tile_z() - 1.0f;
float z1 = tr.tile_z() + static_cast<float>(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<float>(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<DirtyRect> touched;
std::vector<size_t> 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<ptrdiff_t>(ai));
} else {
++ai;
}
}
auto touches_any = [&](float wx, float wz, float pad) -> bool {
for (const auto& r : touched) {
if (wx >= static_cast<float>(r.x0) - pad && wx <= static_cast<float>(r.x1 + 1) + pad &&
wz >= static_cast<float>(r.z0) - pad && wz <= static_cast<float>(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<size_t>(w) * static_cast<size_t>(h));
rev.orig_src.resize(static_cast<size_t>(w) * static_cast<size_t>(h));
for (int oz = 0; oz < h; ++oz) {
for (int ox = 0; ox < w; ++ox) {
size_t idx = static_cast<size_t>(oz) * static_cast<size_t>(w) + static_cast<size_t>(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<float>(tr.aux1) + 1.5f;
float min_z = tr.tile_z() - 0.5f;
float max_z = tr.tile_z() + static_cast<float>(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<float>(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<DirtyRect>& 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;
}
+45 -4
View File
@@ -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<CraftPose> 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<DirtyRect>& 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<assets::MapEntry> orig_dst;
std::vector<assets::MapEntry> orig_src;
};
void compute_start_grid(int count, std::vector<CraftPose>& 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<ProjectileTrace>& 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<bool> trigger_fired_;
bool terrain_dirty_ = false;
int dirty_x0_ = 0, dirty_z0_ = 0, dirty_x1_ = 0, dirty_z1_ = 0;
std::vector<TriggerRuntime> trigger_states_;
std::vector<bool> morph_is_repeating_;
std::vector<bool> morph_fired_once_;
std::vector<ActiveMorph> active_morphs_;
std::vector<DirtyRect> dirty_rects_;
bool columns_dirty_ = false;
float morph_banner_cd_ = 0.0f;
// Clone race recording & playback
CloneRecording recording_;