public Fix64 CalcMaxLag() { int maxLagSteps = 0; foreach (var item in commands) { int receivingStep = item.Key; List <ZeroLagCommand> currStepReceivedCommands = item.Value; foreach (var command in currStepReceivedCommands) { int currCommandLag = receivingStep - command.stepInd; maxLagSteps = Math.Max(currCommandLag, maxLagSteps); } } var replay = FlatternToReplay(); foreach (var item in timeouts) { int receivingStep = item.Key; List <TimeoutCommand> currStepReceivedTimeouts = item.Value; currStepReceivedTimeouts.ForEach(timeout => { ZeroLagCommand command = replay.GetTimeoutedCommand(timeout); int currCommandLag = receivingStep - command.stepInd; maxLagSteps = Math.Max(currCommandLag, maxLagSteps); }); } return(maxLagSteps * settings.fixedDt); }
public Replay <T, S> FlatternToReplay() { Replay <T, S> replay = new Replay <T, S>(fixedDt, settings, stepDts); foreach (var item in commands) { List <ZeroLagCommand> currStepReceivedCommands = item.Value; foreach (var command in currStepReceivedCommands) { replay.ReceiveCommand(command); } } foreach (var timeoutsReceivedOnCurrStep in timeouts.Values) { foreach (var timeout in timeoutsReceivedOnCurrStep) { ZeroLagCommand targetCommand = replay.GetTimeoutedCommand(timeout); targetCommand.OnTimedout(timeout); var targetStepCommands = replay.commands[timeout.targetCommandStep]; targetStepCommands.RemoveOne(cmd => cmd.hash == timeout.targetCommandHash); if (timeout.whatToDo == ActionOnTimeout.ExecuteLater) { replay.ReceiveCommand(targetCommand); // Receive command later. } } } return(replay); }
private bool DebugCheckCommandAllowed(ZeroLagCommand command) { if (debugMaxInputStep == -1) { return(true); } return(command.stepInd <= debugMaxInputStep); }
public virtual ZeroLagCommand GetTimeoutedCommand(TimeoutCommand timeout) { List <ZeroLagCommand> currTurnCommands; if (!commands.TryGetValue(timeout.targetCommandStep, out currTurnCommands)) { return(null); } ZeroLagCommand targetCommand = currTurnCommands.Find(cmd => cmd.hash == timeout.targetCommandHash); return(targetCommand); }
public void ReceiveCommand(ZeroLagCommand command) { List <ZeroLagCommand> currTurnCommands; int commandKey = GetCommandKey(command); //Debug.Log(commandKey.ToString()); if (!commands.TryGetValue(commandKey, out currTurnCommands)) { currTurnCommands = new List <ZeroLagCommand>(); commands.Add(commandKey, currTurnCommands); } command.hash = command.CalculateHash(); currTurnCommands.InsertSorted(command, cmd => cmd.hash); }
/// <summary> /// Each command that received from other PredictiveModels should go here. /// </summary> public virtual void ReceiveCommand(ZeroLagCommand command) { if (!DebugCheckCommandAllowed(command)) { return; } if (stopped) { return; } playedGameReplay.ReceiveCommand(command); // Declare as not actualized all steps after this command's step. // if (command.stepInd == -1) // throw exception. lastActualStep = Math.Min(lastActualStep, command.stepInd); }
private bool TryConsumeTimeout(TimeoutCommand timeout) { ZeroLagCommand targetCommand = playedGameReplay.GetTimeoutedCommand(timeout); if (targetCommand == null) { return(false); } int targetCommandStep = targetCommand.stepInd; lastActualStep = Math.Min(lastActualStep, targetCommandStep); targetCommand.OnTimedout(timeout); playedGameReplay.commands[targetCommandStep].RemoveOne(cmd => cmd.hash == timeout.targetCommandHash); if (timeout.whatToDo == ActionOnTimeout.ExecuteLater) { ReceiveCommand(targetCommand); // Receive command later. } return(true); }
/// <summary> /// Commands saved by step they should be executed in. /// </summary> protected virtual int GetCommandKey(ZeroLagCommand command) => command.stepInd;
/// <summary> /// Commands saved by step they received in. /// </summary> protected override int GetCommandKey(ZeroLagCommand command) => getCurrStep();
public override void ReceiveCommand(ZeroLagCommand command) { lock (commandsBufferLock) commandsThreadBuffer.Add(command); }