@margoengine/voxel 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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1670 -2
- 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-BmXhcsFt.js +412 -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 +40 -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 +1616 -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 +173 -4
package/dist/chunk.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/** Edge length of a chunk in cells: a chunk is 32 x 32 x 32 = 32,768 cells. Chunk coordinates are cell >> 5. */
|
|
2
|
+
export declare const CHUNK_SIZE = 32;
|
|
3
|
+
/**
|
|
4
|
+
* A chunk as plain typed arrays, from {@link VoxelChunk.pack}: post it to a worker with every array's buffer in the
|
|
5
|
+
* transfer list (each array owns its whole buffer and shares nothing with the chunk).
|
|
6
|
+
*/
|
|
7
|
+
export interface PackedChunk {
|
|
8
|
+
/** Block states the cell indices refer to; at most 2^bits and 32,768 entries. Entries no cell uses are allowed. */
|
|
9
|
+
palette: Uint32Array;
|
|
10
|
+
/** Bits per cell index: 0 (every cell is `palette[0]`), 1, 2, 4, 8 or 16. */
|
|
11
|
+
bits: 0 | 1 | 2 | 4 | 8 | 16;
|
|
12
|
+
/**
|
|
13
|
+
* 1,024 x `bits` words. Cell `i = x | z << 5 | y << 10` is bits `i * bits` to `(i + 1) * bits - 1`, low bits first;
|
|
14
|
+
* an index never straddles two words. Empty when `bits` is 0.
|
|
15
|
+
*/
|
|
16
|
+
words: Uint32Array;
|
|
17
|
+
/** One light byte per cell (sky light in the high nibble, block light in the low), or one byte for every cell. */
|
|
18
|
+
light: number | Uint8Array;
|
|
19
|
+
}
|
|
20
|
+
export declare const stateError: (state: unknown) => Error;
|
|
21
|
+
/**
|
|
22
|
+
* Package-internal (the package entry does not export it): widens the cell indices once so that `set` can add those
|
|
23
|
+
* of the first `count` `states` that the palette lacks without widening again (one re-encode instead of one per
|
|
24
|
+
* width step).
|
|
25
|
+
*/
|
|
26
|
+
export declare let reserveCells: (chunk: VoxelChunk, states: ArrayLike<number>, count: number) => void;
|
|
27
|
+
/** Package-internal: after sets prepared by {@link reserveCells}, narrows indices the remaining states do not need. */
|
|
28
|
+
export declare let settleCells: (chunk: VoxelChunk) => void;
|
|
29
|
+
/** The state every cell of `chunk` holds when it is uniform (its palette alone), else undefined. */
|
|
30
|
+
export declare let uniformState: (chunk: VoxelChunk) => number | undefined;
|
|
31
|
+
/** The states `chunk`'s cells use, possibly with unused ones (as `pack` gives them), as a view: no copy. */
|
|
32
|
+
export declare let paletteOf: (chunk: VoxelChunk) => Uint32Array;
|
|
33
|
+
/** The chunk's light: its one byte while every cell has it, else a copy of the 32,768 bytes. */
|
|
34
|
+
export declare let lightOf: (chunk: VoxelChunk) => number | Uint8Array;
|
|
35
|
+
/** Internal light change counter for optional renderers sharing the world's dirty queue. */
|
|
36
|
+
export declare let lightVersion: (chunk: VoxelChunk) => number;
|
|
37
|
+
/** Replaces the chunk's light with one byte for every cell or 32,768 bytes (taken, not copied). */
|
|
38
|
+
export declare let writeLight: (chunk: VoxelChunk, light: number | Uint8Array) => void;
|
|
39
|
+
/**
|
|
40
|
+
* The 32,768 block states and light bytes of one chunk. States are stored as indices into a palette of the distinct
|
|
41
|
+
* states, 0, 1, 2, 4, 8 or 16 bits per cell: a uniform chunk is its palette alone. Palette entries are reference
|
|
42
|
+
* counted, so the index width grows as states are added and shrinks back as they disappear. Light is one byte for
|
|
43
|
+
* every cell until a cell gets a different one. Cells are local (0..31 per axis); edit loaded chunks through
|
|
44
|
+
* {@link VoxelWorld} to keep its dirty set and change events.
|
|
45
|
+
*/
|
|
46
|
+
export declare class VoxelChunk {
|
|
47
|
+
private palette;
|
|
48
|
+
private counts;
|
|
49
|
+
private size;
|
|
50
|
+
private live;
|
|
51
|
+
private free;
|
|
52
|
+
private lookup;
|
|
53
|
+
private bits;
|
|
54
|
+
private words;
|
|
55
|
+
private lightByte;
|
|
56
|
+
private lightRevision;
|
|
57
|
+
private lightCells;
|
|
58
|
+
/**
|
|
59
|
+
* A uniform chunk: every cell is `state` (default 0, air) and every light byte 0.
|
|
60
|
+
* @throws TypeError or RangeError when `state` is not an unsigned 32-bit integer.
|
|
61
|
+
*/
|
|
62
|
+
constructor(state?: number);
|
|
63
|
+
/**
|
|
64
|
+
* Block state of local cell (x, y, z).
|
|
65
|
+
* @throws RangeError (TypeError for non-numbers) when a coordinate is not an integer in 0..31.
|
|
66
|
+
*/
|
|
67
|
+
get(x: number, y: number, z: number): number;
|
|
68
|
+
/**
|
|
69
|
+
* Sets local cell (x, y, z) to `state` and returns its previous state.
|
|
70
|
+
* @throws RangeError (TypeError for non-numbers) for a coordinate outside 0..31 or a state that is not a u32.
|
|
71
|
+
*/
|
|
72
|
+
set(x: number, y: number, z: number, state: number): number;
|
|
73
|
+
/**
|
|
74
|
+
* Sets every cell to `state`; the chunk becomes uniform (its palette alone). Light is unchanged.
|
|
75
|
+
* @throws TypeError or RangeError when `state` is not an unsigned 32-bit integer.
|
|
76
|
+
*/
|
|
77
|
+
fill(state: number): void;
|
|
78
|
+
/**
|
|
79
|
+
* Light byte of local cell (x, y, z): sky light (0-15) in the high nibble, block light (0-15) in the low.
|
|
80
|
+
* @throws RangeError (TypeError for non-numbers) when a coordinate is not an integer in 0..31.
|
|
81
|
+
*/
|
|
82
|
+
getLight(x: number, y: number, z: number): number;
|
|
83
|
+
/**
|
|
84
|
+
* Sets the light byte of local cell (x, y, z) (`sky << 4 | block`). The first byte that differs from the others
|
|
85
|
+
* allocates the 32 KiB per-cell array.
|
|
86
|
+
* @throws RangeError (TypeError for non-numbers) for a coordinate outside 0..31 or a byte outside 0..255.
|
|
87
|
+
*/
|
|
88
|
+
setLight(x: number, y: number, z: number, light: number): void;
|
|
89
|
+
/**
|
|
90
|
+
* Sets every cell's light byte to `light` (`sky << 4 | block`), so the chunk keeps one byte for all of them again.
|
|
91
|
+
* @throws RangeError (TypeError for non-numbers) for a byte outside 0..255.
|
|
92
|
+
*/
|
|
93
|
+
fillLight(light: number): void;
|
|
94
|
+
/** Bytes of the typed arrays the chunk holds now (palette, its counts, cell indices and per-cell light). */
|
|
95
|
+
get byteLength(): number;
|
|
96
|
+
/** A copy of the chunk as transferable typed arrays; {@link VoxelChunk.unpack} rebuilds it. */
|
|
97
|
+
pack(): PackedChunk;
|
|
98
|
+
/**
|
|
99
|
+
* A chunk from {@link VoxelChunk.pack}'s output (copied, so `packed` stays usable). Duplicate or unused palette
|
|
100
|
+
* entries are merged away and an index width wider than needed shrinks.
|
|
101
|
+
* @throws TypeError when `packed` or one of its arrays has the wrong type.
|
|
102
|
+
* @throws RangeError for a bit width other than 0, 1, 2, 4, 8 or 16, wrong array lengths, a light byte outside
|
|
103
|
+
* 0..255, or a cell index past the palette.
|
|
104
|
+
*/
|
|
105
|
+
static unpack(packed: PackedChunk): VoxelChunk;
|
|
106
|
+
private slot;
|
|
107
|
+
private find;
|
|
108
|
+
private grow;
|
|
109
|
+
private add;
|
|
110
|
+
private release;
|
|
111
|
+
private compact;
|
|
112
|
+
}
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import { n as e, s as t } from "./chunk-BCoaKSn4.js";
|
|
2
|
+
import { t as n } from "./world-CelTlft2.js";
|
|
3
|
+
import { MargoError as r } from "@margoengine/math/errors";
|
|
4
|
+
//#region ../shared/bytes.js
|
|
5
|
+
var i = (e) => e < 0 ? -2 * e - 1 : 2 * e, a = (e) => e % 2 ? -(e + 1) / 2 : e / 2, o = class {
|
|
6
|
+
at = 0;
|
|
7
|
+
constructor(e = 64) {
|
|
8
|
+
this.bytes = new Uint8Array(e), this.view = new DataView(this.bytes.buffer);
|
|
9
|
+
}
|
|
10
|
+
room(e) {
|
|
11
|
+
if (this.at + e <= this.bytes.length) return;
|
|
12
|
+
let t = new Uint8Array(Math.max(2 * this.bytes.length, this.at + e));
|
|
13
|
+
t.set(this.bytes), this.bytes = t, this.view = new DataView(t.buffer);
|
|
14
|
+
}
|
|
15
|
+
u8(e) {
|
|
16
|
+
this.room(1), this.bytes[this.at++] = e;
|
|
17
|
+
}
|
|
18
|
+
u16(e) {
|
|
19
|
+
this.room(2), this.view.setUint16(this.at, e, !0), this.at += 2;
|
|
20
|
+
}
|
|
21
|
+
u32(e) {
|
|
22
|
+
this.room(4), this.view.setUint32(this.at, e, !0), this.at += 4;
|
|
23
|
+
}
|
|
24
|
+
f32(e) {
|
|
25
|
+
this.room(4), this.view.setFloat32(this.at, e, !0), this.at += 4;
|
|
26
|
+
}
|
|
27
|
+
varint(e) {
|
|
28
|
+
for (this.room(5); e > 127; e >>>= 7) this.bytes[this.at++] = e & 127 | 128;
|
|
29
|
+
this.bytes[this.at++] = e;
|
|
30
|
+
}
|
|
31
|
+
raw(e) {
|
|
32
|
+
this.room(e.length), this.bytes.set(e, this.at), this.at += e.length;
|
|
33
|
+
}
|
|
34
|
+
done() {
|
|
35
|
+
return this.bytes.slice(0, this.at);
|
|
36
|
+
}
|
|
37
|
+
}, s = class {
|
|
38
|
+
constructor(e, t, n, r = 0) {
|
|
39
|
+
this.bytes = e, this.what = t, this.fix = n, this.at = r, this.view = new DataView(e.buffer, e.byteOffset, e.byteLength);
|
|
40
|
+
}
|
|
41
|
+
fail(e, t = this.fix) {
|
|
42
|
+
return /* @__PURE__ */ RangeError(`${this.what} ${e}; ${t}`);
|
|
43
|
+
}
|
|
44
|
+
need(e) {
|
|
45
|
+
if (this.at + e > this.bytes.length) throw this.fail(`is truncated at byte ${this.at} of ${this.bytes.length}`);
|
|
46
|
+
}
|
|
47
|
+
u8() {
|
|
48
|
+
return this.need(1), this.bytes[this.at++];
|
|
49
|
+
}
|
|
50
|
+
u16() {
|
|
51
|
+
return this.need(2), this.at += 2, this.view.getUint16(this.at - 2, !0);
|
|
52
|
+
}
|
|
53
|
+
u32() {
|
|
54
|
+
return this.need(4), this.at += 4, this.view.getUint32(this.at - 4, !0);
|
|
55
|
+
}
|
|
56
|
+
f32() {
|
|
57
|
+
return this.need(4), this.at += 4, this.view.getFloat32(this.at - 4, !0);
|
|
58
|
+
}
|
|
59
|
+
varint() {
|
|
60
|
+
let e = this.bytes, t = 0, n = 1;
|
|
61
|
+
for (let r = 0;; r++, n *= 128) {
|
|
62
|
+
this.at >= e.length && this.need(1);
|
|
63
|
+
let i = e[this.at++];
|
|
64
|
+
if (r === 4 && i > 15) throw this.fail(`has a varint wider than 32 bits at byte ${this.at - 1}`);
|
|
65
|
+
if (t += (i & 127) * n, i < 128) return t;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
end() {
|
|
69
|
+
if (this.at !== this.bytes.length) throw this.fail(`has ${this.bytes.length - this.at} bytes past its end`);
|
|
70
|
+
}
|
|
71
|
+
}, c = {
|
|
72
|
+
deflate: [
|
|
73
|
+
120,
|
|
74
|
+
156,
|
|
75
|
+
3,
|
|
76
|
+
0,
|
|
77
|
+
0,
|
|
78
|
+
0,
|
|
79
|
+
0,
|
|
80
|
+
1
|
|
81
|
+
],
|
|
82
|
+
"deflate-raw": [3, 0],
|
|
83
|
+
gzip: [
|
|
84
|
+
31,
|
|
85
|
+
139,
|
|
86
|
+
8,
|
|
87
|
+
0,
|
|
88
|
+
0,
|
|
89
|
+
0,
|
|
90
|
+
0,
|
|
91
|
+
0,
|
|
92
|
+
0,
|
|
93
|
+
3,
|
|
94
|
+
3,
|
|
95
|
+
0,
|
|
96
|
+
0,
|
|
97
|
+
0,
|
|
98
|
+
0,
|
|
99
|
+
0,
|
|
100
|
+
0,
|
|
101
|
+
0,
|
|
102
|
+
0,
|
|
103
|
+
0
|
|
104
|
+
]
|
|
105
|
+
}, l = /* @__PURE__ */ new Map();
|
|
106
|
+
async function u(e, t) {
|
|
107
|
+
let n = new Blob([e]).stream().pipeThrough(new DecompressionStream(t)).getReader();
|
|
108
|
+
try {
|
|
109
|
+
for (; !(await n.read()).done;);
|
|
110
|
+
return !0;
|
|
111
|
+
} catch {
|
|
112
|
+
return !1;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function d(e) {
|
|
116
|
+
if (!(e in c)) throw TypeError(`format must be deflate, deflate-raw or gzip (got ${e})`);
|
|
117
|
+
let t = l.get(e);
|
|
118
|
+
return t || (t = u(new Uint8Array([...c[e], 0]), e), l.set(e, t)), t;
|
|
119
|
+
}
|
|
120
|
+
async function f(e, t) {
|
|
121
|
+
return !await d(t) || e.byteLength === 0 ? !1 : u(e.subarray(0, e.byteLength - 1), t);
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/codec.ts
|
|
125
|
+
var p = 32768, m = 1, h = 1, g = 2, _ = 4, v = 262148, y = 263172, b = (e) => e << 5 >> 5 === e;
|
|
126
|
+
async function x(e, t, n) {
|
|
127
|
+
let r = e.writable.getWriter(), i = e.readable.getReader(), a = [];
|
|
128
|
+
r.write(t).catch(() => {}), r.close().catch(() => {});
|
|
129
|
+
let o = 0;
|
|
130
|
+
for (;;) {
|
|
131
|
+
let e;
|
|
132
|
+
try {
|
|
133
|
+
e = await i.read();
|
|
134
|
+
} catch {
|
|
135
|
+
throw RangeError("Chunk data holds a damaged deflate-raw stream; pass the bytes encodeChunk returned");
|
|
136
|
+
}
|
|
137
|
+
if (e.done) break;
|
|
138
|
+
if (o += e.value.length, o > n) throw i.cancel().catch(() => {}), RangeError(`Chunk data inflates past ${n} bytes, more than any chunk; pass what encodeChunk returned`);
|
|
139
|
+
a.push(e.value);
|
|
140
|
+
}
|
|
141
|
+
let s = new Uint8Array(o), c = 0;
|
|
142
|
+
for (let e of a) s.set(e, c), c += e.length;
|
|
143
|
+
return s;
|
|
144
|
+
}
|
|
145
|
+
var S = (e) => {
|
|
146
|
+
if (typeof CompressionStream != "function" || typeof DecompressionStream != "function") throw new r("unsupported", `${e} needs CompressionStream, which this runtime lacks; use a current browser or Node 18+`);
|
|
147
|
+
};
|
|
148
|
+
async function C(t, n = {}) {
|
|
149
|
+
if (!(t instanceof e)) throw TypeError("encodeChunk expects a VoxelChunk");
|
|
150
|
+
let { light: r = !0, compress: i = !0 } = n, { palette: a, bits: s, words: c, light: l } = t.pack(), u = typeof l == "number" ? l : l[0];
|
|
151
|
+
if (r && typeof l != "number") for (let e = 1; e < p && u >= 0; e++) l[e] !== u && (u = -1);
|
|
152
|
+
let d = new o(7 + a.length * 5 + c.length * 4 + (u < 0 ? p : 1));
|
|
153
|
+
d.u8(m), d.u8(r ? u < 0 ? 3 : h : 0), d.varint(a.length);
|
|
154
|
+
for (let e of a) d.varint(e);
|
|
155
|
+
d.u8(s);
|
|
156
|
+
for (let e of c) d.u32(e);
|
|
157
|
+
r && u < 0 ? d.raw(l) : r && d.u8(u);
|
|
158
|
+
let f = d.done();
|
|
159
|
+
if (!i) return f;
|
|
160
|
+
S("encodeChunk with compress");
|
|
161
|
+
let g = await x(new CompressionStream("deflate-raw"), f.subarray(2), Infinity), v = new Uint8Array(2 + g.length);
|
|
162
|
+
return v.set([m, f[1] | _]), v.set(g, 2), v;
|
|
163
|
+
}
|
|
164
|
+
async function w(t) {
|
|
165
|
+
if (!(t instanceof Uint8Array)) throw TypeError("decodeChunk expects the Uint8Array encodeChunk returned");
|
|
166
|
+
let n = new s(t, "Chunk data", "pass the bytes encodeChunk returned"), r = n.u8(), i = n.u8();
|
|
167
|
+
if (r !== m) throw n.fail(`has version ${r}, not ${m}`, "decode it with a matching release");
|
|
168
|
+
if (i > 7 || (i & 3) === g) throw n.fail(`has unknown flags ${i}`);
|
|
169
|
+
let a = t.subarray(2);
|
|
170
|
+
if (a.length > (i & _ ? y : v)) throw n.fail(`has ${t.length} bytes, more than any chunk needs`);
|
|
171
|
+
if (i & _) {
|
|
172
|
+
S("decodeChunk of deflated bytes");
|
|
173
|
+
let e = a;
|
|
174
|
+
if (a = await x(new DecompressionStream("deflate-raw"), e, v), await f(e, "deflate-raw")) throw RangeError("Chunk data holds a damaged deflate-raw stream; pass the bytes encodeChunk returned");
|
|
175
|
+
}
|
|
176
|
+
n = new s(a, "Chunk data", "pass the bytes encodeChunk returned");
|
|
177
|
+
let o = n.varint();
|
|
178
|
+
if (o < 1 || o > p) throw n.fail(`has a palette of ${o} states, not 1..32768`);
|
|
179
|
+
let c = new Uint32Array(o);
|
|
180
|
+
for (let e = 0; e < o; e++) c[e] = n.varint();
|
|
181
|
+
let l = n.u8();
|
|
182
|
+
if (![
|
|
183
|
+
0,
|
|
184
|
+
1,
|
|
185
|
+
2,
|
|
186
|
+
4,
|
|
187
|
+
8,
|
|
188
|
+
16
|
|
189
|
+
].includes(l)) throw n.fail(`has ${l}-bit cell indices, not 0, 1, 2, 4, 8 or 16`);
|
|
190
|
+
if (o > (l ? Math.min(1 << l, p) : 1)) throw n.fail(`has ${o} palette states for ${l}-bit indices`);
|
|
191
|
+
n.need(4096 * l);
|
|
192
|
+
let u = new Uint32Array(1024 * l);
|
|
193
|
+
for (let e = 0; e < u.length; e++) u[e] = n.u32();
|
|
194
|
+
let d = 0;
|
|
195
|
+
i & g ? (n.need(p), d = a.slice(n.at, n.at += p)) : i & h && (d = n.u8()), n.end();
|
|
196
|
+
try {
|
|
197
|
+
return e.unpack({
|
|
198
|
+
palette: c,
|
|
199
|
+
bits: l,
|
|
200
|
+
words: u,
|
|
201
|
+
light: d
|
|
202
|
+
});
|
|
203
|
+
} catch {
|
|
204
|
+
throw n.fail("has a cell index past its palette");
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function T(e) {
|
|
208
|
+
if (!e || typeof e != "object" || !Number.isInteger(e.length) || e.length % 4) throw TypeError("encodeBlockChanges expects a flat [x, y, z, state, ...] array of quadruples");
|
|
209
|
+
let n = e.length / 4, r = new Int32Array(n), a = new Int32Array(n), s = new Int32Array(n), c = new Uint16Array(n), l = new Uint32Array(n);
|
|
210
|
+
for (let i = 0; i < n; i++) {
|
|
211
|
+
let n = e[4 * i], o = e[4 * i + 1], u = e[4 * i + 2], d = e[4 * i + 3], f = (d | 0) === d ? d >>> 0 : d;
|
|
212
|
+
if ((n | 0) !== n || (o | 0) !== o || (u | 0) !== u) throw [
|
|
213
|
+
n,
|
|
214
|
+
o,
|
|
215
|
+
u
|
|
216
|
+
].every((e) => typeof e == "number") ? /* @__PURE__ */ RangeError(`Block change ${i} cell (${n}, ${o}, ${u}) is not three 32-bit integers; floor positions`) : /* @__PURE__ */ TypeError(`Block change ${i} cell coordinates must be numbers`);
|
|
217
|
+
if (f >>> 0 !== f) throw t(d);
|
|
218
|
+
r[i] = n >> 5, a[i] = o >> 5, s[i] = u >> 5, c[i] = n & 31 | (u & 31) << 5 | (o & 31) << 10, l[i] = f;
|
|
219
|
+
}
|
|
220
|
+
let u = (e, t) => r[e] === r[t] && a[e] === a[t] && s[e] === s[t] && c[e] === c[t], d = Array.from({ length: n }, (e, t) => t).sort((e, t) => r[e] - r[t] || a[e] - a[t] || s[e] - s[t] || c[e] - c[t]).filter((e, t, n) => t + 1 === n.length || !u(e, n[t + 1])), f = new o(5 + d.length * 4);
|
|
221
|
+
f.varint(d.length);
|
|
222
|
+
let p = -1;
|
|
223
|
+
for (let e of d) p >= 0 && r[e] === r[p] && a[e] === a[p] && s[e] === s[p] ? f.varint((c[e] - c[p]) * 2) : (f.varint(c[e] * 2 + 1), f.varint(i(r[e] - (p < 0 ? 0 : r[p]))), f.varint(i(a[e] - (p < 0 ? 0 : a[p]))), f.varint(i(s[e] - (p < 0 ? 0 : s[p])))), f.varint(l[e]), p = e;
|
|
224
|
+
return f.done();
|
|
225
|
+
}
|
|
226
|
+
function E(e) {
|
|
227
|
+
if (!(e instanceof Uint8Array)) throw TypeError("decodeBlockChanges expects the Uint8Array encodeBlockChanges returned");
|
|
228
|
+
let t = new s(e, "Block changes", "pass the bytes encodeBlockChanges returned"), n = t.varint();
|
|
229
|
+
if (n > (e.length - t.at) / 2) throw t.fail(`list ${n} changes in ${e.length} bytes`);
|
|
230
|
+
let r = new Int32Array(4 * n), i = 0, o = 0, c = 0, l = -1;
|
|
231
|
+
for (let e = 0; e < n; e++) {
|
|
232
|
+
let n = t.varint();
|
|
233
|
+
if (n % 2) {
|
|
234
|
+
let r = a(t.varint()), s = a(t.varint()), u = a(t.varint());
|
|
235
|
+
if (l >= 0 && !(r > 0 || r === 0 && (s > 0 || s === 0 && u > 0))) throw t.fail(`list chunks out of order at change ${e}`);
|
|
236
|
+
if (i += r, o += s, c += u, !b(i) || !b(o) || !b(c)) throw t.fail(`reach chunk (${i}, ${o}, ${c}) outside the 32-bit cell range at change ${e}`);
|
|
237
|
+
l = (n - 1) / 2;
|
|
238
|
+
} else {
|
|
239
|
+
if (l < 0) throw t.fail("start without a chunk");
|
|
240
|
+
if (!n) throw t.fail(`repeat a cell at change ${e}`);
|
|
241
|
+
l += n / 2;
|
|
242
|
+
}
|
|
243
|
+
if (l > 32767) throw t.fail(`reach cell index ${l}, past 32767, at change ${e}`);
|
|
244
|
+
r[4 * e] = i * 32 + (l & 31), r[4 * e + 1] = o * 32 + (l >> 10), r[4 * e + 2] = c * 32 + (l >> 5 & 31), r[4 * e + 3] = t.varint() | 0;
|
|
245
|
+
}
|
|
246
|
+
return t.end(), r;
|
|
247
|
+
}
|
|
248
|
+
function D(e, t) {
|
|
249
|
+
if (!(e instanceof n)) throw TypeError("applyBlockChanges expects a VoxelWorld");
|
|
250
|
+
return e.setBlocks(E(t));
|
|
251
|
+
}
|
|
252
|
+
var O = [
|
|
253
|
+
"position",
|
|
254
|
+
"rotation",
|
|
255
|
+
"u8",
|
|
256
|
+
"u16",
|
|
257
|
+
"f32"
|
|
258
|
+
], k = 512, A = 2 ** 30, j = /* @__PURE__ */ new WeakMap();
|
|
259
|
+
function M(e) {
|
|
260
|
+
let t = 0;
|
|
261
|
+
for (let n = 1; n < 4; n++) Math.abs(e[n]) > Math.abs(e[t]) && (t = n);
|
|
262
|
+
let n = (e[t] < 0 ? -1 : 1) / Math.hypot(e[0], e[1], e[2], e[3]), r = t * 2 ** 30, i = 20;
|
|
263
|
+
for (let a = 0; a < 4; a++) a !== t && (r += (511 + Math.min(511, Math.max(-511, Math.round(e[a] * n * Math.SQRT2 * 511)))) * 2 ** i, i -= 10);
|
|
264
|
+
return r;
|
|
265
|
+
}
|
|
266
|
+
function N(e) {
|
|
267
|
+
let t = e >>> 30, n = [
|
|
268
|
+
0,
|
|
269
|
+
0,
|
|
270
|
+
0,
|
|
271
|
+
0
|
|
272
|
+
], r = 20, i = 0;
|
|
273
|
+
for (let a = 0; a < 4; a++) a !== t && (n[a] = ((e >>> r & 1023) - 511) / 511 * Math.SQRT1_2, i += n[a] * n[a], r -= 10);
|
|
274
|
+
n[t] = Math.sqrt(Math.max(0, 1 - i));
|
|
275
|
+
let a = Math.hypot(n[0], n[1], n[2], n[3]);
|
|
276
|
+
return [
|
|
277
|
+
n[0] / a,
|
|
278
|
+
n[1] / a,
|
|
279
|
+
n[2] / a,
|
|
280
|
+
n[3] / a
|
|
281
|
+
];
|
|
282
|
+
}
|
|
283
|
+
var P = class {
|
|
284
|
+
constructor(e) {
|
|
285
|
+
if (!e || typeof e != "object") throw TypeError("EntityCodec expects a schema object such as {position: 'position', health: 'u8'}");
|
|
286
|
+
if (this.names = Object.keys(e), this.types = this.names.map((t) => e[t]), !this.names.length || this.names.length > 32) throw RangeError(`EntityCodec schema has ${this.names.length} fields; use 1..32`);
|
|
287
|
+
this.types.forEach((e, t) => {
|
|
288
|
+
if (!O.includes(e)) throw TypeError(`EntityCodec field ${this.names[t]} must be ${O.map((e) => `'${e}'`).join(", ")}, not ${e}`);
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
prepare(e, t = 0) {
|
|
292
|
+
let n = Object.freeze({});
|
|
293
|
+
return j.set(n, {
|
|
294
|
+
codec: this,
|
|
295
|
+
fields: this.quantize(t, e)
|
|
296
|
+
}), n;
|
|
297
|
+
}
|
|
298
|
+
encode(e, t) {
|
|
299
|
+
if (!(e instanceof Map) || t !== void 0 && !(t instanceof Map)) throw TypeError("EntityCodec.encode expects Maps of entity id -> entity");
|
|
300
|
+
let n = new Set(e.keys());
|
|
301
|
+
if (t) for (let e of t.keys()) n.add(e);
|
|
302
|
+
for (let e of n) if (e >>> 0 !== e) throw typeof e == "number" ? /* @__PURE__ */ RangeError(`Entity id ${e} is not an integer in 0..4294967295; key entities by u32 ids`) : /* @__PURE__ */ TypeError("Entity ids must be numbers; key entities by u32 ids");
|
|
303
|
+
let r = new o(), a = -1, s = 0;
|
|
304
|
+
for (let o of new Uint32Array(n).sort()) {
|
|
305
|
+
let n = e.has(o) ? this.fields(o, e.get(o)) : void 0, c = t?.has(o) ? this.fields(o, t.get(o)) : void 0, l = 0;
|
|
306
|
+
if (n) for (let e = 0; e < n.length; e++) {
|
|
307
|
+
let t = n[e], r = c?.[e], i;
|
|
308
|
+
if (Array.isArray(t)) {
|
|
309
|
+
let e = r;
|
|
310
|
+
i = !e || t[0] !== e[0] || t[1] !== e[1] || t[2] !== e[2];
|
|
311
|
+
} else i = t !== r;
|
|
312
|
+
i && (l += 2 ** e);
|
|
313
|
+
}
|
|
314
|
+
(!n || l) && (r.varint(o - a - 1), r.varint(l), a = o, s++, n && this.types.forEach((e, t) => {
|
|
315
|
+
if (!(l >>> t & 1)) return;
|
|
316
|
+
let a = n[t];
|
|
317
|
+
if (e === "position") for (let e = 0; e < 3; e++) r.varint(i(a[e] - (c ? c[t][e] : 0)));
|
|
318
|
+
else e === "rotation" ? r.u32(a) : e === "u8" ? r.u8(a) : e === "u16" ? r.u16(a) : r.f32(a);
|
|
319
|
+
}));
|
|
320
|
+
}
|
|
321
|
+
let c = new o(5 + r.at);
|
|
322
|
+
return c.varint(s), c.raw(r.bytes.subarray(0, r.at)), c.done();
|
|
323
|
+
}
|
|
324
|
+
decode(e, t) {
|
|
325
|
+
if (!(e instanceof Uint8Array)) throw TypeError("EntityCodec.decode expects a Uint8Array from encode");
|
|
326
|
+
if (t !== void 0 && !(t instanceof Map)) throw TypeError("EntityCodec.decode baseline must be a Map of entity id -> entity");
|
|
327
|
+
let n = new s(e, "Entity data", "pass the bytes EntityCodec.encode returned"), r = n.varint(), i = new Map(t), o = 2 ** this.types.length - 1;
|
|
328
|
+
if (r > (e.length - n.at) / 2) throw n.fail(`lists ${r} entities in ${e.length} bytes`);
|
|
329
|
+
let c = -1;
|
|
330
|
+
for (let e = 0; e < r; e++) {
|
|
331
|
+
if (c += n.varint() + 1, c > 4294967295) throw n.fail(`has entity id ${c}, past 4294967295`);
|
|
332
|
+
let e = n.varint(), r = t?.get(c);
|
|
333
|
+
if (e > o) throw n.fail(`sets fields past the schema's ${this.types.length} for entity ${c}`);
|
|
334
|
+
if (!r && e !== o) throw n.fail(`${e ? "updates" : "removes"} entity ${c}, which the baseline lacks`, "decode against the baseline the bytes were encoded from");
|
|
335
|
+
if (!e) {
|
|
336
|
+
i.delete(c);
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
let s = { ...r };
|
|
340
|
+
this.types.forEach((t, i) => {
|
|
341
|
+
if (!(e >>> i & 1)) return;
|
|
342
|
+
let o = this.names[i];
|
|
343
|
+
if (t === "position") {
|
|
344
|
+
let e = r ? this.value(c, o, t, r[o]) : [
|
|
345
|
+
0,
|
|
346
|
+
0,
|
|
347
|
+
0
|
|
348
|
+
], i = [
|
|
349
|
+
0,
|
|
350
|
+
0,
|
|
351
|
+
0
|
|
352
|
+
];
|
|
353
|
+
for (let t = 0; t < 3; t++) {
|
|
354
|
+
let r = e[t] + a(n.varint());
|
|
355
|
+
if (Math.abs(r) >= A) throw n.fail(`moves entity ${c} to ${r / k}, out of range`);
|
|
356
|
+
i[t] = r / k;
|
|
357
|
+
}
|
|
358
|
+
s[o] = i;
|
|
359
|
+
} else if (t === "rotation") s[o] = N(n.u32());
|
|
360
|
+
else if (t === "u8") s[o] = n.u8();
|
|
361
|
+
else if (t === "u16") s[o] = n.u16();
|
|
362
|
+
else {
|
|
363
|
+
let e = n.f32();
|
|
364
|
+
if (!Number.isFinite(e)) throw n.fail(`has a non-finite ${o} for entity ${c}`);
|
|
365
|
+
s[o] = e;
|
|
366
|
+
}
|
|
367
|
+
}), i.set(c, s);
|
|
368
|
+
}
|
|
369
|
+
return n.end(), i;
|
|
370
|
+
}
|
|
371
|
+
fields(e, t) {
|
|
372
|
+
let n = j.get(t);
|
|
373
|
+
if (!n) return this.quantize(e, t);
|
|
374
|
+
if (n.codec !== this) throw TypeError(`Entity ${e} was prepared by another EntityCodec; prepare it with the one that encodes`);
|
|
375
|
+
return n.fields;
|
|
376
|
+
}
|
|
377
|
+
quantize(e, t) {
|
|
378
|
+
if (!t || typeof t != "object") throw TypeError(`Entity ${e} must be an object of its fields`);
|
|
379
|
+
return this.names.map((n, r) => this.value(e, n, this.types[r], t[n]));
|
|
380
|
+
}
|
|
381
|
+
value(e, t, n, r) {
|
|
382
|
+
let i = `Entity ${e} ${t}`;
|
|
383
|
+
if (n === "position" || n === "rotation") {
|
|
384
|
+
let e = n === "position" ? 3 : 4, t = r;
|
|
385
|
+
if (!t || typeof t != "object" || t.length !== e || !Array.from(t).every((e) => typeof e == "number")) throw TypeError(`${i} must be ${e} numbers`);
|
|
386
|
+
let a = t;
|
|
387
|
+
if (n === "rotation") {
|
|
388
|
+
let e = Math.hypot(a[0], a[1], a[2], a[3]);
|
|
389
|
+
if (!(e > 0 && e < Infinity)) throw RangeError(`${i} is not a rotation; pass a unit Quat`);
|
|
390
|
+
return M(a);
|
|
391
|
+
}
|
|
392
|
+
let o = [
|
|
393
|
+
Math.round(a[0] * k),
|
|
394
|
+
Math.round(a[1] * k),
|
|
395
|
+
Math.round(a[2] * k)
|
|
396
|
+
];
|
|
397
|
+
if (!o.every((e) => Math.abs(e) < A)) throw RangeError(`${i} (${a[0]}, ${a[1]}, ${a[2]}) is out of range; keep |coordinates| < 2097152`);
|
|
398
|
+
return o;
|
|
399
|
+
}
|
|
400
|
+
if (typeof r != "number") throw TypeError(`${i} must be a number`);
|
|
401
|
+
if (n === "f32") {
|
|
402
|
+
let e = Math.fround(r);
|
|
403
|
+
if (!Number.isFinite(e)) throw RangeError(`${i} ${r} is not a finite float32; pass a finite number`);
|
|
404
|
+
return e;
|
|
405
|
+
}
|
|
406
|
+
let a = n === "u8" ? 255 : 65535;
|
|
407
|
+
if (!Number.isInteger(r) || r < 0 || r > a) throw RangeError(`${i} ${r} is not an integer in 0..${a}; pass a whole number in that range`);
|
|
408
|
+
return r;
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
//#endregion
|
|
412
|
+
export { T as a, w as i, D as n, C as o, E as r, P as t };
|
package/dist/codec.d.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { Quat, Vec3 } from '@margoengine/math';
|
|
2
|
+
import { VoxelChunk } from './chunk.js';
|
|
3
|
+
import { VoxelWorld } from './world.js';
|
|
4
|
+
/**
|
|
5
|
+
* Encodes `chunk` (block states and, unless `options.light` is false, light bytes) for a save or the network. The
|
|
6
|
+
* chunk is read before this returns, so it may change while compression runs. Bytes: u8 version (1), u8 flags (1
|
|
7
|
+
* light, 2 dense light, 4 deflated), then (deflate-raw when flagged) a varint palette length, that many varint
|
|
8
|
+
* states, u8 index bits (0, 1, 2, 4, 8, 16), 1,024 x bits little-endian u32 index words (the layout of
|
|
9
|
+
* {@link PackedChunk}) and the light: one byte for every cell, or 32,768 bytes.
|
|
10
|
+
* @param options `light` (default true) keeps light bytes (without them every cell decodes with light 0);
|
|
11
|
+
* `compress` (default true) deflates with the browser's `CompressionStream('deflate-raw')`.
|
|
12
|
+
* @throws TypeError (rejects) when `chunk` is not a VoxelChunk.
|
|
13
|
+
* @throws MargoError `'unsupported'` (rejects) when compressing without CompressionStream.
|
|
14
|
+
*/
|
|
15
|
+
export declare function encodeChunk(chunk: VoxelChunk, options?: {
|
|
16
|
+
light?: boolean;
|
|
17
|
+
compress?: boolean;
|
|
18
|
+
}): Promise<Uint8Array>;
|
|
19
|
+
/**
|
|
20
|
+
* The chunk that {@link encodeChunk} wrote into `bytes` (deflated or not, with or without light).
|
|
21
|
+
* @throws TypeError (rejects) when `bytes` is not a Uint8Array.
|
|
22
|
+
* @throws RangeError (rejects) for anything but a complete chunk: truncated, longer than its contents, another
|
|
23
|
+
* version, unknown flags, a palette or index width out of range, a cell index past the palette, a deflate stream
|
|
24
|
+
* that is damaged or inflates past the largest possible chunk (262,148 bytes).
|
|
25
|
+
* @throws MargoError `'unsupported'` (rejects) for deflated bytes without DecompressionStream.
|
|
26
|
+
*/
|
|
27
|
+
export declare function decodeChunk(bytes: Uint8Array): Promise<VoxelChunk>;
|
|
28
|
+
/**
|
|
29
|
+
* Encodes block changes, a flat `[x, y, z, state, ...]` list as {@link VoxelWorld.setBlocks} takes (an Int32Array
|
|
30
|
+
* works: negative states are their u32 bit pattern), for the network or a save. A cell listed twice keeps its last
|
|
31
|
+
* state. Bytes: varint count, then per change, sorted by chunk (cx, cy, cz) and cell index: varint `step`, then for
|
|
32
|
+
* the first change in a chunk (`step` odd, `step >> 1` its cell index) three zigzag varints from the previous chunk
|
|
33
|
+
* (the first from 0, 0, 0), otherwise (`step` even) `step >> 1` is the offset from the previous cell index; then the
|
|
34
|
+
* varint state. Changes within one chunk typically cost 2-4 bytes each.
|
|
35
|
+
* @throws TypeError when `changes` is not an array-like of quadruples or holds a non-number.
|
|
36
|
+
* @throws RangeError for a cell coordinate that is not a 32-bit integer or a state that is not a u32.
|
|
37
|
+
*/
|
|
38
|
+
export declare function encodeBlockChanges(changes: ArrayLike<number>): Uint8Array<ArrayBuffer>;
|
|
39
|
+
/**
|
|
40
|
+
* The changes {@link encodeBlockChanges} wrote, as a flat `[x, y, z, state, ...]` Int32Array sorted by chunk and
|
|
41
|
+
* cell index (states of 2^31 and up read negative: their u32 bit pattern, as {@link VoxelWorld.setBlocks} takes).
|
|
42
|
+
* @throws TypeError when `bytes` is not a Uint8Array.
|
|
43
|
+
* @throws RangeError for truncated bytes, trailing bytes, a count the bytes cannot hold, cells out of order or
|
|
44
|
+
* repeated, a cell index past 32,767, or a chunk outside the 32-bit cell range.
|
|
45
|
+
*/
|
|
46
|
+
export declare function decodeBlockChanges(bytes: Uint8Array): Int32Array;
|
|
47
|
+
/**
|
|
48
|
+
* Decodes `bytes` ({@link encodeBlockChanges}) and applies them with `world.setBlocks`: all or none, with its dirty
|
|
49
|
+
* marking and change events. Returns how many cells changed.
|
|
50
|
+
* @throws TypeError when `world` is not a VoxelWorld; otherwise as {@link decodeBlockChanges} and
|
|
51
|
+
* {@link VoxelWorld.setBlocks} (a y outside the world, an undefined block id), before any cell changes.
|
|
52
|
+
*/
|
|
53
|
+
export declare function applyBlockChanges(world: VoxelWorld, bytes: Uint8Array): number;
|
|
54
|
+
/**
|
|
55
|
+
* Type of one {@link EntityCodec} field: `'position'` (a Vec3 in blocks, sent as the change from the baseline in
|
|
56
|
+
* 1/512-block steps; |coordinate| < 2,097,152), `'rotation'` (a unit quaternion, 32 bits smallest-three: about 0.1
|
|
57
|
+
* degree), `'u8'`, `'u16'` (integers) or `'f32'` (a finite float32).
|
|
58
|
+
*/
|
|
59
|
+
export type EntityField = 'position' | 'rotation' | 'u8' | 'u16' | 'f32';
|
|
60
|
+
/** One entity's values under {@link EntityCodec} schema `S`: a Vec3 per position, a Quat per rotation, numbers. */
|
|
61
|
+
export type EntityState<S extends Readonly<Record<string, EntityField>>> = {
|
|
62
|
+
[K in keyof S]: S[K] extends 'position' ? Vec3 : S[K] extends 'rotation' ? Quat : number;
|
|
63
|
+
};
|
|
64
|
+
declare const preparedBrand: unique symbol;
|
|
65
|
+
/**
|
|
66
|
+
* An entity state quantized once by {@link EntityCodec.prepare}: pass it to {@link EntityCodec.encode} in place of the
|
|
67
|
+
* state for any number of receivers. Opaque; it holds the values as they were when prepared.
|
|
68
|
+
*/
|
|
69
|
+
export interface PreparedEntity {
|
|
70
|
+
readonly [preparedBrand]: true;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Schema-driven entity snapshots for the network: {@link EntityCodec.encode} writes only the entities and fields that
|
|
74
|
+
* differ from a baseline (the last state the receiver acknowledged), {@link EntityCodec.decode} rebuilds the full state
|
|
75
|
+
* from those bytes and the same baseline. Both ends build the codec from the same schema (field order is the key
|
|
76
|
+
* order of the schema object). Positions round to 1/512 block (error <= 1/1024), rotations to smallest-three 32 bits.
|
|
77
|
+
* Bytes: varint count, then per entity in id order: varint id offset (`id - previous id - 1`, the first from -1), a
|
|
78
|
+
* varint field mask (bit i: schema field i follows; 0: the entity was removed), the set fields in schema order:
|
|
79
|
+
* position three zigzag varints (steps from the baseline's, from 0 for a new entity), rotation u32, u8, u16, f32 (LE).
|
|
80
|
+
*
|
|
81
|
+
* ```ts
|
|
82
|
+
* const codec = new EntityCodec({ position: 'position', rotation: 'rotation', health: 'u8' });
|
|
83
|
+
* const bytes = codec.encode(players, acked); // Maps of entity id (u32) -> {position, rotation, health}
|
|
84
|
+
* const state = codec.decode(bytes, received); // the receiver's copy of `acked`
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare class EntityCodec<const S extends Readonly<Record<string, EntityField>>> {
|
|
88
|
+
private readonly names;
|
|
89
|
+
private readonly types;
|
|
90
|
+
/**
|
|
91
|
+
* @throws TypeError when `schema` is not an object of field name -> {@link EntityField}.
|
|
92
|
+
* @throws RangeError for a schema with no fields or more than 32.
|
|
93
|
+
*/
|
|
94
|
+
constructor(schema: S);
|
|
95
|
+
/**
|
|
96
|
+
* `entity` checked and quantized once, for a server that sends the same entity to many receivers: each
|
|
97
|
+
* {@link EntityCodec.encode} that is given the result skips that work and writes the same bytes as for `entity`.
|
|
98
|
+
* Later changes to `entity` do not reach the result; prepare again for a new state.
|
|
99
|
+
* @throws as {@link EntityCodec.encode} for a malformed entity (`id` only names it in the message).
|
|
100
|
+
*/
|
|
101
|
+
prepare(entity: EntityState<S>, id?: number): PreparedEntity;
|
|
102
|
+
/**
|
|
103
|
+
* The entities of `entities` that are new or differ from `baseline` (same id) in any field after quantizing,
|
|
104
|
+
* with only their changed fields, plus the removal of each `baseline` entity `entities` lacks. Without a
|
|
105
|
+
* baseline every entity is sent in full. Either Map may hold {@link EntityCodec.prepare} results instead of states.
|
|
106
|
+
* @throws TypeError when `entities` or `baseline` is not a Map, an entity is not an object, a field has the
|
|
107
|
+
* wrong type, or a prepared entity comes from another codec.
|
|
108
|
+
* @throws RangeError for an id that is not a u32, a non-finite value, a position coordinate of 2,097,152 or more
|
|
109
|
+
* in size, a zero-length rotation, or an integer out of its range.
|
|
110
|
+
*/
|
|
111
|
+
encode(entities: ReadonlyMap<number, EntityState<S> | PreparedEntity>, baseline?: ReadonlyMap<number, EntityState<S> | PreparedEntity>): Uint8Array<ArrayBuffer>;
|
|
112
|
+
/**
|
|
113
|
+
* The full state: `baseline` (the receiver's copy of the baseline `bytes` were encoded against) with the entities
|
|
114
|
+
* in `bytes` added, updated or removed. Unchanged entities and fields are `baseline`'s own values.
|
|
115
|
+
* @throws TypeError when `bytes` is not a Uint8Array or `baseline` is not a Map.
|
|
116
|
+
* @throws RangeError for truncated or trailing bytes, a count the bytes cannot hold, an id past 2^32 - 1, mask
|
|
117
|
+
* bits past the schema, a non-finite f32, a position out of range, or an entity the baseline lacks that is
|
|
118
|
+
* removed or not sent in full (decoded against the wrong baseline).
|
|
119
|
+
*/
|
|
120
|
+
decode(bytes: Uint8Array, baseline?: ReadonlyMap<number, EntityState<S>>): Map<number, EntityState<S>>;
|
|
121
|
+
private fields;
|
|
122
|
+
private quantize;
|
|
123
|
+
private value;
|
|
124
|
+
}
|
|
125
|
+
export {};
|
package/dist/codec.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as EntityCodec, n as applyBlockChanges, r as decodeBlockChanges, i as decodeChunk, a as encodeBlockChanges, o as encodeChunk } from "./codec-BmXhcsFt.js";
|