@n3rdw1z4rd/core 0.18.5 → 0.19.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.
Files changed (109) hide show
  1. package/README.md +72 -10
  2. package/dist/astar.d.ts +44 -0
  3. package/dist/astar.js +118 -0
  4. package/dist/canvas-renderer.d.ts +154 -0
  5. package/dist/canvas-renderer.js +200 -0
  6. package/dist/clock.d.ts +69 -0
  7. package/dist/clock.js +108 -0
  8. package/dist/color-palette.d.ts +25 -0
  9. package/dist/color-palette.js +112 -0
  10. package/dist/color.d.ts +90 -0
  11. package/dist/color.js +224 -0
  12. package/dist/css/main.css +1 -3
  13. package/dist/disposable.d.ts +27 -0
  14. package/dist/disposable.js +32 -0
  15. package/dist/dungeon-generator.d.ts +142 -0
  16. package/dist/dungeon-generator.js +288 -0
  17. package/dist/ecs.d.ts +87 -0
  18. package/dist/ecs.js +101 -0
  19. package/dist/emitter.d.ts +40 -0
  20. package/dist/emitter.js +54 -0
  21. package/dist/event-queue.d.ts +29 -0
  22. package/dist/event-queue.js +40 -0
  23. package/dist/index.cjs +3813 -0
  24. package/dist/index.d.cts +2655 -0
  25. package/dist/index.d.ts +35 -873
  26. package/dist/index.js +34 -3981
  27. package/dist/input/action-map.d.ts +79 -0
  28. package/dist/input/action-map.js +131 -0
  29. package/dist/input/bindings.d.ts +273 -0
  30. package/dist/input/bindings.js +250 -0
  31. package/dist/input/gestures.d.ts +108 -0
  32. package/dist/input/gestures.js +159 -0
  33. package/dist/input/index.d.ts +5 -0
  34. package/dist/input/index.js +5 -0
  35. package/dist/input/input.d.ts +297 -0
  36. package/dist/input/input.js +695 -0
  37. package/dist/log.d.ts +8 -0
  38. package/dist/log.js +9 -0
  39. package/dist/map2d.d.ts +83 -0
  40. package/dist/map2d.js +120 -0
  41. package/dist/map3d-CHDJiq5T.d.cts +236 -0
  42. package/dist/map3d.d.ts +94 -0
  43. package/dist/map3d.js +148 -0
  44. package/dist/math.d.ts +176 -0
  45. package/dist/math.js +206 -0
  46. package/dist/misc.d.ts +46 -0
  47. package/dist/misc.js +71 -0
  48. package/dist/observable.d.ts +43 -0
  49. package/dist/observable.js +53 -0
  50. package/dist/particle-renderer.d.ts +47 -0
  51. package/dist/particle-renderer.js +82 -0
  52. package/dist/particle-system-2d.d.ts +60 -0
  53. package/dist/particle-system-2d.js +147 -0
  54. package/dist/random.d.ts +46 -0
  55. package/dist/random.js +74 -0
  56. package/dist/rectangle.d.ts +44 -0
  57. package/dist/rectangle.js +69 -0
  58. package/dist/rng.d.ts +72 -0
  59. package/dist/rng.js +152 -0
  60. package/dist/signal.d.ts +34 -0
  61. package/dist/signal.js +41 -0
  62. package/dist/spatial-partition-2d.d.ts +42 -0
  63. package/dist/spatial-partition-2d.js +52 -0
  64. package/dist/spatial-partition-3d-Ba-VBveM.cjs +1324 -0
  65. package/dist/spatial-partition-3d.d.ts +44 -0
  66. package/dist/spatial-partition-3d.js +57 -0
  67. package/dist/three/asteroid-mesh.d.ts +41 -0
  68. package/dist/three/asteroid-mesh.js +44 -0
  69. package/dist/three/camera-rig.d.ts +140 -0
  70. package/dist/three/camera-rig.js +197 -0
  71. package/dist/three/cube-sphere-geometry.d.ts +55 -0
  72. package/dist/three/cube-sphere-geometry.js +239 -0
  73. package/dist/three/ecs.d.ts +75 -0
  74. package/dist/three/ecs.js +64 -0
  75. package/dist/three/index.d.ts +12 -0
  76. package/dist/three/index.js +12 -0
  77. package/dist/three/particle-system-3d.d.ts +53 -0
  78. package/dist/three/particle-system-3d.js +224 -0
  79. package/dist/three/renderer.d.ts +38 -0
  80. package/dist/three/renderer.js +57 -0
  81. package/dist/three/starfield.d.ts +23 -0
  82. package/dist/three/starfield.js +64 -0
  83. package/dist/three/texture-atlas.d.ts +76 -0
  84. package/dist/three/texture-atlas.js +84 -0
  85. package/dist/three/texture-utils.d.ts +31 -0
  86. package/dist/three/texture-utils.js +44 -0
  87. package/dist/three/voxel-geometry.d.ts +72 -0
  88. package/dist/three/voxel-geometry.js +231 -0
  89. package/dist/three/voxel-mesh.d.ts +67 -0
  90. package/dist/three/voxel-mesh.js +71 -0
  91. package/dist/three.cjs +1307 -0
  92. package/dist/three.d.cts +655 -0
  93. package/dist/tilemap.d.ts +17 -0
  94. package/dist/tilemap.js +26 -0
  95. package/dist/types.d.ts +34 -0
  96. package/dist/types.js +0 -0
  97. package/dist/vector.d.ts +78 -0
  98. package/dist/vector.js +169 -0
  99. package/dist/webgl-renderer.d.ts +89 -0
  100. package/dist/webgl-renderer.js +140 -0
  101. package/dist/webgl.d.ts +357 -0
  102. package/dist/webgl.js +628 -0
  103. package/dist/worker-interface.d.ts +30 -0
  104. package/dist/worker-interface.js +39 -0
  105. package/package.json +55 -13
  106. package/dist/css/index.ts +0 -1
  107. package/dist/index.css +0 -104
  108. package/dist/index.d.mts +0 -873
  109. package/dist/index.mjs +0 -3977
@@ -0,0 +1,83 @@
1
+ //#region src/map2d.d.ts
2
+ /**
3
+ * Default "empty" value of a {@link Map2D}: cells holding it are not stored.
4
+ *
5
+ * @category Maps & Spatial
6
+ */
7
+ export declare const DEFAULT_MAP_2D_VALUE = -1;
8
+ /**
9
+ * Lowest supported coordinate on each axis of a {@link Map2D} (inclusive, -1,048,576); anything
10
+ * outside the range wraps around and collides with other coordinates.
11
+ *
12
+ * @category Maps & Spatial
13
+ */
14
+ export declare const MAP_2D_MIN_COORD: number;
15
+ /**
16
+ * Highest supported coordinate on each axis of a {@link Map2D} (inclusive, 1,048,575).
17
+ *
18
+ * @category Maps & Spatial
19
+ */
20
+ export declare const MAP_2D_MAX_COORD: number;
21
+ /**
22
+ * Backing store of {@link Map2D} and {@link Map3D}: packed integer coordinate key -> cell value.
23
+ *
24
+ * @category Maps & Spatial
25
+ */
26
+ export type Map2DData = Map<number, number>;
27
+ /**
28
+ * Sparse 2D grid of numbers keyed by integer coordinates. Coordinates are floored, so any point
29
+ * inside a cell addresses that cell. Cells holding `defaultValue` are not stored: setting a cell to
30
+ * it removes the cell, and reading a missing cell returns it.
31
+ *
32
+ * Supports coordinates in [{@link MAP_2D_MIN_COORD}, {@link MAP_2D_MAX_COORD}] per axis; outside
33
+ * that range keys wrap around (a one-time `console.warn` per map reports it).
34
+ *
35
+ * ```ts
36
+ * const map = new Map2D(); // empty = -1
37
+ * map.set(3, -2, 7);
38
+ * map.get(3.9, -1.5); // 7 (floored to 3, -2)
39
+ * map.get(0, 0); // -1
40
+ * map.set(3, -2); // back to empty: removed
41
+ * ```
42
+ *
43
+ * @category Maps & Spatial
44
+ */
45
+ export declare class Map2D {
46
+ /** value of every cell that hasn't been set; default {@link DEFAULT_MAP_2D_VALUE} */
47
+ readonly defaultValue: number;
48
+ private _data;
49
+ private _warnedOutOfRange;
50
+ /** the backing map (packed keys), shared, not a copy */
51
+ get data(): Map2DData;
52
+ /** number of stored (non-default) cells */
53
+ get size(): number;
54
+ /**
55
+ * @param mapData existing backing data to use as is (not copied), e.g. another map's `data`
56
+ */
57
+ constructor(defaultValue?: number, mapData?: Map2DData);
58
+ private _floor;
59
+ private _key;
60
+ private _pos;
61
+ /** Removes every stored cell. */
62
+ clear(): void;
63
+ /**
64
+ * Value of the cell containing (x, y) (coordinates are floored).
65
+ *
66
+ * @param defaultValue returned when the cell isn't stored; defaults to the map's `defaultValue`
67
+ */
68
+ get(x: number, y: number, defaultValue?: number): number;
69
+ /** Sets the cell containing (x, y); leave out `value` (or pass `defaultValue`) to remove it. */
70
+ set(x: number, y: number, value?: number): void;
71
+ /**
72
+ * Calls `callback` for every stored cell, in insertion order, with its coordinates, value `n`
73
+ * and running index `i`. Cells holding the default value are not visited.
74
+ */
75
+ forEach(callback: (x: number, y: number, n: number, i: number) => void): void;
76
+ /**
77
+ * Like {@link Map2D.forEach}, limited to stored cells with `x1 <= x <= x2` and `y1 <= y <= y2`
78
+ * (both corners inclusive, as in {@link Map3D.setArea}). Scans every stored cell, so it costs
79
+ * O(size), not O(area).
80
+ */
81
+ forEachInArea(x1: number, y1: number, x2: number, y2: number, callback: (x: number, y: number, n: number) => void): void;
82
+ }
83
+ //#endregion
package/dist/map2d.js ADDED
@@ -0,0 +1,120 @@
1
+ //#region src/map2d.ts
2
+ /**
3
+ * Default "empty" value of a {@link Map2D}: cells holding it are not stored.
4
+ *
5
+ * @category Maps & Spatial
6
+ */
7
+ const DEFAULT_MAP_2D_VALUE = -1;
8
+ const BASE_2D = 2097152;
9
+ const OFFSET_2D = 1048576;
10
+ /**
11
+ * Lowest supported coordinate on each axis of a {@link Map2D} (inclusive, -1,048,576); anything
12
+ * outside the range wraps around and collides with other coordinates.
13
+ *
14
+ * @category Maps & Spatial
15
+ */
16
+ const MAP_2D_MIN_COORD = -1048576;
17
+ /**
18
+ * Highest supported coordinate on each axis of a {@link Map2D} (inclusive, 1,048,575).
19
+ *
20
+ * @category Maps & Spatial
21
+ */
22
+ const MAP_2D_MAX_COORD = 1048575;
23
+ /**
24
+ * Sparse 2D grid of numbers keyed by integer coordinates. Coordinates are floored, so any point
25
+ * inside a cell addresses that cell. Cells holding `defaultValue` are not stored: setting a cell to
26
+ * it removes the cell, and reading a missing cell returns it.
27
+ *
28
+ * Supports coordinates in [{@link MAP_2D_MIN_COORD}, {@link MAP_2D_MAX_COORD}] per axis; outside
29
+ * that range keys wrap around (a one-time `console.warn` per map reports it).
30
+ *
31
+ * ```ts
32
+ * const map = new Map2D(); // empty = -1
33
+ * map.set(3, -2, 7);
34
+ * map.get(3.9, -1.5); // 7 (floored to 3, -2)
35
+ * map.get(0, 0); // -1
36
+ * map.set(3, -2); // back to empty: removed
37
+ * ```
38
+ *
39
+ * @category Maps & Spatial
40
+ */
41
+ var Map2D = class {
42
+ /** value of every cell that hasn't been set; default {@link DEFAULT_MAP_2D_VALUE} */
43
+ defaultValue;
44
+ _data;
45
+ _warnedOutOfRange = false;
46
+ /** the backing map (packed keys), shared, not a copy */
47
+ get data() {
48
+ return this._data;
49
+ }
50
+ /** number of stored (non-default) cells */
51
+ get size() {
52
+ return this._data.size;
53
+ }
54
+ /**
55
+ * @param mapData existing backing data to use as is (not copied), e.g. another map's `data`
56
+ */
57
+ constructor(defaultValue = -1, mapData) {
58
+ this.defaultValue = defaultValue;
59
+ this._data = mapData ?? /* @__PURE__ */ new Map();
60
+ }
61
+ _floor(x, y) {
62
+ return [Math.floor(x), Math.floor(y)];
63
+ }
64
+ _key(x, y) {
65
+ if (!this._warnedOutOfRange && (x < -1048576 || x > 1048575 || y < -1048576 || y > 1048575)) {
66
+ this._warnedOutOfRange = true;
67
+ console.warn(`Map2D: (${x}, ${y}) is outside the supported range [${MAP_2D_MIN_COORD}, ${MAP_2D_MAX_COORD}] per axis; it wraps around and collides with other coordinates. Further warnings for this map are suppressed.`);
68
+ }
69
+ const X = x + OFFSET_2D & 2097151;
70
+ const Y = y + OFFSET_2D & 2097151;
71
+ return X * BASE_2D + Y;
72
+ }
73
+ _pos(key) {
74
+ const Y = key % BASE_2D;
75
+ return [Math.floor(key / BASE_2D) - OFFSET_2D, Y - OFFSET_2D];
76
+ }
77
+ /** Removes every stored cell. */
78
+ clear() {
79
+ this._data.clear();
80
+ }
81
+ /**
82
+ * Value of the cell containing (x, y) (coordinates are floored).
83
+ *
84
+ * @param defaultValue returned when the cell isn't stored; defaults to the map's `defaultValue`
85
+ */
86
+ get(x, y, defaultValue = this.defaultValue) {
87
+ [x, y] = this._floor(x, y);
88
+ return this._data.get(this._key(x, y)) ?? defaultValue;
89
+ }
90
+ /** Sets the cell containing (x, y); leave out `value` (or pass `defaultValue`) to remove it. */
91
+ set(x, y, value = this.defaultValue) {
92
+ [x, y] = this._floor(x, y);
93
+ const key = this._key(x, y);
94
+ if (value === this.defaultValue) this._data.delete(key);
95
+ else this._data.set(key, value);
96
+ }
97
+ /**
98
+ * Calls `callback` for every stored cell, in insertion order, with its coordinates, value `n`
99
+ * and running index `i`. Cells holding the default value are not visited.
100
+ */
101
+ forEach(callback) {
102
+ let i = 0;
103
+ this._data.forEach((v, key) => {
104
+ const [x, y] = this._pos(key);
105
+ callback(x, y, v, i++);
106
+ });
107
+ }
108
+ /**
109
+ * Like {@link Map2D.forEach}, limited to stored cells with `x1 <= x <= x2` and `y1 <= y <= y2`
110
+ * (both corners inclusive, as in {@link Map3D.setArea}). Scans every stored cell, so it costs
111
+ * O(size), not O(area).
112
+ */
113
+ forEachInArea(x1, y1, x2, y2, callback) {
114
+ this.forEach((x, y, n) => {
115
+ if (x >= x1 && x <= x2 && y >= y1 && y <= y2) callback(x, y, n);
116
+ });
117
+ }
118
+ };
119
+ //#endregion
120
+ export { DEFAULT_MAP_2D_VALUE, MAP_2D_MAX_COORD, MAP_2D_MIN_COORD, Map2D };
@@ -0,0 +1,236 @@
1
+ //#region src/types.d.ts
2
+ /**
3
+ * Any object with numeric `x` and `y`, such as a {@link Vector}.
4
+ * @property x horizontal coordinate
5
+ * @property y vertical coordinate
6
+ * @category Math & Random
7
+ */
8
+ interface XY {
9
+ x: number;
10
+ y: number;
11
+ }
12
+ /**
13
+ * Any object with numeric `x`, `y` and `z`.
14
+ * @property x x coordinate
15
+ * @property y y coordinate
16
+ * @property z z coordinate
17
+ * @category Math & Random
18
+ */
19
+ interface XYZ {
20
+ x: number;
21
+ y: number;
22
+ z: number;
23
+ }
24
+ /**
25
+ * Plain object with string keys and values of any type.
26
+ * @category Math & Random
27
+ */
28
+ type KeyValue = Record<string, any>;
29
+ /**
30
+ * matches simplex-noise's NoiseFunction3D, e.g. createNoise3D(() => rng.nextFloat())
31
+ * @category Math & Random
32
+ */
33
+ type NoiseFunction3D = (x: number, y: number, z: number) => number;
34
+ //#endregion
35
+ //#region src/color-palette.d.ts
36
+ /**
37
+ * Color as `[r, g, b]`, each 0-255.
38
+ * @category Color
39
+ */
40
+ type RGB = readonly [number, number, number];
41
+ /**
42
+ * Flat list of colors, each packed as a `0xRRGGBB` integer, as returned by {@link buildColorPalette}.
43
+ * @category Color
44
+ */
45
+ type ColorPalette = readonly number[];
46
+ /**
47
+ * Builds a flat color palette of packed `0xRRGGBB` integers: a grayscale ramp (black -> white)
48
+ * followed by a light -> dark ramp for each supplied color. Length is
49
+ * `grayscaleSteps + colors.length * colorSteps`.
50
+ *
51
+ * @param grayscaleSteps number of grayscale entries, black -> white (a single step is black)
52
+ * @param colorSteps number of steps per color, from the full color down to 28% of it (never black)
53
+ * @param colors base RGB colors to ramp; defaults to 14 common colors (red, orange, yellow,
54
+ * green, cyan, blue, purple, magenta, pink, brown, tan, olive, teal, maroon).
55
+ * White is always included via the grayscale ramp.
56
+ * @category Color
57
+ */
58
+ declare function buildColorPalette(grayscaleSteps?: number, colorSteps?: number, ...colors: RGB[]): ColorPalette;
59
+ //#endregion
60
+ //#region src/map2d.d.ts
61
+ /**
62
+ * Default "empty" value of a {@link Map2D}: cells holding it are not stored.
63
+ *
64
+ * @category Maps & Spatial
65
+ */
66
+ declare const DEFAULT_MAP_2D_VALUE = -1;
67
+ /**
68
+ * Lowest supported coordinate on each axis of a {@link Map2D} (inclusive, -1,048,576); anything
69
+ * outside the range wraps around and collides with other coordinates.
70
+ *
71
+ * @category Maps & Spatial
72
+ */
73
+ declare const MAP_2D_MIN_COORD: number;
74
+ /**
75
+ * Highest supported coordinate on each axis of a {@link Map2D} (inclusive, 1,048,575).
76
+ *
77
+ * @category Maps & Spatial
78
+ */
79
+ declare const MAP_2D_MAX_COORD: number;
80
+ /**
81
+ * Backing store of {@link Map2D} and {@link Map3D}: packed integer coordinate key -> cell value.
82
+ *
83
+ * @category Maps & Spatial
84
+ */
85
+ type Map2DData = Map<number, number>;
86
+ /**
87
+ * Sparse 2D grid of numbers keyed by integer coordinates. Coordinates are floored, so any point
88
+ * inside a cell addresses that cell. Cells holding `defaultValue` are not stored: setting a cell to
89
+ * it removes the cell, and reading a missing cell returns it.
90
+ *
91
+ * Supports coordinates in [{@link MAP_2D_MIN_COORD}, {@link MAP_2D_MAX_COORD}] per axis; outside
92
+ * that range keys wrap around (a one-time `console.warn` per map reports it).
93
+ *
94
+ * ```ts
95
+ * const map = new Map2D(); // empty = -1
96
+ * map.set(3, -2, 7);
97
+ * map.get(3.9, -1.5); // 7 (floored to 3, -2)
98
+ * map.get(0, 0); // -1
99
+ * map.set(3, -2); // back to empty: removed
100
+ * ```
101
+ *
102
+ * @category Maps & Spatial
103
+ */
104
+ declare class Map2D {
105
+ /** value of every cell that hasn't been set; default {@link DEFAULT_MAP_2D_VALUE} */
106
+ readonly defaultValue: number;
107
+ private _data;
108
+ private _warnedOutOfRange;
109
+ /** the backing map (packed keys), shared, not a copy */
110
+ get data(): Map2DData;
111
+ /** number of stored (non-default) cells */
112
+ get size(): number;
113
+ /**
114
+ * @param mapData existing backing data to use as is (not copied), e.g. another map's `data`
115
+ */
116
+ constructor(defaultValue?: number, mapData?: Map2DData);
117
+ private _floor;
118
+ private _key;
119
+ private _pos;
120
+ /** Removes every stored cell. */
121
+ clear(): void;
122
+ /**
123
+ * Value of the cell containing (x, y) (coordinates are floored).
124
+ *
125
+ * @param defaultValue returned when the cell isn't stored; defaults to the map's `defaultValue`
126
+ */
127
+ get(x: number, y: number, defaultValue?: number): number;
128
+ /** Sets the cell containing (x, y); leave out `value` (or pass `defaultValue`) to remove it. */
129
+ set(x: number, y: number, value?: number): void;
130
+ /**
131
+ * Calls `callback` for every stored cell, in insertion order, with its coordinates, value `n`
132
+ * and running index `i`. Cells holding the default value are not visited.
133
+ */
134
+ forEach(callback: (x: number, y: number, n: number, i: number) => void): void;
135
+ /**
136
+ * Like {@link Map2D.forEach}, limited to stored cells with `x1 <= x <= x2` and `y1 <= y <= y2`
137
+ * (both corners inclusive, as in {@link Map3D.setArea}). Scans every stored cell, so it costs
138
+ * O(size), not O(area).
139
+ */
140
+ forEachInArea(x1: number, y1: number, x2: number, y2: number, callback: (x: number, y: number, n: number) => void): void;
141
+ }
142
+ //#endregion
143
+ //#region src/map3d.d.ts
144
+ /**
145
+ * Default "empty" value of a {@link Map3D}: cells holding it are not stored.
146
+ *
147
+ * @category Maps & Spatial
148
+ */
149
+ declare const DEFAULT_MAP_3D_VALUE = -1;
150
+ /**
151
+ * Lowest supported coordinate on each axis of a {@link Map3D} (inclusive, -65,536); anything
152
+ * outside the range wraps around and collides with other coordinates.
153
+ *
154
+ * @category Maps & Spatial
155
+ */
156
+ declare const MAP_3D_MIN_COORD: number;
157
+ /**
158
+ * Highest supported coordinate on each axis of a {@link Map3D} (inclusive, 65,535).
159
+ *
160
+ * @category Maps & Spatial
161
+ */
162
+ declare const MAP_3D_MAX_COORD: number;
163
+ /**
164
+ * One stored cell, as in {@link Map3D.export} and {@link Map3D.import}.
165
+ *
166
+ * @category Maps & Spatial
167
+ */
168
+ interface Map3DCell {
169
+ /** cell x */
170
+ x: number;
171
+ /** cell y */
172
+ y: number;
173
+ /** cell z */
174
+ z: number;
175
+ /** the cell's value */
176
+ n: number;
177
+ }
178
+ /**
179
+ * Sparse 3D grid of numbers keyed by integer coordinates, e.g. voxel data. Coordinates are floored,
180
+ * so any point inside a cell addresses that cell. Cells holding `defaultValue` are not stored:
181
+ * setting a cell to it removes the cell, and reading a missing cell returns it.
182
+ *
183
+ * Supports coordinates in [{@link MAP_3D_MIN_COORD}, {@link MAP_3D_MAX_COORD}] per axis; outside
184
+ * that range keys wrap around (a one-time `console.warn` per map reports it).
185
+ *
186
+ * ```ts
187
+ * const voxels = new Map3D(); // empty = -1
188
+ * voxels.setArea(0, 0, 0, 3, 0, 3, 1); // 4x1x4 floor (bounds inclusive)
189
+ * voxels.get(2, 0, 2); // 1
190
+ * const saved = voxels.export(); // [{ x, y, z, n }, ...]
191
+ * ```
192
+ *
193
+ * @category Maps & Spatial
194
+ */
195
+ declare class Map3D {
196
+ /** value of every cell that hasn't been set; default {@link DEFAULT_MAP_3D_VALUE} */
197
+ readonly defaultValue: number;
198
+ private _data;
199
+ private _warnedOutOfRange;
200
+ /** the backing map (packed keys), shared, not a copy */
201
+ get data(): Map2DData;
202
+ /** number of stored (non-default) cells */
203
+ get size(): number;
204
+ /**
205
+ * @param mapData existing backing data to use as is (not copied), e.g. another map's `data`
206
+ */
207
+ constructor(defaultValue?: number, mapData?: Map2DData);
208
+ private _floor;
209
+ private _key;
210
+ private _pos;
211
+ /** Removes every stored cell. */
212
+ clear(): void;
213
+ /**
214
+ * Value of the cell containing (x, y, z) (coordinates are floored).
215
+ *
216
+ * @param defaultValue returned when the cell isn't stored; defaults to the map's `defaultValue`
217
+ */
218
+ get(x: number, y: number, z: number, defaultValue?: number): number;
219
+ /** Sets the cell containing (x, y, z) to `n`; leave out `n` (or pass `defaultValue`) to remove it. */
220
+ set(x: number, y: number, z: number, n?: number): void;
221
+ /**
222
+ * Sets every cell in the box from (x1, y1, z1) to (x2, y2, z2) to `n`; both corners are
223
+ * inclusive and must be the min / max corner (nothing is set if x2 < x1, etc.).
224
+ */
225
+ setArea(x1: number, y1: number, z1: number, x2: number, y2: number, z2: number, n: number): void;
226
+ /** Calls `callback` for every stored cell, in insertion order. Cells holding the default value are not visited. */
227
+ forEach(callback: (x: number, y: number, z: number, n: number) => void): void;
228
+ /** Stored cells as a plain array (e.g. for JSON); the inverse of {@link Map3D.import}. */
229
+ export(): Map3DCell[];
230
+ /** Replaces the map's contents with cells from {@link Map3D.export} (clears it first). */
231
+ import(data: Map3DCell[]): void;
232
+ /** Copies every stored cell of a {@link Map2D} into a new map, at z = 0. */
233
+ static fromMap2D(map2d: Map2D, defaultValue?: number): Map3D;
234
+ }
235
+ //#endregion
236
+ export { XYZ as _, Map3DCell as a, MAP_2D_MIN_COORD as c, ColorPalette as d, RGB as f, XY as g, NoiseFunction3D as h, Map3D as i, Map2D as l, KeyValue as m, MAP_3D_MAX_COORD as n, DEFAULT_MAP_2D_VALUE as o, buildColorPalette as p, MAP_3D_MIN_COORD as r, MAP_2D_MAX_COORD as s, DEFAULT_MAP_3D_VALUE as t, Map2DData as u };
@@ -0,0 +1,94 @@
1
+ import { Map2D, Map2DData } from "./map2d.js";
2
+ //#region src/map3d.d.ts
3
+ /**
4
+ * Default "empty" value of a {@link Map3D}: cells holding it are not stored.
5
+ *
6
+ * @category Maps & Spatial
7
+ */
8
+ export declare const DEFAULT_MAP_3D_VALUE = -1;
9
+ /**
10
+ * Lowest supported coordinate on each axis of a {@link Map3D} (inclusive, -65,536); anything
11
+ * outside the range wraps around and collides with other coordinates.
12
+ *
13
+ * @category Maps & Spatial
14
+ */
15
+ export declare const MAP_3D_MIN_COORD: number;
16
+ /**
17
+ * Highest supported coordinate on each axis of a {@link Map3D} (inclusive, 65,535).
18
+ *
19
+ * @category Maps & Spatial
20
+ */
21
+ export declare const MAP_3D_MAX_COORD: number;
22
+ /**
23
+ * One stored cell, as in {@link Map3D.export} and {@link Map3D.import}.
24
+ *
25
+ * @category Maps & Spatial
26
+ */
27
+ export interface Map3DCell {
28
+ /** cell x */
29
+ x: number;
30
+ /** cell y */
31
+ y: number;
32
+ /** cell z */
33
+ z: number;
34
+ /** the cell's value */
35
+ n: number;
36
+ }
37
+ /**
38
+ * Sparse 3D grid of numbers keyed by integer coordinates, e.g. voxel data. Coordinates are floored,
39
+ * so any point inside a cell addresses that cell. Cells holding `defaultValue` are not stored:
40
+ * setting a cell to it removes the cell, and reading a missing cell returns it.
41
+ *
42
+ * Supports coordinates in [{@link MAP_3D_MIN_COORD}, {@link MAP_3D_MAX_COORD}] per axis; outside
43
+ * that range keys wrap around (a one-time `console.warn` per map reports it).
44
+ *
45
+ * ```ts
46
+ * const voxels = new Map3D(); // empty = -1
47
+ * voxels.setArea(0, 0, 0, 3, 0, 3, 1); // 4x1x4 floor (bounds inclusive)
48
+ * voxels.get(2, 0, 2); // 1
49
+ * const saved = voxels.export(); // [{ x, y, z, n }, ...]
50
+ * ```
51
+ *
52
+ * @category Maps & Spatial
53
+ */
54
+ export declare class Map3D {
55
+ /** value of every cell that hasn't been set; default {@link DEFAULT_MAP_3D_VALUE} */
56
+ readonly defaultValue: number;
57
+ private _data;
58
+ private _warnedOutOfRange;
59
+ /** the backing map (packed keys), shared, not a copy */
60
+ get data(): Map2DData;
61
+ /** number of stored (non-default) cells */
62
+ get size(): number;
63
+ /**
64
+ * @param mapData existing backing data to use as is (not copied), e.g. another map's `data`
65
+ */
66
+ constructor(defaultValue?: number, mapData?: Map2DData);
67
+ private _floor;
68
+ private _key;
69
+ private _pos;
70
+ /** Removes every stored cell. */
71
+ clear(): void;
72
+ /**
73
+ * Value of the cell containing (x, y, z) (coordinates are floored).
74
+ *
75
+ * @param defaultValue returned when the cell isn't stored; defaults to the map's `defaultValue`
76
+ */
77
+ get(x: number, y: number, z: number, defaultValue?: number): number;
78
+ /** Sets the cell containing (x, y, z) to `n`; leave out `n` (or pass `defaultValue`) to remove it. */
79
+ set(x: number, y: number, z: number, n?: number): void;
80
+ /**
81
+ * Sets every cell in the box from (x1, y1, z1) to (x2, y2, z2) to `n`; both corners are
82
+ * inclusive and must be the min / max corner (nothing is set if x2 < x1, etc.).
83
+ */
84
+ setArea(x1: number, y1: number, z1: number, x2: number, y2: number, z2: number, n: number): void;
85
+ /** Calls `callback` for every stored cell, in insertion order. Cells holding the default value are not visited. */
86
+ forEach(callback: (x: number, y: number, z: number, n: number) => void): void;
87
+ /** Stored cells as a plain array (e.g. for JSON); the inverse of {@link Map3D.import}. */
88
+ export(): Map3DCell[];
89
+ /** Replaces the map's contents with cells from {@link Map3D.export} (clears it first). */
90
+ import(data: Map3DCell[]): void;
91
+ /** Copies every stored cell of a {@link Map2D} into a new map, at z = 0. */
92
+ static fromMap2D(map2d: Map2D, defaultValue?: number): Map3D;
93
+ }
94
+ //#endregion
package/dist/map3d.js ADDED
@@ -0,0 +1,148 @@
1
+ //#region src/map3d.ts
2
+ /**
3
+ * Default "empty" value of a {@link Map3D}: cells holding it are not stored.
4
+ *
5
+ * @category Maps & Spatial
6
+ */
7
+ const DEFAULT_MAP_3D_VALUE = -1;
8
+ const BASE_3D = 131072;
9
+ const OFFSET_3D = 65536;
10
+ /**
11
+ * Lowest supported coordinate on each axis of a {@link Map3D} (inclusive, -65,536); anything
12
+ * outside the range wraps around and collides with other coordinates.
13
+ *
14
+ * @category Maps & Spatial
15
+ */
16
+ const MAP_3D_MIN_COORD = -65536;
17
+ /**
18
+ * Highest supported coordinate on each axis of a {@link Map3D} (inclusive, 65,535).
19
+ *
20
+ * @category Maps & Spatial
21
+ */
22
+ const MAP_3D_MAX_COORD = 65535;
23
+ /**
24
+ * Sparse 3D grid of numbers keyed by integer coordinates, e.g. voxel data. Coordinates are floored,
25
+ * so any point inside a cell addresses that cell. Cells holding `defaultValue` are not stored:
26
+ * setting a cell to it removes the cell, and reading a missing cell returns it.
27
+ *
28
+ * Supports coordinates in [{@link MAP_3D_MIN_COORD}, {@link MAP_3D_MAX_COORD}] per axis; outside
29
+ * that range keys wrap around (a one-time `console.warn` per map reports it).
30
+ *
31
+ * ```ts
32
+ * const voxels = new Map3D(); // empty = -1
33
+ * voxels.setArea(0, 0, 0, 3, 0, 3, 1); // 4x1x4 floor (bounds inclusive)
34
+ * voxels.get(2, 0, 2); // 1
35
+ * const saved = voxels.export(); // [{ x, y, z, n }, ...]
36
+ * ```
37
+ *
38
+ * @category Maps & Spatial
39
+ */
40
+ var Map3D = class Map3D {
41
+ /** value of every cell that hasn't been set; default {@link DEFAULT_MAP_3D_VALUE} */
42
+ defaultValue;
43
+ _data;
44
+ _warnedOutOfRange = false;
45
+ /** the backing map (packed keys), shared, not a copy */
46
+ get data() {
47
+ return this._data;
48
+ }
49
+ /** number of stored (non-default) cells */
50
+ get size() {
51
+ return this._data.size;
52
+ }
53
+ /**
54
+ * @param mapData existing backing data to use as is (not copied), e.g. another map's `data`
55
+ */
56
+ constructor(defaultValue = -1, mapData) {
57
+ this.defaultValue = defaultValue;
58
+ this._data = mapData ?? /* @__PURE__ */ new Map();
59
+ }
60
+ _floor(x, y, z) {
61
+ return [
62
+ Math.floor(x),
63
+ Math.floor(y),
64
+ Math.floor(z)
65
+ ];
66
+ }
67
+ _key(x, y, z) {
68
+ if (!this._warnedOutOfRange && (x < -65536 || x > 65535 || y < -65536 || y > 65535 || z < -65536 || z > 65535)) {
69
+ this._warnedOutOfRange = true;
70
+ console.warn(`Map3D: (${x}, ${y}, ${z}) is outside the supported range [${MAP_3D_MIN_COORD}, ${MAP_3D_MAX_COORD}] per axis; it wraps around and collides with other coordinates. Further warnings for this map are suppressed.`);
71
+ }
72
+ const X = x + OFFSET_3D & 131071;
73
+ const Y = y + OFFSET_3D & 131071;
74
+ const Z = z + OFFSET_3D & 131071;
75
+ return X * BASE_3D * BASE_3D + Y * BASE_3D + Z;
76
+ }
77
+ _pos(key) {
78
+ const Z = key % BASE_3D;
79
+ const Y = Math.floor(key / BASE_3D) % BASE_3D;
80
+ return [
81
+ Math.floor(key / (BASE_3D * BASE_3D)) - OFFSET_3D,
82
+ Y - OFFSET_3D,
83
+ Z - OFFSET_3D
84
+ ];
85
+ }
86
+ /** Removes every stored cell. */
87
+ clear() {
88
+ this._data.clear();
89
+ }
90
+ /**
91
+ * Value of the cell containing (x, y, z) (coordinates are floored).
92
+ *
93
+ * @param defaultValue returned when the cell isn't stored; defaults to the map's `defaultValue`
94
+ */
95
+ get(x, y, z, defaultValue = this.defaultValue) {
96
+ [x, y, z] = this._floor(x, y, z);
97
+ return this._data.get(this._key(x, y, z)) ?? defaultValue;
98
+ }
99
+ /** Sets the cell containing (x, y, z) to `n`; leave out `n` (or pass `defaultValue`) to remove it. */
100
+ set(x, y, z, n = this.defaultValue) {
101
+ [x, y, z] = this._floor(x, y, z);
102
+ const key = this._key(x, y, z);
103
+ if (n === this.defaultValue) this._data.delete(key);
104
+ else this._data.set(key, n);
105
+ }
106
+ /**
107
+ * Sets every cell in the box from (x1, y1, z1) to (x2, y2, z2) to `n`; both corners are
108
+ * inclusive and must be the min / max corner (nothing is set if x2 < x1, etc.).
109
+ */
110
+ setArea(x1, y1, z1, x2, y2, z2, n) {
111
+ for (let z = z1; z <= z2; z++) for (let y = y1; y <= y2; y++) for (let x = x1; x <= x2; x++) this.set(x, y, z, n);
112
+ }
113
+ /** Calls `callback` for every stored cell, in insertion order. Cells holding the default value are not visited. */
114
+ forEach(callback) {
115
+ this._data.forEach((n, key) => {
116
+ const [x, y, z] = this._pos(key);
117
+ callback(x, y, z, n);
118
+ });
119
+ }
120
+ /** Stored cells as a plain array (e.g. for JSON); the inverse of {@link Map3D.import}. */
121
+ export() {
122
+ const data = [];
123
+ this.forEach((x, y, z, n) => {
124
+ data.push({
125
+ x,
126
+ y,
127
+ z,
128
+ n
129
+ });
130
+ });
131
+ return data;
132
+ }
133
+ /** Replaces the map's contents with cells from {@link Map3D.export} (clears it first). */
134
+ import(data) {
135
+ this.clear();
136
+ data.forEach(({ x, y, z, n }) => {
137
+ this.set(x, y, z, n);
138
+ });
139
+ }
140
+ /** Copies every stored cell of a {@link Map2D} into a new map, at z = 0. */
141
+ static fromMap2D(map2d, defaultValue = -1) {
142
+ const map3d = new Map3D(defaultValue);
143
+ map2d.forEach((x, y, n) => map3d.set(x, y, 0, n));
144
+ return map3d;
145
+ }
146
+ };
147
+ //#endregion
148
+ export { DEFAULT_MAP_3D_VALUE, MAP_3D_MAX_COORD, MAP_3D_MIN_COORD, Map3D };