1681-engine / index / LagCompensator
Class: LagCompensator ​
Defined in: src/net/LagCompensator.ts:35
Lag compensation for a server: where every body was a moment ago, so a shot is judged against what the shooter saw. A client draws other bodies delayMs in the past between two snapshots (InterpolationBuffer); the server records the same positions at the same times, when each snapshot goes out, and blends between them the same way, so a rewind lands on the very picture the shooter aimed at, not on where the target has walked to since.
How far back a client may ask is capped (clampTime): a client that claims an old time to shoot round a corner gets no further back than maxRewindMs.
// when snapshots go out (RoomHost does this for a Room made with `rewindMs`)
lag.record(now, players);
// a client's shot, sent with `at: buffer.viewTime(serverNow())`
const at = LagCompensator.clampTime(msg.at, now, 250);
const hit = lag.alongRay(at, x, y, dx, dy, range, 6, shooterId)[0];Constructors ​
Constructor ​
new LagCompensator(keepMs?): LagCompensator;Defined in: src/net/LagCompensator.ts:39
keepMs: how much history is kept; at least the longest rewind allowed plus a snapshot interval.
Parameters ​
| Parameter | Type | Default value |
|---|---|---|
keepMs | number | 1000 |
Returns ​
LagCompensator
Properties ​
keepMs ​
readonly keepMs: number = 1000;Defined in: src/net/LagCompensator.ts:39
Accessors ​
newest ​
Get Signature ​
get newest(): number;Defined in: src/net/LagCompensator.ts:42
The latest recorded time, or -Infinity before any.
Returns ​
number
oldest ​
Get Signature ​
get oldest(): number;Defined in: src/net/LagCompensator.ts:46
Returns ​
number
Methods ​
all() ​
all(time): RewoundBody[];Defined in: src/net/LagCompensator.ts:92
Every body in the history at time.
Parameters ​
| Parameter | Type |
|---|---|
time | number |
Returns ​
alongRay() ​
alongRay(
time,
x,
y,
dx,
dy,
range,
radius,
except?
): RewoundHit[];Defined in: src/net/LagCompensator.ts:115
A hitscan at time: the bodies a ray from (x, y) along (dx, dy) passed within radius of, before range px, in the order it met them. except (the shooter) is left out. Walls are the game's to check against the first hit.
Parameters ​
| Parameter | Type |
|---|---|
time | number |
x | number |
y | number |
dx | number |
dy | number |
range | number |
radius | number |
except? | number |
Returns ​
at() ​
at(id, time):
| {
x: number;
y: number;
}
| null;Defined in: src/net/LagCompensator.ts:85
Where body id was at time, blended between the frames around it as InterpolationBuffer blends; null when the history has no frame of it. Before the oldest frame it is where it was then; after the newest, where it is.
Parameters ​
| Parameter | Type |
|---|---|
id | number |
time | number |
Returns ​
| { x: number; y: number; } | null
clear() ​
clear(): void;Defined in: src/net/LagCompensator.ts:68
Returns ​
void
forget() ​
forget(id): void;Defined in: src/net/LagCompensator.ts:64
Forgets a body everywhere in the history (it left the game, not just the view).
Parameters ​
| Parameter | Type |
|---|---|
id | number |
Returns ​
void
near() ​
near(
time,
x,
y,
radius,
except?
): RewoundBody[];Defined in: src/net/LagCompensator.ts:106
The bodies whose centre was within radius of (x, y) at time, nearest first; except (the shooter) left out.
Parameters ​
| Parameter | Type |
|---|---|
time | number |
x | number |
y | number |
radius | number |
except? | number |
Returns ​
record() ​
record(time, bodies): void;Defined in: src/net/LagCompensator.ts:51
Records every body's position at time. A time not after the last one recorded replaces that frame.
Parameters ​
| Parameter | Type |
|---|---|
time | number |
bodies | Iterable<{ id: number; x: number; y: number; }> |
Returns ​
void
clampTime() ​
static clampTime(
claimed,
now,
maxRewindMs
): number;Defined in: src/net/LagCompensator.ts:76
A time a client claims it saw, kept within [now - maxRewindMs, now]: the window a rewind is honoured in. A claim that is not a finite number is taken as now (no rewind).
Parameters ​
| Parameter | Type |
|---|---|
claimed | unknown |
now | number |
maxRewindMs | number |
Returns ​
number