@margoengine/netcode 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 +1387 -2
- package/dist/client.d.ts +167 -0
- package/dist/clock.d.ts +29 -0
- package/dist/extrapolate.d.ts +42 -0
- package/dist/extrapolate.js +25 -0
- package/dist/game.d.ts +68 -0
- package/dist/generate-rust.d.ts +14 -0
- package/dist/generate-rust.js +184 -0
- package/dist/generate.d.ts +25 -0
- package/dist/generate.js +156 -0
- package/dist/hitboxes.d.ts +74 -0
- package/dist/hitboxes.js +124 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +457 -0
- package/dist/interest.d.ts +101 -0
- package/dist/interest.js +166 -0
- package/dist/interpolation-delay.d.ts +114 -0
- package/dist/interpolation-delay.js +79 -0
- package/dist/interpolation-fields.d.ts +117 -0
- package/dist/interpolation-fields.js +147 -0
- package/dist/interpolation-timeline.d.ts +100 -0
- package/dist/interpolation-timeline.js +122 -0
- package/dist/interpolation.d.ts +39 -0
- package/dist/interpolation.js +61 -0
- package/dist/lag.d.ts +62 -0
- package/dist/lobby.d.ts +77 -0
- package/dist/lobby.js +96 -0
- package/dist/loopback.d.ts +92 -0
- package/dist/media.d.ts +215 -0
- package/dist/media.js +305 -0
- package/dist/migrate.d.ts +113 -0
- package/dist/migrate.js +289 -0
- package/dist/node.d.ts +101 -0
- package/dist/node.js +166 -0
- package/dist/physics.d.ts +133 -0
- package/dist/physics.js +169 -0
- package/dist/presence.d.ts +156 -0
- package/dist/presence.js +226 -0
- package/dist/protocol-asi_qndb.js +122 -0
- package/dist/protocol.d.ts +75 -0
- package/dist/relay.d.ts +152 -0
- package/dist/relay.js +235 -0
- package/dist/rollback.d.ts +131 -0
- package/dist/rollback.js +194 -0
- package/dist/rooms.d.ts +366 -0
- package/dist/rooms.js +479 -0
- package/dist/schema-CMfqK4Gu.js +616 -0
- package/dist/schema.d.ts +263 -0
- package/dist/server-DAVzHcWW.js +203 -0
- package/dist/server.d.ts +147 -0
- package/dist/shared/bytes.d.ts +38 -0
- package/dist/signal.d.ts +156 -0
- package/dist/signal.js +261 -0
- package/dist/smoothing.d.ts +76 -0
- package/dist/smoothing.js +155 -0
- package/dist/transport-DeQodwyv.js +49 -0
- package/dist/transport.d.ts +76 -0
- package/dist/webrtc.d.ts +32 -0
- package/dist/webrtc.js +64 -0
- package/dist/websocket.d.ts +27 -0
- package/dist/websocket.js +42 -0
- package/dist/webtransport.d.ts +83 -0
- package/dist/webtransport.js +120 -0
- package/package.json +138 -4
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A fixed-size ring of strictly increasing times. It keeps the order and finds the entries around a moment; what an
|
|
3
|
+
* entry holds lives in your own storage at index {@link TimeRing.push} returned. `times` may be that storage: with a
|
|
4
|
+
* `stride`, the time of slot `s` sits at `times[s * stride]`.
|
|
5
|
+
*/
|
|
6
|
+
export declare class TimeRing {
|
|
7
|
+
readonly capacity: number;
|
|
8
|
+
readonly times: Float64Array;
|
|
9
|
+
readonly stride: number;
|
|
10
|
+
/** First of the two slots around the last {@link TimeRing.locate}. */
|
|
11
|
+
a: number;
|
|
12
|
+
/** Second of the two slots. Equal to `a` when the moment is on an entry or outside the ring. */
|
|
13
|
+
b: number;
|
|
14
|
+
/** How far from `a` to `b` the moment lies, 0 to 1. */
|
|
15
|
+
u: number;
|
|
16
|
+
/** How far past the newest entry the moment lies, 0 unless it is. */
|
|
17
|
+
past: number;
|
|
18
|
+
/** Entries held. */
|
|
19
|
+
count: number;
|
|
20
|
+
private head;
|
|
21
|
+
/** @throws RangeError for a capacity that is not an integer from 2 to 65536, or storage too small for it. */
|
|
22
|
+
constructor(capacity: number, times?: Float64Array, stride?: number);
|
|
23
|
+
private slot;
|
|
24
|
+
private time;
|
|
25
|
+
/** The newest time (NaN while empty). */
|
|
26
|
+
get newest(): number;
|
|
27
|
+
/** The oldest time (NaN while empty). */
|
|
28
|
+
get oldest(): number;
|
|
29
|
+
/**
|
|
30
|
+
* Adds `time`, replacing the oldest entry when full.
|
|
31
|
+
* @returns The entry's slot, or -1 for a time not newer than the newest, which changes nothing.
|
|
32
|
+
*/
|
|
33
|
+
push(time: number): number;
|
|
34
|
+
private search;
|
|
35
|
+
/** The slot holding exactly `time`, or -1. */
|
|
36
|
+
find(time: number): number;
|
|
37
|
+
/**
|
|
38
|
+
* Sets {@link TimeRing.a}, {@link TimeRing.b}, {@link TimeRing.u} and {@link TimeRing.past} for the moment `time`.
|
|
39
|
+
* Before the oldest entry both slots are the oldest, past the newest both are the newest.
|
|
40
|
+
* @returns False while empty.
|
|
41
|
+
*/
|
|
42
|
+
locate(time: number): boolean;
|
|
43
|
+
/** The slot of the entry before the newest, or -1 with fewer than two. */
|
|
44
|
+
get previous(): number;
|
|
45
|
+
/** The slot of the oldest entry, or -1 while empty. */
|
|
46
|
+
get first(): number;
|
|
47
|
+
/** The slot of the newest entry, or -1 while empty. */
|
|
48
|
+
get last(): number;
|
|
49
|
+
/** Drops every entry at or before `time`. */
|
|
50
|
+
dropThrough(time: number): void;
|
|
51
|
+
/** Drops every entry. */
|
|
52
|
+
clear(): void;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A {@link TimeRing} that holds a value for each entry, for snapshots that are objects and not rows of numbers.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { Timeline } from '@margoengine/netcode/interpolation-timeline';
|
|
60
|
+
*
|
|
61
|
+
* const states = new Timeline<{ x: number }>(32);
|
|
62
|
+
* states.add(100, { x: 1 });
|
|
63
|
+
* states.add(102, { x: 3 });
|
|
64
|
+
* if (states.locate(101) && states.from && states.to) {
|
|
65
|
+
* const x = states.from.x + (states.to.x - states.from.x) * states.u;
|
|
66
|
+
* console.log(x);
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare class Timeline<T> extends TimeRing {
|
|
71
|
+
private readonly values;
|
|
72
|
+
/** @throws RangeError for a capacity that is not an integer from 2 to 65536. */
|
|
73
|
+
constructor(capacity: number);
|
|
74
|
+
/** Adds `value` at `time`. @returns False for a time not newer than the newest, which changes nothing. */
|
|
75
|
+
add(time: number, value: T): boolean;
|
|
76
|
+
/** The value at exactly `time`, if held. */
|
|
77
|
+
get(time: number): T | undefined;
|
|
78
|
+
/** Replaces the value at exactly `time`. @returns False when no entry has that time. */
|
|
79
|
+
replace(time: number, value: T): boolean;
|
|
80
|
+
/** The value at {@link TimeRing.a}, after {@link TimeRing.locate}. */
|
|
81
|
+
get from(): T | undefined;
|
|
82
|
+
/** The value at {@link TimeRing.b}, after {@link TimeRing.locate}. */
|
|
83
|
+
get to(): T | undefined;
|
|
84
|
+
/** The newest value, if any. */
|
|
85
|
+
get latest(): T | undefined;
|
|
86
|
+
dropThrough(time: number): void;
|
|
87
|
+
clear(): void;
|
|
88
|
+
}
|
|
89
|
+
/** The blend of angles `a` and `b` in radians at `t`, the short way round. The result is not wrapped. */
|
|
90
|
+
export declare function blendAngle(a: number, b: number, t: number): number;
|
|
91
|
+
/**
|
|
92
|
+
* Writes `value` as a unit quaternion at `out[offset]`, safely for overflowing or subnormal magnitudes.
|
|
93
|
+
* @throws RangeError for a zero quaternion.
|
|
94
|
+
*/
|
|
95
|
+
export declare function normalizeQuaternion(value: ArrayLike<number>, out: Float64Array, offset: number): void;
|
|
96
|
+
/**
|
|
97
|
+
* Shortest-arc slerp of the unit quaternions at `data[a]` and `data[b]`, written to `out[offset]`.
|
|
98
|
+
* Near-parallel pairs fall back to a normalized blend.
|
|
99
|
+
*/
|
|
100
|
+
export declare function slerpInto(out: Float64Array, offset: number, data: Float64Array, a: number, b: number, u: number): void;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
//#region src/interpolation-timeline.ts
|
|
2
|
+
var e = class {
|
|
3
|
+
constructor(e, t = new Float64Array(e), n = 1) {
|
|
4
|
+
if (this.capacity = e, this.times = t, this.stride = n, this.a = 0, this.b = 0, this.u = 0, this.past = 0, this.count = 0, this.head = 0, !Number.isInteger(e) || e < 2 || e > 65536) throw RangeError(`TimeRing capacity is ${e}; pass an integer from 2 to 65536`);
|
|
5
|
+
if (!Number.isInteger(n) || n < 1 || t.length < e * n) throw RangeError(`TimeRing storage holds ${t.length} values; pass ${e} times the stride`);
|
|
6
|
+
}
|
|
7
|
+
slot(e) {
|
|
8
|
+
return (this.head - this.count + e + 2 * this.capacity) % this.capacity;
|
|
9
|
+
}
|
|
10
|
+
time(e) {
|
|
11
|
+
return this.times[this.slot(e) * this.stride];
|
|
12
|
+
}
|
|
13
|
+
get newest() {
|
|
14
|
+
return this.count ? this.time(this.count - 1) : NaN;
|
|
15
|
+
}
|
|
16
|
+
get oldest() {
|
|
17
|
+
return this.count ? this.time(0) : NaN;
|
|
18
|
+
}
|
|
19
|
+
push(e) {
|
|
20
|
+
if (this.count && e <= this.newest) return -1;
|
|
21
|
+
let t = this.head;
|
|
22
|
+
return this.times[t * this.stride] = e, this.head = (t + 1) % this.capacity, this.count = Math.min(this.capacity, this.count + 1), t;
|
|
23
|
+
}
|
|
24
|
+
search(e) {
|
|
25
|
+
let t = 0, n = this.count - 1;
|
|
26
|
+
for (; t < n;) {
|
|
27
|
+
let r = t + n >>> 1;
|
|
28
|
+
this.time(r) >= e ? n = r : t = r + 1;
|
|
29
|
+
}
|
|
30
|
+
return t;
|
|
31
|
+
}
|
|
32
|
+
find(e) {
|
|
33
|
+
if (!this.count) return -1;
|
|
34
|
+
let t = this.search(e);
|
|
35
|
+
return this.time(t) === e ? this.slot(t) : -1;
|
|
36
|
+
}
|
|
37
|
+
locate(e) {
|
|
38
|
+
if (!this.count) return !1;
|
|
39
|
+
let t = this.search(e), n = this.slot(t), r = this.times[n * this.stride];
|
|
40
|
+
if (this.b = n, t === 0 || e >= r) return this.a = n, this.u = 0, this.past = Math.max(0, e - r), !0;
|
|
41
|
+
this.a = this.slot(t - 1);
|
|
42
|
+
let i = this.times[this.a * this.stride];
|
|
43
|
+
return this.u = (e - i) / (r - i), this.past = 0, !0;
|
|
44
|
+
}
|
|
45
|
+
get previous() {
|
|
46
|
+
return this.count > 1 ? this.slot(this.count - 2) : -1;
|
|
47
|
+
}
|
|
48
|
+
get first() {
|
|
49
|
+
return this.count ? this.slot(0) : -1;
|
|
50
|
+
}
|
|
51
|
+
get last() {
|
|
52
|
+
return this.count ? this.slot(this.count - 1) : -1;
|
|
53
|
+
}
|
|
54
|
+
dropThrough(e) {
|
|
55
|
+
for (; this.count && this.oldest <= e;) this.count--;
|
|
56
|
+
}
|
|
57
|
+
clear() {
|
|
58
|
+
this.count = this.head = 0;
|
|
59
|
+
}
|
|
60
|
+
}, t = class extends e {
|
|
61
|
+
constructor(e) {
|
|
62
|
+
super(e), this.values = Array(e).fill(void 0);
|
|
63
|
+
}
|
|
64
|
+
add(e, t) {
|
|
65
|
+
let n = this.push(e);
|
|
66
|
+
return n < 0 ? !1 : (this.values[n] = t, !0);
|
|
67
|
+
}
|
|
68
|
+
get(e) {
|
|
69
|
+
let t = this.find(e);
|
|
70
|
+
return t < 0 ? void 0 : this.values[t];
|
|
71
|
+
}
|
|
72
|
+
replace(e, t) {
|
|
73
|
+
let n = this.find(e);
|
|
74
|
+
return n < 0 ? !1 : (this.values[n] = t, !0);
|
|
75
|
+
}
|
|
76
|
+
get from() {
|
|
77
|
+
return this.values[this.a];
|
|
78
|
+
}
|
|
79
|
+
get to() {
|
|
80
|
+
return this.values[this.b];
|
|
81
|
+
}
|
|
82
|
+
get latest() {
|
|
83
|
+
return this.count ? this.values[this.last] : void 0;
|
|
84
|
+
}
|
|
85
|
+
dropThrough(e) {
|
|
86
|
+
for (; this.count && this.oldest <= e;) this.values[this.first] = void 0, this.count--;
|
|
87
|
+
}
|
|
88
|
+
clear() {
|
|
89
|
+
this.values.fill(void 0), super.clear();
|
|
90
|
+
}
|
|
91
|
+
}, n = Math.PI * 2;
|
|
92
|
+
function r(e, t, r) {
|
|
93
|
+
let i = (t - e) % n;
|
|
94
|
+
return i > Math.PI ? i -= n : i <= -Math.PI && (i += n), e + i * r;
|
|
95
|
+
}
|
|
96
|
+
function i(e, t, n) {
|
|
97
|
+
let r = Math.hypot(e[0], e[1], e[2], e[3]);
|
|
98
|
+
if (!r) throw RangeError("Snapshot rotation has zero length; pass a non-zero quaternion [x, y, z, w].");
|
|
99
|
+
let i = Number.isFinite(r) && r >= 22250738585072014e-324 ? 1 : Math.max(Math.abs(e[0]), Math.abs(e[1]), Math.abs(e[2]), Math.abs(e[3]));
|
|
100
|
+
if (i === 1) for (let i = 0; i < 4; i++) t[n + i] = e[i] / r;
|
|
101
|
+
else {
|
|
102
|
+
r = Math.hypot(e[0] / i, e[1] / i, e[2] / i, e[3] / i);
|
|
103
|
+
for (let a = 0; a < 4; a++) t[n + a] = e[a] / i / r;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function a(e, t, n, r, i, a) {
|
|
107
|
+
let o = 0;
|
|
108
|
+
for (let e = 0; e < 4; e++) o += n[r + e] * n[i + e];
|
|
109
|
+
let s = o < 0 ? -1 : 1;
|
|
110
|
+
o = Math.abs(o);
|
|
111
|
+
let c = 1 - a, l = a;
|
|
112
|
+
if (o <= .9995) {
|
|
113
|
+
let e = Math.acos(o), t = Math.sin(e);
|
|
114
|
+
c = Math.sin((1 - a) * e) / t, l = Math.sin(a * e) / t;
|
|
115
|
+
}
|
|
116
|
+
let u = 0;
|
|
117
|
+
for (let a = 0; a < 4; a++) e[t + a] = c * n[r + a] + l * s * n[i + a], u += e[t + a] ** 2;
|
|
118
|
+
u = Math.sqrt(u);
|
|
119
|
+
for (let n = 0; n < 4; n++) e[t + n] /= u;
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
export { e as TimeRing, t as Timeline, r as blendAngle, i as normalizeQuaternion, a as slerpInto };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** One networked transform state. */
|
|
2
|
+
export interface Snapshot {
|
|
3
|
+
/** x, y, z in world units. */
|
|
4
|
+
position: Float64Array;
|
|
5
|
+
/** Unit quaternion x, y, z, w. */
|
|
6
|
+
rotation: Float64Array;
|
|
7
|
+
/** x, y, z in world units per second. */
|
|
8
|
+
velocity: Float64Array;
|
|
9
|
+
}
|
|
10
|
+
/** A zeroed snapshot with the identity rotation, for use as a reusable `sample` output. */
|
|
11
|
+
export declare function createSnapshot(): Snapshot;
|
|
12
|
+
/**
|
|
13
|
+
* Ring buffer of timestamped snapshots for interpolating remote objects. `capacity` is the number of
|
|
14
|
+
* snapshots kept (an integer 2..65536, default 32); `maxPrediction` caps extrapolation past the newest
|
|
15
|
+
* snapshot in seconds (0..10, default 0.25). Out-of-range arguments throw a RangeError.
|
|
16
|
+
*/
|
|
17
|
+
export declare class SnapshotBuffer {
|
|
18
|
+
readonly capacity: number;
|
|
19
|
+
readonly maxPrediction: number;
|
|
20
|
+
/** 11 floats per slot: time, position xyz, rotation xyzw, velocity xyz. */
|
|
21
|
+
readonly samples: Float64Array;
|
|
22
|
+
private readonly output;
|
|
23
|
+
private readonly ring;
|
|
24
|
+
private readonly unit;
|
|
25
|
+
constructor(capacity?: number, maxPrediction?: number);
|
|
26
|
+
/** Timestamp and velocity use seconds. Duplicate/out-of-order packets are ignored. */
|
|
27
|
+
push(time: number, position: ArrayLike<number>, rotation?: ArrayLike<number>, velocity?: ArrayLike<number>): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* State at `time` seconds: Hermite position, its derivative as velocity, and slerped rotation between the surrounding
|
|
30
|
+
* snapshots, or
|
|
31
|
+
* linear extrapolation from the newest (up to `maxPrediction`). Before the oldest it returns the oldest.
|
|
32
|
+
* Returns null while empty. The default output is reused by every call.
|
|
33
|
+
* @throws RangeError for a non-finite time.
|
|
34
|
+
* @throws TypeError for a malformed output.
|
|
35
|
+
*/
|
|
36
|
+
sample(time: number, output?: Snapshot): Snapshot | null;
|
|
37
|
+
/** Drops every snapshot. */
|
|
38
|
+
clear(): void;
|
|
39
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { TimeRing as e, normalizeQuaternion as t, slerpInto as n } from "./interpolation-timeline.js";
|
|
2
|
+
//#region src/interpolation.ts
|
|
3
|
+
var r = [
|
|
4
|
+
0,
|
|
5
|
+
0,
|
|
6
|
+
0
|
|
7
|
+
], i = [
|
|
8
|
+
0,
|
|
9
|
+
0,
|
|
10
|
+
0,
|
|
11
|
+
1
|
|
12
|
+
];
|
|
13
|
+
function a() {
|
|
14
|
+
return {
|
|
15
|
+
position: /* @__PURE__ */ new Float64Array(3),
|
|
16
|
+
rotation: new Float64Array(i),
|
|
17
|
+
velocity: /* @__PURE__ */ new Float64Array(3)
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
var o = (e, t) => {
|
|
21
|
+
if (e.length !== t) throw TypeError("Snapshot vectors need 3 values (position, velocity) or 4 (rotation); pass arrays of the right length.");
|
|
22
|
+
for (let n = 0; n < t; n++) if (!Number.isFinite(e[n])) throw RangeError("Snapshot values must be finite");
|
|
23
|
+
}, s = class {
|
|
24
|
+
constructor(t = 32, n = .25) {
|
|
25
|
+
if (this.capacity = t, this.maxPrediction = n, this.output = a(), this.unit = /* @__PURE__ */ new Float64Array(4), !Number.isInteger(t) || t < 2 || t > 65536 || !Number.isFinite(n) || n < 0 || n > 10) throw RangeError("SnapshotBuffer needs an integer capacity from 2 to 65536 and a maxPrediction between 0 and 10 seconds; fix the arguments.");
|
|
26
|
+
this.samples = new Float64Array(t * 11), this.ring = new e(t, this.samples, 11);
|
|
27
|
+
}
|
|
28
|
+
push(e, n, a = i, s = r) {
|
|
29
|
+
if (!Number.isFinite(e)) throw RangeError("Snapshot time must be a finite number of seconds; pass a finite timestamp.");
|
|
30
|
+
o(n, 3), o(a, 4), o(s, 3);
|
|
31
|
+
let c = this.unit;
|
|
32
|
+
t(a, c, 0);
|
|
33
|
+
let l = this.ring.push(e);
|
|
34
|
+
if (l < 0) return !1;
|
|
35
|
+
let u = l * 11;
|
|
36
|
+
for (let e = 0; e < 3; e++) this.samples[u + 1 + e] = n[e], this.samples[u + 8 + e] = s[e];
|
|
37
|
+
for (let e = 0; e < 4; e++) this.samples[u + 4 + e] = c[e];
|
|
38
|
+
return !0;
|
|
39
|
+
}
|
|
40
|
+
sample(e, t = this.output) {
|
|
41
|
+
if (!Number.isFinite(e)) throw RangeError("Playback time must be a finite number of seconds; pass a finite time.");
|
|
42
|
+
if (t.position.length !== 3 || t.rotation.length !== 4 || t.velocity.length !== 3) throw TypeError("Snapshot output needs position and velocity of length 3 and rotation of length 4; pass a Snapshot made by createSnapshot.");
|
|
43
|
+
let r = this.ring;
|
|
44
|
+
if (!r.locate(e)) return null;
|
|
45
|
+
let i = this.samples, a = r.b * 11;
|
|
46
|
+
if (r.a === r.b) {
|
|
47
|
+
let e = Math.min(this.maxPrediction, r.past);
|
|
48
|
+
for (let n = 0; n < 3; n++) t.position[n] = i[a + 1 + n] + i[a + 8 + n] * e, t.velocity[n] = i[a + 8 + n];
|
|
49
|
+
for (let e = 0; e < 4; e++) t.rotation[e] = i[a + 4 + e];
|
|
50
|
+
return t;
|
|
51
|
+
}
|
|
52
|
+
let o = r.a * 11, s = i[a] - i[o], c = r.u, l = c * c, u = l * c;
|
|
53
|
+
for (let e = 0; e < 3; e++) t.position[e] = (2 * u - 3 * l + 1) * i[o + 1 + e] + (u - 2 * l + c) * s * i[o + 8 + e] + (-2 * u + 3 * l) * i[a + 1 + e] + (u - l) * s * i[a + 8 + e], t.velocity[e] = (6 * l - 6 * c) * (i[o + 1 + e] - i[a + 1 + e]) / s + (3 * l - 4 * c + 1) * i[o + 8 + e] + (3 * l - 2 * c) * i[a + 8 + e];
|
|
54
|
+
return n(t.rotation, 0, i, o + 4, a + 4, c), t;
|
|
55
|
+
}
|
|
56
|
+
clear() {
|
|
57
|
+
this.ring.clear();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
//#endregion
|
|
61
|
+
export { s as SnapshotBuffer, a as createSnapshot };
|
package/dist/lag.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** One hit shape: a circle (2 coordinates) or a sphere (3) of `radius`. */
|
|
2
|
+
export interface Hitbox {
|
|
3
|
+
/** Centre: [x, y] or [x, y, z]. */
|
|
4
|
+
readonly position: readonly number[];
|
|
5
|
+
/** Radius, 0 or more. */
|
|
6
|
+
readonly radius: number;
|
|
7
|
+
}
|
|
8
|
+
/** What {@link LagCompensation.raycast} found. */
|
|
9
|
+
export interface RewindHit {
|
|
10
|
+
/** The id of the shape hit. */
|
|
11
|
+
id: number;
|
|
12
|
+
/** Distance along the ray (in direction lengths: pass a unit direction for world units). */
|
|
13
|
+
distance: number;
|
|
14
|
+
/** The point hit. */
|
|
15
|
+
point: number[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Where every hit shape was at each of the last `history` ticks.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const lag = new LagCompensation({ history: 32 });
|
|
23
|
+
* // each server tick, after the step:
|
|
24
|
+
* lag.record(server.tick, [...state.players].map(([id, p]) => [id, { position: [p.x, p.y], radius: 0.5 }]));
|
|
25
|
+
* // a shot from a player who saw the world at (fractional) tick input.viewTick:
|
|
26
|
+
* const hit = lag.raycast(input.viewTick, [p.x, p.y], [input.aimX, input.aimY], { ignore: shooter });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare class LagCompensation {
|
|
30
|
+
/** Ticks kept. */
|
|
31
|
+
readonly history: number;
|
|
32
|
+
private readonly frames;
|
|
33
|
+
/** @throws RangeError for a history that is not an integer from 2 to 4096. */
|
|
34
|
+
constructor({ history }?: {
|
|
35
|
+
history?: number;
|
|
36
|
+
});
|
|
37
|
+
/** The oldest and newest ticks held, or undefined while empty. */
|
|
38
|
+
get range(): [number, number] | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Records the shapes at `tick` (copies them). Ticks must increase; recording the same tick again replaces it.
|
|
41
|
+
* @throws RangeError for a tick older than the newest, a shape with a non-finite coordinate or a negative radius;
|
|
42
|
+
* TypeError for a shape that is not `{ position, radius }`.
|
|
43
|
+
*/
|
|
44
|
+
record(tick: number, shapes: Iterable<readonly [number, Hitbox]>): void;
|
|
45
|
+
/**
|
|
46
|
+
* The shapes at fractional tick `tick`, blended linearly between the two recorded ticks around it (a shape present
|
|
47
|
+
* in only one of them is taken as it is there). A tick older than the history is clamped to the oldest frame, so
|
|
48
|
+
* a client cannot rewind further than `history` ticks; a newer one to the newest.
|
|
49
|
+
*/
|
|
50
|
+
at(tick: number): Map<number, Hitbox>;
|
|
51
|
+
/**
|
|
52
|
+
* The nearest shape a ray from `origin` along `direction` hits at fractional tick `tick`, within `maxDistance`
|
|
53
|
+
* (default Infinity) and not in `ignore` (an id or a set of ids, the shooter). Undefined when it hits nothing.
|
|
54
|
+
* @throws RangeError for an origin or direction of another dimension than 2 or 3, or a zero direction.
|
|
55
|
+
*/
|
|
56
|
+
raycast(tick: number, origin: readonly number[], direction: readonly number[], { maxDistance, ignore, }?: {
|
|
57
|
+
maxDistance?: number;
|
|
58
|
+
ignore?: number | ReadonlySet<number>;
|
|
59
|
+
}): RewindHit | undefined;
|
|
60
|
+
/** Drops every recorded tick. */
|
|
61
|
+
clear(): void;
|
|
62
|
+
}
|
package/dist/lobby.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Room, type RoomOptions, type RoomServer } from './rooms.js';
|
|
2
|
+
import { type Connection } from './transport.js';
|
|
3
|
+
/** Searchable JSON scalars, copied on publication and retrieval. */
|
|
4
|
+
export type RoomMetadata = Record<string, string | number | boolean | null>;
|
|
5
|
+
/** Public room listing. Tokens and join options are never listed. */
|
|
6
|
+
export interface LobbyListing {
|
|
7
|
+
/** Room id accepted by RoomsClient.joinById. */
|
|
8
|
+
roomId: string;
|
|
9
|
+
/** Registered room name. */
|
|
10
|
+
name: string;
|
|
11
|
+
/** Application metadata. */
|
|
12
|
+
metadata: RoomMetadata;
|
|
13
|
+
/** Connected clients. */
|
|
14
|
+
clients: number;
|
|
15
|
+
/** Reserved and reconnecting seats. */
|
|
16
|
+
reserved: number;
|
|
17
|
+
/** Total seat capacity. */
|
|
18
|
+
maxClients: number;
|
|
19
|
+
}
|
|
20
|
+
/** Exact-match room search; all supplied metadata keys must match. */
|
|
21
|
+
export interface LobbyFilter {
|
|
22
|
+
/** Registered room name; omitted means any. */
|
|
23
|
+
name?: string;
|
|
24
|
+
/** Scalar equality filters; omitted means any metadata. */
|
|
25
|
+
metadata?: RoomMetadata;
|
|
26
|
+
}
|
|
27
|
+
/** One-time bearer credential; send privately over an authenticated encrypted channel. */
|
|
28
|
+
export interface SeatReservation {
|
|
29
|
+
/** Room id for joinById. */
|
|
30
|
+
roomId: string;
|
|
31
|
+
/** Pass as the sole join option `seatToken`. */
|
|
32
|
+
seatToken: string;
|
|
33
|
+
/** Unix milliseconds after which the token is invalid. */
|
|
34
|
+
expiresAt: number;
|
|
35
|
+
}
|
|
36
|
+
/** Room with metadata, visibility and capacity-safe one-time reservations. Subclass in place of Room. */
|
|
37
|
+
export declare abstract class LobbyRoom<State = unknown> extends Room<State> {
|
|
38
|
+
/** Hide from public listings (default false); explicit id and reservations still work. */
|
|
39
|
+
privateRoom: boolean;
|
|
40
|
+
private metadataValue;
|
|
41
|
+
private readonly reservations;
|
|
42
|
+
private lobbyClosed;
|
|
43
|
+
/** A detached metadata copy. Use setMetadata to update it. */
|
|
44
|
+
get metadata(): RoomMetadata;
|
|
45
|
+
/** Publish searchable JSON scalars. @throws TypeError for non-scalar values; use finite JSON scalars. */
|
|
46
|
+
setMetadata(metadata: RoomMetadata): void;
|
|
47
|
+
/** Reserved seats, excluding reconnecting clients. Expired credentials are swept before counting. */
|
|
48
|
+
get reservedSeats(): number;
|
|
49
|
+
/** Includes one-time reservations in addition to the ordinary capacity and manual lock. */
|
|
50
|
+
get locked(): boolean;
|
|
51
|
+
/** Reservations keep an otherwise empty room alive. */
|
|
52
|
+
get empty(): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Reserve one seat with immutable server-side join options. Default lifetime 15 seconds.
|
|
55
|
+
* @throws MargoError `failed` if locked/full/closed; RangeError for a lifetime outside 1..2147483647 ms.
|
|
56
|
+
*/
|
|
57
|
+
reserve(options?: RoomOptions, lifetime?: number): SeatReservation;
|
|
58
|
+
/** Cancel a token, freeing its seat immediately. Returns false for an unknown/already consumed token. */
|
|
59
|
+
cancelReservation(seatToken: string): boolean;
|
|
60
|
+
/** @internal Resolve server-held options before authentication; callers cannot replace the matched identity. */
|
|
61
|
+
admit(connection: Connection, sessionId: string, options: RoomOptions): Promise<'joined' | 'refused' | 'failed'>;
|
|
62
|
+
/** Atomically consume a valid credential after async authentication; ordinary joins cannot take its seat. */
|
|
63
|
+
protected claimSeat(options: RoomOptions): boolean;
|
|
64
|
+
private sweep;
|
|
65
|
+
/** @internal Dispose all pending reservations alongside the room. */
|
|
66
|
+
release(): void;
|
|
67
|
+
}
|
|
68
|
+
/** A live view of a RoomServer, suitable for exposing through an application's authenticated HTTP API. */
|
|
69
|
+
export declare class RoomLobby {
|
|
70
|
+
private readonly server;
|
|
71
|
+
/** Server whose rooms are listed. */
|
|
72
|
+
constructor(server: RoomServer);
|
|
73
|
+
/** Joinable public rooms with detached metadata. Ordinary Room subclasses have empty metadata. */
|
|
74
|
+
list(filter?: LobbyFilter): LobbyListing[];
|
|
75
|
+
/** Reserve on the authoritative room. @throws MargoError `failed` if missing or not a LobbyRoom. */
|
|
76
|
+
reserve(roomId: string, options?: RoomOptions, lifetime?: number): SeatReservation;
|
|
77
|
+
}
|
package/dist/lobby.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { r as e } from "./transport-DeQodwyv.js";
|
|
2
|
+
import { Room as t } from "./rooms.js";
|
|
3
|
+
//#region src/lobby.ts
|
|
4
|
+
var n = class extends t {
|
|
5
|
+
constructor(...e) {
|
|
6
|
+
super(...e), this.privateRoom = !1, this.metadataValue = {}, this.reservations = /* @__PURE__ */ new Map(), this.lobbyClosed = !1;
|
|
7
|
+
}
|
|
8
|
+
get metadata() {
|
|
9
|
+
return { ...this.metadataValue };
|
|
10
|
+
}
|
|
11
|
+
setMetadata(e) {
|
|
12
|
+
for (let t of Object.values(e)) if (t !== null && typeof t != "string" && typeof t != "boolean" && !(typeof t == "number" && Number.isFinite(t))) throw TypeError("Room metadata must contain finite JSON scalars; use strings, booleans, numbers or null");
|
|
13
|
+
this.metadataValue = { ...e };
|
|
14
|
+
}
|
|
15
|
+
get reservedSeats() {
|
|
16
|
+
return this.sweep(), this.reservations.size;
|
|
17
|
+
}
|
|
18
|
+
get locked() {
|
|
19
|
+
let e = this.reservedSeats;
|
|
20
|
+
return this.lobbyClosed || super.locked || this.clients.length + this.dropped.length + e >= this.maxClients;
|
|
21
|
+
}
|
|
22
|
+
get empty() {
|
|
23
|
+
return super.empty && this.reservedSeats === 0;
|
|
24
|
+
}
|
|
25
|
+
reserve(t = {}, n = 15e3) {
|
|
26
|
+
if (!Number.isInteger(n) || n < 1 || n > 2147483647) throw RangeError("Reservation lifetime must be 1..2147483647 ms; pass an integer in range");
|
|
27
|
+
if (this.lobbyClosed || this.locked) throw e("failed", "Room cannot reserve a seat; choose a joinable room");
|
|
28
|
+
let r = JSON.parse(JSON.stringify(t));
|
|
29
|
+
delete r.seatToken;
|
|
30
|
+
let i = [...crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(32))].map((e) => e.toString(16).padStart(2, "0")).join(""), a = Date.now() + n, o = setTimeout(() => {
|
|
31
|
+
this.reservations.delete(i), this.empty && this.autoDispose && this.release();
|
|
32
|
+
}, n);
|
|
33
|
+
return this.reservations.set(i, {
|
|
34
|
+
options: r,
|
|
35
|
+
expiresAt: a,
|
|
36
|
+
timer: o
|
|
37
|
+
}), {
|
|
38
|
+
roomId: this.roomId,
|
|
39
|
+
seatToken: i,
|
|
40
|
+
expiresAt: a
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
cancelReservation(e) {
|
|
44
|
+
let t = this.reservations.get(e);
|
|
45
|
+
return t ? (clearTimeout(t.timer), this.reservations.delete(e), this.empty && this.autoDispose && this.release(), !0) : !1;
|
|
46
|
+
}
|
|
47
|
+
async admit(e, t, n) {
|
|
48
|
+
if ("seatToken" in n) {
|
|
49
|
+
this.sweep();
|
|
50
|
+
let e = this.reservations.get(String(n.seatToken));
|
|
51
|
+
if (!e) return "refused";
|
|
52
|
+
n = {
|
|
53
|
+
...JSON.parse(JSON.stringify(e.options)),
|
|
54
|
+
seatToken: n.seatToken
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return super.admit(e, t, n);
|
|
58
|
+
}
|
|
59
|
+
claimSeat(e) {
|
|
60
|
+
if (!("seatToken" in e)) return super.claimSeat(e);
|
|
61
|
+
this.sweep();
|
|
62
|
+
let t = String(e.seatToken), n = this.reservations.get(t);
|
|
63
|
+
return !n || this.lobbyClosed || super.locked ? !1 : (clearTimeout(n.timer), this.reservations.delete(t), !0);
|
|
64
|
+
}
|
|
65
|
+
sweep() {
|
|
66
|
+
let e = Date.now(), t = !1;
|
|
67
|
+
for (let [n, r] of this.reservations) r.expiresAt <= e && (clearTimeout(r.timer), this.reservations.delete(n), t = !0);
|
|
68
|
+
t && super.empty && !this.reservations.size && this.autoDispose && this.release();
|
|
69
|
+
}
|
|
70
|
+
release() {
|
|
71
|
+
this.lobbyClosed = !0;
|
|
72
|
+
for (let e of this.reservations.values()) clearTimeout(e.timer);
|
|
73
|
+
this.reservations.clear(), super.release();
|
|
74
|
+
}
|
|
75
|
+
}, r = class {
|
|
76
|
+
constructor(e) {
|
|
77
|
+
this.server = e;
|
|
78
|
+
}
|
|
79
|
+
list(e = {}) {
|
|
80
|
+
return this.server.rooms.filter((e) => !e.locked && !(e instanceof n && e.privateRoom)).map((e) => ({
|
|
81
|
+
roomId: e.roomId,
|
|
82
|
+
name: e.roomName,
|
|
83
|
+
metadata: e instanceof n ? e.metadata : {},
|
|
84
|
+
clients: e.clients.length,
|
|
85
|
+
reserved: e.dropped.length + (e instanceof n ? e.reservedSeats : 0),
|
|
86
|
+
maxClients: e.maxClients
|
|
87
|
+
})).filter((t) => (!e.name || t.name === e.name) && Object.entries(e.metadata ?? {}).every(([e, n]) => Object.hasOwn(t.metadata, e) && t.metadata[e] === n)).sort((e, t) => e.roomId < t.roomId ? -1 : +(e.roomId > t.roomId));
|
|
88
|
+
}
|
|
89
|
+
reserve(t, r = {}, i = 15e3) {
|
|
90
|
+
let a = this.server.rooms.find((e) => e.roomId === t);
|
|
91
|
+
if (!(a instanceof n)) throw e("failed", "Room does not support reservations; define a LobbyRoom");
|
|
92
|
+
return a.reserve(r, i);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
//#endregion
|
|
96
|
+
export { n as LobbyRoom, r as RoomLobby };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { Connection, Listener } from './transport.js';
|
|
2
|
+
/** Conditions of one direction of a loopback link. Every value is checked when the network is made. */
|
|
3
|
+
export interface LinkConditions {
|
|
4
|
+
/** One-way delay in milliseconds (default 0). */
|
|
5
|
+
latency?: number;
|
|
6
|
+
/** Extra random delay per message, uniform from 0 to `jitter` milliseconds (default 0). */
|
|
7
|
+
jitter?: number;
|
|
8
|
+
/** Chance from 0 to 1 that an unreliable message is lost (default 0). A lost reliable message is resent. */
|
|
9
|
+
loss?: number;
|
|
10
|
+
/** Chance from 0 to 1 that an unreliable message arrives twice (default 0). */
|
|
11
|
+
duplicate?: number;
|
|
12
|
+
/** Chance from 0 to 1 that an unreliable message is held back by up to two latencies more (default 0). */
|
|
13
|
+
reorder?: number;
|
|
14
|
+
}
|
|
15
|
+
/** Options of {@link LoopbackNetwork}. */
|
|
16
|
+
export interface LoopbackOptions extends LinkConditions {
|
|
17
|
+
/** Seed of the random stream: the same seed and calls give the same deliveries (default 1). */
|
|
18
|
+
seed?: number;
|
|
19
|
+
/** Time in milliseconds a lost reliable message waits before it is resent (default 2 x latency + 10). */
|
|
20
|
+
retransmit?: number;
|
|
21
|
+
}
|
|
22
|
+
/** Counts since the network was made. */
|
|
23
|
+
export interface LoopbackStats {
|
|
24
|
+
/** Messages handed to `send`. */
|
|
25
|
+
sent: number;
|
|
26
|
+
/** Messages delivered (duplicates count twice). */
|
|
27
|
+
delivered: number;
|
|
28
|
+
/** Unreliable messages dropped. */
|
|
29
|
+
lost: number;
|
|
30
|
+
/** Extra copies delivered. */
|
|
31
|
+
duplicated: number;
|
|
32
|
+
/** Reliable messages resent after a simulated loss. */
|
|
33
|
+
resent: number;
|
|
34
|
+
/** Bytes handed to `send`. */
|
|
35
|
+
bytes: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* An in-memory network with a virtual clock. `listen()` gives the server end, `connect()` a client connection whose
|
|
39
|
+
* server half arrives through the listener's `onconnection` at once. Messages are delivered only inside
|
|
40
|
+
* {@link LoopbackNetwork.advance}, in time order, so a test is repeatable from its seed.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const network = new LoopbackNetwork({ latency: 50, jitter: 10, loss: 0.05, seed: 7 });
|
|
45
|
+
* const listener = network.listen();
|
|
46
|
+
* listener.onconnection = (server) => (server.onmessage = (data) => server.send(data));
|
|
47
|
+
* const client = network.connect();
|
|
48
|
+
* client.onmessage = (data) => console.log('echo', data);
|
|
49
|
+
* client.send(new Uint8Array([1, 2, 3]), 'unreliable');
|
|
50
|
+
* network.advance(200);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare class LoopbackNetwork {
|
|
54
|
+
/** The virtual time in milliseconds; starts at 0 and moves only in {@link LoopbackNetwork.advance}. */
|
|
55
|
+
time: number;
|
|
56
|
+
/** Delivery counts. */
|
|
57
|
+
readonly stats: LoopbackStats;
|
|
58
|
+
private readonly random;
|
|
59
|
+
private readonly conditions;
|
|
60
|
+
private readonly queue;
|
|
61
|
+
private order;
|
|
62
|
+
private listener;
|
|
63
|
+
/** @throws RangeError for a condition out of range (latency and jitter 0 to 60,000 ms, loss 0 to 0.99). */
|
|
64
|
+
constructor(options?: LoopbackOptions);
|
|
65
|
+
/** The server end. One listener per network; listening again replaces it. */
|
|
66
|
+
listen(): Listener;
|
|
67
|
+
/**
|
|
68
|
+
* A new client connection, open at once; its server half goes to the listener's `onconnection` now. `conditions`
|
|
69
|
+
* override the network's for this link (both directions).
|
|
70
|
+
* @throws RangeError for a condition out of range.
|
|
71
|
+
*/
|
|
72
|
+
connect(conditions?: LinkConditions): Connection;
|
|
73
|
+
/**
|
|
74
|
+
* Two connected ends without a listener, for peer-to-peer sessions (one pair per two peers). `conditions` override
|
|
75
|
+
* the network's for this link (both directions).
|
|
76
|
+
* @throws RangeError for a condition out of range.
|
|
77
|
+
*/
|
|
78
|
+
pair(conditions?: LinkConditions): [Connection, Connection];
|
|
79
|
+
/**
|
|
80
|
+
* Moves the clock forward by `ms` and delivers, in time order, every message due by then (and any message those
|
|
81
|
+
* deliveries send that is also due).
|
|
82
|
+
* @throws RangeError for a negative or non-finite `ms`.
|
|
83
|
+
*/
|
|
84
|
+
advance(ms: number): void;
|
|
85
|
+
/** Delivers everything queued, moving the clock to the last delivery. */
|
|
86
|
+
flush(): void;
|
|
87
|
+
private next;
|
|
88
|
+
/** @internal Schedules `run` at virtual time `at`. */
|
|
89
|
+
schedule(at: number, run: () => void): void;
|
|
90
|
+
/** @internal The seeded random stream. */
|
|
91
|
+
roll(): number;
|
|
92
|
+
}
|