@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
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Short-range A* on an 8-connected grid with a path-length limit, for steering around small obstacles where a whole
|
|
3
|
+
* flow field per goal would be wasteful (a chaser whose target cell changes every few steps, say).
|
|
4
|
+
*
|
|
5
|
+
* One instance owns its search buffers and reuses them across calls; it is not re-entrant. Ties are broken by heap
|
|
6
|
+
* order, so the same inputs always give the same path.
|
|
7
|
+
*/
|
|
8
|
+
import { DIRX, DIRY } from './dirs.js';
|
|
9
|
+
const SQRT2 = Math.SQRT2;
|
|
10
|
+
export class LocalPathfinder {
|
|
11
|
+
maxNodes;
|
|
12
|
+
/**
|
|
13
|
+
* The last path found, goal first, start last (`length` cells, truncated to `maxNodes + 1` cells when longer).
|
|
14
|
+
* Valid until the next `find`.
|
|
15
|
+
*/
|
|
16
|
+
path;
|
|
17
|
+
/** Cells in `path`; 0 after a failed search. */
|
|
18
|
+
length = 0;
|
|
19
|
+
heapF;
|
|
20
|
+
heapC;
|
|
21
|
+
cells = 0;
|
|
22
|
+
gen = 0;
|
|
23
|
+
stamp = new Int32Array(0);
|
|
24
|
+
gScore = new Float64Array(0);
|
|
25
|
+
parent = new Int32Array(0);
|
|
26
|
+
closed = new Int32Array(0);
|
|
27
|
+
/** @param maxNodes Most cells a search expands before giving up (a safeguard beyond the length limit). Default 1200. */
|
|
28
|
+
constructor(maxNodes = 1200) {
|
|
29
|
+
this.maxNodes = maxNodes;
|
|
30
|
+
this.heapF = new Float64Array(maxNodes * 8 + 1);
|
|
31
|
+
this.heapC = new Int32Array(maxNodes * 8 + 1);
|
|
32
|
+
this.path = new Int32Array(maxNodes + 1);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Find a path from cell `start` to cell `goal` on a `w` × `h` grid whose length (straight steps 1, diagonal √2)
|
|
36
|
+
* stays within `limit` cells, using the octile distance as the estimate. Returns whether one was found; the path is
|
|
37
|
+
* then in `path` / `length`. A blocked goal is never reached.
|
|
38
|
+
*/
|
|
39
|
+
find(w, h, start, goal, limit, rules) {
|
|
40
|
+
const n = w * h;
|
|
41
|
+
if (n !== this.cells) {
|
|
42
|
+
this.cells = n;
|
|
43
|
+
this.stamp = new Int32Array(n);
|
|
44
|
+
this.closed = new Int32Array(n);
|
|
45
|
+
this.gScore = new Float64Array(n);
|
|
46
|
+
this.parent = new Int32Array(n);
|
|
47
|
+
this.gen = 0;
|
|
48
|
+
}
|
|
49
|
+
if (++this.gen === 0x7fffffff) {
|
|
50
|
+
this.stamp.fill(0);
|
|
51
|
+
this.closed.fill(0);
|
|
52
|
+
this.gen = 1;
|
|
53
|
+
}
|
|
54
|
+
this.length = 0;
|
|
55
|
+
const { heapF, heapC, stamp, closed, gScore, parent, gen } = this;
|
|
56
|
+
const blk = rules.block;
|
|
57
|
+
if (blk[goal])
|
|
58
|
+
return false;
|
|
59
|
+
const gx = goal % w, gy = (goal / w) | 0;
|
|
60
|
+
let hs = 0, expanded = 0, found = false;
|
|
61
|
+
stamp[start] = gen;
|
|
62
|
+
gScore[start] = 0;
|
|
63
|
+
parent[start] = -1;
|
|
64
|
+
heapF[0] = 0;
|
|
65
|
+
heapC[hs++] = start;
|
|
66
|
+
while (hs > 0) {
|
|
67
|
+
const c = heapC[0];
|
|
68
|
+
hs--;
|
|
69
|
+
if (hs > 0) {
|
|
70
|
+
const lf = heapF[hs], lc = heapC[hs];
|
|
71
|
+
let k = 0;
|
|
72
|
+
for (;;) {
|
|
73
|
+
let l = 2 * k + 1;
|
|
74
|
+
if (l >= hs)
|
|
75
|
+
break;
|
|
76
|
+
if (l + 1 < hs && heapF[l + 1] < heapF[l])
|
|
77
|
+
l++;
|
|
78
|
+
if (heapF[l] >= lf)
|
|
79
|
+
break;
|
|
80
|
+
heapF[k] = heapF[l];
|
|
81
|
+
heapC[k] = heapC[l];
|
|
82
|
+
k = l;
|
|
83
|
+
}
|
|
84
|
+
heapF[k] = lf;
|
|
85
|
+
heapC[k] = lc;
|
|
86
|
+
}
|
|
87
|
+
if (closed[c] === gen)
|
|
88
|
+
continue;
|
|
89
|
+
closed[c] = gen;
|
|
90
|
+
if (c === goal) {
|
|
91
|
+
found = true;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
if (++expanded > this.maxNodes)
|
|
95
|
+
break;
|
|
96
|
+
const cx = c % w, cy = (c / w) | 0, gc = gScore[c];
|
|
97
|
+
for (let dir = 0; dir < 8; dir++) {
|
|
98
|
+
const nx = cx + DIRX[dir], ny = cy + DIRY[dir];
|
|
99
|
+
if (nx < 0 || ny < 0 || nx >= w || ny >= h)
|
|
100
|
+
continue;
|
|
101
|
+
const nc = ny * w + nx;
|
|
102
|
+
if (closed[nc] === gen || blk[nc])
|
|
103
|
+
continue;
|
|
104
|
+
let ng;
|
|
105
|
+
if (dir & 1) {
|
|
106
|
+
if (!rules.canStepDiag(c, nc))
|
|
107
|
+
continue;
|
|
108
|
+
ng = gc + SQRT2;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
if (!rules.canStep(c, nc))
|
|
112
|
+
continue;
|
|
113
|
+
ng = gc + 1;
|
|
114
|
+
}
|
|
115
|
+
if (stamp[nc] === gen && gScore[nc] <= ng)
|
|
116
|
+
continue;
|
|
117
|
+
const hx = Math.abs(nx - gx), hy = Math.abs(ny - gy);
|
|
118
|
+
const nf = ng + (hx > hy ? hx - hy + hy * SQRT2 : hy - hx + hx * SQRT2);
|
|
119
|
+
if (nf > limit || hs >= heapF.length)
|
|
120
|
+
continue;
|
|
121
|
+
stamp[nc] = gen;
|
|
122
|
+
gScore[nc] = ng;
|
|
123
|
+
parent[nc] = c;
|
|
124
|
+
let k = hs++;
|
|
125
|
+
while (k > 0) {
|
|
126
|
+
const p = (k - 1) >> 1;
|
|
127
|
+
if (heapF[p] <= nf)
|
|
128
|
+
break;
|
|
129
|
+
heapF[k] = heapF[p];
|
|
130
|
+
heapC[k] = heapC[p];
|
|
131
|
+
k = p;
|
|
132
|
+
}
|
|
133
|
+
heapF[k] = nf;
|
|
134
|
+
heapC[k] = nc;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (!found)
|
|
138
|
+
return false;
|
|
139
|
+
let len = 0;
|
|
140
|
+
for (let c = goal; c !== -1 && len < this.path.length; c = parent[c])
|
|
141
|
+
this.path[len++] = c;
|
|
142
|
+
this.length = len;
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
}
|
package/dist/march.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Formation } from './formation.js';
|
|
2
|
+
import { type March, type MarchGround, type MarchMembers, type MarchRules } from './marchTypes.js';
|
|
3
|
+
/** What `createMarch` needs to know about the order. */
|
|
4
|
+
export interface MarchOrder {
|
|
5
|
+
/** The movement layer the group marches on; the ground passed along must be that layer's. */
|
|
6
|
+
layer: number;
|
|
7
|
+
/** The centre of the group: the column forms up round it, the front rank at its front. */
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
/** The goal, and the cell it is in. */
|
|
11
|
+
destX: number;
|
|
12
|
+
destY: number;
|
|
13
|
+
destCell: number;
|
|
14
|
+
/** The slowest member's marching speed and full speed, in world units per second. */
|
|
15
|
+
speed: number;
|
|
16
|
+
run: number;
|
|
17
|
+
/** Whether the front rank halts while enough members are engaged. */
|
|
18
|
+
hold: boolean;
|
|
19
|
+
/** The facing at the goal. */
|
|
20
|
+
fx: number;
|
|
21
|
+
fy: number;
|
|
22
|
+
/** The members' ranks, from `lineFormation` (member speeds in world units per second). Each member keeps its rank, place and lateral offset for the whole march. */
|
|
23
|
+
formation: Formation;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Set a group marching. The march is appended to `marches`, or reuses the first one in it that is no longer alive
|
|
27
|
+
* (so its `id` is its index there). Whether a group is compact enough to march together is the game's call, before
|
|
28
|
+
* it asks.
|
|
29
|
+
*/
|
|
30
|
+
export declare function createMarch(marches: March[], gr: MarchGround, M: MarchRules, order: MarchOrder): March;
|
|
31
|
+
/**
|
|
32
|
+
* Turn a marching group onto a new route (the same members ordered elsewhere) without forming up again: the column
|
|
33
|
+
* and its trail stay, and the front rank turns straight for the new goal. Returns false and changes nothing when the
|
|
34
|
+
* new route sets off at an angle to the front rank's heading whose cosine is below `rerouteCos` (it would have to
|
|
35
|
+
* turn back); the game then starts a new march. For a march still on its way: one that has arrived is not asked.
|
|
36
|
+
*
|
|
37
|
+
* The stretch of trail the probe had walked ahead of the front rank is dropped, the probe returns to the front rank
|
|
38
|
+
* and walks out its lead along the new route. The points kept get their envelope redone from the widths measured at
|
|
39
|
+
* each: narrowing passed back from the dropped stretch is gone, narrow places behind still count.
|
|
40
|
+
*/
|
|
41
|
+
export declare function rerouteMarch(gr: MarchGround, g: March, M: MarchRules, destX: number, destY: number, destCell: number, hold: boolean, fx: number, fy: number): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Advance every march by one step of `dt` seconds; call once per simulation step, before the members move.
|
|
44
|
+
* @param grounds The ground of a movement layer.
|
|
45
|
+
* @param tick The simulation step: the members are tallied on every eighth.
|
|
46
|
+
*/
|
|
47
|
+
export declare function updateMarches(marches: March[], grounds: (layer: number) => MarchGround, members: MarchMembers, M: MarchRules, dt: number, tick: number): void;
|
|
48
|
+
/**
|
|
49
|
+
* Result of `steerMember`. With `STEER_KEEP`: the velocity to move at; `set` is false when the member is on its
|
|
50
|
+
* place and the answer is "as you were" (the velocity is then zero, and a game that had the member moving for a
|
|
51
|
+
* reason of its own may leave that be). With `STEER_APART` and `STEER_PUSH`: the speed to move at along its own
|
|
52
|
+
* route. `apart`: whether the member is off its place this step (the game keeps it for `MarchMembers.apart`).
|
|
53
|
+
*/
|
|
54
|
+
export declare const STEER: {
|
|
55
|
+
vx: number;
|
|
56
|
+
vy: number;
|
|
57
|
+
set: boolean;
|
|
58
|
+
speed: number;
|
|
59
|
+
apart: boolean;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* What `steerMember` returns:
|
|
63
|
+
* - `STEER_IDLE`: the group is not marching in ranks right now; the member moves as if it had no march.
|
|
64
|
+
* - `STEER_LEAVE`: the member has left the march (the game clears its membership) and makes for its own spot.
|
|
65
|
+
* - `STEER_PUSH`: the member stays in the march but walks on along its route instead of waiting for its place.
|
|
66
|
+
* - `STEER_APART`: the member finds its own way along its route at `STEER.speed`, and rejoins when it can.
|
|
67
|
+
* - `STEER_KEEP`: the member keeps its place: it moves at (`STEER.vx`, `STEER.vy`).
|
|
68
|
+
*/
|
|
69
|
+
export declare const STEER_IDLE = 0, STEER_LEAVE = 1, STEER_PUSH = 2, STEER_APART = 3, STEER_KEEP = 4;
|
|
70
|
+
/**
|
|
71
|
+
* What a member of march `g` does this step; call it for each member after `updateMarches`.
|
|
72
|
+
* @param row, col, lateral The member's rank, place in it and lateral offset, from the formation the march was made with.
|
|
73
|
+
* @param ux, uy Where the member stands; destX, destY its own spot at the goal.
|
|
74
|
+
* @param speed, run The member's speed now and its full speed, in world units per second.
|
|
75
|
+
* @param free The member acts on its own this step (it keeps no place).
|
|
76
|
+
*/
|
|
77
|
+
export declare function steerMember(gr: MarchGround, g: March, M: MarchRules, row: number, col: number, lateral: number, ux: number, uy: number, destX: number, destY: number, speed: number, run: number, free: boolean): number;
|