]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Move most rotting code to shared (#28050)
authorDrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com>
Wed, 15 May 2024 21:45:50 +0000 (14:45 -0700)
committerGitHub <noreply@github.com>
Wed, 15 May 2024 21:45:50 +0000 (17:45 -0400)
* Move most rotting code to shared

* Remove unused dependency

Content.Server/Atmos/Rotting/RottingSystem.cs
Content.Shared/Atmos/Rotting/SharedRottingSystem.cs

index dd6387ff4463df1568a3be961f98efa42b2ed7a0..5070b3f197f0fb577609792a281f9015f1ee078e 100644 (file)
@@ -1,15 +1,9 @@
-using Content.Shared.Damage;
-using Content.Shared.Atmos;
 using Content.Server.Atmos.EntitySystems;
 using Content.Server.Body.Components;
 using Content.Server.Temperature.Components;
+using Content.Shared.Atmos;
 using Content.Shared.Atmos.Rotting;
-using Content.Shared.Examine;
-using Content.Shared.IdentityManagement;
-using Content.Shared.Mobs;
-using Content.Shared.Mobs.Components;
-using Content.Shared.Mobs.Systems;
-using Content.Shared.Rejuvenate;
+using Content.Shared.Damage;
 using Robust.Server.Containers;
 using Robust.Shared.Physics.Components;
 using Robust.Shared.Timing;
@@ -22,83 +16,16 @@ public sealed class RottingSystem : SharedRottingSystem
     [Dependency] private readonly AtmosphereSystem _atmosphere = default!;
     [Dependency] private readonly ContainerSystem _container = default!;
     [Dependency] private readonly DamageableSystem _damageable = default!;
-    [Dependency] private readonly MobStateSystem _mobState = default!;
 
     public override void Initialize()
     {
         base.Initialize();
 
-        SubscribeLocalEvent<PerishableComponent, MapInitEvent>(OnPerishableMapInit);
-        SubscribeLocalEvent<PerishableComponent, MobStateChangedEvent>(OnMobStateChanged);
-        SubscribeLocalEvent<PerishableComponent, ExaminedEvent>(OnPerishableExamined);
-
-        SubscribeLocalEvent<RottingComponent, ComponentShutdown>(OnShutdown);
-        SubscribeLocalEvent<RottingComponent, MobStateChangedEvent>(OnRottingMobStateChanged);
         SubscribeLocalEvent<RottingComponent, BeingGibbedEvent>(OnGibbed);
-        SubscribeLocalEvent<RottingComponent, RejuvenateEvent>(OnRejuvenate);
 
         SubscribeLocalEvent<TemperatureComponent, IsRottingEvent>(OnTempIsRotting);
     }
 
-    private void OnPerishableMapInit(EntityUid uid, PerishableComponent component, MapInitEvent args)
-    {
-        component.RotNextUpdate = _timing.CurTime + component.PerishUpdateRate;
-    }
-
-    private void OnMobStateChanged(EntityUid uid, PerishableComponent component, MobStateChangedEvent args)
-    {
-        if (args.NewMobState != MobState.Dead && args.OldMobState != MobState.Dead)
-            return;
-
-        if (HasComp<RottingComponent>(uid))
-            return;
-
-        component.RotAccumulator = TimeSpan.Zero;
-        component.RotNextUpdate = _timing.CurTime + component.PerishUpdateRate;
-    }
-
-    private void OnShutdown(EntityUid uid, RottingComponent component, ComponentShutdown args)
-    {
-        if (TryComp<PerishableComponent>(uid, out var perishable))
-        {
-            perishable.RotNextUpdate = TimeSpan.Zero;
-        }
-    }
-
-    private void OnRottingMobStateChanged(EntityUid uid, RottingComponent component, MobStateChangedEvent args)
-    {
-        if (args.NewMobState == MobState.Dead)
-            return;
-        RemCompDeferred(uid, component);
-    }
-
-    public bool IsRotProgressing(EntityUid uid, PerishableComponent? perishable)
-    {
-        // things don't perish by default.
-        if (!Resolve(uid, ref perishable, false))
-            return false;
-
-        // only dead things or inanimate objects can rot
-        if (TryComp<MobStateComponent>(uid, out var mobState) && !_mobState.IsDead(uid, mobState))
-            return false;
-
-        if (_container.TryGetOuterContainer(uid, Transform(uid), out var container) &&
-            HasComp<AntiRottingContainerComponent>(container.Owner))
-        {
-            return false;
-        }
-
-        var ev = new IsRottingEvent();
-        RaiseLocalEvent(uid, ref ev);
-
-        return !ev.Handled;
-    }
-
-    public bool IsRotten(EntityUid uid, RottingComponent? rotting = null)
-    {
-        return Resolve(uid, ref rotting, false);
-    }
-
     private void OnGibbed(EntityUid uid, RottingComponent component, BeingGibbedEvent args)
     {
         if (!TryComp<PhysicsComponent>(uid, out var physics))
@@ -112,36 +39,6 @@ public sealed class RottingSystem : SharedRottingSystem
         tileMix?.AdjustMoles(Gas.Ammonia, molsToDump);
     }
 
-    private void OnPerishableExamined(Entity<PerishableComponent> perishable, ref ExaminedEvent args)
-    {
-        int stage = PerishStage(perishable, MaxStages);
-        if (stage < 1 || stage > MaxStages)
-        {
-            // We dont push an examined string if it hasen't started "perishing" or it's already rotting
-            return;
-        }
-
-        var isMob = HasComp<MobStateComponent>(perishable);
-        var description = "perishable-" + stage + (!isMob ? "-nonmob" : string.Empty);
-        args.PushMarkup(Loc.GetString(description, ("target", Identity.Entity(perishable, EntityManager))));
-    }
-
-    /// <summary>
-    /// Return an integer from 0 to maxStage representing how close to rotting an entity is. Used to
-    /// generate examine messages for items that are starting to rot.
-    /// </summary>
-    public int PerishStage(Entity<PerishableComponent> perishable, int maxStages)
-    {
-        if (perishable.Comp.RotAfter.TotalSeconds == 0 || perishable.Comp.RotAccumulator.TotalSeconds == 0)
-            return 0;
-        return (int)(1 + maxStages * perishable.Comp.RotAccumulator.TotalSeconds / perishable.Comp.RotAfter.TotalSeconds);
-    }
-
-    private void OnRejuvenate(EntityUid uid, RottingComponent component, RejuvenateEvent args)
-    {
-        RemCompDeferred<RottingComponent>(uid);
-    }
-
     private void OnTempIsRotting(EntityUid uid, TemperatureComponent component, ref IsRottingEvent args)
     {
         if (args.Handled)
@@ -149,29 +46,6 @@ public sealed class RottingSystem : SharedRottingSystem
         args.Handled = component.CurrentTemperature < Atmospherics.T0C + 0.85f;
     }
 
-
-    public void ReduceAccumulator(EntityUid uid, TimeSpan time)
-    {
-        if (!TryComp<PerishableComponent>(uid, out var perishable))
-            return;
-
-        if (!TryComp<RottingComponent>(uid, out var rotting))
-        {
-            perishable.RotAccumulator -= time;
-            return;
-        }
-        var total = (rotting.TotalRotTime + perishable.RotAccumulator) - time;
-
-        if (total < perishable.RotAfter)
-        {
-            RemCompDeferred(uid, rotting);
-            perishable.RotAccumulator = total;
-        }
-
-        else
-            rotting.TotalRotTime = total - perishable.RotAfter;
-    }
-    
     /// <summary>
     /// Is anything speeding up the decay?
     /// e.g. buried in a grave
index 5e1758203a899d16af2035f09335692fffcf822e..840818dee5911f4e58ce16a1630cc3f3f230d673 100644 (file)
@@ -1,29 +1,85 @@
 using Content.Shared.Examine;
 using Content.Shared.IdentityManagement;
+using Content.Shared.Mobs;
 using Content.Shared.Mobs.Components;
+using Content.Shared.Mobs.Systems;
+using Content.Shared.Rejuvenate;
+using Robust.Shared.Containers;
+using Robust.Shared.Timing;
 
 namespace Content.Shared.Atmos.Rotting;
 
 public abstract class SharedRottingSystem : EntitySystem
 {
+    [Dependency] private readonly IGameTiming _timing = default!;
+    [Dependency] private readonly SharedContainerSystem _container = default!;
+    [Dependency] private readonly MobStateSystem _mobState = default!;
+
     public const int MaxStages = 3;
 
     public override void Initialize()
     {
         base.Initialize();
 
+        SubscribeLocalEvent<PerishableComponent, MapInitEvent>(OnPerishableMapInit);
+        SubscribeLocalEvent<PerishableComponent, MobStateChangedEvent>(OnMobStateChanged);
+        SubscribeLocalEvent<PerishableComponent, ExaminedEvent>(OnPerishableExamined);
+
+        SubscribeLocalEvent<RottingComponent, ComponentShutdown>(OnShutdown);
+        SubscribeLocalEvent<RottingComponent, MobStateChangedEvent>(OnRottingMobStateChanged);
+        SubscribeLocalEvent<RottingComponent, RejuvenateEvent>(OnRejuvenate);
         SubscribeLocalEvent<RottingComponent, ExaminedEvent>(OnExamined);
     }
 
-    /// <summary>
-    /// Return the rot stage, usually from 0 to 2 inclusive.
-    /// </summary>
-    public int RotStage(EntityUid uid, RottingComponent? comp = null, PerishableComponent? perishable = null)
+    private void OnPerishableMapInit(EntityUid uid, PerishableComponent component, MapInitEvent args)
     {
-        if (!Resolve(uid, ref comp, ref perishable))
-            return 0;
+        component.RotNextUpdate = _timing.CurTime + component.PerishUpdateRate;
+    }
 
-        return (int) (comp.TotalRotTime.TotalSeconds / perishable.RotAfter.TotalSeconds);
+    private void OnMobStateChanged(EntityUid uid, PerishableComponent component, MobStateChangedEvent args)
+    {
+        if (args.NewMobState != MobState.Dead && args.OldMobState != MobState.Dead)
+            return;
+
+        if (HasComp<RottingComponent>(uid))
+            return;
+
+        component.RotAccumulator = TimeSpan.Zero;
+        component.RotNextUpdate = _timing.CurTime + component.PerishUpdateRate;
+    }
+
+    private void OnPerishableExamined(Entity<PerishableComponent> perishable, ref ExaminedEvent args)
+    {
+        int stage = PerishStage(perishable, MaxStages);
+        if (stage < 1 || stage > MaxStages)
+        {
+            // We dont push an examined string if it hasen't started "perishing" or it's already rotting
+            return;
+        }
+
+        var isMob = HasComp<MobStateComponent>(perishable);
+        var description = "perishable-" + stage + (!isMob ? "-nonmob" : string.Empty);
+        args.PushMarkup(Loc.GetString(description, ("target", Identity.Entity(perishable, EntityManager))));
+    }
+
+    private void OnShutdown(EntityUid uid, RottingComponent component, ComponentShutdown args)
+    {
+        if (TryComp<PerishableComponent>(uid, out var perishable))
+        {
+            perishable.RotNextUpdate = TimeSpan.Zero;
+        }
+    }
+
+    private void OnRottingMobStateChanged(EntityUid uid, RottingComponent component, MobStateChangedEvent args)
+    {
+        if (args.NewMobState == MobState.Dead)
+            return;
+        RemCompDeferred(uid, component);
+    }
+
+    private void OnRejuvenate(EntityUid uid, RottingComponent component, RejuvenateEvent args)
+    {
+        RemCompDeferred<RottingComponent>(uid);
     }
 
     private void OnExamined(EntityUid uid, RottingComponent component, ExaminedEvent args)
@@ -41,4 +97,75 @@ public abstract class SharedRottingSystem : EntitySystem
 
         args.PushMarkup(Loc.GetString(description, ("target", Identity.Entity(uid, EntityManager))));
     }
+
+    /// <summary>
+    /// Return an integer from 0 to maxStage representing how close to rotting an entity is. Used to
+    /// generate examine messages for items that are starting to rot.
+    /// </summary>
+    public int PerishStage(Entity<PerishableComponent> perishable, int maxStages)
+    {
+        if (perishable.Comp.RotAfter.TotalSeconds == 0 || perishable.Comp.RotAccumulator.TotalSeconds == 0)
+            return 0;
+        return (int)(1 + maxStages * perishable.Comp.RotAccumulator.TotalSeconds / perishable.Comp.RotAfter.TotalSeconds);
+    }
+
+    public bool IsRotProgressing(EntityUid uid, PerishableComponent? perishable)
+    {
+        // things don't perish by default.
+        if (!Resolve(uid, ref perishable, false))
+            return false;
+
+        // only dead things or inanimate objects can rot
+        if (TryComp<MobStateComponent>(uid, out var mobState) && !_mobState.IsDead(uid, mobState))
+            return false;
+
+        if (_container.TryGetOuterContainer(uid, Transform(uid), out var container) &&
+            HasComp<AntiRottingContainerComponent>(container.Owner))
+        {
+            return false;
+        }
+
+        var ev = new IsRottingEvent();
+        RaiseLocalEvent(uid, ref ev);
+
+        return !ev.Handled;
+    }
+
+    public bool IsRotten(EntityUid uid, RottingComponent? rotting = null)
+    {
+        return Resolve(uid, ref rotting, false);
+    }
+
+    public void ReduceAccumulator(EntityUid uid, TimeSpan time)
+    {
+        if (!TryComp<PerishableComponent>(uid, out var perishable))
+            return;
+
+        if (!TryComp<RottingComponent>(uid, out var rotting))
+        {
+            perishable.RotAccumulator -= time;
+            return;
+        }
+        var total = (rotting.TotalRotTime + perishable.RotAccumulator) - time;
+
+        if (total < perishable.RotAfter)
+        {
+            RemCompDeferred(uid, rotting);
+            perishable.RotAccumulator = total;
+        }
+
+        else
+            rotting.TotalRotTime = total - perishable.RotAfter;
+    }
+
+    /// <summary>
+    /// Return the rot stage, usually from 0 to 2 inclusive.
+    /// </summary>
+    public int RotStage(EntityUid uid, RottingComponent? comp = null, PerishableComponent? perishable = null)
+    {
+        if (!Resolve(uid, ref comp, ref perishable))
+            return 0;
+
+        return (int) (comp.TotalRotTime.TotalSeconds / perishable.RotAfter.TotalSeconds);
+    }
 }