]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Add MinHealth Construction Condition (#30892)
authorblueDev2 <89804215+blueDev2@users.noreply.github.com>
Sun, 11 Aug 2024 09:14:35 +0000 (05:14 -0400)
committerGitHub <noreply@github.com>
Sun, 11 Aug 2024 09:14:35 +0000 (19:14 +1000)
* Add Min Health Condition and associated Locale

* Add Comment

Content.Server/Construction/Conditions/MinHealth.cs [new file with mode: 0644]
Resources/Locale/en-US/construction/conditions/min-health.ftl [new file with mode: 0644]

diff --git a/Content.Server/Construction/Conditions/MinHealth.cs b/Content.Server/Construction/Conditions/MinHealth.cs
new file mode 100644 (file)
index 0000000..980f6a4
--- /dev/null
@@ -0,0 +1,92 @@
+using Content.Server.Destructible;
+using Content.Shared.Construction;
+using Content.Shared.Damage;
+using Content.Shared.Examine;
+using Content.Shared.FixedPoint;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Content.Server.Construction.Conditions;
+
+/// <summary>
+/// Requires that the structure has at least some amount of health
+/// </summary>
+[DataDefinition]
+public sealed partial class MinHealth : IGraphCondition
+{
+    /// <summary>
+    /// If ByProportion is true, Threshold is a value less than or equal to 1, but more than 0,
+    /// which is compared to the percent of health remaining in the structure.
+    /// Else, Threshold is any positive value with at most 2 decimal points of percision,
+    /// which is compared to the current health of the structure.
+    /// </summary>
+    [DataField]
+    public FixedPoint2 Threshold = 1;
+    [DataField]
+    public bool ByProportion = false;
+
+    [DataField]
+    public bool IncludeEquals = true;
+
+    public bool Condition(EntityUid uid, IEntityManager entMan)
+    {
+        if (!entMan.TryGetComponent(uid, out DestructibleComponent? destructibleComp) ||
+            !entMan.TryGetComponent(uid, out DamageableComponent? damageComp))
+        {
+            return false;
+        }
+
+        var destructionSys = entMan.System<DestructibleSystem>();
+        var maxHealth = destructionSys.DestroyedAt(uid, destructibleComp);
+        var curHealth = maxHealth - damageComp.TotalDamage;
+        var proportionHealth = curHealth / maxHealth;
+
+        if (IncludeEquals)
+        {
+            if (ByProportion)
+            {
+                return proportionHealth >= Threshold;
+            }
+            else
+            {
+                return curHealth >= Threshold;
+            }
+        }
+        else
+        {
+            if (ByProportion)
+            {
+                return proportionHealth > Threshold;
+            }
+            else
+            {
+                return curHealth > Threshold;
+            }
+        }
+    }
+
+    public bool DoExamine(ExaminedEvent args)
+    {
+        var entMan = IoCManager.Resolve<IEntityManager>();
+        var entity = args.Examined;
+
+        if (Condition(entity, entMan))
+        {
+            return false;
+        }
+        args.PushMarkup(Loc.GetString("construction-examine-condition-low-health"));
+
+        return true;
+    }
+
+    public IEnumerable<ConstructionGuideEntry> GenerateGuideEntry()
+    {
+        yield return new ConstructionGuideEntry()
+        {
+            Localization = "construction-step-condition-low-health"
+        };
+    }
+}
diff --git a/Resources/Locale/en-US/construction/conditions/min-health.ftl b/Resources/Locale/en-US/construction/conditions/min-health.ftl
new file mode 100644 (file)
index 0000000..daaddcf
--- /dev/null
@@ -0,0 +1,2 @@
+construction-examine-condition-low-health = First, repair it.
+construction-step-condition-low-health = It must be repaired.