summaryrefslogtreecommitdiff
path: root/platformer/src/world.c
diff options
context:
space:
mode:
Diffstat (limited to 'platformer/src/world.c')
-rw-r--r--platformer/src/world.c208
1 files changed, 185 insertions, 23 deletions
diff --git a/platformer/src/world.c b/platformer/src/world.c
index 4ae0d00..fb7a840 100644
--- a/platformer/src/world.c
+++ b/platformer/src/world.c
@@ -2,47 +2,209 @@
* ============================================================
* FILE: world.c
*
- * PURPOSE: The blank test world: an empty, flat square of ground where we
- * tune movement. It stays empty on purpose, like the test space
- * SM64's team used before building real levels.
+ * PURPOSE: The test world: a flat square of ground with a scatter of box
+ * platforms to run around and jump on. It's still a test space,
+ * like the one SM64's team tuned Mario in, just with things to
+ * jump onto now.
*
* FITS IN: Implements world.h. Has no dependencies besides config.h and
- * math3d.h. physics.c is its main caller.
+ * math3d.h. physics.c is its main caller; render.c draws its boxes.
*
* IMPLEMENT:
- * - world_init(): nothing to do yet. Leave a comment saying this is where
- * level data would be loaded.
- * - world_ground_height(x, z): return 0.0f. Mark the unused parameters with
- * (void)x; (void)z; so -Wextra stays quiet, and add a comment saying this
- * is where a heightmap/platform lookup will go.
- * - world_clamp_to_bounds(p): clamp p.x and p.z with clampf from math3d.h.
- * - world_spawn_point(): return vec3_make(0, 0, 0).
- * - world_half_extent(): return WORLD_HALF_EXTENT.
+ * - Level data: a static array of WORLD_PLATFORM_COUNT boxes, filled by
+ * world_init().
*
- * CONVENTIONS: Y up. The ground surface is the plane y = world_ground_height.
+ * - world_init(): generate the platforms with a tiny xorshift32 random
+ * generator seeded from WORLD_SEED. WHY our own instead of rand(): rand()
+ * differs between C libraries, so the "same" seed could give a different
+ * level on another machine. Two kinds of platform (ranges are named
+ * constants at the top of this file):
+ * block stands on the ground (bottom 0), top low enough to jump onto
+ * from the ground (a full jump lifts the feet ~2.3 m)
+ * floating a thin slab higher up, reached by jumping from a block
+ * Each gets a random footprint and a random spot within WORLD_PLATFORM_AREA
+ * of spawn (x and z). Re-roll the spot if the footprint comes within
+ * SPAWN_CLEAR_RADIUS of spawn, so the player never starts inside a platform.
+ * Platforms may overlap each other; that's fine for both collision and
+ * drawing.
*
- * NOT IN SCOPE: Anything that isn't flat ground. Don't build a collision
- * system "for later"; add it when the first platform shows up.
+ * - 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.
+ * 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_clamp_to_bounds, world_spawn_point, world_half_extent: as before.
*
- * DONE WHEN: It compiles warning-free, and the player can't leave the square.
+ * CONVENTIONS: Y up. Boxes are axis-aligned. The ground surface is y = 0.
+ *
+ * NOT IN SCOPE: Level files, guaranteeing every platform is reachable, slopes,
+ * moving platforms.
+ *
+ * DONE WHEN: It compiles warning-free, the same seed always builds the same
+ * level, spawn is clear, and the player can't leave the square.
* ============================================================
*/
+#include <stdint.h>
+
#include "config.h"
#include "world.h"
+/* Platform generation ranges (level design, not movement feel). */
+#define PLATFORM_MIN_SIZE 2.0f /* m, shortest footprint side */
+#define PLATFORM_MAX_SIZE 6.0f /* m, longest footprint side */
+#define BLOCK_MIN_TOP 0.6f /* m, lowest ground block */
+#define BLOCK_MAX_TOP 2.0f /* m, highest ground block (full jump reaches ~2.3) */
+#define FLOATING_MIN_TOP 2.6f /* m, lowest floating slab top */
+#define FLOATING_MAX_TOP 4.2f /* m, highest floating slab top (reachable from a block) */
+#define FLOATING_THICKNESS 0.5f /* m, floating slab thickness */
+#define FLOATING_CHANCE 0.35f /* fraction of platforms that float */
+#define SPAWN_CLEAR_RADIUS 4.0f /* m around spawn kept free of platforms */
+#define MAX_PLACEMENT_TRIES 50 /* re-rolls before giving up on a platform */
+
+#define SURFACE_EPSILON 0.01f /* m, "standing exactly on it" tolerance */
+#define OVERLAP_EPSILON 1e-4f /* m, edges closer than this only touch */
+
+static Box s_boxes[WORLD_PLATFORM_COUNT];
+static int s_box_count;
+static uint32_t s_rng_state;
+
+/* ---- Random numbers ---------------------------------------------------- */
+
+/* xorshift32: three shifts and XORs give a decent, fully repeatable sequence. */
+static uint32_t next_random(void)
+{
+ s_rng_state ^= s_rng_state << 13;
+ s_rng_state ^= s_rng_state >> 17;
+ s_rng_state ^= s_rng_state << 5;
+ return s_rng_state;
+}
+
+/* A float in [lo, hi]. */
+static float random_range(float lo, float hi)
+{
+ return lo + (hi - lo) * ((float)next_random() / 4294967295.0f);
+}
+
+/* ---- Level generation -------------------------------------------------- */
+
+/* True if the footprint comes within SPAWN_CLEAR_RADIUS of spawn. Uses the
+ * closest point of the footprint to spawn, so big platforms can't sneak in. */
+static bool too_close_to_spawn(Box b)
+{
+ Vec3 spawn = world_spawn_point();
+ float dx = clampf(spawn.x, b.min.x, b.max.x) - spawn.x;
+ float dz = clampf(spawn.z, b.min.z, b.max.z) - spawn.z;
+ return dx * dx + dz * dz < SPAWN_CLEAR_RADIUS * SPAWN_CLEAR_RADIUS;
+}
+
+/* One platform with a random size and height (but no position yet). */
+static Box random_platform_shape(void)
+{
+ float half_x = 0.5f * random_range(PLATFORM_MIN_SIZE, PLATFORM_MAX_SIZE);
+ float half_z = 0.5f * random_range(PLATFORM_MIN_SIZE, PLATFORM_MAX_SIZE);
+ float bottom, top;
+ if (random_range(0.0f, 1.0f) < FLOATING_CHANCE) {
+ top = random_range(FLOATING_MIN_TOP, FLOATING_MAX_TOP);
+ bottom = top - FLOATING_THICKNESS;
+ } else {
+ top = random_range(BLOCK_MIN_TOP, BLOCK_MAX_TOP);
+ bottom = 0.0f;
+ }
+ Box b = { { -half_x, bottom, -half_z }, { half_x, top, half_z } };
+ return b;
+}
+
+/* Moves a box so its footprint is centered on (x, z). */
+static Box place_box(Box b, float x, float z)
+{
+ Vec3 offset = vec3_make(x, 0.0f, z);
+ b.min = vec3_add(b.min, offset);
+ b.max = vec3_add(b.max, offset);
+ return b;
+}
+
void world_init(void)
{
- /* Nothing to do yet. This is where level data would be loaded. */
+ s_rng_state = WORLD_SEED ? WORLD_SEED : 1u; /* xorshift must not start at 0 */
+ s_box_count = 0;
+
+ for (int i = 0; i < WORLD_PLATFORM_COUNT; i++) {
+ Box shape = random_platform_shape();
+ for (int tries = 0; tries < MAX_PLACEMENT_TRIES; tries++) {
+ Box placed = place_box(shape,
+ random_range(-WORLD_PLATFORM_AREA, WORLD_PLATFORM_AREA),
+ random_range(-WORLD_PLATFORM_AREA, WORLD_PLATFORM_AREA));
+ if (!too_close_to_spawn(placed)) {
+ s_boxes[s_box_count++] = placed;
+ break;
+ }
+ }
+ }
+}
+
+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)
+{
+ float floor_y = 0.0f; /* the ground */
+ 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;
+ }
+ 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;
}
-float world_ground_height(float x, float z)
+bool world_find_overlap(Vec3 feet, float half_width, float height, Box *hit)
{
- /* The world is flat for now. This is where a heightmap/platform lookup
- * will go once there's terrain to stand on. */
- (void)x;
- (void)z;
- return 0.0f;
+ 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. */