public bool OnUpdate(float deltaTime) { _stopwatch.Stop(); double elapsed = _stopwatch.Elapsed.TotalSeconds; _stopwatch.Restart(); if (_game.type == FrameSyncGameType.Online && _game.gameState == FrameSyncGameState.Running) { _inputSampleTimer += deltaTime; int serverPlayerFrameCount = PlayerFrameCountOnServer; int localServerFrameCount = LocalServerFrameCount; bool adjusted = false; if (_game.clientSidePrediction) { FrameSyncTime.DoFixedTickIfNecessary((float)elapsed, serverPlayerFrameCount, () => { if (_localCounter == 0) { _stopwatch1.Start(); } _localCounter++; SWConsole.Debug($"=====[OnUpdate] serverPlayerFrameCount={serverPlayerFrameCount} ====="); FlushInputOnlinePrediction(); RunningOnlineWithPrediction(); if (_localCounter == 900) { _stopwatch1.Stop(); double elapsed1 = _stopwatch1.Elapsed.TotalSeconds; //SWConsole.Error($"elapsed={elapsed1}"); } }); } else { adjusted = FrameSyncTime.Adjust(serverPlayerFrameCount, localServerFrameCount, deltaTime); if (_inputSampleTimer > FrameSyncTime.internalInputSampleInterval) { _inputSampleTimer = 0; FlushInputOnline(); } } return(adjusted); } return(false); }
public static bool AdjustPrediction(int serverPlayerFrameCount, int localServerFrameCount, float deltaTime) { _adjustTimer += deltaTime; if (_adjustTimer > _adjustInterval) { _adjustTimer = 0; UpdateLocalServerFrameCount(localServerFrameCount); UpdateServerPlayerFrameCount(serverPlayerFrameCount); SWConsole.Debug($"[internalInputSampleInterval]"); internalInputSampleInterval = Optimize( internalInputSampleInterval, _avgServerPlayerFrameCount, _previousAvgServerPlayerFrameCount, FrameSyncConstant.OPTIMIZED_SERVER_PREDICTION_PLAYER_FRAME_COUNT, _maxDeltaTime, _minDeltaTime, 0.05f, 0.2f, FrameSyncConstant.DYNAMIC_ADJUST_SMALL_STEP, FrameSyncConstant.DYNAMIC_ADJUST_STEP, true); _previousAvgServerPlayerFrameCount = _avgServerPlayerFrameCount; //SWConsole.Debug($"[internalFixedDeltaTime]"); //internalFixedDeltaTime = Optimize( // internalFixedDeltaTime, // _avgLocalServerFrameCount, // _previousAvgLocalServerFrameCount, // FrameSyncConstant.OPTIMIZED_LOCAL_PREDICTION_FRAME_COUNT, // _maxDeltaTime, // _minDeltaTime, // 0.1f, // 0.1f, // FrameSyncConstant.DYNAMIC_ADJUST_SMALL_STEP, // FrameSyncConstant.DYNAMIC_ADJUST_STEP, // false); //_previousAvgLocalServerFrameCount = _avgLocalServerFrameCount; return(false); } return(false); }
static float Optimize( float currentTime, float value, float previousValue, float optimizedValue, float maxValue, float minValue, float optimizerDelta, float countDelta, float smallStep, float largeStep, bool reversed) { float delta = previousValue - value; SWConsole.Debug($"=========================Optimize================================="); SWConsole.Debug($"previousValue={previousValue}"); SWConsole.Debug($"value={value}"); SWConsole.Debug($"delta={delta}"); OptimizerState optimizerState = OptimizerState.Constant; if (delta > optimizerDelta) { optimizerState = OptimizerState.Decreasing; } else if (delta < -optimizerDelta) { optimizerState = OptimizerState.Increasing; } float deltaToOptimized = optimizedValue - value; OptimizerCountState countState = OptimizerCountState.Optimized; if (deltaToOptimized > countDelta) { countState = OptimizerCountState.NotEnough; } else if (deltaToOptimized < -countDelta) { countState = OptimizerCountState.TooMany; } bool shouldSlowDown = false; bool shouldSlowDownSmall = false; bool shouldSpeedUpSmall = false; bool shouldSpeedUp = false; if (reversed) { shouldSpeedUp = (countState == OptimizerCountState.NotEnough && optimizerState == OptimizerState.Decreasing); shouldSpeedUp |= (countState == OptimizerCountState.Optimized && optimizerState == OptimizerState.Decreasing); shouldSlowDown = (countState == OptimizerCountState.TooMany && optimizerState == OptimizerState.Increasing); shouldSlowDown |= (countState == OptimizerCountState.Optimized && optimizerState == OptimizerState.Increasing); shouldSpeedUpSmall = (optimizerState == OptimizerState.Constant && countState == OptimizerCountState.NotEnough); shouldSlowDownSmall = (optimizerState == OptimizerState.Constant && countState == OptimizerCountState.TooMany); } else { shouldSlowDown = (countState == OptimizerCountState.NotEnough && optimizerState == OptimizerState.Decreasing); shouldSlowDown |= (countState == OptimizerCountState.Optimized && optimizerState == OptimizerState.Decreasing); shouldSpeedUp = (countState == OptimizerCountState.TooMany && optimizerState == OptimizerState.Increasing); shouldSpeedUp |= (countState == OptimizerCountState.Optimized && optimizerState == OptimizerState.Increasing); shouldSlowDownSmall = (optimizerState == OptimizerState.Constant && countState == OptimizerCountState.NotEnough); shouldSpeedUpSmall = (optimizerState == OptimizerState.Constant && countState == OptimizerCountState.TooMany); } SWConsole.Debug($"optimizerState={optimizerState}"); SWConsole.Debug($"countState={countState}"); float result = currentTime; if (shouldSpeedUp) { result = currentTime * largeStep; if (result < minValue) { result = minValue; } SWConsole.Debug($"shouldSpeedUp"); } else if (shouldSpeedUpSmall) { result = currentTime * smallStep; if (result < minValue) { result = minValue; } SWConsole.Debug($"shouldSpeedUpSmall"); } else if (shouldSlowDown) { result = currentTime / largeStep; if (result > maxValue) { result = maxValue; } SWConsole.Debug($"shouldSlowDown"); } else if (shouldSlowDownSmall) { result = currentTime / smallStep; if (result > maxValue) { result = maxValue; } SWConsole.Debug($"shouldSlowDownSmall"); } SWConsole.Debug($"result={result}"); return(result); }