2021-02-10 19:34:41 +00:00
|
|
|
|
using OWML.Common;
|
2021-08-09 11:49:58 +01:00
|
|
|
|
using QSB.ClientServerStateSync;
|
2021-02-10 19:34:41 +00:00
|
|
|
|
using QSB.DeathSync;
|
2021-06-11 14:34:27 +01:00
|
|
|
|
using QSB.Inputs;
|
2021-12-23 17:07:29 -08:00
|
|
|
|
using QSB.Messaging;
|
2021-08-08 20:02:46 +01:00
|
|
|
|
using QSB.Player;
|
2021-12-23 17:07:29 -08:00
|
|
|
|
using QSB.Player.Messages;
|
|
|
|
|
using QSB.TimeSync.Messages;
|
2021-02-10 19:34:41 +00:00
|
|
|
|
using QSB.Utility;
|
2020-12-04 22:15:41 +00:00
|
|
|
|
using QuantumUNET;
|
2021-08-08 20:02:46 +01:00
|
|
|
|
using System;
|
2021-08-09 11:49:58 +01:00
|
|
|
|
using UnityEngine;
|
2020-02-14 22:14:24 +01:00
|
|
|
|
|
2020-02-24 19:55:16 +01:00
|
|
|
|
namespace QSB.TimeSync
|
2020-02-15 20:48:02 +01:00
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
public class WakeUpSync : QNetworkBehaviour
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
public static WakeUpSync LocalInstance { get; private set; }
|
|
|
|
|
|
2021-03-26 14:44:43 +00:00
|
|
|
|
private const float PauseOrFastForwardThreshold = 1.0f;
|
2021-03-25 23:07:53 +00:00
|
|
|
|
private const float TimescaleBounds = 0.3f;
|
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private const float MaxFastForwardSpeed = 60f;
|
|
|
|
|
private const float MaxFastForwardDiff = 20f;
|
|
|
|
|
private const float MinFastForwardSpeed = 2f;
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
public enum State { NotLoaded, Loaded, FastForwarding, Pausing }
|
2020-12-03 08:28:05 +00:00
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
public State CurrentState { get; private set; } = State.NotLoaded;
|
|
|
|
|
public Enum CurrentReason { get; private set; }
|
2020-12-02 21:29:53 +00:00
|
|
|
|
|
|
|
|
|
private float _sendTimer;
|
|
|
|
|
private float _serverTime;
|
|
|
|
|
private int _serverLoopCount;
|
2021-05-25 10:04:07 +01:00
|
|
|
|
private bool _hasWokenUp;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
|
2020-12-18 20:28:22 +00:00
|
|
|
|
public override void OnStartLocalPlayer() => LocalInstance = this;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
|
2021-11-11 19:34:25 +00:00
|
|
|
|
public void OnDisconnect()
|
|
|
|
|
{
|
|
|
|
|
OWTime.SetTimeScale(1f);
|
|
|
|
|
OWTime.SetMaxDeltaTime(0.06666667f);
|
|
|
|
|
OWTime.SetFixedTimestep(0.01666667f);
|
|
|
|
|
Locator.GetActiveCamera().enabled = true;
|
|
|
|
|
CurrentState = State.NotLoaded;
|
|
|
|
|
CurrentReason = null;
|
|
|
|
|
|
|
|
|
|
Physics.SyncTransforms();
|
|
|
|
|
SpinnerUI.Hide();
|
|
|
|
|
TimeSyncUI.Stop();
|
|
|
|
|
|
|
|
|
|
QSBInputManager.Instance.SetInputsEnabled(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 21:41:56 +01:00
|
|
|
|
public void Start()
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (!IsLocalPlayer)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (QSBSceneManager.IsInUniverse)
|
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
|
|
|
|
|
|
2021-12-26 00:53:43 -08:00
|
|
|
|
GlobalMessenger.AddListener(OWEvents.WakeUp, OnWakeUp);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 23:07:53 +00:00
|
|
|
|
public float GetTimeDifference()
|
|
|
|
|
{
|
|
|
|
|
var myTime = Time.timeSinceLevelLoad;
|
|
|
|
|
return myTime - _serverTime;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private void OnWakeUp()
|
|
|
|
|
{
|
2021-02-14 09:43:36 +00:00
|
|
|
|
DebugLog.DebugWrite($"OnWakeUp", MessageType.Info);
|
2020-12-23 12:58:45 +00:00
|
|
|
|
if (QNetworkServer.active)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2021-02-06 22:03:10 +00:00
|
|
|
|
RespawnOnDeath.Instance.Init();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-05-25 10:04:07 +01:00
|
|
|
|
_hasWokenUp = true;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 21:41:56 +01:00
|
|
|
|
public void OnDestroy()
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
|
2021-12-26 00:53:43 -08:00
|
|
|
|
GlobalMessenger.RemoveListener(OWEvents.WakeUp, OnWakeUp);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
private void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2021-05-25 10:04:07 +01:00
|
|
|
|
_hasWokenUp = false;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
if (isInUniverse)
|
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (newScene == OWScene.EyeOfTheUniverse)
|
2021-05-25 10:04:07 +01:00
|
|
|
|
{
|
|
|
|
|
_hasWokenUp = true;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
CurrentState = State.NotLoaded;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2021-12-22 18:20:53 -08:00
|
|
|
|
new RequestStateResyncMessage().Send();
|
2021-08-08 20:02:46 +01:00
|
|
|
|
CurrentState = State.Loaded;
|
2021-05-08 16:57:01 +01:00
|
|
|
|
gameObject.GetAddComponent<PreserveTimeScale>();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
if (IsServer)
|
|
|
|
|
{
|
|
|
|
|
SendServerTime();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-11-25 23:52:46 +00:00
|
|
|
|
if (!QSBCore.SkipTitleScreen)
|
|
|
|
|
{
|
|
|
|
|
WakeUpOrSleep();
|
|
|
|
|
}
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 11:26:05 +01:00
|
|
|
|
private void SendServerTime()
|
2021-12-22 21:06:58 -08:00
|
|
|
|
=> new ServerTimeMessage(_serverTime, PlayerData.LoadLoopCount()).Send();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
|
2021-12-22 21:06:58 -08:00
|
|
|
|
public void OnClientReceiveMessage(float time, int count)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2021-12-22 21:06:58 -08:00
|
|
|
|
_serverTime = time;
|
|
|
|
|
_serverLoopCount = count;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WakeUpOrSleep()
|
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (CurrentState == State.NotLoaded)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (PlayerData.LoadLoopCount() != _serverLoopCount && !IsServer)
|
2021-05-09 19:55:19 +01:00
|
|
|
|
{
|
2021-06-23 21:46:52 +01:00
|
|
|
|
DebugLog.ToConsole($"Warning - ServerLoopCount is not the same as local loop count! local:{PlayerData.LoadLoopCount()} server:{_serverLoopCount}");
|
2021-05-09 19:55:19 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
var myTime = Time.timeSinceLevelLoad;
|
|
|
|
|
var diff = myTime - _serverTime;
|
|
|
|
|
|
2021-12-10 22:13:39 +00:00
|
|
|
|
if (ServerStateManager.Instance.GetServerState() is not ServerState.InSolarSystem and not ServerState.InEye)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 23:07:53 +00:00
|
|
|
|
if (diff > PauseOrFastForwardThreshold)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
StartPausing(PauseReason.TooFarAhead);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 23:07:53 +00:00
|
|
|
|
if (diff < -PauseOrFastForwardThreshold)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
StartFastForwarding(FastForwardReason.TooFarBehind);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
private void StartFastForwarding(FastForwardReason reason)
|
2021-05-09 19:55:19 +01:00
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (CurrentState == State.FastForwarding)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2020-12-19 19:06:13 +00:00
|
|
|
|
TimeSyncUI.TargetTime = _serverTime;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-02-10 19:34:41 +00:00
|
|
|
|
DebugLog.DebugWrite($"START FASTFORWARD (Target:{_serverTime} Current:{Time.timeSinceLevelLoad})", MessageType.Info);
|
2021-03-08 20:35:36 +00:00
|
|
|
|
if (Locator.GetActiveCamera() != null)
|
2021-02-28 14:43:05 +00:00
|
|
|
|
{
|
2021-03-08 20:35:36 +00:00
|
|
|
|
Locator.GetActiveCamera().enabled = false;
|
2021-02-28 14:43:05 +00:00
|
|
|
|
}
|
2021-06-11 14:34:27 +01:00
|
|
|
|
|
|
|
|
|
//OWInput.ChangeInputMode(InputMode.None);
|
|
|
|
|
QSBInputManager.Instance.SetInputsEnabled(false);
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
CurrentState = State.FastForwarding;
|
|
|
|
|
CurrentReason = reason;
|
2021-02-28 14:43:05 +00:00
|
|
|
|
OWTime.SetMaxDeltaTime(0.033333335f);
|
2021-03-08 20:35:36 +00:00
|
|
|
|
OWTime.SetFixedTimestep(0.033333335f);
|
2020-12-19 19:06:13 +00:00
|
|
|
|
TimeSyncUI.TargetTime = _serverTime;
|
2021-08-08 20:02:46 +01:00
|
|
|
|
TimeSyncUI.Start(TimeSyncType.Fastforwarding, FastForwardReason.TooFarBehind);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
private void StartPausing(PauseReason reason)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (CurrentState == State.Pausing)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-02-10 19:34:41 +00:00
|
|
|
|
DebugLog.DebugWrite($"START PAUSING (Target:{_serverTime} Current:{Time.timeSinceLevelLoad})", MessageType.Info);
|
2021-03-08 20:35:36 +00:00
|
|
|
|
Locator.GetActiveCamera().enabled = false;
|
2021-06-11 14:34:27 +01:00
|
|
|
|
|
|
|
|
|
//OWInput.ChangeInputMode(InputMode.None);
|
|
|
|
|
QSBInputManager.Instance.SetInputsEnabled(false);
|
|
|
|
|
|
2021-03-26 14:44:43 +00:00
|
|
|
|
OWTime.SetTimeScale(0f);
|
2021-08-08 20:02:46 +01:00
|
|
|
|
CurrentState = State.Pausing;
|
|
|
|
|
CurrentReason = reason;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
SpinnerUI.Show();
|
2021-08-08 20:02:46 +01:00
|
|
|
|
TimeSyncUI.Start(TimeSyncType.Pausing, reason);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ResetTimeScale()
|
|
|
|
|
{
|
2021-03-26 14:44:43 +00:00
|
|
|
|
OWTime.SetTimeScale(1f);
|
2021-02-28 14:43:05 +00:00
|
|
|
|
OWTime.SetMaxDeltaTime(0.06666667f);
|
2021-03-08 19:31:46 +00:00
|
|
|
|
OWTime.SetFixedTimestep(0.01666667f);
|
2021-03-08 20:35:36 +00:00
|
|
|
|
Locator.GetActiveCamera().enabled = true;
|
2021-08-08 20:02:46 +01:00
|
|
|
|
CurrentState = State.Loaded;
|
|
|
|
|
CurrentReason = null;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
|
2021-02-10 19:34:41 +00:00
|
|
|
|
DebugLog.DebugWrite($"RESET TIMESCALE", MessageType.Info);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
Physics.SyncTransforms();
|
|
|
|
|
SpinnerUI.Hide();
|
|
|
|
|
TimeSyncUI.Stop();
|
2021-12-22 18:20:53 -08:00
|
|
|
|
new RequestStateResyncMessage().Send();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
RespawnOnDeath.Instance.Init();
|
2021-05-25 10:04:07 +01:00
|
|
|
|
|
2021-06-11 14:34:27 +01:00
|
|
|
|
QSBInputManager.Instance.SetInputsEnabled(true);
|
2021-05-25 10:04:07 +01:00
|
|
|
|
|
|
|
|
|
if (!_hasWokenUp)
|
|
|
|
|
{
|
|
|
|
|
WakeUp();
|
|
|
|
|
}
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 11:26:05 +01:00
|
|
|
|
private void WakeUp()
|
2021-12-26 21:05:07 -08:00
|
|
|
|
=> Locator.GetPlayerCamera().GetComponent<PlayerCameraEffectController>().WakeUp();
|
2021-05-25 10:04:07 +01:00
|
|
|
|
|
2020-12-14 21:41:56 +01:00
|
|
|
|
public void Update()
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (IsServer)
|
|
|
|
|
{
|
|
|
|
|
UpdateServer();
|
|
|
|
|
}
|
2021-11-25 23:52:46 +00:00
|
|
|
|
else if (IsLocalPlayer && !QSBCore.AvoidTimeSync)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
UpdateClient();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateServer()
|
|
|
|
|
{
|
2021-03-25 23:07:53 +00:00
|
|
|
|
_serverTime = Time.timeSinceLevelLoad;
|
2021-08-22 16:54:48 +01:00
|
|
|
|
|
2021-11-24 22:16:56 +00:00
|
|
|
|
if (ServerStateManager.Instance == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Warning - ServerStateManager.Instance is null!", MessageType.Warning);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (QSBPlayerManager.LocalPlayer == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Warning - LocalPlayer is null!", MessageType.Warning);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
var serverState = ServerStateManager.Instance.GetServerState();
|
|
|
|
|
var clientState = QSBPlayerManager.LocalPlayer.State;
|
|
|
|
|
|
2021-12-21 20:05:06 +00:00
|
|
|
|
if (serverState == ServerState.WaitingForAllPlayersToReady && clientState == ClientState.WaitingForOthersToBeReady)
|
2021-08-08 20:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
if (CurrentState != State.Pausing)
|
|
|
|
|
{
|
|
|
|
|
StartPausing(PauseReason.WaitingForAllPlayersToBeReady);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CurrentState == State.Pausing && (PauseReason)CurrentReason == PauseReason.WaitingForAllPlayersToBeReady)
|
|
|
|
|
{
|
|
|
|
|
if (clientState == ClientState.AliveInSolarSystem && serverState == ServerState.InSolarSystem)
|
|
|
|
|
{
|
|
|
|
|
ResetTimeScale();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:05:06 +00:00
|
|
|
|
if (serverState == ServerState.WaitingForAllPlayersToDie && clientState == ClientState.WaitingForOthersToBeReady)
|
2021-10-25 16:31:15 +01:00
|
|
|
|
{
|
|
|
|
|
if (CurrentState == State.Pausing && (PauseReason)CurrentReason == PauseReason.WaitingForAllPlayersToBeReady)
|
|
|
|
|
{
|
|
|
|
|
//?
|
|
|
|
|
DebugLog.ToConsole($"Warning - Server waiting for players to die, but players waiting for ready signal! Assume players correct.", MessageType.Warning);
|
2021-12-22 18:37:24 -08:00
|
|
|
|
ServerStateManager.Instance.SendChangeServerStateMessage(ServerState.WaitingForAllPlayersToReady);
|
2021-10-25 16:31:15 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (CurrentState != State.Loaded)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_sendTimer += Time.unscaledDeltaTime;
|
|
|
|
|
if (_sendTimer > 1)
|
|
|
|
|
{
|
|
|
|
|
SendServerTime();
|
|
|
|
|
_sendTimer = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
private void UpdateClient()
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
_serverTime += Time.unscaledDeltaTime;
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
var serverState = ServerStateManager.Instance.GetServerState();
|
|
|
|
|
var clientState = QSBPlayerManager.LocalPlayer.State;
|
2021-09-25 09:49:03 +01:00
|
|
|
|
var currentScene = QSBSceneManager.CurrentScene;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
// set fastforwarding timescale
|
|
|
|
|
|
|
|
|
|
if (CurrentState == State.FastForwarding)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2021-02-28 14:43:05 +00:00
|
|
|
|
if (Locator.GetPlayerCamera() != null && !Locator.GetPlayerCamera().enabled)
|
|
|
|
|
{
|
|
|
|
|
Locator.GetPlayerCamera().enabled = false;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
var diff = _serverTime - Time.timeSinceLevelLoad;
|
2021-03-26 14:44:43 +00:00
|
|
|
|
OWTime.SetTimeScale(Mathf.SmoothStep(MinFastForwardSpeed, MaxFastForwardSpeed, Mathf.Abs(diff) / MaxFastForwardDiff));
|
2020-12-02 21:29:53 +00:00
|
|
|
|
|
2021-07-29 23:44:59 +01:00
|
|
|
|
TimeSyncUI.TargetTime = _serverTime;
|
2021-08-08 20:02:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CurrentState != State.Loaded && CurrentState != State.NotLoaded && CurrentReason == null)
|
|
|
|
|
{
|
2021-10-24 10:47:25 +01:00
|
|
|
|
DebugLog.ToConsole($"Warning - CurrentReason is null.", MessageType.Warning);
|
2021-08-08 20:02:46 +01:00
|
|
|
|
}
|
2021-07-29 23:44:59 +01:00
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
// Checks to pause/fastforward
|
|
|
|
|
|
2021-11-20 19:49:50 +00:00
|
|
|
|
if (clientState is ClientState.NotLoaded or ClientState.InTitleScreen)
|
2021-08-08 20:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 09:49:03 +01:00
|
|
|
|
if (serverState == ServerState.NotLoaded && CurrentState != State.Pausing && QSBSceneManager.IsInUniverse)
|
2021-08-08 20:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
StartPausing(PauseReason.ServerNotStarted);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:05:06 +00:00
|
|
|
|
if (serverState == ServerState.WaitingForAllPlayersToReady && CurrentState != State.Pausing && clientState == ClientState.WaitingForOthersToBeReady)
|
2021-08-08 20:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
StartPausing(PauseReason.WaitingForAllPlayersToBeReady);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 20:05:06 +00:00
|
|
|
|
if (serverState == ServerState.WaitingForAllPlayersToDie && clientState == ClientState.WaitingForOthersToBeReady)
|
2021-08-08 20:02:46 +01:00
|
|
|
|
{
|
|
|
|
|
StartPausing(PauseReason.WaitingForAllPlayersToBeReady);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checks to revert to normal
|
|
|
|
|
|
|
|
|
|
if (CurrentState == State.Pausing && (PauseReason)CurrentReason == PauseReason.ServerNotStarted)
|
|
|
|
|
{
|
|
|
|
|
if (serverState != ServerState.NotLoaded)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
ResetTimeScale();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (CurrentState == State.Pausing && (PauseReason)CurrentReason == PauseReason.WaitingForAllPlayersToBeReady)
|
|
|
|
|
{
|
|
|
|
|
if (clientState == ClientState.AliveInSolarSystem && serverState == ServerState.InSolarSystem)
|
|
|
|
|
{
|
|
|
|
|
ResetTimeScale();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-02 21:29:53 +00:00
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (CurrentState == State.Pausing && (PauseReason)CurrentReason == PauseReason.TooFarAhead)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (Time.timeSinceLevelLoad <= _serverTime)
|
|
|
|
|
{
|
|
|
|
|
ResetTimeScale();
|
|
|
|
|
}
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2021-03-25 23:07:53 +00:00
|
|
|
|
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (CurrentState == State.FastForwarding && (FastForwardReason)CurrentReason == FastForwardReason.TooFarBehind)
|
2021-03-25 23:07:53 +00:00
|
|
|
|
{
|
2021-08-08 20:02:46 +01:00
|
|
|
|
if (Time.timeSinceLevelLoad >= _serverTime)
|
|
|
|
|
{
|
|
|
|
|
ResetTimeScale();
|
|
|
|
|
}
|
2021-03-25 23:07:53 +00:00
|
|
|
|
}
|
2021-08-10 14:54:23 +01:00
|
|
|
|
|
|
|
|
|
if (CurrentState == State.Loaded)
|
|
|
|
|
{
|
|
|
|
|
CheckTimeDifference();
|
|
|
|
|
}
|
2021-03-25 23:07:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckTimeDifference()
|
|
|
|
|
{
|
|
|
|
|
var diff = GetTimeDifference();
|
|
|
|
|
|
2021-11-20 19:49:50 +00:00
|
|
|
|
if (diff is > PauseOrFastForwardThreshold or < (-PauseOrFastForwardThreshold))
|
2021-03-25 23:07:53 +00:00
|
|
|
|
{
|
|
|
|
|
WakeUpOrSleep();
|
2021-05-09 19:55:19 +01:00
|
|
|
|
return;
|
2021-03-25 23:07:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 21:04:57 +00:00
|
|
|
|
var mappedTimescale = diff.Map(-PauseOrFastForwardThreshold, PauseOrFastForwardThreshold, 1 + TimescaleBounds, 1 - TimescaleBounds, true);
|
2021-05-08 09:30:09 +01:00
|
|
|
|
if (mappedTimescale > 100f)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Warning - CheckTimeDifference() returned over 100 - should have switched into fast-forward!", MessageType.Warning);
|
2021-06-11 14:34:27 +01:00
|
|
|
|
mappedTimescale = 0f;
|
2021-05-08 09:30:09 +01:00
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-05-08 09:30:09 +01:00
|
|
|
|
if (mappedTimescale < 0)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Warning - CheckTimeDifference() returned below 0 - should have switched into pausing!", MessageType.Warning);
|
|
|
|
|
mappedTimescale = 0f;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-03-25 23:07:53 +00:00
|
|
|
|
OWTime.SetTimeScale(mappedTimescale);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-26 13:09:34 +00:00
|
|
|
|
}
|