@pocketrts/grid 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +146 -2
- package/dist/buckets.d.ts +32 -0
- package/dist/buckets.js +82 -0
- package/dist/cells.d.ts +21 -0
- package/dist/cells.js +108 -0
- package/dist/chain.d.ts +27 -0
- package/dist/chain.js +112 -0
- package/dist/dirs.d.ts +7 -0
- package/dist/dirs.js +7 -0
- package/dist/flowfield.d.ts +85 -0
- package/dist/flowfield.js +214 -0
- package/dist/formation.d.ts +74 -0
- package/dist/formation.js +143 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +16 -0
- package/dist/localpath.d.ts +35 -0
- package/dist/localpath.js +145 -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/dist/polygon.d.ts +4 -0
- package/dist/polygon.js +25 -0
- package/dist/vision.d.ts +11 -0
- package/dist/vision.js +30 -0
- package/package.json +45 -3
package/dist/march.js
ADDED
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Column march: setting a group marching, forming up again, turning onto a new route, how the probe walks, how fast
|
|
3
|
+
* and how far the front rank moves each step, the tally of the members, and what each member does.
|
|
4
|
+
*
|
|
5
|
+
* The probe takes the route a single unit would take; it does not pick a wider way because the column is wide.
|
|
6
|
+
* Charging for ground near obstacles would bend the route and make the whole block turn with it, so the column goes
|
|
7
|
+
* straight and closes up where the way is narrow.
|
|
8
|
+
*
|
|
9
|
+
* Results are deterministic: the same calls with the same inputs give the same bits.
|
|
10
|
+
*/
|
|
11
|
+
import { dmath, smooth } from '@pocketrts/determinism';
|
|
12
|
+
import { DIR_HERE, DIRX, DIRY } from './dirs.js';
|
|
13
|
+
import { FOLLOW, FOLLOW_APART, FOLLOW_SLOT, SLOT, marchFollow, marchRelease, marchSlot, rowsAt } from './marchSlots.js';
|
|
14
|
+
import { TP, TRAIL, TRAIL_MAX, addPoint, cutAhead, layTrail, nearCell, routeCut, trailAt, widthAt } from './marchTrail.js';
|
|
15
|
+
import { MEMBER_ENGAGED, MEMBER_FREE, MEMBER_OPPOSED, MEMBER_OUT, } from './marchTypes.js';
|
|
16
|
+
/** 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. */
|
|
17
|
+
const PROBE_SUB = 2;
|
|
18
|
+
/** When the front rank arrives, a narrow place within this many trail steps behind the last rank's rear still counts as not passed by every rank. */
|
|
19
|
+
const TAIL_SLACK = 2;
|
|
20
|
+
/** The members are tallied every this many steps (a power of two). */
|
|
21
|
+
const TALLY = 8;
|
|
22
|
+
/** Scratch results of `rowsAt` (for measuring each rank's speed). */
|
|
23
|
+
let TB = new Float64Array(64), TL = new Float64Array(64), TE = new Float64Array(64), TW = new Float64Array(64);
|
|
24
|
+
/**
|
|
25
|
+
* Set a group marching. The march is appended to `marches`, or reuses the first one in it that is no longer alive
|
|
26
|
+
* (so its `id` is its index there). Whether a group is compact enough to march together is the game's call, before
|
|
27
|
+
* it asks.
|
|
28
|
+
*/
|
|
29
|
+
export function createMarch(marches, gr, M, order) {
|
|
30
|
+
const { layer, x, y, destX, destY, destCell, speed, run, hold, fx, fy, formation: f } = order;
|
|
31
|
+
const n = f.row.length;
|
|
32
|
+
let g = marches.find(m => !m.alive);
|
|
33
|
+
if (!g) {
|
|
34
|
+
const e = new Float64Array(0);
|
|
35
|
+
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, rowK: e };
|
|
36
|
+
marches.push(g);
|
|
37
|
+
}
|
|
38
|
+
const width = f.width, depth = f.depth, rows = f.rowW.length;
|
|
39
|
+
const ahead = width / 2 / M.narrowIn + M.probePad;
|
|
40
|
+
// The trail must hold the column stretched to `stretchMax` times its depth, plus the probe's lead
|
|
41
|
+
const stretched = depth * M.stretchMax;
|
|
42
|
+
const cap = Math.min(TRAIL_MAX, Math.ceil((stretched + ahead) / M.trailStep) + 8);
|
|
43
|
+
if (g.trail.length < cap * TRAIL)
|
|
44
|
+
g.trail = new Float64Array(cap * TRAIL);
|
|
45
|
+
if (g.rowA.length < rows) {
|
|
46
|
+
g.rowA = new Float32Array(rows);
|
|
47
|
+
g.rowW = new Float64Array(rows);
|
|
48
|
+
g.rowD = new Float64Array(rows);
|
|
49
|
+
g.rowSp = new Float64Array(rows);
|
|
50
|
+
g.rowRad = new Float64Array(rows);
|
|
51
|
+
g.rowRun = new Float64Array(rows);
|
|
52
|
+
g.rowB = new Float64Array(rows);
|
|
53
|
+
g.rowLen = new Float64Array(rows);
|
|
54
|
+
g.rowE = new Float64Array(rows);
|
|
55
|
+
g.rowK = new Float64Array(rows);
|
|
56
|
+
}
|
|
57
|
+
if (TB.length < rows) {
|
|
58
|
+
TB = new Float64Array(rows);
|
|
59
|
+
TL = new Float64Array(rows);
|
|
60
|
+
TE = new Float64Array(rows);
|
|
61
|
+
TW = new Float64Array(rows);
|
|
62
|
+
}
|
|
63
|
+
// Rounded to single precision first, like offsets a game keeps per member in a Float32Array: in the open each rank
|
|
64
|
+
// then stands bit for bit where its members' own offsets put them
|
|
65
|
+
const front = Math.fround(f.rowFa[0]);
|
|
66
|
+
let big = 0;
|
|
67
|
+
for (let r = 0; r < rows; r++) {
|
|
68
|
+
g.rowA[r] = Math.fround(f.rowFa[r]) - front;
|
|
69
|
+
g.rowW[r] = f.rowW[r];
|
|
70
|
+
g.rowD[r] = f.rowD[r];
|
|
71
|
+
g.rowSp[r] = f.rowW[r] / f.rowN[r];
|
|
72
|
+
g.rowRad[r] = f.rowRad[r];
|
|
73
|
+
g.rowRun[r] = f.rowRun[r];
|
|
74
|
+
if (f.rowRad[r] > big)
|
|
75
|
+
big = f.rowRad[r];
|
|
76
|
+
}
|
|
77
|
+
Object.assign(g, {
|
|
78
|
+
alive: true, layer, destX, destY, destCell,
|
|
79
|
+
speed, run, hold, engaged: 0, free: 0, cEngaged: 0, cFree: 0, cOpposed: 0, fx, fy, arrived: false, blocked: false, loose: false, opposedT: 0, n,
|
|
80
|
+
cnt: 0, lagSum: 0, lagApart: 0, over: 0, sx: 0, sy: 0, cap, width, depth, open: M.flank ? width + big : width / 2 + big, ahead, rows, tail: false, tailT: 0,
|
|
81
|
+
});
|
|
82
|
+
anchor(gr, g, M, x, y, false);
|
|
83
|
+
return g;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Form up (on an order, after an engagement, when the route is cut): lay the trail out straight behind the front
|
|
87
|
+
* rank against the direction to the goal, and let the probe walk out its lead from the front rank at once. With
|
|
88
|
+
* `MarchRules.faceRoute` the direction is that of the route's first stretch, and a column forming up round its
|
|
89
|
+
* centre (`front` false) is laid out along the route instead: the straight trail lies behind the place of its rear,
|
|
90
|
+
* the probe walks one column depth further than its lead, and the front rank stands that much on along its way.
|
|
91
|
+
* @param x, y The centre of the group (`front` false: the column forms up round it, the front rank at its front), or
|
|
92
|
+
* the front rank's position (`front` true).
|
|
93
|
+
*/
|
|
94
|
+
function anchor(gr, g, M, x, y, front) {
|
|
95
|
+
// The front rank must not start where nobody can stand
|
|
96
|
+
const c0 = gr.cellAt(x, y);
|
|
97
|
+
if (gr.solid(c0)) {
|
|
98
|
+
const c = gr.nearestFree(c0, 20);
|
|
99
|
+
x = (c % gr.w + 0.5) * gr.cellSize;
|
|
100
|
+
y = (((c / gr.w) | 0) + 0.5) * gr.cellSize;
|
|
101
|
+
}
|
|
102
|
+
const dx = g.destX - x, dy = g.destY - y, dl = dmath.hypot(dx, dy) || 1;
|
|
103
|
+
let hx = dx / dl, hy = dy / dl;
|
|
104
|
+
if (M.faceRoute && gr.lineBlocked(x, y, g.destX, g.destY)) {
|
|
105
|
+
// The route does not set off straight for the goal: face the furthest point in sight along it. A route still
|
|
106
|
+
// being searched is finished on the spot, as for a new route
|
|
107
|
+
const f = gr.field(g.destCell);
|
|
108
|
+
if (f.pending)
|
|
109
|
+
gr.finish(f);
|
|
110
|
+
if (lookAhead(gr, g, M, f, gr.cellAt(x, y), x, y, false)) {
|
|
111
|
+
const ax = AIM.x - x, ay = AIM.y - y, al = dmath.hypot(ax, ay);
|
|
112
|
+
if (al > 1e-6) {
|
|
113
|
+
hx = ax / al;
|
|
114
|
+
hy = ay / al;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// With `faceRoute`: how far along the route the column is laid out when forming up round the centre
|
|
119
|
+
let along = 0;
|
|
120
|
+
if (!front && M.faceRoute) {
|
|
121
|
+
// The column forms up along the route: its rear as far behind the centre as there is room for (at most half its
|
|
122
|
+
// depth), and from there one column depth on along the way the probe walks. Laid out straight round the centre
|
|
123
|
+
// instead, the ranks of a column that forms up in a cramped place, or whose route sets off round a corner, would
|
|
124
|
+
// have their places in or behind a wall
|
|
125
|
+
const half = g.depth / 2, reach = gr.lineReach(x, y, x - hx * half, y - hy * half);
|
|
126
|
+
const back = reach >= 1 ? half : Math.max(0, half * reach - gr.cellSize / 2);
|
|
127
|
+
if (back > 0 && !gr.blocked(gr.cellAt(x - hx * back, y - hy * back))) {
|
|
128
|
+
x -= hx * back;
|
|
129
|
+
y -= hy * back;
|
|
130
|
+
}
|
|
131
|
+
along = g.depth;
|
|
132
|
+
}
|
|
133
|
+
else if (!front) {
|
|
134
|
+
// The front rank at the front of the column (where it can walk there): the column then stands round the centre
|
|
135
|
+
const fwd = Math.min(g.depth / 2, dl - M.scale);
|
|
136
|
+
const lx = x + hx * fwd, ly = y + hy * fwd;
|
|
137
|
+
if (fwd > 0 && !gr.blocked(gr.cellAt(lx, ly)) && !gr.lineBlocked(x, y, lx, ly)) {
|
|
138
|
+
x = lx;
|
|
139
|
+
y = ly;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
layTrail(g, M, x, y, hx, hy);
|
|
143
|
+
Object.assign(g, {
|
|
144
|
+
x, y, hx, hy, px: x, py: y, phx: hx, phy: hy, head: 0, rec: 0, v: 0, lag: M.lagStop,
|
|
145
|
+
lead: 0, pDone: false, pBarred: false, narrowB: Infinity, endB: Infinity, tail: false, tailT: 0,
|
|
146
|
+
field: gr.lineBlocked(x, y, g.destX, g.destY) ? gr.field(g.destCell) : null,
|
|
147
|
+
});
|
|
148
|
+
probeMove(gr, g, M, g.ahead + along, g.speed);
|
|
149
|
+
// The front rank one column depth on from where the probe set out; no further than the probe got
|
|
150
|
+
if (along)
|
|
151
|
+
g.lead = g.lead > along ? g.lead - along : 0;
|
|
152
|
+
placeFront(g, M);
|
|
153
|
+
rowsAt(g, M, g.lead, g.rowB, g.rowLen, g.rowE);
|
|
154
|
+
g.rowK.fill(1);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Turn a marching group onto a new route (the same members ordered elsewhere) without forming up again: the column
|
|
158
|
+
* and its trail stay, and the front rank turns straight for the new goal. Returns false and changes nothing when the
|
|
159
|
+
* new route sets off at an angle to the front rank's heading whose cosine is below `rerouteCos` (it would have to
|
|
160
|
+
* turn back); the game then starts a new march. For a march still on its way: one that has arrived is not asked.
|
|
161
|
+
*
|
|
162
|
+
* The stretch of trail the probe had walked ahead of the front rank is dropped, the probe returns to the front rank
|
|
163
|
+
* and walks out its lead along the new route. The points kept get their envelope redone from the widths measured at
|
|
164
|
+
* each: narrowing passed back from the dropped stretch is gone, narrow places behind still count.
|
|
165
|
+
*/
|
|
166
|
+
export function rerouteMarch(gr, g, M, destX, destY, destCell, hold, fx, fy) {
|
|
167
|
+
const field = gr.lineBlocked(g.x, g.y, destX, destY) ? gr.field(destCell) : null;
|
|
168
|
+
// A route still being searched is finished on the spot: judging by the straight line alone would take a route that
|
|
169
|
+
// leads back round an obstacle for one that needs no turning back
|
|
170
|
+
if (field?.pending)
|
|
171
|
+
gr.finish(field);
|
|
172
|
+
const ox = g.destX, oy = g.destY, oc = g.destCell;
|
|
173
|
+
g.destX = destX;
|
|
174
|
+
g.destY = destY;
|
|
175
|
+
g.destCell = destCell;
|
|
176
|
+
let ax = destX - g.x, ay = destY - g.y;
|
|
177
|
+
if (field && lookAhead(gr, g, M, field, gr.cellAt(g.x, g.y), g.x, g.y)) {
|
|
178
|
+
ax = AIM.x - g.x;
|
|
179
|
+
ay = AIM.y - g.y;
|
|
180
|
+
}
|
|
181
|
+
const al = dmath.hypot(ax, ay), hl = dmath.hypot(g.hx, g.hy);
|
|
182
|
+
if (al > 1e-6 && (ax * g.hx + ay * g.hy) / (al * (hl || 1)) < M.rerouteCos) {
|
|
183
|
+
g.destX = ox;
|
|
184
|
+
g.destY = oy;
|
|
185
|
+
g.destCell = oc;
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
Object.assign(g, { field, hold, fx, fy });
|
|
189
|
+
cutAhead(g, M);
|
|
190
|
+
probeMove(gr, g, M, g.ahead, g.speed);
|
|
191
|
+
placeFront(g, M);
|
|
192
|
+
rowsAt(g, M, g.lead, g.rowB, g.rowLen, g.rowE);
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
/** The front rank's position and heading: the point of the trail `lead` behind the probe. */
|
|
196
|
+
function placeFront(g, M) {
|
|
197
|
+
trailAt(g, M, g.lead);
|
|
198
|
+
g.x = TP.x;
|
|
199
|
+
g.y = TP.y;
|
|
200
|
+
g.hx = TP.hx;
|
|
201
|
+
g.hy = TP.hy;
|
|
202
|
+
}
|
|
203
|
+
const AIM = { x: 0, y: 0 };
|
|
204
|
+
/** The width of the way at the probe as far as it is known: with `flank`, the usable width at the newest trail point where that is less than the column's. */
|
|
205
|
+
function wayAt(g, M) {
|
|
206
|
+
return M.flank ? Math.min(g.width, widthAt(g, M, 0)) : g.width;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Count cells along the flow field from under the probe and take the furthest one a straight walk reaches (no
|
|
210
|
+
* further than the look-ahead distance), into `AIM`. A flow field has eight directions; walked as it is, the route
|
|
211
|
+
* is a polyline, and a rank following it would wheel at every kink. With `MarchRules.lookClear` the furthest cell
|
|
212
|
+
* the walk to which is also clear that share of the column's width to each side is taken when there is one, so the
|
|
213
|
+
* straightened route keeps its distance from the corners it goes round.
|
|
214
|
+
* @param laid False while forming up, before the trail is laid out: no usable width is known yet.
|
|
215
|
+
*/
|
|
216
|
+
function lookAhead(gr, g, M, f, cell, x = g.px, y = g.py, laid = true) {
|
|
217
|
+
const mw = gr.w, T = gr.cellSize;
|
|
218
|
+
const look = Math.max(M.lookMin, g.width * M.lookK);
|
|
219
|
+
const max = Math.ceil(look / T) * 2 + 2;
|
|
220
|
+
// With `lookClear` a point the walk to which is also clear to each side is preferred to a further one that is only
|
|
221
|
+
// just in sight: a line of sight that only just passes a corner would lead the column along the corner's wall, to
|
|
222
|
+
// turn round the corner on the spot
|
|
223
|
+
const side = M.lookClear > 0 ? (laid ? wayAt(g, M) : g.width) * M.lookClear : 0;
|
|
224
|
+
let c = cell, ok = false, wide = false;
|
|
225
|
+
for (let k = 0; k < max; k++) {
|
|
226
|
+
const dir = f.dirs[c];
|
|
227
|
+
if (dir > 7) {
|
|
228
|
+
if (dir === DIR_HERE && !gr.lineBlocked(x, y, g.destX, g.destY)) {
|
|
229
|
+
AIM.x = g.destX;
|
|
230
|
+
AIM.y = g.destY;
|
|
231
|
+
ok = true;
|
|
232
|
+
}
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
c += DIRX[dir] + DIRY[dir] * mw;
|
|
236
|
+
const px = (c % mw + 0.5) * T, py = (((c / mw) | 0) + 0.5) * T;
|
|
237
|
+
if ((px - x) ** 2 + (py - y) ** 2 > look * look)
|
|
238
|
+
break;
|
|
239
|
+
if (gr.lineBlocked(x, y, px, py))
|
|
240
|
+
break;
|
|
241
|
+
if (side > 0) {
|
|
242
|
+
const l = dmath.hypot(px - x, py - y);
|
|
243
|
+
if (l > 1e-6) {
|
|
244
|
+
const sx = -(py - y) / l * side, sy = (px - x) / l * side;
|
|
245
|
+
if (!gr.lineBlocked(x + sx, y + sy, px + sx, py + sy) && !gr.lineBlocked(x - sx, y - sy, px - sx, py - sy))
|
|
246
|
+
wide = true;
|
|
247
|
+
else if (wide)
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
AIM.x = px;
|
|
252
|
+
AIM.y = py;
|
|
253
|
+
ok = true;
|
|
254
|
+
}
|
|
255
|
+
return ok;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* The probe walks `dist` along the route (in steps of at most `PROBE_SUB`), recording the trail as it goes. It looks
|
|
259
|
+
* a stretch along the route and heads for the furthest point in sight (straightening the flow field's polyline). Its
|
|
260
|
+
* heading (the direction the ranks face) turns no faster than the column's width allows: the turning radius is at
|
|
261
|
+
* least half the width, or the inner places would fold back and the outer ones run a wide arc. It may walk a little
|
|
262
|
+
* off its heading (at most `slide`), so the ranks sidestep onto the route. It stops while the route is still being
|
|
263
|
+
* searched, and in front of a barred cell.
|
|
264
|
+
* @param vref The speed this stretch is walked at: the heading turns at most `turn` per second, converted here to a
|
|
265
|
+
* turn per unit walked.
|
|
266
|
+
*/
|
|
267
|
+
function probeMove(gr, g, M, dist, vref) {
|
|
268
|
+
const mw = gr.w, T = gr.cellSize;
|
|
269
|
+
const turnK = M.turn / (vref > M.scale ? vref : M.scale), sub = PROBE_SUB * M.scale;
|
|
270
|
+
while (dist > 1e-9) {
|
|
271
|
+
const step = dist > sub ? sub : dist;
|
|
272
|
+
dist -= step;
|
|
273
|
+
const cell = ((g.py / T) | 0) * mw + ((g.px / T) | 0);
|
|
274
|
+
const ddx = g.destX - g.px, ddy = g.destY - g.py;
|
|
275
|
+
const dd = Math.sqrt(ddx * ddx + ddy * ddy);
|
|
276
|
+
if (dd <= step + M.scale) {
|
|
277
|
+
advance(gr, g, M, g.destX, g.destY, true);
|
|
278
|
+
g.pDone = true;
|
|
279
|
+
g.pBarred = false;
|
|
280
|
+
g.endB = 0;
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
// Where to: the furthest point in sight along the route; straight for the goal when it is near
|
|
284
|
+
let tx = g.destX, ty = g.destY;
|
|
285
|
+
let f = g.field;
|
|
286
|
+
let fdx = 0, fdy = 0;
|
|
287
|
+
if (f) {
|
|
288
|
+
if (f.dead)
|
|
289
|
+
f = g.field = gr.field(g.destCell);
|
|
290
|
+
f.lastUse = gr.tick;
|
|
291
|
+
const dir = f.dirs[cell];
|
|
292
|
+
if (dir < 8) {
|
|
293
|
+
const kk = dir & 1 ? 0.7071 : 1;
|
|
294
|
+
fdx = DIRX[dir] * kk;
|
|
295
|
+
fdy = DIRY[dir] * kk;
|
|
296
|
+
if (lookAhead(gr, g, M, f, cell)) {
|
|
297
|
+
tx = AIM.x;
|
|
298
|
+
ty = AIM.y;
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
tx = g.px + fdx * T;
|
|
302
|
+
ty = g.py + fdy * T;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
else if (dir !== DIR_HERE && f.pending)
|
|
306
|
+
return; // The route is not known here yet: wait
|
|
307
|
+
}
|
|
308
|
+
const ha = dmath.atan2(g.phy, g.phx);
|
|
309
|
+
let da = dmath.atan2(ty - g.py, tx - g.px) - ha;
|
|
310
|
+
if (da > Math.PI)
|
|
311
|
+
da -= Math.PI * 2;
|
|
312
|
+
else if (da < -Math.PI)
|
|
313
|
+
da += Math.PI * 2;
|
|
314
|
+
// With `flank` the usable width is known all along the route: where the way is narrower than the column, the
|
|
315
|
+
// squeezed ranks can wheel as tightly as the way is wide
|
|
316
|
+
const way = wayAt(g, M);
|
|
317
|
+
const radius = dd < g.width ? M.scale : Math.max(M.turnR, way / 2);
|
|
318
|
+
let turn = step / radius;
|
|
319
|
+
if (turn > step * turnK)
|
|
320
|
+
turn = step * turnK;
|
|
321
|
+
const na = ha + (da > turn ? turn : da < -turn ? -turn : da);
|
|
322
|
+
g.phx = dmath.cos(na);
|
|
323
|
+
g.phy = dmath.sin(na);
|
|
324
|
+
// The direction walked: while the heading still turns, a little off it, so the ranks sidestep onto the route
|
|
325
|
+
let ma = na;
|
|
326
|
+
const rest = da - (na - ha);
|
|
327
|
+
if (rest > M.slide)
|
|
328
|
+
ma += M.slide;
|
|
329
|
+
else if (rest < -M.slide)
|
|
330
|
+
ma -= M.slide;
|
|
331
|
+
else
|
|
332
|
+
ma += rest;
|
|
333
|
+
let nx = g.px + dmath.cos(ma) * step, ny = g.py + dmath.sin(ma) * step;
|
|
334
|
+
let nc = ((ny / T) | 0) * mw + ((nx / T) | 0);
|
|
335
|
+
if (nc !== cell && (gr.blocked(nc) || !gr.canStep(cell, nc)) && (fdx || fdy)) {
|
|
336
|
+
// The shortcut ran into an obstacle: follow the flow field as it is
|
|
337
|
+
nx = g.px + fdx * step;
|
|
338
|
+
ny = g.py + fdy * step;
|
|
339
|
+
nc = ((ny / T) | 0) * mw + ((nx / T) | 0);
|
|
340
|
+
}
|
|
341
|
+
if (nc !== cell && (gr.blocked(nc) || !gr.canStep(cell, nc))) {
|
|
342
|
+
// No straight way: take the flow field from now on; where its route is barred, stop and wait
|
|
343
|
+
if (!g.field)
|
|
344
|
+
g.field = gr.field(g.destCell);
|
|
345
|
+
else if (gr.barred(nc))
|
|
346
|
+
g.pBarred = true;
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
g.pBarred = false;
|
|
350
|
+
advance(gr, g, M, nx, ny, true);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
/** With the probe at the goal (while winding up), it coasts straight on along its last heading: no cell checks, no narrow places measured, it only keeps leading the column on. */
|
|
354
|
+
function coast(gr, g, M, dist) {
|
|
355
|
+
advance(gr, g, M, g.px + g.phx * dist, g.py + g.phy * dist, false);
|
|
356
|
+
}
|
|
357
|
+
/** Move the probe to (nx, ny): the front rank falls that much further behind, and the stretch walked gets a trail point every `trailStep`, each with its narrow place measured when `measure` is set. */
|
|
358
|
+
function advance(gr, g, M, nx, ny, measure) {
|
|
359
|
+
const st = M.trailStep, T = gr.cellSize;
|
|
360
|
+
const ox = g.px, oy = g.py;
|
|
361
|
+
const moved = dmath.hypot(nx - ox, ny - oy);
|
|
362
|
+
g.px = nx;
|
|
363
|
+
g.py = ny;
|
|
364
|
+
if (moved <= 0)
|
|
365
|
+
return;
|
|
366
|
+
g.lead += moved;
|
|
367
|
+
g.narrowB += moved;
|
|
368
|
+
g.endB += moved;
|
|
369
|
+
g.rec += moved;
|
|
370
|
+
while (g.rec >= st) {
|
|
371
|
+
// A point on the stretch just walked, exactly one step from the previous trail point
|
|
372
|
+
const back = (g.rec - st) / moved;
|
|
373
|
+
const x = nx - (nx - ox) * back, y = ny - (ny - oy) * back;
|
|
374
|
+
// The cell recorded is the one the probe stood in at the nearer end of the stretch (it found that one passable),
|
|
375
|
+
// used later to see whether the route was cut. The point's own cell is not worked out afresh: an interpolated
|
|
376
|
+
// point on a cell border may fall into the neighbouring cell
|
|
377
|
+
const ex = back < 0.5 ? nx : ox, ey = back < 0.5 ? ny : oy;
|
|
378
|
+
g.rec -= st;
|
|
379
|
+
addPoint(gr, g, M, x, y, measure ? ((ey / T) | 0) * gr.w + ((ex / T) | 0) : -1, measure);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* The front rank's top speed (before ground, weather and waiting): the group's marching speed, or its full speed once
|
|
384
|
+
* most members act on their own. When the ranks ahead spread out, ranks still in a narrow place must move faster to
|
|
385
|
+
* keep up: this measures how far each rank moves per unit the front rank moves (into `rowK`) and keeps every rank
|
|
386
|
+
* within `trot` of its own full speed. With ranks folded, the whole column trots through, at the pace of the slowest
|
|
387
|
+
* folded rank.
|
|
388
|
+
*/
|
|
389
|
+
function paceTop(g, M) {
|
|
390
|
+
rowsAt(g, M, g.lead - M.scale, TB, TL, TE, TW);
|
|
391
|
+
let fold = 1, foldRun = Infinity;
|
|
392
|
+
for (let r = 0; r < g.rows; r++) {
|
|
393
|
+
// Kept in this order, not as (rowE − TE) / scale: with scale 1 the grouping decides the last bit of the result
|
|
394
|
+
g.rowK[r] = 1 + g.rowE[r] / M.scale - TE[r] / M.scale;
|
|
395
|
+
const fr = g.rowLen[r] / g.rowD[r];
|
|
396
|
+
if (fr > 1 && g.rowRun[r] < foldRun)
|
|
397
|
+
foldRun = g.rowRun[r];
|
|
398
|
+
if (fr > fold)
|
|
399
|
+
fold = fr;
|
|
400
|
+
}
|
|
401
|
+
let top = g.free > 0.5 ? g.run : g.speed;
|
|
402
|
+
if (fold > 1 && foldRun * M.trot > top)
|
|
403
|
+
top += (foldRun * M.trot - top) * smooth(fold - 1);
|
|
404
|
+
for (let r = 0; r < g.rows; r++) {
|
|
405
|
+
let kr = g.rowK[r];
|
|
406
|
+
// With `wheel` it is the outermost place of the rank that must keep within the rank's speed. A rank on a bend
|
|
407
|
+
// with the front rank past it moves that much less along the trail (it closes up), so the two cancel; a front
|
|
408
|
+
// rank on a bend itself is what this slows down. Not within one width of the goal: there the probe turned as
|
|
409
|
+
// sharply as it took to reach the goal, and the members are about to leave for their own spots
|
|
410
|
+
if (M.wheel && !(g.pDone && g.lead < g.width))
|
|
411
|
+
kr *= TW[r];
|
|
412
|
+
if (kr <= 1)
|
|
413
|
+
continue;
|
|
414
|
+
const rt = g.rowRun[r] * M.trot, lim = (rt > g.speed ? rt : g.speed) / kr;
|
|
415
|
+
if (lim < top)
|
|
416
|
+
top = lim;
|
|
417
|
+
}
|
|
418
|
+
return top;
|
|
419
|
+
}
|
|
420
|
+
/** 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. */
|
|
421
|
+
function needTail(g, M) {
|
|
422
|
+
const last = g.rows - 1;
|
|
423
|
+
return g.narrowB < g.rowB[last] + g.rowLen[last] / 2 + M.trailStep * TAIL_SLACK;
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* The speed factor of the ground under the front rank, always positive. The front rank is an interpolated point of
|
|
427
|
+
* the trail: along a bridge or a bank it may fall into an impassable cell, and while winding up it coasts in a
|
|
428
|
+
* straight line, possibly into water or walls. Without a positive value there, the goal cell counts while winding up,
|
|
429
|
+
* otherwise the cell the probe walked through at the nearest trail point; failing that, 1. A zero would stop the front
|
|
430
|
+
* rank, the probe with it, and the group would stand there for good.
|
|
431
|
+
*/
|
|
432
|
+
function frontGround(gr, g, M) {
|
|
433
|
+
const mw = gr.w, mh = gr.h, T = gr.cellSize;
|
|
434
|
+
const cxi = (g.x / T) | 0, cyi = (g.y / T) | 0;
|
|
435
|
+
const cell = (cyi < 0 ? 0 : cyi >= mh ? mh - 1 : cyi) * mw + (cxi < 0 ? 0 : cxi >= mw ? mw - 1 : cxi);
|
|
436
|
+
let v = gr.speedAt(cell);
|
|
437
|
+
if (v > 0)
|
|
438
|
+
return v;
|
|
439
|
+
if (g.tail)
|
|
440
|
+
v = gr.speedAt(g.destCell);
|
|
441
|
+
else {
|
|
442
|
+
const near = nearCell(g, M, -1);
|
|
443
|
+
v = near >= 0 ? gr.speedAt(near) : 0;
|
|
444
|
+
}
|
|
445
|
+
return v > 0 ? v : 1;
|
|
446
|
+
}
|
|
447
|
+
/** Count each group's members and how far they lag, and settle whether it is opposed, breaks ranks or forms up again. */
|
|
448
|
+
function tally(marches, grounds, mem, M, dt) {
|
|
449
|
+
for (const g of marches)
|
|
450
|
+
g.cnt = g.lagSum = g.lagApart = g.over = g.cEngaged = g.cFree = g.cOpposed = g.sx = g.sy = 0;
|
|
451
|
+
// Each march's ground, asked for once per tally
|
|
452
|
+
const grs = [];
|
|
453
|
+
for (let i = 0; i < mem.n; i++) {
|
|
454
|
+
const gi = mem.march(i);
|
|
455
|
+
if (gi < 0 || !mem.alive(i))
|
|
456
|
+
continue;
|
|
457
|
+
const g = marches[gi];
|
|
458
|
+
const st = mem.state(i);
|
|
459
|
+
if (!g.alive || (g.arrived && !g.tail) || st & MEMBER_OUT) {
|
|
460
|
+
mem.leave(i);
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
g.cnt++;
|
|
464
|
+
g.sx += mem.x(i);
|
|
465
|
+
g.sy += mem.y(i);
|
|
466
|
+
if (st & MEMBER_ENGAGED) {
|
|
467
|
+
g.cEngaged++;
|
|
468
|
+
if (st & MEMBER_OPPOSED)
|
|
469
|
+
g.cOpposed++;
|
|
470
|
+
}
|
|
471
|
+
if (st & MEMBER_FREE) {
|
|
472
|
+
// A member acting on its own keeps no place and does not count as lagging
|
|
473
|
+
g.cFree++;
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
marchSlot(g, M, mem.row(i), mem.col(i), mem.lateral(i));
|
|
477
|
+
const gr = grs[gi] ??= grounds(g.layer);
|
|
478
|
+
// A place nobody can stand on, or cut off from its rank's middle: this part of the column is draped over an obstacle
|
|
479
|
+
if (!gr.standable(SLOT.x, SLOT.y) || gr.lineBlocked(SLOT.cx, SLOT.cy, SLOT.x, SLOT.y))
|
|
480
|
+
g.over++;
|
|
481
|
+
const ox = SLOT.x - mem.x(i), oy = SLOT.y - mem.y(i);
|
|
482
|
+
// A member whose place is behind it does not lag: it waits for the place, or walks on while the group is opposed;
|
|
483
|
+
// either way the front rank need not wait
|
|
484
|
+
if (ox * SLOT.hx + oy * SLOT.hy < -M.waitAhead)
|
|
485
|
+
continue;
|
|
486
|
+
const lag = Math.sqrt(ox * ox + oy * oy);
|
|
487
|
+
if (mem.apart(i))
|
|
488
|
+
g.lagApart += lag > M.lagCap ? M.lagCap : lag;
|
|
489
|
+
else
|
|
490
|
+
g.lagSum += lag > M.lagCap ? M.lagCap : lag;
|
|
491
|
+
}
|
|
492
|
+
for (const g of marches) {
|
|
493
|
+
if (!g.alive)
|
|
494
|
+
continue;
|
|
495
|
+
g.n = g.cnt;
|
|
496
|
+
if (!g.cnt)
|
|
497
|
+
g.alive = false;
|
|
498
|
+
else {
|
|
499
|
+
// While the column is draped over an obstacle, members out of reach of their places are finding their own way
|
|
500
|
+
// and the front rank does not wait for them: waiting would hold the ranks behind on this side of the obstacle
|
|
501
|
+
// too, and the group would pass it one rank at a time. Once the whole column is clear they count again, and the
|
|
502
|
+
// front rank slows for them to fall in
|
|
503
|
+
const draped = g.over > Math.max(1, g.cnt * M.overMin);
|
|
504
|
+
g.lag = (g.lagSum + (draped ? 0 : g.lagApart)) / g.cnt;
|
|
505
|
+
g.engaged = g.cEngaged / g.cnt;
|
|
506
|
+
g.free = g.cFree / g.cnt;
|
|
507
|
+
// Engaged: break ranks. Afterwards the group forms up round where its members are now; it does not walk back
|
|
508
|
+
const loose = (g.hold && g.engaged > M.holdEngaged) || g.free > 0.5;
|
|
509
|
+
// Counted down by the tally interval, so a lone opponent appearing and gone again does not send the group
|
|
510
|
+
// forward and straight back to waiting for places
|
|
511
|
+
if (g.cOpposed > 0)
|
|
512
|
+
g.opposedT = M.opposedLinger;
|
|
513
|
+
else if (g.opposedT > 0)
|
|
514
|
+
g.opposedT -= dt * TALLY;
|
|
515
|
+
if (g.loose && !loose && !g.arrived)
|
|
516
|
+
anchor(grounds(g.layer), g, M, g.sx / g.cnt, g.sy / g.cnt, false);
|
|
517
|
+
g.loose = loose;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Advance every march by one step of `dt` seconds; call once per simulation step, before the members move.
|
|
523
|
+
* @param grounds The ground of a movement layer.
|
|
524
|
+
* @param tick The simulation step: the members are tallied on every eighth.
|
|
525
|
+
*/
|
|
526
|
+
export function updateMarches(marches, grounds, members, M, dt, tick) {
|
|
527
|
+
if (!marches.length)
|
|
528
|
+
return;
|
|
529
|
+
if ((tick & (TALLY - 1)) === 0)
|
|
530
|
+
tally(marches, grounds, members, M, dt);
|
|
531
|
+
for (const g of marches) {
|
|
532
|
+
if (!g.alive || (g.arrived && !g.tail))
|
|
533
|
+
continue;
|
|
534
|
+
if (g.tail && (g.tailT += dt) > M.tailMax) {
|
|
535
|
+
// Winding up for too long (somebody is stuck): break up, the rest make for their spots on their own
|
|
536
|
+
g.tail = false;
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
const gr = grounds(g.layer);
|
|
540
|
+
// While winding up the column only leads the ranks behind through the narrow place; it waits for nobody
|
|
541
|
+
const k = (M.lagStop - g.lag) / (M.lagStop - M.lagFull);
|
|
542
|
+
const mult = g.tail ? 1 : g.hold && g.engaged > M.holdEngaged ? 0 : k < 0 ? 0 : k > 1 ? 1 : k;
|
|
543
|
+
const sp = paceTop(g, M) * frontGround(gr, g, M) * gr.speedScale * mult;
|
|
544
|
+
g.v = sp;
|
|
545
|
+
if (sp <= 0)
|
|
546
|
+
continue;
|
|
547
|
+
const step = sp * dt;
|
|
548
|
+
if (g.pDone && !g.tail && g.lead <= step + M.scale) {
|
|
549
|
+
g.arrived = true;
|
|
550
|
+
if (needTail(g, M)) {
|
|
551
|
+
// Ranks behind have not passed the narrow place: the column stays and keeps leading; members leave one by one
|
|
552
|
+
g.tail = true;
|
|
553
|
+
g.tailT = 0;
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
g.x = g.destX;
|
|
557
|
+
g.y = g.destY;
|
|
558
|
+
g.v = 0;
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
// The front rank takes the heading factor like the members do, or places and members would drift apart. Only the
|
|
563
|
+
// direction counts: on a bend the interpolated heading is not a unit vector and must not weaken the factor
|
|
564
|
+
const wf = gr.headingScale(g.hx, g.hy);
|
|
565
|
+
const full = step * wf;
|
|
566
|
+
// The front rank walks along the trail, up to the probe
|
|
567
|
+
const df = full > g.lead ? g.lead : full;
|
|
568
|
+
g.lead -= df;
|
|
569
|
+
if (df < full)
|
|
570
|
+
g.v = df / (dt * wf);
|
|
571
|
+
// 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,
|
|
572
|
+
// or the route has only just been found)
|
|
573
|
+
const want = g.ahead - g.lead;
|
|
574
|
+
if (want > 1e-6) {
|
|
575
|
+
const adv = want < df + gr.cellSize ? want : df + gr.cellSize;
|
|
576
|
+
if (g.tail)
|
|
577
|
+
coast(gr, g, M, adv);
|
|
578
|
+
else if (!g.pDone)
|
|
579
|
+
probeMove(gr, g, M, adv, sp * wf);
|
|
580
|
+
}
|
|
581
|
+
if (!g.tail)
|
|
582
|
+
g.blocked = g.pBarred && g.lead < M.scale;
|
|
583
|
+
placeFront(g, M);
|
|
584
|
+
// The route the probe found has been closed since: form up again from where the front rank is now
|
|
585
|
+
if (!g.tail && df > 0 && routeCut(gr, g, M))
|
|
586
|
+
anchor(gr, g, M, g.x, g.y, true);
|
|
587
|
+
rowsAt(g, M, g.lead, g.rowB, g.rowLen, g.rowE);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Result of `steerMember`. With `STEER_KEEP`: the velocity to move at; `set` is false when the member is on its
|
|
592
|
+
* place and the answer is "as you were" (the velocity is then zero, and a game that had the member moving for a
|
|
593
|
+
* reason of its own may leave that be). With `STEER_APART` and `STEER_PUSH`: the speed to move at along its own
|
|
594
|
+
* route. `apart`: whether the member is off its place this step (the game keeps it for `MarchMembers.apart`).
|
|
595
|
+
*/
|
|
596
|
+
export const STEER = { vx: 0, vy: 0, set: false, speed: 0, apart: false };
|
|
597
|
+
/**
|
|
598
|
+
* What `steerMember` returns:
|
|
599
|
+
* - `STEER_IDLE`: the group is not marching in ranks right now; the member moves as if it had no march.
|
|
600
|
+
* - `STEER_LEAVE`: the member has left the march (the game clears its membership) and makes for its own spot.
|
|
601
|
+
* - `STEER_PUSH`: the member stays in the march but walks on along its route instead of waiting for its place.
|
|
602
|
+
* - `STEER_APART`: the member finds its own way along its route at `STEER.speed`, and rejoins when it can.
|
|
603
|
+
* - `STEER_KEEP`: the member keeps its place: it moves at (`STEER.vx`, `STEER.vy`).
|
|
604
|
+
*/
|
|
605
|
+
export const STEER_IDLE = 0, STEER_LEAVE = 1, STEER_PUSH = 2, STEER_APART = 3, STEER_KEEP = 4;
|
|
606
|
+
/**
|
|
607
|
+
* What a member of march `g` does this step; call it for each member after `updateMarches`.
|
|
608
|
+
* @param row, col, lateral The member's rank, place in it and lateral offset, from the formation the march was made with.
|
|
609
|
+
* @param ux, uy Where the member stands; destX, destY its own spot at the goal.
|
|
610
|
+
* @param speed, run The member's speed now and its full speed, in world units per second.
|
|
611
|
+
* @param free The member acts on its own this step (it keeps no place).
|
|
612
|
+
*/
|
|
613
|
+
export function steerMember(gr, g, M, row, col, lateral, ux, uy, destX, destY, speed, run, free) {
|
|
614
|
+
STEER.vx = STEER.vy = 0;
|
|
615
|
+
STEER.set = STEER.apart = false;
|
|
616
|
+
STEER.speed = speed;
|
|
617
|
+
if (!g.alive || (g.arrived && !g.tail))
|
|
618
|
+
return STEER_LEAVE;
|
|
619
|
+
// Places are kept only while nothing else is going on
|
|
620
|
+
if (g.blocked || g.loose || free)
|
|
621
|
+
return STEER_IDLE;
|
|
622
|
+
marchSlot(g, M, row, col, lateral);
|
|
623
|
+
// Whether the place is behind the member, measured along the column's direction of travel there
|
|
624
|
+
const behind = (SLOT.x - ux) * SLOT.hx + (SLOT.y - uy) * SLOT.hy < -M.waitAhead;
|
|
625
|
+
// Winding up: the rank has passed the goal, or no narrow place lies ahead and the spot is in sight
|
|
626
|
+
if (g.tail && marchRelease(gr, g, ux, uy, destX, destY))
|
|
627
|
+
return STEER_LEAVE;
|
|
628
|
+
// The place is behind and the group is opposed: do not wait for it. Whether the place is in reach does not matter
|
|
629
|
+
// (past an obstacle it often is not), and there is nothing to catch up with: the member is ahead of its place
|
|
630
|
+
if (behind && g.opposedT > 0)
|
|
631
|
+
return STEER_PUSH;
|
|
632
|
+
const mode = marchFollow(gr, ux, uy);
|
|
633
|
+
if (mode !== FOLLOW_SLOT)
|
|
634
|
+
STEER.apart = true;
|
|
635
|
+
// Winding up with neither the place nor the rank in reach: no rejoining
|
|
636
|
+
if (mode === FOLLOW_APART && g.tail)
|
|
637
|
+
return STEER_LEAVE;
|
|
638
|
+
if (mode === FOLLOW_APART) {
|
|
639
|
+
// Out of reach of the place and the rank: do not wait, find a way round and fall in afterwards. Run to catch up
|
|
640
|
+
// with a place ahead; with the place behind, move at the rank's pace instead of running ahead to stand and wait
|
|
641
|
+
if (!behind)
|
|
642
|
+
STEER.speed = run;
|
|
643
|
+
else {
|
|
644
|
+
const v = Math.max(SLOT.v, speed * M.apartTrail);
|
|
645
|
+
STEER.speed = v < run ? v : run;
|
|
646
|
+
}
|
|
647
|
+
return STEER_APART;
|
|
648
|
+
}
|
|
649
|
+
// Aim a little ahead of the point to follow: on its place the member moves at its rank's pace, behind it runs
|
|
650
|
+
const ex = FOLLOW.x + SLOT.hx * SLOT.v * M.leadTime - ux, ey = FOLLOW.y + SLOT.hy * SLOT.v * M.leadTime - uy;
|
|
651
|
+
const ed = Math.sqrt(ex * ex + ey * ey);
|
|
652
|
+
if (behind) {
|
|
653
|
+
// The place is behind (the member got ahead on a detour): stand and wait for it, do not walk back
|
|
654
|
+
STEER.set = true;
|
|
655
|
+
}
|
|
656
|
+
else if (ed > 0.5 * M.scale) {
|
|
657
|
+
let v = ed / M.leadTime;
|
|
658
|
+
if (v > run)
|
|
659
|
+
v = run;
|
|
660
|
+
STEER.vx = (ex / ed) * v;
|
|
661
|
+
STEER.vy = (ey / ed) * v;
|
|
662
|
+
STEER.set = true;
|
|
663
|
+
}
|
|
664
|
+
return STEER_KEEP;
|
|
665
|
+
}
|