@pocketrts/grid 0.5.0 → 0.7.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/README.md +13 -1
- package/dist/march.js +95 -52
- package/dist/marchChoke.d.ts +17 -6
- package/dist/marchChoke.js +26 -5
- package/dist/marchSlots.d.ts +18 -6
- package/dist/marchSlots.js +68 -12
- package/dist/marchTrail.d.ts +13 -9
- package/dist/marchTrail.js +125 -56
- package/dist/marchTypes.d.ts +13 -6
- package/dist/marchTypes.js +1 -1
- package/dist/vision.d.ts +54 -2
- package/dist/vision.js +114 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @pocketrts/grid
|
|
2
2
|
|
|
3
|
-
Grid algorithms for real-time strategy games on an 8-connected cell grid: flow fields shared by whole armies and computed under a fixed per-step budget, bounded local A*, float-cost A* within an area, 4-connected regions, straight-line reach, nearest free cell, distance fields, bucket grids for neighbour queries, field of view, polygons, 8-connected wall chains, line formations and a complete elastic-column march system. Pure logic over typed arrays, deterministic (only `@pocketrts/determinism` math), no DOM.
|
|
3
|
+
Grid algorithms for real-time strategy games on an 8-connected cell grid: flow fields shared by whole armies and computed under a fixed per-step budget, bounded local A*, float-cost A* within an area, 4-connected regions, straight-line reach, nearest free cell, distance fields, bucket grids for neighbour queries, field of view, fog of war per side, polygons, 8-connected wall chains, line formations and a complete elastic-column march system. Pure logic over typed arrays, deterministic (only `@pocketrts/determinism` math), no DOM.
|
|
4
4
|
|
|
5
5
|
Try it: [`apps/grid-demo`](../../apps/grid-demo) (`pnpm --filter @pocketrts/grid-demo dev`).
|
|
6
6
|
|
|
@@ -33,6 +33,7 @@ ES module, TypeScript types included. Depends on `@pocketrts/determinism`.
|
|
|
33
33
|
| `chamferDistance(src, w, h)` | Two-pass distance to the nearest source cell. |
|
|
34
34
|
| `BucketGrid` | Counting-sort bucket grid rebuilt each step: `build(n, x, y, alive)`, `start` / `items` runs per cell, `cellAt`, `forEachNear(x, y, range, visit)`. |
|
|
35
35
|
| `stampDisc(vis, stamped, w, h, cx, cy, r)` | Reveal a disc of cells; repeated stamps from one cell cost nothing. |
|
|
36
|
+
| `FogOfWar(w, h, sides, side?)` | Fog of war for sides 1 to `sides` (0 = the whole map): `update(vis, version)` takes each side's visible cells once per version and adds them to that side's explored cells; `watch(side)` switches the view; `visible` / `explored` are the viewed side's arrays (a different array after `watch`, so read them through the getters) and `isVisible` / `isExplored` ask about the viewed side; `visibleOf` / `exploredOf` give any side's arrays; `reset()` forgets everything. |
|
|
36
37
|
| `inPolygon(points, x, y)`, `polygonDist(points, x, y)` | Even–odd containment and distance to the outline of a flat `[x0, y0, x1, y1, …]` polygon. |
|
|
37
38
|
| `stepCells`, `ringChain`, `chainLinks`, `chainOutSides` | 8-connected chains (walls, fences): cells between two points, a closed chain through waypoints with loops and extra corner cells removed, which neighbours a chain cell joins, which side of each joined segment faces out. |
|
|
38
39
|
| `lineFormation(units, cx, cy, dx, dy, positions?, options?)`, `FormationUnit`, `Formation`, `FormationOptions`, `DEFAULT_FORMATION_OPTIONS` | Ranks facing a direction: one rank class per rank (lower in front), frontage from the group's footprint (with `taper`, the ranks in front narrower: a wedge); with current positions, members keep their lateral order so paths do not cross. Returns spots, offsets, rank and place per member, and per-rank width, depth, count, radius and slowest speed. |
|
|
@@ -157,3 +158,14 @@ Start with `marchRules(cellSize / 32)`. The column forms up along the route, kee
|
|
|
157
158
|
- For games that let members advance along their own routes during combat, set `pushAhead` to the distance in world units a member must be ahead of its place to receive `STEER_PUSH` while the group is opposed. The default is `Infinity`.
|
|
158
159
|
- For subcell passages such as diagonal bridges, keep the map's world-space movement queries and optional `crossSection` measurements. They supply the physical boundaries of the passage.
|
|
159
160
|
- March trajectories and checksums change with this version. Pin the same grid version on every peer in a lockstep match and replay recordings with the version that produced them.
|
|
161
|
+
|
|
162
|
+
## Upgrading the march from 0.6 to 0.7
|
|
163
|
+
|
|
164
|
+
The column is elastic along its length as well. Each rank moves on at its own pace, at most `trot` of its slowest member's full speed. A rank that cannot keep up with the one ahead of it (it is still in a narrow place, or slower) falls behind instead of holding the ranks ahead back, and closes up again afterwards. The front rank slows as the column strings out and stands once the gaps add up to `slack` times the column's depth. Near the goal it paces itself so that the ranks arrive closed up. A large column no longer crawls while its rear is still in a narrow place.
|
|
165
|
+
|
|
166
|
+
- `slack` is a new rule, 2 by default. Lower it to keep the column tighter at the price of a slower front rank after a narrow place.
|
|
167
|
+
- In the march state, `rowK` is gone. `rowV` holds each rank's speed along the trail and `rowGap` how far it has fallen behind the rank ahead of it. Games that hash or serialize a march by its properties get them in the new order.
|
|
168
|
+
- A trail point on a cell that is solid on the grid, reached through the map's own world-space queries, no longer counts as a narrow place of no width.
|
|
169
|
+
- Beside a wall, the ends of a rank go by the room along the rank's own line, looked up on the grid, instead of the width measured across the route: a column rounding the end of a wall keeps to the wall, and a rank end no longer reaches into a wall the rank stands askew to. That room narrows and widens gradually along the trail like the widths across the route, and on the inside of a turn it counts no further than the centre of the turn. The probe still goes by the widths across the route, and so do ranks where the ground supplies its own `crossSection`.
|
|
170
|
+
- A rank turns towards the direction of a narrow place only as far as that direction lies across the column there: fully while it is within about 63° of the line square to the heading, less beyond that, and not at all beyond about 81°. A direction carried along the trail to where the column heads another way could otherwise stand a rank along the route, end for end from the next.
|
|
171
|
+
- March trajectories and checksums change with this version. Pin the same grid version on every peer in a lockstep match and replay recordings with the version that produced them.
|
package/dist/march.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
import { dmath, smooth } from '@pocketrts/determinism';
|
|
12
12
|
import { nearestCell } from './cells.js';
|
|
13
13
|
import { DIR_HERE, DIR_NONE, DIRX, DIRY } from './dirs.js';
|
|
14
|
-
import { FOLLOW, FOLLOW_APART, FOLLOW_SLOT, SLOT, marchFollow, marchRelease, marchSlot, rowsAt } from './marchSlots.js';
|
|
14
|
+
import { FOLLOW, FOLLOW_APART, FOLLOW_SLOT, SLOT, frontWheel, marchFollow, marchRelease, marchSlot, rowsAt, rowsStep } from './marchSlots.js';
|
|
15
15
|
import { TP, TRAIL, TRAIL_MAX, addPoint, cutAhead, layTrail, nearCell, routeCut, trailAt, widthAt } from './marchTrail.js';
|
|
16
16
|
import { MEMBER_ENGAGED, MEMBER_FREE, MEMBER_OPPOSED, MEMBER_OUT, } from './marchTypes.js';
|
|
17
17
|
/** The furthest the probe walks in one go, in units of `MarchRules.scale`; further is split into steps, each turning and checking its cell on its own. */
|
|
@@ -20,8 +20,13 @@ const PROBE_SUB = 2;
|
|
|
20
20
|
const TAIL_SLACK = 2;
|
|
21
21
|
/** The members are tallied every this many steps (a power of two). */
|
|
22
22
|
const TALLY = 8;
|
|
23
|
-
/**
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* The least share of its pace the front rank keeps while it waits, near the goal, for the ranks behind to close up:
|
|
25
|
+
* it does not creep up to the goal for the sake of the last of a gap.
|
|
26
|
+
*/
|
|
27
|
+
const CLOSE_MIN = 0.4;
|
|
28
|
+
/** Scratch: the furthest each rank may move along the trail this step, and the furthest its outermost place may. */
|
|
29
|
+
let FAR = new Float64Array(64), OUT = new Float64Array(64);
|
|
25
30
|
/**
|
|
26
31
|
* Set a group marching. The march is appended to `marches`, or reuses the first one in it that is no longer alive
|
|
27
32
|
* (so its `id` is its index there). Whether a group is compact enough to march together is the game's call, before
|
|
@@ -33,13 +38,14 @@ export function createMarch(marches, gr, M, order) {
|
|
|
33
38
|
let g = marches.find(m => !m.alive);
|
|
34
39
|
if (!g) {
|
|
35
40
|
const e = new Float64Array(0);
|
|
36
|
-
g = { id: marches.length, trail: e, rowA: new Float32Array(0), rowW: e, rowD: e, rowSp: e, rowRad: e, rowRun: e, rowB: e, rowLen: e, rowE: e,
|
|
41
|
+
g = { id: marches.length, trail: e, rowA: new Float32Array(0), rowW: e, rowD: e, rowSp: e, rowRad: e, rowRun: e, rowB: e, rowLen: e, rowE: e, rowV: e, rowGap: e };
|
|
37
42
|
marches.push(g);
|
|
38
43
|
}
|
|
39
44
|
const width = f.width, depth = f.depth, rows = f.rowW.length;
|
|
40
45
|
const ahead = width / 2 / M.narrowIn + M.probePad;
|
|
41
|
-
// The trail must hold the column stretched to `stretchMax` times its depth
|
|
42
|
-
|
|
46
|
+
// The trail must hold the column stretched to `stretchMax` times its depth and strung out by `slack` times its
|
|
47
|
+
// depth, plus the probe's lead
|
|
48
|
+
const stretched = depth * (M.stretchMax + M.slack);
|
|
43
49
|
const cap = Math.min(TRAIL_MAX, Math.ceil((stretched + ahead) / M.trailStep) + 8);
|
|
44
50
|
if (g.trail.length < cap * TRAIL)
|
|
45
51
|
g.trail = new Float64Array(cap * TRAIL);
|
|
@@ -53,13 +59,12 @@ export function createMarch(marches, gr, M, order) {
|
|
|
53
59
|
g.rowB = new Float64Array(rows);
|
|
54
60
|
g.rowLen = new Float64Array(rows);
|
|
55
61
|
g.rowE = new Float64Array(rows);
|
|
56
|
-
g.
|
|
62
|
+
g.rowV = new Float64Array(rows);
|
|
63
|
+
g.rowGap = new Float64Array(rows);
|
|
57
64
|
}
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
TE = new Float64Array(rows);
|
|
62
|
-
TW = new Float64Array(rows);
|
|
65
|
+
if (FAR.length < rows) {
|
|
66
|
+
FAR = new Float64Array(rows);
|
|
67
|
+
OUT = new Float64Array(rows);
|
|
63
68
|
}
|
|
64
69
|
// Rounded to single precision first, like offsets a game keeps per member in a Float32Array: in the open each rank
|
|
65
70
|
// then stands bit for bit where its members' own offsets put them
|
|
@@ -139,8 +144,9 @@ function anchor(gr, g, M, x, y, front) {
|
|
|
139
144
|
if (along)
|
|
140
145
|
g.lead = g.lead > along ? g.lead - along : 0;
|
|
141
146
|
placeFront(g, M);
|
|
142
|
-
|
|
143
|
-
g.
|
|
147
|
+
g.rowGap.fill(0);
|
|
148
|
+
g.rowV.fill(0);
|
|
149
|
+
rowsAt(g, M, g.lead);
|
|
144
150
|
}
|
|
145
151
|
/**
|
|
146
152
|
* Turn a marching group onto a new route (the same members ordered elsewhere) without forming up again: the column
|
|
@@ -175,10 +181,10 @@ export function rerouteMarch(gr, g, M, destX, destY, destCell, hold, fx, fy) {
|
|
|
175
181
|
return false;
|
|
176
182
|
}
|
|
177
183
|
Object.assign(g, { field, hold, fx, fy });
|
|
178
|
-
cutAhead(g, M);
|
|
184
|
+
cutAhead(gr, g, M);
|
|
179
185
|
probeMove(gr, g, M, g.ahead, g.speed);
|
|
180
186
|
placeFront(g, M);
|
|
181
|
-
rowsAt(g, M, g.lead
|
|
187
|
+
rowsAt(g, M, g.lead);
|
|
182
188
|
return true;
|
|
183
189
|
}
|
|
184
190
|
/** The front rank's position and heading: the point of the trail `lead` behind the probe. */
|
|
@@ -190,17 +196,17 @@ function placeFront(g, M) {
|
|
|
190
196
|
g.hy = TP.hy;
|
|
191
197
|
}
|
|
192
198
|
const AIM = { x: 0, y: 0 };
|
|
193
|
-
/** The width of the way at the probe as far as it is known: the
|
|
199
|
+
/** The width of the way at the probe as far as it is known: the width across the route at the newest trail point where that is less than the column's. The probe keeps to what was measured across its route; the ranks behind use the room along their own lines. */
|
|
194
200
|
function wayAt(g, M) {
|
|
195
|
-
return Math.min(g.width, widthAt(g, M, 0));
|
|
201
|
+
return Math.min(g.width, widthAt(g, M, 0, true));
|
|
196
202
|
}
|
|
197
203
|
/**
|
|
198
204
|
* Count cells along the flow field from under the probe and take the furthest one a straight walk reaches (no
|
|
199
205
|
* further than the look-ahead distance), into `AIM`. A flow field has eight directions; walked as it is, the route
|
|
200
|
-
* is a polyline, and a rank following it would wheel at every kink. The furthest cell
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* @param laid False while forming up, before the trail is laid out: no
|
|
206
|
+
* is a polyline, and a rank following it would wheel at every kink. The furthest cell with a straight walk clear for
|
|
207
|
+
* a quarter of the width across the route to each side is taken when there is one, so the straightened route keeps
|
|
208
|
+
* its distance from the corners it goes round.
|
|
209
|
+
* @param laid False while forming up, before the trail is laid out: no width across the route is known yet.
|
|
204
210
|
*/
|
|
205
211
|
function lookAhead(gr, g, M, f, cell, x = g.px, y = g.py, laid = true) {
|
|
206
212
|
const mw = gr.w, T = gr.cellSize;
|
|
@@ -409,41 +415,61 @@ function advance(gr, g, M, nx, ny, measure) {
|
|
|
409
415
|
}
|
|
410
416
|
/**
|
|
411
417
|
* The front rank's top speed (before ground, weather and waiting): the group's marching speed, or its full speed once
|
|
412
|
-
* most members act on their own.
|
|
413
|
-
*
|
|
414
|
-
* within `trot` of
|
|
415
|
-
*
|
|
418
|
+
* most members act on their own. With ranks folded, the column trots through at the folded ranks' own paces (`trot` of
|
|
419
|
+
* each one's full speed, the more folded a rank the more it counts), no faster than the front rank's own.
|
|
420
|
+
* On a bend the front rank's outermost place must keep within `trot` of the rank's full speed. And the further the
|
|
421
|
+
* column has strung out behind (ranks that could not keep up with the rank ahead of them), the slower the front rank
|
|
422
|
+
* goes, so the ranks behind close up again: in proportion, down to a halt once the gaps add up to `slack` times the
|
|
423
|
+
* column's depth; and once the probe is at the goal, slow enough to arrive there with the ranks closed up.
|
|
416
424
|
*/
|
|
417
425
|
function paceTop(g, M) {
|
|
418
|
-
|
|
419
|
-
let fold = 1, foldRun = Infinity;
|
|
426
|
+
let fold = 1, weight = 0, paces = 0, gaps = 0;
|
|
420
427
|
for (let r = 0; r < g.rows; r++) {
|
|
421
|
-
g.rowK[r] = 1 + g.rowE[r] / M.scale - TE[r] / M.scale;
|
|
422
428
|
const fr = g.rowLen[r] / g.rowD[r];
|
|
423
|
-
if (fr > 1
|
|
424
|
-
|
|
429
|
+
if (fr > 1) {
|
|
430
|
+
const w = smooth(fr - 1);
|
|
431
|
+
weight += w;
|
|
432
|
+
paces += w * rankPace(g, M, r);
|
|
433
|
+
}
|
|
425
434
|
if (fr > fold)
|
|
426
435
|
fold = fr;
|
|
436
|
+
gaps += g.rowGap[r];
|
|
427
437
|
}
|
|
428
438
|
let top = g.free > 0.5 ? g.run : g.speed;
|
|
429
|
-
if (
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
if (
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
439
|
+
if (weight > 0) {
|
|
440
|
+
// The folded ranks' own paces, each counting by how far it is folded; no faster than the front rank's own
|
|
441
|
+
const first = rankPace(g, M, 0), pace = paces / weight < first ? paces / weight : first;
|
|
442
|
+
if (pace > top)
|
|
443
|
+
top += (pace - top) * smooth(fold - 1);
|
|
444
|
+
}
|
|
445
|
+
// Not within one width of the goal: there the probe turned as sharply as it took to reach the goal, and the members
|
|
446
|
+
// are about to leave for their own spots
|
|
447
|
+
if (!(g.pDone && g.lead < g.width)) {
|
|
448
|
+
const wheel = frontWheel(g, M, g.lead);
|
|
449
|
+
if (wheel > 1) {
|
|
450
|
+
const lim = rankPace(g, M, 0) / wheel;
|
|
451
|
+
if (lim < top)
|
|
452
|
+
top = lim;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
const room = M.slack * g.depth;
|
|
456
|
+
if (gaps >= room)
|
|
457
|
+
return 0;
|
|
458
|
+
top *= 1 - gaps / room;
|
|
459
|
+
// With the probe at the goal, the front rank takes as long to get there as a rank at its own pace takes to cover
|
|
460
|
+
// that and the gaps as well: it arrives with the ranks closed up (or nearly: see `CLOSE_MIN`), not ahead of ranks
|
|
461
|
+
// that would then have to find their own way
|
|
462
|
+
if (g.pDone && !g.tail && gaps > 0) {
|
|
463
|
+
const f = g.lead / (g.lead + gaps);
|
|
464
|
+
top *= f > CLOSE_MIN ? f : CLOSE_MIN;
|
|
444
465
|
}
|
|
445
466
|
return top;
|
|
446
467
|
}
|
|
468
|
+
/** The fastest rank r is asked to move: `trot` of the rank's full speed, or the marching speed where that is higher. */
|
|
469
|
+
function rankPace(g, M, r) {
|
|
470
|
+
const rt = g.rowRun[r] * M.trot;
|
|
471
|
+
return rt > g.speed ? rt : g.speed;
|
|
472
|
+
}
|
|
447
473
|
/** Whether the column must wind up when the front rank arrives: the nearest narrow place is still at the last rank's rear or behind it, so some ranks have not passed it. */
|
|
448
474
|
function needTail(g, M) {
|
|
449
475
|
const last = g.rows - 1;
|
|
@@ -571,8 +597,12 @@ export function updateMarches(marches, grounds, members, M, dt, tick) {
|
|
|
571
597
|
const mult = g.tail ? 1 : g.hold && g.engaged > M.holdEngaged ? 0 : k < 0 ? 0 : k > 1 ? 1 : k;
|
|
572
598
|
const sp = paceTop(g, M) * frontGround(gr, g, M) * gr.speedScale * mult;
|
|
573
599
|
g.v = sp;
|
|
574
|
-
|
|
600
|
+
// Waiting for its members (or held): the ranks stand as they are. A front rank halted only because the column
|
|
601
|
+
// has strung out goes through the step all the same, so that the ranks behind close up
|
|
602
|
+
if (mult <= 0 || gr.speedScale <= 0) {
|
|
603
|
+
g.rowV.fill(0);
|
|
575
604
|
continue;
|
|
605
|
+
}
|
|
576
606
|
const step = sp * dt;
|
|
577
607
|
if (g.pDone && !g.tail && g.lead <= step + M.scale) {
|
|
578
608
|
if (gr.probeBlocked?.(g.x, g.y, g.destX, g.destY)) {
|
|
@@ -601,6 +631,7 @@ export function updateMarches(marches, grounds, members, M, dt, tick) {
|
|
|
601
631
|
g.lead -= df;
|
|
602
632
|
if (df < full)
|
|
603
633
|
g.v = df / (dt * wf);
|
|
634
|
+
const from = g.lead;
|
|
604
635
|
// The probe walks as far as the front rank did, and a little more while it is short of its lead (it was held up,
|
|
605
636
|
// or the route has only just been found)
|
|
606
637
|
const want = g.ahead - g.lead;
|
|
@@ -617,12 +648,24 @@ export function updateMarches(marches, grounds, members, M, dt, tick) {
|
|
|
617
648
|
placeFront(g, M);
|
|
618
649
|
if (!g.tail && df > 0) {
|
|
619
650
|
// A gate narrower than the trail sampling interval can close behind the probe.
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
anchor(gr, g, M, g.x, g.y, true);
|
|
651
|
+
const shut = gr.probeBlocked?.(oldX, oldY, g.x, g.y);
|
|
652
|
+
if (shut || routeCut(gr, g, M)) {
|
|
653
|
+
// Formed up afresh, from where the front rank was when its own step is shut
|
|
654
|
+
anchor(gr, g, M, shut ? oldX : g.x, shut ? oldY : g.y, true);
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
// Each rank moves on at its own pace: the ranks are not rigidly coupled. Along the trail a rank keeps within
|
|
659
|
+
// `trot` of its full speed, which leaves its members room to catch up with their places; its outermost place on a
|
|
660
|
+
// bend may be asked for the full speed. Only the front rank's pace follows the ground under it: behind it, a
|
|
661
|
+
// rank whose members are slowed by theirs shows in the lag, which slows the whole column
|
|
662
|
+
const rate = gr.speedScale * mult * wf * dt;
|
|
663
|
+
for (let r = 0; r < g.rows; r++) {
|
|
664
|
+
FAR[r] = rankPace(g, M, r) * rate;
|
|
665
|
+
OUT[r] = (g.rowRun[r] > g.speed ? g.rowRun[r] : g.speed) * rate;
|
|
624
666
|
}
|
|
625
|
-
|
|
667
|
+
// The ranks' speeds leave the heading factor out, as the front rank's does
|
|
668
|
+
rowsStep(g, M, g.lead, g.lead - from, dt * wf, FAR, OUT);
|
|
626
669
|
}
|
|
627
670
|
}
|
|
628
671
|
/**
|
package/dist/marchChoke.d.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Column march: measuring a narrow place. At a point of the trail, find the shortest cross-section with a large
|
|
3
|
-
* obstacle on both sides, each blocking its whole wing, and give the
|
|
4
|
-
* the cross-section. A place with an obstacle on one side only, or one that leaves the wing tip free
|
|
5
|
-
* pass round it), is not a narrow place. A long wall blocks its wing however thin it is, and
|
|
6
|
-
*
|
|
7
|
-
* clear of it.
|
|
3
|
+
* obstacle on both sides, each blocking its whole wing, and give the width across the route on each side and the
|
|
4
|
+
* direction of the cross-section. A place with an obstacle on one side only, or one that leaves the wing tip free
|
|
5
|
+
* (the wings can pass round it), is not a narrow place. A long wall blocks its wing however thin it is, and where no
|
|
6
|
+
* narrow place is found, what stands beside the route is still recorded, side by side, so that ranks keep clear of it.
|
|
8
7
|
*/
|
|
9
8
|
import type { March, MarchGround, MarchRules } from './marchTypes.js';
|
|
10
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Result of `chokeAt`: width across the route on each side (R on the right of travel), the cross-section's direction
|
|
11
|
+
* (pointing right of travel), whether a narrow place was found (1 or 0), and whether the ground gave the
|
|
12
|
+
* cross-section itself (1 or 0).
|
|
13
|
+
*/
|
|
11
14
|
export declare const CHOKE: {
|
|
12
15
|
L: number;
|
|
13
16
|
R: number;
|
|
14
17
|
ax: number;
|
|
15
18
|
ay: number;
|
|
16
19
|
has: number;
|
|
20
|
+
own: number;
|
|
17
21
|
};
|
|
18
22
|
/** Set `CHOKE` to "no narrow place": both sides as wide as where nothing was measured. */
|
|
19
23
|
export declare function chokeClear(g: March): void;
|
|
@@ -25,3 +29,10 @@ export declare function chokeClear(g: March): void;
|
|
|
25
29
|
* and at a narrow place add up to less than `narrow` times the column's width.
|
|
26
30
|
*/
|
|
27
31
|
export declare function chokeAt(gr: MarchGround, g: March, M: MarchRules, x: number, y: number, tx: number, ty: number): void;
|
|
32
|
+
/**
|
|
33
|
+
* The room from (x, y) along the unit vector (dx, dy): to the first solid cell within the column's width, less the
|
|
34
|
+
* clearance `chokePad`; as wide as where nothing was measured when nothing stands that way.
|
|
35
|
+
*/
|
|
36
|
+
export declare function roomAlong(gr: MarchGround, g: March, M: MarchRules, x: number, y: number, dx: number, dy: number): number;
|
|
37
|
+
/** Whether (x, y) is on a solid cell of the grid (or off the map). */
|
|
38
|
+
export declare function onSolid(gr: MarchGround, x: number, y: number): boolean;
|
package/dist/marchChoke.js
CHANGED
|
@@ -2,12 +2,16 @@
|
|
|
2
2
|
const AXES = [1, 0, Math.SQRT1_2, Math.SQRT1_2, 0, 1, -Math.SQRT1_2, Math.SQRT1_2];
|
|
3
3
|
/** A direction whose cosine with the direction of travel exceeds this is not used: it runs along the route, not across it. */
|
|
4
4
|
const AXIS_PAR = 0.8;
|
|
5
|
-
/**
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Result of `chokeAt`: width across the route on each side (R on the right of travel), the cross-section's direction
|
|
7
|
+
* (pointing right of travel), whether a narrow place was found (1 or 0), and whether the ground gave the
|
|
8
|
+
* cross-section itself (1 or 0).
|
|
9
|
+
*/
|
|
10
|
+
export const CHOKE = { L: 0, R: 0, ax: 0, ay: 0, has: 0, own: 0 };
|
|
7
11
|
/** Set `CHOKE` to "no narrow place": both sides as wide as where nothing was measured. */
|
|
8
12
|
export function chokeClear(g) {
|
|
9
13
|
CHOKE.L = CHOKE.R = g.open;
|
|
10
|
-
CHOKE.ax = CHOKE.ay = CHOKE.has = 0;
|
|
14
|
+
CHOKE.ax = CHOKE.ay = CHOKE.has = CHOKE.own = 0;
|
|
11
15
|
}
|
|
12
16
|
/** Nobody can stand in cell (cx, cy): solid, or off the map. */
|
|
13
17
|
function solidCell(gr, cx, cy) {
|
|
@@ -108,6 +112,7 @@ export function chokeAt(gr, g, M, x, y, tx, ty) {
|
|
|
108
112
|
chokeClear(g);
|
|
109
113
|
const section = gr.crossSection?.(x, y, tx, ty);
|
|
110
114
|
if (section) {
|
|
115
|
+
CHOKE.own = 1;
|
|
111
116
|
const { axisX, axisY, positive, negative } = section;
|
|
112
117
|
const forward = -ty * axisX + tx * axisY >= 0;
|
|
113
118
|
const r = forward ? positive : negative, l = forward ? negative : positive;
|
|
@@ -123,6 +128,11 @@ export function chokeAt(gr, g, M, x, y, tx, ty) {
|
|
|
123
128
|
CHOKE.has = 1;
|
|
124
129
|
return;
|
|
125
130
|
}
|
|
131
|
+
// A point on a solid cell measures nothing (a ray from it reads 0 both ways, which would pass for a narrow place of
|
|
132
|
+
// no width in whichever direction is tried first). It gets there where the ground's own queries let the probe
|
|
133
|
+
// through part of a cell that is solid on the grid; the points before and after it carry the width
|
|
134
|
+
if (onSolid(gr, x, y))
|
|
135
|
+
return;
|
|
126
136
|
let best = g.width * M.narrow;
|
|
127
137
|
for (let a = 0; a < AXES.length; a += 2) {
|
|
128
138
|
const ux = AXES[a], uy = AXES[a + 1];
|
|
@@ -144,8 +154,7 @@ export function chokeAt(gr, g, M, x, y, tx, ty) {
|
|
|
144
154
|
CHOKE.ay = dy;
|
|
145
155
|
CHOKE.has = 1;
|
|
146
156
|
}
|
|
147
|
-
|
|
148
|
-
if (CHOKE.has || solidCell(gr, Math.floor(x / gr.cellSize), Math.floor(y / gr.cellSize)))
|
|
157
|
+
if (CHOKE.has)
|
|
149
158
|
return;
|
|
150
159
|
// Without a narrow place, whatever stands within the column's width square to the travel
|
|
151
160
|
// still counts on its own side: no direction of a narrow place goes with it, so ranks stay square to their heading
|
|
@@ -155,3 +164,15 @@ export function chokeAt(gr, g, M, x, y, tx, ty) {
|
|
|
155
164
|
if (dl >= 0)
|
|
156
165
|
CHOKE.L = Math.max(0, dl - M.chokePad);
|
|
157
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* The room from (x, y) along the unit vector (dx, dy): to the first solid cell within the column's width, less the
|
|
169
|
+
* clearance `chokePad`; as wide as where nothing was measured when nothing stands that way.
|
|
170
|
+
*/
|
|
171
|
+
export function roomAlong(gr, g, M, x, y, dx, dy) {
|
|
172
|
+
const d = rayHit(gr, x, y, dx, dy, g.width);
|
|
173
|
+
return d < 0 ? g.open : Math.max(0, d - M.chokePad);
|
|
174
|
+
}
|
|
175
|
+
/** Whether (x, y) is on a solid cell of the grid (or off the map). */
|
|
176
|
+
export function onSolid(gr, x, y) {
|
|
177
|
+
return solidCell(gr, Math.floor(x / gr.cellSize), Math.floor(y / gr.cellSize));
|
|
178
|
+
}
|
package/dist/marchSlots.d.ts
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import type { March, MarchGround, MarchRules } from './marchTypes.js';
|
|
2
2
|
/**
|
|
3
|
-
* Where each rank stands with the front rank `b` behind the probe
|
|
4
|
-
* taking the stretch `rowLength` gives
|
|
5
|
-
*
|
|
6
|
-
* the rank's outermost place moves than its point on the trail, averaged over its stretch).
|
|
3
|
+
* Where each rank stands with the front rank `b` behind the probe, into the march (`rowB`, `rowLen`, `rowE`): from
|
|
4
|
+
* the front rank back, one after another, each taking the stretch `rowLength` gives, as far behind the rank ahead of
|
|
5
|
+
* it as it has fallen (`rowGap`).
|
|
7
6
|
*/
|
|
8
|
-
export declare function rowsAt(g: March, M: MarchRules, b: number
|
|
7
|
+
export declare function rowsAt(g: March, M: MarchRules, b: number): void;
|
|
8
|
+
/** How much further the front rank's outermost place moves than its point on the trail, with the rank `b` behind the probe (its wheel factor, averaged over its stretch). */
|
|
9
|
+
export declare function frontWheel(g: March, M: MarchRules, b: number): number;
|
|
10
|
+
/**
|
|
11
|
+
* Move the ranks on by one step, the front rank now `b` behind the probe, which walked `adv` this step; `dt` is what
|
|
12
|
+
* the distance a rank moved is divided by for its speed (the step's seconds, times any factor the speeds leave out).
|
|
13
|
+
* Each rank closes up behind the one ahead of it when it can get there this step: `far[r]` is the furthest its point
|
|
14
|
+
* on the trail may move, and `out[r]` the furthest its outermost place may (which on a bend moves further than the
|
|
15
|
+
* point on the trail). A rank that cannot get there (the ranks ahead spread out of a narrow place it is still in, or
|
|
16
|
+
* it is slower) moves as far as it may and falls behind (`rowGap`), and the ranks behind it follow it, not the ranks
|
|
17
|
+
* ahead. Later it closes the gap at the same pace. The ranks' places and speeds go to the march (`rowB`, `rowLen`,
|
|
18
|
+
* `rowE`, `rowV`).
|
|
19
|
+
*/
|
|
20
|
+
export declare function rowsStep(g: March, M: MarchRules, b: number, adv: number, dt: number, far: Float64Array, out: Float64Array): void;
|
|
9
21
|
/**
|
|
10
22
|
* Result of `marchSlot`: the place and the direction of travel there, the point of the rank's middle line (same short
|
|
11
23
|
* rank, lateral offset 0), the place's distance behind the probe along the trail, its lateral offset from the middle
|
|
@@ -25,7 +37,7 @@ export declare const SLOT: {
|
|
|
25
37
|
};
|
|
26
38
|
/**
|
|
27
39
|
* The place right now of the member in rank r, place c of the rank, lateral offset l (positive to the right of the
|
|
28
|
-
* facing); the result goes to `SLOT`. The rank's stretch of trail
|
|
40
|
+
* facing); the result goes to `SLOT`. The rank's stretch of trail is kept in the march. Folding
|
|
29
41
|
* into short ranks: squeezed to half its width, every other member drops back half a stretch and then closes up
|
|
30
42
|
* behind its neighbour; at a quarter, it folds again, and so on. Each fold first staggers members front to back and
|
|
31
43
|
* only then closes them up sideways, so no two collide on the way. Sideways the rank closes up in proportion to the
|
package/dist/marchSlots.js
CHANGED
|
@@ -84,24 +84,80 @@ function rowLength(g, M, r, edge) {
|
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
/**
|
|
87
|
-
* Where each rank stands with the front rank `b` behind the probe
|
|
88
|
-
* taking the stretch `rowLength` gives
|
|
89
|
-
*
|
|
90
|
-
* the rank's outermost place moves than its point on the trail, averaged over its stretch).
|
|
87
|
+
* Where each rank stands with the front rank `b` behind the probe, into the march (`rowB`, `rowLen`, `rowE`): from
|
|
88
|
+
* the front rank back, one after another, each taking the stretch `rowLength` gives, as far behind the rank ahead of
|
|
89
|
+
* it as it has fallen (`rowGap`).
|
|
91
90
|
*/
|
|
92
|
-
export function rowsAt(g, M, b
|
|
91
|
+
export function rowsAt(g, M, b) {
|
|
93
92
|
let extra = 0;
|
|
94
93
|
for (let r = 0; r < g.rows; r++) {
|
|
95
94
|
const d = g.rowD[r];
|
|
95
|
+
extra += g.rowGap[r];
|
|
96
96
|
// The rank's middle when unfolded
|
|
97
97
|
const c = b - g.rowA[r] + extra;
|
|
98
98
|
const len = rowLength(g, M, r, c - d / 2);
|
|
99
99
|
const e = extra + (len - d) / 2;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
100
|
+
g.rowB[r] = b - g.rowA[r] + e;
|
|
101
|
+
g.rowLen[r] = len;
|
|
102
|
+
g.rowE[r] = e;
|
|
103
|
+
extra += len - d;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** How much further the front rank's outermost place moves than its point on the trail, with the rank `b` behind the probe (its wheel factor, averaged over its stretch). */
|
|
107
|
+
export function frontWheel(g, M, b) {
|
|
108
|
+
rowLength(g, M, 0, b - g.rowA[0] - g.rowD[0] / 2);
|
|
109
|
+
return WHEEL;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Move the ranks on by one step, the front rank now `b` behind the probe, which walked `adv` this step; `dt` is what
|
|
113
|
+
* the distance a rank moved is divided by for its speed (the step's seconds, times any factor the speeds leave out).
|
|
114
|
+
* Each rank closes up behind the one ahead of it when it can get there this step: `far[r]` is the furthest its point
|
|
115
|
+
* on the trail may move, and `out[r]` the furthest its outermost place may (which on a bend moves further than the
|
|
116
|
+
* point on the trail). A rank that cannot get there (the ranks ahead spread out of a narrow place it is still in, or
|
|
117
|
+
* it is slower) moves as far as it may and falls behind (`rowGap`), and the ranks behind it follow it, not the ranks
|
|
118
|
+
* ahead. Later it closes the gap at the same pace. The ranks' places and speeds go to the march (`rowB`, `rowLen`,
|
|
119
|
+
* `rowE`, `rowV`).
|
|
120
|
+
*/
|
|
121
|
+
export function rowsStep(g, M, b, adv, dt, far, out) {
|
|
122
|
+
const per = dt > 0 ? 1 / dt : 0;
|
|
123
|
+
let extra = 0;
|
|
124
|
+
for (let r = 0; r < g.rows; r++) {
|
|
125
|
+
const d = g.rowD[r];
|
|
126
|
+
// Where the rank stood, measured from where the probe is now
|
|
127
|
+
const wasB = g.rowB[r] + adv, wasEdge = wasB - g.rowLen[r] / 2;
|
|
128
|
+
// Closed up behind the rank ahead
|
|
129
|
+
const edge = b - g.rowA[r] + extra - d / 2;
|
|
130
|
+
let len = rowLength(g, M, r, edge), gap = 0;
|
|
131
|
+
const need = wasB - (edge + len / 2);
|
|
132
|
+
let can = far[r];
|
|
133
|
+
if (r > 0 && need > can) {
|
|
134
|
+
// On a bend its outermost place moves further than its point on the trail, and that is what must keep within
|
|
135
|
+
// its pace: where it stands, not where it would stand closed up. Not while it is folded: there the turning of
|
|
136
|
+
// its cross-section is the narrow place taking it in, which is over in a moment
|
|
137
|
+
const fold = g.rowLen[r] / d;
|
|
138
|
+
if (fold < 2) {
|
|
139
|
+
rowLength(g, M, r, wasEdge);
|
|
140
|
+
const lim = out[r] / (fold > 1 ? 1 + (WHEEL - 1) * (1 - smooth(fold - 1)) : WHEEL);
|
|
141
|
+
if (lim < can)
|
|
142
|
+
can = lim;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (r > 0 && need > can) {
|
|
146
|
+
// Its front edge goes the same share of its way as its middle may go of its own. Where the rank is also
|
|
147
|
+
// growing shorter (it is coming out of a narrow place) that still lets the middle move a little further
|
|
148
|
+
gap = wasEdge + (edge - wasEdge) * (can / need) - edge;
|
|
149
|
+
if (gap > 0)
|
|
150
|
+
len = rowLength(g, M, r, edge + gap);
|
|
151
|
+
else
|
|
152
|
+
gap = 0;
|
|
153
|
+
}
|
|
154
|
+
extra += gap;
|
|
155
|
+
const e = extra + (len - d) / 2;
|
|
156
|
+
g.rowB[r] = b - g.rowA[r] + e;
|
|
157
|
+
g.rowLen[r] = len;
|
|
158
|
+
g.rowE[r] = e;
|
|
159
|
+
g.rowGap[r] = gap;
|
|
160
|
+
g.rowV[r] = (wasB - g.rowB[r]) * per;
|
|
105
161
|
extra += len - d;
|
|
106
162
|
}
|
|
107
163
|
}
|
|
@@ -113,7 +169,7 @@ export function rowsAt(g, M, b, rb, rl, re, wh) {
|
|
|
113
169
|
export const SLOT = { x: 0, y: 0, hx: 0, hy: 0, cx: 0, cy: 0, b: 0, l: 0, half: 1, v: 0 };
|
|
114
170
|
/**
|
|
115
171
|
* The place right now of the member in rank r, place c of the rank, lateral offset l (positive to the right of the
|
|
116
|
-
* facing); the result goes to `SLOT`. The rank's stretch of trail
|
|
172
|
+
* facing); the result goes to `SLOT`. The rank's stretch of trail is kept in the march. Folding
|
|
117
173
|
* into short ranks: squeezed to half its width, every other member drops back half a stretch and then closes up
|
|
118
174
|
* behind its neighbour; at a quarter, it folds again, and so on. Each fold first staggers members front to back and
|
|
119
175
|
* only then closes them up sideways, so no two collide on the way. Sideways the rank closes up in proportion to the
|
|
@@ -152,7 +208,7 @@ export function marchSlot(g, M, r, c, l) {
|
|
|
152
208
|
// Half width there: half the column's width in the open, half the usable width where that is less
|
|
153
209
|
const room = L + R < g.width ? L + R : g.width;
|
|
154
210
|
SLOT.half = Math.max(room / 2, sp / 2);
|
|
155
|
-
SLOT.v = g.
|
|
211
|
+
SLOT.v = g.rowV[r];
|
|
156
212
|
}
|
|
157
213
|
/** Result of `marchFollow`: the point to head for this step. */
|
|
158
214
|
export const FOLLOW = { x: 0, y: 0 };
|
package/dist/marchTrail.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { March, MarchGround, MarchRules } from './marchTypes.js';
|
|
2
2
|
/**
|
|
3
|
-
* Numbers per trail point: position, heading,
|
|
4
|
-
* narrow place and where it comes from (0 none, 1 measured here, 2 carried over from a narrow place ahead or
|
|
5
|
-
* direction of the cross-section, the cell the probe stood in and found passable when it recorded the point
|
|
6
|
-
* laid out when forming up, or coasting while winding up),
|
|
7
|
-
* the envelope on a new route)
|
|
3
|
+
* Numbers per trail point: position, heading, width left and right across the route (after the envelope), direction
|
|
4
|
+
* of the narrow place and where it comes from (0 none, 1 measured here, 2 carried over from a narrow place ahead or
|
|
5
|
+
* behind), direction of the cross-section, the cell the probe stood in and found passable when it recorded the point
|
|
6
|
+
* (−1 none: laid out when forming up, or coasting while winding up), the widths measured at the point itself (for
|
|
7
|
+
* redoing the envelope on a new route), the room left and right along the cross-section, the usable width left and
|
|
8
|
+
* right (that room after its own envelope), and whether the ground gave the cross-section there itself.
|
|
8
9
|
*/
|
|
9
|
-
export declare const TRAIL =
|
|
10
|
+
export declare const TRAIL = 19;
|
|
10
11
|
/** The most trail points kept: enough for the whole column at its longest. */
|
|
11
12
|
export declare const TRAIL_MAX = 2048;
|
|
12
13
|
/** Result of `trailAt`: a point of the trail, its heading, usable width left and right, and the direction of the cross-section (pointing right of travel). */
|
|
@@ -25,8 +26,11 @@ export declare const TP: {
|
|
|
25
26
|
* a point further back than the trail is on the line through the oldest point, along its heading.
|
|
26
27
|
*/
|
|
27
28
|
export declare function trailAt(g: March, M: MarchRules, b: number): void;
|
|
28
|
-
/**
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* The usable width (left plus right) of the trail `b` behind the probe, along the cross-section there; with `across`,
|
|
31
|
+
* the width across the route instead (what was measured across it, after the envelope).
|
|
32
|
+
*/
|
|
33
|
+
export declare function widthAt(g: March, M: MarchRules, b: number, across?: boolean): number;
|
|
30
34
|
/** Lay the whole trail out straight behind (x, y) against heading (hx, hy), as wide as where nothing was measured (when forming up; (x, y) is point 0). */
|
|
31
35
|
export declare function layTrail(g: March, M: MarchRules, x: number, y: number, hx: number, hy: number): void;
|
|
32
36
|
/**
|
|
@@ -41,7 +45,7 @@ export declare function addPoint(gr: MarchGround, g: March, M: MarchRules, x: nu
|
|
|
41
45
|
* heading. The points kept get their envelope redone from the widths measured at each (the narrowing first passed
|
|
42
46
|
* backwards, then limited forwards at the widening rate), narrow-place directions carried along as before.
|
|
43
47
|
*/
|
|
44
|
-
export declare function cutAhead(g: March, M: MarchRules): void;
|
|
48
|
+
export declare function cutAhead(gr: MarchGround, g: March, M: MarchRules): void;
|
|
45
49
|
/** The cell the probe walked through, as recorded at the trail point nearest the front rank; `fallback` when there is none. */
|
|
46
50
|
export declare function nearCell(g: March, M: MarchRules, fallback: number): number;
|
|
47
51
|
/** Whether the cell recorded at the trail point just ahead of the front rank has become solid: the route the probe found has been closed since. */
|
package/dist/marchTrail.js
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Column march: the trail. Where the probe has walked is recorded as a point every `trailStep`, in a ring buffer
|
|
3
|
-
* (`head` is the newest point, `rec` away from the probe).
|
|
4
|
-
*
|
|
5
|
-
* measured along it.
|
|
3
|
+
* (`head` is the newest point, `rec` away from the probe). The ranks stand along the trail, each at a distance behind
|
|
4
|
+
* the probe measured along it. Each point carries two kinds of width:
|
|
6
5
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* place it comes from goes with it.
|
|
6
|
+
* - The width across the route on each side, as measured there, with the direction of the narrow place. Its envelope
|
|
7
|
+
* narrows gradually ahead of a narrow place (`narrowIn` per unit of travel) and widens gradually behind it
|
|
8
|
+
* (`narrowOut`); wherever a narrowing reaches, the direction of the narrow place it comes from goes with it. The
|
|
9
|
+
* probe goes by these widths, and they set the direction of the cross-section.
|
|
10
|
+
* - The usable width on each side: the room along the cross-section itself, with an envelope of its own at the same
|
|
11
|
+
* rates. The ranks go by these.
|
|
10
12
|
*/
|
|
11
13
|
import { dmath, smooth } from '@pocketrts/determinism';
|
|
12
|
-
import { CHOKE, chokeAt, chokeClear } from './marchChoke.js';
|
|
14
|
+
import { CHOKE, chokeAt, chokeClear, onSolid, roomAlong } from './marchChoke.js';
|
|
13
15
|
/**
|
|
14
|
-
* Numbers per trail point: position, heading,
|
|
15
|
-
* narrow place and where it comes from (0 none, 1 measured here, 2 carried over from a narrow place ahead or
|
|
16
|
-
* direction of the cross-section, the cell the probe stood in and found passable when it recorded the point
|
|
17
|
-
* laid out when forming up, or coasting while winding up),
|
|
18
|
-
* the envelope on a new route)
|
|
16
|
+
* Numbers per trail point: position, heading, width left and right across the route (after the envelope), direction
|
|
17
|
+
* of the narrow place and where it comes from (0 none, 1 measured here, 2 carried over from a narrow place ahead or
|
|
18
|
+
* behind), direction of the cross-section, the cell the probe stood in and found passable when it recorded the point
|
|
19
|
+
* (−1 none: laid out when forming up, or coasting while winding up), the widths measured at the point itself (for
|
|
20
|
+
* redoing the envelope on a new route), the room left and right along the cross-section, the usable width left and
|
|
21
|
+
* right (that room after its own envelope), and whether the ground gave the cross-section there itself.
|
|
19
22
|
*/
|
|
20
|
-
export const TRAIL =
|
|
23
|
+
export const TRAIL = 19;
|
|
21
24
|
const T_L = 4, T_R = 5, T_AX = 6, T_AY = 7, T_HAS = 8, T_LX = 9, T_LY = 10, T_CELL = 11, T_RL = 12, T_RR = 13;
|
|
25
|
+
const T_QL = 14, T_QR = 15, T_UL = 16, T_UR = 17, T_OWN = 18;
|
|
26
|
+
/**
|
|
27
|
+
* A narrow place's direction is followed fully when its cosine with the line square to the heading is at least
|
|
28
|
+
* `AXIS_ON`, and not at all below `AXIS_OFF`.
|
|
29
|
+
*/
|
|
30
|
+
const AXIS_ON = 0.45, AXIS_OFF = 0.15;
|
|
22
31
|
/** The most trail points kept: enough for the whole column at its longest. */
|
|
23
32
|
export const TRAIL_MAX = 2048;
|
|
24
33
|
/** Index in the buffer of trail point k (0 is the newest). */
|
|
@@ -46,8 +55,8 @@ export function trailAt(g, M, b) {
|
|
|
46
55
|
}
|
|
47
56
|
TP.hx = g.phx;
|
|
48
57
|
TP.hy = g.phy;
|
|
49
|
-
TP.L = t[o +
|
|
50
|
-
TP.R = t[o +
|
|
58
|
+
TP.L = t[o + T_UL];
|
|
59
|
+
TP.R = t[o + T_UR];
|
|
51
60
|
if (t[o + T_HAS]) {
|
|
52
61
|
TP.lx = t[o + T_LX];
|
|
53
62
|
TP.ly = t[o + T_LY];
|
|
@@ -66,8 +75,8 @@ export function trailAt(g, M, b) {
|
|
|
66
75
|
TP.hy = t[o + 3];
|
|
67
76
|
TP.x = t[o] - TP.hx * over;
|
|
68
77
|
TP.y = t[o + 1] - TP.hy * over;
|
|
69
|
-
TP.L = t[o +
|
|
70
|
-
TP.R = t[o +
|
|
78
|
+
TP.L = t[o + T_UL];
|
|
79
|
+
TP.R = t[o + T_UR];
|
|
71
80
|
TP.lx = t[o + T_LX];
|
|
72
81
|
TP.ly = t[o + T_LY];
|
|
73
82
|
return;
|
|
@@ -78,27 +87,30 @@ export function trailAt(g, M, b) {
|
|
|
78
87
|
TP.y = t[o + 1] + (t[o2 + 1] - t[o + 1]) * f;
|
|
79
88
|
TP.hx = t[o + 2] + (t[o2 + 2] - t[o + 2]) * f;
|
|
80
89
|
TP.hy = t[o + 3] + (t[o2 + 3] - t[o + 3]) * f;
|
|
81
|
-
TP.L = t[o +
|
|
82
|
-
TP.R = t[o +
|
|
90
|
+
TP.L = t[o + T_UL] + (t[o2 + T_UL] - t[o + T_UL]) * f;
|
|
91
|
+
TP.R = t[o + T_UR] + (t[o2 + T_UR] - t[o + T_UR]) * f;
|
|
83
92
|
TP.lx = t[o + T_LX] + (t[o2 + T_LX] - t[o + T_LX]) * f;
|
|
84
93
|
TP.ly = t[o + T_LY] + (t[o2 + T_LY] - t[o + T_LY]) * f;
|
|
85
94
|
}
|
|
86
|
-
/**
|
|
87
|
-
|
|
88
|
-
|
|
95
|
+
/**
|
|
96
|
+
* The usable width (left plus right) of the trail `b` behind the probe, along the cross-section there; with `across`,
|
|
97
|
+
* the width across the route instead (what was measured across it, after the envelope).
|
|
98
|
+
*/
|
|
99
|
+
export function widthAt(g, M, b, across = false) {
|
|
100
|
+
const t = g.trail, L = across ? T_L : T_UL, R = across ? T_R : T_UR;
|
|
89
101
|
if (b < g.rec) {
|
|
90
102
|
const o = g.head * TRAIL;
|
|
91
|
-
return t[o +
|
|
103
|
+
return t[o + L] + t[o + R];
|
|
92
104
|
}
|
|
93
105
|
const q = (b - g.rec) / M.trailStep, k = q | 0;
|
|
94
106
|
if (k >= g.cap - 1) {
|
|
95
107
|
const o = pt(g, g.cap - 1);
|
|
96
|
-
return t[o +
|
|
108
|
+
return t[o + L] + t[o + R];
|
|
97
109
|
}
|
|
98
110
|
const f = q - k;
|
|
99
111
|
const o = pt(g, k), o2 = pt(g, k + 1);
|
|
100
|
-
const w0 = t[o +
|
|
101
|
-
return w0 + (t[o2 +
|
|
112
|
+
const w0 = t[o + L] + t[o + R];
|
|
113
|
+
return w0 + (t[o2 + L] + t[o2 + R] - w0) * f;
|
|
102
114
|
}
|
|
103
115
|
/** Lay the whole trail out straight behind (x, y) against heading (hx, hy), as wide as where nothing was measured (when forming up; (x, y) is point 0). */
|
|
104
116
|
export function layTrail(g, M, x, y, hx, hy) {
|
|
@@ -109,8 +121,8 @@ export function layTrail(g, M, x, y, hx, hy) {
|
|
|
109
121
|
t[o + 1] = y - hy * st * k;
|
|
110
122
|
t[o + 2] = hx;
|
|
111
123
|
t[o + 3] = hy;
|
|
112
|
-
t[o + T_L] = t[o + T_R] = t[o + T_RL] = t[o + T_RR] = open;
|
|
113
|
-
t[o + T_AX] = t[o + T_AY] = t[o + T_HAS] = 0;
|
|
124
|
+
t[o + T_L] = t[o + T_R] = t[o + T_RL] = t[o + T_RR] = t[o + T_QL] = t[o + T_QR] = t[o + T_UL] = t[o + T_UR] = open;
|
|
125
|
+
t[o + T_AX] = t[o + T_AY] = t[o + T_HAS] = t[o + T_OWN] = 0;
|
|
114
126
|
t[o + T_LX] = -hy;
|
|
115
127
|
t[o + T_LY] = hx;
|
|
116
128
|
t[o + T_CELL] = -1;
|
|
@@ -148,42 +160,97 @@ export function addPoint(gr, g, M, x, y, cell, measure) {
|
|
|
148
160
|
chokeClear(g);
|
|
149
161
|
if (CHOKE.L + CHOKE.R < g.width * M.narrow)
|
|
150
162
|
g.narrowB = g.rec;
|
|
151
|
-
envelope(g, M, o, prev);
|
|
163
|
+
envelope(gr, g, M, o, prev);
|
|
164
|
+
usable(g, M);
|
|
152
165
|
}
|
|
153
166
|
/**
|
|
154
|
-
* The cross-section
|
|
155
|
-
* direction of the narrow place when the
|
|
156
|
-
* fully at `axisFull
|
|
167
|
+
* The cross-section at trail point o: its direction, and the room each way along it. Square to the heading in the
|
|
168
|
+
* open; it starts turning towards the direction of the narrow place when the width across the route drops below
|
|
169
|
+
* `axisFrom` of the column's width, and follows it fully at `axisFull`, as far as that direction lies across the
|
|
170
|
+
* column there (`AXIS_ON`, `AXIS_OFF`). The room is looked up on the grid along that very line, where the ranks will
|
|
171
|
+
* stand: the widths across the route are measured square to the travel or across a narrow place, which is not where
|
|
172
|
+
* a rank stands while the column turns. Where the grid does not tell (a point not walked by the probe, a point on a
|
|
173
|
+
* solid cell, a point where the ground gave the cross-section itself), the room is the width across the route.
|
|
157
174
|
*/
|
|
158
|
-
function crossAt(g, M, o) {
|
|
175
|
+
function crossAt(gr, g, M, o) {
|
|
159
176
|
const t = g.trail, hlx = -t[o + 3], hly = t[o + 2];
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
177
|
+
let lx = hlx, ly = hly;
|
|
178
|
+
if (t[o + T_HAS]) {
|
|
179
|
+
// On the same side as the line square to the heading, so the transition never flips
|
|
180
|
+
const ax = t[o + T_AX], ay = t[o + T_AY], d = ax * hlx + ay * hly, sg = d >= 0 ? 1 : -1;
|
|
181
|
+
const narrow = smooth((M.axisFrom - (t[o + T_L] + t[o + T_R]) / g.width) / (M.axisFrom - M.axisFull));
|
|
182
|
+
// A narrow place's direction may run nearly along this point's heading (carried here from a narrow place further
|
|
183
|
+
// on, measured while the heading lags the travel, or given by the ground): the nearer it is to the heading the
|
|
184
|
+
// less it is followed, and near enough not at all. Otherwise a rank would stand along the route, and end for end
|
|
185
|
+
// from the next rank where the direction passes the heading
|
|
186
|
+
const across = smooth((d * sg - AXIS_OFF) / (AXIS_ON - AXIS_OFF));
|
|
187
|
+
const k = narrow * across;
|
|
188
|
+
const x = hlx + (ax * sg - hlx) * k, y = hly + (ay * sg - hly) * k;
|
|
189
|
+
const l = dmath.hypot(x, y) || 1;
|
|
190
|
+
lx = x / l;
|
|
191
|
+
ly = y / l;
|
|
192
|
+
}
|
|
193
|
+
t[o + T_LX] = lx;
|
|
194
|
+
t[o + T_LY] = ly;
|
|
195
|
+
if (t[o + T_CELL] < 0 || t[o + T_OWN] || onSolid(gr, t[o], t[o + 1])) {
|
|
196
|
+
t[o + T_QL] = t[o + T_L];
|
|
197
|
+
t[o + T_QR] = t[o + T_R];
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
t[o + T_QL] = roomAlong(gr, g, M, t[o], t[o + 1], -lx, -ly);
|
|
201
|
+
t[o + T_QR] = roomAlong(gr, g, M, t[o], t[o + 1], lx, ly);
|
|
164
202
|
}
|
|
165
|
-
const k = smooth((M.axisFrom - (t[o + T_L] + t[o + T_R]) / g.width) / (M.axisFrom - M.axisFull));
|
|
166
|
-
// On the same side as the line square to the heading, so the transition never flips
|
|
167
|
-
const ax = t[o + T_AX], ay = t[o + T_AY], sg = ax * hlx + ay * hly >= 0 ? 1 : -1;
|
|
168
|
-
const x = hlx + (ax * sg - hlx) * k, y = hly + (ay * sg - hly) * k;
|
|
169
|
-
const l = dmath.hypot(x, y) || 1;
|
|
170
|
-
t[o + T_LX] = x / l;
|
|
171
|
-
t[o + T_LY] = y / l;
|
|
172
203
|
}
|
|
173
204
|
/**
|
|
174
|
-
*
|
|
175
|
-
*
|
|
205
|
+
* The usable widths of the whole trail: the room along each cross-section, narrowing gradually ahead of a place with
|
|
206
|
+
* less room (`narrowIn` per unit of travel) and widening gradually behind it (`narrowOut`), so that the ranks' ends
|
|
207
|
+
* do not jump where a cross-section sweeps past the end of a wall. On the inside of a turn the room counts no further
|
|
208
|
+
* than the centre of the turn (the trail's turning radius there), unless the width across the route is more: a rank
|
|
209
|
+
* end beyond the centre would walk backwards while the column wheels.
|
|
176
210
|
*/
|
|
177
|
-
function
|
|
211
|
+
function usable(g, M) {
|
|
212
|
+
const t = g.trail, cap = g.cap, si = M.narrowIn * M.trailStep, so = M.narrowOut * M.trailStep;
|
|
213
|
+
for (let n = 0; n < cap; n++) {
|
|
214
|
+
const o = pt(g, n), a = pt(g, n < cap - 1 ? n + 1 : n), b = pt(g, n > 0 ? n - 1 : n);
|
|
215
|
+
let ql = t[o + T_QL], qr = t[o + T_QR];
|
|
216
|
+
// Turning: positive towards the right. The sine stands in for the angle between the two headings, and a turn of
|
|
217
|
+
// a right angle or more between them counts as one
|
|
218
|
+
const c = t[a + 2] * t[b + 3] - t[a + 3] * t[b + 2];
|
|
219
|
+
const sin = t[a + 2] * t[b + 2] + t[a + 3] * t[b + 3] > 0 ? Math.abs(c) : 1;
|
|
220
|
+
if (sin > 1e-6) {
|
|
221
|
+
const r = ((n > 0 && n < cap - 1 ? 2 : 1) * M.trailStep) / sin;
|
|
222
|
+
if (c > 0)
|
|
223
|
+
qr = Math.min(qr, Math.max(r, t[o + T_R]));
|
|
224
|
+
else
|
|
225
|
+
ql = Math.min(ql, Math.max(r, t[o + T_L]));
|
|
226
|
+
}
|
|
227
|
+
t[o + T_UL] = ql;
|
|
228
|
+
t[o + T_UR] = qr;
|
|
229
|
+
}
|
|
230
|
+
for (let n = 1; n < cap; n++)
|
|
231
|
+
lower(t, pt(g, n), pt(g, n - 1), si, T_UL, T_UR);
|
|
232
|
+
for (let k = cap - 2; k >= 0; k--)
|
|
233
|
+
lower(t, pt(g, k), pt(g, k + 1), so, T_UL, T_UR);
|
|
234
|
+
}
|
|
235
|
+
/** Keep the two widths at offsets `iL` and `iR` of trail point o within those of its neighbour `src` plus `slope`. Returns whether either was lowered. */
|
|
236
|
+
function lower(t, o, src, slope, iL, iR) {
|
|
178
237
|
let hit = false;
|
|
179
|
-
if (t[src +
|
|
180
|
-
t[o +
|
|
238
|
+
if (t[src + iL] + slope < t[o + iL]) {
|
|
239
|
+
t[o + iL] = t[src + iL] + slope;
|
|
181
240
|
hit = true;
|
|
182
241
|
}
|
|
183
|
-
if (t[src +
|
|
184
|
-
t[o +
|
|
242
|
+
if (t[src + iR] + slope < t[o + iR]) {
|
|
243
|
+
t[o + iR] = t[src + iR] + slope;
|
|
185
244
|
hit = true;
|
|
186
245
|
}
|
|
246
|
+
return hit;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Keep the widths of trail point o within those of its neighbour `src` plus `slope`. Where that lowers one and o has
|
|
250
|
+
* no narrow-place direction of its own, it takes over `src`'s. Returns whether anything was lowered.
|
|
251
|
+
*/
|
|
252
|
+
function pull(t, o, src, slope) {
|
|
253
|
+
const hit = lower(t, o, src, slope, T_L, T_R);
|
|
187
254
|
if (hit && t[src + T_HAS] && !t[o + T_HAS]) {
|
|
188
255
|
t[o + T_AX] = t[src + T_AX];
|
|
189
256
|
t[o + T_AY] = t[src + T_AY];
|
|
@@ -196,23 +263,24 @@ function pull(t, o, src, slope) {
|
|
|
196
263
|
* is first limited by the point behind it (`prev`) at the widening rate, then its narrowing is passed back along the
|
|
197
264
|
* trail until nothing gets narrower.
|
|
198
265
|
*/
|
|
199
|
-
function envelope(g, M, o, prev) {
|
|
266
|
+
function envelope(gr, g, M, o, prev) {
|
|
200
267
|
const t = g.trail, st = M.trailStep;
|
|
201
268
|
t[o + T_RL] = t[o + T_L] = CHOKE.L;
|
|
202
269
|
t[o + T_RR] = t[o + T_R] = CHOKE.R;
|
|
203
270
|
t[o + T_HAS] = CHOKE.has;
|
|
271
|
+
t[o + T_OWN] = CHOKE.own;
|
|
204
272
|
if (CHOKE.has) {
|
|
205
273
|
t[o + T_AX] = CHOKE.ax;
|
|
206
274
|
t[o + T_AY] = CHOKE.ay;
|
|
207
275
|
}
|
|
208
276
|
pull(t, o, prev, M.narrowOut * st);
|
|
209
|
-
crossAt(g, M, o);
|
|
277
|
+
crossAt(gr, g, M, o);
|
|
210
278
|
const si = M.narrowIn * st;
|
|
211
279
|
for (let n = 1, j = o; n < g.cap; n++) {
|
|
212
280
|
const k = pt(g, n);
|
|
213
281
|
if (!pull(t, k, j, si))
|
|
214
282
|
break;
|
|
215
|
-
crossAt(g, M, k);
|
|
283
|
+
crossAt(gr, g, M, k);
|
|
216
284
|
j = k;
|
|
217
285
|
}
|
|
218
286
|
}
|
|
@@ -222,7 +290,7 @@ function envelope(g, M, o, prev) {
|
|
|
222
290
|
* heading. The points kept get their envelope redone from the widths measured at each (the narrowing first passed
|
|
223
291
|
* backwards, then limited forwards at the widening rate), narrow-place directions carried along as before.
|
|
224
292
|
*/
|
|
225
|
-
export function cutAhead(g, M) {
|
|
293
|
+
export function cutAhead(gr, g, M) {
|
|
226
294
|
const st = M.trailStep, t = g.trail, cap = g.cap;
|
|
227
295
|
const k0 = g.lead <= g.rec ? 0 : Math.min(cap - 1, Math.ceil((g.lead - g.rec) / st - 1e-9));
|
|
228
296
|
const oldTail = pt(g, cap - 1);
|
|
@@ -258,7 +326,8 @@ export function cutAhead(g, M) {
|
|
|
258
326
|
for (let k = cap - 2; k >= 0; k--)
|
|
259
327
|
pull(t, pt(g, k), pt(g, k + 1), so);
|
|
260
328
|
for (let k = 0; k < cap; k++)
|
|
261
|
-
crossAt(g, M, pt(g, k));
|
|
329
|
+
crossAt(gr, g, M, pt(g, k));
|
|
330
|
+
usable(g, M);
|
|
262
331
|
}
|
|
263
332
|
/** The cell the probe walked through, as recorded at the trail point nearest the front rank; `fallback` when there is none. */
|
|
264
333
|
export function nearCell(g, M, fallback) {
|
package/dist/marchTypes.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ export interface MarchRules {
|
|
|
31
31
|
pushAhead: number;
|
|
32
32
|
/** The most the probe's heading (the direction the ranks face) turns per second. Default 1.4. */
|
|
33
33
|
turn: number;
|
|
34
|
-
/** The column's turning radius is half its
|
|
34
|
+
/** The column's turning radius is half the width across its route, and at least this; unlimited within one width of the goal. Default 40. */
|
|
35
35
|
turnR: number;
|
|
36
36
|
/** While the heading still turns, the probe may walk at most this far off it, so the ranks sidestep onto the route. Default 0.6. */
|
|
37
37
|
slide: number;
|
|
@@ -53,17 +53,23 @@ export interface MarchRules {
|
|
|
53
53
|
chokePad: number;
|
|
54
54
|
/** The obstacle on each side of a narrow place must run at least this far along the cross-section. Default 32. */
|
|
55
55
|
chokeMin: number;
|
|
56
|
-
/** A place counts as narrow when its
|
|
56
|
+
/** A place counts as narrow when the widths across the route on its two sides add up to less than this share of the column's width. Default 0.95. */
|
|
57
57
|
narrow: number;
|
|
58
58
|
/** The most a rank stretches along the trail in a narrow place; narrower still, its members close up sideways. Default 4. */
|
|
59
59
|
stretchMax: number;
|
|
60
|
-
/** A rank's cross-section starts turning from square to the column towards the direction of the narrow place when the
|
|
60
|
+
/** A rank's cross-section starts turning from square to the column towards the direction of the narrow place when the width across the route drops below `axisFrom` of the column's width, and follows it fully at `axisFull`. Defaults 0.5, 0.3. */
|
|
61
61
|
axisFrom: number;
|
|
62
62
|
axisFull: number;
|
|
63
63
|
/** Each fold of a rank first staggers members front to back, then closes them up sideways; each takes this share of a doubling of the stretch. Default 0.3. */
|
|
64
64
|
foldEase: number;
|
|
65
|
-
/** No rank is asked for more than this share of its slowest member's full speed; a column with folded ranks moves up to that. Default 0.88. */
|
|
65
|
+
/** No rank's point on the trail is asked for more than this share of its slowest member's full speed; a column with folded ranks moves up to that. Default 0.88. */
|
|
66
66
|
trot: number;
|
|
67
|
+
/**
|
|
68
|
+
* The column may string out by up to this many times its depth: a rank that cannot keep up with the one ahead of it
|
|
69
|
+
* (it is still in a narrow place, or slower) falls behind instead of holding the ranks ahead back, and the front
|
|
70
|
+
* rank's pace falls in proportion to how far the column has strung out, down to a halt at this much. Default 2.
|
|
71
|
+
*/
|
|
72
|
+
slack: number;
|
|
67
73
|
/** A new route keeps the column when the cosine between its first direction and the front rank's heading is at least this (0 = up to a right angle); otherwise the group forms up again. Default 0. */
|
|
68
74
|
rerouteCos: number;
|
|
69
75
|
/** After the front rank arrives, the column keeps leading ranks through a narrow place for at most this many seconds. Default 20. */
|
|
@@ -259,9 +265,10 @@ export interface March {
|
|
|
259
265
|
rowSp: Float64Array;
|
|
260
266
|
rowRad: Float64Array;
|
|
261
267
|
rowRun: Float64Array;
|
|
262
|
-
/** Per rank, now: distance of its middle from the probe, length along the trail, how much further back it is than unfolded, and how far it
|
|
268
|
+
/** Per rank, now: distance of its middle from the probe, length along the trail, how much further back it is than unfolded and closed up, its speed along the trail, and how far it has fallen behind the rank ahead of it. */
|
|
263
269
|
rowB: Float64Array;
|
|
264
270
|
rowLen: Float64Array;
|
|
265
271
|
rowE: Float64Array;
|
|
266
|
-
|
|
272
|
+
rowV: Float64Array;
|
|
273
|
+
rowGap: Float64Array;
|
|
267
274
|
}
|
package/dist/marchTypes.js
CHANGED
|
@@ -2,7 +2,7 @@ export const DEFAULT_MARCH_RULES = Object.freeze({
|
|
|
2
2
|
scale: 1,
|
|
3
3
|
lagFull: 30, lagStop: 110, lagCap: 400, pushAhead: Infinity, turn: 1.4, turnR: 40, slide: 0.6, lookK: 0.75, lookMin: 320, probePad: 48,
|
|
4
4
|
holdEngaged: 0.15, trailStep: 12, overMin: 0.03, narrowIn: 0.55, narrowOut: 1, chokePad: 1, chokeMin: 32, narrow: 0.95,
|
|
5
|
-
stretchMax: 4, axisFrom: 0.5, axisFull: 0.3, foldEase: 0.3, trot: 0.88, rerouteCos: 0, tailMax: 20,
|
|
5
|
+
stretchMax: 4, axisFrom: 0.5, axisFull: 0.3, foldEase: 0.3, trot: 0.88, slack: 2, rerouteCos: 0, tailMax: 20,
|
|
6
6
|
opposedLinger: 5, leadTime: 0.4,
|
|
7
7
|
});
|
|
8
8
|
/** The rules that are distances: the ones `marchRules` scales. */
|
package/dist/vision.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Field of view on a cell raster: each viewer reveals a disc of cells
|
|
3
|
-
* stamped there lets many viewers standing in the same cell cost one stamp.
|
|
2
|
+
* Field of view on a cell raster: each viewer reveals a disc of cells, and a per-cell record of the largest radius
|
|
3
|
+
* already stamped there lets many viewers standing in the same cell cost one stamp. Fog of war keeps, per side, the
|
|
4
|
+
* cells seen now and the cells ever seen, and which side's view is shown.
|
|
4
5
|
*/
|
|
5
6
|
/**
|
|
6
7
|
* Mark the disc of radius `r` cells around cell (cx, cy) in `vis` (set to 1), clipped to the `w` × `h` raster.
|
|
@@ -9,3 +10,54 @@
|
|
|
9
10
|
* `vis` and `stamped` together (fill with 0) before each recomputation.
|
|
10
11
|
*/
|
|
11
12
|
export declare function stampDisc(vis: Uint8Array, stamped: Uint8Array | Uint16Array, w: number, h: number, cx: number, cy: number, r: number): void;
|
|
13
|
+
/**
|
|
14
|
+
* Fog of war for several sides (alliances, or any party with a shared view) on one cell raster: what each side sees now,
|
|
15
|
+
* what each side has ever seen, and the view being shown. Sides are numbered 1 to `sides`; side 0 is the whole-map view,
|
|
16
|
+
* where every cell is visible and explored. The game computes each side's visible cells (with `stampDisc`, say) and hands
|
|
17
|
+
* them in with a version that changes whenever they are recomputed; `update` copies and accumulates them only when the
|
|
18
|
+
* version changes, so calling it every frame is cheap and every reader of one frame sees the same cells.
|
|
19
|
+
*/
|
|
20
|
+
export declare class FogOfWar {
|
|
21
|
+
readonly w: number;
|
|
22
|
+
readonly h: number;
|
|
23
|
+
readonly sides: number;
|
|
24
|
+
/** Cells the side sees now (1 = visible), per side; [0] is the whole map, all 1 */
|
|
25
|
+
private readonly visibleBy;
|
|
26
|
+
/** Cells the side has ever seen (1 = explored), per side; [0] is the whole map, all 1 */
|
|
27
|
+
private readonly exploredBy;
|
|
28
|
+
private viewSide;
|
|
29
|
+
private lastVersion;
|
|
30
|
+
/** All cells start unseen and unexplored, and the view starts on side `side` (default 0, the whole map) */
|
|
31
|
+
constructor(w: number, h: number, sides: number, side?: number);
|
|
32
|
+
/** The side being viewed; 0 = the whole map */
|
|
33
|
+
get side(): number;
|
|
34
|
+
/** The version of the last update, NaN before the first */
|
|
35
|
+
get version(): number;
|
|
36
|
+
/**
|
|
37
|
+
* The viewed side's cells visible now (1 = visible). The array belongs to the side, so `watch` makes this return
|
|
38
|
+
* another array: read it through this getter rather than keeping it. Do not write to it.
|
|
39
|
+
*/
|
|
40
|
+
get visible(): Uint8Array;
|
|
41
|
+
/** The viewed side's explored cells (1 = explored); like `visible`, a different array after `watch`. Do not write to it */
|
|
42
|
+
get explored(): Uint8Array;
|
|
43
|
+
/** Side `side`'s cells visible now, whatever the view; do not write to it */
|
|
44
|
+
visibleOf(side: number): Uint8Array;
|
|
45
|
+
/** Side `side`'s explored cells, whatever the view; do not write to it */
|
|
46
|
+
exploredOf(side: number): Uint8Array;
|
|
47
|
+
/** Show side `side`'s view (0 = the whole map). Every side keeps its own history, so switching loses nothing */
|
|
48
|
+
watch(side: number): void;
|
|
49
|
+
/**
|
|
50
|
+
* Take each side's visible cells: `vis[s]` holds side s's cells (any nonzero = visible, `w` × `h`, row-major) for s
|
|
51
|
+
* from 1 to `sides`; `vis[0]` is ignored. Nothing happens when `version` equals the last update's. Otherwise every
|
|
52
|
+
* side's current cells are replaced and added to its explored cells, whichever side is viewed. Returns whether it
|
|
53
|
+
* took them.
|
|
54
|
+
*/
|
|
55
|
+
update(vis: readonly ArrayLike<number>[], version: number): boolean;
|
|
56
|
+
/** Whether the viewed side sees cell (x, y) now; false outside the raster */
|
|
57
|
+
isVisible(x: number, y: number): boolean;
|
|
58
|
+
/** Whether the viewed side has explored cell (x, y); false outside the raster */
|
|
59
|
+
isExplored(x: number, y: number): boolean;
|
|
60
|
+
/** Forget everything every side has seen and wait for the next update, whatever its version; the view is kept */
|
|
61
|
+
reset(): void;
|
|
62
|
+
private check;
|
|
63
|
+
}
|
package/dist/vision.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Field of view on a cell raster: each viewer reveals a disc of cells
|
|
3
|
-
* stamped there lets many viewers standing in the same cell cost one stamp.
|
|
2
|
+
* Field of view on a cell raster: each viewer reveals a disc of cells, and a per-cell record of the largest radius
|
|
3
|
+
* already stamped there lets many viewers standing in the same cell cost one stamp. Fog of war keeps, per side, the
|
|
4
|
+
* cells seen now and the cells ever seen, and which side's view is shown.
|
|
4
5
|
*/
|
|
5
6
|
/**
|
|
6
7
|
* Mark the disc of radius `r` cells around cell (cx, cy) in `vis` (set to 1), clipped to the `w` × `h` raster.
|
|
@@ -28,3 +29,114 @@ export function stampDisc(vis, stamped, w, h, cx, cy, r) {
|
|
|
28
29
|
vis[row + xx] = 1;
|
|
29
30
|
}
|
|
30
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Fog of war for several sides (alliances, or any party with a shared view) on one cell raster: what each side sees now,
|
|
34
|
+
* what each side has ever seen, and the view being shown. Sides are numbered 1 to `sides`; side 0 is the whole-map view,
|
|
35
|
+
* where every cell is visible and explored. The game computes each side's visible cells (with `stampDisc`, say) and hands
|
|
36
|
+
* them in with a version that changes whenever they are recomputed; `update` copies and accumulates them only when the
|
|
37
|
+
* version changes, so calling it every frame is cheap and every reader of one frame sees the same cells.
|
|
38
|
+
*/
|
|
39
|
+
export class FogOfWar {
|
|
40
|
+
w;
|
|
41
|
+
h;
|
|
42
|
+
sides;
|
|
43
|
+
/** Cells the side sees now (1 = visible), per side; [0] is the whole map, all 1 */
|
|
44
|
+
visibleBy;
|
|
45
|
+
/** Cells the side has ever seen (1 = explored), per side; [0] is the whole map, all 1 */
|
|
46
|
+
exploredBy;
|
|
47
|
+
viewSide = 0;
|
|
48
|
+
lastVersion = NaN;
|
|
49
|
+
/** All cells start unseen and unexplored, and the view starts on side `side` (default 0, the whole map) */
|
|
50
|
+
constructor(w, h, sides, side = 0) {
|
|
51
|
+
this.w = w;
|
|
52
|
+
this.h = h;
|
|
53
|
+
this.sides = sides;
|
|
54
|
+
if (!Number.isInteger(sides) || sides < 0)
|
|
55
|
+
throw new RangeError(`sides must be a non-negative integer, got ${sides}`);
|
|
56
|
+
const n = w * h, all = new Uint8Array(n).fill(1);
|
|
57
|
+
this.visibleBy = Array.from({ length: sides + 1 }, (_, s) => (s ? new Uint8Array(n) : all));
|
|
58
|
+
this.exploredBy = Array.from({ length: sides + 1 }, (_, s) => (s ? new Uint8Array(n) : all));
|
|
59
|
+
this.watch(side);
|
|
60
|
+
}
|
|
61
|
+
/** The side being viewed; 0 = the whole map */
|
|
62
|
+
get side() {
|
|
63
|
+
return this.viewSide;
|
|
64
|
+
}
|
|
65
|
+
/** The version of the last update, NaN before the first */
|
|
66
|
+
get version() {
|
|
67
|
+
return this.lastVersion;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The viewed side's cells visible now (1 = visible). The array belongs to the side, so `watch` makes this return
|
|
71
|
+
* another array: read it through this getter rather than keeping it. Do not write to it.
|
|
72
|
+
*/
|
|
73
|
+
get visible() {
|
|
74
|
+
return this.visibleBy[this.viewSide];
|
|
75
|
+
}
|
|
76
|
+
/** The viewed side's explored cells (1 = explored); like `visible`, a different array after `watch`. Do not write to it */
|
|
77
|
+
get explored() {
|
|
78
|
+
return this.exploredBy[this.viewSide];
|
|
79
|
+
}
|
|
80
|
+
/** Side `side`'s cells visible now, whatever the view; do not write to it */
|
|
81
|
+
visibleOf(side) {
|
|
82
|
+
return this.visibleBy[this.check(side)];
|
|
83
|
+
}
|
|
84
|
+
/** Side `side`'s explored cells, whatever the view; do not write to it */
|
|
85
|
+
exploredOf(side) {
|
|
86
|
+
return this.exploredBy[this.check(side)];
|
|
87
|
+
}
|
|
88
|
+
/** Show side `side`'s view (0 = the whole map). Every side keeps its own history, so switching loses nothing */
|
|
89
|
+
watch(side) {
|
|
90
|
+
this.viewSide = this.check(side);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Take each side's visible cells: `vis[s]` holds side s's cells (any nonzero = visible, `w` × `h`, row-major) for s
|
|
94
|
+
* from 1 to `sides`; `vis[0]` is ignored. Nothing happens when `version` equals the last update's. Otherwise every
|
|
95
|
+
* side's current cells are replaced and added to its explored cells, whichever side is viewed. Returns whether it
|
|
96
|
+
* took them.
|
|
97
|
+
*/
|
|
98
|
+
update(vis, version) {
|
|
99
|
+
if (version === this.lastVersion)
|
|
100
|
+
return false;
|
|
101
|
+
const n = this.w * this.h;
|
|
102
|
+
if (vis.length < this.sides + 1)
|
|
103
|
+
throw new RangeError(`expected visible cells for sides 1 to ${this.sides}`);
|
|
104
|
+
for (let s = 1; s <= this.sides; s++) {
|
|
105
|
+
if (vis[s].length !== n)
|
|
106
|
+
throw new RangeError(`side ${s}: expected ${n} cells, got ${vis[s].length}`);
|
|
107
|
+
}
|
|
108
|
+
for (let s = 1; s <= this.sides; s++) {
|
|
109
|
+
const src = vis[s], cur = this.visibleBy[s], ex = this.exploredBy[s];
|
|
110
|
+
for (let i = 0; i < n; i++) {
|
|
111
|
+
if (src[i])
|
|
112
|
+
cur[i] = ex[i] = 1;
|
|
113
|
+
else
|
|
114
|
+
cur[i] = 0;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
this.lastVersion = version;
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
/** Whether the viewed side sees cell (x, y) now; false outside the raster */
|
|
121
|
+
isVisible(x, y) {
|
|
122
|
+
return x >= 0 && y >= 0 && x < this.w && y < this.h && this.visible[y * this.w + x] === 1;
|
|
123
|
+
}
|
|
124
|
+
/** Whether the viewed side has explored cell (x, y); false outside the raster */
|
|
125
|
+
isExplored(x, y) {
|
|
126
|
+
return x >= 0 && y >= 0 && x < this.w && y < this.h && this.explored[y * this.w + x] === 1;
|
|
127
|
+
}
|
|
128
|
+
/** Forget everything every side has seen and wait for the next update, whatever its version; the view is kept */
|
|
129
|
+
reset() {
|
|
130
|
+
for (let s = 1; s <= this.sides; s++) {
|
|
131
|
+
this.visibleBy[s].fill(0);
|
|
132
|
+
this.exploredBy[s].fill(0);
|
|
133
|
+
}
|
|
134
|
+
this.lastVersion = NaN;
|
|
135
|
+
}
|
|
136
|
+
check(side) {
|
|
137
|
+
if (!Number.isInteger(side) || side < 0 || side > this.sides) {
|
|
138
|
+
throw new RangeError(`side must be an integer from 0 to ${this.sides}, got ${side}`);
|
|
139
|
+
}
|
|
140
|
+
return side;
|
|
141
|
+
}
|
|
142
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pocketrts/grid",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Grid algorithms for RTS games: budgeted flow fields, bounded local A*, float-cost A* within an area, 4-connected regions, line reach, distance fields, bucket grids, field of view, polygons, cell chains, line formations and column march",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Grid algorithms for RTS games: budgeted flow fields, bounded local A*, float-cost A* within an area, 4-connected regions, line reach, distance fields, bucket grids, field of view, fog of war per side, polygons, cell chains, line formations and column march",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Junjie Bai <bjj19851010@gmail.com>",
|
|
7
7
|
"repository": {
|