MinHealth.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Content.Server.Destructible;
  2. using Content.Shared.Construction;
  3. using Content.Shared.Damage;
  4. using Content.Shared.Examine;
  5. using Content.Shared.FixedPoint;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Content.Server.Construction.Conditions;
  12. /// <summary>
  13. /// Requires that the structure has at least some amount of health
  14. /// </summary>
  15. [DataDefinition]
  16. public sealed partial class MinHealth : IGraphCondition
  17. {
  18. /// <summary>
  19. /// If ByProportion is true, Threshold is a value less than or equal to 1, but more than 0,
  20. /// which is compared to the percent of health remaining in the structure.
  21. /// Else, Threshold is any positive value with at most 2 decimal points of percision,
  22. /// which is compared to the current health of the structure.
  23. /// </summary>
  24. [DataField]
  25. public FixedPoint2 Threshold = 1;
  26. [DataField]
  27. public bool ByProportion = false;
  28. [DataField]
  29. public bool IncludeEquals = true;
  30. public bool Condition(EntityUid uid, IEntityManager entMan)
  31. {
  32. if (!entMan.TryGetComponent(uid, out DestructibleComponent? destructibleComp) ||
  33. !entMan.TryGetComponent(uid, out DamageableComponent? damageComp))
  34. {
  35. return false;
  36. }
  37. var destructionSys = entMan.System<DestructibleSystem>();
  38. var maxHealth = destructionSys.DestroyedAt(uid, destructibleComp);
  39. var curHealth = maxHealth - damageComp.TotalDamage;
  40. var proportionHealth = curHealth / maxHealth;
  41. if (IncludeEquals)
  42. {
  43. if (ByProportion)
  44. {
  45. return proportionHealth >= Threshold;
  46. }
  47. else
  48. {
  49. return curHealth >= Threshold;
  50. }
  51. }
  52. else
  53. {
  54. if (ByProportion)
  55. {
  56. return proportionHealth > Threshold;
  57. }
  58. else
  59. {
  60. return curHealth > Threshold;
  61. }
  62. }
  63. }
  64. public bool DoExamine(ExaminedEvent args)
  65. {
  66. var entMan = IoCManager.Resolve<IEntityManager>();
  67. var entity = args.Examined;
  68. if (Condition(entity, entMan))
  69. {
  70. return false;
  71. }
  72. args.PushMarkup(Loc.GetString("construction-examine-condition-low-health"));
  73. return true;
  74. }
  75. public IEnumerable<ConstructionGuideEntry> GenerateGuideEntry()
  76. {
  77. yield return new ConstructionGuideEntry()
  78. {
  79. Localization = "construction-step-condition-low-health"
  80. };
  81. }
  82. }