/* * ============================================================ * FILE: world.c * * PURPOSE: Where the level lives while the game runs: it stores the boxes * level.c builds, moves the moving ones every tick, and answers * geometry questions about them (floors, ceilings, overlaps). * * FITS IN: Implements world.h. Calls level_build() once and * level_motion_offset() every tick (level.h). physics.c is its * main caller; render.c draws its boxes; main.c calls * world_update() once per tick. * * IMPLEMENT: * - Storage: the built LevelBox list (each box at offset 0 plus its motion), * the boxes where they are NOW, and how far each moved last tick. * - world_init(): level_build(), then place every box at its time-0 spot. * - world_update(dt): advance the clock; move each moving box to its built * position + level_motion_offset(motion, time), remembering how far it * moved (world_box_moved) so physics can carry whatever stands on it. * * - Queries, all over the body's FOOTPRINT (a square of +-half_width, the * same shape as the player's box), with edges that only touch NOT counting: * world_floor_below: the highest top <= feet.y + SURFACE_EPSILON among * boxes whose footprint overlaps, or 0 (the ground). The epsilon lets * a body standing exactly on a top still find it. Also reports WHICH * box that is (-1 for the ground), for riding moving platforms. * world_ceiling_above: the lowest bottom >= head.y - SURFACE_EPSILON, or * WORLD_NO_CEILING. * world_find_overlap: the first box that overlaps the body's full volume * (footprint AND height range). * - world_object_at(point, radius, hit): the first object (never a zone) * that, grown by `radius` on every side, contains the point. * - Zones (level_build_zones()): stored separately from the objects, so no * collision query ever sees them. world_in_zone(point, kind): is the point * inside (edges count) any zone of that kind. * - world_clamp_to_bounds, world_spawn_point (level_spawn_point()), * world_half_extent. * * CONVENTIONS: Y up. Boxes are axis-aligned. The ground surface is y = 0. * * NOT IN SCOPE: Deciding what's in the level (level.c), rotating boxes, * spatial partitioning (a few hundred boxes is fine to loop over). * * DONE WHEN: physics.c and render.c see every box where it is this tick, and * moving boxes report how far they moved. * ============================================================ */ #include "config.h" #include "level.h" #include "world.h" #define SURFACE_EPSILON 0.01f /* m, "standing exactly on it" tolerance */ #define OVERLAP_EPSILON 1e-4f /* m, edges closer than this only touch */ static LevelBox s_level[LEVEL_MAX_BOXES]; /* as built: each box at offset 0, and how it moves */ static Box s_boxes[LEVEL_MAX_BOXES]; /* where each box is now */ static Vec3 s_moved[LEVEL_MAX_BOXES]; /* how far each box moved in the last world_update */ static int s_box_count; static float s_time; /* s since world_init */ static Zone s_zones[LEVEL_MAX_ZONES]; /* non-solid regions, kept apart from the boxes */ static int s_zone_count; /* Where box i is at the current time. */ static Box box_now(int i) { Vec3 offset = level_motion_offset(s_level[i].motion, s_time); Box b = s_level[i].box; b.min = vec3_add(b.min, offset); b.max = vec3_add(b.max, offset); return b; } void world_init(void) { s_box_count = level_build(s_level, LEVEL_MAX_BOXES); s_zone_count = level_build_zones(s_zones, LEVEL_MAX_ZONES); s_time = 0.0f; for (int i = 0; i < s_box_count; i++) { s_boxes[i] = box_now(i); s_moved[i] = vec3_make(0.0f, 0.0f, 0.0f); } } void world_update(float dt) { s_time += dt; for (int i = 0; i < s_box_count; i++) { if (s_level[i].motion.kind == MOTION_NONE) continue; /* s_moved stays 0 */ Box now = box_now(i); s_moved[i] = vec3_sub(now.min, s_boxes[i].min); s_boxes[i] = now; } } Vec3 world_box_moved(int i) { return s_moved[i]; } int world_box_count(void) { return s_box_count; } Box world_box(int i) { return s_boxes[i]; } /* ---- Geometry queries -------------------------------------------------- */ /* Does the footprint around (x, z) overlap the box's footprint? Edges that * only touch don't count, so a body pushed flush against a wall isn't "in" it. */ static bool footprint_overlaps(Box b, float x, float z, float half_width) { return x + half_width > b.min.x + OVERLAP_EPSILON && x - half_width < b.max.x - OVERLAP_EPSILON && z + half_width > b.min.z + OVERLAP_EPSILON && z - half_width < b.max.z - OVERLAP_EPSILON; } float world_floor_below(Vec3 feet, float half_width, int *box) { float floor_y = 0.0f; /* the ground */ int floor_box = -1; for (int i = 0; i < s_box_count; i++) { Box b = s_boxes[i]; if (footprint_overlaps(b, feet.x, feet.z, half_width) && b.max.y <= feet.y + SURFACE_EPSILON && b.max.y > floor_y) { floor_y = b.max.y; floor_box = i; } } if (box) *box = floor_box; return floor_y; } float world_ceiling_above(Vec3 head, float half_width) { float ceiling_y = WORLD_NO_CEILING; for (int i = 0; i < s_box_count; i++) { Box b = s_boxes[i]; if (footprint_overlaps(b, head.x, head.z, half_width) && b.min.y >= head.y - SURFACE_EPSILON && b.min.y < ceiling_y) ceiling_y = b.min.y; } return ceiling_y; } bool world_find_overlap(Vec3 feet, float half_width, float height, Box *hit) { for (int i = 0; i < s_box_count; i++) { Box b = s_boxes[i]; bool vertical = feet.y < b.max.y - OVERLAP_EPSILON && feet.y + height > b.min.y + OVERLAP_EPSILON; if (vertical && footprint_overlaps(b, feet.x, feet.z, half_width)) { *hit = b; return true; } } return false; } /* Keeps x and z on the ground square; y (height) is untouched. */ Vec3 world_clamp_to_bounds(Vec3 p) { p.x = clampf(p.x, -WORLD_HALF_EXTENT, WORLD_HALF_EXTENT); p.z = clampf(p.z, -WORLD_HALF_EXTENT, WORLD_HALF_EXTENT); return p; } Vec3 world_spawn_point(void) { return level_spawn_point(); } bool world_object_at(Vec3 point, float radius, Box *hit) { for (int i = 0; i < s_box_count; i++) { Box b = s_boxes[i]; if (point.x > b.min.x - radius && point.x < b.max.x + radius && point.y > b.min.y - radius && point.y < b.max.y + radius && point.z > b.min.z - radius && point.z < b.max.z + radius) { *hit = b; return true; } } return false; } int world_zone_count(void) { return s_zone_count; } Zone world_zone(int i) { return s_zones[i]; } bool world_in_zone(Vec3 point, ZoneKind kind) { for (int i = 0; i < s_zone_count; i++) { Box b = s_zones[i].bounds; if (s_zones[i].kind == kind && point.x >= b.min.x && point.x <= b.max.x && point.y >= b.min.y && point.y <= b.max.y && point.z >= b.min.z && point.z <= b.max.z) return true; } return false; } float world_half_extent(void) { return WORLD_HALF_EXTENT; }