0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 09:50:43 -05:00
DanW bf07137cf7 Improve Robustness of RaidChangesUtil and RaidTimeUtil (!45)
While continuing mod development with SPT 3.7.4 (namely for Questing Bots and SAIN), I found that the API I added to SPT 3.7.4 is unintuitive and clunky to use after the raid is done loading but before the countdown timer expires. With the current SPT code, calling `GetRemainingRaidSeconds()` or `GetRaidTimeRemainingFraction()` will result in an exception during this time unless you check `HasRaidStarted()` first.

These changes will prevent those methods from throwing an exception unless you try calling them before a raid has finished loading (which should rarely happen). This allows mods to more easily do things using `GetRemainingRaidSeconds()` or `GetRaidTimeRemainingFraction()` before the countdown timer expires (i.e. cache bots).

These changes only improve the robustness of the existing code and will not break any mods that use the existing API.

Co-authored-by: dwesterwick <dwesterwick@yahoo.com>
Co-authored-by: chomp <chomp@noreply.dev.sp-tarkov.com>
Reviewed-on: SPT-AKI/Modules#45
Co-authored-by: DanW <danw@noreply.dev.sp-tarkov.com>
Co-committed-by: DanW <danw@noreply.dev.sp-tarkov.com>
2023-12-27 11:25:15 +00:00

83 lines
3.4 KiB
C#

using Comfort.Common;
using EFT;
using System;
namespace Aki.SinglePlayer.Utils.InRaid
{
/// <summary>
/// Allow modders to access information about the current raid time, especially if its reduced for Scav raids
/// </summary>
public static class RaidTimeUtil
{
/// <summary>
/// Determines if a raid has loaded by checking if a GameTimer instance has been created for one
/// </summary>
public static bool HasRaidLoaded()
{
return Singleton<AbstractGame>.Instance?.GameTimer != null;
}
/// <summary>
/// Determines if a raid is in-progress by checking if the GameTimer has started
/// </summary>
public static bool HasRaidStarted()
{
return Singleton<AbstractGame>.Instance.GameTimer.Started();
}
/// <summary>
/// <para>Calculates the seconds remaining in the current raid.</para>
/// <para>Please ensure <see cref="HasRaidLoaded"/> is <c>true</c>, or this will throw an exception.</para>
/// </summary>
/// <returns>Seconds remaining in the raid</returns>
/// <exception cref="InvalidOperationException">Thrown if there is no raid in progress</exception>
public static float GetRemainingRaidSeconds()
{
if (!HasRaidLoaded())
{
throw new InvalidOperationException("The raid-time remaining can only be calculated when a raid is in-progress");
}
if (!HasRaidStarted())
{
return RaidChangesUtil.NewEscapeTimeSeconds;
}
float remainingTimeSeconds = Singleton<AbstractGame>.Instance.GameTimer.EscapeTimeSeconds();
// Until the raid starts, remainingTimeSeconds is TimeSpan.MaxValue, so it needs to be reduced to the actual starting raid time
return Math.Min(remainingTimeSeconds, RaidChangesUtil.NewEscapeTimeSeconds);
}
/// <summary>
/// <para>Calculates the fraction of raid-time remaining relative to the original escape time for the map.
/// 1.0 = the raid just started, and 0.0 = the raid is over (and you're MIA).</para>
/// <para>Please ensure <see cref="HasRaidLoaded"/> is <c>true</c>, or this will throw an exception.</para>
/// </summary>
/// <returns>The fraction of raid-time remaining (0.0 - 1.0) relative to the original escape time for the map</returns>
/// <exception cref="InvalidOperationException">Thrown if there is no raid in progress</exception>
public static float GetRaidTimeRemainingFraction()
{
return GetRemainingRaidSeconds() / RaidChangesUtil.OriginalEscapeTimeSeconds;
}
/// <summary>
/// Calculates the seconds since the player spawned into the raid
/// </summary>
/// <returns>Seconds since the player spawned into the raid</returns>
public static float GetSecondsSinceSpawning()
{
return Singleton<AbstractGame>.Instance.GameTimer.PastTimeSeconds();
}
/// <summary>
/// Calculates the elapsed seconds in the raid from the original escape time for the map.
/// </summary>
/// <returns>Elapsed seconds in the raid</returns>
public static float GetElapsedRaidSeconds()
{
return RaidChangesUtil.RaidTimeReductionSeconds + GetSecondsSinceSpawning();
}
}
}