@pocketrts/grid 0.1.0 → 0.2.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 +73 -2
- package/dist/formation.d.ts +23 -1
- package/dist/formation.js +43 -5
- package/dist/index.d.ts +5 -0
- package/dist/index.js +7 -0
- package/dist/march.d.ts +77 -0
- package/dist/march.js +665 -0
- package/dist/marchChoke.d.ts +27 -0
- package/dist/marchChoke.js +140 -0
- package/dist/marchGround.d.ts +45 -0
- package/dist/marchGround.js +73 -0
- package/dist/marchSlots.d.ts +66 -0
- package/dist/marchSlots.js +215 -0
- package/dist/marchTrail.d.ts +48 -0
- package/dist/marchTrail.js +282 -0
- package/dist/marchTypes.d.ts +289 -0
- package/dist/marchTypes.js +31 -0
- package/package.json +2 -2
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
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 usable width on each side and the direction of
|
|
4
|
+
* the cross-section. A place with an obstacle on one side only, or one that leaves the wing tip free (the wings can
|
|
5
|
+
* pass round it), is not a narrow place. With `MarchRules.flank`, a long wall blocks its wing however thin it is, and
|
|
6
|
+
* where no narrow place is found, what stands beside the route is still recorded, side by side, so that ranks keep
|
|
7
|
+
* clear of it.
|
|
8
|
+
*/
|
|
9
|
+
import type { March, MarchGround, MarchRules } from './marchTypes.js';
|
|
10
|
+
/** Result of `chokeAt`: usable width on each side (R on the right of travel), the cross-section's direction (pointing right of travel), and whether a narrow place was found (1 or 0). */
|
|
11
|
+
export declare const CHOKE: {
|
|
12
|
+
L: number;
|
|
13
|
+
R: number;
|
|
14
|
+
ax: number;
|
|
15
|
+
ay: number;
|
|
16
|
+
has: number;
|
|
17
|
+
};
|
|
18
|
+
/** Set `CHOKE` to "no narrow place": both sides as wide as where nothing was measured. */
|
|
19
|
+
export declare function chokeClear(g: March): void;
|
|
20
|
+
/**
|
|
21
|
+
* Measure the narrow place at trail point (x, y), travelling in direction (tx, ty); the result goes to `CHOKE`. Among
|
|
22
|
+
* four fixed directions, take the shortest line not parallel to the travel that hits a large obstacle both ways as
|
|
23
|
+
* the cross-section: it follows the opening (parallel to the wall), not the heading of the column. Nothing found
|
|
24
|
+
* leaves `CHOKE` as `chokeClear` sets it (but see `MarchRules.flank`). The widths are less the clearance `chokePad`,
|
|
25
|
+
* and at a narrow place add up to less than `narrow` times the column's width.
|
|
26
|
+
*/
|
|
27
|
+
export declare function chokeAt(gr: MarchGround, g: March, M: MarchRules, x: number, y: number, tx: number, ty: number): void;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/** The four directions a cross-section is looked for in (east–west, the two diagonals, north–south), two numbers each. */
|
|
2
|
+
const AXES = [1, 0, Math.SQRT1_2, Math.SQRT1_2, 0, 1, -Math.SQRT1_2, Math.SQRT1_2];
|
|
3
|
+
/** A direction whose cosine with the direction of travel exceeds this is not used: it runs along the route, not across it. */
|
|
4
|
+
const AXIS_PAR = 0.8;
|
|
5
|
+
/** Result of `chokeAt`: usable width on each side (R on the right of travel), the cross-section's direction (pointing right of travel), and whether a narrow place was found (1 or 0). */
|
|
6
|
+
export const CHOKE = { L: 0, R: 0, ax: 0, ay: 0, has: 0 };
|
|
7
|
+
/** Set `CHOKE` to "no narrow place": both sides as wide as where nothing was measured. */
|
|
8
|
+
export function chokeClear(g) {
|
|
9
|
+
CHOKE.L = CHOKE.R = g.open;
|
|
10
|
+
CHOKE.ax = CHOKE.ay = CHOKE.has = 0;
|
|
11
|
+
}
|
|
12
|
+
/** Nobody can stand in cell (cx, cy): solid, or off the map. */
|
|
13
|
+
function solidCell(gr, cx, cy) {
|
|
14
|
+
if (cx < 0 || cy < 0 || cx >= gr.w || cy >= gr.h)
|
|
15
|
+
return true;
|
|
16
|
+
return gr.solid(cy * gr.w + cx);
|
|
17
|
+
}
|
|
18
|
+
/** Distance from (x, y) along the unit vector (dx, dy) to the first solid cell; −1 when there is none within `max`. */
|
|
19
|
+
function rayHit(gr, x, y, dx, dy, max) {
|
|
20
|
+
const T = gr.cellSize;
|
|
21
|
+
let cx = Math.floor(x / T), cy = Math.floor(y / T);
|
|
22
|
+
if (solidCell(gr, cx, cy))
|
|
23
|
+
return 0;
|
|
24
|
+
const sx = dx > 0 ? 1 : -1, sy = dy > 0 ? 1 : -1;
|
|
25
|
+
let tx = dx !== 0 ? ((dx > 0 ? (cx + 1) * T : cx * T) - x) / dx : Infinity;
|
|
26
|
+
let ty = dy !== 0 ? ((dy > 0 ? (cy + 1) * T : cy * T) - y) / dy : Infinity;
|
|
27
|
+
const ddx = dx !== 0 ? T / Math.abs(dx) : Infinity, ddy = dy !== 0 ? T / Math.abs(dy) : Infinity;
|
|
28
|
+
for (;;) {
|
|
29
|
+
let d;
|
|
30
|
+
if (Math.abs(tx - ty) < 1e-6) {
|
|
31
|
+
// Exactly through a cell corner: blocked when either cell beside it is solid
|
|
32
|
+
d = tx;
|
|
33
|
+
if (d > max)
|
|
34
|
+
return -1;
|
|
35
|
+
if (solidCell(gr, cx + sx, cy) || solidCell(gr, cx, cy + sy))
|
|
36
|
+
return d;
|
|
37
|
+
cx += sx;
|
|
38
|
+
cy += sy;
|
|
39
|
+
tx += ddx;
|
|
40
|
+
ty += ddy;
|
|
41
|
+
}
|
|
42
|
+
else if (tx < ty) {
|
|
43
|
+
d = tx;
|
|
44
|
+
cx += sx;
|
|
45
|
+
tx += ddx;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
d = ty;
|
|
49
|
+
cy += sy;
|
|
50
|
+
ty += ddy;
|
|
51
|
+
}
|
|
52
|
+
if (d > max)
|
|
53
|
+
return -1;
|
|
54
|
+
if (solidCell(gr, cx, cy))
|
|
55
|
+
return d;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Distance from the trail point (x, y) along (dx, dy) to an obstacle; −1 when the obstacle does not block this whole
|
|
60
|
+
* wing: it must run on from where it was hit out to the wing tip (half the column's width, at least `chokeMin`).
|
|
61
|
+
* It is sampled every half cell, the first sample a quarter cell in, so every cell is looked at twice and a gap one
|
|
62
|
+
* cell wide is not missed. With `MarchRules.flank`, an obstacle thinner than that still counts when it is a wall
|
|
63
|
+
* (`wallRuns`): otherwise a corridor between thin walls is not measured across, only along a diagonal where the
|
|
64
|
+
* walls happen to be thick enough, and passes for as wide as that diagonal is long.
|
|
65
|
+
*/
|
|
66
|
+
function wallAt(gr, g, M, x, y, dx, dy) {
|
|
67
|
+
const T = gr.cellSize;
|
|
68
|
+
const d = rayHit(gr, x, y, dx, dy, g.width);
|
|
69
|
+
if (d < 0)
|
|
70
|
+
return -1;
|
|
71
|
+
const run = Math.max(M.chokeMin, g.width / 2 - d);
|
|
72
|
+
for (let k = T / 4; k <= run; k += T / 2) {
|
|
73
|
+
const px = x + dx * (d + k), py = y + dy * (d + k);
|
|
74
|
+
if (!solidCell(gr, Math.floor(px / T), Math.floor(py / T)))
|
|
75
|
+
return M.flank && wallRuns(gr, g, M, x + dx * (d + T / 2), y + dy * (d + T / 2), -dy, dx) ? d : -1;
|
|
76
|
+
}
|
|
77
|
+
return d;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Whether the obstacle holding (x, y) is a wall: it runs on along (ux, uy) and against it, the two ways together for
|
|
81
|
+
* at least half the column's width (at least `chokeMin`), sampled every half cell. However thin, such a wall blocks
|
|
82
|
+
* its wing as a thick obstacle does: nobody walks round it to keep a place on its far side.
|
|
83
|
+
*/
|
|
84
|
+
function wallRuns(gr, g, M, x, y, ux, uy) {
|
|
85
|
+
const T = gr.cellSize;
|
|
86
|
+
if (!solidCell(gr, Math.floor(x / T), Math.floor(y / T)))
|
|
87
|
+
return false;
|
|
88
|
+
const need = Math.max(M.chokeMin, g.width / 2);
|
|
89
|
+
let len = 0;
|
|
90
|
+
for (let sg = -1; sg <= 1; sg += 2) {
|
|
91
|
+
for (let k = T / 2; len < need; k += T / 2) {
|
|
92
|
+
const px = x + ux * sg * k, py = y + uy * sg * k;
|
|
93
|
+
if (!solidCell(gr, Math.floor(px / T), Math.floor(py / T)))
|
|
94
|
+
break;
|
|
95
|
+
len += T / 2;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return len >= need;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Measure the narrow place at trail point (x, y), travelling in direction (tx, ty); the result goes to `CHOKE`. Among
|
|
102
|
+
* four fixed directions, take the shortest line not parallel to the travel that hits a large obstacle both ways as
|
|
103
|
+
* the cross-section: it follows the opening (parallel to the wall), not the heading of the column. Nothing found
|
|
104
|
+
* leaves `CHOKE` as `chokeClear` sets it (but see `MarchRules.flank`). The widths are less the clearance `chokePad`,
|
|
105
|
+
* and at a narrow place add up to less than `narrow` times the column's width.
|
|
106
|
+
*/
|
|
107
|
+
export function chokeAt(gr, g, M, x, y, tx, ty) {
|
|
108
|
+
chokeClear(g);
|
|
109
|
+
let best = g.width * M.narrow;
|
|
110
|
+
for (let a = 0; a < AXES.length; a += 2) {
|
|
111
|
+
const ux = AXES[a], uy = AXES[a + 1];
|
|
112
|
+
const dot = ux * tx + uy * ty;
|
|
113
|
+
if (dot > AXIS_PAR || dot < -AXIS_PAR)
|
|
114
|
+
continue;
|
|
115
|
+
// The end on the right of travel counts as positive
|
|
116
|
+
const sg = -ux * ty + uy * tx >= 0 ? 1 : -1, dx = ux * sg, dy = uy * sg;
|
|
117
|
+
const dr = wallAt(gr, g, M, x, y, dx, dy);
|
|
118
|
+
if (dr < 0)
|
|
119
|
+
continue;
|
|
120
|
+
const dl = wallAt(gr, g, M, x, y, -dx, -dy);
|
|
121
|
+
if (dl < 0 || dl + dr >= best)
|
|
122
|
+
continue;
|
|
123
|
+
best = dl + dr;
|
|
124
|
+
CHOKE.R = Math.max(0, dr - M.chokePad);
|
|
125
|
+
CHOKE.L = Math.max(0, dl - M.chokePad);
|
|
126
|
+
CHOKE.ax = dx;
|
|
127
|
+
CHOKE.ay = dy;
|
|
128
|
+
CHOKE.has = 1;
|
|
129
|
+
}
|
|
130
|
+
// A point on a solid cell measures nothing either way (a ray from it would read 0 on both sides)
|
|
131
|
+
if (CHOKE.has || !M.flank || solidCell(gr, Math.floor(x / gr.cellSize), Math.floor(y / gr.cellSize)))
|
|
132
|
+
return;
|
|
133
|
+
// No narrow place by that measure. With `flank`, whatever stands within the column's width square to the travel
|
|
134
|
+
// still counts on its own side: no direction of a narrow place goes with it, so ranks stay square to their heading
|
|
135
|
+
const dr = rayHit(gr, x, y, -ty, tx, g.width), dl = rayHit(gr, x, y, ty, -tx, g.width);
|
|
136
|
+
if (dr >= 0)
|
|
137
|
+
CHOKE.R = Math.max(0, dr - M.chokePad);
|
|
138
|
+
if (dl >= 0)
|
|
139
|
+
CHOKE.L = Math.max(0, dl - M.chokePad);
|
|
140
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type FlowField, type FlowFields } from './flowfield.js';
|
|
2
|
+
import type { MarchGround } from './marchTypes.js';
|
|
3
|
+
export interface GridGroundOptions {
|
|
4
|
+
/** Grid size in cells, and world units per cell. */
|
|
5
|
+
w: number;
|
|
6
|
+
h: number;
|
|
7
|
+
cellSize: number;
|
|
8
|
+
/** Non-zero = the cell cannot be entered. The outermost ring must be blocked. */
|
|
9
|
+
block: ArrayLike<number>;
|
|
10
|
+
/** The flow-field cache routes come from, and the layer of it this ground is for. Default layer 0. */
|
|
11
|
+
fields: FlowFields;
|
|
12
|
+
layer?: number;
|
|
13
|
+
/** How fast a cell is walked, as a factor of normal speed. Default 1 for every open cell. */
|
|
14
|
+
speed?: (cell: number) => number;
|
|
15
|
+
}
|
|
16
|
+
export declare class GridGround implements MarchGround {
|
|
17
|
+
readonly w: number;
|
|
18
|
+
readonly h: number;
|
|
19
|
+
readonly cellSize: number;
|
|
20
|
+
/** Set by the game before each step: fields count as in use on it, and one left behind would be recomputed over and over. */
|
|
21
|
+
tick: number;
|
|
22
|
+
/** A factor on every member's speed; the game may change it between steps. */
|
|
23
|
+
speedScale: number;
|
|
24
|
+
private readonly block;
|
|
25
|
+
private readonly fields;
|
|
26
|
+
private readonly layer;
|
|
27
|
+
private readonly speed?;
|
|
28
|
+
private readonly stuck;
|
|
29
|
+
constructor(options: GridGroundOptions);
|
|
30
|
+
cellAt(x: number, y: number): number;
|
|
31
|
+
solid(cell: number): boolean;
|
|
32
|
+
blocked(cell: number): boolean;
|
|
33
|
+
/** Between neighbouring cells only: straight steps are free, a diagonal step must not brush a blocked corner. */
|
|
34
|
+
canStep(from: number, to: number): boolean;
|
|
35
|
+
barred(): boolean;
|
|
36
|
+
nearestFree(cell: number, maxR: number): number;
|
|
37
|
+
lineReach(ax: number, ay: number, bx: number, by: number): number;
|
|
38
|
+
lineBlocked(ax: number, ay: number, bx: number, by: number): boolean;
|
|
39
|
+
standable(x: number, y: number): boolean;
|
|
40
|
+
/** Fields are cached per layer and goal cell: key = layer × cells + cell. */
|
|
41
|
+
field(cell: number): FlowField;
|
|
42
|
+
finish(field: FlowField): void;
|
|
43
|
+
speedAt(cell: number): number;
|
|
44
|
+
headingScale(): number;
|
|
45
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Column march: a ready-made `MarchGround` for a plain grid, where a cell is either open or blocked for everybody
|
|
3
|
+
* of a movement layer. A game with more to its map (levels, gates, terrain speeds that change) writes its own.
|
|
4
|
+
*/
|
|
5
|
+
import { lineReach, nearestCell } from './cells.js';
|
|
6
|
+
import { cornerFree } from './flowfield.js';
|
|
7
|
+
export class GridGround {
|
|
8
|
+
w;
|
|
9
|
+
h;
|
|
10
|
+
cellSize;
|
|
11
|
+
/** Set by the game before each step: fields count as in use on it, and one left behind would be recomputed over and over. */
|
|
12
|
+
tick = 0;
|
|
13
|
+
/** A factor on every member's speed; the game may change it between steps. */
|
|
14
|
+
speedScale = 1;
|
|
15
|
+
block;
|
|
16
|
+
fields;
|
|
17
|
+
layer;
|
|
18
|
+
speed;
|
|
19
|
+
stuck = (_from, to) => this.block[to] !== 0;
|
|
20
|
+
constructor(options) {
|
|
21
|
+
this.w = options.w;
|
|
22
|
+
this.h = options.h;
|
|
23
|
+
this.cellSize = options.cellSize;
|
|
24
|
+
this.block = options.block;
|
|
25
|
+
this.fields = options.fields;
|
|
26
|
+
this.layer = options.layer ?? 0;
|
|
27
|
+
this.speed = options.speed;
|
|
28
|
+
}
|
|
29
|
+
cellAt(x, y) {
|
|
30
|
+
return ((y / this.cellSize) | 0) * this.w + ((x / this.cellSize) | 0);
|
|
31
|
+
}
|
|
32
|
+
solid(cell) {
|
|
33
|
+
return this.block[cell] !== 0;
|
|
34
|
+
}
|
|
35
|
+
blocked(cell) {
|
|
36
|
+
return this.block[cell] !== 0;
|
|
37
|
+
}
|
|
38
|
+
/** Between neighbouring cells only: straight steps are free, a diagonal step must not brush a blocked corner. */
|
|
39
|
+
canStep(from, to) {
|
|
40
|
+
const dx = (to % this.w) - (from % this.w), dy = ((to / this.w) | 0) - ((from / this.w) | 0);
|
|
41
|
+
if (dx < -1 || dx > 1 || dy < -1 || dy > 1)
|
|
42
|
+
return false;
|
|
43
|
+
return dx === 0 || dy === 0 || cornerFree(this.block, this.w, from, to);
|
|
44
|
+
}
|
|
45
|
+
barred() {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
nearestFree(cell, maxR) {
|
|
49
|
+
return nearestCell(this.w, this.h, cell, maxR, c => this.block[c] === 0);
|
|
50
|
+
}
|
|
51
|
+
lineReach(ax, ay, bx, by) {
|
|
52
|
+
return lineReach(ax, ay, bx, by, this.cellSize, this.w, this.stuck, this.cellSize / 2);
|
|
53
|
+
}
|
|
54
|
+
lineBlocked(ax, ay, bx, by) {
|
|
55
|
+
return this.lineReach(ax, ay, bx, by) < 1;
|
|
56
|
+
}
|
|
57
|
+
standable(x, y) {
|
|
58
|
+
return x >= 0 && y >= 0 && x < this.w * this.cellSize && y < this.h * this.cellSize && this.block[this.cellAt(x, y)] === 0;
|
|
59
|
+
}
|
|
60
|
+
/** Fields are cached per layer and goal cell: key = layer × cells + cell. */
|
|
61
|
+
field(cell) {
|
|
62
|
+
return this.fields.get(this.layer * this.w * this.h + cell, this.layer, this.tick, () => Int32Array.of(cell));
|
|
63
|
+
}
|
|
64
|
+
finish(field) {
|
|
65
|
+
this.fields.finish(field);
|
|
66
|
+
}
|
|
67
|
+
speedAt(cell) {
|
|
68
|
+
return this.block[cell] ? 0 : this.speed ? this.speed(cell) : 1;
|
|
69
|
+
}
|
|
70
|
+
headingScale() {
|
|
71
|
+
return 1;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { March, MarchGround, MarchRules } from './marchTypes.js';
|
|
2
|
+
/**
|
|
3
|
+
* Where each rank stands with the front rank `b` behind the probe: from the front rank back, one after another, each
|
|
4
|
+
* taking the stretch `rowLength` gives. Results go to `rb` (distance of the rank's middle behind the probe), `rl`
|
|
5
|
+
* (length of its stretch), `re` (how much further back it is than unfolded) and, when given, `wh` (how much further
|
|
6
|
+
* the rank's outermost place moves than its point on the trail, averaged over its stretch: 1 without
|
|
7
|
+
* `MarchRules.wheel`).
|
|
8
|
+
*/
|
|
9
|
+
export declare function rowsAt(g: March, M: MarchRules, b: number, rb: Float64Array, rl: Float64Array, re: Float64Array, wh?: Float64Array): void;
|
|
10
|
+
/**
|
|
11
|
+
* Result of `marchSlot`: the place and the direction of travel there, the point of the rank's middle line (same short
|
|
12
|
+
* rank, lateral offset 0), the place's distance behind the probe along the trail, its lateral offset from the middle
|
|
13
|
+
* line and the half width there (both after folding and narrowing), and the rank's current speed.
|
|
14
|
+
*/
|
|
15
|
+
export declare const SLOT: {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
hx: number;
|
|
19
|
+
hy: number;
|
|
20
|
+
cx: number;
|
|
21
|
+
cy: number;
|
|
22
|
+
b: number;
|
|
23
|
+
l: number;
|
|
24
|
+
half: number;
|
|
25
|
+
v: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The place right now of the member in rank r, place c of the rank, lateral offset l (positive to the right of the
|
|
29
|
+
* facing); the result goes to `SLOT`. The rank's stretch of trail comes from `rowsAt` (kept in the march). Folding
|
|
30
|
+
* into short ranks: squeezed to half its width, every other member drops back half a stretch and then closes up
|
|
31
|
+
* behind its neighbour; at a quarter, it folds again, and so on. Each fold first staggers members front to back and
|
|
32
|
+
* only then closes them up sideways, so no two collide on the way. Sideways the rank closes up in proportion to the
|
|
33
|
+
* usable width there; a rank narrower than the way keeps the trail as its middle line and only gives way beside an
|
|
34
|
+
* obstacle. The cross-section follows the direction of the narrow place, not the column's heading. In the open the
|
|
35
|
+
* place is the rank's point on the trail moved sideways by l.
|
|
36
|
+
*/
|
|
37
|
+
export declare function marchSlot(g: March, M: MarchRules, r: number, c: number, l: number): void;
|
|
38
|
+
/** Result of `marchFollow`: the point to head for this step. */
|
|
39
|
+
export declare const FOLLOW: {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
};
|
|
43
|
+
/** What `marchFollow` returns: head for the place; close in towards the middle line and go along with the rank; neither the place nor the rank is in reach (find your own way). */
|
|
44
|
+
export declare const FOLLOW_SLOT = 0, FOLLOW_SQUEEZE = 1, FOLLOW_APART = 2;
|
|
45
|
+
/**
|
|
46
|
+
* Which point the member standing at (ux, uy) heads for this step, into `FOLLOW`. Its place must be in `SLOT` (from
|
|
47
|
+
* `marchSlot`).
|
|
48
|
+
* - The place can be stood on and walked to in a straight line: head for it.
|
|
49
|
+
* - The place is out of reach but the point on the rank's middle line is in sight: an obstacle has cut this side
|
|
50
|
+
* narrower, so the member closes in towards the middle line and goes along with its rank. From the middle point
|
|
51
|
+
* towards the place, the line runs up to the obstacle: that is the width left on this side, and the member stands at
|
|
52
|
+
* the same share of it as its offset is of the half width there (`SLOT.l` ÷ `SLOT.half`). The outermost member is at
|
|
53
|
+
* the obstacle and the others spread inwards, instead of all crowding at its edge. When the place itself can be
|
|
54
|
+
* stood on and reached from the middle point (only a small obstacle is in this member's way), this side is no
|
|
55
|
+
* narrower and the place stands. With the chosen point out of sight for a moment (round a corner), the member heads
|
|
56
|
+
* for the middle point first.
|
|
57
|
+
* - Not even the middle point is in sight (a wall, a building, a river in between): returns `FOLLOW_APART` and leaves
|
|
58
|
+
* `FOLLOW` alone; the member finds its own way along its route.
|
|
59
|
+
*/
|
|
60
|
+
export declare function marchFollow(gr: MarchGround, ux: number, uy: number): number;
|
|
61
|
+
/**
|
|
62
|
+
* While the column winds up (`g.tail`), whether the member standing at (ux, uy), with its spot at the goal at
|
|
63
|
+
* (dx, dy), should leave for the spot: its rank has passed the goal, or no narrow place lies ahead of its place and
|
|
64
|
+
* the spot is in plain sight. Its place must be in `SLOT` (from `marchSlot`).
|
|
65
|
+
*/
|
|
66
|
+
export declare function marchRelease(gr: MarchGround, g: March, ux: number, uy: number, dx: number, dy: number): boolean;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Column march: ranks and members. Which stretch of the trail each rank stands on right now (a rank wider than the
|
|
3
|
+
* way folds into several short ranks and takes a longer stretch), each member's place in the column, the point a
|
|
4
|
+
* member heads for this step, and whether a member should leave the column while it winds up.
|
|
5
|
+
*/
|
|
6
|
+
import { smooth } from '@pocketrts/determinism';
|
|
7
|
+
import { TP, trailAt, widthAt } from './marchTrail.js';
|
|
8
|
+
/** Length of each piece when working out how long a stretch a rank takes, in units of `MarchRules.scale`. */
|
|
9
|
+
const ROW_STEP = 4;
|
|
10
|
+
/**
|
|
11
|
+
* How much further the outermost place of rank r moves than the rank's point on the trail, with the rank `b` behind
|
|
12
|
+
* the probe: 1 where the cross-section keeps its direction; where it turns (on a bend, or towards the direction of a
|
|
13
|
+
* narrow place), 1 plus its turn per unit of trail times the distance of the rank's furthest place from the trail.
|
|
14
|
+
* The half width and the shift follow `marchSlot`'s, though not necessarily to the last bit: this only estimates a
|
|
15
|
+
* pace.
|
|
16
|
+
*/
|
|
17
|
+
function wheelAt(g, M, r, b) {
|
|
18
|
+
const d = M.trailStep;
|
|
19
|
+
trailAt(g, M, b - d);
|
|
20
|
+
const ax = TP.lx, ay = TP.ly;
|
|
21
|
+
trailAt(g, M, b + d);
|
|
22
|
+
let turn = ax * TP.ly - ay * TP.lx;
|
|
23
|
+
if (turn < 0)
|
|
24
|
+
turn = -turn;
|
|
25
|
+
trailAt(g, M, b);
|
|
26
|
+
// The rank's half width and how far it has moved over, as `marchSlot` places it
|
|
27
|
+
const rad = g.rowRad[r], L = TP.L > rad ? TP.L - rad : 0, R = TP.R > rad ? TP.R - rad : 0;
|
|
28
|
+
const rw = g.rowW[r], half = (L + R < rw ? L + R : rw) / 2;
|
|
29
|
+
const lo = half - L, hi = R - half, sh = lo > hi ? (R - L) / 2 : lo > 0 ? lo : hi < 0 ? hi : 0;
|
|
30
|
+
return 1 + turn / (2 * d) * ((sh < 0 ? -sh : sh) + half);
|
|
31
|
+
}
|
|
32
|
+
/** Set by `rowLength`: the rank's wheel factor (`wheelAt`), averaged over the stretch it takes; exactly 1 where no piece of it has a factor above 1, and without `MarchRules.wheel`. */
|
|
33
|
+
let WHEEL = 1;
|
|
34
|
+
/**
|
|
35
|
+
* How long a stretch of trail rank r takes back from its leading edge `edge` (distance behind the probe). Each piece
|
|
36
|
+
* counts for "usable width ÷ rank width" of its length (at most 1, at least 1 / `stretchMax`) towards one rank depth.
|
|
37
|
+
* Where the way is wide enough a piece counts whole, so in the open the stretch is exactly one rank depth; where the
|
|
38
|
+
* way is a fraction of the rank's width the stretch is that many times longer, up to `stretchMax`. The length of a
|
|
39
|
+
* long folded rank changes smoothly across a change of width, without a jump. With `MarchRules.wheel` a piece on a
|
|
40
|
+
* bend counts for more, by as much as the rank's outermost place moves further than its point on the trail: the
|
|
41
|
+
* stretch is shorter there, and ranks pass a bend closed up along the trail.
|
|
42
|
+
*/
|
|
43
|
+
function rowLength(g, M, r, edge) {
|
|
44
|
+
const d = g.rowD[r], rw = g.rowW[r], pad = g.rowRad[r] * 2;
|
|
45
|
+
const lo = 1 / M.stretchMax, piece = ROW_STEP * M.scale;
|
|
46
|
+
let acc = 0, len = 0, squeezed = false;
|
|
47
|
+
// For `wheel`: the wheel factor of each piece times its length, summed, and whether any piece had one above 1
|
|
48
|
+
let turned = 0, w = 1, bent = false;
|
|
49
|
+
WHEEL = 1;
|
|
50
|
+
for (;;) {
|
|
51
|
+
const q = (d - acc) * (squeezed ? 1 / lo : 1);
|
|
52
|
+
if (q < 1e-6) {
|
|
53
|
+
if (bent && len > 0)
|
|
54
|
+
WHEEL = turned / len;
|
|
55
|
+
return len;
|
|
56
|
+
}
|
|
57
|
+
const step = q < piece ? q : piece;
|
|
58
|
+
let f = (widthAt(g, M, edge + len + step / 2) - pad) / rw;
|
|
59
|
+
if (f >= 1)
|
|
60
|
+
f = 1;
|
|
61
|
+
else {
|
|
62
|
+
squeezed = true;
|
|
63
|
+
if (f < lo)
|
|
64
|
+
f = lo;
|
|
65
|
+
}
|
|
66
|
+
if (M.wheel) {
|
|
67
|
+
// On a bend the ranks keep their distance along the outside of it: along the trail they close up
|
|
68
|
+
w = wheelAt(g, M, r, edge + len + step / 2);
|
|
69
|
+
if (w > 1) {
|
|
70
|
+
f *= w;
|
|
71
|
+
squeezed = bent = true;
|
|
72
|
+
}
|
|
73
|
+
else
|
|
74
|
+
w = 1;
|
|
75
|
+
}
|
|
76
|
+
if (acc + f * step >= d) {
|
|
77
|
+
// Wide enough all the way: exactly one rank depth, with no accumulated rounding, so in the open the column is
|
|
78
|
+
// bit for bit the formation it was made from
|
|
79
|
+
const all = squeezed ? len + (d - acc) / f : d;
|
|
80
|
+
if (bent && all > 0)
|
|
81
|
+
WHEEL = (turned + w * (all - len)) / all;
|
|
82
|
+
return all;
|
|
83
|
+
}
|
|
84
|
+
acc += f * step;
|
|
85
|
+
len += step;
|
|
86
|
+
turned += w * step;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Where each rank stands with the front rank `b` behind the probe: from the front rank back, one after another, each
|
|
91
|
+
* taking the stretch `rowLength` gives. Results go to `rb` (distance of the rank's middle behind the probe), `rl`
|
|
92
|
+
* (length of its stretch), `re` (how much further back it is than unfolded) and, when given, `wh` (how much further
|
|
93
|
+
* the rank's outermost place moves than its point on the trail, averaged over its stretch: 1 without
|
|
94
|
+
* `MarchRules.wheel`).
|
|
95
|
+
*/
|
|
96
|
+
export function rowsAt(g, M, b, rb, rl, re, wh) {
|
|
97
|
+
let extra = 0;
|
|
98
|
+
for (let r = 0; r < g.rows; r++) {
|
|
99
|
+
const d = g.rowD[r];
|
|
100
|
+
// The rank's middle when unfolded
|
|
101
|
+
const c = b - g.rowA[r] + extra;
|
|
102
|
+
const len = rowLength(g, M, r, c - d / 2);
|
|
103
|
+
const e = extra + (len - d) / 2;
|
|
104
|
+
rb[r] = b - g.rowA[r] + e;
|
|
105
|
+
rl[r] = len;
|
|
106
|
+
re[r] = e;
|
|
107
|
+
if (wh)
|
|
108
|
+
wh[r] = WHEEL;
|
|
109
|
+
extra += len - d;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Result of `marchSlot`: the place and the direction of travel there, the point of the rank's middle line (same short
|
|
114
|
+
* rank, lateral offset 0), the place's distance behind the probe along the trail, its lateral offset from the middle
|
|
115
|
+
* line and the half width there (both after folding and narrowing), and the rank's current speed.
|
|
116
|
+
*/
|
|
117
|
+
export const SLOT = { x: 0, y: 0, hx: 0, hy: 0, cx: 0, cy: 0, b: 0, l: 0, half: 1, v: 0 };
|
|
118
|
+
/**
|
|
119
|
+
* The place right now of the member in rank r, place c of the rank, lateral offset l (positive to the right of the
|
|
120
|
+
* facing); the result goes to `SLOT`. The rank's stretch of trail comes from `rowsAt` (kept in the march). Folding
|
|
121
|
+
* into short ranks: squeezed to half its width, every other member drops back half a stretch and then closes up
|
|
122
|
+
* behind its neighbour; at a quarter, it folds again, and so on. Each fold first staggers members front to back and
|
|
123
|
+
* only then closes them up sideways, so no two collide on the way. Sideways the rank closes up in proportion to the
|
|
124
|
+
* usable width there; a rank narrower than the way keeps the trail as its middle line and only gives way beside an
|
|
125
|
+
* obstacle. The cross-section follows the direction of the narrow place, not the column's heading. In the open the
|
|
126
|
+
* place is the rank's point on the trail moved sideways by l.
|
|
127
|
+
*/
|
|
128
|
+
export function marchSlot(g, M, r, c, l) {
|
|
129
|
+
const rw = g.rowW[r], sp = g.rowSp[r], len = g.rowLen[r];
|
|
130
|
+
// How many short ranks the rank folds into. With `wheel` the stretch is also shorter on a bend, so the folding is
|
|
131
|
+
// taken from the usable width at the rank's middle instead
|
|
132
|
+
let m = len / g.rowD[r];
|
|
133
|
+
if (M.wheel) {
|
|
134
|
+
const fw = (widthAt(g, M, g.rowB[r]) - g.rowRad[r] * 2) / rw;
|
|
135
|
+
m = fw >= 1 ? 1 : fw * M.stretchMax < 1 ? M.stretchMax : 1 / fw;
|
|
136
|
+
}
|
|
137
|
+
const E = M.foldEase;
|
|
138
|
+
let ph = 0, dc = 0;
|
|
139
|
+
for (let k = 0, p2 = 1; k < 7 && p2 < m; k++, p2 *= 2) {
|
|
140
|
+
const bit = (c >> k) & 1;
|
|
141
|
+
ph += (bit - 0.5) * smooth((m / p2 - 1) / E) / (2 * p2);
|
|
142
|
+
dc += (0.5 - bit) * p2 * smooth((m / p2 - 1 - E) / E);
|
|
143
|
+
}
|
|
144
|
+
const b = g.rowB[r] + ph * len;
|
|
145
|
+
trailAt(g, M, b);
|
|
146
|
+
// The rank also keeps its own largest radius clear of obstacles on each side
|
|
147
|
+
const rad = g.rowRad[r], L = TP.L > rad ? TP.L - rad : 0, R = TP.R > rad ? TP.R - rad : 0;
|
|
148
|
+
const k = Math.min(1, (L + R) / rw), half = rw * k / 2;
|
|
149
|
+
const lo = half - L, hi = R - half;
|
|
150
|
+
const sh = lo > hi ? (R - L) / 2 : lo > 0 ? lo : hi < 0 ? hi : 0;
|
|
151
|
+
const ll = (l + dc * sp) * k, lat = sh + ll;
|
|
152
|
+
SLOT.x = TP.x + TP.lx * lat;
|
|
153
|
+
SLOT.y = TP.y + TP.ly * lat;
|
|
154
|
+
SLOT.hx = TP.ly;
|
|
155
|
+
SLOT.hy = -TP.lx;
|
|
156
|
+
SLOT.cx = TP.x + TP.lx * sh;
|
|
157
|
+
SLOT.cy = TP.y + TP.ly * sh;
|
|
158
|
+
SLOT.b = b;
|
|
159
|
+
SLOT.l = ll;
|
|
160
|
+
// Half width there: half the column's width in the open, half the usable width where that is less
|
|
161
|
+
const room = L + R < g.width ? L + R : g.width;
|
|
162
|
+
SLOT.half = Math.max(room / 2, sp / 2);
|
|
163
|
+
SLOT.v = g.v * g.rowK[r];
|
|
164
|
+
}
|
|
165
|
+
/** Result of `marchFollow`: the point to head for this step. */
|
|
166
|
+
export const FOLLOW = { x: 0, y: 0 };
|
|
167
|
+
/** What `marchFollow` returns: head for the place; close in towards the middle line and go along with the rank; neither the place nor the rank is in reach (find your own way). */
|
|
168
|
+
export const FOLLOW_SLOT = 0, FOLLOW_SQUEEZE = 1, FOLLOW_APART = 2;
|
|
169
|
+
/**
|
|
170
|
+
* Which point the member standing at (ux, uy) heads for this step, into `FOLLOW`. Its place must be in `SLOT` (from
|
|
171
|
+
* `marchSlot`).
|
|
172
|
+
* - The place can be stood on and walked to in a straight line: head for it.
|
|
173
|
+
* - The place is out of reach but the point on the rank's middle line is in sight: an obstacle has cut this side
|
|
174
|
+
* narrower, so the member closes in towards the middle line and goes along with its rank. From the middle point
|
|
175
|
+
* towards the place, the line runs up to the obstacle: that is the width left on this side, and the member stands at
|
|
176
|
+
* the same share of it as its offset is of the half width there (`SLOT.l` ÷ `SLOT.half`). The outermost member is at
|
|
177
|
+
* the obstacle and the others spread inwards, instead of all crowding at its edge. When the place itself can be
|
|
178
|
+
* stood on and reached from the middle point (only a small obstacle is in this member's way), this side is no
|
|
179
|
+
* narrower and the place stands. With the chosen point out of sight for a moment (round a corner), the member heads
|
|
180
|
+
* for the middle point first.
|
|
181
|
+
* - Not even the middle point is in sight (a wall, a building, a river in between): returns `FOLLOW_APART` and leaves
|
|
182
|
+
* `FOLLOW` alone; the member finds its own way along its route.
|
|
183
|
+
*/
|
|
184
|
+
export function marchFollow(gr, ux, uy) {
|
|
185
|
+
const px = SLOT.x, py = SLOT.y, cx = SLOT.cx, cy = SLOT.cy, l = SLOT.l;
|
|
186
|
+
if (gr.standable(px, py) && !gr.lineBlocked(ux, uy, px, py)) {
|
|
187
|
+
FOLLOW.x = px;
|
|
188
|
+
FOLLOW.y = py;
|
|
189
|
+
return FOLLOW_SLOT;
|
|
190
|
+
}
|
|
191
|
+
if (gr.lineBlocked(ux, uy, cx, cy))
|
|
192
|
+
return FOLLOW_APART;
|
|
193
|
+
const reach = gr.lineReach(cx, cy, px, py);
|
|
194
|
+
let k = reach < 1 || !gr.standable(px, py) ? reach * (l < 0 ? -l : l) / SLOT.half : 1;
|
|
195
|
+
if (k > 1)
|
|
196
|
+
k = 1;
|
|
197
|
+
const qx = cx + (px - cx) * k, qy = cy + (py - cy) * k;
|
|
198
|
+
if (gr.lineBlocked(ux, uy, qx, qy)) {
|
|
199
|
+
FOLLOW.x = cx;
|
|
200
|
+
FOLLOW.y = cy;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
FOLLOW.x = qx;
|
|
204
|
+
FOLLOW.y = qy;
|
|
205
|
+
}
|
|
206
|
+
return FOLLOW_SQUEEZE;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* While the column winds up (`g.tail`), whether the member standing at (ux, uy), with its spot at the goal at
|
|
210
|
+
* (dx, dy), should leave for the spot: its rank has passed the goal, or no narrow place lies ahead of its place and
|
|
211
|
+
* the spot is in plain sight. Its place must be in `SLOT` (from `marchSlot`).
|
|
212
|
+
*/
|
|
213
|
+
export function marchRelease(gr, g, ux, uy, dx, dy) {
|
|
214
|
+
return SLOT.b <= g.endB || (SLOT.b < g.narrowB && !gr.lineBlocked(ux, uy, dx, dy));
|
|
215
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { March, MarchGround, MarchRules } from './marchTypes.js';
|
|
2
|
+
/**
|
|
3
|
+
* Numbers per trail point: position, heading, usable width left and right (after the envelope), direction of the
|
|
4
|
+
* narrow place and where it comes from (0 none, 1 measured here, 2 carried over from a narrow place ahead or behind),
|
|
5
|
+
* direction of the cross-section, the cell the probe stood in and found passable when it recorded the point (−1 none:
|
|
6
|
+
* laid out when forming up, or coasting while winding up), and the widths measured at the point itself (for redoing
|
|
7
|
+
* the envelope on a new route).
|
|
8
|
+
*/
|
|
9
|
+
export declare const TRAIL = 14;
|
|
10
|
+
/** The most trail points kept: enough for the whole column at its longest. */
|
|
11
|
+
export declare const TRAIL_MAX = 2048;
|
|
12
|
+
/** 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). */
|
|
13
|
+
export declare const TP: {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
hx: number;
|
|
17
|
+
hy: number;
|
|
18
|
+
L: number;
|
|
19
|
+
R: number;
|
|
20
|
+
lx: number;
|
|
21
|
+
ly: number;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The point of the trail `b` behind the probe, into `TP`. A negative `b` is ahead of the probe, along its heading;
|
|
25
|
+
* a point further back than the trail is on the line through the oldest point, along its heading.
|
|
26
|
+
*/
|
|
27
|
+
export declare function trailAt(g: March, M: MarchRules, b: number): void;
|
|
28
|
+
/** The usable width (left plus right) of the trail `b` behind the probe. */
|
|
29
|
+
export declare function widthAt(g: March, M: MarchRules, b: number): number;
|
|
30
|
+
/** 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
|
+
export declare function layTrail(g: March, M: MarchRules, x: number, y: number, hx: number, hy: number): void;
|
|
32
|
+
/**
|
|
33
|
+
* Record a new trail point (x, y) ahead of the newest, with the probe's current heading; `cell` is the cell the probe
|
|
34
|
+
* found passable near it (−1 none). With `measure`, the narrow place there is measured and goes into the width
|
|
35
|
+
* envelope; a narrow place found sets its distance from the probe (`g.rec` must be up to date).
|
|
36
|
+
*/
|
|
37
|
+
export declare function addPoint(gr: MarchGround, g: March, M: MarchRules, x: number, y: number, cell: number, measure: boolean): void;
|
|
38
|
+
/**
|
|
39
|
+
* On a new route, cut the trail ahead of the front rank: the probe returns to the front rank's position and heading.
|
|
40
|
+
* The points dropped move to the old end of the ring buffer, laid out straight behind the oldest point along its
|
|
41
|
+
* heading. The points kept get their envelope redone from the widths measured at each (the narrowing first passed
|
|
42
|
+
* backwards, then limited forwards at the widening rate), narrow-place directions carried along as before.
|
|
43
|
+
*/
|
|
44
|
+
export declare function cutAhead(g: March, M: MarchRules): void;
|
|
45
|
+
/** The cell the probe walked through, as recorded at the trail point nearest the front rank; `fallback` when there is none. */
|
|
46
|
+
export declare function nearCell(g: March, M: MarchRules, fallback: number): number;
|
|
47
|
+
/** 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. */
|
|
48
|
+
export declare function routeCut(gr: MarchGround, g: March, M: MarchRules): boolean;
|