@driftengine/network 3.61.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 +202 -0
- package/NOTICE +9 -0
- package/README.md +125 -0
- package/dist/authority.d.ts +170 -0
- package/dist/authority.js +235 -0
- package/dist/fingerprint.d.ts +64 -0
- package/dist/fingerprint.js +108 -0
- package/dist/fixture.d.ts +32 -0
- package/dist/fixture.js +136 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +32 -0
- package/dist/inputLog.d.ts +117 -0
- package/dist/inputLog.js +232 -0
- package/dist/lockstep.d.ts +181 -0
- package/dist/lockstep.js +291 -0
- package/dist/loopback.d.ts +115 -0
- package/dist/loopback.js +207 -0
- package/dist/rewind.d.ts +206 -0
- package/dist/rewind.js +212 -0
- package/dist/scriptSession.d.ts +77 -0
- package/dist/scriptSession.js +81 -0
- package/dist/simNumber.d.ts +103 -0
- package/dist/simNumber.js +156 -0
- package/dist/snapshotter.d.ts +43 -0
- package/dist/snapshotter.js +81 -0
- package/dist/transport.d.ts +63 -0
- package/dist/transport.js +40 -0
- package/dist/webrtc.d.ts +74 -0
- package/dist/webrtc.js +108 -0
- package/dist/websocket.d.ts +70 -0
- package/dist/websocket.js +105 -0
- package/dist/wire.d.ts +94 -0
- package/dist/wire.js +238 -0
- package/package.json +61 -0
- package/src/authority.ts +340 -0
- package/src/fingerprint.ts +143 -0
- package/src/fixture.ts +188 -0
- package/src/index.ts +95 -0
- package/src/inputLog.ts +262 -0
- package/src/lockstep.ts +374 -0
- package/src/loopback.ts +250 -0
- package/src/rewind.ts +337 -0
- package/src/scriptSession.ts +137 -0
- package/src/simNumber.ts +176 -0
- package/src/snapshotter.ts +98 -0
- package/src/transport.ts +69 -0
- package/src/webrtc.ts +141 -0
- package/src/websocket.ts +140 -0
- package/src/wire.ts +269 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Drift Technologies
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
DriftEngine
|
|
2
|
+
Copyright 2026 Drift Technologies
|
|
3
|
+
|
|
4
|
+
This product includes software developed at Drift Technologies
|
|
5
|
+
(https://github.com/drftrun/driftengine).
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0. Section 4(d) of that licence
|
|
8
|
+
requires this NOTICE to be reproduced in any derivative work you distribute,
|
|
9
|
+
in the source, the documentation, or a display generated by the work.
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @driftengine/network
|
|
2
|
+
|
|
3
|
+
A transport seam, rewind and replay, and the two networking models built on them.
|
|
4
|
+
|
|
5
|
+
**Cost: 2.4 KB gzipped standalone.** Measured by `scripts/size-gate.test.mjs`, which fails if it
|
|
6
|
+
drifts more than 3% — the number comes from the same floors that gate asserts, so a README quoting
|
|
7
|
+
a stale one is a red suite rather than a thing somebody notices.
|
|
8
|
+
|
|
9
|
+
**Standalone, and the raw figure is the claim.** 6,901 bytes raw against core's 2.79 MB: no
|
|
10
|
+
renderer is in this package's module graph. `boundaries.test.mjs` already makes the argument about
|
|
11
|
+
`@driftengine/physics` — _"a deterministic simulation with no renderer in its module graph, which is
|
|
12
|
+
what an authoritative host runs"_ — and this is the second package it applies to. It imports
|
|
13
|
+
`@driftengine/entities` at runtime and nothing else.
|
|
14
|
+
|
|
15
|
+
## What is in it
|
|
16
|
+
|
|
17
|
+
| | |
|
|
18
|
+
| ----------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
|
|
19
|
+
| `RewindLoop` | Rewind to a tick, replay to now. Both models sit on this one |
|
|
20
|
+
| `InputLog` | Every participant's input, over a window that is explicit rather than a modulo |
|
|
21
|
+
| `Snapshotter` | The state seam. `worldSnapshotter`, `randomSnapshotter`, `combineSnapshotters` |
|
|
22
|
+
| `Transport` | The seam a caller supplies. `LoopbackTransport` with an impairment model, `WebSocketTransport`, `WebRtcTransport` |
|
|
23
|
+
| `LockstepSession` | Peers exchange inputs; a wrong guess is unwound; a divergence halts and names its tick |
|
|
24
|
+
| `AuthorityHost`, `PredictingClient` | One world is the truth; a client predicts its own player and corrects |
|
|
25
|
+
| `StateInterpolator` | Remote bodies drawn between two states the authority sent |
|
|
26
|
+
| `Fingerprint` | A hash of state, so a desync has a tick number |
|
|
27
|
+
| `Sim` | Opt-in 48.16 fixed point, for a consumer who would rather not depend on an argument about IEEE 754 |
|
|
28
|
+
|
|
29
|
+
## The one idea
|
|
30
|
+
|
|
31
|
+
**Prediction and rollback are the same operation.** A lockstep peer that guessed a remote input and
|
|
32
|
+
then learned the real one has to unwind the ticks it computed from the guess. A predicting client
|
|
33
|
+
whose authority disagrees has to unwind the ticks it computed from a world that was wrong. Both are:
|
|
34
|
+
put the state back to tick _T_, correct what was wrong about _T_, and step forward to where we were.
|
|
35
|
+
|
|
36
|
+
So there is one `RewindLoop` and both models sit on it, rather than two implementations of one
|
|
37
|
+
mechanism.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const rewind = new RewindLoop({
|
|
41
|
+
step: mySimulation, // (dt, tick) => void — the loop's own signature
|
|
42
|
+
snapshotter: combineSnapshotters([worldSnapshotter(world), randomSnapshotter(rng)]),
|
|
43
|
+
inputs: new InputLog({ participants: 2, depth: 32, inputBytes: 2 }),
|
|
44
|
+
fixedDt: 1 / 60,
|
|
45
|
+
depth: 8, // 133 ms of rewind at 60 Hz
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
startLoop({
|
|
49
|
+
simulate: (dt, tick) => rewind.advance(dt, tick),
|
|
50
|
+
render,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// A packet arrives, possibly for a tick that has already run.
|
|
54
|
+
rewind.supply(participant, tick, payload); // false when it is beyond the window
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## What it does not know
|
|
58
|
+
|
|
59
|
+
**It does not know what an input is.** A payload is a fixed number of bytes a consumer encodes.
|
|
60
|
+
`AGENTS.md` states the rule — engine APIs never take another game's noun — and this is the place
|
|
61
|
+
most netcode gives it up. The size being fixed is what buys the allocation story: one array for the
|
|
62
|
+
session, so neither recording an input nor reading one back during a replay allocates anything.
|
|
63
|
+
|
|
64
|
+
**It does not know what your state is.** `Snapshotter<S>` is generic over its own slot type, so a
|
|
65
|
+
rewind needs no serialization at all — a snapshot never leaves the process. `worldSnapshotter` is
|
|
66
|
+
one implementation, over `@driftengine/entities`; a simulation whose state is four typed arrays
|
|
67
|
+
writes another in thirty lines.
|
|
68
|
+
|
|
69
|
+
**It does not decide fairness.** `rewindTo` is public so lag compensation is implementable, and lag
|
|
70
|
+
compensation is not implemented: an authority rewinding the world to a shooter's view of the past is
|
|
71
|
+
a policy about who wins a disputed shot, and that belongs to a game.
|
|
72
|
+
|
|
73
|
+
## Two things that are easy to get wrong, and are handled here
|
|
74
|
+
|
|
75
|
+
**A snapshot is taken before the step, not after.** `advance(dt, T)` saves and then steps, so slot
|
|
76
|
+
_T_ holds the world as tick _T_ began — which is what a rewind to _T_ wants, because _T_'s input is
|
|
77
|
+
about to be applied. Saving afterwards makes every rewind land one tick late, and the symptom is a
|
|
78
|
+
world that drifts slowly rather than one that breaks.
|
|
79
|
+
|
|
80
|
+
**A late input and a late state are different corrections.** A late _input_ means the ticks since
|
|
81
|
+
were computed from a wrong input: `supply` records it and the next `advance` replays. A late _state_
|
|
82
|
+
means they were computed from a wrong world: `reconcileSnapshot` overwrites that tick's state,
|
|
83
|
+
re-saves it, and replays the local inputs over the top — which is what makes a correction converge
|
|
84
|
+
instead of erasing what the player did.
|
|
85
|
+
|
|
86
|
+
## A rewind snapshot is not a save file
|
|
87
|
+
|
|
88
|
+
`serializeWorld` in `@driftengine/entities` writes a scene: schemas for migration, values keyed by
|
|
89
|
+
stable field id, and entities _created_ on load. Every one of those is wrong for a rewind, which
|
|
90
|
+
needs the handles it had — an input recorded against entity 4,194,307 must still mean that entity —
|
|
91
|
+
and needs to cost nothing per tick. `WorldSnapshot` is the other artefact, and `world.ts` carries
|
|
92
|
+
the table comparing them.
|
|
93
|
+
|
|
94
|
+
## Two things a lossy link taught the design
|
|
95
|
+
|
|
96
|
+
**An input message carries a run of consecutive ticks.** An input is only useful for the tick it
|
|
97
|
+
names, so asking for a lost one and waiting a round trip delivers it after that tick has gone. Every
|
|
98
|
+
packet carries the last four inputs, and a test at 15% loss is what turned that from a nicety into a
|
|
99
|
+
requirement — the control, with one input per packet, does not converge.
|
|
100
|
+
|
|
101
|
+
**A fingerprint is only comparable at a confirmed tick.** The newest tick is speculative: each peer
|
|
102
|
+
has predicted inputs the other already knows, so their live worlds differ constantly and correctly.
|
|
103
|
+
Comparing them halted a healthy session at tick 32 during development.
|
|
104
|
+
|
|
105
|
+
**And a third, which cost a consumer a session on a perfect link.** Three windows move independently
|
|
106
|
+
and a session lives inside all of them. `redundancy` says how far back every packet reaches;
|
|
107
|
+
`RewindLoop`'s `depth` says how far back a correction can reach; and the session retains inputs down
|
|
108
|
+
to the oldest tick that rewind window covers, forgetting everything below it every tick. So on any
|
|
109
|
+
session whose redundancy reaches further back than its rewind depth, the oldest word of every packet
|
|
110
|
+
names a tick the log has already been told to forget — which is not a fault, and must not be read as
|
|
111
|
+
one. A copy of a tick this world already applied cannot change it and cannot be evidence of a
|
|
112
|
+
divergence, whatever the log can still say about it; an input for a tick this world _never_ had and
|
|
113
|
+
can no longer apply is fatal, and still halts. Redundancy larger than the rewind depth is a fine
|
|
114
|
+
thing to ask for, and the two numbers do not have to be chosen together.
|
|
115
|
+
|
|
116
|
+
## Determinism
|
|
117
|
+
|
|
118
|
+
A rewind is only as good as the simulation's reproducibility. `scripts/determinism.mjs` is the gate:
|
|
119
|
+
the arithmetic a simulation may use is what IEEE 754 fixes exactly, and the twenty-two functions
|
|
120
|
+
ECMAScript declines to specify are refused inside the declared simulation set. `packages/network` is
|
|
121
|
+
in that set, and `packages/core/src/math/exact.ts` supplies the transcendentals it may still need.
|
|
122
|
+
|
|
123
|
+
**`scripts/exactness-cross.mjs` is how a cross-machine claim becomes a measurement.** It prints what
|
|
124
|
+
this machine computes — golden bits, a seeded stream, and both arms of the conformance fixture — in
|
|
125
|
+
a form another machine's output can be diffed against.
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One machine's state is the truth, and everybody else predicts and corrects.
|
|
3
|
+
*
|
|
4
|
+
* **The correction is a rewind**, which is why this file is short. A client that mispredicted has
|
|
5
|
+
* computed some ticks from a world that was wrong; putting the authority's world back at that tick
|
|
6
|
+
* and replaying the local inputs over it is exactly `reconcileSnapshot`. There is no second
|
|
7
|
+
* mechanism here, only a different reason for reaching the same one.
|
|
8
|
+
*
|
|
9
|
+
* ---
|
|
10
|
+
*
|
|
11
|
+
* ## What crosses, and who decides
|
|
12
|
+
*
|
|
13
|
+
* A `Replicator` is supplied by the consumer. This package cannot know which of a game's fields are
|
|
14
|
+
* authoritative, which are cosmetic, or how to pack them — and an engine that did would be a
|
|
15
|
+
* particular game's engine. What it does own is *when*: the host publishes on a cadence, and the
|
|
16
|
+
* client applies at the tick the message names.
|
|
17
|
+
*
|
|
18
|
+
* ## Prediction is for the local participant only
|
|
19
|
+
*
|
|
20
|
+
* A client predicts *its own* entity from its own inputs, immediately, because the whole point is
|
|
21
|
+
* that a player's controls feel attached to their hands. It does **not** extrapolate other players
|
|
22
|
+
* forward. Extrapolation is a choice between showing a stale position and showing a wrong one, and
|
|
23
|
+
* which is worse depends on the game: a wrong position in a shooter decides a hit, and a stale one
|
|
24
|
+
* in a racing game is a car in the wrong place on a corner. So remote bodies are *interpolated*
|
|
25
|
+
* between two states the authority actually sent — `StateInterpolator` below — and anything more
|
|
26
|
+
* aggressive is a consumer's to write.
|
|
27
|
+
*
|
|
28
|
+
* ## What is not here
|
|
29
|
+
*
|
|
30
|
+
* **Lag compensation.** An authority rewinding the world to a shooter's view of the past, so their
|
|
31
|
+
* shot hits what was on their screen, is a real technique and a policy about who wins a disputed
|
|
32
|
+
* shot. `RewindLoop.rewindTo` is public so it can be built; choosing it for every game is not this
|
|
33
|
+
* package's business.
|
|
34
|
+
*
|
|
35
|
+
* **Interest management.** A world large enough that a client should not receive all of it needs
|
|
36
|
+
* spatial partitioning of replication state. Reversed by a consumer measuring replication bandwidth
|
|
37
|
+
* as their constraint.
|
|
38
|
+
*/
|
|
39
|
+
import type { InputLog } from './inputLog.ts';
|
|
40
|
+
import type { RewindLoop } from './rewind.ts';
|
|
41
|
+
import { type PeerId, type Transport } from './transport.ts';
|
|
42
|
+
/**
|
|
43
|
+
* What a consumer packs and unpacks.
|
|
44
|
+
*
|
|
45
|
+
* `encode` answers -1 when the state does not fit, for the reason the wire encoders do: the caller
|
|
46
|
+
* is a frame loop and `AGENTS.md` forbids throwing in one.
|
|
47
|
+
*/
|
|
48
|
+
export interface Replicator {
|
|
49
|
+
encode(into: Uint8Array, tick: number): number;
|
|
50
|
+
apply(from: Uint8Array, tick: number): void;
|
|
51
|
+
}
|
|
52
|
+
export interface AuthorityOptions<S> {
|
|
53
|
+
readonly transport: Transport;
|
|
54
|
+
readonly loop: RewindLoop<S>;
|
|
55
|
+
readonly inputs: InputLog;
|
|
56
|
+
readonly replicator: Replicator;
|
|
57
|
+
/**
|
|
58
|
+
* How often the authoritative state is published, in ticks.
|
|
59
|
+
*
|
|
60
|
+
* **Three at 60 Hz is twenty a second**, which is what most shipped netcode settles near: often
|
|
61
|
+
* enough that a correction is small, rare enough that the bandwidth is a fraction of what a
|
|
62
|
+
* per-tick broadcast costs. A consumer whose state is tiny can publish every tick and one whose
|
|
63
|
+
* world is large should publish less and send deltas.
|
|
64
|
+
*/
|
|
65
|
+
readonly stateEvery?: number;
|
|
66
|
+
/** Room for one encoded state. 8 KB by default; a state that does not fit is a state to delta. */
|
|
67
|
+
readonly maxStateBytes?: number;
|
|
68
|
+
}
|
|
69
|
+
/** The machine whose world is the truth. */
|
|
70
|
+
export declare class AuthorityHost<S> {
|
|
71
|
+
private readonly transport;
|
|
72
|
+
private readonly loop;
|
|
73
|
+
private readonly inputs;
|
|
74
|
+
private readonly replicator;
|
|
75
|
+
private readonly stateEvery;
|
|
76
|
+
private readonly outgoing;
|
|
77
|
+
private readonly decoded;
|
|
78
|
+
private published;
|
|
79
|
+
constructor(options: AuthorityOptions<S>);
|
|
80
|
+
/** How many state messages have gone out. For a consumer measuring what replication costs. */
|
|
81
|
+
get publishedStates(): number;
|
|
82
|
+
poll(): void;
|
|
83
|
+
/**
|
|
84
|
+
* Take a tick, then publish if this is a publishing tick.
|
|
85
|
+
*
|
|
86
|
+
* **The state is encoded *after* the step**, so it describes the world at the end of `tick`, and
|
|
87
|
+
* a client applying it reconciles at `tick + 1`. Publishing before the step would send the world
|
|
88
|
+
* as it was when the tick began, which is the state the client already had.
|
|
89
|
+
*/
|
|
90
|
+
advance(dt: number, tick: number): void;
|
|
91
|
+
}
|
|
92
|
+
export interface ClientOptions<S> {
|
|
93
|
+
readonly transport: Transport;
|
|
94
|
+
readonly loop: RewindLoop<S>;
|
|
95
|
+
readonly inputs: InputLog;
|
|
96
|
+
readonly replicator: Replicator;
|
|
97
|
+
readonly self: PeerId;
|
|
98
|
+
/** Predict the local participant. Off means wait for the authority, which is the control. */
|
|
99
|
+
readonly predict?: boolean;
|
|
100
|
+
/** Recent inputs carried in every packet, for the reason `LockstepSession` documents. */
|
|
101
|
+
readonly redundancy?: number;
|
|
102
|
+
}
|
|
103
|
+
/** A machine that predicts its own player and corrects toward the authority. */
|
|
104
|
+
export declare class PredictingClient<S> {
|
|
105
|
+
readonly self: PeerId;
|
|
106
|
+
private readonly transport;
|
|
107
|
+
private readonly loop;
|
|
108
|
+
private readonly inputs;
|
|
109
|
+
private readonly replicator;
|
|
110
|
+
private readonly predict;
|
|
111
|
+
private readonly redundancy;
|
|
112
|
+
private readonly recent;
|
|
113
|
+
private readonly outgoing;
|
|
114
|
+
private readonly decoded;
|
|
115
|
+
private recentFrom;
|
|
116
|
+
private recentCount;
|
|
117
|
+
private correctionsValue;
|
|
118
|
+
private lastAppliedState;
|
|
119
|
+
constructor(options: ClientOptions<S>);
|
|
120
|
+
/** How many authoritative states have been applied. Each one is a rewind. */
|
|
121
|
+
get corrections(): number;
|
|
122
|
+
/**
|
|
123
|
+
* Send this input and, when predicting, apply it locally on the same tick.
|
|
124
|
+
*
|
|
125
|
+
* **No input delay**, unlike lockstep. A delay there buys exactness for everybody; here the
|
|
126
|
+
* authority is the source of truth anyway, so delaying the local input would add latency to the
|
|
127
|
+
* player's own controls and buy nothing.
|
|
128
|
+
*/
|
|
129
|
+
submit(tick: number, payload: Uint8Array): void;
|
|
130
|
+
poll(): void;
|
|
131
|
+
advance(dt: number, tick: number): void;
|
|
132
|
+
private remember;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Two authoritative states and where between them to draw.
|
|
136
|
+
*
|
|
137
|
+
* **Rendering at a delay is what stops a remote body teleporting.** States arrive every few ticks
|
|
138
|
+
* and unevenly; drawing the newest one puts a body wherever the last packet said, which on a jittery
|
|
139
|
+
* link is a stutter. Drawing *between* the two most recent, a fixed distance behind the newest, costs
|
|
140
|
+
* that distance in latency and buys smooth motion — and the latency is honest, because the position
|
|
141
|
+
* shown is one the authority actually published rather than one guessed forward.
|
|
142
|
+
*
|
|
143
|
+
* This owns the timing and none of the content: `sample` answers which two payloads and how far
|
|
144
|
+
* between, and the consumer decodes and blends. It cannot do otherwise, since the payload's shape is
|
|
145
|
+
* the consumer's.
|
|
146
|
+
*/
|
|
147
|
+
export declare class StateInterpolator {
|
|
148
|
+
readonly delayTicks: number;
|
|
149
|
+
private readonly ticks;
|
|
150
|
+
private readonly payloads;
|
|
151
|
+
private readonly lengths;
|
|
152
|
+
private count;
|
|
153
|
+
private cursor;
|
|
154
|
+
constructor(capacity?: number, maxBytes?: number, delayTicks?: number);
|
|
155
|
+
/** Keep a state. The bytes are copied, because the transport reuses its buffer. */
|
|
156
|
+
push(tick: number, payload: Uint8Array): void;
|
|
157
|
+
/**
|
|
158
|
+
* The two states to blend at `renderTick`, and how far between them, or null.
|
|
159
|
+
*
|
|
160
|
+
* `alpha` is clamped to [0, 1]: past the newest state it holds there rather than extrapolating,
|
|
161
|
+
* which is the same choice `TickTrace.sample` makes at the end of a recording and for the same
|
|
162
|
+
* reason — a character who has finished should look like they finished.
|
|
163
|
+
*/
|
|
164
|
+
sample(renderTick: number): {
|
|
165
|
+
from: Uint8Array;
|
|
166
|
+
to: Uint8Array;
|
|
167
|
+
alpha: number;
|
|
168
|
+
} | null;
|
|
169
|
+
private slice;
|
|
170
|
+
}
|