summaryrefslogtreecommitdiff
path: root/platformer/src/world.c
blob: fb7a840346b68006ae10c7b1bcb6db893fb5a9a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * ============================================================
 * FILE:        world.c
 *
 * 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; render.c draws its boxes.
 *
 * IMPLEMENT:
 *   - Level data: a static array of WORLD_PLATFORM_COUNT boxes, filled by
 *     world_init().
 *
 *   - 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.
 *
 *   - 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.
 *
 * 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)
{
    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;
}

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 vec3_make(0.0f, 0.0f, 0.0f);
}

float world_half_extent(void)
{
    return WORLD_HALF_EXTENT;
}