@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.
@@ -0,0 +1,85 @@
1
+ /** One movement layer of the grid. */
2
+ export interface FlowLayer {
3
+ /**
4
+ * Cost of entering each cell, in tenths (an ordinary cell is 10); 0 = impassable. A diagonal step costs ⌊cost × 1.4142⌋.
5
+ * The outermost ring of cells must be 0: searches do not check the grid bounds.
6
+ */
7
+ readonly cost: Uint16Array;
8
+ /** Whether a unit may step straight from cell `a` to the adjacent cell `b` (both passable). */
9
+ canStep(a: number, b: number): boolean;
10
+ /** Whether a unit may step diagonally from cell `a` to the diagonal cell `b`. See `cornerFree` for the usual rule. */
11
+ canStepDiag(a: number, b: number): boolean;
12
+ }
13
+ export interface FlowField {
14
+ /** The cache key the field was requested under. */
15
+ readonly key: number;
16
+ /** The layer it was computed on. */
17
+ readonly layer: number;
18
+ /** The first goal cell. */
19
+ readonly cell: number;
20
+ readonly seeds: Int32Array;
21
+ /** Per cell: the direction index (`DIRX` / `DIRY`) of the next step, `DIR_HERE` on a goal, `DIR_NONE` if not reached. */
22
+ dirs: Uint8Array;
23
+ /** The grid version the current `dirs` were computed for. */
24
+ version: number;
25
+ /** The last step it was requested on. */
26
+ lastUse: number;
27
+ /** Evicted from the cache: request a new field instead of keeping this one. */
28
+ dead: boolean;
29
+ /** A search is running: the first one (`dirs` partial), or a refresh after `invalidate` (`dirs` still the previous result). */
30
+ pending: boolean;
31
+ }
32
+ export interface FlowFieldOptions {
33
+ /** Heap pops per `maintain` call, across all searches (about 1–2 ms at the default). Default 22000. */
34
+ budget: number;
35
+ /** Every this many steps, one outdated field still in use starts recomputing (when at most one search is queued). Default 20. */
36
+ refreshEvery: number;
37
+ /** A field counts as in use when it was requested within this many steps. Default 60. */
38
+ refreshWithin: number;
39
+ /** Fields are evicted only while the cache holds more than this many. Default 48. */
40
+ evictAbove: number;
41
+ /** Every this many steps, fields unused for `evictAfter` steps are evicted. Default 64. */
42
+ evictEvery: number;
43
+ /** Steps without a request after which a field may be evicted. Default 300. */
44
+ evictAfter: number;
45
+ }
46
+ export declare const DEFAULT_FLOW_FIELD_OPTIONS: Readonly<FlowFieldOptions>;
47
+ /**
48
+ * The usual diagonal rule: a diagonal step from `a` to `b` is allowed only when neither cell nor either of the two
49
+ * corner cells it brushes past is blocked, so units never squeeze through the gap between two diagonal obstacles.
50
+ * Symmetric in `a` and `b`. `w` is the grid width.
51
+ */
52
+ export declare function cornerFree(block: ArrayLike<number>, w: number, a: number, b: number): boolean;
53
+ export declare class FlowFields {
54
+ readonly w: number;
55
+ readonly h: number;
56
+ readonly layers: readonly FlowLayer[];
57
+ /** Number of cells. */
58
+ readonly n: number;
59
+ readonly options: Readonly<FlowFieldOptions>;
60
+ /** Grid version: raised by `invalidate()`. */
61
+ version: number;
62
+ /** Cached fields by key, in creation order. */
63
+ readonly fields: Map<number, FlowField>;
64
+ /** Searches started so far (statistics). */
65
+ computed: number;
66
+ private readonly jobs;
67
+ private readonly pool;
68
+ private readonly off;
69
+ constructor(w: number, h: number, layers: readonly FlowLayer[], options?: Partial<FlowFieldOptions>);
70
+ /** Call after changing any layer's costs or step rules. */
71
+ invalidate(): void;
72
+ /**
73
+ * The field cached under `key`, or a new one computed on `layer` towards `seeds` (called only when it is not cached).
74
+ * Keys are the caller's: they must tell layers and goals apart, and two layers that always move alike may share keys.
75
+ */
76
+ get(key: number, layer: number, tick: number, seeds: () => Int32Array): FlowField;
77
+ /** Finish a field's pending search at once (tests and the few callers that need a synchronous result). */
78
+ finish(f: FlowField): void;
79
+ /** Call once per simulation step: advance queued searches, refresh outdated fields, evict unused ones. */
80
+ maintain(tick: number): void;
81
+ private start;
82
+ private done;
83
+ /** Advance one search by at most `budget` heap pops; returns the budget left. */
84
+ private step;
85
+ }
@@ -0,0 +1,214 @@
1
+ /**
2
+ * Flow fields over an 8-connected grid: one field per goal, shared by every unit heading there, so pathfinding cost
3
+ * does not grow with the number of units.
4
+ *
5
+ * A field is a Dijkstra search spreading out from the goal cells, recording for each cell the direction of the next
6
+ * step. Searches are spread over steps with a fixed node budget per `maintain` call, so no single step stalls; a cell
7
+ * not reached yet reads `DIR_NONE`, and units should head straight for the goal until it fills in. The budget is a
8
+ * node count, not a time, so every peer finishes a field on the same step.
9
+ *
10
+ * The game owns the grid itself as layers (typically one per side or per movement class), each a cost array plus its
11
+ * step rules; after changing any layer, call `invalidate()`: fields still in use are recomputed in the background and
12
+ * swapped in whole when done.
13
+ */
14
+ import { DIR_HERE, DIR_NONE, DIRX, DIRY } from './dirs.js';
15
+ export const DEFAULT_FLOW_FIELD_OPTIONS = Object.freeze({
16
+ budget: 22000,
17
+ refreshEvery: 20,
18
+ refreshWithin: 60,
19
+ evictAbove: 48,
20
+ evictEvery: 64,
21
+ evictAfter: 300,
22
+ });
23
+ /**
24
+ * The usual diagonal rule: a diagonal step from `a` to `b` is allowed only when neither cell nor either of the two
25
+ * corner cells it brushes past is blocked, so units never squeeze through the gap between two diagonal obstacles.
26
+ * Symmetric in `a` and `b`. `w` is the grid width.
27
+ */
28
+ export function cornerFree(block, w, a, b) {
29
+ const dx = (b % w) - (a % w);
30
+ return !(block[a] || block[b] || block[a + dx] || block[b - dx]);
31
+ }
32
+ export class FlowFields {
33
+ w;
34
+ h;
35
+ layers;
36
+ /** Number of cells. */
37
+ n;
38
+ options;
39
+ /** Grid version: raised by `invalidate()`. */
40
+ version = 1;
41
+ /** Cached fields by key, in creation order. */
42
+ fields = new Map();
43
+ /** Searches started so far (statistics). */
44
+ computed = 0;
45
+ jobs = [];
46
+ pool = [];
47
+ off;
48
+ constructor(w, h, layers, options = {}) {
49
+ this.w = w;
50
+ this.h = h;
51
+ this.layers = layers;
52
+ this.n = w * h;
53
+ this.options = { ...DEFAULT_FLOW_FIELD_OPTIONS, ...options };
54
+ this.off = Int32Array.from(DIRX.map((dx, i) => dx + DIRY[i] * w));
55
+ }
56
+ /** Call after changing any layer's costs or step rules. */
57
+ invalidate() {
58
+ this.version++;
59
+ }
60
+ /**
61
+ * The field cached under `key`, or a new one computed on `layer` towards `seeds` (called only when it is not cached).
62
+ * Keys are the caller's: they must tell layers and goals apart, and two layers that always move alike may share keys.
63
+ */
64
+ get(key, layer, tick, seeds) {
65
+ let f = this.fields.get(key);
66
+ if (!f) {
67
+ const s = seeds();
68
+ f = {
69
+ key, layer, cell: s[0], seeds: s, dirs: new Uint8Array(this.n).fill(DIR_NONE), version: this.version, lastUse: tick,
70
+ dead: false, pending: true,
71
+ };
72
+ this.fields.set(key, f);
73
+ this.start(f, f.dirs);
74
+ }
75
+ f.lastUse = tick;
76
+ return f;
77
+ }
78
+ /** Finish a field's pending search at once (tests and the few callers that need a synchronous result). */
79
+ finish(f) {
80
+ const i = this.jobs.findIndex(j => j.field === f);
81
+ if (i < 0)
82
+ return;
83
+ const job = this.jobs[i];
84
+ this.step(job, Infinity);
85
+ this.done(job);
86
+ this.jobs.splice(i, 1);
87
+ }
88
+ /** Call once per simulation step: advance queued searches, refresh outdated fields, evict unused ones. */
89
+ maintain(tick) {
90
+ const o = this.options;
91
+ let budget = o.budget;
92
+ while (budget > 0 && this.jobs.length) {
93
+ const job = this.jobs[0];
94
+ budget = this.step(job, budget);
95
+ if (job.hs > 0)
96
+ break;
97
+ this.done(job);
98
+ this.jobs.shift();
99
+ }
100
+ if (tick % o.refreshEvery === 0 && this.jobs.length < 2) {
101
+ for (const f of this.fields.values()) {
102
+ if (f.version !== this.version && !f.pending && f.lastUse >= tick - o.refreshWithin) {
103
+ this.start(f, new Uint8Array(this.n).fill(DIR_NONE));
104
+ break;
105
+ }
106
+ }
107
+ }
108
+ if (this.fields.size > o.evictAbove && tick % o.evictEvery === 0) {
109
+ for (const [k, f] of this.fields) {
110
+ if (f.lastUse < tick - o.evictAfter && !f.pending) {
111
+ f.dead = true;
112
+ this.fields.delete(k);
113
+ }
114
+ }
115
+ }
116
+ }
117
+ start(f, dirs) {
118
+ this.computed++;
119
+ const buf = this.pool.pop() ?? { dist: new Uint32Array(this.n), hd: new Uint32Array(this.n * 2), hc: new Int32Array(this.n * 2) };
120
+ const job = { field: f, dirs, dist: buf.dist, hd: buf.hd, hc: buf.hc, hs: 0, version: this.version };
121
+ job.dist.fill(0xffffffff);
122
+ for (let i = 0; i < f.seeds.length; i++) {
123
+ const s = f.seeds[i];
124
+ job.dist[s] = 0;
125
+ dirs[s] = DIR_HERE;
126
+ job.hd[job.hs] = 0;
127
+ job.hc[job.hs++] = s;
128
+ }
129
+ f.pending = true;
130
+ this.jobs.push(job);
131
+ }
132
+ done(job) {
133
+ const f = job.field;
134
+ f.dirs = job.dirs;
135
+ f.version = job.version;
136
+ f.pending = false;
137
+ this.pool.push({ dist: job.dist, hd: job.hd, hc: job.hc });
138
+ }
139
+ /** Advance one search by at most `budget` heap pops; returns the budget left. */
140
+ step(job, budget) {
141
+ const layer = this.layers[job.field.layer], cost = layer.cost;
142
+ const dist = job.dist, dirs = job.dirs;
143
+ const off = this.off;
144
+ let hd = job.hd, hc = job.hc, hs = job.hs;
145
+ while (hs > 0 && budget-- > 0) {
146
+ const d = hd[0], c = hc[0];
147
+ hs--;
148
+ if (hs > 0) {
149
+ const ld = hd[hs], lc = hc[hs];
150
+ let i = 0;
151
+ for (;;) {
152
+ let l = 2 * i + 1;
153
+ if (l >= hs)
154
+ break;
155
+ if (l + 1 < hs && hd[l + 1] < hd[l])
156
+ l++;
157
+ if (hd[l] >= ld)
158
+ break;
159
+ hd[i] = hd[l];
160
+ hc[i] = hc[l];
161
+ i = l;
162
+ }
163
+ hd[i] = ld;
164
+ hc[i] = lc;
165
+ }
166
+ if (d > dist[c])
167
+ continue;
168
+ // Cost of entering c; a goal cell always costs 10
169
+ const cc = d === 0 ? 10 : cost[c];
170
+ const cd = (cc * 1.4142) | 0;
171
+ for (let dir = 0; dir < 8; dir++) {
172
+ const nc = c + off[dir];
173
+ if (cost[nc] === 0)
174
+ continue;
175
+ let nd;
176
+ // The search spreads out from the goal: this edge is a unit stepping from nc to c
177
+ if (dir & 1) {
178
+ if (!layer.canStepDiag(nc, c))
179
+ continue;
180
+ nd = d + cd;
181
+ }
182
+ else {
183
+ if (!layer.canStep(nc, c))
184
+ continue;
185
+ nd = d + cc;
186
+ }
187
+ if (nd < dist[nc]) {
188
+ dist[nc] = nd;
189
+ dirs[nc] = (dir + 4) & 7;
190
+ if (hs >= hd.length) {
191
+ const nhd = new Uint32Array(hd.length * 2), nhc = new Int32Array(hc.length * 2);
192
+ nhd.set(hd);
193
+ nhc.set(hc);
194
+ hd = job.hd = nhd;
195
+ hc = job.hc = nhc;
196
+ }
197
+ let i = hs++;
198
+ while (i > 0) {
199
+ const p = (i - 1) >> 1;
200
+ if (hd[p] <= nd)
201
+ break;
202
+ hd[i] = hd[p];
203
+ hc[i] = hc[p];
204
+ i = p;
205
+ }
206
+ hd[i] = nd;
207
+ hc[i] = nc;
208
+ }
209
+ }
210
+ }
211
+ job.hs = hs;
212
+ return budget;
213
+ }
214
+ }
@@ -0,0 +1,74 @@
1
+ /** What `lineFormation` needs to know about one member. */
2
+ export interface FormationUnit {
3
+ /** Rank class: lower classes stand further forward; one rank only ever holds one class. */
4
+ row: number;
5
+ radius: number;
6
+ /** Running speed, for each rank's slowest speed. */
7
+ speed: number;
8
+ }
9
+ export interface FormationOptions {
10
+ /** Spacing added to each unit's diameter, side by side and front to back. Default 3. */
11
+ gap: number;
12
+ /** Narrowest frontage. Default 48. */
13
+ minWidth: number;
14
+ /** Frontage = √(total footprint area × aspect), so the block is about `aspect` times wider than deep. Default 2.6. */
15
+ aspect: number;
16
+ /**
17
+ * Wedge: how much wider each rank may be than the one before it, per unit of rank depth; the front rank may be
18
+ * half that times its own depth wide. With 2 and members of one size the ranks hold 1, 3, 5, … members, up to the
19
+ * frontage. Default 0: every rank may be as wide as the frontage.
20
+ */
21
+ taper: number;
22
+ }
23
+ export declare const DEFAULT_FORMATION_OPTIONS: Readonly<FormationOptions>;
24
+ /** The shapes `formationShape` knows. */
25
+ export type FormationShape = 'line' | 'column' | 'wedge' | 'loose';
26
+ export declare const FORMATION_SHAPES: readonly FormationShape[];
27
+ /**
28
+ * The options of a named shape, for `lineFormation`:
29
+ * - `line`: the defaults, a block about 2.6 times wider than deep;
30
+ * - `column`: a block about three times deeper than wide, for the march;
31
+ * - `wedge`: a point in front, each rank one member wider on each side than the one before;
32
+ * - `loose`: a line with a member's width of room between neighbours.
33
+ *
34
+ * Whatever its shape, a formation is ranks, so a march carries it the same way (see `createMarch`).
35
+ * @param scale The size of the world relative to the one the defaults were made for (members about 12 across): the
36
+ * options that are lengths are multiplied by it, as `marchRules` does for the march.
37
+ */
38
+ export declare function formationShape(shape: FormationShape, scale?: number): FormationOptions;
39
+ /** The result of `lineFormation`, per member (in input order) and per rank (front to back). */
40
+ export interface Formation {
41
+ /** Each member's spot. */
42
+ sx: Float32Array;
43
+ sy: Float32Array;
44
+ /** Each member's offset from the centre along the facing direction and across it. */
45
+ fa: Float32Array;
46
+ fl: Float32Array;
47
+ /** Each member's rank (0 = front) and place in the rank (0 = leftmost, i.e. smallest lateral offset). */
48
+ row: Int16Array;
49
+ col: Int16Array;
50
+ /** Per rank: total width (sum of spacings), depth (largest spacing), members, offset along the facing direction, largest radius, slowest speed. */
51
+ rowW: number[];
52
+ rowD: number[];
53
+ rowN: number[];
54
+ rowFa: number[];
55
+ rowRad: number[];
56
+ rowRun: number[];
57
+ /** Radius of the circle around the whole block. */
58
+ radius: number;
59
+ width: number;
60
+ depth: number;
61
+ }
62
+ /**
63
+ * Ranks centred on (cx, cy) facing (dx, dy) (any length; a zero vector faces −y). Members are sorted by rank class,
64
+ * then front to back by their current position when `positions` is given, then by input order, and poured into ranks
65
+ * no wider than the frontage (with `taper`, the ranks in front narrower still: a wedge), a new rank starting at each
66
+ * class change. Within a rank, members keep that order, or
67
+ * with `positions` are placed left to right by current lateral position, which keeps paths from crossing. Facing east
68
+ * (+x), the left-to-right axis is +y.
69
+ * @param positions Each member's current position, in input order.
70
+ */
71
+ export declare function lineFormation(units: readonly FormationUnit[], cx: number, cy: number, dx: number, dy: number, positions?: readonly {
72
+ x: number;
73
+ y: number;
74
+ }[] | null, options?: Partial<FormationOptions>): Formation;
@@ -0,0 +1,143 @@
1
+ /** Formations: arrange a group of units in ranks facing a direction, as a line, a column, a wedge or a loose line. */
2
+ import { dmath } from '@pocketrts/determinism';
3
+ export const DEFAULT_FORMATION_OPTIONS = Object.freeze({ gap: 3, minWidth: 48, aspect: 2.6, taper: 0 });
4
+ export const FORMATION_SHAPES = ['line', 'column', 'wedge', 'loose'];
5
+ /**
6
+ * The options of a named shape, for `lineFormation`:
7
+ * - `line`: the defaults, a block about 2.6 times wider than deep;
8
+ * - `column`: a block about three times deeper than wide, for the march;
9
+ * - `wedge`: a point in front, each rank one member wider on each side than the one before;
10
+ * - `loose`: a line with a member's width of room between neighbours.
11
+ *
12
+ * Whatever its shape, a formation is ranks, so a march carries it the same way (see `createMarch`).
13
+ * @param scale The size of the world relative to the one the defaults were made for (members about 12 across): the
14
+ * options that are lengths are multiplied by it, as `marchRules` does for the march.
15
+ */
16
+ export function formationShape(shape, scale = 1) {
17
+ const o = { ...DEFAULT_FORMATION_OPTIONS };
18
+ if (shape === 'column')
19
+ o.aspect = 0.3;
20
+ else if (shape === 'wedge') {
21
+ o.aspect = 4;
22
+ o.taper = 2;
23
+ }
24
+ else if (shape === 'loose')
25
+ o.gap = 12;
26
+ o.gap *= scale;
27
+ o.minWidth *= scale;
28
+ return o;
29
+ }
30
+ /**
31
+ * Ranks centred on (cx, cy) facing (dx, dy) (any length; a zero vector faces −y). Members are sorted by rank class,
32
+ * then front to back by their current position when `positions` is given, then by input order, and poured into ranks
33
+ * no wider than the frontage (with `taper`, the ranks in front narrower still: a wedge), a new rank starting at each
34
+ * class change. Within a rank, members keep that order, or
35
+ * with `positions` are placed left to right by current lateral position, which keeps paths from crossing. Facing east
36
+ * (+x), the left-to-right axis is +y.
37
+ * @param positions Each member's current position, in input order.
38
+ */
39
+ export function lineFormation(units, cx, cy, dx, dy, positions = null, options = {}) {
40
+ const { gap, minWidth, aspect, taper } = { ...DEFAULT_FORMATION_OPTIONS, ...options };
41
+ const n = units.length;
42
+ const sx = new Float32Array(n), sy = new Float32Array(n), fa = new Float32Array(n), fl = new Float32Array(n);
43
+ const row = new Int16Array(n), col = new Int16Array(n);
44
+ const dl = dmath.hypot(dx, dy);
45
+ if (dl < 1e-4) {
46
+ dx = 0;
47
+ dy = -1;
48
+ }
49
+ else {
50
+ dx /= dl;
51
+ dy /= dl;
52
+ }
53
+ const lx = -dy, ly = dx;
54
+ const along = new Float32Array(n), lat = new Float32Array(n);
55
+ if (positions) {
56
+ for (let m = 0; m < n; m++) {
57
+ const p = positions[m];
58
+ along[m] = p.x * dx + p.y * dy;
59
+ lat[m] = p.x * lx + p.y * ly;
60
+ }
61
+ }
62
+ const idx = Array.from({ length: n }, (_, i) => i);
63
+ idx.sort((a, b) => units[a].row - units[b].row || along[b] - along[a] || a - b);
64
+ let area = 0;
65
+ for (let m = 0; m < n; m++) {
66
+ const sp = units[m].radius * 2 + gap;
67
+ area += sp * sp;
68
+ }
69
+ const width = Math.max(minWidth, Math.sqrt(area * aspect));
70
+ const rows = [];
71
+ let cur = [], curW = 0, curRow = -1;
72
+ // For a wedge: the depth of the ranks poured so far, the depth of the rank being poured, and how wide it may be
73
+ let before = 0, curD = 0, limit = width;
74
+ for (const m of idx) {
75
+ const u = units[m];
76
+ const sp = u.radius * 2 + gap;
77
+ if (taper > 0 && !cur.length)
78
+ limit = Math.min(width, taper * (before + sp / 2));
79
+ if (cur.length && (curW + sp > limit || u.row !== curRow)) {
80
+ rows.push(cur);
81
+ cur = [];
82
+ curW = 0;
83
+ if (taper > 0) {
84
+ before += curD;
85
+ curD = 0;
86
+ limit = Math.min(width, taper * (before + sp / 2));
87
+ }
88
+ }
89
+ curRow = u.row;
90
+ cur.push(m);
91
+ curW += sp;
92
+ if (sp > curD)
93
+ curD = sp;
94
+ }
95
+ if (cur.length)
96
+ rows.push(cur);
97
+ let depth = 0;
98
+ const rowDepth = [];
99
+ for (const r of rows) {
100
+ let mx = 0;
101
+ for (const m of r)
102
+ mx = Math.max(mx, units[m].radius * 2 + gap);
103
+ rowDepth.push(mx);
104
+ depth += mx;
105
+ }
106
+ const rowW = [], rowN = [], rowFa = [], rowRad = [], rowRun = [];
107
+ let dAcc = 0;
108
+ for (let ri = 0; ri < rows.length; ri++) {
109
+ const r = rows[ri];
110
+ if (positions)
111
+ r.sort((a, b) => lat[a] - lat[b]);
112
+ let w = 0, rad = 0, run = Infinity;
113
+ for (const m of r) {
114
+ const u = units[m];
115
+ w += u.radius * 2 + gap;
116
+ if (u.radius > rad)
117
+ rad = u.radius;
118
+ if (u.speed < run)
119
+ run = u.speed;
120
+ }
121
+ let xo = -w / 2;
122
+ const fwd = depth / 2 - dAcc - rowDepth[ri] / 2;
123
+ for (let k = 0; k < r.length; k++) {
124
+ const m = r[k];
125
+ const sp = units[m].radius * 2 + gap;
126
+ const l = xo + sp / 2;
127
+ fa[m] = fwd;
128
+ fl[m] = l;
129
+ row[m] = ri;
130
+ col[m] = k;
131
+ sx[m] = cx + lx * l + dx * fwd;
132
+ sy[m] = cy + ly * l + dy * fwd;
133
+ xo += sp;
134
+ }
135
+ rowW.push(w);
136
+ rowN.push(r.length);
137
+ rowFa.push(fwd);
138
+ rowRad.push(rad);
139
+ rowRun.push(run);
140
+ dAcc += rowDepth[ri];
141
+ }
142
+ return { sx, sy, fa, fl, row, col, rowW, rowD: rowDepth, rowN, rowFa, rowRad, rowRun, radius: dmath.hypot(width, depth) / 2, width, depth };
143
+ }
@@ -0,0 +1,14 @@
1
+ export * from './dirs.js';
2
+ export * from './flowfield.js';
3
+ export * from './localpath.js';
4
+ export * from './cells.js';
5
+ export * from './buckets.js';
6
+ export * from './vision.js';
7
+ export * from './polygon.js';
8
+ export * from './chain.js';
9
+ export * from './formation.js';
10
+ export * from './marchTypes.js';
11
+ export * from './march.js';
12
+ export * from './marchGround.js';
13
+ export { TP as MARCH_POINT, trailAt as marchTrailAt } from './marchTrail.js';
14
+ export { FOLLOW as MARCH_FOLLOW, FOLLOW_APART, FOLLOW_SLOT, FOLLOW_SQUEEZE, SLOT as MARCH_SLOT, marchFollow, marchRelease, marchSlot, } from './marchSlots.js';
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ export * from './dirs.js';
2
+ export * from './flowfield.js';
3
+ export * from './localpath.js';
4
+ export * from './cells.js';
5
+ export * from './buckets.js';
6
+ export * from './vision.js';
7
+ export * from './polygon.js';
8
+ export * from './chain.js';
9
+ export * from './formation.js';
10
+ export * from './marchTypes.js';
11
+ export * from './march.js';
12
+ export * from './marchGround.js';
13
+ // For games that draw the column or steer members their own way: a point of a march's trail, a member's place, and
14
+ // the point a member heads for. Everything else in the march files works on a march from the inside
15
+ export { TP as MARCH_POINT, trailAt as marchTrailAt } from './marchTrail.js';
16
+ export { FOLLOW as MARCH_FOLLOW, FOLLOW_APART, FOLLOW_SLOT, FOLLOW_SQUEEZE, SLOT as MARCH_SLOT, marchFollow, marchRelease, marchSlot, } from './marchSlots.js';
@@ -0,0 +1,35 @@
1
+ /** Step rules for `LocalPathfinder.find`. */
2
+ export interface LocalStepRules {
3
+ /** Non-zero = the cell cannot be entered. */
4
+ readonly block: ArrayLike<number>;
5
+ /** Whether a unit may step straight from `a` to the adjacent passable cell `b`. */
6
+ canStep(a: number, b: number): boolean;
7
+ /** Whether a unit may step diagonally from `a` to `b`. */
8
+ canStepDiag(a: number, b: number): boolean;
9
+ }
10
+ export declare class LocalPathfinder {
11
+ readonly maxNodes: number;
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
+ readonly path: Int32Array;
17
+ /** Cells in `path`; 0 after a failed search. */
18
+ length: number;
19
+ private readonly heapF;
20
+ private readonly heapC;
21
+ private cells;
22
+ private gen;
23
+ private stamp;
24
+ private gScore;
25
+ private parent;
26
+ private closed;
27
+ /** @param maxNodes Most cells a search expands before giving up (a safeguard beyond the length limit). Default 1200. */
28
+ constructor(maxNodes?: number);
29
+ /**
30
+ * Find a path from cell `start` to cell `goal` on a `w` × `h` grid whose length (straight steps 1, diagonal √2)
31
+ * stays within `limit` cells, using the octile distance as the estimate. Returns whether one was found; the path is
32
+ * then in `path` / `length`. A blocked goal is never reached.
33
+ */
34
+ find(w: number, h: number, start: number, goal: number, limit: number, rules: LocalStepRules): boolean;
35
+ }