2020-01-29 03:51:15 +00:00
|
|
|
|
2020-06-07 05:56:06 +00:00
|
|
|
export const TICK_LENGTH = 33; // roughly 30fps
|
2020-02-16 00:52:00 +00:00
|
|
|
|
2020-05-02 02:51:21 +00:00
|
|
|
export interface LockstepProcessor<LocalInput, GlobalInput, State> {
|
|
|
|
compareInput(a: GlobalInput, b: GlobalInput): boolean;
|
2020-05-18 23:32:56 +00:00
|
|
|
predictInput(prev: GlobalInput | undefined, localPlayer: number, localInput: LocalInput): GlobalInput;
|
2020-02-15 18:54:59 +00:00
|
|
|
cloneState(source: State): State;
|
2020-05-02 02:51:21 +00:00
|
|
|
advanceState(state: State, input: GlobalInput): void;
|
2020-02-15 18:54:59 +00:00
|
|
|
}
|
2020-01-29 03:51:15 +00:00
|
|
|
|
2020-05-02 02:51:21 +00:00
|
|
|
export class LockstepState<LocalInput, GlobalInput, State> {
|
2020-01-29 03:51:15 +00:00
|
|
|
|
|
|
|
private inputIndex = -1;
|
2020-05-02 02:51:21 +00:00
|
|
|
private inputLog: GlobalInput[] = [];
|
2020-01-29 03:51:15 +00:00
|
|
|
private canonIndex = -1;
|
|
|
|
private canonState: State;
|
|
|
|
private renderIndex = -1;
|
|
|
|
private renderState: State;
|
|
|
|
|
2020-05-02 02:51:21 +00:00
|
|
|
constructor(private initialState: State, private engine: LockstepProcessor<LocalInput, GlobalInput, State>) {
|
2020-02-15 18:54:59 +00:00
|
|
|
this.canonState = engine.cloneState(initialState);
|
|
|
|
this.renderState = engine.cloneState(initialState);
|
2020-01-29 03:51:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 02:51:21 +00:00
|
|
|
public addCanonInput(input: GlobalInput): void {
|
2020-01-29 03:51:15 +00:00
|
|
|
this.canonIndex++;
|
|
|
|
|
|
|
|
// advance canonical game state
|
2020-02-15 18:54:59 +00:00
|
|
|
this.engine.advanceState(this.canonState, input);
|
2020-01-29 03:51:15 +00:00
|
|
|
|
|
|
|
if(this.canonIndex <= this.renderIndex) {
|
|
|
|
// we're rendering predicted states, so if the input changes we need to invalidate the rendered state
|
2020-02-15 18:54:59 +00:00
|
|
|
if(!this.engine.compareInput(this.inputLog[this.canonIndex], input)) {
|
|
|
|
this.renderState = this.engine.cloneState(this.canonState);
|
2020-01-29 03:51:15 +00:00
|
|
|
this.renderIndex = this.canonIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.inputLog[this.canonIndex] = input;
|
|
|
|
}
|
|
|
|
|
2020-05-02 01:45:31 +00:00
|
|
|
/** Warning: this only supports one player input per frame. There is no support for two players using the same lockstep simulation instance. */
|
2020-05-02 02:51:21 +00:00
|
|
|
public addLocalInput(player: number, input: LocalInput): void {
|
2020-01-29 03:51:15 +00:00
|
|
|
this.inputIndex++;
|
|
|
|
|
|
|
|
// ensure that we don't overwrite the canon input with local input somehow
|
|
|
|
// (probably only possible in situations where game is unplayable anyways? but still for sanity.)
|
|
|
|
if(this.inputIndex > this.canonIndex) {
|
2020-05-18 23:32:56 +00:00
|
|
|
this.inputLog[this.inputIndex] = this.engine.predictInput(this.inputLog[this.inputIndex - 1] ?? undefined, player, input);
|
2020-01-29 03:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do any necessary simulation to catchup the predicted game state to the frame
|
|
|
|
* that should be rendered, then return it.
|
|
|
|
*/
|
|
|
|
public getStateToRender(): State {
|
|
|
|
// TODO: input lag by X frames
|
|
|
|
const targetIndex = this.inputLog.length - 1;
|
|
|
|
|
|
|
|
while(this.renderIndex < targetIndex) {
|
|
|
|
this.renderIndex++;
|
2020-02-15 18:54:59 +00:00
|
|
|
this.engine.advanceState(this.renderState, this.inputLog[this.renderIndex]);
|
2020-01-29 03:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.renderState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 02:51:21 +00:00
|
|
|
export class Playback<LocalInput, GlobalInput, State> {
|
2020-02-15 19:12:53 +00:00
|
|
|
private frame = 0;
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
private state: State,
|
2020-05-02 02:51:21 +00:00
|
|
|
private inputLog: GlobalInput[],
|
|
|
|
private engine: LockstepProcessor<LocalInput, GlobalInput, State>,
|
2020-02-15 19:12:53 +00:00
|
|
|
) {}
|
|
|
|
|
|
|
|
public getNextState(): State {
|
|
|
|
if(this.frame < this.inputLog.length) {
|
|
|
|
this.engine.advanceState(this.state, this.inputLog[this.frame]);
|
|
|
|
this.frame++;
|
|
|
|
}
|
|
|
|
return this.state;
|
|
|
|
}
|
|
|
|
}
|