replace Time.time with Time.timeAsDouble - fix problems with floating-point numbers precision when game uptime is high (eg. 1 month)

This commit is contained in:
in0finite 2022-05-06 17:08:48 +02:00
parent cc79defb97
commit 01633f3817
23 changed files with 74 additions and 74 deletions

View file

@ -45,7 +45,7 @@ namespace SanAndreasUnity.Behaviours
public bool IsMinimapVisible => _canvas.enabled && this.gameObject.activeInHierarchy;
private float _timeWhenRetrievedZoneName = 0f;
private double _timeWhenRetrievedZoneName = 0f;
private string _lastZoneName = "";
@ -53,9 +53,9 @@ namespace SanAndreasUnity.Behaviours
{
get
{
if (Time.time - _timeWhenRetrievedZoneName > 2f)
if (Time.timeAsDouble - _timeWhenRetrievedZoneName > 2f)
{
_timeWhenRetrievedZoneName = Time.time;
_timeWhenRetrievedZoneName = Time.timeAsDouble;
_lastZoneName = Zone.GetZoneName(this.FocusPos);
}

View file

@ -334,12 +334,12 @@ namespace SanAndreasUnity.Behaviours
// yield until you find ground beneath or above the player, or until timeout expires
float timeStarted = Time.time;
double timeStarted = Time.timeAsDouble;
int numAttempts = 1;
while (true) {
if (Time.time - timeStarted > 4.0f) {
if (Time.timeAsDouble - timeStarted > 4.0f) {
// timeout expired
Debug.LogWarningFormat("Failed to find ground for ped {0} - timeout expired", this.DescriptionForLogging);
yield break;

View file

@ -29,8 +29,8 @@ namespace SanAndreasUnity.Behaviours
/// </summary>
public DamageInfo KillingDamageInfo { get; set; }
public float LastTimeWhenDamaged { get; private set; }
public float TimeSinceDamaged => Time.time - this.LastTimeWhenDamaged;
public double LastTimeWhenDamaged { get; private set; }
public double TimeSinceDamaged => Time.timeAsDouble - this.LastTimeWhenDamaged;
private bool m_alreadyKilled = false;
@ -53,10 +53,10 @@ namespace SanAndreasUnity.Behaviours
public struct UnderAimInfo
{
public DamageInfo damageInfo;
public float time;
public double time;
public Ped ped;
public UnderAimInfo(DamageInfo damageInfo, float time, Ped ped)
public UnderAimInfo(DamageInfo damageInfo, double time, Ped ped)
{
this.damageInfo = damageInfo;
this.time = time;
@ -150,7 +150,7 @@ namespace SanAndreasUnity.Behaviours
if (this.Health <= 0)
return;
this.LastTimeWhenDamaged = Time.time;
this.LastTimeWhenDamaged = Time.timeAsDouble;
var damageInfo = this.Damageable.LastDamageInfo;
@ -168,7 +168,7 @@ namespace SanAndreasUnity.Behaviours
public void OnReceivedDamageEventFromServer(float damageAmount, Ped attackingPed)
{
this.LastTimeWhenDamaged = Time.time;
this.LastTimeWhenDamaged = Time.timeAsDouble;
if (attackingPed != null && attackingPed.IsControlledByLocalPlayer && attackingPed != this)
{
@ -210,11 +210,11 @@ namespace SanAndreasUnity.Behaviours
int index = _underAimInfos.FindIndex(_ => _.ped == attackerPed);
if (index >= 0)
{
_underAimInfos[index] = new UnderAimInfo(damageInfo, Time.time, attackerPed);
_underAimInfos[index] = new UnderAimInfo(damageInfo, Time.timeAsDouble, attackerPed);
return;
}
_underAimInfos.Add(new UnderAimInfo(damageInfo, Time.time, attackerPed));
_underAimInfos.Add(new UnderAimInfo(damageInfo, Time.timeAsDouble, attackerPed));
}
private bool ShouldUnderAimInfoBeRemoved(UnderAimInfo underAimInfo)
@ -222,7 +222,7 @@ namespace SanAndreasUnity.Behaviours
if (null == underAimInfo.ped)
return true;
return Time.time - underAimInfo.time > PedManager.Instance.timeIntervalToUpdateUnderAimStatus;
return Time.timeAsDouble - underAimInfo.time > PedManager.Instance.timeIntervalToUpdateUnderAimStatus;
}
}

View file

@ -26,7 +26,7 @@ namespace SanAndreasUnity.Behaviours.Peds.States
public virtual float TimeUntilStateCanBeSwitchedToOtherAimMovementState => PedManager.Instance.timeUntilAimMovementStateCanBeSwitchedToOtherAimMovementState;
public virtual float TimeUntilStateCanBeEnteredFromOtherAimMovementState => PedManager.Instance.timeUntilAimMovementStateCanBeEnteredFromOtherAimMovementState;
protected float m_timeWhenDidUnderAimDetection = 0f;
protected double m_timeWhenDidUnderAimDetection = 0f;
@ -262,9 +262,9 @@ namespace SanAndreasUnity.Behaviours.Peds.States
this.UpdateAimAnim (state);
// do this right after UpdateAimAnim(), because that's the state when weapon conducts attack
if (m_isServer && Time.time - m_timeWhenDidUnderAimDetection >= PedManager.Instance.timeIntervalToUpdateUnderAimStatus)
if (m_isServer && Time.timeAsDouble - m_timeWhenDidUnderAimDetection >= PedManager.Instance.timeIntervalToUpdateUnderAimStatus)
{
m_timeWhenDidUnderAimDetection = Time.time;
m_timeWhenDidUnderAimDetection = Time.timeAsDouble;
UpdateUnderAimDetection(m_ped);
}
}

View file

@ -120,7 +120,7 @@ namespace SanAndreasUnity.Behaviours.Peds.States
for (int i = 0; i < aimStates.Count; i++)
{
if (Time.time - aimStates[i].LastTimeWhenDeactivated < PedManager.Instance.minTimeToReturnToAimState)
if (Time.timeAsDouble - aimStates[i].LastTimeWhenDeactivated < PedManager.Instance.minTimeToReturnToAimState)
return false;
}

View file

@ -22,10 +22,10 @@ namespace SanAndreasUnity.Behaviours.Peds.States
protected bool m_isClientOnly => Net.NetStatus.IsClientOnly;
protected bool m_shouldSendButtonEvents { get { return !m_isServer && m_ped.IsControlledByLocalPlayer; } }
public float LastTimeWhenActivated { get; set; } = 0f;
public float TimeSinceActivated => Time.time - this.LastTimeWhenActivated;
public float LastTimeWhenDeactivated { get; set; } = 0f;
public float TimeSinceDeactivated => Time.time - this.LastTimeWhenDeactivated;
public double LastTimeWhenActivated { get; set; } = 0;
public double TimeSinceActivated => Time.timeAsDouble - this.LastTimeWhenActivated;
public double LastTimeWhenDeactivated { get; set; } = 0;
public double TimeSinceDeactivated => Time.timeAsDouble - this.LastTimeWhenDeactivated;

View file

@ -50,7 +50,7 @@ namespace SanAndreasUnity.Behaviours.Peds.States
public float timeUntilAbleToEnterState = 0.5f;
public float timeUntilAbleToExitState = 0.5f;
private float m_lastTimeWhenChangedAnim = 0f;
private double m_lastTimeWhenChangedAnim = 0f;
private string m_lastAnim = null;
public float timeUntilAbleToChangeAnim = 0.5f;
@ -131,10 +131,10 @@ namespace SanAndreasUnity.Behaviours.Peds.States
string GetAnimBasedOnAimDirSmoothed()
{
if (m_lastAnim != null && Time.time - m_lastTimeWhenChangedAnim < this.timeUntilAbleToChangeAnim)
if (m_lastAnim != null && Time.timeAsDouble - m_lastTimeWhenChangedAnim < this.timeUntilAbleToChangeAnim)
return m_lastAnim;
m_lastTimeWhenChangedAnim = Time.time;
m_lastTimeWhenChangedAnim = Time.timeAsDouble;
m_lastAnim = this.GetAnimBasedOnAimDir();
return m_lastAnim;

View file

@ -19,7 +19,7 @@ namespace SanAndreasUnity.Behaviours.Peds.States
private const int kFemaleSoundIndexMin = 88;
private const int kFemaleSoundIndexMax = 130;
private float _timeWhenEmittedSound = 0f;
private double _timeWhenEmittedSound = 0;
public float randomAverageTimeIntervalToEmitSound = 10f;
private float _timeToEmitSound;
@ -65,14 +65,14 @@ namespace SanAndreasUnity.Behaviours.Peds.States
private void TryEmitSound()
{
if (Time.time - _timeWhenEmittedSound < _timeToEmitSound)
if (Time.timeAsDouble - _timeWhenEmittedSound < _timeToEmitSound)
return;
_timeToEmitSound = Random.Range(
this.randomAverageTimeIntervalToEmitSound * 0.5f,
this.randomAverageTimeIntervalToEmitSound * 1.5f);
_timeWhenEmittedSound = Time.time;
_timeWhenEmittedSound = Time.timeAsDouble;
m_ped.PlaySoundFromPedMouth(this.GetSoundToPlay());
}

View file

@ -7,7 +7,7 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
public class UpdateAttackParams
{
public bool wasInRange = false;
public float timeWhenAddedFireOffset = 0f;
public double timeWhenAddedFireOffset = 0;
public float timeUntilOffsetChanges = 1f;
public Vector3 newFireOffset = Vector3.zero;
@ -136,9 +136,9 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
public void UpdateAttackOnPed(Ped ped, UpdateAttackParams updateAttackParams)
{
if (Time.time - updateAttackParams.timeWhenAddedFireOffset > updateAttackParams.timeUntilOffsetChanges)
if (Time.timeAsDouble - updateAttackParams.timeWhenAddedFireOffset > updateAttackParams.timeUntilOffsetChanges)
{
updateAttackParams.timeWhenAddedFireOffset = Time.time;
updateAttackParams.timeWhenAddedFireOffset = Time.timeAsDouble;
updateAttackParams.newFireOffset = Random.onUnitSphere * 0.2f;
}

View file

@ -14,9 +14,9 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
private ChaseState _chaseState;
private float _timeWhenStartedSurrendering = 0f;
public float TimeSinceStartedSurrendering => Time.time - _timeWhenStartedSurrendering;
public bool IsSurrendering => this.TimeSinceStartedSurrendering < 4f;
private double _timeWhenStartedSurrendering = 0;
public double TimeSinceStartedSurrendering => Time.timeAsDouble - _timeWhenStartedSurrendering;
public bool IsSurrendering => this.TimeSinceStartedSurrendering < 4.0;
public float nodeSearchRadius = 500f;
@ -143,7 +143,7 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
return;
}
_timeWhenStartedSurrendering = Time.time;
_timeWhenStartedSurrendering = Time.timeAsDouble;
}
}

View file

@ -14,7 +14,7 @@ namespace SanAndreasUnity.Behaviours
public bool spawnPlayerWhenConnected = true;
public bool IsSpawningPaused { get; set; } = false;
public float spawnInterval = 4f;
float m_lastSpawnTime = 0f;
double m_lastSpawnTime = 0;
public bool addWeaponsToSpawnedPlayers = true;
@ -77,10 +77,10 @@ namespace SanAndreasUnity.Behaviours
if (this.IsSpawningPaused)
return;
if (Time.time - m_lastSpawnTime >= this.spawnInterval)
if (Time.timeAsDouble - m_lastSpawnTime >= this.spawnInterval)
{
// enough time passed
m_lastSpawnTime = Time.time;
m_lastSpawnTime = Time.timeAsDouble;
F.RunExceptionSafe(() => this.SpawnPlayers());
}

View file

@ -420,7 +420,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
return;
m_lastPreparedPeds[index] = ped;
seat.TimeWhenPedChanged = Time.time;
seat.TimeWhenPedChanged = Time.timeAsDouble;
this.OnPedAssignedToVehicle_Radio(ped, seat);
@ -437,7 +437,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
int index = this.Seats.FindIndex(s => s == seat);
m_lastPreparedPeds[index] = null;
seat.TimeWhenPedChanged = Time.time;
seat.TimeWhenPedChanged = Time.timeAsDouble;
}

View file

@ -23,8 +23,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles
bool m_alreadyExploded = false;
public bool ExplodedThisFrame => m_alreadyExploded;
public float TimeWhenBecameUnderFlame { get; private set; } = float.NegativeInfinity;
public float TimeSinceBecameUnderFlame => Time.time - this.TimeWhenBecameUnderFlame;
public double TimeWhenBecameUnderFlame { get; private set; } = double.NegativeInfinity;
public double TimeSinceBecameUnderFlame => Time.timeAsDouble - this.TimeWhenBecameUnderFlame;
GameObject m_smokeGameObject;
GameObject m_flameGameObject;
@ -85,12 +85,12 @@ namespace SanAndreasUnity.Behaviours.Vehicles
// flame status changed
this.IsUnderFlame = shouldBeUnderFlame;
if (this.IsUnderFlame)
this.TimeWhenBecameUnderFlame = Time.time;
this.TimeWhenBecameUnderFlame = Time.timeAsDouble;
// update vfx
this.UpdateFlameVfx();
}
if (this.IsUnderFlame && Time.time - this.TimeWhenBecameUnderFlame >= 5)
if (this.IsUnderFlame && Time.timeAsDouble - this.TimeWhenBecameUnderFlame >= 5)
{
// enough time passed since vehicle flamed - explode it
if (NetStatus.IsServer)

View file

@ -21,8 +21,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles
bool m_isWaitingForNewRadioSound = false;
public bool IsWaitingForNewRadioSound => m_isWaitingForNewRadioSound;
public float TimeWhenRadioStationChanged { get; private set; } = float.NegativeInfinity;
public float TimeSinceRadioStationChanged => Time.time - this.TimeWhenRadioStationChanged;
public double TimeWhenRadioStationChanged { get; private set; } = double.NegativeInfinity;
public double TimeSinceRadioStationChanged => Time.timeAsDouble - this.TimeWhenRadioStationChanged;
@ -237,7 +237,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
if (m_currentRadioStationIndex == index)
return;
this.TimeWhenRadioStationChanged = Time.time;
this.TimeWhenRadioStationChanged = Time.timeAsDouble;
this.StopPlayingRadio();

View file

@ -243,8 +243,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles
/// <summary> Ped that is occupying this seat. </summary>
public Ped OccupyingPed { get; internal set; }
public float TimeWhenPedChanged { get; internal set; } = float.NegativeInfinity;
public float TimeSincePedChanged => Time.time - this.TimeWhenPedChanged;
public double TimeWhenPedChanged { get; internal set; } = double.NegativeInfinity;
public double TimeSincePedChanged => Time.timeAsDouble - this.TimeWhenPedChanged;
public bool IsTaken { get { return this.OccupyingPed != null; } }

View file

@ -122,8 +122,8 @@ namespace SanAndreasUnity.Behaviours
public AnimationState AimAnimState { get; set; }
public Transform GunFlash { get; private set; }
public float LastTimeWhenFired { get; protected set; } = float.NegativeInfinity;
public float TimeSinceFired => Time.time - this.LastTimeWhenFired;
public double LastTimeWhenFired { get; protected set; } = double.NegativeInfinity;
public double TimeSinceFired => Time.timeAsDouble - this.LastTimeWhenFired;
// weapon sounds are located in SFX -> GENRL -> BANK 137
@ -677,7 +677,7 @@ namespace SanAndreasUnity.Behaviours
public virtual void FireProjectile (Vector3 firePos, Vector3 fireDir, WeaponAttackParams parameters)
{
this.LastTimeWhenFired = Time.time;
this.LastTimeWhenFired = Time.timeAsDouble;
F.RunExceptionSafe(() => this.PlayFireSound());

View file

@ -27,8 +27,8 @@ namespace SanAndreasUnity.Behaviours.World
private float m_timeSinceTimeAdvanced = 0;
public float TimeWhenTimeWasSet { get; private set; } = 0;
public float TimeSinceTimeWasSet => Time.time - this.TimeWhenTimeWasSet;
public double TimeWhenTimeWasSet { get; private set; } = 0;
public double TimeSinceTimeWasSet => Time.timeAsDouble - this.TimeWhenTimeWasSet;
public float timeScale = 1;
@ -126,7 +126,7 @@ namespace SanAndreasUnity.Behaviours.World
this.CurrentTimeMinutes = minutes;
m_timeSinceTimeAdvanced = 0;
this.TimeWhenTimeWasSet = Time.time;
this.TimeWhenTimeWasSet = Time.timeAsDouble;
float curveTime = this.CurrentCurveTimeStepped;

View file

@ -36,7 +36,7 @@ namespace SanAndreasUnity.Behaviours.World
public WorldSystem.FocusPoint focusPoint;
public Transform transform;
public float timeToKeepRevealingAfterRemoved;
public float timeWhenRemoved;
public double timeWhenRemoved;
public bool hasRevealRadius;
}
@ -82,7 +82,7 @@ namespace SanAndreasUnity.Behaviours.World
if (focusPoint.timeToKeepRevealingAfterRemoved > 0)
{
focusPoint.timeWhenRemoved = Time.time;
focusPoint.timeWhenRemoved = Time.timeAsDouble;
_focusPointsToRemoveAfterTimeout.Add(focusPoint);
_focusPoints.RemoveAt(index);
return;
@ -94,7 +94,7 @@ namespace SanAndreasUnity.Behaviours.World
public void Update()
{
float timeNow = Time.time;
double timeNow = Time.timeAsDouble;
UnityEngine.Profiling.Profiler.BeginSample("Update focus points");
this._focusPoints.RemoveAll(f =>

View file

@ -25,7 +25,7 @@ namespace SanAndreasUnity.Commands
private struct PlayerData
{
public float timeWhenLastExecutedCommand;
public double timeWhenLastExecutedCommand;
}
readonly Dictionary<Player, PlayerData> m_perPlayerData = new Dictionary<Player, PlayerData>();
@ -224,10 +224,10 @@ namespace SanAndreasUnity.Commands
{
m_perPlayerData.TryGetValue(context.player, out PlayerData playerData);
if (commandInfo.limitInterval > 0 && Time.time - playerData.timeWhenLastExecutedCommand < commandInfo.limitInterval)
if (commandInfo.limitInterval > 0 && Time.timeAsDouble - playerData.timeWhenLastExecutedCommand < commandInfo.limitInterval)
return ProcessCommandResult.LimitInterval(commandInfo.limitInterval);
playerData.timeWhenLastExecutedCommand = Time.time;
playerData.timeWhenLastExecutedCommand = Time.timeAsDouble;
m_perPlayerData[context.player] = playerData;
}

View file

@ -169,13 +169,13 @@ namespace SanAndreasUnity.UI
if (LanTabIndex == m_currentTabIndex)
{
GUI.enabled = ! m_netDiscoveryHUD.IsRefreshing;
buttonText = m_netDiscoveryHUD.IsRefreshing ? ( "Refreshing." + new string('.', (int) ((Time.time * 2) % 3)) ) : "Refresh LAN";
buttonText = m_netDiscoveryHUD.IsRefreshing ? ( "Refreshing." + new string('.', (int) ((Time.timeAsDouble * 2) % 3)) ) : "Refresh LAN";
buttonAction = () => m_netDiscoveryHUD.Refresh();
}
else if (InternetTabIndex == m_currentTabIndex)
{
GUI.enabled = !_isRefreshingMasterServerList;
buttonText = _isRefreshingMasterServerList ? ( "Refreshing." + new string('.', (int) ((Time.time * 2) % 3)) ) : "Refresh servers";
buttonText = _isRefreshingMasterServerList ? ( "Refreshing." + new string('.', (int) ((Time.timeAsDouble * 2) % 3)) ) : "Refresh servers";
buttonAction = async () => await RefreshMasterServersButtonPressed();
}
}

View file

@ -11,10 +11,10 @@ public class GLDebug : MonoBehaviour
public Vector3 start;
public Vector3 end;
public Color color;
public float startTime;
public double startTime;
public float duration;
public Line (Vector3 start, Vector3 end, Color color, float startTime, float duration)
public Line (Vector3 start, Vector3 end, Color color, double startTime, float duration)
{
this.start = start;
this.end = end;
@ -31,7 +31,7 @@ public class GLDebug : MonoBehaviour
GL.Vertex (start);
GL.Vertex (end);
}
return Time.time - startTime >= duration;
return Time.timeAsDouble - startTime >= duration;
}
}
@ -149,9 +149,9 @@ public class GLDebug : MonoBehaviour
if (start == end)
return;
if (depthTest)
instance.linesZOn.Add (new Line (start, end, color, Time.time, duration));
instance.linesZOn.Add (new Line (start, end, color, Time.timeAsDouble, duration));
else
instance.linesZOff.Add (new Line (start, end, color, Time.time, duration));
instance.linesZOff.Add (new Line (start, end, color, Time.timeAsDouble, duration));
}
/// <summary>

View file

@ -15,8 +15,8 @@ namespace SanAndreasUnity.Utilities
object ParameterForEnteringState { set; }
float LastTimeWhenActivated { get; set; }
float LastTimeWhenDeactivated { get; set; }
double LastTimeWhenActivated { get; set; }
double LastTimeWhenDeactivated { get; set; }
}
}

View file

@ -8,7 +8,7 @@ namespace SanAndreasUnity.Utilities
IState m_currentState;
public IState CurrentState { get { return m_currentState; } }
bool m_isSwitchingState = false;
public float TimeWhenSwitchedState { get; private set; }
public double TimeWhenSwitchedState { get; private set; }
public long FrameWhenSwitchedState { get; private set; }
@ -33,20 +33,20 @@ namespace SanAndreasUnity.Utilities
// no longer be possible to switch states, because 'm_isSwitchingState' is true
F.RunExceptionSafe(() =>
{
oldState.LastTimeWhenDeactivated = Time.time;
oldState.LastTimeWhenDeactivated = Time.timeAsDouble;
oldState.OnBecameInactive();
});
}
m_isSwitchingState = false;
this.TimeWhenSwitchedState = Time.time;
this.TimeWhenSwitchedState = Time.timeAsDouble;
this.FrameWhenSwitchedState = Time.frameCount;
if (m_currentState != null)
{
m_currentState.ParameterForEnteringState = parameterForEnteringState;
m_currentState.LastTimeWhenActivated = Time.time;
m_currentState.LastTimeWhenActivated = Time.timeAsDouble;
m_currentState.OnBecameActive();
}
}