@margoengine/tilemap 0.0.0-stage → 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.
@@ -0,0 +1,105 @@
1
+ import { type TileLayer, type Properties } from './index.js';
2
+ /** Tiled Wang colour metadata. Array index plus one is the colour id; zero means no terrain. */
3
+ export interface WangColor {
4
+ readonly name: string;
5
+ readonly color: string;
6
+ readonly tile: number;
7
+ readonly probability: number;
8
+ readonly properties?: Properties;
9
+ }
10
+ /** Edge/corner colours clockwise: N, NE, E, SE, S, SW, W, NW. */
11
+ export interface WangTile {
12
+ readonly tile: number;
13
+ readonly colors: readonly [number, number, number, number, number, number, number, number];
14
+ readonly weight?: number;
15
+ /** Global-id flip bits carried by older Tiled Wang entries. Default zero. */
16
+ readonly flags?: number;
17
+ }
18
+ /** Imported Wang rules, including multi-colour edge, corner and mixed sets. */
19
+ export interface WangRuleSet {
20
+ readonly mode: 'wang';
21
+ readonly name: string;
22
+ readonly type: 'edge' | 'corner' | 'mixed';
23
+ readonly colors: readonly WangColor[];
24
+ readonly tiles: readonly WangTile[];
25
+ readonly properties?: Properties;
26
+ }
27
+ /** Shared lattice values make neighbouring Wang cells agree on edges and corners. */
28
+ export interface WangGrid {
29
+ /** (width + 1) * (height + 1) corner colours. Missing means unconstrained. -1 is a wildcard. */
30
+ readonly vertices?: ArrayLike<number>;
31
+ /** width * (height + 1) horizontal edges, row-major. */
32
+ readonly horizontal?: ArrayLike<number>;
33
+ /** (width + 1) * height vertical edges, row-major. */
34
+ readonly vertical?: ArrayLike<number>;
35
+ }
36
+ /** Match imported Wang rules against a shared lattice, with seeded weighted variants. Unmatched cells are zero. */
37
+ export declare function resolveWang(grid: WangGrid, width: number, height: number, set: WangRuleSet, options?: TerrainOptions): Uint32Array;
38
+ /** The 47 distinct blob masks, ascending. Bits: N 1, NE 2, E 4, SE 8, S 16, SW 32, W 64, NW 128. */
39
+ export declare const BLOB_MASKS: readonly number[];
40
+ /** Bitmask of the four edge neighbours: N 1, E 2, S 4, W 8. Any truthy argument counts as filled. */
41
+ export declare function cardinalMask(n: unknown, e: unknown, s: unknown, w: unknown): number;
42
+ /**
43
+ * Bitmask of all eight neighbours for blob tiling, reduced: a corner only counts when both edges beside it are
44
+ * filled, so the 256 raw cases collapse to the 47 values in `BLOB_MASKS`. Bits: N 1, NE 2, E 4, SE 8, S 16, SW 32, W 64, NW 128.
45
+ */
46
+ export declare function blobMask(n: unknown, ne: unknown, e: unknown, se: unknown, s: unknown, sw: unknown, w: unknown, nw: unknown): number;
47
+ /** Position of a reduced blob mask in `BLOB_MASKS` (0..46). Throws a RangeError for a mask that is not reduced. */
48
+ export declare function blobIndex(mask: number): number;
49
+ /**
50
+ * Tile choices as data. `cardinal`: 16 tiles indexed by `cardinalMask`. `blob`: 47 tiles in `BLOB_MASKS` order.
51
+ * `tiles` hold tile ids local to the tileset.
52
+ */
53
+ export interface AutotileRuleSet {
54
+ readonly mode: 'cardinal' | 'blob';
55
+ readonly tiles: readonly number[];
56
+ }
57
+ /** Options of `autotileGrid` and `autotile`. */
58
+ export interface AutotileOptions {
59
+ /** Global id of the tileset's first tile, added to each rule tile. Default 1. */
60
+ readonly firstGid?: number;
61
+ /** Whether cells beyond the map edge count as filled (the terrain continues off the map). Default false. */
62
+ readonly outside?: boolean;
63
+ }
64
+ /**
65
+ * Pick a tile for every filled cell of a `width x height` grid from the filled neighbours. Returns global tile
66
+ * values, 0 where the grid is empty. The same input always gives the same output.
67
+ */
68
+ export declare function autotileGrid(filled: ArrayLike<unknown>, width: number, height: number, rules: AutotileRuleSet, options?: AutotileOptions): Uint32Array;
69
+ /**
70
+ * Auto-tile a tile layer: cells that `matches` (default: any tile) get the tile their neighbours call for, other
71
+ * cells are copied unchanged. Returns a new layer.
72
+ */
73
+ export declare function autotile(layer: TileLayer, rules: AutotileRuleSet, options?: AutotileOptions & {
74
+ readonly matches?: (value: number) => boolean;
75
+ }): TileLayer;
76
+ /** A tile for terrain matching: terrain ids at its corners, top left, top right, bottom right, bottom left. */
77
+ export interface TerrainTile {
78
+ /** Tile id local to the tileset. */
79
+ readonly tile: number;
80
+ readonly corners: readonly [number, number, number, number];
81
+ /** Chance weight among tiles with the same corners. Default 1. */
82
+ readonly weight?: number;
83
+ }
84
+ /** Tiles tagged by the terrain at their corners. */
85
+ export interface TerrainSet {
86
+ readonly tiles: readonly TerrainTile[];
87
+ }
88
+ /** Options of `resolveTerrain`. */
89
+ export interface TerrainOptions {
90
+ /** Global id of the tileset's first tile. Default 1. */
91
+ readonly firstGid?: number;
92
+ /** Changes which of several matching tiles is picked; the same seed always gives the same map. Default 0. */
93
+ readonly seed?: number;
94
+ }
95
+ /**
96
+ * Pick the tile whose four corners equal the terrain at the four corners of each cell. `vertices` is the
97
+ * `(width + 1) x (height + 1)` grid of corner terrain ids. Cells no tile matches stay 0. Several matches are
98
+ * chosen by weight with a hash of the cell and seed, so the result is deterministic.
99
+ */
100
+ export declare function resolveTerrain(vertices: ArrayLike<number>, width: number, height: number, set: TerrainSet, options?: TerrainOptions): Uint32Array;
101
+ /**
102
+ * Corner terrain from painted cells: each corner takes the highest terrain id among the cells that touch it, so
103
+ * 0 (nothing) loses to any terrain. Returns the `(width + 1) x (height + 1)` grid for `resolveTerrain`.
104
+ */
105
+ export declare function verticesFromCells(cells: ArrayLike<number>, width: number, height: number): Uint8Array;
@@ -0,0 +1,116 @@
1
+ import "./src-CJvCfCkX.js";
2
+ //#region src/autotile.ts
3
+ function e(e, t, n, r, i = {}) {
4
+ if (!Number.isSafeInteger(t) || !Number.isSafeInteger(n) || t < 0 || n < 0 || t * n > 16777216) throw RangeError("Wang grid dimensions must be non-negative integers up to 16777216 cells; reduce the grid.");
5
+ let a = t + 1;
6
+ for (let [i, o] of [
7
+ [e.vertices, a * (n + 1)],
8
+ [e.horizontal, t * (n + 1)],
9
+ [e.vertical, a * n]
10
+ ]) if (i && (i.length !== o || Array.from(i).some((e) => !Number.isInteger(e) || e < -1 || e > r.colors.length))) throw RangeError("Wang lattice size or colour id is invalid; match the grid size and use -1 or a defined colour id.");
11
+ for (let e of r.tiles) if (e.colors.length !== 8 || !Number.isInteger(e.tile) || e.tile < 0 || e.colors.some((e) => !Number.isInteger(e) || e < 0 || e > r.colors.length) || !(Number.isFinite(e.weight ?? 1) && (e.weight ?? 1) >= 0)) throw RangeError("Invalid Wang tile; supply a non-negative tile id, eight defined colours and a non-negative finite weight.");
12
+ let o = new Uint32Array(t * n);
13
+ for (let s = 0; s < n; s++) for (let n = 0; n < t; n++) {
14
+ let c = [
15
+ e.horizontal?.[s * t + n] ?? -1,
16
+ e.vertices?.[s * a + n + 1] ?? -1,
17
+ e.vertical?.[s * a + n + 1] ?? -1,
18
+ e.vertices?.[(s + 1) * a + n + 1] ?? -1,
19
+ e.horizontal?.[(s + 1) * t + n] ?? -1,
20
+ e.vertices?.[(s + 1) * a + n] ?? -1,
21
+ e.vertical?.[s * a + n] ?? -1,
22
+ e.vertices?.[s * a + n] ?? -1
23
+ ], u = r.tiles.filter((e) => (e.weight ?? 1) > 0 && e.colors.every((e, t) => c[t] < 0 || c[t] === e)), d = u.reduce((e, t) => Math.max(e, t.weight ?? 1), 0), f = l(n, s, i.seed ?? 0) * u.reduce((e, t) => e + (t.weight ?? 1) / d, 0);
24
+ for (let e = 0; e < u.length; e++) {
25
+ let r = u[e];
26
+ if (f -= (r.weight ?? 1) / d, f < 0 || e === u.length - 1) {
27
+ o[s * t + n] = (i.firstGid ?? 1) + r.tile | (r.flags ?? 0);
28
+ break;
29
+ }
30
+ }
31
+ }
32
+ return o;
33
+ }
34
+ function t(e) {
35
+ let t = e & 255;
36
+ return t & 1 && t & 4 || (t &= -3), t & 4 && t & 16 || (t &= -9), t & 16 && t & 64 || (t &= -33), t & 64 && t & 1 || (t &= -129), t;
37
+ }
38
+ var n = (() => {
39
+ let e = /* @__PURE__ */ new Set();
40
+ for (let n = 0; n < 256; n++) e.add(t(n));
41
+ return [...e].sort((e, t) => e - t);
42
+ })(), r = new Map(n.map((e, t) => [e, t]));
43
+ function i(e, t, n, r) {
44
+ return !!e | (t ? 2 : 0) | (n ? 4 : 0) | (r ? 8 : 0);
45
+ }
46
+ function a(e, n, r, i, a, o, s, c) {
47
+ return t(!!e | (n ? 2 : 0) | (r ? 4 : 0) | (i ? 8 : 0) | (a ? 16 : 0) | (o ? 32 : 0) | (s ? 64 : 0) | (c ? 128 : 0));
48
+ }
49
+ function o(e) {
50
+ let t = r.get(e);
51
+ if (t === void 0) throw RangeError(`${e} is not a reduced blob mask; build it with blobMask().`);
52
+ return t;
53
+ }
54
+ function s(e, t, n, r, s = {}) {
55
+ let c = r.mode === "blob" ? 47 : r.mode === "cardinal" ? 16 : -1;
56
+ if (c < 0) throw TypeError(`rules.mode must be 'cardinal' or 'blob', got '${String(r.mode)}'.`);
57
+ if (r.tiles.length !== c) throw RangeError(`a ${r.mode} rule set needs ${c} tiles, got ${r.tiles.length}; list one per case.`);
58
+ if (e.length !== t * n) throw RangeError(`grid has ${e.length} cells for ${t} x ${n}; pass width * height of them.`);
59
+ let l = s.firstGid ?? 1, u = s.outside ?? !1, d = (r, i) => r < 0 || i < 0 || r >= t || i >= n ? u : !!e[i * t + r], f = new Uint32Array(t * n);
60
+ for (let s = 0; s < n; s++) for (let n = 0; n < t; n++) {
61
+ if (!e[s * t + n]) continue;
62
+ let c = r.mode === "blob" ? o(a(d(n, s - 1), d(n + 1, s - 1), d(n + 1, s), d(n + 1, s + 1), d(n, s + 1), d(n - 1, s + 1), d(n - 1, s), d(n - 1, s - 1))) : i(d(n, s - 1), d(n + 1, s), d(n, s + 1), d(n - 1, s));
63
+ f[s * t + n] = l + r.tiles[c];
64
+ }
65
+ return f;
66
+ }
67
+ function c(e, t, n = {}) {
68
+ let r = n.matches ?? ((e) => !!(e & 268435455)), i = new Uint8Array(e.data.length);
69
+ for (let t = 0; t < i.length; t++) i[t] = +!!r(e.data[t]);
70
+ let a = s(i, e.width, e.height, t, n), o = new Uint32Array(e.data);
71
+ for (let e = 0; e < o.length; e++) i[e] && (o[e] = a[e]);
72
+ return {
73
+ ...e,
74
+ data: o
75
+ };
76
+ }
77
+ function l(e, t, n) {
78
+ let r = (Math.imul(e, 668265261) ^ Math.imul(t, 374761393) ^ Math.imul(n, 2654435761)) >>> 0;
79
+ return r = Math.imul(r ^ r >>> 15, 2246822507), r = Math.imul(r ^ r >>> 13, 3266489909), ((r ^ r >>> 16) >>> 0) / 4294967296;
80
+ }
81
+ function u(e, t, n, r, i = {}) {
82
+ let a = t + 1;
83
+ if (e.length !== a * (n + 1)) throw RangeError(`vertex grid has ${e.length} values, expected ${a * (n + 1)}; pass (width + 1) * (height + 1).`);
84
+ let o = i.firstGid ?? 1, s = i.seed ?? 0, c = new Uint32Array(t * n);
85
+ for (let i = 0; i < n; i++) for (let n = 0; n < t; n++) {
86
+ let u = e[i * a + n], d = e[i * a + n + 1], f = e[(i + 1) * a + n + 1], p = e[(i + 1) * a + n], m = 0, h = [];
87
+ for (let e of r.tiles) e.corners[0] === u && e.corners[1] === d && e.corners[2] === f && e.corners[3] === p && (h.push(e), m += e.weight ?? 1);
88
+ if (h.length === 0) continue;
89
+ let g = l(n, i, s) * m, _ = h[h.length - 1];
90
+ for (let e of h) if (g -= e.weight ?? 1, g < 0) {
91
+ _ = e;
92
+ break;
93
+ }
94
+ c[i * t + n] = o + _.tile;
95
+ }
96
+ return c;
97
+ }
98
+ function d(e, t, n) {
99
+ if (e.length !== t * n) throw RangeError(`grid has ${e.length} cells for ${t} x ${n}; pass width * height of them.`);
100
+ let r = t + 1, i = new Uint8Array(r * (n + 1));
101
+ for (let a = 0; a < n; a++) for (let n = 0; n < t; n++) {
102
+ let o = e[a * t + n];
103
+ for (let [e, t] of [
104
+ [0, 0],
105
+ [1, 0],
106
+ [0, 1],
107
+ [1, 1]
108
+ ]) {
109
+ let s = (a + t) * r + n + e;
110
+ o > i[s] && (i[s] = o);
111
+ }
112
+ }
113
+ return i;
114
+ }
115
+ //#endregion
116
+ export { n as BLOB_MASKS, c as autotile, s as autotileGrid, o as blobIndex, a as blobMask, i as cardinalMask, u as resolveTerrain, e as resolveWang, d as verticesFromCells };
@@ -0,0 +1,45 @@
1
+ import { type MapTileset, type TileAnimationStep, type TileLayer } from './index.js';
2
+ /** Tiles per side of a renderer chunk. */
3
+ export declare const TILE_CHUNK_SIZE = 64;
4
+ /** Tiles in one renderer chunk. */
5
+ export declare const CHUNK_CELLS: number;
6
+ /** Chunks across and down a layer of `width x height` tiles; an empty layer has none. */
7
+ export declare function chunkGrid(width: number, height: number): {
8
+ chunksWide: number;
9
+ chunksHigh: number;
10
+ };
11
+ /** Copies chunk `(chunkX, chunkY)` of a layer into `out` (`CHUNK_CELLS` values, row by row); cells past the layer are 0. */
12
+ export declare function fillChunk(layer: TileLayer, chunkX: number, chunkY: number, out: Uint32Array): void;
13
+ /**
14
+ * Counts how many cells of `values` belong to each tileset in `tilesets` (sorted by `firstGid`) into `counts` at `at`.
15
+ * Empty cells and ids past every tileset count for nothing.
16
+ */
17
+ export declare function countTilesets(values: Uint32Array, tilesets: readonly MapTileset[], counts: Uint32Array, at: number): void;
18
+ /** How far drawn tiles can reach past their cell, in map pixels. */
19
+ export interface TileReach {
20
+ readonly left: number;
21
+ readonly top: number;
22
+ readonly right: number;
23
+ readonly bottom: number;
24
+ }
25
+ /**
26
+ * The reach of a map's tilesets. A tile sits on the bottom left of its cell, so one bigger than the cell reaches up and
27
+ * right, and a diagonal flip swaps its width and height. `tileOffset` moves it further.
28
+ */
29
+ export declare function tileReach(tilesets: readonly MapTileset[], cellWidth: number, cellHeight: number): TileReach;
30
+ /** The chunks a target shows, inclusive; empty when `lastX < firstX` or `lastY < firstY`. */
31
+ export interface ChunkRange {
32
+ firstX: number;
33
+ firstY: number;
34
+ lastX: number;
35
+ lastY: number;
36
+ }
37
+ /**
38
+ * Which chunks can show in a `targetWidth x targetHeight` target when the layer's top left lands at pixel
39
+ * `(originX, originY)` and `zoom` target pixels make one map pixel. Writes to `out` and returns it.
40
+ */
41
+ export declare function visibleChunks(originX: number, originY: number, zoom: number, targetWidth: number, targetHeight: number, cellWidth: number, cellHeight: number, chunksWide: number, chunksHigh: number, reach: TileReach, out?: ChunkRange): ChunkRange;
42
+ /** The local tile an animation shows at `time` milliseconds, looping. An empty animation returns -1. */
43
+ export declare function animatedTile(animation: readonly TileAnimationStep[], time: number): number;
44
+ /** A map colour `#rrggbb` or `#aarrggbb` as `[r, g, b, a]` from 0 to 1, or undefined when it is neither. */
45
+ export declare function mapColor(text: string | undefined): [number, number, number, number] | undefined;
package/dist/fov.d.ts ADDED
@@ -0,0 +1,73 @@
1
+ import { type TileMapData } from './index.js';
2
+ /** Cells that may block sight: `opaque[y * width + x]` truthy blocks it. */
3
+ export interface SightGrid {
4
+ readonly width: number;
5
+ readonly height: number;
6
+ readonly opaque: ArrayLike<number | boolean>;
7
+ /** The map's orientation; staggered and hexagonal maps are refused (their cells are not squares in a grid). */
8
+ readonly orientation?: TileMapData['orientation'];
9
+ }
10
+ /** Settings of {@link fieldOfView}. */
11
+ export interface SightOptions {
12
+ /** Cells whose centre is farther than this from the viewer's centre, in cells, are not seen. Default Infinity. */
13
+ radius?: number;
14
+ /** Marks the seen cells with 1 in this mask (other cells are left as they are), to join several viewers. */
15
+ into?: Uint8Array;
16
+ }
17
+ /** Settings of {@link tileOpacity}. */
18
+ export interface TileOpacityOptions {
19
+ /** Tile layers to read, by name; a cell blocks when any of its tiles does. Default every tile layer. */
20
+ layers?: readonly string[];
21
+ /** Tile property that blocks sight when true. Default `'opaque'`. */
22
+ property?: string;
23
+ }
24
+ /** Which cells block sight, from the tiles' `opaque: true` property (or `options.property`). */
25
+ export declare function tileOpacity(map: TileMapData, options?: TileOpacityOptions): SightGrid;
26
+ /**
27
+ * The cells a viewer at (`x`, `y`) sees, as a mask of `width * height` (1 seen). A cell is seen when a fan of sight
28
+ * lines of more than zero width from the centre of the viewer's cell reaches it without first passing through a
29
+ * blocking cell; blocking cells that are reached are seen too, and the viewer's own cell always is. Cells outside the
30
+ * grid block. Sight is not symmetric: a cell seen from here may not see back.
31
+ */
32
+ export declare function fieldOfView(grid: SightGrid, x: number, y: number, options?: SightOptions): Uint8Array;
33
+ /** A viewer of {@link FogOfWar.update}: its cell and how far it sees, in cells (default Infinity). */
34
+ export interface Viewer {
35
+ readonly x: number;
36
+ readonly y: number;
37
+ readonly radius?: number;
38
+ }
39
+ /** Fog states of {@link FogOfWar.state}. */
40
+ export declare const FOG_UNSEEN = 0, FOG_EXPLORED = 1, FOG_VISIBLE = 2;
41
+ /** RGBA colours of {@link FogOfWar.image}, 0 to 255 each. */
42
+ export interface FogColors {
43
+ /** Default black, opaque. */
44
+ unseen?: readonly [number, number, number, number];
45
+ /** Default black at alpha 128. */
46
+ explored?: readonly [number, number, number, number];
47
+ /** Default clear. */
48
+ visible?: readonly [number, number, number, number];
49
+ }
50
+ /**
51
+ * What the player sees now and what they have seen before: `update` recomputes `visible` from the viewers and adds it
52
+ * to `explored`, which persists (save it, and pass it back to the constructor to restore it).
53
+ */
54
+ export declare class FogOfWar {
55
+ readonly grid: SightGrid;
56
+ readonly width: number;
57
+ readonly height: number;
58
+ /** 1 where a viewer sees the cell after the last `update`. */
59
+ readonly visible: Uint8Array;
60
+ /** 1 where any viewer has ever seen the cell. */
61
+ readonly explored: Uint8Array;
62
+ constructor(grid: SightGrid, explored?: ArrayLike<number>);
63
+ /** Recomputes what the viewers see now (the grid's `opaque` is read again, so opened doors count) and explores it. */
64
+ update(viewers: readonly Viewer[]): this;
65
+ /** {@link FOG_VISIBLE}, {@link FOG_EXPLORED} or {@link FOG_UNSEEN}; cells outside the grid are unseen. */
66
+ state(x: number, y: number): number;
67
+ /** One RGBA pixel per cell, rows from the top, to draw over the map as a fog texture scaled to the map's size. */
68
+ image(colors?: FogColors): {
69
+ width: number;
70
+ height: number;
71
+ data: Uint8Array;
72
+ };
73
+ }
package/dist/fov.js ADDED
@@ -0,0 +1,107 @@
1
+ import { a as e } from "./src-CJvCfCkX.js";
2
+ //#region src/fov.ts
3
+ function t(t, n = {}) {
4
+ let r = n.property ?? "opaque", { width: i, height: a } = t, o = new Uint8Array(i * a), s = /* @__PURE__ */ new Map(), c = (e) => {
5
+ let n = s.get(e);
6
+ if (n !== void 0) return n;
7
+ n = !1;
8
+ for (let i of t.tilesets) e >= i.firstGid && e < i.firstGid + i.tileCount && (n = i.tiles?.find((t) => t.id === e - i.firstGid)?.properties?.[r] === !0);
9
+ return s.set(e, n), n;
10
+ };
11
+ for (let r of t.layers) {
12
+ if (r.type !== "tile" || n.layers && !n.layers.includes(r.name)) continue;
13
+ let t = r;
14
+ if (t.width !== i || t.height !== a) throw RangeError(`layer "${t.name}" is ${t.width} x ${t.height}, the map ${i} x ${a}; every tile layer must match the map size.`);
15
+ for (let n = 0; n < o.length; n++) {
16
+ let r = t.data[n] & e;
17
+ r && c(r) && (o[n] = 1);
18
+ }
19
+ }
20
+ return {
21
+ width: i,
22
+ height: a,
23
+ opaque: o,
24
+ orientation: t.orientation
25
+ };
26
+ }
27
+ var n = (e) => {
28
+ if (!e || !Number.isInteger(e.width) || !Number.isInteger(e.height) || e.width < 1 || e.height < 1) throw RangeError("a SightGrid needs a whole width and height of 1 or more.");
29
+ if (e.opaque?.length !== e.width * e.height) throw RangeError(`a SightGrid of ${e.width} x ${e.height} needs ${e.width * e.height} opaque values.`);
30
+ let t = e.orientation ?? "orthogonal";
31
+ if (t !== "orthogonal" && t !== "isometric") throw RangeError(`field of view needs an orthogonal or isometric grid, got ${t}.`);
32
+ };
33
+ function r(e, t, r, i = {}) {
34
+ n(e);
35
+ let { width: a, height: o, opaque: s } = e, c = i.radius ?? Infinity;
36
+ if (!Number.isInteger(t) || !Number.isInteger(r) || t < 0 || r < 0 || t >= a || r >= o) throw RangeError(`viewer (${t}, ${r}) is outside the ${a} x ${o} grid; pass a cell inside it.`);
37
+ if (!(c >= 0)) throw RangeError(`radius must be 0 or more, got ${c}.`);
38
+ let l = i.into ?? new Uint8Array(a * o);
39
+ if (l.length !== a * o) throw RangeError(`into holds ${l.length} cells, the grid ${a * o}; pass a mask of width * height.`);
40
+ l[r * a + t] = 1;
41
+ let u = c * c, d = Math.min(Math.max(a, o), Math.floor(c));
42
+ for (let e = 0; e < 8; e++) {
43
+ let n = e >= 4, i = e & 1 ? -1 : 1, c = e & 2 ? -1 : 1, f = [0, 1];
44
+ for (let e = 1; e <= d && f.length; e++) for (let d = 0; d <= e && f.length; d++) {
45
+ let p = n ? c * d : i * e, m = n ? i * e : c * d, h = t + p, g = r + m, _ = d === 0 ? -.5 / (e - .5) : (d - .5) / (e + .5), v = (d + .5) / (e - .5), y = !1;
46
+ for (let e = 0; e < f.length && !y; e += 2) y = Math.min(f[e + 1], v) > Math.max(f[e], _);
47
+ if (!y) continue;
48
+ let b = h >= 0 && g >= 0 && h < a && g < o;
49
+ if (b && p * p + m * m <= u && (l[g * a + h] = 1), b && !s[g * a + h]) continue;
50
+ let x = [];
51
+ for (let e = 0; e < f.length; e += 2) {
52
+ let t = f[e], n = f[e + 1];
53
+ n <= _ || t >= v ? x.push(t, n) : (_ > t && x.push(t, _), v < n && x.push(v, n));
54
+ }
55
+ f = x;
56
+ }
57
+ }
58
+ return l;
59
+ }
60
+ var i = 0, a = 1, o = 2, s = class {
61
+ constructor(e, t) {
62
+ if (this.grid = e, n(e), this.width = e.width, this.height = e.height, this.visible = new Uint8Array(e.width * e.height), this.explored = new Uint8Array(e.width * e.height), t) {
63
+ if (t.length !== this.explored.length) throw RangeError(`explored holds ${t.length} cells, the grid ${this.explored.length}; pass the explored mask saved from this grid.`);
64
+ for (let e = 0; e < t.length; e++) this.explored[e] = +!!t[e];
65
+ }
66
+ }
67
+ update(e) {
68
+ this.visible.fill(0);
69
+ for (let t of e) r(this.grid, t.x, t.y, {
70
+ radius: t.radius,
71
+ into: this.visible
72
+ });
73
+ for (let e = 0; e < this.visible.length; e++) this.visible[e] && (this.explored[e] = 1);
74
+ return this;
75
+ }
76
+ state(e, t) {
77
+ if (!(e >= 0 && t >= 0 && e < this.width && t < this.height)) return 0;
78
+ let n = Math.floor(t) * this.width + Math.floor(e);
79
+ return this.visible[n] ? 2 : +!!this.explored[n];
80
+ }
81
+ image(e = {}) {
82
+ let t = e.unseen ?? [
83
+ 0,
84
+ 0,
85
+ 0,
86
+ 255
87
+ ], n = e.explored ?? [
88
+ 0,
89
+ 0,
90
+ 0,
91
+ 128
92
+ ], r = e.visible ?? [
93
+ 0,
94
+ 0,
95
+ 0,
96
+ 0
97
+ ], i = new Uint8Array(this.width * this.height * 4);
98
+ for (let e = 0; e < this.visible.length; e++) i.set(this.visible[e] ? r : this.explored[e] ? n : t, e * 4);
99
+ return {
100
+ width: this.width,
101
+ height: this.height,
102
+ data: i
103
+ };
104
+ }
105
+ };
106
+ //#endregion
107
+ export { a as FOG_EXPLORED, i as FOG_UNSEEN, o as FOG_VISIBLE, s as FogOfWar, r as fieldOfView, t as tileOpacity };
@@ -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/hex.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { type MapGrid } from './layout.js';
2
+ export { cellBox, cellCenter, cellNeighbors, cellPolygon, mapPixelSize, paintOrder, pixelToCell, type CellBox, type MapGrid, } from './layout.js';
3
+ /** Cube coordinates of a hex cell: `q + r + s = 0`. */
4
+ export type Cube = readonly [q: number, r: number, s: number];
5
+ /** The cube coordinates of map cell (`x`, `y`). */
6
+ export declare function cellToCube(map: MapGrid, x: number, y: number): Cube;
7
+ /** The map cell of cube coordinates `cube`. */
8
+ export declare function cubeToCell(map: MapGrid, [q, r]: Cube): [number, number];
9
+ /** Steps between two cells over shared edges (hexagonal maps) or shared edges and corners (staggered maps' rows). */
10
+ export declare function hexDistance(map: MapGrid, a: readonly [number, number], b: readonly [number, number]): number;
11
+ /** The nearest cube to fractional cube coordinates: round all three, then fix the one that moved most. */
12
+ export declare function roundCube(q: number, r: number, s: number): Cube;
13
+ /**
14
+ * The cells on the straight line from `a` to `b`, both included, one per step: the cube points at equal fractions of
15
+ * the way, nudged by a millionth so a line along an edge always picks the same side, rounded to cells.
16
+ */
17
+ export declare function hexLine(map: MapGrid, a: readonly [number, number], b: readonly [number, number]): [number, number][];
18
+ /** The cells exactly `radius` steps from (`x`, `y`), walking the ring from one corner; the cell itself for radius 0. */
19
+ export declare function hexRing(map: MapGrid, x: number, y: number, radius: number): [number, number][];
20
+ /** True when two cells share an edge: a {@link cellNeighbors} pair. */
21
+ export declare function hexAdjacent(map: MapGrid, a: readonly [number, number], b: readonly [number, number]): boolean;