Add MinHealth Construction Condition (#30892)
* Add Min Health Condition and associated Locale * Add Comment
This commit is contained in:
92
Content.Server/Construction/Conditions/MinHealth.cs
Normal file
92
Content.Server/Construction/Conditions/MinHealth.cs
Normal file
@@ -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"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
construction-examine-condition-low-health = First, repair it.
|
||||
construction-step-condition-low-health = It must be repaired.
|
||||
Reference in New Issue
Block a user