@pocketrts/grid 0.2.0 → 0.3.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 +2 -0
- package/dist/march.js +67 -13
- package/dist/marchChoke.js +18 -0
- package/dist/marchTrail.js +5 -2
- package/dist/marchTypes.d.ts +20 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,6 +37,8 @@ ES module, TypeScript types included. Depends on `@pocketrts/determinism`.
|
|
|
37
37
|
| `formationShape(shape, scale?)`, `FormationShape`, `FORMATION_SHAPES` | The `lineFormation` options of a named shape (`line`, `column`, `wedge`, `loose`), scaled for another world. A shape is only ranks, so `createMarch` carries it unchanged: a march keeps its formation on the way and at the goal. |
|
|
38
38
|
| `MarchRules`, `DEFAULT_MARCH_RULES`, `marchRules(scale, overrides?)` | The march system's tunable values (distances, speeds' limits, turning, folding, engagement) and the defaults, made for a world of cells of about 32 units; `marchRules` scales the distances for another world. |
|
|
39
39
|
| `MarchGround`, `GridGround`, `GridGroundOptions` | What the game gives the march about its map, per movement layer (cells, passability, lines of sight, routes, ground speed), and a ready-made implementation for a plain grid of open and blocked cells. |
|
|
40
|
+
| `MarchGround.crossSection`, `MarchCrossSection` | Supply a passage’s transverse unit axis and distances to its physical edges for route alignment and rank placement. The march applies its own padding and member radii. |
|
|
41
|
+
| `MarchGround.probeBlocked` | Check probe movement and trail obstructions in world coordinates. Both ground queries are optional and can return `undefined` locally to use the grid rules. |
|
|
40
42
|
| `MarchMembers`, `MEMBER_OUT`, `MEMBER_ENGAGED`, `MEMBER_OPPOSED`, `MEMBER_FREE` | What the game gives the march about its units (position, march, rank, place, lateral offset, condition flags) and the condition flags. |
|
|
41
43
|
| `March` | The state of one marching group: the front rank and the probe ahead of it, the trail, the ranks' current widths and lengths, arrival and hold flags. The game reads it; only the march functions write it. |
|
|
42
44
|
| `createMarch(marches, ground, rules, order)`, `MarchOrder`, `rerouteMarch(...)` | Set a group marching from the ranks of a `lineFormation` (reusing a dead entry of `marches`), or turn a group already marching onto a new goal without forming up again (false: the new route turns back, start a new march). |
|
package/dist/march.js
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
* Results are deterministic: the same calls with the same inputs give the same bits.
|
|
10
10
|
*/
|
|
11
11
|
import { dmath, smooth } from '@pocketrts/determinism';
|
|
12
|
-
import {
|
|
12
|
+
import { nearestCell } from './cells.js';
|
|
13
|
+
import { DIR_HERE, DIR_NONE, DIRX, DIRY } from './dirs.js';
|
|
13
14
|
import { FOLLOW, FOLLOW_APART, FOLLOW_SLOT, SLOT, marchFollow, marchRelease, marchSlot, rowsAt } from './marchSlots.js';
|
|
14
15
|
import { TP, TRAIL, TRAIL_MAX, addPoint, cutAhead, layTrail, nearCell, routeCut, trailAt, widthAt } from './marchTrail.js';
|
|
15
16
|
import { MEMBER_ENGAGED, MEMBER_FREE, MEMBER_OPPOSED, MEMBER_OUT, } from './marchTypes.js';
|
|
@@ -94,7 +95,7 @@ export function createMarch(marches, gr, M, order) {
|
|
|
94
95
|
function anchor(gr, g, M, x, y, front) {
|
|
95
96
|
// The front rank must not start where nobody can stand
|
|
96
97
|
const c0 = gr.cellAt(x, y);
|
|
97
|
-
if (gr.solid(c0)) {
|
|
98
|
+
if (gr.probeBlocked?.(x, y, x, y) ?? gr.solid(c0)) {
|
|
98
99
|
const c = gr.nearestFree(c0, 20);
|
|
99
100
|
x = (c % gr.w + 0.5) * gr.cellSize;
|
|
100
101
|
y = (((c / gr.w) | 0) + 0.5) * gr.cellSize;
|
|
@@ -124,9 +125,10 @@ function anchor(gr, g, M, x, y, front) {
|
|
|
124
125
|
// have their places in or behind a wall
|
|
125
126
|
const half = g.depth / 2, reach = gr.lineReach(x, y, x - hx * half, y - hy * half);
|
|
126
127
|
const back = reach >= 1 ? half : Math.max(0, half * reach - gr.cellSize / 2);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
128
|
+
const bx = x - hx * back, by = y - hy * back;
|
|
129
|
+
if (back > 0 && !(gr.probeBlocked?.(x, y, bx, by) ?? gr.blocked(gr.cellAt(bx, by)))) {
|
|
130
|
+
x = bx;
|
|
131
|
+
y = by;
|
|
130
132
|
}
|
|
131
133
|
along = g.depth;
|
|
132
134
|
}
|
|
@@ -134,7 +136,7 @@ function anchor(gr, g, M, x, y, front) {
|
|
|
134
136
|
// The front rank at the front of the column (where it can walk there): the column then stands round the centre
|
|
135
137
|
const fwd = Math.min(g.depth / 2, dl - M.scale);
|
|
136
138
|
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)) {
|
|
139
|
+
if (fwd > 0 && !(gr.probeBlocked?.(x, y, lx, ly) ?? gr.blocked(gr.cellAt(lx, ly))) && !gr.lineBlocked(x, y, lx, ly)) {
|
|
138
140
|
x = lx;
|
|
139
141
|
y = ly;
|
|
140
142
|
}
|
|
@@ -222,6 +224,18 @@ function lookAhead(gr, g, M, f, cell, x = g.px, y = g.py, laid = true) {
|
|
|
222
224
|
// turn round the corner on the spot
|
|
223
225
|
const side = M.lookClear > 0 ? (laid ? wayAt(g, M) : g.width) * M.lookClear : 0;
|
|
224
226
|
let c = cell, ok = false, wide = false;
|
|
227
|
+
// A real passage can overlap a blocked cell with no flow direction. Rejoin the field through a visible
|
|
228
|
+
// neighbour; its centre remains the aim if the following bend hides every point further down the route.
|
|
229
|
+
if (f.dirs[c] === DIR_NONE && gr.probeBlocked && gr.blocked(c) && gr.probeBlocked(x, y, x, y) === false) {
|
|
230
|
+
c = nearestCell(mw, gr.h, cell, 1, candidate => f.dirs[candidate] !== DIR_NONE && !gr.blocked(candidate)
|
|
231
|
+
&& !gr.lineBlocked(x, y, (candidate % mw + 0.5) * T, (((candidate / mw) | 0) + 0.5) * T));
|
|
232
|
+
if (c !== cell) {
|
|
233
|
+
AIM.x = (c % mw + 0.5) * T;
|
|
234
|
+
AIM.y = (((c / mw) | 0) + 0.5) * T;
|
|
235
|
+
ok = true;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
let inPassage = false;
|
|
225
239
|
for (let k = 0; k < max; k++) {
|
|
226
240
|
const dir = f.dirs[c];
|
|
227
241
|
if (dir > 7) {
|
|
@@ -233,7 +247,28 @@ function lookAhead(gr, g, M, f, cell, x = g.px, y = g.py, laid = true) {
|
|
|
233
247
|
break;
|
|
234
248
|
}
|
|
235
249
|
c += DIRX[dir] + DIRY[dir] * mw;
|
|
236
|
-
|
|
250
|
+
let px = (c % mw + 0.5) * T, py = (((c / mw) | 0) + 0.5) * T;
|
|
251
|
+
let centered = false;
|
|
252
|
+
// A coarse route can alternate between the two edges of a straight passage. Aim down its measured middle
|
|
253
|
+
// wherever that shortcut is walkable. At junctions the ground can leave the waypoint to the field.
|
|
254
|
+
if (gr.crossSection) {
|
|
255
|
+
const d = dir & 1 ? Math.SQRT1_2 : 1;
|
|
256
|
+
const section = gr.crossSection(px, py, DIRX[dir] * d, DIRY[dir] * d);
|
|
257
|
+
if (section && section.negative + section.positive < g.width * M.narrow) {
|
|
258
|
+
const offset = (section.positive - section.negative) / 2;
|
|
259
|
+
const mx = px + section.axisX * offset, my = py + section.axisY * offset;
|
|
260
|
+
if (!gr.lineBlocked(x, y, mx, my)) {
|
|
261
|
+
px = mx;
|
|
262
|
+
py = my;
|
|
263
|
+
centered = true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
else if (inPassage) {
|
|
267
|
+
// Keep the exit as the waypoint until it is within one cell. Looking past it draws a straight
|
|
268
|
+
// corridor towards whichever corner happens to reveal the next cell outside.
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
237
272
|
if ((px - x) ** 2 + (py - y) ** 2 > look * look)
|
|
238
273
|
break;
|
|
239
274
|
if (gr.lineBlocked(x, y, px, py))
|
|
@@ -250,10 +285,14 @@ function lookAhead(gr, g, M, f, cell, x = g.px, y = g.py, laid = true) {
|
|
|
250
285
|
}
|
|
251
286
|
AIM.x = px;
|
|
252
287
|
AIM.y = py;
|
|
288
|
+
inPassage = centered && (px - x) ** 2 + (py - y) ** 2 > T * T;
|
|
253
289
|
ok = true;
|
|
254
290
|
}
|
|
255
291
|
return ok;
|
|
256
292
|
}
|
|
293
|
+
function probeStepBlocked(gr, g, cell, nc, nx, ny) {
|
|
294
|
+
return gr.probeBlocked?.(g.px, g.py, nx, ny) ?? (nc !== cell && (gr.blocked(nc) || !gr.canStep(cell, nc)));
|
|
295
|
+
}
|
|
257
296
|
/**
|
|
258
297
|
* The probe walks `dist` along the route (in steps of at most `PROBE_SUB`), recording the trail as it goes. It looks
|
|
259
298
|
* a stretch along the route and heads for the furthest point in sight (straightening the flow field's polyline). Its
|
|
@@ -273,7 +312,7 @@ function probeMove(gr, g, M, dist, vref) {
|
|
|
273
312
|
const cell = ((g.py / T) | 0) * mw + ((g.px / T) | 0);
|
|
274
313
|
const ddx = g.destX - g.px, ddy = g.destY - g.py;
|
|
275
314
|
const dd = Math.sqrt(ddx * ddx + ddy * ddy);
|
|
276
|
-
if (dd <= step + M.scale) {
|
|
315
|
+
if (dd <= step + M.scale && !gr.probeBlocked?.(g.px, g.py, g.destX, g.destY)) {
|
|
277
316
|
advance(gr, g, M, g.destX, g.destY, true);
|
|
278
317
|
g.pDone = true;
|
|
279
318
|
g.pBarred = false;
|
|
@@ -302,6 +341,10 @@ function probeMove(gr, g, M, dist, vref) {
|
|
|
302
341
|
ty = g.py + fdy * T;
|
|
303
342
|
}
|
|
304
343
|
}
|
|
344
|
+
else if (dir === DIR_NONE && gr.probeBlocked && lookAhead(gr, g, M, f, cell)) {
|
|
345
|
+
tx = AIM.x;
|
|
346
|
+
ty = AIM.y;
|
|
347
|
+
}
|
|
305
348
|
else if (dir !== DIR_HERE && f.pending)
|
|
306
349
|
return; // The route is not known here yet: wait
|
|
307
350
|
}
|
|
@@ -332,13 +375,15 @@ function probeMove(gr, g, M, dist, vref) {
|
|
|
332
375
|
ma += rest;
|
|
333
376
|
let nx = g.px + dmath.cos(ma) * step, ny = g.py + dmath.sin(ma) * step;
|
|
334
377
|
let nc = ((ny / T) | 0) * mw + ((nx / T) | 0);
|
|
335
|
-
|
|
378
|
+
let blocked = probeStepBlocked(gr, g, cell, nc, nx, ny);
|
|
379
|
+
if (blocked && (fdx || fdy)) {
|
|
336
380
|
// The shortcut ran into an obstacle: follow the flow field as it is
|
|
337
381
|
nx = g.px + fdx * step;
|
|
338
382
|
ny = g.py + fdy * step;
|
|
339
383
|
nc = ((ny / T) | 0) * mw + ((nx / T) | 0);
|
|
384
|
+
blocked = probeStepBlocked(gr, g, cell, nc, nx, ny);
|
|
340
385
|
}
|
|
341
|
-
if (
|
|
386
|
+
if (blocked) {
|
|
342
387
|
// No straight way: take the flow field from now on; where its route is barred, stop and wait
|
|
343
388
|
if (!g.field)
|
|
344
389
|
g.field = gr.field(g.destCell);
|
|
@@ -546,6 +591,10 @@ export function updateMarches(marches, grounds, members, M, dt, tick) {
|
|
|
546
591
|
continue;
|
|
547
592
|
const step = sp * dt;
|
|
548
593
|
if (g.pDone && !g.tail && g.lead <= step + M.scale) {
|
|
594
|
+
if (gr.probeBlocked?.(g.x, g.y, g.destX, g.destY)) {
|
|
595
|
+
anchor(gr, g, M, g.x, g.y, true);
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
549
598
|
g.arrived = true;
|
|
550
599
|
if (needTail(g, M)) {
|
|
551
600
|
// Ranks behind have not passed the narrow place: the column stays and keeps leading; members leave one by one
|
|
@@ -580,10 +629,15 @@ export function updateMarches(marches, grounds, members, M, dt, tick) {
|
|
|
580
629
|
}
|
|
581
630
|
if (!g.tail)
|
|
582
631
|
g.blocked = g.pBarred && g.lead < M.scale;
|
|
632
|
+
const oldX = g.x, oldY = g.y;
|
|
583
633
|
placeFront(g, M);
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
634
|
+
if (!g.tail && df > 0) {
|
|
635
|
+
// A gate narrower than the trail sampling interval can close behind the probe.
|
|
636
|
+
if (gr.probeBlocked?.(oldX, oldY, g.x, g.y))
|
|
637
|
+
anchor(gr, g, M, oldX, oldY, true);
|
|
638
|
+
else if (routeCut(gr, g, M))
|
|
639
|
+
anchor(gr, g, M, g.x, g.y, true);
|
|
640
|
+
}
|
|
587
641
|
rowsAt(g, M, g.lead, g.rowB, g.rowLen, g.rowE);
|
|
588
642
|
}
|
|
589
643
|
}
|
package/dist/marchChoke.js
CHANGED
|
@@ -106,6 +106,24 @@ function wallRuns(gr, g, M, x, y, ux, uy) {
|
|
|
106
106
|
*/
|
|
107
107
|
export function chokeAt(gr, g, M, x, y, tx, ty) {
|
|
108
108
|
chokeClear(g);
|
|
109
|
+
const section = gr.crossSection?.(x, y, tx, ty);
|
|
110
|
+
if (section) {
|
|
111
|
+
const { axisX, axisY, positive, negative } = section;
|
|
112
|
+
const forward = -ty * axisX + tx * axisY >= 0;
|
|
113
|
+
const r = forward ? positive : negative, l = forward ? negative : positive;
|
|
114
|
+
if (r === Infinity && l === Infinity)
|
|
115
|
+
return;
|
|
116
|
+
// An unbounded wing has room for a whole rank to shift away from the other edge.
|
|
117
|
+
const unbounded = M.flank ? g.open : g.open + g.width / 2;
|
|
118
|
+
CHOKE.R = r === Infinity ? unbounded : Math.max(0, r - M.chokePad);
|
|
119
|
+
CHOKE.L = l === Infinity ? unbounded : Math.max(0, l - M.chokePad);
|
|
120
|
+
if (r + l >= g.width * M.narrow)
|
|
121
|
+
return;
|
|
122
|
+
CHOKE.ax = forward ? axisX : -axisX;
|
|
123
|
+
CHOKE.ay = forward ? axisY : -axisY;
|
|
124
|
+
CHOKE.has = 1;
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
109
127
|
let best = g.width * M.narrow;
|
|
110
128
|
for (let a = 0; a < AXES.length; a += 2) {
|
|
111
129
|
const ux = AXES[a], uy = AXES[a + 1];
|
package/dist/marchTrail.js
CHANGED
|
@@ -277,6 +277,9 @@ export function routeCut(gr, g, M) {
|
|
|
277
277
|
const k = ((g.lead - g.rec) / M.trailStep) | 0;
|
|
278
278
|
if (k >= g.cap)
|
|
279
279
|
return false;
|
|
280
|
-
const
|
|
281
|
-
|
|
280
|
+
const o = pt(g, k), c = g.trail[o + T_CELL];
|
|
281
|
+
if (c < 0)
|
|
282
|
+
return false;
|
|
283
|
+
const x = g.trail[o], y = g.trail[o + 1];
|
|
284
|
+
return gr.probeBlocked?.(x, y, x, y) ?? gr.solid(c);
|
|
282
285
|
}
|
package/dist/marchTypes.d.ts
CHANGED
|
@@ -121,6 +121,15 @@ export declare const DEFAULT_MARCH_RULES: Readonly<MarchRules>;
|
|
|
121
121
|
* @param overrides Rule values to use instead, as given (not scaled).
|
|
122
122
|
*/
|
|
123
123
|
export declare function marchRules(scale: number, overrides?: Partial<Omit<MarchRules, 'scale'>>): MarchRules;
|
|
124
|
+
/** Physical edges of a local passage, measured from the query point in world units. */
|
|
125
|
+
export interface MarchCrossSection {
|
|
126
|
+
/** Unit vector across the passage. Either orientation is valid. */
|
|
127
|
+
axisX: number;
|
|
128
|
+
axisY: number;
|
|
129
|
+
/** Non-negative distances to the edges along and against the axis, before padding or member radii. Infinity means open. */
|
|
130
|
+
positive: number;
|
|
131
|
+
negative: number;
|
|
132
|
+
}
|
|
124
133
|
/**
|
|
125
134
|
* The map as one group of marchers sees it. A game with several movement layers (sides with different gates, say)
|
|
126
135
|
* gives one ground per layer. Cells are numbered row-major, `cell = y × w + x`.
|
|
@@ -135,11 +144,18 @@ export interface MarchGround {
|
|
|
135
144
|
/** The cell holding a world position. */
|
|
136
145
|
cellAt(x: number, y: number): number;
|
|
137
146
|
/**
|
|
138
|
-
*
|
|
139
|
-
* one. Every blocked cell is solid; a game may have solid cells that can be walked over (a wall top, say).
|
|
147
|
+
* Solid for cell-based width measurements, forming up and trail checks. Every blocked cell is solid; a game may have solid cells that can be walked over (a wall top, say).
|
|
140
148
|
*/
|
|
141
149
|
solid(cell: number): boolean;
|
|
142
|
-
/**
|
|
150
|
+
/** Local cross-section at a trail point travelling along (tx, ty). Return undefined to measure the solid cells. */
|
|
151
|
+
crossSection?(x: number, y: number, tx: number, ty: number): MarchCrossSection | undefined;
|
|
152
|
+
/**
|
|
153
|
+
* Optional world-space obstruction test for a probe step, including steps inside one cell and the final step
|
|
154
|
+
* to the goal. A zero-length step tests a point when forming up or rechecking the trail. Return undefined to use
|
|
155
|
+
* the ordinary cell rules here. Keep this consistent with standable, lineBlocked and lineReach, including gates.
|
|
156
|
+
*/
|
|
157
|
+
probeBlocked?(ax: number, ay: number, bx: number, by: number): boolean | undefined;
|
|
158
|
+
/** The cell cannot be walked into under the cell-based probe rules. */
|
|
143
159
|
blocked(cell: number): boolean;
|
|
144
160
|
/** Whether a member may step from a cell to a neighbouring one (both passable). */
|
|
145
161
|
canStep(from: number, to: number): boolean;
|
|
@@ -150,7 +166,7 @@ export interface MarchGround {
|
|
|
150
166
|
/** Whether the straight walk between two points is blocked, and how much of it is clear (1 = all of it). */
|
|
151
167
|
lineBlocked(ax: number, ay: number, bx: number, by: number): boolean;
|
|
152
168
|
lineReach(ax: number, ay: number, bx: number, by: number): number;
|
|
153
|
-
/** Whether a member can stand at
|
|
169
|
+
/** Whether a member can stand at this world position, including any subcell terrain. */
|
|
154
170
|
standable(x: number, y: number): boolean;
|
|
155
171
|
/** The flow field towards a cell, and finishing its search at once. */
|
|
156
172
|
field(cell: number): FlowField;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pocketrts/grid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
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>",
|