@margoengine/voxel 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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1671 -3
- package/dist/bevel.d.ts +79 -0
- package/dist/bevel.js +453 -0
- package/dist/blocks-BabtpQT-.js +179 -0
- package/dist/blocks.d.ts +220 -0
- package/dist/border.d.ts +9 -0
- package/dist/chunk-BCoaKSn4.js +158 -0
- package/dist/chunk.d.ts +112 -0
- package/dist/codec-ZZFj1xvt.js +410 -0
- package/dist/codec.d.ts +125 -0
- package/dist/codec.js +1 -0
- package/dist/debris.d.ts +114 -0
- package/dist/debris.js +387 -0
- package/dist/faces-DUR7LhrN.js +146 -0
- package/dist/faces.d.ts +7 -0
- package/dist/fluids.d.ts +74 -0
- package/dist/fluids.js +226 -0
- package/dist/generate.d.ts +78 -0
- package/dist/generate.js +99 -0
- package/dist/gi.d.ts +76 -0
- package/dist/gi.js +264 -0
- package/dist/gpu-mesher.d.ts +77 -0
- package/dist/gpu-mesher.js +461 -0
- package/dist/granular-mpm.d.ts +32 -0
- package/dist/granular-mpm.js +46 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/light-rgb.d.ts +71 -0
- package/dist/light-rgb.js +287 -0
- package/dist/light-worker.d.ts +84 -0
- package/dist/light-worker.js +110 -0
- package/dist/light.d.ts +108 -0
- package/dist/light.js +332 -0
- package/dist/light.worker.d.ts +1 -0
- package/dist/lightmap.d.ts +50 -0
- package/dist/lightmap.js +68 -0
- package/dist/lod.d.ts +87 -0
- package/dist/lod.js +182 -0
- package/dist/mesher.d.ts +214 -0
- package/dist/mesher.js +539 -0
- package/dist/mesher.worker.d.ts +1 -0
- package/dist/model-data-D_rIKJCQ.js +41 -0
- package/dist/model-data.d.ts +21 -0
- package/dist/model-mesh-C4Gi6bMM.js +122 -0
- package/dist/model-mesh.d.ts +21 -0
- package/dist/model-surface.d.ts +5 -0
- package/dist/models.d.ts +74 -0
- package/dist/models.js +308 -0
- package/dist/objects.d.ts +73 -0
- package/dist/objects.js +152 -0
- package/dist/octants-C_Ic7gdn.js +10 -0
- package/dist/octants.d.ts +16 -0
- package/dist/origin-DsMelUxs.js +23 -0
- package/dist/origin.d.ts +7 -0
- package/dist/physics.d.ts +147 -0
- package/dist/physics.js +486 -0
- package/dist/query.d.ts +166 -0
- package/dist/query.js +305 -0
- package/dist/render-post.d.ts +6 -0
- package/dist/render.d.ts +296 -0
- package/dist/render.js +1613 -0
- package/dist/shared/borrowed.d.ts +44 -0
- package/dist/smooth-CEwHkXli.js +968 -0
- package/dist/smooth-clip.d.ts +24 -0
- package/dist/smooth-lod.d.ts +118 -0
- package/dist/smooth-lod.js +244 -0
- package/dist/smooth.d.ts +171 -0
- package/dist/smooth.js +1 -0
- package/dist/stream.d.ts +201 -0
- package/dist/stream.js +402 -0
- package/dist/terrain.worker.d.ts +1 -0
- package/dist/textures.d.ts +86 -0
- package/dist/ticks.d.ts +126 -0
- package/dist/ticks.js +239 -0
- package/dist/unit-light-Ch7roJgB.js +7 -0
- package/dist/vox.d.ts +237 -0
- package/dist/vox.js +780 -0
- package/dist/wind-data.d.ts +13 -0
- package/dist/wind.d.ts +68 -0
- package/dist/wind.js +170 -0
- package/dist/worker-DlGRxlc4.js +44 -0
- package/dist/worker-pool-sPyC6jCE.js +93 -0
- package/dist/workers/light.js +1 -0
- package/dist/workers/mesher.js +1 -0
- package/dist/workers/terrain.js +1 -0
- package/dist/world-CelTlft2.js +153 -0
- package/dist/world.d.ts +117 -0
- package/package.json +175 -6
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
/** Raw RGBA8 pixels, top row first, 4 bytes per texel: what `ImageData` holds, without needing the DOM. */
|
|
3
|
+
export interface BlockPixels {
|
|
4
|
+
readonly width: number;
|
|
5
|
+
readonly height: number;
|
|
6
|
+
readonly data: Uint8Array | Uint8ClampedArray;
|
|
7
|
+
}
|
|
8
|
+
/** Options of {@link BlockTextures}. */
|
|
9
|
+
export interface BlockTexturesOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Width and height of every layer in texels, a power of two from 1 to 4,096. Images of another size are drawn at
|
|
12
|
+
* this size, nearest-neighbour. Default: the first image's width, or 16.
|
|
13
|
+
*/
|
|
14
|
+
size?: number;
|
|
15
|
+
/**
|
|
16
|
+
* One image per texture layer, layer 0 first, at most 2048: anything `drawImage` takes, or raw `ImageData`-shaped
|
|
17
|
+
* `{width, height, data}` RGBA8 pixels (top row first) of exactly `size` x `size`. Default: one white layer.
|
|
18
|
+
*/
|
|
19
|
+
images?: readonly (CanvasImageSource | ImageData | BlockPixels)[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The block textures of a {@link VoxelRenderer}: one `rgba8unorm-srgb` 2D-array texture, a layer per texture index of
|
|
23
|
+
* `BlockTypeOptions.textures`, sampled nearest-neighbour up close and trilinear in the distance. Mips are made once on
|
|
24
|
+
* the CPU per layer (see the README); the renderer creates the GPU texture on its device. Missing layers are white.
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* const textures = await BlockTextures.load(['stone.png', 'dirt.png', 'grass-top.png']); // layers 0, 1, 2
|
|
28
|
+
* const atlas = BlockTextures.fromAtlas(image, { size: 16 }); // a grid of 16 x 16 tiles, row by row
|
|
29
|
+
* textures.set(1, canvas); // live: the next frame shows it
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare class BlockTextures {
|
|
33
|
+
#private;
|
|
34
|
+
/** Width and height of every layer in texels. */
|
|
35
|
+
readonly size: number;
|
|
36
|
+
/**
|
|
37
|
+
* @throws TypeError when an option or image has the wrong type.
|
|
38
|
+
* @throws RangeError for a size that is not a power of two up to 4,096, more than 2048 images, or raw pixels of
|
|
39
|
+
* another size.
|
|
40
|
+
* @throws MargoError `unsupported` for an image to scale or draw without OffscreenCanvas (raw pixels work anywhere).
|
|
41
|
+
*/
|
|
42
|
+
constructor(options?: BlockTexturesOptions);
|
|
43
|
+
/**
|
|
44
|
+
* Fetches one image per URL (layer i from `urls[i]`) through `options.fetch` (default `globalThis.fetch`; for
|
|
45
|
+
* example a LoadingManager's `fetch`) and makes the textures from them.
|
|
46
|
+
* @throws TypeError when `urls` is not an array of 1 to 2048 URLs (in the promise, like every failure).
|
|
47
|
+
* @throws MargoError `failed` when an image does not load.
|
|
48
|
+
*/
|
|
49
|
+
static load(urls: readonly (string | URL)[], { fetch: get, ...options }?: Omit<BlockTexturesOptions, 'images'> & {
|
|
50
|
+
fetch?: typeof fetch;
|
|
51
|
+
}): Promise<BlockTextures>;
|
|
52
|
+
/**
|
|
53
|
+
* Cuts an atlas into layers: `size` x `size` tiles (default 16) row by row, so the tile at column c and row r of a
|
|
54
|
+
* `columns`-wide atlas is layer `r * columns + c`. Pixels past the last whole tile are ignored.
|
|
55
|
+
* @throws TypeError when `atlas` is not an image or pixels.
|
|
56
|
+
* @throws RangeError for a bad size, an atlas smaller than one tile, or more than 256 tiles.
|
|
57
|
+
* @throws MargoError `unsupported` for an image without OffscreenCanvas (raw pixels work anywhere).
|
|
58
|
+
*/
|
|
59
|
+
static fromAtlas(atlas: CanvasImageSource | ImageData | BlockPixels, options?: Omit<BlockTexturesOptions, 'images'>): BlockTextures;
|
|
60
|
+
/** Number of layers: one past the highest layer given. */
|
|
61
|
+
get layers(): number;
|
|
62
|
+
/**
|
|
63
|
+
* Replaces layer `layer` (0-2047) with `image`, drawn at `size` x `size`; a layer past the current count adds layers
|
|
64
|
+
* (white up to it). Live: renderers upload it before their next frame.
|
|
65
|
+
* @throws TypeError or RangeError for a bad layer or image (as the constructor).
|
|
66
|
+
* @throws MargoError `disposed` after {@link BlockTextures.dispose}.
|
|
67
|
+
*/
|
|
68
|
+
set(layer: number, image: CanvasImageSource | ImageData | BlockPixels): void;
|
|
69
|
+
/**
|
|
70
|
+
* Destroys the GPU texture. Renderers still using these textures throw on their next frame. The texture is also
|
|
71
|
+
* destroyed when the last renderer drawing with it is disposed or removed, and made again when one draws.
|
|
72
|
+
*/
|
|
73
|
+
dispose(): void;
|
|
74
|
+
[Symbol.dispose](): void;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* VoxelRenderer's view of `textures` on `device` (2d-array): creates the texture (again after a device change or new
|
|
78
|
+
* layers) and uploads the layers changed since the last call. Not part of the public API.
|
|
79
|
+
* @throws MargoError `disposed` after `textures.dispose()`.
|
|
80
|
+
*/
|
|
81
|
+
export declare function blockTextureView(textures: BlockTextures, device: GPUDevice, user: object): GPUTextureView;
|
|
82
|
+
/**
|
|
83
|
+
* `user` no longer draws with `textures`: the last user destroys the GPU texture, which the next
|
|
84
|
+
* {@link blockTextureView} makes again from the kept pixels. Not part of the public API.
|
|
85
|
+
*/
|
|
86
|
+
export declare function releaseBlockTextureView(textures: BlockTextures, user: object): void;
|
package/dist/ticks.d.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { VoxelWorld } from './world.js';
|
|
2
|
+
/** What a {@link VoxelTickRule} hook gets: the cell, its state, and the ticker's random numbers and schedule. */
|
|
3
|
+
export interface VoxelTickContext {
|
|
4
|
+
readonly world: VoxelWorld;
|
|
5
|
+
readonly x: number;
|
|
6
|
+
readonly y: number;
|
|
7
|
+
readonly z: number;
|
|
8
|
+
/** The cell's state when the hook runs. */
|
|
9
|
+
readonly state: number;
|
|
10
|
+
/** The tick number. */
|
|
11
|
+
readonly tick: number;
|
|
12
|
+
/** A number in [0, 1) from the ticker's seeded generator. */
|
|
13
|
+
random(): number;
|
|
14
|
+
/** Runs this cell's `scheduled` hook `delay` ticks from now (an integer >= 1). */
|
|
15
|
+
schedule(delay: number): void;
|
|
16
|
+
}
|
|
17
|
+
/** The hooks of one block type; any may be left out. */
|
|
18
|
+
export interface VoxelTickRule {
|
|
19
|
+
/** A random tick picked this cell. */
|
|
20
|
+
random?(context: VoxelTickContext): void;
|
|
21
|
+
/** A tick this cell asked for (or another rule asked for at it) came. */
|
|
22
|
+
scheduled?(context: VoxelTickContext): void;
|
|
23
|
+
/** A block beside this cell (one of six) changed. */
|
|
24
|
+
neighbor?(context: VoxelTickContext): void;
|
|
25
|
+
/** This cell changed (it was set to this block, or its data changed). */
|
|
26
|
+
placed?(context: VoxelTickContext): void;
|
|
27
|
+
}
|
|
28
|
+
/** Options of {@link VoxelTicks.attach}. */
|
|
29
|
+
export interface VoxelTicksOptions {
|
|
30
|
+
/** Rules by block type id or name. More with {@link VoxelTicks.rule}. */
|
|
31
|
+
rules?: Readonly<Record<string, VoxelTickRule>>;
|
|
32
|
+
/** Cells picked per loaded chunk per tick for random ticks, an integer 0..4096. Default 24 (3 per 16-cubed). */
|
|
33
|
+
randomTicks?: number;
|
|
34
|
+
/** Ticks a second {@link VoxelTicks.update} advances by. Default 20. */
|
|
35
|
+
ticksPerSecond?: number;
|
|
36
|
+
/** Seed of the random ticks and `random()`, an integer. Default 1. */
|
|
37
|
+
seed?: number;
|
|
38
|
+
/** Most scheduled and neighbour updates a tick; the rest wait for the next. Default 65,536. */
|
|
39
|
+
maxUpdates?: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Random, scheduled and neighbour block updates for a world. Attach it, give block types rules, and call
|
|
43
|
+
* {@link VoxelTicks.update} every frame (or {@link VoxelTicks.tick} for one tick).
|
|
44
|
+
*/
|
|
45
|
+
export declare class VoxelTicks {
|
|
46
|
+
#private;
|
|
47
|
+
readonly world: VoxelWorld;
|
|
48
|
+
/**
|
|
49
|
+
* @throws TypeError for a wrong world or option type; RangeError for options out of range or a rule for a block
|
|
50
|
+
* that is not defined.
|
|
51
|
+
*/
|
|
52
|
+
static attach(world: VoxelWorld, options?: VoxelTicksOptions): VoxelTicks;
|
|
53
|
+
private constructor();
|
|
54
|
+
/**
|
|
55
|
+
* Sets (or with `undefined` removes) the rule of a block type.
|
|
56
|
+
* @throws RangeError for a block that is not defined; TypeError for a rule that is not an object of functions.
|
|
57
|
+
*/
|
|
58
|
+
rule(block: number | string, rule: VoxelTickRule | undefined): void;
|
|
59
|
+
/** Ticks run so far. */
|
|
60
|
+
get ticks(): number;
|
|
61
|
+
/** Scheduled updates waiting, plus neighbour updates not yet run. */
|
|
62
|
+
get pending(): number;
|
|
63
|
+
/**
|
|
64
|
+
* Runs the `scheduled` hook of the block at (x, y, z) `delay` ticks from now.
|
|
65
|
+
* @throws RangeError for a cell or delay that is not an integer (delay >= 1).
|
|
66
|
+
*/
|
|
67
|
+
schedule(x: number, y: number, z: number, delay: number): void;
|
|
68
|
+
/**
|
|
69
|
+
* Advances by `seconds` (whole ticks at `ticksPerSecond`; the rest carries over), at most 10 ticks a call. Returns
|
|
70
|
+
* the ticks run.
|
|
71
|
+
* @throws RangeError for negative or non-finite seconds; MargoError `disposed` after `dispose()`.
|
|
72
|
+
*/
|
|
73
|
+
update(seconds: number): number;
|
|
74
|
+
/**
|
|
75
|
+
* One tick: the scheduled updates due, then the random ticks of every loaded chunk (chunks in load order), then the
|
|
76
|
+
* `placed` and `neighbor` hooks of every change so far (changes they make run next tick). Up to `maxUpdates` scheduled and
|
|
77
|
+
* neighbour updates; the rest wait.
|
|
78
|
+
* @throws MargoError `disposed` after `dispose()`; errors of rules propagate, and the updates after the one that threw
|
|
79
|
+
* wait for the next tick.
|
|
80
|
+
*/
|
|
81
|
+
tick(): void;
|
|
82
|
+
/** Stops following the world and drops what is scheduled. */
|
|
83
|
+
dispose(): void;
|
|
84
|
+
[Symbol.dispose](): void;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Falling blocks (sand, gravel): a block with an open cell below falls one cell every `delay` ticks.
|
|
88
|
+
* A liquid cell below moves up into the space left by the falling block.
|
|
89
|
+
*/
|
|
90
|
+
export declare function fallingRule(delay?: number): VoxelTickRule;
|
|
91
|
+
/** Options of {@link spreadRule}. */
|
|
92
|
+
export interface SpreadRuleOptions {
|
|
93
|
+
/** The block it spreads onto, by id. */
|
|
94
|
+
onto: number;
|
|
95
|
+
/** Chance per random tick, 0..1. Default 1. */
|
|
96
|
+
chance?: number;
|
|
97
|
+
/** Least sky light (high 4 bits of the light byte) of the cell above the target, 0..15. Default 0. */
|
|
98
|
+
light?: number;
|
|
99
|
+
/** What the block turns into when a solid block covers it (grass under stone becomes dirt), by id. Default none. */
|
|
100
|
+
smothered?: number;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* A block that spreads onto `onto` blocks within one cell sideways and one up or down (grass onto dirt, moss onto
|
|
104
|
+
* stone) on random ticks, where the cell above the target is not solid and lit enough.
|
|
105
|
+
*/
|
|
106
|
+
export declare function spreadRule(options: SpreadRuleOptions): VoxelTickRule;
|
|
107
|
+
/**
|
|
108
|
+
* A block that grows through `stages` (its state data 0 to stages - 1) on random ticks with `chance`, then turns into
|
|
109
|
+
* `grown` (by id) when given (a sapling into a log).
|
|
110
|
+
*/
|
|
111
|
+
export declare function growRule(stages: number, chance?: number, grown?: number): VoxelTickRule;
|
|
112
|
+
/** Options of {@link fireRule}. */
|
|
113
|
+
export interface FireRuleOptions {
|
|
114
|
+
/** Block ids that burn, with their chance per fire tick of catching (0..1). */
|
|
115
|
+
burns: Readonly<Record<number, number>>;
|
|
116
|
+
/** Ticks between fire steps. Default 30. */
|
|
117
|
+
delay?: number;
|
|
118
|
+
/** Fire steps a fire lasts (its state data counts them). Default 8. */
|
|
119
|
+
life?: number;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Fire: a fire block (put it in a cell to light it) wakes every `delay` ticks, sets fire to the air beside each
|
|
123
|
+
* burnable neighbour with that neighbour's chance (the neighbour burns away into fire), ages, and goes out after
|
|
124
|
+
* `life` steps or when nothing burnable is beside it.
|
|
125
|
+
*/
|
|
126
|
+
export declare function fireRule(options: FireRuleOptions): VoxelTickRule;
|
package/dist/ticks.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { i as e } from "./blocks-BabtpQT-.js";
|
|
5
|
+
import { MargoError as t } from "@margoengine/math/errors";
|
|
6
|
+
//#region src/ticks.ts
|
|
7
|
+
var n = (e, t, n, r) => {
|
|
8
|
+
if (typeof t != "number") throw TypeError(`VoxelTicks ${e} must be a number`);
|
|
9
|
+
if (!Number.isInteger(t) || t < n || t > r) throw RangeError(`VoxelTicks ${e} is ${t}; pass an integer in ${n}..${r}`);
|
|
10
|
+
return t;
|
|
11
|
+
}, r = [
|
|
12
|
+
[
|
|
13
|
+
1,
|
|
14
|
+
0,
|
|
15
|
+
0
|
|
16
|
+
],
|
|
17
|
+
[
|
|
18
|
+
-1,
|
|
19
|
+
0,
|
|
20
|
+
0
|
|
21
|
+
],
|
|
22
|
+
[
|
|
23
|
+
0,
|
|
24
|
+
1,
|
|
25
|
+
0
|
|
26
|
+
],
|
|
27
|
+
[
|
|
28
|
+
0,
|
|
29
|
+
-1,
|
|
30
|
+
0
|
|
31
|
+
],
|
|
32
|
+
[
|
|
33
|
+
0,
|
|
34
|
+
0,
|
|
35
|
+
1
|
|
36
|
+
],
|
|
37
|
+
[
|
|
38
|
+
0,
|
|
39
|
+
0,
|
|
40
|
+
-1
|
|
41
|
+
]
|
|
42
|
+
], i = class e {
|
|
43
|
+
#e = /* @__PURE__ */ new Map();
|
|
44
|
+
#t = /* @__PURE__ */ new Map();
|
|
45
|
+
#n = /* @__PURE__ */ new Map();
|
|
46
|
+
#r = [];
|
|
47
|
+
#i = 0;
|
|
48
|
+
#a = 0;
|
|
49
|
+
#o = 0;
|
|
50
|
+
#s;
|
|
51
|
+
#c;
|
|
52
|
+
#l;
|
|
53
|
+
#u;
|
|
54
|
+
#d;
|
|
55
|
+
#f = !1;
|
|
56
|
+
static attach(t, n = {}) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
57
|
+
return new e(t, n);
|
|
58
|
+
}
|
|
59
|
+
constructor(e, t) {
|
|
60
|
+
if (typeof e?.onChange != "function" || typeof e.onChunk != "function") throw TypeError("VoxelTicks.attach expects a VoxelWorld; pass new VoxelWorld({blocks})");
|
|
61
|
+
if (!t || typeof t != "object") throw TypeError("VoxelTicks options must be an object");
|
|
62
|
+
let { rules: i = {}, randomTicks: a = 24, ticksPerSecond: o = 20, seed: s = 1, maxUpdates: c = 65536 } = t;
|
|
63
|
+
if (this.#l = n("randomTicks", a, 0, 4096), this.#s = n("seed", s, -(2 ** 31), 2 ** 31 - 1) | 0 || 1, typeof o != "number") throw TypeError("VoxelTicks ticksPerSecond must be a number");
|
|
64
|
+
if (!(o > 0 && o <= 1e3)) throw RangeError(`VoxelTicks ticksPerSecond is ${o}; pass a number in (0, 1000]`);
|
|
65
|
+
this.#c = o, this.#u = n("maxUpdates", c, 1, 2 ** 30), this.world = e;
|
|
66
|
+
for (let [e, t] of Object.entries(i)) this.rule(/^\d+$/.test(e) ? Number(e) : e, t);
|
|
67
|
+
this.#d = [e.onChunk((e, t, n, r) => {
|
|
68
|
+
let i = `${e},${t},${n}`;
|
|
69
|
+
r ? this.#t.set(i, [
|
|
70
|
+
e,
|
|
71
|
+
t,
|
|
72
|
+
n
|
|
73
|
+
]) : this.#t.delete(i);
|
|
74
|
+
}), e.onChange((e, t, n) => {
|
|
75
|
+
this.#r.push(e, t, n, 1);
|
|
76
|
+
for (let [i, a, o] of r) this.#r.push(e + i, t + a, n + o, 0);
|
|
77
|
+
})];
|
|
78
|
+
}
|
|
79
|
+
rule(e, t) {
|
|
80
|
+
let n = this.world.blocks.table, r = typeof e == "string" ? n.find((t) => t.name === e) : n[e];
|
|
81
|
+
if (!r) throw RangeError(`VoxelTicks rule block "${e}" is not defined; define it with blocks.define()`);
|
|
82
|
+
if (t === void 0) return void this.#e.delete(r.id);
|
|
83
|
+
if (!t || typeof t != "object" || [
|
|
84
|
+
"random",
|
|
85
|
+
"scheduled",
|
|
86
|
+
"neighbor",
|
|
87
|
+
"placed"
|
|
88
|
+
].some((e) => t[e] !== void 0 && typeof t[e] != "function")) throw TypeError("VoxelTicks rule must be {random?, scheduled?, neighbor?, placed?} functions");
|
|
89
|
+
this.#e.set(r.id, t);
|
|
90
|
+
}
|
|
91
|
+
get ticks() {
|
|
92
|
+
return this.#a;
|
|
93
|
+
}
|
|
94
|
+
get pending() {
|
|
95
|
+
return this.#i + this.#r.length / 4;
|
|
96
|
+
}
|
|
97
|
+
schedule(e, t, r, i) {
|
|
98
|
+
if (![
|
|
99
|
+
e,
|
|
100
|
+
t,
|
|
101
|
+
r
|
|
102
|
+
].every(Number.isInteger)) throw RangeError("VoxelTicks.schedule cell must be integers x, y, z");
|
|
103
|
+
n("delay", i, 1, 2 ** 30);
|
|
104
|
+
let a = this.#a + i, o = this.#n.get(a);
|
|
105
|
+
o || this.#n.set(a, o = []), o.push(e, t, r), this.#i++;
|
|
106
|
+
}
|
|
107
|
+
#p = () => {
|
|
108
|
+
let e = this.#s = this.#s + 1831565813 | 0;
|
|
109
|
+
return e = Math.imul(e ^ e >>> 15, e | 1), e ^= e + Math.imul(e ^ e >>> 7, e | 61), ((e ^ e >>> 14) >>> 0) / 4294967296;
|
|
110
|
+
};
|
|
111
|
+
#m(e, t, n, r) {
|
|
112
|
+
let i = this.world.getBlock(e, t, n), a = this.#e.get(i & 65535), o = a?.[r];
|
|
113
|
+
o && o.call(a, {
|
|
114
|
+
world: this.world,
|
|
115
|
+
x: e,
|
|
116
|
+
y: t,
|
|
117
|
+
z: n,
|
|
118
|
+
state: i,
|
|
119
|
+
tick: this.#a,
|
|
120
|
+
random: this.#p,
|
|
121
|
+
schedule: (r) => this.schedule(e, t, n, r)
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
update(e) {
|
|
125
|
+
if (typeof e != "number" || !(e >= 0) || !Number.isFinite(e)) throw RangeError(`VoxelTicks.update seconds is ${e}; pass the frame's seconds, 0 or more`);
|
|
126
|
+
this.#o = Math.min(this.#o + e * this.#c, 10);
|
|
127
|
+
let t = 0;
|
|
128
|
+
for (; this.#o >= 1; this.#o--, t++) this.tick();
|
|
129
|
+
return t;
|
|
130
|
+
}
|
|
131
|
+
tick() {
|
|
132
|
+
if (this.#f) throw new t("disposed", "VoxelTicks is disposed; attach a new one");
|
|
133
|
+
let e = ++this.#a, n = this.#u, r = this.#n.get(e);
|
|
134
|
+
if (r) {
|
|
135
|
+
this.#n.delete(e);
|
|
136
|
+
let t = 0;
|
|
137
|
+
try {
|
|
138
|
+
for (; t < r.length && n > 0; n--) t += 3, this.#i--, this.#m(r[t - 3], r[t - 2], r[t - 1], "scheduled");
|
|
139
|
+
} finally {
|
|
140
|
+
t < r.length && this.#n.set(e + 1, [...r.slice(t), ...this.#n.get(e + 1) ?? []]);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (this.#l && this.#e.size) for (let [e, t, n] of [...this.#t.values()]) for (let r = 0; r < this.#l; r++) {
|
|
144
|
+
let r = this.#p() * 32768 | 0;
|
|
145
|
+
this.#m(e * 32 + (r & 31), t * 32 + (r >> 10), n * 32 + (r >> 5 & 31), "random");
|
|
146
|
+
}
|
|
147
|
+
let i = this.#r;
|
|
148
|
+
this.#r = [];
|
|
149
|
+
let a = 0;
|
|
150
|
+
try {
|
|
151
|
+
for (; a < i.length && n > 0; n--) a += 4, this.#m(i[a - 4], i[a - 3], i[a - 2], i[a - 1] ? "placed" : "neighbor");
|
|
152
|
+
} finally {
|
|
153
|
+
a < i.length && (this.#r = i.slice(a).concat(this.#r));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
dispose() {
|
|
157
|
+
if (!this.#f) {
|
|
158
|
+
this.#f = !0;
|
|
159
|
+
for (let e of this.#d) e();
|
|
160
|
+
this.#n.clear(), this.#r = [], this.#i = 0;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
[Symbol.dispose]() {
|
|
164
|
+
this.dispose();
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
function a(e = 2) {
|
|
168
|
+
n("falling delay", e, 1, 2 ** 20);
|
|
169
|
+
let t = (e, t) => {
|
|
170
|
+
let n = e.world.blocks.table[e.world.getBlock(e.x, t, e.z) & 65535];
|
|
171
|
+
return t >= e.world.minY && !!e.world.getChunk(e.x >> 5, t >> 5, e.z >> 5) && (!n?.solid || n.id === 0);
|
|
172
|
+
}, r = (n) => {
|
|
173
|
+
t(n, n.y - 1) && n.schedule(e);
|
|
174
|
+
};
|
|
175
|
+
return {
|
|
176
|
+
neighbor: r,
|
|
177
|
+
placed: r,
|
|
178
|
+
scheduled: (e) => {
|
|
179
|
+
if (!t(e, e.y - 1)) return;
|
|
180
|
+
let n = e.world.getBlock(e.x, e.y - 1, e.z), r = e.world.blocks.table[n & 65535]?.liquid;
|
|
181
|
+
e.world.setBlocks([
|
|
182
|
+
e.x,
|
|
183
|
+
e.y,
|
|
184
|
+
e.z,
|
|
185
|
+
r ? n : 0,
|
|
186
|
+
e.x,
|
|
187
|
+
e.y - 1,
|
|
188
|
+
e.z,
|
|
189
|
+
e.state
|
|
190
|
+
]);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function o(e) {
|
|
195
|
+
if (!e || typeof e != "object") throw TypeError("spreadRule options must be {onto}");
|
|
196
|
+
let { onto: t, chance: r = 1, light: i = 0, smothered: a } = e;
|
|
197
|
+
if (n("spread onto", t, 0, 65535), !(r >= 0 && r <= 1)) throw RangeError(`spreadRule chance is ${r}; pass 0..1`);
|
|
198
|
+
n("spread light", i, 0, 15);
|
|
199
|
+
let o = (e, t, n, r) => {
|
|
200
|
+
let a = e.world.getBlock(t, n + 1, r), o = e.world.getChunk(t >> 5, n + 1 >> 5, r >> 5);
|
|
201
|
+
return e.world.blocks.table[a & 65535]?.solid ? !1 : !i || (o?.getLight(t & 31, n + 1 & 31, r & 31) ?? 0) >> 4 >= i;
|
|
202
|
+
};
|
|
203
|
+
return { random: (e) => {
|
|
204
|
+
if (a !== void 0 && e.world.blocks.table[e.world.getBlock(e.x, e.y + 1, e.z) & 65535]?.solid) return void e.world.setBlock(e.x, e.y, e.z, a);
|
|
205
|
+
if (e.random() >= r) return;
|
|
206
|
+
let n = e.x + Math.floor(e.random() * 3) - 1, i = e.y + Math.floor(e.random() * 3) - 1, s = e.z + Math.floor(e.random() * 3) - 1;
|
|
207
|
+
(e.world.getBlock(n, i, s) & 65535) === t && o(e, n, i, s) && e.world.setBlock(n, i, s, e.state);
|
|
208
|
+
} };
|
|
209
|
+
}
|
|
210
|
+
function s(t, r = 1, i) {
|
|
211
|
+
if (n("grow stages", t, 1, 65535), !(r >= 0 && r <= 1)) throw RangeError(`growRule chance is ${r}; pass 0..1`);
|
|
212
|
+
return { random: (n) => {
|
|
213
|
+
if (n.random() >= r) return;
|
|
214
|
+
let a = n.state >>> 16;
|
|
215
|
+
a + 1 < t ? n.world.setBlock(n.x, n.y, n.z, e(n.state & 65535, a + 1)) : i !== void 0 && n.world.setBlock(n.x, n.y, n.z, i);
|
|
216
|
+
} };
|
|
217
|
+
}
|
|
218
|
+
function c(t) {
|
|
219
|
+
if (!t || typeof t != "object" || !t.burns || typeof t.burns != "object") throw TypeError("fireRule options must be {burns: {[blockId]: chance}}");
|
|
220
|
+
let { delay: i = 30, life: a = 8 } = t, o = new Map(Object.entries(t.burns).map(([e, t]) => [Number(e), t]));
|
|
221
|
+
n("fire delay", i, 1, 2 ** 20), n("fire life", a, 1, 65535);
|
|
222
|
+
for (let e of o.values()) if (!(e >= 0 && e <= 1)) throw RangeError(`fireRule chance is ${e}; pass 0..1`);
|
|
223
|
+
return {
|
|
224
|
+
placed: (e) => {
|
|
225
|
+
e.state >>> 16 || e.schedule(i);
|
|
226
|
+
},
|
|
227
|
+
scheduled: (t) => {
|
|
228
|
+
let n = t.state >>> 16, s = t.state & 65535, c = !1;
|
|
229
|
+
for (let [n, i, a] of r) {
|
|
230
|
+
let r = t.x + n, l = t.y + i, u = t.z + a, d = o.get(t.world.getBlock(r, l, u) & 65535);
|
|
231
|
+
d !== void 0 && (c = !0, t.random() < d && t.world.setBlock(r, l, u, e(s, 0)));
|
|
232
|
+
}
|
|
233
|
+
if (!c || n + 1 >= a) return void t.world.setBlock(t.x, t.y, t.z, 0);
|
|
234
|
+
t.world.setBlock(t.x, t.y, t.z, e(s, n + 1)), t.schedule(i);
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
//#endregion
|
|
239
|
+
export { i as VoxelTicks, a as fallingRule, c as fireRule, s as growRule, o as spreadRule };
|