@margoengine/tilemap 0.1.0 → 1.0.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 +201 -201
- package/NOTICE +4 -4
- package/README.md +388 -294
- package/dist/grid-search-B0VbYKpe.js +98 -0
- package/dist/grid-search.d.ts +33 -0
- package/dist/hpa.d.ts +87 -0
- package/dist/hpa.js +239 -0
- package/dist/iso-C2Hwh5Gr.js +4 -4
- package/dist/objects-BK2mewd1.js +4 -4
- package/dist/path.d.ts +15 -1
- package/dist/path.js +90 -118
- package/dist/plane.js +3 -3
- package/dist/replan.d.ts +58 -0
- package/dist/replan.js +114 -0
- package/dist/src-CJvCfCkX.js +4 -4
- package/package.json +98 -90
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { r as e } from "./layout-CLx3KdGC.js";
|
|
2
|
+
//#region src/grid-search.ts
|
|
3
|
+
var t = Math.SQRT2, n = (e) => e > 0 && e < Infinity, r = class {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.cells = [], this.keys = [], this.order = [], this.pushed = 0;
|
|
6
|
+
}
|
|
7
|
+
get size() {
|
|
8
|
+
return this.cells.length;
|
|
9
|
+
}
|
|
10
|
+
less(e, t) {
|
|
11
|
+
return this.keys[e] < this.keys[t] || this.keys[e] === this.keys[t] && this.order[e] < this.order[t];
|
|
12
|
+
}
|
|
13
|
+
swap(e, t) {
|
|
14
|
+
for (let n of [
|
|
15
|
+
this.cells,
|
|
16
|
+
this.keys,
|
|
17
|
+
this.order
|
|
18
|
+
]) [n[e], n[t]] = [n[t], n[e]];
|
|
19
|
+
}
|
|
20
|
+
push(e, t) {
|
|
21
|
+
let n = this.cells.length;
|
|
22
|
+
for (this.cells.push(e), this.keys.push(t), this.order.push(this.pushed++); n > 0;) {
|
|
23
|
+
let e = n - 1 >> 1;
|
|
24
|
+
if (!this.less(n, e)) break;
|
|
25
|
+
this.swap(n, e), n = e;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
pop() {
|
|
29
|
+
let e = this.cells[0], t = this.cells.length - 1;
|
|
30
|
+
this.swap(0, t), this.cells.pop(), this.keys.pop(), this.order.pop();
|
|
31
|
+
for (let e = 0;;) {
|
|
32
|
+
let t = e * 2 + 1, n = t + 1, r = e;
|
|
33
|
+
if (t < this.cells.length && this.less(t, r) && (r = t), n < this.cells.length && this.less(n, r) && (r = n), r === e) break;
|
|
34
|
+
this.swap(e, r), e = r;
|
|
35
|
+
}
|
|
36
|
+
return e;
|
|
37
|
+
}
|
|
38
|
+
}, i = (e, t) => {
|
|
39
|
+
if (!e || !Number.isInteger(e.width) || !Number.isInteger(e.height) || e.cost?.length !== e.width * e.height) throw RangeError("a CostGrid needs whole width and height and width * height costs.");
|
|
40
|
+
let n = t.diagonal ?? "no-corners";
|
|
41
|
+
if (n !== "never" && n !== "always" && n !== "no-corners") throw RangeError(`diagonal must be never, always or no-corners, got ${n}.`);
|
|
42
|
+
let r = t.maxCells ?? 1e6;
|
|
43
|
+
if (!(r >= 1)) throw RangeError(`maxCells must be 1 or more, got ${r}.`);
|
|
44
|
+
let i = e.layout?.orientation ?? "orthogonal";
|
|
45
|
+
return {
|
|
46
|
+
diagonal: n,
|
|
47
|
+
max: r,
|
|
48
|
+
square: i === "orthogonal" || i === "isometric"
|
|
49
|
+
};
|
|
50
|
+
}, a = (r, i, a, o, s) => {
|
|
51
|
+
let { width: c, height: l, cost: u } = r, d = o % c, f = (o - d) / c, p = (e, t) => e >= 0 && t >= 0 && e < c && t < l;
|
|
52
|
+
if (i) {
|
|
53
|
+
for (let [e, t] of [
|
|
54
|
+
[0, -1],
|
|
55
|
+
[1, 0],
|
|
56
|
+
[0, 1],
|
|
57
|
+
[-1, 0]
|
|
58
|
+
]) p(d + e, f + t) && n(u[(f + t) * c + d + e]) && s((f + t) * c + d + e, 1);
|
|
59
|
+
if (a !== "never") for (let [e, r] of [
|
|
60
|
+
[1, -1],
|
|
61
|
+
[1, 1],
|
|
62
|
+
[-1, 1],
|
|
63
|
+
[-1, -1]
|
|
64
|
+
]) p(d + e, f + r) && n(u[(f + r) * c + d + e]) && (a !== "no-corners" || n(u[f * c + d + e]) && n(u[(f + r) * c + d])) && s((f + r) * c + d + e, t);
|
|
65
|
+
} else for (let [t, i] of e(r.layout, d, f)) p(t, i) && n(u[i * c + t]) && s(i * c + t, 1);
|
|
66
|
+
}, o = (e, [t, n], r) => {
|
|
67
|
+
if (!Number.isInteger(t) || !Number.isInteger(n) || t < 0 || n < 0 || t >= e.width || n >= e.height) throw RangeError(`${r} (${t}, ${n}) is outside the ${e.width} x ${e.height} grid; pass a cell inside it.`);
|
|
68
|
+
return n * e.width + t;
|
|
69
|
+
}, s = (e, n, r, i) => {
|
|
70
|
+
let { width: a } = e;
|
|
71
|
+
return (o, s) => {
|
|
72
|
+
let l = o % a, u = (o - l) / a, d = s % a, f = (s - d) / a;
|
|
73
|
+
if (!n) {
|
|
74
|
+
let t = c(e.layout, l, u), n = c(e.layout, d, f);
|
|
75
|
+
return i * Math.max(Math.abs(t[0] - n[0]), Math.abs(t[1] - n[1]), Math.abs(t[2] - n[2]));
|
|
76
|
+
}
|
|
77
|
+
let p = Math.abs(l - d), m = Math.abs(u - f);
|
|
78
|
+
return i * (r === "never" ? p + m : Math.max(p, m) + (t - 1) * Math.min(p, m));
|
|
79
|
+
};
|
|
80
|
+
}, c = (e, t, n) => {
|
|
81
|
+
let r = (e.staggerIndex ?? "odd") === "odd";
|
|
82
|
+
if ((e.staggerAxis ?? "y") === "y") {
|
|
83
|
+
let e = t - (r ? (n - (n & 1)) / 2 : (n + (n & 1)) / 2);
|
|
84
|
+
return [
|
|
85
|
+
e,
|
|
86
|
+
n,
|
|
87
|
+
-e - n
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
let i = n - (r ? (t - (t & 1)) / 2 : (t + (t & 1)) / 2);
|
|
91
|
+
return [
|
|
92
|
+
t,
|
|
93
|
+
i,
|
|
94
|
+
-t - i
|
|
95
|
+
];
|
|
96
|
+
};
|
|
97
|
+
//#endregion
|
|
98
|
+
export { o as a, s as i, t as n, n as o, i as r, a as s, r as t };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type MapGrid } from './layout.js';
|
|
2
|
+
import type { CostGrid, PathOptions } from './path.js';
|
|
3
|
+
/** The corner-step rule of {@link PathOptions}. */
|
|
4
|
+
export type Diagonal = NonNullable<PathOptions['diagonal']>;
|
|
5
|
+
export declare const SQRT2: number;
|
|
6
|
+
export declare const open: (c: number) => boolean;
|
|
7
|
+
/** A binary min-heap of cell indices by priority; ties pop the cell pushed first. */
|
|
8
|
+
export declare class Heap {
|
|
9
|
+
private cells;
|
|
10
|
+
private keys;
|
|
11
|
+
private order;
|
|
12
|
+
private pushed;
|
|
13
|
+
get size(): number;
|
|
14
|
+
private less;
|
|
15
|
+
private swap;
|
|
16
|
+
push(cell: number, key: number): void;
|
|
17
|
+
pop(): number;
|
|
18
|
+
}
|
|
19
|
+
export declare const check: (grid: CostGrid, options: PathOptions) => {
|
|
20
|
+
diagonal: "always" | "never" | "no-corners";
|
|
21
|
+
max: number;
|
|
22
|
+
square: boolean;
|
|
23
|
+
};
|
|
24
|
+
/** Calls `visit(next, length)` for each open step from `cell`; steps are symmetric, so the same edges serve both ways. */
|
|
25
|
+
export declare const steps: (grid: CostGrid, square: boolean, diagonal: Diagonal, cell: number, visit: (next: number, length: number) => void) => void;
|
|
26
|
+
export declare const index: (grid: CostGrid, [x, y]: readonly [number, number], what: string) => number;
|
|
27
|
+
/**
|
|
28
|
+
* An estimate of the cost between two cells that never overestimates, for a grid whose open cells cost `low` or more:
|
|
29
|
+
* the octile distance (the hex distance on staggered and hexagonal maps, the row and column distance without corner
|
|
30
|
+
* steps) times `low`. It also obeys the triangle inequality.
|
|
31
|
+
*/
|
|
32
|
+
export declare const estimator: (grid: CostGrid, square: boolean, diagonal: Diagonal, low: number) => (a: number, b: number) => number;
|
|
33
|
+
export declare const cube: (map: MapGrid, x: number, y: number) => [number, number, number];
|
package/dist/hpa.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { CostGrid, GridPath, PathOptions } from './path.js';
|
|
2
|
+
/** Settings of {@link PathHierarchy}. */
|
|
3
|
+
export interface HierarchyOptions {
|
|
4
|
+
/** Corner steps, as `findPath` takes them. Default `'no-corners'`. */
|
|
5
|
+
diagonal?: PathOptions['diagonal'];
|
|
6
|
+
/** Stops one `findPath` call after this many expanded cells and nodes. Default 1,000,000. */
|
|
7
|
+
maxCells?: number;
|
|
8
|
+
/** Cells along a cluster's side, a whole number of 2 or more. Default 16. */
|
|
9
|
+
cluster?: number;
|
|
10
|
+
/**
|
|
11
|
+
* How many steps any border crossing may lie from a transition, a whole number of 0 or more. 0 makes every crossing a
|
|
12
|
+
* transition, for exact costs and the largest graph; higher values make a smaller graph. Default 3.
|
|
13
|
+
*/
|
|
14
|
+
detour?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Long paths over a large grid for the price of short ones: build once, then {@link PathHierarchy.findPath} searches a
|
|
18
|
+
* graph of cluster borders and fills in only the clusters the path crosses. Change `grid.cost`, tell
|
|
19
|
+
* {@link PathHierarchy.update} which cells changed, and only the clusters around them are rebuilt.
|
|
20
|
+
*/
|
|
21
|
+
export declare class PathHierarchy {
|
|
22
|
+
/** The grid searched; change its costs in place, then call {@link update}. */
|
|
23
|
+
readonly grid: CostGrid;
|
|
24
|
+
readonly cluster: number;
|
|
25
|
+
readonly detour: number;
|
|
26
|
+
private readonly diagonal;
|
|
27
|
+
private readonly square;
|
|
28
|
+
private readonly max;
|
|
29
|
+
private readonly across;
|
|
30
|
+
private readonly down;
|
|
31
|
+
private low;
|
|
32
|
+
private readonly clusters;
|
|
33
|
+
/** The transitions between two clusters, under `first * count + second`. */
|
|
34
|
+
private readonly borders;
|
|
35
|
+
/** Each node's transitions, both ways. */
|
|
36
|
+
private readonly links;
|
|
37
|
+
private readonly dirty;
|
|
38
|
+
private readonly dist;
|
|
39
|
+
private readonly came;
|
|
40
|
+
private readonly seen;
|
|
41
|
+
private readonly done;
|
|
42
|
+
private stamp;
|
|
43
|
+
private expanded;
|
|
44
|
+
constructor(grid: CostGrid, options?: HierarchyOptions);
|
|
45
|
+
/** The nodes of the abstract graph, as cells, for drawing and for judging the size `cluster` and `detour` give. */
|
|
46
|
+
get nodes(): [number, number][];
|
|
47
|
+
/**
|
|
48
|
+
* Tells the hierarchy which cells' costs changed since the last call; the clusters around them are rebuilt before the
|
|
49
|
+
* next query. Every changed cell must be listed.
|
|
50
|
+
*/
|
|
51
|
+
update(cells: Iterable<readonly [number, number]>): this;
|
|
52
|
+
/**
|
|
53
|
+
* A path from `start` to `goal`, or null when there is no way (or `maxCells` ran out). It finds a way whenever one
|
|
54
|
+
* exists. Its cost is the cheapest one with a `detour` of 0, and otherwise above it by at most
|
|
55
|
+
* `(2 * detour + 1) * L * highest` for each cluster border a cheapest path crosses, where `highest` is the dearest open
|
|
56
|
+
* cell and `L` is sqrt(2) with corner steps and 1 without them (and on hexagonal and staggered maps). `smooth`
|
|
57
|
+
* (default true) searches the path again in short stretches and keeps what is cheaper.
|
|
58
|
+
*/
|
|
59
|
+
findPath(start: readonly [number, number], goal: readonly [number, number], { smooth }?: {
|
|
60
|
+
smooth?: boolean;
|
|
61
|
+
}): GridPath | null;
|
|
62
|
+
private clusterOf;
|
|
63
|
+
private inside;
|
|
64
|
+
/** The cost of the step from `a` to its neighbour `b`. */
|
|
65
|
+
private price;
|
|
66
|
+
/**
|
|
67
|
+
* Searches from `from` over the cells `allowed` accepts: to `target` by A*, or everywhere (-1). With `reverse` each
|
|
68
|
+
* distance is the cost of walking from the cell to `from`. True when the target was reached (or there is none).
|
|
69
|
+
*/
|
|
70
|
+
private explore;
|
|
71
|
+
/** The distances the last search found to those of `cells` it reached. */
|
|
72
|
+
private reached;
|
|
73
|
+
/** The last search's way to `cell`, from its start. */
|
|
74
|
+
private trace;
|
|
75
|
+
/**
|
|
76
|
+
* Searches stretches of the path again, each a cluster side long and overlapping the next by half, inside the stretch's
|
|
77
|
+
* bounds widened by half a cluster, and takes the new way where it is cheaper.
|
|
78
|
+
*/
|
|
79
|
+
private shorten;
|
|
80
|
+
/** Chooses the transitions of the borders of every dirty cluster again, then the ways inside the clusters they end in. */
|
|
81
|
+
private rebuild;
|
|
82
|
+
/**
|
|
83
|
+
* The transitions from cluster `a` to cluster `b`: enough of the steps between them that every such step is within
|
|
84
|
+
* `detour` hops of one. A hop moves each end by at most one step inside its own cluster.
|
|
85
|
+
*/
|
|
86
|
+
private transitions;
|
|
87
|
+
}
|
package/dist/hpa.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { a as e, i as t, n, o as r, r as i, s as a, t as o } from "./grid-search-B0VbYKpe.js";
|
|
2
|
+
//#region src/hpa.ts
|
|
3
|
+
var s = class {
|
|
4
|
+
constructor(e, t = {}) {
|
|
5
|
+
this.low = Infinity, this.borders = /* @__PURE__ */ new Map(), this.links = /* @__PURE__ */ new Map(), this.dirty = /* @__PURE__ */ new Set(), this.stamp = 0, this.expanded = 0;
|
|
6
|
+
let { diagonal: n, max: a, square: o } = i(e, t), s = t.cluster ?? 16, c = t.detour ?? 3;
|
|
7
|
+
if (!Number.isInteger(s) || s < 2) throw RangeError(`cluster must be a whole number of 2 or more, got ${s}.`);
|
|
8
|
+
if (!Number.isInteger(c) || c < 0) throw RangeError(`detour must be a whole number of 0 or more, got ${c}.`);
|
|
9
|
+
this.grid = e, this.diagonal = n, this.square = o, this.max = a, this.cluster = s, this.detour = c, this.across = Math.ceil(e.width / s), this.down = Math.ceil(e.height / s), this.clusters = Array.from({ length: this.across * this.down }, () => ({
|
|
10
|
+
nodes: [],
|
|
11
|
+
edges: /* @__PURE__ */ new Map()
|
|
12
|
+
}));
|
|
13
|
+
let l = e.cost.length;
|
|
14
|
+
this.dist = new Float64Array(l), this.came = new Int32Array(l), this.seen = new Uint32Array(l), this.done = new Uint32Array(l);
|
|
15
|
+
for (let t = 0; t < l; t++) r(e.cost[t]) && e.cost[t] < this.low && (this.low = e.cost[t]);
|
|
16
|
+
for (let e = 0; e < this.clusters.length; e++) this.dirty.add(e);
|
|
17
|
+
this.rebuild();
|
|
18
|
+
}
|
|
19
|
+
get nodes() {
|
|
20
|
+
this.rebuild();
|
|
21
|
+
let { width: e } = this.grid;
|
|
22
|
+
return this.clusters.flatMap((t) => t.nodes.map((t) => [t % e, Math.floor(t / e)]));
|
|
23
|
+
}
|
|
24
|
+
update(t) {
|
|
25
|
+
let { grid: n } = this;
|
|
26
|
+
for (let i of t) {
|
|
27
|
+
let t = e(n, i, "cell"), [a, o] = i;
|
|
28
|
+
r(n.cost[t]) && n.cost[t] < this.low && (this.low = n.cost[t]);
|
|
29
|
+
for (let e = -1; e <= 1; e++) for (let r = -1; r <= 1; r++) a + r >= 0 && o + e >= 0 && a + r < n.width && o + e < n.height && this.dirty.add(this.clusterOf(t + e * n.width + r));
|
|
30
|
+
}
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
findPath(n, i, { smooth: a = !0 } = {}) {
|
|
34
|
+
let { grid: s } = this, { cost: l, width: u } = s, d = e(s, n, "start"), f = e(s, i, "goal");
|
|
35
|
+
if (!r(l[d]) || !r(l[f])) return null;
|
|
36
|
+
this.rebuild(), this.expanded = 0;
|
|
37
|
+
let p = this.clusterOf(d), m = this.clusterOf(f), h = t(s, this.square, this.diagonal, this.low);
|
|
38
|
+
this.explore(d, this.inside(p), -1, !1);
|
|
39
|
+
let g = this.reached([...this.clusters[p].nodes, ...p === m ? [f] : []]);
|
|
40
|
+
this.explore(f, this.inside(m), -1, !0);
|
|
41
|
+
let _ = this.reached(this.clusters[m].nodes), v = /* @__PURE__ */ new Map([[d, 0]]), y = /* @__PURE__ */ new Map(), b = /* @__PURE__ */ new Set(), x = new o();
|
|
42
|
+
for (x.push(d, h(d, f)); x.size;) {
|
|
43
|
+
let e = x.pop();
|
|
44
|
+
if (b.has(e)) continue;
|
|
45
|
+
if (b.add(e), e === f) break;
|
|
46
|
+
if (++this.expanded > this.max) return null;
|
|
47
|
+
let t = v.get(e), n = (n, r) => {
|
|
48
|
+
let i = t + r;
|
|
49
|
+
i < (v.get(n) ?? Infinity) && (v.set(n, i), y.set(n, e), x.push(n, i + h(n, f)));
|
|
50
|
+
};
|
|
51
|
+
if (e === d) for (let [e, t] of g) n(e, t);
|
|
52
|
+
let r = this.clusterOf(e), i = this.clusters[r].edges.get(e);
|
|
53
|
+
if (i) for (let e = 0; e < i.to.length; e++) n(i.to[e], i.cost[e]);
|
|
54
|
+
for (let t of this.links.get(e) ?? []) {
|
|
55
|
+
let r = t.a === e ? t.b : t.a;
|
|
56
|
+
n(r, l[r] * t.length);
|
|
57
|
+
}
|
|
58
|
+
let a = r === m ? _.get(e) : void 0;
|
|
59
|
+
a !== void 0 && n(f, a);
|
|
60
|
+
}
|
|
61
|
+
if (!b.has(f)) return null;
|
|
62
|
+
let S = [f];
|
|
63
|
+
for (; S[S.length - 1] !== d;) S.push(y.get(S[S.length - 1]));
|
|
64
|
+
S.reverse();
|
|
65
|
+
let C = [d];
|
|
66
|
+
for (let e = 1; e < S.length; e++) {
|
|
67
|
+
let t = S[e - 1], n = S[e], r = this.clusterOf(t);
|
|
68
|
+
if (this.clusterOf(n) !== r) C.push(n);
|
|
69
|
+
else {
|
|
70
|
+
if (!this.explore(t, this.inside(r), n, !1)) return null;
|
|
71
|
+
let e = this.trace(n);
|
|
72
|
+
for (let t = 1; t < e.length; t++) C.push(e[t]);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (C = c(C), a && (C = c(this.shorten(C))), this.expanded > this.max) return null;
|
|
76
|
+
let w = 0;
|
|
77
|
+
for (let e = 1; e < C.length; e++) w += this.price(C[e - 1], C[e]);
|
|
78
|
+
return {
|
|
79
|
+
cells: C.map((e) => [e % u, Math.floor(e / u)]),
|
|
80
|
+
cost: w,
|
|
81
|
+
searched: this.expanded,
|
|
82
|
+
scanned: 0
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
clusterOf(e) {
|
|
86
|
+
let t = e % this.grid.width, n = (e - t) / this.grid.width;
|
|
87
|
+
return Math.floor(n / this.cluster) * this.across + Math.floor(t / this.cluster);
|
|
88
|
+
}
|
|
89
|
+
inside(e) {
|
|
90
|
+
return (t) => this.clusterOf(t) === e;
|
|
91
|
+
}
|
|
92
|
+
price(e, t) {
|
|
93
|
+
let { width: r, cost: i } = this.grid, a = this.square && e % r !== t % r && Math.floor(e / r) !== Math.floor(t / r);
|
|
94
|
+
return i[t] * (a ? n : 1);
|
|
95
|
+
}
|
|
96
|
+
explore(e, n, r, i) {
|
|
97
|
+
let { grid: s, dist: c, came: l, seen: u, done: d } = this, { cost: f } = s, p = ++this.stamp, m = r < 0 ? void 0 : t(s, this.square, this.diagonal, this.low), h = new o();
|
|
98
|
+
for (u[e] = p, c[e] = 0, l[e] = -1, h.push(e, 0); h.size;) {
|
|
99
|
+
let e = h.pop();
|
|
100
|
+
if (d[e] !== p) {
|
|
101
|
+
if (d[e] = p, e === r) return !0;
|
|
102
|
+
this.expanded++, a(s, this.square, this.diagonal, e, (t, a) => {
|
|
103
|
+
if (d[t] === p || !n(t)) return;
|
|
104
|
+
let o = c[e] + f[i ? e : t] * a;
|
|
105
|
+
(u[t] !== p || o < c[t]) && (u[t] = p, c[t] = o, l[t] = e, h.push(t, m ? o + m(t, r) : o));
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return r < 0;
|
|
110
|
+
}
|
|
111
|
+
reached(e) {
|
|
112
|
+
let t = /* @__PURE__ */ new Map();
|
|
113
|
+
for (let n of e) this.done[n] === this.stamp && t.set(n, this.dist[n]);
|
|
114
|
+
return t;
|
|
115
|
+
}
|
|
116
|
+
trace(e) {
|
|
117
|
+
let t = [];
|
|
118
|
+
for (let n = e; n !== -1; n = this.came[n]) t.push(n);
|
|
119
|
+
return t.reverse();
|
|
120
|
+
}
|
|
121
|
+
shorten(e) {
|
|
122
|
+
let { width: t } = this.grid, n = this.cluster, r = n >> 1 || 1;
|
|
123
|
+
for (let i = 0; i + 2 < e.length; i += r) {
|
|
124
|
+
let a = Math.min(e.length - 1, i + n), o = t, s = this.grid.height, c = 0, l = 0, u = 0;
|
|
125
|
+
for (let n = i; n <= a; n++) {
|
|
126
|
+
let r = e[n] % t, a = (e[n] - r) / t;
|
|
127
|
+
o = Math.min(o, r), c = Math.max(c, r), s = Math.min(s, a), l = Math.max(l, a), n > i && (u += this.price(e[n - 1], e[n]));
|
|
128
|
+
}
|
|
129
|
+
let d = (e) => {
|
|
130
|
+
let n = e % t, i = (e - n) / t;
|
|
131
|
+
return n >= o - r && n <= c + r && i >= s - r && i <= l + r;
|
|
132
|
+
};
|
|
133
|
+
if (this.expanded > this.max) break;
|
|
134
|
+
if (this.explore(e[i], d, e[a], !1) && this.dist[e[a]] < u - u * 1e-9) {
|
|
135
|
+
let t = this.trace(e[a]);
|
|
136
|
+
e = e.slice(0, i).concat(t, e.slice(a + 1));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return e;
|
|
140
|
+
}
|
|
141
|
+
rebuild() {
|
|
142
|
+
if (!this.dirty.size) return;
|
|
143
|
+
let { across: e, down: t, clusters: n, borders: r, links: i } = this, a = /* @__PURE__ */ new Set(), o = new Set(this.dirty);
|
|
144
|
+
for (let r of this.dirty) {
|
|
145
|
+
let i = r % e, o = (r - i) / e;
|
|
146
|
+
for (let s = -1; s <= 1; s++) for (let c = -1; c <= 1; c++) {
|
|
147
|
+
if (!c && !s || i + c < 0 || o + s < 0 || i + c >= e || o + s >= t) continue;
|
|
148
|
+
let l = r + s * e + c;
|
|
149
|
+
a.add(Math.min(r, l) * n.length + Math.max(r, l));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
for (let e of a) {
|
|
153
|
+
let t = e % n.length, a = (e - t) / n.length, s = r.get(e) ?? [], c = this.transitions(a, t);
|
|
154
|
+
if (!(s.length === c.length && s.every((e, t) => e.a === c[t].a && e.b === c[t].b))) {
|
|
155
|
+
for (let e of s) for (let t of [e.a, e.b]) {
|
|
156
|
+
let n = i.get(t).filter((t) => t !== e);
|
|
157
|
+
n.length ? i.set(t, n) : i.delete(t);
|
|
158
|
+
}
|
|
159
|
+
for (let e of c) for (let t of [e.a, e.b]) i.set(t, [...i.get(t) ?? [], e]);
|
|
160
|
+
r.set(e, c), o.add(a), o.add(t);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
for (let i of o) {
|
|
164
|
+
let a = i % e, o = (i - a) / e, s = /* @__PURE__ */ new Set();
|
|
165
|
+
for (let c = -1; c <= 1; c++) for (let l = -1; l <= 1; l++) {
|
|
166
|
+
if (!l && !c || a + l < 0 || o + c < 0 || a + l >= e || o + c >= t) continue;
|
|
167
|
+
let u = i + c * e + l;
|
|
168
|
+
for (let e of r.get(Math.min(i, u) * n.length + Math.max(i, u)) ?? []) s.add(i < u ? e.a : e.b);
|
|
169
|
+
}
|
|
170
|
+
let c = n[i];
|
|
171
|
+
c.nodes = [...s].sort((e, t) => e - t), c.edges = /* @__PURE__ */ new Map();
|
|
172
|
+
for (let e of c.nodes) {
|
|
173
|
+
this.explore(e, this.inside(i), -1, !1);
|
|
174
|
+
let t = this.reached(c.nodes);
|
|
175
|
+
t.delete(e), c.edges.set(e, {
|
|
176
|
+
to: [...t.keys()],
|
|
177
|
+
cost: [...t.values()]
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
this.dirty.clear();
|
|
182
|
+
}
|
|
183
|
+
transitions(e, t) {
|
|
184
|
+
let { grid: n, across: i, cluster: o, detour: s } = this, { width: c, height: l, cost: u } = n, d = e % i * o, f = Math.floor(e / i) * o, p = Math.min(c, d + o) - 1, m = Math.min(l, f + o) - 1, h = [], g = /* @__PURE__ */ new Map();
|
|
185
|
+
for (let e = f; e <= m; e++) for (let i = d; i <= p; i += e === f || e === m || i === p ? 1 : p - d) {
|
|
186
|
+
let o = e * c + i;
|
|
187
|
+
r(u[o]) && a(n, this.square, this.diagonal, o, (e, n) => {
|
|
188
|
+
if (this.clusterOf(e) !== t) return;
|
|
189
|
+
let r = g.get(o);
|
|
190
|
+
r ? r.push(h.length) : g.set(o, [h.length]), h.push({
|
|
191
|
+
a: o,
|
|
192
|
+
b: e,
|
|
193
|
+
length: n
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
if (!s || h.length < 2) return h;
|
|
198
|
+
let _ = (e, t) => {
|
|
199
|
+
let r = /* @__PURE__ */ new Set([e]);
|
|
200
|
+
return a(n, this.square, this.diagonal, e, (e) => {
|
|
201
|
+
this.clusterOf(e) === t && r.add(e);
|
|
202
|
+
}), r;
|
|
203
|
+
}, v = [], y = (n) => {
|
|
204
|
+
let r = v[n];
|
|
205
|
+
if (r) return r;
|
|
206
|
+
r = [];
|
|
207
|
+
let i = _(h[n].b, t);
|
|
208
|
+
for (let t of _(h[n].a, e)) for (let e of g.get(t) ?? []) e !== n && i.has(h[e].b) && r.push(e);
|
|
209
|
+
return v[n] = r, r;
|
|
210
|
+
}, b = (e) => {
|
|
211
|
+
let t = /* @__PURE__ */ new Map([[e, 0]]);
|
|
212
|
+
for (let [e, n] of t) if (n < s) for (let r of y(e)) t.has(r) || t.set(r, n + 1);
|
|
213
|
+
return t;
|
|
214
|
+
}, x = new Uint8Array(h.length), S = [];
|
|
215
|
+
for (let e = 0; e < h.length; e++) {
|
|
216
|
+
if (x[e]) continue;
|
|
217
|
+
let t, n = e, r = -1, i = 0;
|
|
218
|
+
for (let a of b(e).keys()) {
|
|
219
|
+
let e = b(a), o = 0, s = 0;
|
|
220
|
+
for (let [t, n] of e) x[t] || (o++, s += n);
|
|
221
|
+
let c = h[a].length - h[n].length;
|
|
222
|
+
(o > r || o === r && (c < 0 || c === 0 && s < i)) && (t = e, n = a, r = o, i = s);
|
|
223
|
+
}
|
|
224
|
+
for (let e of t.keys()) x[e] = 1;
|
|
225
|
+
S.push(h[n]);
|
|
226
|
+
}
|
|
227
|
+
return S;
|
|
228
|
+
}
|
|
229
|
+
}, c = (e) => {
|
|
230
|
+
let t = /* @__PURE__ */ new Map(), n = [];
|
|
231
|
+
for (let r of e) {
|
|
232
|
+
let e = t.get(r);
|
|
233
|
+
if (e === void 0) t.set(r, n.length), n.push(r);
|
|
234
|
+
else for (let r of n.splice(e + 1)) t.delete(r);
|
|
235
|
+
}
|
|
236
|
+
return n;
|
|
237
|
+
};
|
|
238
|
+
//#endregion
|
|
239
|
+
export { s as PathHierarchy };
|
package/dist/iso-C2Hwh5Gr.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
-
const m=(engine,version="
|
|
3
|
+
const m=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
4
|
import { A as e, C as t, D as n, E as r, O as i, S as a, T as o, a as s, h as c, k as l, v as u, w as d, x as f } from "./src-CJvCfCkX.js";
|
|
5
5
|
import { t as p } from "./layout-CLx3KdGC.js";
|
|
6
6
|
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
@@ -60,7 +60,7 @@ function b(e, t, n, r = []) {
|
|
|
60
60
|
for (let e = x; e <= S; e++) for (let t of [!1, !0]) for (let n = C; n <= w; n++) (n & 1) == 1 === T === t && r.push(e * i + n);
|
|
61
61
|
return r;
|
|
62
62
|
}
|
|
63
|
-
var x = class h {coreRequirement=__margoCoreRequirement("
|
|
63
|
+
var x = class h {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
64
64
|
constructor(n) {
|
|
65
65
|
if (this.time = 0, this.stats = {
|
|
66
66
|
draws: 0,
|
|
@@ -84,7 +84,7 @@ var x = class h {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
|
|
|
84
84
|
zoom: 1
|
|
85
85
|
}, this.snap = n.snap ?? !0, this.parallax = n.parallax ?? !0, this.setTarget(n.target), this.clear = n.clear, this.order = r, this.layerMask = t(n.layers ?? 1, "GridTileMap layers"), this.name = n.name ?? `grid-tilemap-${++y}`, this.setMap(n.map);
|
|
86
86
|
}
|
|
87
|
-
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("
|
|
87
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
88
88
|
let n = new h(t);
|
|
89
89
|
return m(e).use(n), n;
|
|
90
90
|
}
|
package/dist/objects-BK2mewd1.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
-
const _=(engine,version="
|
|
3
|
+
const _=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
4
|
import { C as e, D as t, E as n, O as r, S as i, T as a, a as o, d as s, f as c, h as l, k as u, v as d, w as f, x as p } from "./src-CJvCfCkX.js";
|
|
5
5
|
import { i as m, r as h } from "./iso-C2Hwh5Gr.js";
|
|
6
6
|
import { t as g } from "./layout-CLx3KdGC.js";
|
|
@@ -156,7 +156,7 @@ ${m}
|
|
|
156
156
|
let color=textureLoad(atlas,vec2i(clamp(floor(v.texel),v.low,v.high)),0)*v.tint;
|
|
157
157
|
if(color.a==0){discard;}
|
|
158
158
|
return color;
|
|
159
|
-
}`, x = 0, S = class o {coreRequirement=__margoCoreRequirement("
|
|
159
|
+
}`, x = 0, S = class o {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
160
160
|
constructor(t) {
|
|
161
161
|
if (this.time = 0, this.stats = {
|
|
162
162
|
draws: 0,
|
|
@@ -177,7 +177,7 @@ ${m}
|
|
|
177
177
|
for (let [e, n] of Object.entries(t.images ?? {})) this.sources[`i:${e}`] = n;
|
|
178
178
|
this.setTarget(t.target);
|
|
179
179
|
}
|
|
180
|
-
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("
|
|
180
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
181
181
|
let n = new o(t);
|
|
182
182
|
return _(e).use(n), n;
|
|
183
183
|
}
|
package/dist/path.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type TileMapData } from './index.js';
|
|
2
|
-
import {
|
|
2
|
+
import type { MapGrid } from './layout.js';
|
|
3
3
|
/** Cells to search: `cost[y * width + x]` is the price of stepping into a cell; 0, a negative or Infinity blocks it. */
|
|
4
4
|
export interface CostGrid {
|
|
5
5
|
readonly width: number;
|
|
@@ -20,6 +20,13 @@ export interface PathOptions {
|
|
|
20
20
|
diagonal?: 'never' | 'always' | 'no-corners';
|
|
21
21
|
/** Stops a search after this many cells; `findPath` then returns null. Default 1,000,000. */
|
|
22
22
|
maxCells?: number;
|
|
23
|
+
/**
|
|
24
|
+
* `findPath` only: jump point search, which visits far fewer cells on open maps for the same cost. It applies where it
|
|
25
|
+
* is exact, an orthogonal or isometric grid whose open cells all cost the same with `diagonal: 'no-corners'`; on any
|
|
26
|
+
* other grid the search is plain A*. Paths are cheapest ones, as plain A*'s, though where several tie the cells may
|
|
27
|
+
* differ. Default false.
|
|
28
|
+
*/
|
|
29
|
+
jumpPoints?: boolean;
|
|
23
30
|
}
|
|
24
31
|
/** Settings of {@link tileCosts}. */
|
|
25
32
|
export interface TileCostOptions {
|
|
@@ -43,6 +50,13 @@ export declare function tileCosts(map: TileMapData, options?: TileCostOptions):
|
|
|
43
50
|
export interface GridPath {
|
|
44
51
|
readonly cells: [number, number][];
|
|
45
52
|
readonly cost: number;
|
|
53
|
+
/** Cells the search took from its queue and expanded (with `jumpPoints`, the jump points). */
|
|
54
|
+
readonly searched: number;
|
|
55
|
+
/**
|
|
56
|
+
* Cells a jump passed over without queueing them, with `jumpPoints` (0 for plain A*). A scanned cell costs a few
|
|
57
|
+
* reads of the grid, an expanded one a heap pop and pushes. `maxCells` limits the two together.
|
|
58
|
+
*/
|
|
59
|
+
readonly scanned: number;
|
|
46
60
|
}
|
|
47
61
|
/**
|
|
48
62
|
* A cheapest path from `start` to `goal` by A*, or null when the goal cannot be reached (or `maxCells` ran out). A step
|