]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Expand UseDelay to support multiple delays per entity; fix bible healing and bag...
authorTayrtahn <tayrtahn@gmail.com>
Fri, 26 Apr 2024 02:25:52 +0000 (22:25 -0400)
committerGitHub <noreply@github.com>
Fri, 26 Apr 2024 02:25:52 +0000 (22:25 -0400)
* Upgraded UseDelay to support multiple delays per entity

* Implement secondary delay for bibles.
Also some improvements to make it work nicely.

* Documentation is good

* Reserve the previous change; now Storage uses the special ID and Bible uses the default.

* .0

* Added VV support to UseDelayInfo

* Serialize better

* No register, just setlength

Content.Client/UserInterface/Systems/Hands/HandsUIController.cs
Content.Server/Fluids/EntitySystems/SpraySystem.cs
Content.Server/Storage/EntitySystems/StorageSystem.cs
Content.Shared/Storage/EntitySystems/SharedStorageSystem.cs
Content.Shared/Storage/StorageComponent.cs
Content.Shared/Timing/UseDelayComponent.cs
Content.Shared/Timing/UseDelaySystem.cs

index 99d7bc77b81a00f111c9c6714652d3f655261d52..9ee429ba7e99aa58e184be83ef003cc3f2d8b398 100644 (file)
@@ -22,6 +22,7 @@ public sealed class HandsUIController : UIController, IOnStateEntered<GameplaySt
     [Dependency] private readonly IPlayerManager _player = default!;
 
     [UISystemDependency] private readonly HandsSystem _handsSystem = default!;
+    [UISystemDependency] private readonly UseDelaySystem _useDelay = default!;
 
     private readonly List<HandsContainer> _handsContainers = new();
     private readonly Dictionary<string, int> _handContainerIndices = new();
@@ -450,15 +451,15 @@ public sealed class HandsUIController : UIController, IOnStateEntered<GameplaySt
             foreach (var hand in container.GetButtons())
             {
 
-                if (!_entities.TryGetComponent(hand.Entity, out UseDelayComponent? useDelay) ||
-                    useDelay is not { DelayStartTime: var start, DelayEndTime: var end })
+                if (!_entities.TryGetComponent(hand.Entity, out UseDelayComponent? useDelay))
                 {
                     hand.CooldownDisplay.Visible = false;
                     continue;
                 }
+                var delay = _useDelay.GetLastEndingDelay((hand.Entity.Value, useDelay));
 
                 hand.CooldownDisplay.Visible = true;
-                hand.CooldownDisplay.FromTime(start, end);
+                hand.CooldownDisplay.FromTime(delay.StartTime, delay.EndTime);
             }
         }
     }
index f7621aec62611e7b55c015bd286317247e676ac1..40f19aff2b0cf125ac7ed7be9e683c95ed272044 100644 (file)
@@ -144,7 +144,7 @@ public sealed class SpraySystem : EntitySystem
 
         _audio.PlayPvs(entity.Comp.SpraySound, entity, entity.Comp.SpraySound.Params.WithVariation(0.125f));
 
-        _useDelay.SetDelay((entity, useDelay), TimeSpan.FromSeconds(cooldownTime));
+        _useDelay.SetLength((entity, useDelay), TimeSpan.FromSeconds(cooldownTime));
         _useDelay.TryResetDelay((entity, useDelay));
     }
 }
index 5d41e0a52140f2ef05c4fc2145aa69a4bb0e5359..390310390e01d298b26c00b1c0238ace562eacad 100644 (file)
@@ -24,7 +24,8 @@ public sealed partial class StorageSystem : SharedStorageSystem
     [Dependency] private readonly IPrototypeManager _prototype = default!;
     [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
     [Dependency] private readonly SharedAudioSystem _audio = default!;
-    [Dependency] private readonly UseDelaySystem _useDelay = default!;
+
+    private const string OpenUiUseDelayID = "storage";
 
     public override void Initialize()
     {
@@ -39,6 +40,14 @@ public sealed partial class StorageSystem : SharedStorageSystem
         SubscribeLocalEvent<StorageFillComponent, MapInitEvent>(OnStorageFillMapInit);
     }
 
+    protected override void OnMapInit(Entity<StorageComponent> entity, ref MapInitEvent args)
+    {
+        base.OnMapInit(entity, ref args);
+
+        if (TryComp<UseDelayComponent>(entity, out var useDelay))
+            UseDelay.SetLength((entity, useDelay), entity.Comp.OpenUiCooldown, OpenUiUseDelayID);
+    }
+
     private void AddUiVerb(EntityUid uid, StorageComponent component, GetVerbsEvent<ActivationVerb> args)
     {
         var silent = false;
@@ -120,13 +129,13 @@ public sealed partial class StorageSystem : SharedStorageSystem
             return;
 
         // prevent spamming bag open / honkerton honk sound
-        silent |= TryComp<UseDelayComponent>(uid, out var useDelay) && _useDelay.IsDelayed((uid, useDelay));
+        silent |= TryComp<UseDelayComponent>(uid, out var useDelay) && UseDelay.IsDelayed((uid, useDelay), OpenUiUseDelayID);
         if (!silent)
         {
             if (!storageComp.IsUiOpen)
                 _audio.PlayPvs(storageComp.StorageOpenSound, uid);
             if (useDelay != null)
-                _useDelay.TryResetDelay((uid, useDelay));
+                UseDelay.TryResetDelay((uid, useDelay), id: OpenUiUseDelayID);
         }
 
         Log.Debug($"Storage (UID {uid}) \"used\" by player session (UID {player.PlayerSession.AttachedEntity}).");
index 2021dfe2deec4b7562f0ebb6a0ef1b2b0bd399d2..3bec0e1d0166b74b2b9c6709ff72a3b6dfcfac43 100644 (file)
@@ -72,6 +72,8 @@ public abstract class SharedStorageSystem : EntitySystem
     private readonly List<ItemSizePrototype> _sortedSizes = new();
     private FrozenDictionary<string, ItemSizePrototype> _nextSmallest = FrozenDictionary<string, ItemSizePrototype>.Empty;
 
+    private const string QuickInsertUseDelayID = "quickInsert";
+
     protected readonly List<string> CantFillReasons = [];
 
     /// <inheritdoc />
@@ -84,6 +86,7 @@ public abstract class SharedStorageSystem : EntitySystem
         _xformQuery = GetEntityQuery<TransformComponent>();
         _prototype.PrototypesReloaded += OnPrototypesReloaded;
 
+        SubscribeLocalEvent<StorageComponent, MapInitEvent>(OnMapInit);
         SubscribeLocalEvent<StorageComponent, ComponentGetState>(OnStorageGetState);
         SubscribeLocalEvent<StorageComponent, ComponentHandleState>(OnStorageHandleState);
         SubscribeLocalEvent<StorageComponent, ComponentInit>(OnComponentInit, before: new[] { typeof(SharedContainerSystem) });
@@ -118,6 +121,12 @@ public abstract class SharedStorageSystem : EntitySystem
         UpdatePrototypeCache();
     }
 
+    protected virtual void OnMapInit(Entity<StorageComponent> entity, ref MapInitEvent args)
+    {
+        if (TryComp<UseDelayComponent>(entity, out var useDelayComp))
+            UseDelay.SetLength((entity, useDelayComp), entity.Comp.QuickInsertCooldown, QuickInsertUseDelayID);
+    }
+
     private void OnStorageGetState(EntityUid uid, StorageComponent component, ref ComponentGetState args)
     {
         var storedItems = new Dictionary<NetEntity, ItemStorageLocation>();
@@ -275,7 +284,7 @@ public abstract class SharedStorageSystem : EntitySystem
     /// <returns></returns>
     private void AfterInteract(EntityUid uid, StorageComponent storageComp, AfterInteractEvent args)
     {
-        if (args.Handled || !args.CanReach || !UseDelay.TryResetDelay(uid, checkDelayed: true))
+        if (args.Handled || !args.CanReach || !UseDelay.TryResetDelay(uid, checkDelayed: true, id: QuickInsertUseDelayID))
             return;
 
         // Pick up all entities in a radius around the clicked location.
index 16987f1de022be4a1aa752f2a88f3bc46e12851e..43a93e4b0f8d5c9dd8f92c2438a9b4e0e3c6e32c 100644 (file)
@@ -57,6 +57,19 @@ namespace Content.Shared.Storage
         [DataField]
         public bool QuickInsert; // Can insert storables by clicking them with the storage entity
 
+        /// <summary>
+        /// Minimum delay between quick/area insert actions.
+        /// </summary>
+        /// <remarks>Used to prevent autoclickers spamming server with individual pickup actions.</remarks>
+        public TimeSpan QuickInsertCooldown = TimeSpan.FromSeconds(0.5);
+
+        /// <summary>
+        /// Minimum delay between UI open actions.
+        /// <remarks>Used to spamming opening sounds.</remarks>
+        /// </summary>
+        [DataField]
+        public TimeSpan OpenUiCooldown = TimeSpan.Zero;
+
         [DataField]
         public bool ClickInsert = true; // Can insert stuff by clicking the storage entity with it
 
index 1560d4dd0b92990a583c00046f3309e3183e5e39..c7b21bd1feb6219b7a8a1b494eee61b87a7ebf3e 100644 (file)
@@ -1,38 +1,47 @@
 using Robust.Shared.GameStates;
-using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
+using Robust.Shared.Serialization;
 
 namespace Content.Shared.Timing;
 
 /// <summary>
-/// Timer that creates a cooldown each time an object is activated/used
+/// Timer that creates a cooldown each time an object is activated/used.
+/// Can support additional, separate cooldown timers on the object by passing a unique ID with the system methods.
 /// </summary>
-/// <remarks>
-/// Currently it only supports a single delay per entity, this means that for things that have two delay interactions they will share one timer, so this can cause issues. For example, the bible has a delay when opening the storage UI and when applying it's interaction effect, and they share the same delay.
-/// </remarks>
 [RegisterComponent]
-[NetworkedComponent, AutoGenerateComponentState, AutoGenerateComponentPause]
+[NetworkedComponent, AutoGenerateComponentState]
 [Access(typeof(UseDelaySystem))]
 public sealed partial class UseDelayComponent : Component
 {
-    /// <summary>
-    /// When the delay starts.
-    /// </summary>
-    [ViewVariables(VVAccess.ReadWrite), DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), AutoNetworkedField]
-    [AutoPausedField]
-    public TimeSpan DelayStartTime;
-
-    /// <summary>
-    /// When the delay ends.
-    /// </summary>
-    [ViewVariables(VVAccess.ReadWrite), DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), AutoNetworkedField]
-    [AutoPausedField]
-    public TimeSpan DelayEndTime;
+    [DataField, AutoNetworkedField]
+    public Dictionary<string, UseDelayInfo> Delays = [];
 
     /// <summary>
-    /// Default delay time
+    /// Default delay time.
     /// </summary>
+    /// <remarks>
+    /// This is only used at MapInit and should not be expected
+    /// to reflect the length of the default delay after that.
+    /// Use <see cref="UseDelaySystem.TryGetDelayInfo"/> instead.
+    /// </remarks>
     [DataField]
-    [ViewVariables(VVAccess.ReadWrite)]
-    [AutoNetworkedField]
     public TimeSpan Delay = TimeSpan.FromSeconds(1);
 }
+
+[Serializable, NetSerializable]
+[DataDefinition]
+public sealed partial class UseDelayInfo
+{
+    [DataField]
+    public TimeSpan Length { get; set; }
+    [DataField]
+    public TimeSpan StartTime { get; set; }
+    [DataField]
+    public TimeSpan EndTime { get; set; }
+
+    public UseDelayInfo(TimeSpan length, TimeSpan startTime = default, TimeSpan endTime = default)
+    {
+        Length = length;
+        StartTime = startTime;
+        EndTime = endTime;
+    }
+}
index 388f31079cdc7b6dd928ef3e970be38c9ef907c5..3d2498203c62be9bad2ef041a084763aee3c0c25 100644 (file)
@@ -1,3 +1,4 @@
+using System.Diagnostics.CodeAnalysis;
 using Robust.Shared.Timing;
 
 namespace Content.Shared.Timing;
@@ -7,53 +8,142 @@ public sealed class UseDelaySystem : EntitySystem
     [Dependency] private readonly IGameTiming _gameTiming = default!;
     [Dependency] private readonly MetaDataSystem _metadata = default!;
 
-    public void SetDelay(Entity<UseDelayComponent> ent, TimeSpan delay)
+    private const string DefaultId = "default";
+
+    public override void Initialize()
     {
-        if (ent.Comp.Delay == delay)
-            return;
+        base.Initialize();
+
+        SubscribeLocalEvent<UseDelayComponent, MapInitEvent>(OnMapInit);
+        SubscribeLocalEvent<UseDelayComponent, EntityUnpausedEvent>(OnUnpaused);
+    }
+
+    private void OnMapInit(Entity<UseDelayComponent> ent, ref MapInitEvent args)
+    {
+        // Set default delay length from the prototype
+        // This makes it easier for simple use cases that only need a single delay
+        SetLength(ent, ent.Comp.Delay, DefaultId);
+    }
+
+    private void OnUnpaused(Entity<UseDelayComponent> ent, ref EntityUnpausedEvent args)
+    {
+        // We have to do this manually, since it's not just a single field.
+        foreach (var entry in ent.Comp.Delays.Values)
+        {
+            entry.EndTime += args.PausedTime;
+        }
+    }
+
+    /// <summary>
+    /// Sets the length of the delay with the specified ID.
+    /// </summary>
+    public bool SetLength(Entity<UseDelayComponent> ent, TimeSpan length, string id = DefaultId)
+    {
+        if (ent.Comp.Delays.TryGetValue(id, out var entry))
+        {
+            if (entry.Length == length)
+                return true;
+
+            entry.Length = length;
+        }
+        else
+        {
+            ent.Comp.Delays.Add(id, new UseDelayInfo(length));
+        }
 
-        ent.Comp.Delay = delay;
         Dirty(ent);
+        return true;
     }
 
     /// <summary>
-    /// Returns true if the entity has a currently active UseDelay.
+    /// Returns true if the entity has a currently active UseDelay with the specified ID.
     /// </summary>
-    public bool IsDelayed(Entity<UseDelayComponent> ent)
+    public bool IsDelayed(Entity<UseDelayComponent> ent, string id = DefaultId)
     {
-        return ent.Comp.DelayEndTime >= _gameTiming.CurTime;
+        if (!ent.Comp.Delays.TryGetValue(id, out var entry))
+            return false;
+
+        return entry.EndTime >= _gameTiming.CurTime;
     }
 
     /// <summary>
-    /// Cancels the current delay.
+    /// Cancels the delay with the specified ID.
     /// </summary>
-    public void CancelDelay(Entity<UseDelayComponent> ent)
+    public void CancelDelay(Entity<UseDelayComponent> ent, string id = DefaultId)
     {
-        ent.Comp.DelayEndTime = _gameTiming.CurTime;
+        if (!ent.Comp.Delays.TryGetValue(id, out var entry))
+            return;
+
+        entry.EndTime = _gameTiming.CurTime;
         Dirty(ent);
     }
 
     /// <summary>
-    /// Resets the UseDelay entirely for this entity if possible.
+    /// Tries to get info about the delay with the specified ID. See <see cref="UseDelayInfo"/>.
     /// </summary>
-    /// <param name="checkDelayed">Check if the entity has an ongoing delay, return false if it does, return true if it does not.</param>
-    public bool TryResetDelay(Entity<UseDelayComponent> ent, bool checkDelayed = false)
+    /// <param name="ent"></param>
+    /// <param name="info"></param>
+    /// <param name="id"></param>
+    /// <returns></returns>
+    public bool TryGetDelayInfo(Entity<UseDelayComponent> ent, [NotNullWhen(true)] out UseDelayInfo? info, string id = DefaultId)
     {
-        if (checkDelayed && IsDelayed(ent))
+        return ent.Comp.Delays.TryGetValue(id, out info);
+    }
+
+    /// <summary>
+    /// Returns info for the delay that will end farthest in the future.
+    /// </summary>
+    public UseDelayInfo GetLastEndingDelay(Entity<UseDelayComponent> ent)
+    {
+        var last = ent.Comp.Delays[DefaultId];
+        foreach (var entry in ent.Comp.Delays)
+        {
+            if (entry.Value.EndTime > last.EndTime)
+                last = entry.Value;
+        }
+        return last;
+    }
+
+    /// <summary>
+    /// Resets the delay with the specified ID for this entity if possible.
+    /// </summary>
+    /// <param name="checkDelayed">Check if the entity has an ongoing delay with the specified ID.
+    /// If it does, return false and don't reset it.
+    /// Otherwise reset it and return true.</param>
+    public bool TryResetDelay(Entity<UseDelayComponent> ent, bool checkDelayed = false, string id = DefaultId)
+    {
+        if (checkDelayed && IsDelayed(ent, id))
+            return false;
+
+        if (!ent.Comp.Delays.TryGetValue(id, out var entry))
             return false;
 
         var curTime = _gameTiming.CurTime;
-        ent.Comp.DelayStartTime = curTime;
-        ent.Comp.DelayEndTime = curTime - _metadata.GetPauseTime(ent) + ent.Comp.Delay;
+        entry.StartTime = curTime;
+        entry.EndTime = curTime - _metadata.GetPauseTime(ent) + entry.Length;
         Dirty(ent);
         return true;
     }
 
-    public bool TryResetDelay(EntityUid uid, bool checkDelayed = false, UseDelayComponent? component = null)
+    public bool TryResetDelay(EntityUid uid, bool checkDelayed = false, UseDelayComponent? component = null, string id = DefaultId)
     {
         if (!Resolve(uid, ref component, false))
             return false;
 
-        return TryResetDelay((uid, component), checkDelayed);
+        return TryResetDelay((uid, component), checkDelayed, id);
+    }
+
+    /// <summary>
+    /// Resets all delays on the entity.
+    /// </summary>
+    public void ResetAllDelays(Entity<UseDelayComponent> ent)
+    {
+        var curTime = _gameTiming.CurTime;
+        foreach (var entry in ent.Comp.Delays.Values)
+        {
+            entry.StartTime = curTime;
+            entry.EndTime = curTime - _metadata.GetPauseTime(ent) + entry.Length;
+        }
+        Dirty(ent);
     }
 }