Skip to content

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.

ts
// 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 ​

ts
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 ​

ParameterTypeDefault value
keepMsnumber1000

Returns ​

LagCompensator

Properties ​

keepMs ​

ts
readonly keepMs: number = 1000;

Defined in: src/net/LagCompensator.ts:39

Accessors ​

newest ​

Get Signature ​

ts
get newest(): number;

Defined in: src/net/LagCompensator.ts:42

The latest recorded time, or -Infinity before any.

Returns ​

number


oldest ​

Get Signature ​

ts
get oldest(): number;

Defined in: src/net/LagCompensator.ts:46

Returns ​

number

Methods ​

all() ​

ts
all(time): RewoundBody[];

Defined in: src/net/LagCompensator.ts:92

Every body in the history at time.

Parameters ​

ParameterType
timenumber

Returns ​

RewoundBody[]


alongRay() ​

ts
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 ​

ParameterType
timenumber
xnumber
ynumber
dxnumber
dynumber
rangenumber
radiusnumber
except?number

Returns ​

RewoundHit[]


at() ​

ts
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 ​

ParameterType
idnumber
timenumber

Returns ​

| { x: number; y: number; } | null


clear() ​

ts
clear(): void;

Defined in: src/net/LagCompensator.ts:68

Returns ​

void


forget() ​

ts
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 ​

ParameterType
idnumber

Returns ​

void


near() ​

ts
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 ​

ParameterType
timenumber
xnumber
ynumber
radiusnumber
except?number

Returns ​

RewoundBody[]


record() ​

ts
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 ​

ParameterType
timenumber
bodiesIterable<{ id: number; x: number; y: number; }>

Returns ​

void


clampTime() ​

ts
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 ​

ParameterType
claimedunknown
nownumber
maxRewindMsnumber

Returns ​

number

Released under the MIT License.