@korial29/tiny-fps 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.js CHANGED
@@ -5,37 +5,7 @@ var PLAYER_R = .4;
5
5
  var PLAYER_H = 1.8;
6
6
  /** Pitch is clamped to ±MAX_PITCH radians, just short of straight up or down. */
7
7
  var MAX_PITCH = 1.55;
8
- /** A floor at most this much higher is walked onto; a higher one needs a jump. */
9
- var STEP_H = .5;
10
8
  var RULES = { maxPlayers: 16 };
11
- // src/shared/level.ts
12
- /** Joins every edge a→b with the sector that has b→a, if any: that edge is a portal. */
13
- var compile = (map) => {
14
- const key = (ax, az, bx, bz) => `${ax},${az},${bx},${bz}`;
15
- const owner = /* @__PURE__ */ new Map();
16
- map.sectors.forEach(({ pts }, s) => pts.forEach(([ax, az], i) => owner.set(key(ax, az, ...pts[(i + 1) % pts.length]), s)));
17
- return {
18
- map,
19
- edges: map.sectors.flatMap(({ pts }, front) => pts.map(([ax, az], i) => {
20
- const [bx, bz] = pts[(i + 1) % pts.length];
21
- return {
22
- ax,
23
- az,
24
- bx,
25
- bz,
26
- front,
27
- back: owner.get(key(bx, bz, ax, az)) ?? -1
28
- };
29
- }))
30
- };
31
- };
32
- /** Whether (x, z) is inside the sector, its edges included. */
33
- var inside = ({ pts }, x, z) => pts.every(([ax, az], i) => {
34
- const [bx, bz] = pts[(i + 1) % pts.length];
35
- return (bx - ax) * (z - az) - (bz - az) * (x - ax) >= 0;
36
- });
37
- /** Index of the first sector holding (x, z), -1 outside the map. */
38
- var sectorAt = ({ map }, x, z) => map.sectors.findIndex((s) => inside(s, x, z));
39
9
  // src/shared/types.ts
40
10
  /** Bits of Input.keys. Keys are read by KeyboardEvent.code, so WASD is ZQSD on AZERTY. */
41
11
  var KEY = {
@@ -45,7 +15,7 @@ var KEY = {
45
15
  RIGHT: 8,
46
16
  JUMP: 16
47
17
  };
48
- /** Procedural textures, generated by the client. A sector names one per surface. */
18
+ /** Procedural textures, generated by the client. A brush names one for its top and one for its sides. */
49
19
  var TEX = {
50
20
  BRICK: 0,
51
21
  TILE: 1,
@@ -61,236 +31,293 @@ var TEX = {
61
31
  GRATE: 7
62
32
  };
63
33
  // src/shared/map.ts
64
- var sector = (pts, floor, ceil, ft, light) => ({
65
- pts,
66
- floor,
67
- ceil,
68
- ft,
69
- wt: TEX.BRICK,
70
- ct: TEX.STONE,
71
- light
72
- });
73
- sector([
74
- [0, 0],
75
- [16, 0],
76
- [16, 6],
77
- [16, 10],
78
- [16, 16],
79
- [0, 16]
80
- ], 1, 5, TEX.TILE), sector([
81
- [16, 6],
82
- [20, 6],
83
- [20, 10],
84
- [16, 10]
85
- ], 1, 3.5, TEX.TILE, .6), sector([
86
- [20, 0],
87
- [36, 0],
88
- [36, 6],
89
- [30, 6],
90
- [26, 6],
91
- [20, 6]
92
- ], 1, 7, TEX.TILE), sector([
93
- [20, 6],
94
- [26, 6],
95
- [26, 10],
96
- [20, 10]
97
- ], 1, 7, TEX.TILE), sector([
98
- [30, 6],
99
- [36, 6],
100
- [36, 10],
101
- [30, 10]
102
- ], 1, 7, TEX.TILE), sector([
103
- [20, 10],
104
- [26, 10],
105
- [30, 10],
106
- [36, 10],
107
- [36, 16],
108
- [32, 16],
109
- [28, 16],
110
- [20, 16]
111
- ], 1, 7, TEX.TILE), sector([
112
- [26, 6],
113
- [30, 6],
114
- [30, 10],
115
- [26, 10]
116
- ], 0, 7, TEX.LAVA), sector([
117
- [28, 16],
118
- [32, 16],
119
- [32, 17],
120
- [28, 17]
121
- ], 1.4, 7, TEX.STONE), sector([
122
- [28, 17],
123
- [32, 17],
124
- [32, 18],
125
- [28, 18]
126
- ], 1.8, 7, TEX.STONE), sector([
127
- [28, 18],
128
- [32, 18],
129
- [32, 19],
130
- [28, 19]
131
- ], 2.2, 7, TEX.STONE), sector([
132
- [24, 19],
133
- [28, 19],
134
- [32, 19],
135
- [36, 19],
136
- [36, 26],
137
- [24, 26]
138
- ], 2.6, 7, TEX.TILE, .8);
139
- /** Grid lines of ROOF, metres: every feature below starts and ends on one, so the cells join exactly. */
140
- var GRID_X = [
141
- 0,
142
- 1.2,
143
- 7,
144
- 9,
145
- 14,
146
- 17,
147
- 23,
148
- 26,
149
- 27,
150
- 31,
151
- 38.8,
152
- 40
153
- ];
154
- var GRID_Z = [
155
- 0,
156
- 1.2,
157
- 8,
158
- 9,
159
- 10,
160
- 11,
161
- 16,
162
- 18,
163
- 24,
164
- 26,
165
- 38.8,
166
- 40
167
- ];
168
- /** A very high ceiling: the sky, until the sector walls go (step 2). */
169
- var SKY = 120;
170
- /** What ROOF is made of, painted in order: x0, z0, x1, z1, floor height, floor texture. */
171
- var PAINT = [
172
- [
173
- 0,
174
- 0,
175
- 40,
176
- 40,
177
- 1,
178
- TEX.STONE
179
- ],
180
- [
181
- 1.2,
182
- 1.2,
183
- 38.8,
184
- 38.8,
185
- 0,
186
- TEX.TILE
187
- ],
188
- [
189
- 14,
190
- 1.2,
191
- 26,
192
- 8,
193
- 1.6,
194
- TEX.TILE
34
+ /** The bottom of every building: far below the kill floor, so nothing shows beneath a roof. */
35
+ var BASE = -30;
36
+ /** A building: the footprint x0..x1 by z0..z1 from the street up to a flat roof at `top`. */
37
+ var roof = (x0, z0, x1, z1, top) => ({
38
+ min: [
39
+ x0,
40
+ BASE,
41
+ z0
195
42
  ],
196
- [
197
- 17,
198
- 8,
199
- 23,
200
- 9,
201
- 1.2,
202
- TEX.STONE
43
+ max: [
44
+ x1,
45
+ top,
46
+ z1
203
47
  ],
204
- [
205
- 17,
206
- 9,
207
- 23,
208
- 10,
209
- .8,
210
- TEX.STONE
211
- ],
212
- [
213
- 17,
214
- 10,
215
- 23,
216
- 11,
217
- .4,
218
- TEX.STONE
48
+ top: TEX.TILE,
49
+ side: TEX.BRICK
50
+ });
51
+ /** Any box, corners (x0, y0, z0) and (x1, y1, z1). */
52
+ var box = ([x0, y0, z0], [x1, y1, z1], top, side = top, tint) => ({
53
+ min: [
54
+ x0,
55
+ y0,
56
+ z0
219
57
  ],
220
- [
221
- 7,
222
- 16,
223
- 9,
224
- 18,
225
- 1,
226
- TEX.WOOD
58
+ max: [
59
+ x1,
60
+ y1,
61
+ z1
227
62
  ],
228
- [
229
- 27,
230
- 24,
231
- 31,
232
- 26,
233
- 2,
234
- TEX.METAL
235
- ]
236
- ];
63
+ top,
64
+ side,
65
+ tint
66
+ });
67
+ /** Six steps of 0.4 m, 1 m deep and 2 m wide (x 12 to 14) from z down to z - 6, on a roof at y. */
68
+ var stair = (z, y) => [
69
+ 1,
70
+ 2,
71
+ 3,
72
+ 4,
73
+ 5,
74
+ 6
75
+ ].map((k) => box([
76
+ 12,
77
+ y,
78
+ z - k
79
+ ], [
80
+ 14,
81
+ y + .4 * k,
82
+ z - k + 1
83
+ ], TEX.STONE, TEX.STONE));
237
84
  /**
238
- * The first rooftop, for the capture-the-flag game: a flat 40 x 40 m deck in the open, a 1 m
239
- * parapet round it, a crate, a block and a platform with steps. Built as a grid of cells, one
240
- * sector each: neighbours at one height are open to each other, and the edge is solid wall.
85
+ * A parkour test course, laid out for the moves of step 3 and walkable now with run, jump and the
86
+ * 0.5 m step-up. Metres, y up, the course runs north (toward -z). Jumps reach about 4 m on the
87
+ * flat, and a box 1.7 m high.
88
+ *
89
+ * - A: the start roof (10 m), with the spawns. B: a roof at 10 m, across a 2.5 m gap from A.
90
+ * - On B, a platform floating at 12.2 to 12.6 m (an overhang to walk under, a bump for a jump) and
91
+ * a stair of six 0.4 m steps up to it. 3 m north of it, C (12.4 m): the south face of C above B
92
+ * is a 2.4 m ledge to grab. Two poles stand by the platform.
93
+ * - E (8 m), 3 m east of C and 4.4 m lower: a long wall (6.5 m high, 26 m long) to run along, ledges
94
+ * 2.0 and 2.2 m high, and a 1.2 m low gap under a lintel to slide under, with a way round it.
95
+ * - G (8 m) is across a 10 m drop from E, reached by a 0.5 m wide beam; it has a 1.6 m ledge.
96
+ * - F (12.4 m) is 8 m north of C: out of reach until the dash and the double jump.
241
97
  */
242
- var ROOF = {
98
+ var ROOFTOPS = {
243
99
  name: "Rooftops",
244
- fog: [
245
- .8,
246
- .88,
247
- 1,
248
- .02
100
+ brushes: [
101
+ roof(4, 56, 24, 76, 10),
102
+ roof(4, 36, 24, 53.5, 10),
103
+ roof(4, 20, 24, 36, 12.4),
104
+ roof(4, 0, 24, 12, 12.4),
105
+ roof(27, 20, 47, 50, 8),
106
+ roof(27, 60, 47, 76, 8),
107
+ box([
108
+ 9,
109
+ 12.2,
110
+ 39
111
+ ], [
112
+ 19,
113
+ 12.6,
114
+ 47
115
+ ], TEX.METAL, TEX.METAL),
116
+ ...stair(53, 10),
117
+ box([
118
+ 5,
119
+ 10,
120
+ 42
121
+ ], [
122
+ 5.5,
123
+ 14,
124
+ 42.5
125
+ ], TEX.METAL),
126
+ box([
127
+ 5,
128
+ 10,
129
+ 50
130
+ ], [
131
+ 5.5,
132
+ 14,
133
+ 50.5
134
+ ], TEX.METAL),
135
+ box([
136
+ 8,
137
+ 12.4,
138
+ 24
139
+ ], [
140
+ 20,
141
+ 12.85,
142
+ 24.45
143
+ ], TEX.METAL),
144
+ box([
145
+ 36,
146
+ 8,
147
+ 22
148
+ ], [
149
+ 37,
150
+ 14.5,
151
+ 48
152
+ ], TEX.STONE, TEX.STONE, [
153
+ 1,
154
+ .75,
155
+ .62
156
+ ]),
157
+ box([
158
+ 30,
159
+ 8,
160
+ 28
161
+ ], [
162
+ 34,
163
+ 10,
164
+ 32
165
+ ], TEX.STONE, TEX.BRICK),
166
+ box([
167
+ 40,
168
+ 8,
169
+ 30
170
+ ], [
171
+ 44,
172
+ 10.2,
173
+ 34
174
+ ], TEX.STONE, TEX.BRICK),
175
+ box([
176
+ 28,
177
+ 9.2,
178
+ 44
179
+ ], [
180
+ 35,
181
+ 12,
182
+ 46
183
+ ], TEX.METAL, TEX.GRATE, [
184
+ .8,
185
+ .9,
186
+ 1
187
+ ]),
188
+ box([
189
+ 41.5,
190
+ 7.6,
191
+ 50
192
+ ], [
193
+ 42,
194
+ 8,
195
+ 60
196
+ ], TEX.METAL),
197
+ box([
198
+ 30,
199
+ 8,
200
+ 64
201
+ ], [
202
+ 34,
203
+ 9.6,
204
+ 68
205
+ ], TEX.STONE, TEX.BRICK)
249
206
  ],
250
- sectors: GRID_X.slice(1).flatMap((x1, i) => GRID_Z.slice(1).map((z1, j) => {
251
- const [x0, z0] = [GRID_X[i], GRID_Z[j]];
252
- const [cx, cz] = [(x0 + x1) / 2, (z0 + z1) / 2];
253
- const [, , , , floor, tex] = PAINT.filter(([ax, az, bx, bz]) => ax < cx && cx < bx && az < cz && cz < bz).at(-1);
254
- return sector([
255
- [x0, z0],
256
- [x1, z0],
257
- [x1, z1],
258
- [x0, z1]
259
- ], floor, SKY, tex);
260
- })),
261
207
  spawns: [
262
208
  [
263
209
  6,
264
- 34,
210
+ 10,
211
+ 72,
265
212
  0
266
213
  ],
267
214
  [
268
- 13,
269
- 34,
215
+ 10,
216
+ 10,
217
+ 72,
270
218
  0
271
219
  ],
272
220
  [
273
- 20,
274
- 34,
221
+ 14,
222
+ 10,
223
+ 72,
275
224
  0
276
225
  ],
277
226
  [
278
- 27,
279
- 34,
227
+ 18,
228
+ 10,
229
+ 72,
280
230
  0
281
231
  ],
282
232
  [
283
- 34,
284
- 34,
233
+ 22,
234
+ 10,
235
+ 72,
236
+ 0
237
+ ],
238
+ [
239
+ 8,
240
+ 10,
241
+ 66,
242
+ 0
243
+ ],
244
+ [
245
+ 14,
246
+ 10,
247
+ 66,
285
248
  0
286
249
  ],
287
250
  [
288
251
  20,
289
- 22,
252
+ 10,
253
+ 66,
290
254
  0
291
255
  ]
256
+ ],
257
+ floor: -25,
258
+ sun: {
259
+ dir: [
260
+ .6,
261
+ .64,
262
+ .48
263
+ ],
264
+ color: [
265
+ 3,
266
+ 2.7,
267
+ 2.2
268
+ ]
269
+ },
270
+ sky: [[
271
+ .12,
272
+ .3,
273
+ .75
274
+ ], [
275
+ .7,
276
+ .82,
277
+ .95
278
+ ]],
279
+ fog: [
280
+ .7,
281
+ .82,
282
+ .95,
283
+ .008
292
284
  ]
293
285
  };
286
+ // src/shared/world.ts
287
+ /** Metres per grid cell: about the size of the player's reach, so a query touches 1 to 4 cells. */
288
+ var CELL = 4;
289
+ /** Cell coordinates run from -OFF to OFF - 1: a 8 km square at 4 m, far more than any map. */
290
+ var OFF = 1024;
291
+ /** The key of the cell with integer coordinates (cx, cz). */
292
+ var key = (cx, cz) => (cx + OFF) * (2 * OFF) + cz + OFF;
293
+ /** Integer cell coordinate of a metre value, clamped to the keyed range. */
294
+ var at = (v, cell) => Math.min(Math.max(Math.floor(v / cell), -1024), 1023);
295
+ /** Builds the grid: each brush is listed in every cell its footprint covers. */
296
+ var build = (def, cell = CELL) => {
297
+ const grid = /* @__PURE__ */ new Map();
298
+ def.brushes.forEach(({ min, max }, i) => {
299
+ for (let cx = at(min[0], cell); cx <= at(max[0], cell); cx++) for (let cz = at(min[2], cell); cz <= at(max[2], cell); cz++) {
300
+ const k = key(cx, cz);
301
+ const list = grid.get(k);
302
+ if (list) list.push(i);
303
+ else grid.set(k, [i]);
304
+ }
305
+ });
306
+ return {
307
+ def,
308
+ cell,
309
+ grid
310
+ };
311
+ };
312
+ /**
313
+ * Indices of the brushes whose footprint may overlap the square of half side r around (x, z): every
314
+ * brush listed in a cell the square touches, once each. A candidate list, not an exact overlap.
315
+ */
316
+ var near = (w, x, z, r) => {
317
+ const out = [];
318
+ for (let cx = at(x - r, w.cell); cx <= at(x + r, w.cell); cx++) for (let cz = at(z - r, w.cell); cz <= at(z + r, w.cell); cz++) for (const i of w.grid.get(key(cx, cz)) ?? []) if (!out.includes(i)) out.push(i);
319
+ return out;
320
+ };
294
321
  // src/shared/physics.ts
295
322
  var DT = 1 / 60;
296
323
  var TAU = Math.PI * 2;
@@ -308,54 +335,89 @@ var norm = (a) => (a % TAU + TAU) % TAU;
308
335
  var forward = (yaw) => [-Math.sin(yaw), -Math.cos(yaw)];
309
336
  var f32 = Math.fround;
310
337
  var clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi);
311
- /**
312
- * Whether the edge stops a body of height h whose feet are at y: a solid wall, a floor on the far
313
- * side more than `step` above the feet, or an opening (the lower ceiling minus the higher floor)
314
- * too short for the body or under the head. SKIN keeps rounding from flipping the answer for a
315
- * player resting on a step or against a ceiling.
316
- */
317
- var shut = ({ map }, e, y, h, step) => {
318
- if (e.back < 0) return true;
319
- const f = map.sectors[e.front];
320
- const k = map.sectors[e.back];
321
- const top = Math.min(f.ceil, k.ceil);
322
- return k.floor > y + step + SKIN || top - Math.max(f.floor, k.floor) < h - SKIN || top < y + h - SKIN;
338
+ /** A player standing on spawn point n of the world (modulo the count). */
339
+ var spawn = (id, w, n) => {
340
+ const { spawns } = w.def;
341
+ const [x, y, z, yaw] = spawns[(n % spawns.length + spawns.length) % spawns.length];
342
+ return {
343
+ id,
344
+ x: f32(x),
345
+ y: f32(y),
346
+ z: f32(z),
347
+ vy: 0,
348
+ yaw: norm(yaw),
349
+ pitch: 0,
350
+ ground: true
351
+ };
323
352
  };
353
+ /** Whether the brush stops a body whose feet are at y when it walks sideways into it. */
354
+ var blocks = (b, y) => b.max[1] > y + .5 + SKIN && b.min[1] < y + 1.8 - SKIN;
355
+ /** The closest point of the brush footprint to (x, z), as the offset from it to (x, z). */
356
+ var gap = (b, x, z) => [x - clamp(x, b.min[0], b.max[0]), z - clamp(z, b.min[2], b.max[2])];
324
357
  /**
325
- * Pushes the player out of every shut edge it overlaps, deepest first (up to 4 passes: two walls
326
- * in a corner settle in two). The push is along the vector from the closest point of the edge, so
327
- * against a wall it is the normal (the other axis keeps moving: the slide) and at an endpoint it
328
- * is radial: a player running at a corner slides round it. Deepest first matters on a straight
329
- * wall split by a collinear point: the endpoint of the next piece is always shallower than the
330
- * normal push of the piece the player is on, so it never pulls him back along the wall.
331
- * An edge only counts from its front side (the inside of its sector); across it, the twin edge of
332
- * the neighbour decides.
333
- * Tunnelling is impossible: a step is RUN_SPEED / TICK_HZ = 0.1 m and the axis ends each step at
334
- * least PLAYER_R = 0.4 m from every shut edge it started beside, so it cannot cross one in 0.1 m.
358
+ * Pushes the player out of every brush that blocks it and that the circle overlaps, deepest first
359
+ * (up to 4 passes: two walls in a corner settle in two). The push is along the vector from the
360
+ * closest point of the box, so against a side it is the normal (the other axis keeps moving: the
361
+ * slide) and at an edge it is radial: a player running at a corner slides round it. Deepest first
362
+ * matters on a flat wall made of several boxes: the corner of the next box is always shallower
363
+ * than the normal push of the box the player is on, so it never pulls him back along the wall.
364
+ * A centre inside a footprint (it never gets there by walking, see below) leaves by the nearest
365
+ * side.
366
+ *
367
+ * Tunnelling is impossible: a step is RUN_SPEED / TICK_HZ = 0.1 m, and each push leaves the centre
368
+ * at least PLAYER_R = 0.4 m from the footprint of every box it started beside, so a 0.1 m step
369
+ * cannot bring the centre across one, however thin. The candidates come from near() with twice
370
+ * PLAYER_R, enough for the one push of at most PLAYER_R that matters.
335
371
  */
336
- var push = (p, lvl, r, h, step) => {
337
- const reach = (r - EPS) ** 2;
372
+ var push = (p, w) => {
373
+ const { brushes } = w.def;
374
+ const ids = near(w, p.x, p.z, 2 * PLAYER_R);
375
+ const reach = (PLAYER_R - EPS) ** 2;
338
376
  for (let pass = 0; pass < 4; pass++) {
339
377
  let best = 0;
340
378
  let px = 0;
341
379
  let pz = 0;
342
- for (const e of lvl.edges) {
343
- const ex = e.bx - e.ax;
344
- const ez = e.bz - e.az;
345
- const rx = p.x - e.ax;
346
- const rz = p.z - e.az;
347
- if (ex * rz - ez * rx < 0) continue;
348
- const t = clamp((rx * ex + rz * ez) / (ex * ex + ez * ez), 0, 1);
349
- const dx = rx - t * ex;
350
- const dz = rz - t * ez;
380
+ for (const i of ids) {
381
+ const b = brushes[i];
382
+ if (!blocks(b, p.y)) continue;
383
+ const [dx, dz] = gap(b, p.x, p.z);
351
384
  const d2 = dx * dx + dz * dz;
352
385
  if (d2 >= reach) continue;
353
- const d = Math.sqrt(d2);
354
- if (r - d <= best || !shut(lvl, e, p.y, h, step)) continue;
355
- best = r - d;
356
- const n = d || Math.hypot(ex, ez);
357
- px = (d ? dx : -ez) / n * best;
358
- pz = (d ? dz : ex) / n * best;
386
+ if (d2) {
387
+ const d = Math.sqrt(d2);
388
+ if (.4 - d > best) {
389
+ best = PLAYER_R - d;
390
+ px = dx / d * best;
391
+ pz = dz / d * best;
392
+ }
393
+ continue;
394
+ }
395
+ const [m, nx, nz] = [
396
+ [
397
+ p.x - b.min[0],
398
+ -1,
399
+ 0
400
+ ],
401
+ [
402
+ b.max[0] - p.x,
403
+ 1,
404
+ 0
405
+ ],
406
+ [
407
+ p.z - b.min[2],
408
+ 0,
409
+ -1
410
+ ],
411
+ [
412
+ b.max[2] - p.z,
413
+ 0,
414
+ 1
415
+ ]
416
+ ].reduce((a, c) => c[0] < a[0] ? c : a);
417
+ if (m + .4 > best) {
418
+ best = m + PLAYER_R;
419
+ [px, pz] = [nx * best, nz * best];
420
+ }
359
421
  }
360
422
  if (!best) return;
361
423
  p.x += px;
@@ -363,42 +425,40 @@ var push = (p, lvl, r, h, step) => {
363
425
  }
364
426
  };
365
427
  /**
366
- * The floor and the ceiling under the player: the highest floor and the lowest ceiling of the
367
- * sector holding the axis and of every sector across a portal the circle overlaps and could walk
368
- * through (a shut edge is a wall: the circle never counts what lies behind it).
428
+ * The floor and the ceiling under the player: the highest top the player can stand on (at most
429
+ * STEP_H above the feet) and the lowest bottom above the head, among the boxes the circle overlaps.
430
+ * Every top between the feet and the fall is a candidate and so is every bottom above the head,
431
+ * however far: a fall of any speed stops on the first top under the circle, and a rise (at most
432
+ * JUMP_SPEED / TICK_HZ = 0.12 m a step) cannot pass a bottom. Only a corner clipped by under a
433
+ * millimetre of overlap at one end of a step and none at the other could slip past.
369
434
  */
370
- var span = (lvl, x, z, y, r, h, step) => {
371
- const { map, edges } = lvl;
372
- const s = Math.max(sectorAt(lvl, x, z), 0);
373
- let { floor, ceil } = map.sectors[s];
374
- const reach = (r - EPS) ** 2;
375
- for (const e of edges) {
376
- if (e.front !== s || e.back < 0) continue;
377
- const ex = e.bx - e.ax;
378
- const ez = e.bz - e.az;
379
- const rx = x - e.ax;
380
- const rz = z - e.az;
381
- const t = clamp((rx * ex + rz * ez) / (ex * ex + ez * ez), 0, 1);
382
- if ((rx - t * ex) ** 2 + (rz - t * ez) ** 2 < reach && !shut(lvl, e, y, h, step)) {
383
- const k = map.sectors[e.back];
384
- floor = Math.max(floor, k.floor);
385
- ceil = Math.min(ceil, k.ceil);
386
- }
435
+ var span = (p, w) => {
436
+ const { brushes } = w.def;
437
+ const reach = (PLAYER_R - EPS) ** 2;
438
+ let floor = -Infinity;
439
+ let ceil = Infinity;
440
+ for (const i of near(w, p.x, p.z, PLAYER_R)) {
441
+ const b = brushes[i];
442
+ const [dx, dz] = gap(b, p.x, p.z);
443
+ if (dx * dx + dz * dz >= reach) continue;
444
+ if (b.max[1] <= p.y + .5 + SKIN) floor = Math.max(floor, b.max[1]);
445
+ else if (b.min[1] >= p.y + 1.8 - SKIN) ceil = Math.min(ceil, b.min[1]);
387
446
  }
388
447
  return [floor, ceil];
389
448
  };
390
449
  /**
391
450
  * Moves the feet by (dx, dz) for 1 / TICK_HZ s, then a jump from the ground, GRAVITY and the
392
- * collisions of the cylinder (PLAYER_R, PLAYER_H) with the sectors of the level: it slides along
393
- * shut edges (see shut and push), stands on the highest floor under the circle (a step up to
394
- * STEP_H is climbed at once, a higher one needs a jump, a ledge is a fall), and bumps its head on
395
- * the lowest ceiling, in place.
451
+ * collisions of the cylinder (PLAYER_R, PLAYER_H) with the brushes of the world: it slides along
452
+ * their sides (see push), stands on the highest top under the circle (a top up to STEP_H above the
453
+ * feet is climbed at once, a higher one needs a jump, an edge is a fall), and bumps its head on the
454
+ * lowest bottom, in place. Feet under the world's kill floor put the player back at a spawn.
455
+ * Sides and tops are judged from the feet at the start of the step.
396
456
  */
397
- var walk = (p, dx, dz, jump, lvl) => {
457
+ var walk = (p, dx, dz, jump, w) => {
398
458
  p.x += dx;
399
459
  p.z += dz;
400
- push(p, lvl, PLAYER_R, PLAYER_H, STEP_H);
401
- const [floor, ceil] = span(lvl, p.x, p.z, p.y, PLAYER_R, PLAYER_H, STEP_H);
460
+ push(p, w);
461
+ const [floor, ceil] = span(p, w);
402
462
  if (p.ground && jump) p.vy = 7;
403
463
  const vy = p.vy - 20 * DT;
404
464
  p.y += (p.vy + vy) / 2 * DT;
@@ -415,6 +475,16 @@ var walk = (p, dx, dz, jump, lvl) => {
415
475
  p.ground = true;
416
476
  }
417
477
  }
478
+ if (p.y < w.def.floor) {
479
+ const { x, y, z } = spawn(p.id, w, p.id);
480
+ Object.assign(p, {
481
+ x,
482
+ y,
483
+ z,
484
+ vy: 0,
485
+ ground: true
486
+ });
487
+ }
418
488
  p.x = f32(p.x);
419
489
  p.y = f32(p.y);
420
490
  p.z = f32(p.z);
@@ -424,30 +494,14 @@ var walk = (p, dx, dz, jump, lvl) => {
424
494
  * Applies one input for 1 / TICK_HZ s, in place: yaw and pitch from the input (pitch clamped to
425
495
  * MAX_PITCH), RUN_SPEED along the held keys (diagonals not faster), then walk().
426
496
  */
427
- var move = (p, i, lvl) => {
497
+ var move = (p, i, w) => {
428
498
  p.yaw = norm(i.yaw);
429
499
  p.pitch = Math.max(-MAX_PITCH, Math.min(MAX_PITCH, i.pitch));
430
500
  const f = (i.keys & KEY.FORWARD ? 1 : 0) - (i.keys & KEY.BACK ? 1 : 0);
431
501
  const s = (i.keys & KEY.RIGHT ? 1 : 0) - (i.keys & KEY.LEFT ? 1 : 0);
432
502
  const d = 6 * DT / (Math.hypot(f, s) || 1);
433
503
  const [fx, fz] = forward(p.yaw);
434
- walk(p, (fx * f - fz * s) * d, (fz * f + fx * s) * d, (i.keys & KEY.JUMP) !== 0, lvl);
435
- };
436
- /** A player standing on the floor at spawn point n of the map (modulo the count). */
437
- var spawn = (id, lvl, n) => {
438
- const { spawns, sectors } = lvl.map;
439
- const [x, z, yaw] = spawns[(n % spawns.length + spawns.length) % spawns.length];
440
- const y = sectors[Math.max(sectorAt(lvl, x, z), 0)].floor;
441
- return {
442
- id,
443
- x: f32(x),
444
- y: f32(y),
445
- z: f32(z),
446
- vy: 0,
447
- yaw: norm(yaw),
448
- pitch: 0,
449
- ground: true
450
- };
504
+ walk(p, (fx * f - fz * s) * d, (fz * f + fx * s) * d, (i.keys & KEY.JUMP) !== 0, w);
451
505
  };
452
506
  var TURN = 65536 / (2 * Math.PI);
453
507
  /** Radians to 65536ths of a turn; the mask also folds negative and ≥ 2π angles into one turn. */
@@ -552,18 +606,18 @@ var Room = class {
552
606
  ticks = 0;
553
607
  /** The players as each snapshot of the last MAX_REWIND ticks sent them. */
554
608
  history = [];
555
- lvl;
609
+ world;
556
610
  halt;
557
611
  rules;
558
612
  /** Round-robin cursor over the spawn points, used once they are all taken. */
559
613
  rr = 0;
560
614
  /** Missing rules fall back to RULES. */
561
- constructor(rules = {}, map = ROOF) {
615
+ constructor(rules = {}, map = ROOFTOPS) {
562
616
  this.rules = {
563
617
  ...RULES,
564
618
  ...rules
565
619
  };
566
- this.lvl = compile(map);
620
+ this.world = build(map);
567
621
  }
568
622
  /** Number of joined players. */
569
623
  get size() {
@@ -584,7 +638,7 @@ var Room = class {
584
638
  const input = p && decodeInput(data);
585
639
  if (input && p.bank > 0) {
586
640
  p.bank--;
587
- move(p.state, input, this.lvl);
641
+ move(p.state, input, this.world);
588
642
  p.ack = input.seq;
589
643
  }
590
644
  }
@@ -673,11 +727,11 @@ var Room = class {
673
727
  }
674
728
  /** The first spawn point with none of `others` within SPAWN_CLEAR, else the next one round-robin. */
675
729
  place(id, others) {
676
- const n = this.lvl.map.spawns.findIndex((_, k) => {
677
- const { x, z } = spawn(id, this.lvl, k);
730
+ const n = this.world.def.spawns.findIndex((_, k) => {
731
+ const { x, z } = spawn(id, this.world, k);
678
732
  return !others.some((o) => Math.hypot(o.x - x, o.z - z) < SPAWN_CLEAR);
679
733
  });
680
- return spawn(id, this.lvl, n < 0 ? this.rr++ : n);
734
+ return spawn(id, this.world, n < 0 ? this.rr++ : n);
681
735
  }
682
736
  players() {
683
737
  return [...this.peers.values()].filter((p) => p !== null);