diff --git a/src/sim.c b/src/sim.c index 3bebb15f..d68c622f 100644 --- a/src/sim.c +++ b/src/sim.c @@ -561,26 +561,18 @@ struct v2 sim_pos_from_world_tile_index(struct v2i32 world_tile_index) f32 tile_size = 1.f / SIM_TILES_PER_UNIT_SQRT; res.x = (f32)world_tile_index.x * tile_size; res.y = (f32)world_tile_index.y * tile_size; - if (world_tile_index.x < 0) { - res.x += tile_size; - } - if (world_tile_index.y < 0) { - res.y += tile_size; - } return res; } struct v2i32 sim_local_tile_index_from_world_tile_index(struct v2i32 world_tile_index) { - struct v2i32 res = ZI; - res.x = world_tile_index.x % SIM_TILES_PER_CHUNK_SQRT; - res.y = world_tile_index.y % SIM_TILES_PER_CHUNK_SQRT; - if (world_tile_index.x < 0) { - res.x = SIM_TILES_PER_CHUNK_SQRT + res.x - 1; - } - if (world_tile_index.y < 0) { - res.y = SIM_TILES_PER_CHUNK_SQRT + res.y - 1; - } + struct v2i32 res = world_tile_index; + res.x += res.x < 0; + res.y += res.y < 0; + res.x = res.x % SIM_TILES_PER_CHUNK_SQRT; + res.y = res.y % SIM_TILES_PER_CHUNK_SQRT; + res.x += (world_tile_index.x < 0) * (SIM_TILES_PER_CHUNK_SQRT - 1); + res.y += (world_tile_index.y < 0) * (SIM_TILES_PER_CHUNK_SQRT - 1); return res; } @@ -594,9 +586,11 @@ struct v2i32 sim_world_tile_index_from_local_tile_index(struct v2i32 tile_chunk_ struct v2i32 sim_tile_chunk_index_from_world_tile_index(struct v2i32 world_tile_index) { - struct v2i32 res = ZI; - res.x = world_tile_index.x / SIM_TILES_PER_CHUNK_SQRT; - res.y = world_tile_index.y / SIM_TILES_PER_CHUNK_SQRT; + struct v2i32 res = world_tile_index; + res.x += res.x < 0; + res.y += res.y < 0; + res.x = res.x / SIM_TILES_PER_CHUNK_SQRT; + res.y = res.y / SIM_TILES_PER_CHUNK_SQRT; res.x -= world_tile_index.x < 0; res.y -= world_tile_index.y < 0; return res; diff --git a/src/sim_ent.c b/src/sim_ent.c index f4172c4a..28c78bb3 100644 --- a/src/sim_ent.c +++ b/src/sim_ent.c @@ -566,11 +566,17 @@ void sim_ent_apply_torque(struct sim_ent *ent, f32 torque) * Tile * ========================== */ +struct sim_ent *sim_tile_chunk_from_chunk_index(struct sim_snapshot *ss, struct v2i32 chunk_index) +{ + struct sim_ent_id chunk_id = sim_ent_tile_chunk_id_from_tile_chunk_index(chunk_index); + struct sim_ent *chunk_ent = sim_ent_from_id(ss, chunk_id); + return chunk_ent; +} + struct sim_ent *sim_tile_chunk_from_world_tile_index(struct sim_snapshot *ss, struct v2i32 world_tile_index) { struct v2i32 chunk_index = sim_tile_chunk_index_from_world_tile_index(world_tile_index); - struct sim_ent_id chunk_id = sim_ent_tile_chunk_id_from_tile_chunk_index(chunk_index); - struct sim_ent *chunk_ent = sim_ent_from_id(ss, chunk_id); + struct sim_ent *chunk_ent = sim_tile_chunk_from_chunk_index(ss, chunk_index); return chunk_ent; } diff --git a/src/sim_ent.h b/src/sim_ent.h index aeef2597..00f91031 100644 --- a/src/sim_ent.h +++ b/src/sim_ent.h @@ -543,6 +543,7 @@ void sim_ent_apply_angular_impulse(struct sim_ent *ent, f32 impulse); void sim_ent_apply_torque(struct sim_ent *ent, f32 torque); /* Tile */ +struct sim_ent *sim_tile_chunk_from_chunk_index(struct sim_snapshot *ss, struct v2i32 chunk_index); struct sim_ent *sim_tile_chunk_from_world_tile_index(struct sim_snapshot *ss, struct v2i32 world_tile_index); enum sim_tile_kind sim_get_chunk_tile(struct sim_ent *chunk_ent, struct v2i32 local_tile_index); diff --git a/src/sim_step.c b/src/sim_step.c index 90555a25..ae0525df 100644 --- a/src/sim_step.c +++ b/src/sim_step.c @@ -429,8 +429,8 @@ INTERNAL void test_generate_walls(struct sim_ent *parent) if (!chunk->valid) continue; if (sim_ent_has_prop(chunk, SEPROP_TILE_CHUNK)) { struct v2i32 chunk_index = chunk->tile_chunk_index; - struct sim_ent *top_chunk = sim_tile_chunk_from_world_tile_index(world, V2I32(chunk_index.x, chunk_index.y - 1)); - struct sim_ent *bottom_chunk = sim_tile_chunk_from_world_tile_index(world, V2I32(chunk_index.x, chunk_index.y + 1)); + struct sim_ent *top_chunk = sim_tile_chunk_from_chunk_index(world, V2I32(chunk_index.x, chunk_index.y - 1)); + struct sim_ent *bottom_chunk = sim_tile_chunk_from_chunk_index(world, V2I32(chunk_index.x, chunk_index.y + 1)); /* If there's no chunk below this one, then do an extra iteration (since walls are created at the top of each tile) */ i32 y_iterations = SIM_TILES_PER_CHUNK_SQRT + !bottom_chunk->valid; i32 x_iterations = SIM_TILES_PER_CHUNK_SQRT + 1; @@ -439,20 +439,29 @@ INTERNAL void test_generate_walls(struct sim_ent *parent) i32 wall_end = -1; for (i32 tile_x = 0; tile_x < x_iterations; ++tile_x) { enum sim_tile_kind tile = SIM_TILE_KIND_NONE; - enum sim_tile_kind top_tile = SIM_TILE_KIND_NONE; if (tile_x < SIM_TILES_PER_CHUNK_SQRT && tile_y < SIM_TILES_PER_CHUNK_SQRT) { tile = sim_get_chunk_tile(chunk, V2I32(tile_x, tile_y)); } - if (tile == SIM_TILE_KIND_NONE && tile_x < SIM_TILES_PER_CHUNK_SQRT) { + b32 should_have_top_wall = false; + if (tile_x < SIM_TILES_PER_CHUNK_SQRT) { + enum sim_tile_kind top_tile = SIM_TILE_KIND_NONE; if (tile_y == 0) { if (top_chunk->valid) { - top_tile = sim_get_chunk_tile(top_chunk, V2I32(tile_x, SIM_TILES_PER_CHUNK_SQRT - 1)); + struct v2i32 top_tile_local_index = V2I32(tile_x, SIM_TILES_PER_CHUNK_SQRT - 1); + top_tile = sim_get_chunk_tile(top_chunk, top_tile_local_index); } } else { top_tile = sim_get_chunk_tile(chunk, V2I32(tile_x, tile_y - 1)); } + if (tile == SIM_TILE_KIND_WALL) { + /* Process wall tile */ + should_have_top_wall = top_tile != SIM_TILE_KIND_WALL; + } else { + /* Process non-wall tile */ + should_have_top_wall = top_tile == SIM_TILE_KIND_WALL; + } } - if (top_tile == SIM_TILE_KIND_WALL && tile != SIM_TILE_KIND_WALL) { + if (should_have_top_wall) { if (wall_start < 0) { /* Start wall */ wall_start = tile_x;