@margoengine/tilemap 0.0.0-stage → 0.1.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,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 };
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;
package/dist/hex.js ADDED
@@ -0,0 +1,101 @@
1
+ import { a as e, i as t, n, o as r, r as i, s as a, t as o } from "./layout-CLx3KdGC.js";
2
+ //#region src/hex.ts
3
+ var s = (e) => {
4
+ if (e.orientation !== "hexagonal" && e.orientation !== "staggered") throw RangeError(`hex cells need a hexagonal or staggered map, got "${e.orientation ?? "orthogonal"}".`);
5
+ return {
6
+ y: (e.staggerAxis ?? "y") === "y",
7
+ odd: (e.staggerIndex ?? "odd") === "odd"
8
+ };
9
+ };
10
+ function c(e, t, n) {
11
+ let { y: r, odd: i } = s(e);
12
+ if (r) {
13
+ let e = t - (i ? (n - (n & 1)) / 2 : (n + (n & 1)) / 2);
14
+ return [
15
+ e,
16
+ n,
17
+ -e - n
18
+ ];
19
+ }
20
+ let a = n - (i ? (t - (t & 1)) / 2 : (t + (t & 1)) / 2);
21
+ return [
22
+ t,
23
+ a,
24
+ -t - a
25
+ ];
26
+ }
27
+ function l(e, [t, n]) {
28
+ let { y: r, odd: i } = s(e);
29
+ return r ? [t + (i ? (n - (n & 1)) / 2 : (n + (n & 1)) / 2), n] : [t, n + (i ? (t - (t & 1)) / 2 : (t + (t & 1)) / 2)];
30
+ }
31
+ function u(e, t, n) {
32
+ let [r, i, a] = c(e, t[0], t[1]), [o, s, l] = c(e, n[0], n[1]);
33
+ return Math.max(Math.abs(r - o), Math.abs(i - s), Math.abs(a - l));
34
+ }
35
+ function d(e, t, n) {
36
+ let r = Math.round(e), i = Math.round(t), a = Math.round(n), o = Math.abs(r - e), s = Math.abs(i - t), c = Math.abs(a - n);
37
+ return o > s && o > c ? r = -i - a : s > c ? i = -r - a : a = -r - i, [
38
+ r + 0,
39
+ i + 0,
40
+ a + 0
41
+ ];
42
+ }
43
+ function f(e, t, n) {
44
+ let r = u(e, t, n), [i, a, o] = c(e, t[0], t[1]), [s, f, p] = c(e, n[0], n[1]), m = [];
45
+ for (let t = 0; t <= r; t++) {
46
+ let n = r ? t / r : 0;
47
+ m.push(l(e, d(i + (s - i) * n + 1e-6, a + (f - a) * n + 1e-6, o + (p - o) * n - 2e-6)));
48
+ }
49
+ return m;
50
+ }
51
+ function p(e, t, n, r) {
52
+ if (!Number.isInteger(r) || r < 0) throw RangeError(`radius must be a whole number of 0 or more, got ${r}.`);
53
+ if (!r) return [[t, n]];
54
+ let [i, a, o] = c(e, t, n), s = [
55
+ [
56
+ 1,
57
+ -1,
58
+ 0
59
+ ],
60
+ [
61
+ 1,
62
+ 0,
63
+ -1
64
+ ],
65
+ [
66
+ 0,
67
+ 1,
68
+ -1
69
+ ],
70
+ [
71
+ -1,
72
+ 1,
73
+ 0
74
+ ],
75
+ [
76
+ -1,
77
+ 0,
78
+ 1
79
+ ],
80
+ [
81
+ 0,
82
+ -1,
83
+ 1
84
+ ]
85
+ ], u = [], d = [
86
+ i + s[4][0] * r,
87
+ a + s[4][1] * r,
88
+ o + s[4][2] * r
89
+ ];
90
+ for (let t of s) for (let n = 0; n < r; n++) u.push(l(e, d)), d = [
91
+ d[0] + t[0],
92
+ d[1] + t[1],
93
+ d[2] + t[2]
94
+ ];
95
+ return u;
96
+ }
97
+ function m(e, t, n) {
98
+ return i(e, t[0], t[1]).some(([e, t]) => e === n[0] && t === n[1]);
99
+ }
100
+ //#endregion
101
+ export { o as cellBox, n as cellCenter, i as cellNeighbors, t as cellPolygon, c as cellToCube, l as cubeToCell, m as hexAdjacent, u as hexDistance, f as hexLine, p as hexRing, e as mapPixelSize, r as paintOrder, a as pixelToCell, d as roundCube };
@@ -0,0 +1,33 @@
1
+ import { a as e, r as t } from "./src-CJvCfCkX.js";
2
+ //#region src/import.ts
3
+ var n = class extends Error {
4
+ constructor(e, t, n) {
5
+ super(`${e}: ${n}`), this.name = "MapImportError", this.format = e, this.code = t;
6
+ }
7
+ }, r = 1 << 24, i = (e) => typeof e == "object" && !!e && !Array.isArray(e), a = (e, t, n = 0) => {
8
+ let r = e[t];
9
+ return typeof r == "number" && Number.isFinite(r) ? r : n;
10
+ }, o = (e, t) => typeof e[t] == "string" ? e[t] : void 0, s = (e, t) => Array.isArray(e[t]) ? e[t] : [], c = (n, r = !1) => (n & (e | t | (r ? 268435456 : 0))) >>> 0;
11
+ function l(e, t, n) {
12
+ Object.defineProperty(e, t, {
13
+ value: n,
14
+ enumerable: !0,
15
+ writable: !0,
16
+ configurable: !0
17
+ });
18
+ }
19
+ function u(e, t) {
20
+ let r = t;
21
+ if (t instanceof Uint8Array && (r = new TextDecoder().decode(t)), typeof r == "string") try {
22
+ r = JSON.parse(r);
23
+ } catch {
24
+ throw new n(e, "syntax", "the text is not valid JSON; pass the file contents as saved by the editor.");
25
+ }
26
+ if (!i(r)) throw new n(e, "syntax", "expected a JSON object at the top level.");
27
+ return r;
28
+ }
29
+ function d(e, t) {
30
+ if (t?.aborted) throw new n(e, "aborted", "the import was aborted by its signal.");
31
+ }
32
+ //#endregion
33
+ export { s as a, l as c, i, o as l, n, a as o, c as r, u as s, r as t, d as u };
@@ -0,0 +1,29 @@
1
+ import { type PropertyValue, type Properties } from './index.js';
2
+ /** What went wrong in an import: bad text, a missing piece, something this importer does not read, a size limit. */
3
+ export type MapImportErrorCode = 'syntax' | 'truncated' | 'missing' | 'unsupported' | 'limit' | 'aborted';
4
+ /** Thrown by the Tiled and LDtk importers for malformed, truncated, oversized or unsupported input. The message names the fix. */
5
+ export declare class MapImportError extends Error {
6
+ readonly format: string;
7
+ readonly code: MapImportErrorCode;
8
+ constructor(format: string, code: MapImportErrorCode, message: string);
9
+ }
10
+ /** Cells per tile layer (4096 x 4096). */
11
+ export declare const MAX_TILES: number;
12
+ /** A parsed JSON object. */
13
+ export type Obj = {
14
+ readonly [key: string]: unknown;
15
+ };
16
+ export declare const isObj: (v: unknown) => v is Obj;
17
+ export declare const num: (o: Obj, key: string, fallback?: number) => number;
18
+ export declare const str: (o: Obj, key: string) => string | undefined;
19
+ export declare const list: (o: Obj, key: string) => readonly unknown[];
20
+ /** Keep the id bits and the three flip bits; drop Tiled's hex rotation bit unless `hex` (a hexagonal map). */
21
+ export declare const cleanGid: (value: number, hex?: boolean) => number;
22
+ /** Set an own property by name even when the name is `__proto__`. */
23
+ export declare function setOwn<T>(target: {
24
+ [key: string]: T;
25
+ }, key: string, value: T): void;
26
+ /** Parse JSON text, or pass an object through. */
27
+ export declare function parseJson(format: string, input: unknown): Obj;
28
+ export declare function throwIfAborted(format: string, signal?: AbortSignal): void;
29
+ export type { PropertyValue, Properties };