@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,282 @@
|
|
|
1
|
+
/**
|
|
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). Each point carries the usable width on each side and the
|
|
4
|
+
* direction of the cross-section there. The ranks stand along the trail, each at a distance behind the probe
|
|
5
|
+
* measured along it.
|
|
6
|
+
*
|
|
7
|
+
* Width envelope: the width measured at a point narrows gradually ahead of a narrow place (`narrowIn` per unit of
|
|
8
|
+
* travel) and widens gradually behind it (`narrowOut`); wherever a narrowing reaches, the direction of the narrow
|
|
9
|
+
* place it comes from goes with it.
|
|
10
|
+
*/
|
|
11
|
+
import { dmath, smooth } from '@pocketrts/determinism';
|
|
12
|
+
import { CHOKE, chokeAt, chokeClear } from './marchChoke.js';
|
|
13
|
+
/**
|
|
14
|
+
* Numbers per trail point: position, heading, usable width left and right (after the envelope), direction of the
|
|
15
|
+
* narrow place and where it comes from (0 none, 1 measured here, 2 carried over from a narrow place ahead or behind),
|
|
16
|
+
* direction of the cross-section, the cell the probe stood in and found passable when it recorded the point (−1 none:
|
|
17
|
+
* laid out when forming up, or coasting while winding up), and the widths measured at the point itself (for redoing
|
|
18
|
+
* the envelope on a new route).
|
|
19
|
+
*/
|
|
20
|
+
export const TRAIL = 14;
|
|
21
|
+
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;
|
|
22
|
+
/** The most trail points kept: enough for the whole column at its longest. */
|
|
23
|
+
export const TRAIL_MAX = 2048;
|
|
24
|
+
/** Index in the buffer of trail point k (0 is the newest). */
|
|
25
|
+
function pt(g, k) {
|
|
26
|
+
return ((g.head + k) % g.cap) * TRAIL;
|
|
27
|
+
}
|
|
28
|
+
/** 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). */
|
|
29
|
+
export const TP = { x: 0, y: 0, hx: 0, hy: 0, L: 0, R: 0, lx: 0, ly: 0 };
|
|
30
|
+
/**
|
|
31
|
+
* The point of the trail `b` behind the probe, into `TP`. A negative `b` is ahead of the probe, along its heading;
|
|
32
|
+
* a point further back than the trail is on the line through the oldest point, along its heading.
|
|
33
|
+
*/
|
|
34
|
+
export function trailAt(g, M, b) {
|
|
35
|
+
const t = g.trail;
|
|
36
|
+
if (b < g.rec) {
|
|
37
|
+
const o = g.head * TRAIL;
|
|
38
|
+
if (b <= 0) {
|
|
39
|
+
TP.x = g.px - g.phx * b;
|
|
40
|
+
TP.y = g.py - g.phy * b;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const f = b / g.rec;
|
|
44
|
+
TP.x = g.px + (t[o] - g.px) * f;
|
|
45
|
+
TP.y = g.py + (t[o + 1] - g.py) * f;
|
|
46
|
+
}
|
|
47
|
+
TP.hx = g.phx;
|
|
48
|
+
TP.hy = g.phy;
|
|
49
|
+
TP.L = t[o + T_L];
|
|
50
|
+
TP.R = t[o + T_R];
|
|
51
|
+
if (t[o + T_HAS]) {
|
|
52
|
+
TP.lx = t[o + T_LX];
|
|
53
|
+
TP.ly = t[o + T_LY];
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
TP.lx = -g.phy;
|
|
57
|
+
TP.ly = g.phx;
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const step = M.trailStep;
|
|
62
|
+
const s = b - g.rec, q = s / step, k = q | 0;
|
|
63
|
+
if (k >= g.cap - 1) {
|
|
64
|
+
const o = pt(g, g.cap - 1), over = s - (g.cap - 1) * step;
|
|
65
|
+
TP.hx = t[o + 2];
|
|
66
|
+
TP.hy = t[o + 3];
|
|
67
|
+
TP.x = t[o] - TP.hx * over;
|
|
68
|
+
TP.y = t[o + 1] - TP.hy * over;
|
|
69
|
+
TP.L = t[o + T_L];
|
|
70
|
+
TP.R = t[o + T_R];
|
|
71
|
+
TP.lx = t[o + T_LX];
|
|
72
|
+
TP.ly = t[o + T_LY];
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const f = q - k;
|
|
76
|
+
const o = pt(g, k), o2 = pt(g, k + 1);
|
|
77
|
+
TP.x = t[o] + (t[o2] - t[o]) * f;
|
|
78
|
+
TP.y = t[o + 1] + (t[o2 + 1] - t[o + 1]) * f;
|
|
79
|
+
TP.hx = t[o + 2] + (t[o2 + 2] - t[o + 2]) * f;
|
|
80
|
+
TP.hy = t[o + 3] + (t[o2 + 3] - t[o + 3]) * f;
|
|
81
|
+
TP.L = t[o + T_L] + (t[o2 + T_L] - t[o + T_L]) * f;
|
|
82
|
+
TP.R = t[o + T_R] + (t[o2 + T_R] - t[o + T_R]) * f;
|
|
83
|
+
TP.lx = t[o + T_LX] + (t[o2 + T_LX] - t[o + T_LX]) * f;
|
|
84
|
+
TP.ly = t[o + T_LY] + (t[o2 + T_LY] - t[o + T_LY]) * f;
|
|
85
|
+
}
|
|
86
|
+
/** The usable width (left plus right) of the trail `b` behind the probe. */
|
|
87
|
+
export function widthAt(g, M, b) {
|
|
88
|
+
const t = g.trail;
|
|
89
|
+
if (b < g.rec) {
|
|
90
|
+
const o = g.head * TRAIL;
|
|
91
|
+
return t[o + T_L] + t[o + T_R];
|
|
92
|
+
}
|
|
93
|
+
const q = (b - g.rec) / M.trailStep, k = q | 0;
|
|
94
|
+
if (k >= g.cap - 1) {
|
|
95
|
+
const o = pt(g, g.cap - 1);
|
|
96
|
+
return t[o + T_L] + t[o + T_R];
|
|
97
|
+
}
|
|
98
|
+
const f = q - k;
|
|
99
|
+
const o = pt(g, k), o2 = pt(g, k + 1);
|
|
100
|
+
const w0 = t[o + T_L] + t[o + T_R];
|
|
101
|
+
return w0 + (t[o2 + T_L] + t[o2 + T_R] - w0) * f;
|
|
102
|
+
}
|
|
103
|
+
/** 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
|
+
export function layTrail(g, M, x, y, hx, hy) {
|
|
105
|
+
const st = M.trailStep, t = g.trail, open = g.open;
|
|
106
|
+
for (let k = 0; k < g.cap; k++) {
|
|
107
|
+
const o = k * TRAIL;
|
|
108
|
+
t[o] = x - hx * st * k;
|
|
109
|
+
t[o + 1] = y - hy * st * k;
|
|
110
|
+
t[o + 2] = hx;
|
|
111
|
+
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;
|
|
114
|
+
t[o + T_LX] = -hy;
|
|
115
|
+
t[o + T_LY] = hx;
|
|
116
|
+
t[o + T_CELL] = -1;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Record a new trail point (x, y) ahead of the newest, with the probe's current heading; `cell` is the cell the probe
|
|
121
|
+
* found passable near it (−1 none). With `measure`, the narrow place there is measured and goes into the width
|
|
122
|
+
* envelope; a narrow place found sets its distance from the probe (`g.rec` must be up to date).
|
|
123
|
+
*/
|
|
124
|
+
export function addPoint(gr, g, M, x, y, cell, measure) {
|
|
125
|
+
const t = g.trail;
|
|
126
|
+
const prev = g.head * TRAIL;
|
|
127
|
+
g.head = (g.head + g.cap - 1) % g.cap;
|
|
128
|
+
const o = g.head * TRAIL;
|
|
129
|
+
t[o] = x;
|
|
130
|
+
t[o + 1] = y;
|
|
131
|
+
t[o + 2] = g.phx;
|
|
132
|
+
t[o + 3] = g.phy;
|
|
133
|
+
t[o + T_CELL] = cell;
|
|
134
|
+
if (measure) {
|
|
135
|
+
let tx = x - t[prev], ty = y - t[prev + 1];
|
|
136
|
+
const tl = dmath.hypot(tx, ty);
|
|
137
|
+
if (tl > 1e-3) {
|
|
138
|
+
tx /= tl;
|
|
139
|
+
ty /= tl;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
tx = g.phx;
|
|
143
|
+
ty = g.phy;
|
|
144
|
+
}
|
|
145
|
+
chokeAt(gr, g, M, x, y, tx, ty);
|
|
146
|
+
}
|
|
147
|
+
else
|
|
148
|
+
chokeClear(g);
|
|
149
|
+
if (CHOKE.L + CHOKE.R < g.width * M.narrow)
|
|
150
|
+
g.narrowB = g.rec;
|
|
151
|
+
envelope(g, M, o, prev);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The cross-section's direction at trail point o: square to the heading in the open; it starts turning towards the
|
|
155
|
+
* direction of the narrow place when the usable width drops below `axisFrom` of the column's width, and follows it
|
|
156
|
+
* fully at `axisFull`.
|
|
157
|
+
*/
|
|
158
|
+
function crossAt(g, M, o) {
|
|
159
|
+
const t = g.trail, hlx = -t[o + 3], hly = t[o + 2];
|
|
160
|
+
if (!t[o + T_HAS]) {
|
|
161
|
+
t[o + T_LX] = hlx;
|
|
162
|
+
t[o + T_LY] = hly;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
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
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Keep the widths of trail point o within those of its neighbour `src` plus `slope`. Where that lowers one and o has
|
|
175
|
+
* no narrow-place direction of its own, it takes over `src`'s. Returns whether anything was lowered.
|
|
176
|
+
*/
|
|
177
|
+
function pull(t, o, src, slope) {
|
|
178
|
+
let hit = false;
|
|
179
|
+
if (t[src + T_L] + slope < t[o + T_L]) {
|
|
180
|
+
t[o + T_L] = t[src + T_L] + slope;
|
|
181
|
+
hit = true;
|
|
182
|
+
}
|
|
183
|
+
if (t[src + T_R] + slope < t[o + T_R]) {
|
|
184
|
+
t[o + T_R] = t[src + T_R] + slope;
|
|
185
|
+
hit = true;
|
|
186
|
+
}
|
|
187
|
+
if (hit && t[src + T_HAS] && !t[o + T_HAS]) {
|
|
188
|
+
t[o + T_AX] = t[src + T_AX];
|
|
189
|
+
t[o + T_AY] = t[src + T_AY];
|
|
190
|
+
t[o + T_HAS] = 2;
|
|
191
|
+
}
|
|
192
|
+
return hit;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Take the trail point o just recorded (`CHOKE` holds what was measured there) into the width envelope: the new point
|
|
196
|
+
* is first limited by the point behind it (`prev`) at the widening rate, then its narrowing is passed back along the
|
|
197
|
+
* trail until nothing gets narrower.
|
|
198
|
+
*/
|
|
199
|
+
function envelope(g, M, o, prev) {
|
|
200
|
+
const t = g.trail, st = M.trailStep;
|
|
201
|
+
t[o + T_RL] = t[o + T_L] = CHOKE.L;
|
|
202
|
+
t[o + T_RR] = t[o + T_R] = CHOKE.R;
|
|
203
|
+
t[o + T_HAS] = CHOKE.has;
|
|
204
|
+
if (CHOKE.has) {
|
|
205
|
+
t[o + T_AX] = CHOKE.ax;
|
|
206
|
+
t[o + T_AY] = CHOKE.ay;
|
|
207
|
+
}
|
|
208
|
+
pull(t, o, prev, M.narrowOut * st);
|
|
209
|
+
crossAt(g, M, o);
|
|
210
|
+
const si = M.narrowIn * st;
|
|
211
|
+
for (let n = 1, j = o; n < g.cap; n++) {
|
|
212
|
+
const k = pt(g, n);
|
|
213
|
+
if (!pull(t, k, j, si))
|
|
214
|
+
break;
|
|
215
|
+
crossAt(g, M, k);
|
|
216
|
+
j = k;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* On a new route, cut the trail ahead of the front rank: the probe returns to the front rank's position and heading.
|
|
221
|
+
* The points dropped move to the old end of the ring buffer, laid out straight behind the oldest point along its
|
|
222
|
+
* heading. The points kept get their envelope redone from the widths measured at each (the narrowing first passed
|
|
223
|
+
* backwards, then limited forwards at the widening rate), narrow-place directions carried along as before.
|
|
224
|
+
*/
|
|
225
|
+
export function cutAhead(g, M) {
|
|
226
|
+
const st = M.trailStep, t = g.trail, cap = g.cap;
|
|
227
|
+
const k0 = g.lead <= g.rec ? 0 : Math.min(cap - 1, Math.ceil((g.lead - g.rec) / st - 1e-9));
|
|
228
|
+
const oldTail = pt(g, cap - 1);
|
|
229
|
+
const tx = t[oldTail], ty = t[oldTail + 1], thx = t[oldTail + 2], thy = t[oldTail + 3];
|
|
230
|
+
// The points dropped (the k0 before the new head) become the oldest: laid out behind what was the oldest point
|
|
231
|
+
for (let j = 0; j < k0; j++) {
|
|
232
|
+
const o = pt(g, j), back = (j + 1) * st;
|
|
233
|
+
t[o] = tx - thx * back;
|
|
234
|
+
t[o + 1] = ty - thy * back;
|
|
235
|
+
t[o + 2] = thx;
|
|
236
|
+
t[o + 3] = thy;
|
|
237
|
+
t[o + T_RL] = t[o + T_RR] = g.open;
|
|
238
|
+
t[o + T_HAS] = 0;
|
|
239
|
+
t[o + T_CELL] = -1;
|
|
240
|
+
}
|
|
241
|
+
g.rec = g.rec + k0 * st - g.lead;
|
|
242
|
+
g.head = (g.head + k0) % cap;
|
|
243
|
+
const hl = dmath.hypot(g.hx, g.hy) || 1;
|
|
244
|
+
Object.assign(g, { px: g.x, py: g.y, phx: g.hx / hl, phy: g.hy / hl, lead: 0, pDone: false, pBarred: false, blocked: false, endB: Infinity, narrowB: Infinity });
|
|
245
|
+
// Back to the widths measured at each point, keeping only the narrow-place directions measured on the spot
|
|
246
|
+
for (let k = 0; k < cap; k++) {
|
|
247
|
+
const o = pt(g, k);
|
|
248
|
+
t[o + T_L] = t[o + T_RL];
|
|
249
|
+
t[o + T_R] = t[o + T_RR];
|
|
250
|
+
if (t[o + T_HAS] !== 1)
|
|
251
|
+
t[o + T_HAS] = 0;
|
|
252
|
+
if (g.narrowB === Infinity && t[o + T_RL] + t[o + T_RR] < g.width * M.narrow)
|
|
253
|
+
g.narrowB = g.rec + k * st;
|
|
254
|
+
}
|
|
255
|
+
const si = M.narrowIn * st, so = M.narrowOut * st;
|
|
256
|
+
for (let n = 1; n < cap; n++)
|
|
257
|
+
pull(t, pt(g, n), pt(g, n - 1), si);
|
|
258
|
+
for (let k = cap - 2; k >= 0; k--)
|
|
259
|
+
pull(t, pt(g, k), pt(g, k + 1), so);
|
|
260
|
+
for (let k = 0; k < cap; k++)
|
|
261
|
+
crossAt(g, M, pt(g, k));
|
|
262
|
+
}
|
|
263
|
+
/** The cell the probe walked through, as recorded at the trail point nearest the front rank; `fallback` when there is none. */
|
|
264
|
+
export function nearCell(g, M, fallback) {
|
|
265
|
+
if (g.lead < g.rec)
|
|
266
|
+
return fallback;
|
|
267
|
+
let k = Math.round((g.lead - g.rec) / M.trailStep);
|
|
268
|
+
if (k > g.cap - 1)
|
|
269
|
+
k = g.cap - 1;
|
|
270
|
+
const c = g.trail[pt(g, k) + T_CELL];
|
|
271
|
+
return c >= 0 ? c : fallback;
|
|
272
|
+
}
|
|
273
|
+
/** 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. */
|
|
274
|
+
export function routeCut(gr, g, M) {
|
|
275
|
+
if (g.lead < g.rec)
|
|
276
|
+
return false;
|
|
277
|
+
const k = ((g.lead - g.rec) / M.trailStep) | 0;
|
|
278
|
+
if (k >= g.cap)
|
|
279
|
+
return false;
|
|
280
|
+
const c = g.trail[pt(g, k) + T_CELL];
|
|
281
|
+
return c >= 0 && gr.solid(c);
|
|
282
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Column march: what the game provides (its ground, its members, its rule values) and the state of one marching group.
|
|
3
|
+
*
|
|
4
|
+
* A group ordered somewhere together marches as an elastic column. An invisible "probe" walks ahead along the route a
|
|
5
|
+
* single unit would take and records where it went (the "trail"). The front rank follows the trail a stretch behind
|
|
6
|
+
* the probe, and the ranks behind it follow one after another, so the column bends round corners like a snake. Ahead
|
|
7
|
+
* of a place narrower than the column (a gate, a bridge) the ranks close up and fold into files, and spread out again
|
|
8
|
+
* behind it.
|
|
9
|
+
*
|
|
10
|
+
* The march system knows nothing about the game: no sides, buildings, combat or weather. The game describes its map
|
|
11
|
+
* through `MarchGround`, its units through `MarchMembers`, and tunes behaviour through `MarchRules`.
|
|
12
|
+
*
|
|
13
|
+
* Scale: the default rule values suit a world where a cell is about 32 units, a member about 12 across and speeds a
|
|
14
|
+
* few dozen units per second. For a world of another scale take the rules from `marchRules(scale)`.
|
|
15
|
+
*/
|
|
16
|
+
import type { FlowField } from './flowfield.js';
|
|
17
|
+
/** Rule values of the march system; distances are world units, speeds world units per second, angles radians. */
|
|
18
|
+
export interface MarchRules {
|
|
19
|
+
/**
|
|
20
|
+
* The size of the world relative to the one the defaults were made for (see `marchRules`). The march system's
|
|
21
|
+
* built-in tolerances are multiplied by it: the probe walks in sub-steps of 2 × `scale`, rank lengths are worked
|
|
22
|
+
* out in pieces of 4 × `scale`, and "there" means within `scale`. Default 1.
|
|
23
|
+
*/
|
|
24
|
+
scale: number;
|
|
25
|
+
/** The front rank moves at full pace while members are on average within `lagFull` of their places, and waits once they are `lagStop` away. Defaults 30, 110. */
|
|
26
|
+
lagFull: number;
|
|
27
|
+
lagStop: number;
|
|
28
|
+
/** The most one member counts for in that average. Default 400. */
|
|
29
|
+
lagCap: number;
|
|
30
|
+
/** A member whose place has dropped this far behind it (it got ahead on a detour) stands and waits instead of walking back. Default 14. */
|
|
31
|
+
waitAhead: number;
|
|
32
|
+
/** The most the probe's heading (the direction the ranks face) turns per second. Default 1.4. */
|
|
33
|
+
turn: number;
|
|
34
|
+
/** The column's turning radius is half its width, and at least this; unlimited within one width of the goal. Default 40. */
|
|
35
|
+
turnR: number;
|
|
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
|
+
slide: number;
|
|
38
|
+
/** How far the probe looks along the route to straighten it: width × `lookK`, at least `lookMin`. Defaults 0.75, 96. */
|
|
39
|
+
lookK: number;
|
|
40
|
+
lookMin: number;
|
|
41
|
+
/** The probe leads the front rank by the distance the ranks need to narrow from full width to nothing, plus this. Default 48. */
|
|
42
|
+
probePad: number;
|
|
43
|
+
/** A front rank that holds on engagement stops while more than this share of the members are engaged. Default 0.15. */
|
|
44
|
+
holdEngaged: number;
|
|
45
|
+
/** Distance between trail points. Default 12. */
|
|
46
|
+
trailStep: number;
|
|
47
|
+
/** With more than this share of places (and more than one) in or behind obstacles, the column counts as draped over an obstacle: the front rank then does not wait for members finding their own way. Default 0.03. */
|
|
48
|
+
overMin: number;
|
|
49
|
+
/** The usable width on each side shrinks at most `narrowIn` per unit of travel towards a narrow place and grows at most `narrowOut` per unit past it. Defaults 0.55, 1. */
|
|
50
|
+
narrowIn: number;
|
|
51
|
+
narrowOut: number;
|
|
52
|
+
/** Clearance kept from the obstacles of a narrow place, on each side; each rank also keeps its largest member radius. Default 1. */
|
|
53
|
+
chokePad: number;
|
|
54
|
+
/** The obstacle on each side of a narrow place must run at least this far along the cross-section. Default 32. */
|
|
55
|
+
chokeMin: number;
|
|
56
|
+
/** A place counts as narrow when its usable widths add up to less than this share of the column's width. Default 0.95. */
|
|
57
|
+
narrow: number;
|
|
58
|
+
/** The most a rank stretches along the trail in a narrow place; narrower still, its members close up sideways. Default 4. */
|
|
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 usable width drops below `axisFrom` of the column's width, and follows it fully at `axisFull`. Defaults 0.75, 0.5. */
|
|
61
|
+
axisFrom: number;
|
|
62
|
+
axisFull: number;
|
|
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
|
+
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. */
|
|
66
|
+
trot: number;
|
|
67
|
+
/** A member that finds its own way while its place is behind it moves at its rank's pace, and at least this share of its own speed. Default 0.3. */
|
|
68
|
+
apartTrail: number;
|
|
69
|
+
/** 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. */
|
|
70
|
+
rerouteCos: number;
|
|
71
|
+
/** After the front rank arrives, the column keeps leading ranks through a narrow place for at most this many seconds. Default 20. */
|
|
72
|
+
tailMax: number;
|
|
73
|
+
/** A group counts as opposed for this many seconds after a member last had an opponent. Default 5. */
|
|
74
|
+
opposedLinger: number;
|
|
75
|
+
/** A member aims at where its place will be this many seconds from now, so it settles at its rank's pace. Default 0.4. */
|
|
76
|
+
leadTime: number;
|
|
77
|
+
/**
|
|
78
|
+
* Also keep clear of an obstacle on one side of the route only, and of obstacles too thin to block a whole wing:
|
|
79
|
+
* the ranks move over to the open side, or close up between two such obstacles; a wall that runs on for half the
|
|
80
|
+
* column's width or more makes a narrow place however thin it is, so a corridor between thin walls is measured
|
|
81
|
+
* straight across; and where the way is narrower than the column, the column turns as tightly as half the way's
|
|
82
|
+
* width instead of half its own. Default false: only a place with a large obstacle on both sides narrows the
|
|
83
|
+
* column; elsewhere members whose places are cut off close in towards their rank's middle on their own, which
|
|
84
|
+
* crowds them when the route runs right along a wall.
|
|
85
|
+
*/
|
|
86
|
+
flank: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Form up along the route: facing the way it sets off (the furthest point in sight along it), the rear rank as far
|
|
89
|
+
* behind the group's centre as there is room for (at most half the column's depth), and the ranks ahead of it
|
|
90
|
+
* along the way the probe walks. A route still being searched is finished on the spot for it, whatever the flow
|
|
91
|
+
* fields' budget. Default false: the column forms up in a straight line round the centre, facing the goal; a
|
|
92
|
+
* column whose route sets off round an obstacle then wheels onto it as it sets off, and one that forms up in a
|
|
93
|
+
* cramped place has ranks whose places are in or behind a wall.
|
|
94
|
+
*/
|
|
95
|
+
faceRoute: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Wheel in turn: on a bend each rank moves at the pace at which its outermost place keeps the column's speed, as
|
|
98
|
+
* the inner flank of a wheeling line shortens its step for the outer one. Along the trail the ranks on the bend
|
|
99
|
+
* close up; the ranks before it march up at the column's pace and the ranks past it march on at it. The front
|
|
100
|
+
* rank slows only while it is on a bend itself. Default false: every rank keeps the column's pace along the trail
|
|
101
|
+
* through a bend, the members on the outside of it fall behind their places and catch up on the next straight
|
|
102
|
+
* stretch.
|
|
103
|
+
*/
|
|
104
|
+
wheel: boolean;
|
|
105
|
+
/**
|
|
106
|
+
* When straightening the route, prefer a point the walk to which is clear this share of the column's width to
|
|
107
|
+
* each side (with `flank`, of the usable width where that is less) to a further one that is only just in sight
|
|
108
|
+
* past a corner. For routes that keep their distance from walls (a game that charges for ground near obstacles):
|
|
109
|
+
* straightened by sight alone they would be drawn back to the corners they go round. Default 0: the furthest
|
|
110
|
+
* point in sight.
|
|
111
|
+
*/
|
|
112
|
+
lookClear: number;
|
|
113
|
+
}
|
|
114
|
+
export declare const DEFAULT_MARCH_RULES: Readonly<MarchRules>;
|
|
115
|
+
/**
|
|
116
|
+
* The default rules for a world `scale` times the size of the one they were made for (cells of about 32 units,
|
|
117
|
+
* members about 12 across): every rule that is a distance is multiplied by `scale`, and so are the built-in
|
|
118
|
+
* tolerances. For cells of 16 units, `marchRules(0.5)`; where a unit is a cell, `marchRules(1 / 32)`. Speeds are the
|
|
119
|
+
* game's own (it passes them to `createMarch` and `steerMember`), so no rule holds one. `lineFormation`'s `gap` and
|
|
120
|
+
* `minWidth` are lengths made for the same world: scale them by the same factor.
|
|
121
|
+
* @param overrides Rule values to use instead, as given (not scaled).
|
|
122
|
+
*/
|
|
123
|
+
export declare function marchRules(scale: number, overrides?: Partial<Omit<MarchRules, 'scale'>>): MarchRules;
|
|
124
|
+
/**
|
|
125
|
+
* The map as one group of marchers sees it. A game with several movement layers (sides with different gates, say)
|
|
126
|
+
* gives one ground per layer. Cells are numbered row-major, `cell = y × w + x`.
|
|
127
|
+
*/
|
|
128
|
+
export interface MarchGround {
|
|
129
|
+
/** Grid size in cells, and world units per cell. */
|
|
130
|
+
readonly w: number;
|
|
131
|
+
readonly h: number;
|
|
132
|
+
readonly cellSize: number;
|
|
133
|
+
/** The current simulation step (flow fields are marked as used on it). */
|
|
134
|
+
readonly tick: number;
|
|
135
|
+
/** The cell holding a world position. */
|
|
136
|
+
cellAt(x: number, y: number): number;
|
|
137
|
+
/**
|
|
138
|
+
* Nobody can stand in the cell: narrow places are measured against solid cells, and the column never starts on
|
|
139
|
+
* one. Every blocked cell is solid; a game may have solid cells that can be walked over (a wall top, say).
|
|
140
|
+
*/
|
|
141
|
+
solid(cell: number): boolean;
|
|
142
|
+
/** The cell cannot be walked into: the probe never steps onto it. */
|
|
143
|
+
blocked(cell: number): boolean;
|
|
144
|
+
/** Whether a member may step from a cell to a neighbouring one (both passable). */
|
|
145
|
+
canStep(from: number, to: number): boolean;
|
|
146
|
+
/** The cell is closed by something that may open or go away (a gate, a barricade): the column waits in front of it instead of taking it for a wall. */
|
|
147
|
+
barred(cell: number): boolean;
|
|
148
|
+
/** The nearest cell to `cell` a member can stand in, within `maxR` cells. */
|
|
149
|
+
nearestFree(cell: number, maxR: number): number;
|
|
150
|
+
/** Whether the straight walk between two points is blocked, and how much of it is clear (1 = all of it). */
|
|
151
|
+
lineBlocked(ax: number, ay: number, bx: number, by: number): boolean;
|
|
152
|
+
lineReach(ax: number, ay: number, bx: number, by: number): number;
|
|
153
|
+
/** Whether a member can stand at a point (inside the map, on a passable cell). */
|
|
154
|
+
standable(x: number, y: number): boolean;
|
|
155
|
+
/** The flow field towards a cell, and finishing its search at once. */
|
|
156
|
+
field(cell: number): FlowField;
|
|
157
|
+
finish(field: FlowField): void;
|
|
158
|
+
/** How fast the ground of a cell is walked, as a factor of normal speed; 0 or less where it cannot be walked. */
|
|
159
|
+
speedAt(cell: number): number;
|
|
160
|
+
/** A factor on every member's speed this step (weather, say). */
|
|
161
|
+
readonly speedScale: number;
|
|
162
|
+
/** A factor on the speed of anything moving in direction (hx, hy) this step (wind, say). */
|
|
163
|
+
headingScale(hx: number, hy: number): number;
|
|
164
|
+
}
|
|
165
|
+
/** A member's condition, as `MarchMembers.state` reports it, is any of the `MEMBER_…` flags. This one: out of action for good (fleeing, say); the member leaves its march at the next tally. */
|
|
166
|
+
export declare const MEMBER_OUT = 1;
|
|
167
|
+
/** Occupied with something other than marching. */
|
|
168
|
+
export declare const MEMBER_ENGAGED = 2;
|
|
169
|
+
/** Engaged with an opponent that is still fighting; counts only together with `MEMBER_ENGAGED`. */
|
|
170
|
+
export declare const MEMBER_OPPOSED = 4;
|
|
171
|
+
/** Not keeping its place for now (acting on its own). */
|
|
172
|
+
export declare const MEMBER_FREE = 8;
|
|
173
|
+
/** The game's units, as far as the march system needs them. Members are numbered 0 … n − 1. */
|
|
174
|
+
export interface MarchMembers {
|
|
175
|
+
readonly n: number;
|
|
176
|
+
alive(i: number): boolean;
|
|
177
|
+
/** The march member i belongs to (index into the marches list), or −1. */
|
|
178
|
+
march(i: number): number;
|
|
179
|
+
/** Take member i out of its march. */
|
|
180
|
+
leave(i: number): void;
|
|
181
|
+
x(i: number): number;
|
|
182
|
+
y(i: number): number;
|
|
183
|
+
/** The member's rank and place in the rank (`Formation.row`, `Formation.col`), and its lateral offset (`Formation.fl`). */
|
|
184
|
+
row(i: number): number;
|
|
185
|
+
col(i: number): number;
|
|
186
|
+
lateral(i: number): number;
|
|
187
|
+
/** `STEER.apart` from this step's `steerMember` call for the member; false on a step it was not steered. */
|
|
188
|
+
apart(i: number): boolean;
|
|
189
|
+
/** `MEMBER_…` flags; `MEMBER_OUT` takes the member out of its march. */
|
|
190
|
+
state(i: number): number;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* One marching group. The game reads it (where the front rank is, whether it has arrived, …); only the march functions
|
|
194
|
+
* write it. Objects are plain and reused: a march whose `alive` is false is handed out again by `createMarch`.
|
|
195
|
+
*/
|
|
196
|
+
export interface March {
|
|
197
|
+
/** Its index in the marches list. */
|
|
198
|
+
id: number;
|
|
199
|
+
alive: boolean;
|
|
200
|
+
/** The movement layer whose ground the group marches on. */
|
|
201
|
+
layer: number;
|
|
202
|
+
/** The front rank's position and heading (a unit vector): the point of the trail `lead` behind the probe. */
|
|
203
|
+
x: number;
|
|
204
|
+
y: number;
|
|
205
|
+
hx: number;
|
|
206
|
+
hy: number;
|
|
207
|
+
/** The probe's position and heading (the direction the ranks face; it may walk a little off it). */
|
|
208
|
+
px: number;
|
|
209
|
+
py: number;
|
|
210
|
+
phx: number;
|
|
211
|
+
phy: number;
|
|
212
|
+
/** The probe has reached the goal. */
|
|
213
|
+
pDone: boolean;
|
|
214
|
+
/** The probe stands in front of a barred cell. */
|
|
215
|
+
pBarred: boolean;
|
|
216
|
+
/** How far along the trail the front rank is behind the probe, and how far the probe should lead. */
|
|
217
|
+
lead: number;
|
|
218
|
+
ahead: number;
|
|
219
|
+
destX: number;
|
|
220
|
+
destY: number;
|
|
221
|
+
destCell: number;
|
|
222
|
+
/** The flow field towards the goal, or null while the straight line there is clear. */
|
|
223
|
+
field: FlowField | null;
|
|
224
|
+
/** The slowest member's marching speed and full speed. */
|
|
225
|
+
speed: number;
|
|
226
|
+
run: number;
|
|
227
|
+
/** The front rank halts while enough members are engaged, and moves on afterwards. */
|
|
228
|
+
hold: boolean;
|
|
229
|
+
/** The share of members engaged, and of members not keeping their place. */
|
|
230
|
+
engaged: number;
|
|
231
|
+
free: number;
|
|
232
|
+
cEngaged: number;
|
|
233
|
+
cFree: number;
|
|
234
|
+
/** The front rank's current speed (0 while waiting). */
|
|
235
|
+
v: number;
|
|
236
|
+
/** The facing at the goal. */
|
|
237
|
+
fx: number;
|
|
238
|
+
fy: number;
|
|
239
|
+
arrived: boolean;
|
|
240
|
+
/** The front rank has reached the probe waiting in front of a barred cell: no places are kept and the members deal with it on their own. Once it is open the probe walks on and they take their places again. */
|
|
241
|
+
blocked: boolean;
|
|
242
|
+
/** The group is not marching in ranks for now (held by engagement, or most members acting on their own); it forms up again afterwards. */
|
|
243
|
+
loose: boolean;
|
|
244
|
+
/** Seconds the group still counts as opposed; while positive, members whose place is behind them walk on instead of waiting. */
|
|
245
|
+
opposedT: number;
|
|
246
|
+
/** Members with an opponent (counted every 8 steps). */
|
|
247
|
+
cOpposed: number;
|
|
248
|
+
/** Member count and average lag (counted every 8 steps). */
|
|
249
|
+
n: number;
|
|
250
|
+
lag: number;
|
|
251
|
+
cnt: number;
|
|
252
|
+
lagSum: number;
|
|
253
|
+
/** The lag of members finding their own way: left out of `lag` while the column is draped over an obstacle. */
|
|
254
|
+
lagApart: number;
|
|
255
|
+
/** Places in or behind obstacles (counted every 8 steps). */
|
|
256
|
+
over: number;
|
|
257
|
+
/** Sum of member positions (for the centre of the group). */
|
|
258
|
+
sx: number;
|
|
259
|
+
sy: number;
|
|
260
|
+
/** The trail: a ring buffer of `cap` points `TRAIL` numbers each, `head` the newest, `rec` its distance to the probe. */
|
|
261
|
+
trail: Float64Array;
|
|
262
|
+
cap: number;
|
|
263
|
+
head: number;
|
|
264
|
+
rec: number;
|
|
265
|
+
/** The column's width and depth when not folded. */
|
|
266
|
+
width: number;
|
|
267
|
+
depth: number;
|
|
268
|
+
/** The usable width recorded on each side where nothing was measured: half the width plus the largest member radius (with `MarchRules.flank`, the whole width plus that radius, so a rank has room to move over). */
|
|
269
|
+
open: number;
|
|
270
|
+
/** Distance from the probe to the nearest narrow place on the trail (Infinity = none). */
|
|
271
|
+
narrowB: number;
|
|
272
|
+
/** Winding up: after the front rank arrived, the column keeps leading ranks through a narrow place. `endB` is the goal's distance from the probe along the trail, `tailT` how long the winding up has lasted. */
|
|
273
|
+
tail: boolean;
|
|
274
|
+
endB: number;
|
|
275
|
+
tailT: number;
|
|
276
|
+
/** Rank count; per rank: offset behind the front rank, width, depth, average spacing, largest radius, slowest full speed. */
|
|
277
|
+
rows: number;
|
|
278
|
+
rowA: Float32Array;
|
|
279
|
+
rowW: Float64Array;
|
|
280
|
+
rowD: Float64Array;
|
|
281
|
+
rowSp: Float64Array;
|
|
282
|
+
rowRad: Float64Array;
|
|
283
|
+
rowRun: Float64Array;
|
|
284
|
+
/** 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 moves per unit the front rank moves. */
|
|
285
|
+
rowB: Float64Array;
|
|
286
|
+
rowLen: Float64Array;
|
|
287
|
+
rowE: Float64Array;
|
|
288
|
+
rowK: Float64Array;
|
|
289
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const DEFAULT_MARCH_RULES = Object.freeze({
|
|
2
|
+
scale: 1,
|
|
3
|
+
lagFull: 30, lagStop: 110, lagCap: 400, waitAhead: 14, turn: 1.4, turnR: 40, slide: 0.6, lookK: 0.75, lookMin: 96, probePad: 48,
|
|
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.75, axisFull: 0.5, foldEase: 0.3, trot: 0.88, apartTrail: 0.3, rerouteCos: 0, tailMax: 20,
|
|
6
|
+
opposedLinger: 5, leadTime: 0.4, flank: false, faceRoute: false, wheel: false, lookClear: 0,
|
|
7
|
+
});
|
|
8
|
+
/** The rules that are distances: the ones `marchRules` scales. */
|
|
9
|
+
const LENGTHS = ['lagFull', 'lagStop', 'lagCap', 'waitAhead', 'turnR', 'lookMin', 'probePad', 'trailStep', 'chokePad', 'chokeMin'];
|
|
10
|
+
/**
|
|
11
|
+
* The default rules for a world `scale` times the size of the one they were made for (cells of about 32 units,
|
|
12
|
+
* members about 12 across): every rule that is a distance is multiplied by `scale`, and so are the built-in
|
|
13
|
+
* tolerances. For cells of 16 units, `marchRules(0.5)`; where a unit is a cell, `marchRules(1 / 32)`. Speeds are the
|
|
14
|
+
* game's own (it passes them to `createMarch` and `steerMember`), so no rule holds one. `lineFormation`'s `gap` and
|
|
15
|
+
* `minWidth` are lengths made for the same world: scale them by the same factor.
|
|
16
|
+
* @param overrides Rule values to use instead, as given (not scaled).
|
|
17
|
+
*/
|
|
18
|
+
export function marchRules(scale, overrides = {}) {
|
|
19
|
+
const rules = { ...DEFAULT_MARCH_RULES, scale };
|
|
20
|
+
for (const k of LENGTHS)
|
|
21
|
+
rules[k] *= scale;
|
|
22
|
+
return { ...rules, ...overrides };
|
|
23
|
+
}
|
|
24
|
+
/** A member's condition, as `MarchMembers.state` reports it, is any of the `MEMBER_…` flags. This one: out of action for good (fleeing, say); the member leaves its march at the next tally. */
|
|
25
|
+
export const MEMBER_OUT = 1;
|
|
26
|
+
/** Occupied with something other than marching. */
|
|
27
|
+
export const MEMBER_ENGAGED = 2;
|
|
28
|
+
/** Engaged with an opponent that is still fighting; counts only together with `MEMBER_ENGAGED`. */
|
|
29
|
+
export const MEMBER_OPPOSED = 4;
|
|
30
|
+
/** Not keeping its place for now (acting on its own). */
|
|
31
|
+
export const MEMBER_FREE = 8;
|
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*, line reach, distance fields, bucket grids, field of view, polygons, cell chains and
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Grid algorithms for RTS games: budgeted flow fields, bounded local A*, line reach, distance fields, bucket grids, field of view, polygons, cell chains, line formations and column march",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Junjie Bai <bjj19851010@gmail.com>",
|
|
7
7
|
"repository": {
|