@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
package/dist/path.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { a as e } from "./src-CJvCfCkX.js";
|
|
2
|
-
import { r as t } from "./
|
|
2
|
+
import { a as t, i as n, n as r, o as i, r as a, s as o, t as s } from "./grid-search-B0VbYKpe.js";
|
|
3
3
|
//#region src/path.ts
|
|
4
|
-
function
|
|
4
|
+
function c(t, n = {}) {
|
|
5
5
|
let { property: r = "cost", solid: i = "solid", fallback: a = 1 } = n, o = n.empty ?? a;
|
|
6
6
|
if (!(a > 0)) throw RangeError(`fallback must be above 0, got ${a}.`);
|
|
7
7
|
let s = t.width, c = t.height, l = new Float64Array(s * c).fill(NaN), u = /* @__PURE__ */ new Map(), d = (e) => {
|
|
@@ -29,135 +29,107 @@ function n(t, n = {}) {
|
|
|
29
29
|
layout: t
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
let n = this.cells.length;
|
|
51
|
-
for (this.cells.push(e), this.keys.push(t), this.order.push(this.pushed++); n > 0;) {
|
|
52
|
-
let e = n - 1 >> 1;
|
|
53
|
-
if (!this.less(n, e)) break;
|
|
54
|
-
this.swap(n, e), n = e;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
pop() {
|
|
58
|
-
let e = this.cells[0], t = this.cells.length - 1;
|
|
59
|
-
this.swap(0, t), this.cells.pop(), this.keys.pop(), this.order.pop();
|
|
60
|
-
for (let e = 0;;) {
|
|
61
|
-
let t = e * 2 + 1, n = t + 1, r = e;
|
|
62
|
-
if (t < this.cells.length && this.less(t, r) && (r = t), n < this.cells.length && this.less(n, r) && (r = n), r === e) break;
|
|
63
|
-
this.swap(e, r), e = r;
|
|
32
|
+
function l(e, r, c, l = {}) {
|
|
33
|
+
let { diagonal: d, max: f, square: p } = a(e, l), m = t(e, r, "start"), h = t(e, c, "goal"), { width: g, cost: _ } = e;
|
|
34
|
+
if (!i(_[h]) || !i(_[m])) return null;
|
|
35
|
+
let v = Infinity, y = 0;
|
|
36
|
+
for (let e = 0; e < _.length; e++) i(_[e]) && (_[e] < v && (v = _[e]), _[e] > y && (y = _[e]));
|
|
37
|
+
if (l.jumpPoints && p && d === "no-corners" && v === y) return u(e, m, h, v, f);
|
|
38
|
+
let b = n(e, p, d, v), x = (e) => b(e, h), S = new Float64Array(_.length).fill(Infinity), C = new Int32Array(_.length).fill(-1), w = new Uint8Array(_.length), T = new s();
|
|
39
|
+
S[m] = 0, T.push(m, x(m));
|
|
40
|
+
let E = 0;
|
|
41
|
+
for (; T.size;) {
|
|
42
|
+
let t = T.pop();
|
|
43
|
+
if (!w[t]) {
|
|
44
|
+
if (w[t] = 1, E++, t === h) break;
|
|
45
|
+
if (E > f) return null;
|
|
46
|
+
o(e, p, d, t, (e, n) => {
|
|
47
|
+
let r = S[t] + _[e] * n;
|
|
48
|
+
r < S[e] && (S[e] = r, C[e] = t, T.push(e, r + x(e)));
|
|
49
|
+
});
|
|
64
50
|
}
|
|
65
|
-
return e;
|
|
66
51
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
let
|
|
70
|
-
if (n !== "never" && n !== "always" && n !== "no-corners") throw RangeError(`diagonal must be never, always or no-corners, got ${n}.`);
|
|
71
|
-
let r = t.maxCells ?? 1e6;
|
|
72
|
-
if (!(r >= 1)) throw RangeError(`maxCells must be 1 or more, got ${r}.`);
|
|
73
|
-
let i = e.layout?.orientation ?? "orthogonal";
|
|
52
|
+
if (!w[h]) return null;
|
|
53
|
+
let D = [];
|
|
54
|
+
for (let e = h; e !== -1; e = C[e]) D.push([e % g, Math.floor(e / g)]);
|
|
74
55
|
return {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
56
|
+
cells: D.reverse(),
|
|
57
|
+
cost: S[h],
|
|
58
|
+
searched: E,
|
|
59
|
+
scanned: 0
|
|
78
60
|
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
[-1, 1],
|
|
92
|
-
[-1, -1]
|
|
93
|
-
]) p(d + e, f + t) && i(u[(f + t) * c + d + e]) && (a !== "no-corners" || i(u[f * c + d + e]) && i(u[(f + t) * c + d])) && s((f + t) * c + d + e, r);
|
|
94
|
-
} else for (let [n, r] of t(e.layout, d, f)) p(n, r) && i(u[r * c + n]) && s(r * c + n, 1);
|
|
95
|
-
}, c = (e, [t, n], r) => {
|
|
96
|
-
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.`);
|
|
97
|
-
return n * e.width + t;
|
|
98
|
-
};
|
|
99
|
-
function l(e, t, n, l = {}) {
|
|
100
|
-
let { diagonal: d, max: f, square: p } = o(e, l), m = c(e, t, "start"), h = c(e, n, "goal"), { width: g, cost: _ } = e;
|
|
101
|
-
if (!i(_[h]) || !i(_[m])) return null;
|
|
102
|
-
let v = Infinity;
|
|
103
|
-
for (let e = 0; e < _.length; e++) i(_[e]) && _[e] < v && (v = _[e]);
|
|
104
|
-
let y = n[0], b = n[1], x = p ? void 0 : u(e.layout, y, b), S = (t) => {
|
|
105
|
-
let n = t % g, i = (t - n) / g;
|
|
106
|
-
if (x) {
|
|
107
|
-
let [t, r, a] = u(e.layout, n, i);
|
|
108
|
-
return v * Math.max(Math.abs(t - x[0]), Math.abs(r - x[1]), Math.abs(a - x[2]));
|
|
61
|
+
}
|
|
62
|
+
function u(e, t, n, a, o) {
|
|
63
|
+
let { width: c, height: l, cost: u } = e, d = n % c, f = (n - d) / c, p = (e, t) => e >= 0 && t >= 0 && e < c && t < l && i(u[t * c + e]), m = (e, t) => {
|
|
64
|
+
let n = Math.abs(e), i = Math.abs(t);
|
|
65
|
+
return Math.max(n, i) + (r - 1) * Math.min(n, i);
|
|
66
|
+
}, h = new Float64Array(u.length).fill(Infinity), g = new Int32Array(u.length).fill(-1), _ = new Uint8Array(u.length), v = new s(), y = 0, b = (e, t, n, r) => {
|
|
67
|
+
for (;;) {
|
|
68
|
+
if (e += n, t += r, y++, !p(e, t)) return -1;
|
|
69
|
+
if (e === d && t === f) return t * c + e;
|
|
70
|
+
if (n) {
|
|
71
|
+
if (p(e, t - 1) && !p(e - n, t - 1) || p(e, t + 1) && !p(e - n, t + 1)) return t * c + e;
|
|
72
|
+
} else if (p(e - 1, t) && !p(e - 1, t - r) || p(e + 1, t) && !p(e + 1, t - r)) return t * c + e;
|
|
109
73
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
74
|
+
}, x = (e, t, n, r) => {
|
|
75
|
+
for (;;) {
|
|
76
|
+
if (!(p(e + n, t + r) && p(e + n, t) && p(e, t + r))) return -1;
|
|
77
|
+
if (e += n, t += r, y++, e === d && t === f || b(e, t, n, 0) !== -1 || b(e, t, 0, r) !== -1) return t * c + e;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
h[t] = 0, v.push(t, a * m(d - t % c, f - Math.floor(t / c)));
|
|
81
|
+
let S = 0;
|
|
82
|
+
for (; v.size;) {
|
|
83
|
+
let e = v.pop();
|
|
84
|
+
if (_[e]) continue;
|
|
85
|
+
if (_[e] = 1, S++, e === n) break;
|
|
86
|
+
if (S + y > o) return null;
|
|
87
|
+
let t = e % c, r = (e - t) / c, i = g[e], s = [];
|
|
88
|
+
if (i < 0) for (let e = -1; e <= 1; e++) for (let t = -1; t <= 1; t++) (t || e) && s.push([t, e]);
|
|
89
|
+
else {
|
|
90
|
+
let e = i % c, n = Math.sign(t - e), a = Math.sign(r - (i - e) / c);
|
|
91
|
+
if (n && a) s.push([n, 0], [0, a], [n, a]);
|
|
92
|
+
else if (n) {
|
|
93
|
+
s.push([n, 0]);
|
|
94
|
+
for (let e of [-1, 1]) p(t, r + e) && !p(t - n, r + e) && s.push([0, e], [n, e]);
|
|
95
|
+
} else {
|
|
96
|
+
s.push([0, a]);
|
|
97
|
+
for (let e of [-1, 1]) p(t + e, r) && !p(t + e, r - a) && s.push([e, 0], [e, a]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
for (let [n, i] of s) {
|
|
101
|
+
let o = n && i ? x(t, r, n, i) : b(t, r, n, i);
|
|
102
|
+
if (o < 0 || _[o]) continue;
|
|
103
|
+
let s = o % c, l = (o - s) / c, u = h[e] + a * m(s - t, l - r);
|
|
104
|
+
u < h[o] && (h[o] = u, g[o] = e, v.push(o, u + a * m(d - s, f - l)));
|
|
124
105
|
}
|
|
125
106
|
}
|
|
126
|
-
if (!
|
|
127
|
-
let
|
|
128
|
-
for (let e =
|
|
107
|
+
if (!_[n]) return null;
|
|
108
|
+
let C = [];
|
|
109
|
+
for (let e = n; e !== -1; e = g[e]) C.push(e);
|
|
110
|
+
C.reverse();
|
|
111
|
+
let w = [[t % c, Math.floor(t / c)]];
|
|
112
|
+
for (let e = 1; e < C.length; e++) {
|
|
113
|
+
let [t, n] = w[w.length - 1], r = C[e] % c, i = Math.floor(C[e] / c), a = Math.sign(r - t), o = Math.sign(i - n);
|
|
114
|
+
for (; t !== r || n !== i;) t += t === r ? 0 : a, n += n === i ? 0 : o, w.push([t, n]);
|
|
115
|
+
}
|
|
129
116
|
return {
|
|
130
|
-
cells:
|
|
131
|
-
cost:
|
|
117
|
+
cells: w,
|
|
118
|
+
cost: h[n],
|
|
119
|
+
searched: S,
|
|
120
|
+
scanned: y
|
|
132
121
|
};
|
|
133
122
|
}
|
|
134
|
-
|
|
135
|
-
let r = (
|
|
136
|
-
|
|
137
|
-
let
|
|
138
|
-
|
|
139
|
-
e,
|
|
140
|
-
n,
|
|
141
|
-
-e - n
|
|
142
|
-
];
|
|
143
|
-
}
|
|
144
|
-
let i = n - (r ? (t - (t & 1)) / 2 : (t + (t & 1)) / 2);
|
|
145
|
-
return [
|
|
146
|
-
t,
|
|
147
|
-
i,
|
|
148
|
-
-t - i
|
|
149
|
-
];
|
|
150
|
-
};
|
|
151
|
-
function d(e, t, n = {}) {
|
|
152
|
-
let { diagonal: r, max: l, square: u } = o(e, n), { width: d, height: f, cost: p } = e, m = new Float64Array(p.length).fill(Infinity), h = new Int32Array(p.length).fill(-1), g = new Uint8Array(p.length), _ = new a();
|
|
153
|
-
for (let n of t) {
|
|
154
|
-
let t = c(e, n, "goal");
|
|
155
|
-
i(p[t]) && m[t] !== 0 && (m[t] = 0, _.push(t, 0));
|
|
123
|
+
function d(e, n, r = {}) {
|
|
124
|
+
let { diagonal: c, max: l, square: u } = a(e, r), { width: d, height: f, cost: p } = e, m = new Float64Array(p.length).fill(Infinity), h = new Int32Array(p.length).fill(-1), g = new Uint8Array(p.length), _ = new s();
|
|
125
|
+
for (let r of n) {
|
|
126
|
+
let n = t(e, r, "goal");
|
|
127
|
+
i(p[n]) && m[n] !== 0 && (m[n] = 0, _.push(n, 0));
|
|
156
128
|
}
|
|
157
129
|
let v = 0;
|
|
158
130
|
for (; _.size && v < l;) {
|
|
159
131
|
let t = _.pop();
|
|
160
|
-
g[t] || (g[t] = 1, v++,
|
|
132
|
+
g[t] || (g[t] = 1, v++, o(e, u, c, t, (e, n) => {
|
|
161
133
|
let r = m[t] + p[t] * n;
|
|
162
134
|
r < m[e] && (m[e] = r, h[e] = t, _.push(e, r));
|
|
163
135
|
}));
|
|
@@ -175,4 +147,4 @@ function f(e, t, n) {
|
|
|
175
147
|
return r;
|
|
176
148
|
}
|
|
177
149
|
//#endregion
|
|
178
|
-
export { l as findPath, d as flowField, f as followFlow,
|
|
150
|
+
export { l as findPath, d as flowField, f as followFlow, c as tileCosts };
|
package/dist/plane.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 n=(engine,version="
|
|
3
|
+
const n=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
4
|
import { k as e } from "./src-CJvCfCkX.js";
|
|
5
5
|
import { t } from "./objects-BK2mewd1.js";
|
|
6
6
|
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
@@ -33,7 +33,7 @@ var r = class r extends t {
|
|
|
33
33
|
if (n.length !== 16) throw e("Plane transform needs sixteen affine values", "tilemap-options");
|
|
34
34
|
this.transform = Float32Array.from(n), this.depthWrite = t.depthWrite ?? !0, this.depthTest = t.depthTest ?? !0, this.checkTransform();
|
|
35
35
|
}
|
|
36
|
-
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("
|
|
36
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
37
37
|
let i = new r(t);
|
|
38
38
|
return n(e).use(i), i;
|
|
39
39
|
}
|
package/dist/replan.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { CostGrid, GridPath, PathOptions } from './path.js';
|
|
2
|
+
/** Settings of {@link PathReplanner}. */
|
|
3
|
+
export interface ReplanOptions {
|
|
4
|
+
/** Corner steps, as `findPath` takes them. Default `'no-corners'`. */
|
|
5
|
+
diagonal?: PathOptions['diagonal'];
|
|
6
|
+
/** Stops one `findPath` call after this many expanded cells; the work done is kept. Default 1,000,000. */
|
|
7
|
+
maxCells?: number;
|
|
8
|
+
/**
|
|
9
|
+
* The lowest cost an open cell will ever have, which the distance estimate is multiplied by. A changed cell below it
|
|
10
|
+
* starts the search afresh, so set it when costs will drop below today's lowest. Default the lowest cost on the grid.
|
|
11
|
+
*/
|
|
12
|
+
minCost?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Cheapest paths to one goal over a grid that changes, for a unit that moves along its path and learns of new walls on
|
|
16
|
+
* the way. Change `grid.cost`, tell {@link PathReplanner.update} which cells changed, and ask for the path again: the
|
|
17
|
+
* cost is the one a fresh A* search would find, usually for a fraction of the work.
|
|
18
|
+
*/
|
|
19
|
+
export declare class PathReplanner {
|
|
20
|
+
/** The grid searched; change its costs in place, then call {@link update}. */
|
|
21
|
+
readonly grid: CostGrid;
|
|
22
|
+
readonly goal: readonly [number, number];
|
|
23
|
+
private readonly diagonal;
|
|
24
|
+
private readonly square;
|
|
25
|
+
private readonly max;
|
|
26
|
+
private readonly to;
|
|
27
|
+
private low;
|
|
28
|
+
private estimate;
|
|
29
|
+
/** The start of the last search, -1 before the first. */
|
|
30
|
+
private last;
|
|
31
|
+
private km;
|
|
32
|
+
private readonly g;
|
|
33
|
+
private readonly rhs;
|
|
34
|
+
private readonly heap;
|
|
35
|
+
private readonly slot;
|
|
36
|
+
private readonly key1;
|
|
37
|
+
private readonly key2;
|
|
38
|
+
constructor(grid: CostGrid, goal: readonly [number, number], options?: ReplanOptions);
|
|
39
|
+
/**
|
|
40
|
+
* Tells the replanner which cells' costs changed since the last call. Every changed cell must be listed, or later
|
|
41
|
+
* paths may use the old costs.
|
|
42
|
+
*/
|
|
43
|
+
update(cells: Iterable<readonly [number, number]>): this;
|
|
44
|
+
/**
|
|
45
|
+
* A cheapest path from `start` to the goal under the costs as last told, or null when there is no way (or `maxCells`
|
|
46
|
+
* ran out; calling again continues from there). `searched` counts the cells this call expanded.
|
|
47
|
+
*/
|
|
48
|
+
findPath(start: readonly [number, number]): GridPath | null;
|
|
49
|
+
/** Sets a cell's look-ahead from its steps: 0 at an open goal, Infinity on a blocked cell. */
|
|
50
|
+
private look;
|
|
51
|
+
/** Expands cells until the start's distance is settled; the cells expanded, or -1 when `maxCells` ran out. */
|
|
52
|
+
private search;
|
|
53
|
+
/** Queues `u` under today's key when its distance and look-ahead disagree, and takes it out when they agree. */
|
|
54
|
+
private place;
|
|
55
|
+
private remove;
|
|
56
|
+
/** Moves the cell in place `i` up or down to where its key belongs. */
|
|
57
|
+
private sift;
|
|
58
|
+
}
|
package/dist/replan.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { a as e, i as t, o as n, r, s as i } from "./grid-search-B0VbYKpe.js";
|
|
2
|
+
import { MargoError as a } from "@margoengine/math/errors";
|
|
3
|
+
//#region src/replan.ts
|
|
4
|
+
var o = class {
|
|
5
|
+
constructor(i, a, o = {}) {
|
|
6
|
+
this.last = -1, this.km = 0, this.heap = [];
|
|
7
|
+
let { diagonal: s, max: c, square: l } = r(i, o);
|
|
8
|
+
this.grid = i, this.diagonal = s, this.square = l, this.max = c, this.to = e(i, a, "goal"), this.goal = [a[0], a[1]];
|
|
9
|
+
let u = o.minCost;
|
|
10
|
+
if (u === void 0) {
|
|
11
|
+
u = Infinity;
|
|
12
|
+
for (let e = 0; e < i.cost.length; e++) n(i.cost[e]) && i.cost[e] < u && (u = i.cost[e]);
|
|
13
|
+
u === Infinity && (u = 1);
|
|
14
|
+
} else if (!n(u)) throw RangeError(`minCost must be a finite number above 0, got ${u}.`);
|
|
15
|
+
this.low = u, this.estimate = t(i, l, s, u);
|
|
16
|
+
let d = i.cost.length;
|
|
17
|
+
this.g = new Float64Array(d), this.rhs = new Float64Array(d), this.slot = new Int32Array(d), this.key1 = new Float64Array(d), this.key2 = new Float64Array(d);
|
|
18
|
+
}
|
|
19
|
+
update(r) {
|
|
20
|
+
let { grid: i } = this, a = /* @__PURE__ */ new Set(), o = this.low;
|
|
21
|
+
for (let t of r) {
|
|
22
|
+
let r = e(i, t, "cell"), [s, c] = t;
|
|
23
|
+
n(i.cost[r]) && i.cost[r] < o && (o = i.cost[r]), a.add(r);
|
|
24
|
+
for (let e = -1; e <= 1; e++) for (let t = -1; t <= 1; t++) s + t >= 0 && c + e >= 0 && s + t < i.width && c + e < i.height && a.add(r + e * i.width + t);
|
|
25
|
+
}
|
|
26
|
+
if (o < this.low) this.low = o, this.estimate = t(i, this.square, this.diagonal, o), this.last = -1;
|
|
27
|
+
else if (this.last >= 0) for (let e of a) this.look(e), this.place(e);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
findPath(t) {
|
|
31
|
+
let { grid: r, g: o, rhs: s, to: c } = this, { cost: l, width: u } = r, d = e(r, t, "start");
|
|
32
|
+
if (this.last < 0 ? (o.fill(Infinity), s.fill(Infinity), this.slot.fill(-1), this.heap.length = 0, this.km = 0, this.last = d, this.look(c), this.place(c)) : d !== this.last && (this.km += this.estimate(this.last, d), this.last = d), !n(l[d]) || !n(l[c])) return null;
|
|
33
|
+
let f = this.search(d);
|
|
34
|
+
if (f < 0 || s[d] === Infinity) return null;
|
|
35
|
+
let p = [[t[0], t[1]]], m = 0;
|
|
36
|
+
for (let e = d; e !== c;) {
|
|
37
|
+
let t = Infinity, n = -1, s = 0;
|
|
38
|
+
if (i(r, this.square, this.diagonal, e, (e, r) => {
|
|
39
|
+
let i = l[e] * r + o[e];
|
|
40
|
+
i < t && (t = i, n = e, s = l[e] * r);
|
|
41
|
+
}), n < 0 || p.length > l.length) throw new a("state", "The grid changed without update(); pass every changed cell to update() before findPath().");
|
|
42
|
+
e = n, m += s, p.push([e % u, Math.floor(e / u)]);
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
cells: p,
|
|
46
|
+
cost: m,
|
|
47
|
+
searched: f,
|
|
48
|
+
scanned: 0
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
look(e) {
|
|
52
|
+
let { grid: t, g: r } = this, { cost: a } = t, o = Infinity;
|
|
53
|
+
n(a[e]) && (e === this.to ? o = 0 : i(t, this.square, this.diagonal, e, (e, t) => {
|
|
54
|
+
let n = a[e] * t + r[e];
|
|
55
|
+
n < o && (o = n);
|
|
56
|
+
})), this.rhs[e] = o;
|
|
57
|
+
}
|
|
58
|
+
search(e) {
|
|
59
|
+
let { grid: t, g: r, rhs: a, heap: o, key1: s, key2: c, to: l } = this, { cost: u } = t, d = 0;
|
|
60
|
+
for (; o.length;) {
|
|
61
|
+
let f = o[0], p = Math.min(r[e], a[e]) + this.km;
|
|
62
|
+
if (!(s[f] <= p + p * 1e-9 || a[e] > r[e])) break;
|
|
63
|
+
let m = Math.min(r[f], a[f]), h = m + this.estimate(this.last, f) + this.km;
|
|
64
|
+
if (s[f] < h || s[f] === h && c[f] < m) this.place(f);
|
|
65
|
+
else {
|
|
66
|
+
if (++d > this.max) return -1;
|
|
67
|
+
if (r[f] > a[f]) {
|
|
68
|
+
if (r[f] = a[f], this.remove(f), !n(u[f])) continue;
|
|
69
|
+
i(t, this.square, this.diagonal, f, (e, t) => {
|
|
70
|
+
e !== l && (a[e] = Math.min(a[e], u[f] * t + r[f])), this.place(e);
|
|
71
|
+
});
|
|
72
|
+
} else {
|
|
73
|
+
let e = r[f];
|
|
74
|
+
r[f] = Infinity, i(t, this.square, this.diagonal, f, (t, n) => {
|
|
75
|
+
t !== l && a[t] === u[f] * n + e && this.look(t), this.place(t);
|
|
76
|
+
}), this.place(f);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return d;
|
|
81
|
+
}
|
|
82
|
+
place(e) {
|
|
83
|
+
let { g: t, rhs: n, slot: r, key1: i, key2: a, heap: o } = this;
|
|
84
|
+
if (t[e] === n[e]) {
|
|
85
|
+
this.remove(e);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
let s = Math.min(t[e], n[e]);
|
|
89
|
+
i[e] = s + this.estimate(this.last, e) + this.km, a[e] = s, r[e] < 0 && (r[e] = o.length, o.push(e)), this.sift(r[e]);
|
|
90
|
+
}
|
|
91
|
+
remove(e) {
|
|
92
|
+
let { slot: t, heap: n } = this, r = t[e];
|
|
93
|
+
if (r < 0) return;
|
|
94
|
+
t[e] = -1;
|
|
95
|
+
let i = n.pop();
|
|
96
|
+
i !== e && (n[r] = i, t[i] = r, this.sift(r));
|
|
97
|
+
}
|
|
98
|
+
sift(e) {
|
|
99
|
+
let { heap: t, slot: n, key1: r, key2: i } = this, a = t[e], o = (e, t) => r[e] < r[t] || r[e] === r[t] && i[e] < i[t];
|
|
100
|
+
for (; e > 0;) {
|
|
101
|
+
let r = e - 1 >> 1;
|
|
102
|
+
if (!o(a, t[r])) break;
|
|
103
|
+
t[e] = t[r], n[t[e]] = e, e = r;
|
|
104
|
+
}
|
|
105
|
+
for (;;) {
|
|
106
|
+
let r = e * 2 + 1;
|
|
107
|
+
if (r >= t.length || (r + 1 < t.length && o(t[r + 1], t[r]) && r++, !o(t[r], a))) break;
|
|
108
|
+
t[e] = t[r], n[t[e]] = e, e = r;
|
|
109
|
+
}
|
|
110
|
+
t[e] = a, n[a] = e;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
//#endregion
|
|
114
|
+
export { o as PathReplanner };
|
package/dist/src-CJvCfCkX.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 e=(engine,version="
|
|
3
|
+
const e=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
4
|
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
5
5
|
import { hexColor as t } from "@margoengine/math";
|
|
6
6
|
//#region ../shared/errors.js
|
|
@@ -177,7 +177,7 @@ function E(e) {
|
|
|
177
177
|
}
|
|
178
178
|
//#endregion
|
|
179
179
|
//#region src/renderer.ts
|
|
180
|
-
var D = "\nstruct Layer{size:vec2f,origin:vec2f,cell:vec2f,zoom:f32,opacity:f32,tint:vec4f,chunks:u32}\nstruct Sheet{first:u32,count:u32,columns:u32,margin:u32,tile:vec2f,step:vec2f,offset:vec2f}\n@group(0) @binding(0) var<uniform> layer:Layer;\n@group(1) @binding(0) var<storage,read> cells:array<u32>;\n@group(2) @binding(0) var<uniform> sheet:Sheet;\n@group(2) @binding(1) var<storage,read> frames:array<u32>;\n@group(2) @binding(2) var atlas:texture_2d<f32>;\nstruct Varying{\n @builtin(position) position:vec4f,\n @location(0) texel:vec2f,\n @location(1) @interpolate(flat) low:vec2f,\n @location(2) @interpolate(flat) high:vec2f,\n}\n@vertex fn vertexMain(@builtin(vertex_index) corner:u32,@builtin(instance_index) index:u32)->Varying{\n var v:Varying;\n let value=cells[index];\n let gid=value&0x0fffffffu;\n if(gid<sheet.first||gid>=sheet.first+sheet.count){v.position=vec4f(2,2,2,1);return v;}\n let id=frames[gid-sheet.first];\n let chunk=index/4096u;\n let inner=index%4096u;\n let tile=vec2u(chunk%layer.chunks,chunk/layer.chunks)*64u+vec2u(inner%64u,inner/64u);\n let c=vec2f(f32(corner&1u),f32(corner>>1u));\n let diagonal=(value&0x20000000u)!=0u;\n let drawn=select(sheet.tile,sheet.tile.yx,diagonal);\n let place=vec2f(tile)*layer.cell+vec2f(0,layer.cell.y-drawn.y)+sheet.offset;\n let p=layer.origin+(place+c*drawn)*layer.zoom;\n v.position=vec4f(p/layer.size*vec2f(2,-2)+vec2f(-1,1),0,1);\n\n var s=c;\n if((value&0x80000000u)!=0u){s.x=1-s.x;}\n if((value&0x40000000u)!=0u){s.y=1-s.y;}\n if(diagonal){s=s.yx;}\n let corner0=vec2f(f32(sheet.margin))+vec2f(f32(id%sheet.columns),f32(id/sheet.columns))*sheet.step;\n v.texel=corner0+s*sheet.tile;\n v.low=corner0;\n v.high=corner0+sheet.tile-1;\n return v;\n}\n@fragment fn fragmentMain(v:Varying)->@location(0) vec4f{\n var color=textureLoad(atlas,vec2i(clamp(floor(v.texel),v.low,v.high)),0)*layer.tint;\n color.a*=layer.opacity;\n if(color.a==0){discard;}\n return color;\n}", O = 256, k = 64, A = 40, j = 0, M = class t {coreRequirement=__margoCoreRequirement("
|
|
180
|
+
var D = "\nstruct Layer{size:vec2f,origin:vec2f,cell:vec2f,zoom:f32,opacity:f32,tint:vec4f,chunks:u32}\nstruct Sheet{first:u32,count:u32,columns:u32,margin:u32,tile:vec2f,step:vec2f,offset:vec2f}\n@group(0) @binding(0) var<uniform> layer:Layer;\n@group(1) @binding(0) var<storage,read> cells:array<u32>;\n@group(2) @binding(0) var<uniform> sheet:Sheet;\n@group(2) @binding(1) var<storage,read> frames:array<u32>;\n@group(2) @binding(2) var atlas:texture_2d<f32>;\nstruct Varying{\n @builtin(position) position:vec4f,\n @location(0) texel:vec2f,\n @location(1) @interpolate(flat) low:vec2f,\n @location(2) @interpolate(flat) high:vec2f,\n}\n@vertex fn vertexMain(@builtin(vertex_index) corner:u32,@builtin(instance_index) index:u32)->Varying{\n var v:Varying;\n let value=cells[index];\n let gid=value&0x0fffffffu;\n if(gid<sheet.first||gid>=sheet.first+sheet.count){v.position=vec4f(2,2,2,1);return v;}\n let id=frames[gid-sheet.first];\n let chunk=index/4096u;\n let inner=index%4096u;\n let tile=vec2u(chunk%layer.chunks,chunk/layer.chunks)*64u+vec2u(inner%64u,inner/64u);\n let c=vec2f(f32(corner&1u),f32(corner>>1u));\n let diagonal=(value&0x20000000u)!=0u;\n let drawn=select(sheet.tile,sheet.tile.yx,diagonal);\n let place=vec2f(tile)*layer.cell+vec2f(0,layer.cell.y-drawn.y)+sheet.offset;\n let p=layer.origin+(place+c*drawn)*layer.zoom;\n v.position=vec4f(p/layer.size*vec2f(2,-2)+vec2f(-1,1),0,1);\n\n var s=c;\n if((value&0x80000000u)!=0u){s.x=1-s.x;}\n if((value&0x40000000u)!=0u){s.y=1-s.y;}\n if(diagonal){s=s.yx;}\n let corner0=vec2f(f32(sheet.margin))+vec2f(f32(id%sheet.columns),f32(id/sheet.columns))*sheet.step;\n v.texel=corner0+s*sheet.tile;\n v.low=corner0;\n v.high=corner0+sheet.tile-1;\n return v;\n}\n@fragment fn fragmentMain(v:Varying)->@location(0) vec4f{\n var color=textureLoad(atlas,vec2i(clamp(floor(v.texel),v.low,v.high)),0)*layer.tint;\n color.a*=layer.opacity;\n if(color.a==0){discard;}\n return color;\n}", O = 256, k = 64, A = 40, j = 0, M = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
181
181
|
constructor(e) {
|
|
182
182
|
if (this.time = 0, this.visible = !0, this.stats = {
|
|
183
183
|
draws: 0,
|
|
@@ -202,7 +202,7 @@ var D = "\nstruct Layer{size:vec2f,origin:vec2f,cell:vec2f,zoom:f32,opacity:f32,
|
|
|
202
202
|
zoom: 1
|
|
203
203
|
}, this.snap = e.snap ?? !0, this.parallax = e.parallax ?? !0, this.setTarget(e.target), this.clear = e.clear, this.order = t, this.layerMask = g(e.layers ?? 1, "TileMap layers"), this.name = e.name ?? `tilemap-${++j}`, this.setMap(e.map);
|
|
204
204
|
}
|
|
205
|
-
static attach(n, r) {__margoAssertCore(n,__margoCoreRequirement("
|
|
205
|
+
static attach(n, r) {__margoAssertCore(n,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
206
206
|
let i = new t(r);
|
|
207
207
|
return e(n).use(i), i;
|
|
208
208
|
}
|