1
0

DestructibleSystem.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Server.Administration.Logs;
  3. using Content.Server.Atmos.EntitySystems;
  4. using Content.Server.Body.Systems;
  5. using Content.Server.Construction;
  6. using Content.Server.Destructible.Thresholds;
  7. using Content.Server.Destructible.Thresholds.Behaviors;
  8. using Content.Server.Destructible.Thresholds.Triggers;
  9. using Content.Server.Explosion.EntitySystems;
  10. using Content.Server.Fluids.EntitySystems;
  11. using Content.Server.Stack;
  12. using Content.Shared.Chemistry.EntitySystems;
  13. using Content.Shared.Damage;
  14. using Content.Shared.Database;
  15. using Content.Shared.Destructible;
  16. using Content.Shared.FixedPoint;
  17. using JetBrains.Annotations;
  18. using Robust.Server.Audio;
  19. using Robust.Server.GameObjects;
  20. using Robust.Shared.Containers;
  21. using Robust.Shared.Prototypes;
  22. using Robust.Shared.Random;
  23. using System.Linq;
  24. namespace Content.Server.Destructible
  25. {
  26. [UsedImplicitly]
  27. public sealed class DestructibleSystem : SharedDestructibleSystem
  28. {
  29. [Dependency] public readonly IRobustRandom Random = default!;
  30. public new IEntityManager EntityManager => base.EntityManager;
  31. [Dependency] public readonly AtmosphereSystem AtmosphereSystem = default!;
  32. [Dependency] public readonly AudioSystem AudioSystem = default!;
  33. [Dependency] public readonly BodySystem BodySystem = default!;
  34. [Dependency] public readonly ConstructionSystem ConstructionSystem = default!;
  35. [Dependency] public readonly ExplosionSystem ExplosionSystem = default!;
  36. [Dependency] public readonly StackSystem StackSystem = default!;
  37. [Dependency] public readonly TriggerSystem TriggerSystem = default!;
  38. [Dependency] public readonly SharedSolutionContainerSystem SolutionContainerSystem = default!;
  39. [Dependency] public readonly PuddleSystem PuddleSystem = default!;
  40. [Dependency] public readonly SharedContainerSystem ContainerSystem = default!;
  41. [Dependency] public readonly IPrototypeManager PrototypeManager = default!;
  42. [Dependency] public readonly IComponentFactory ComponentFactory = default!;
  43. [Dependency] public readonly IAdminLogManager _adminLogger = default!;
  44. public override void Initialize()
  45. {
  46. base.Initialize();
  47. SubscribeLocalEvent<DestructibleComponent, DamageChangedEvent>(Execute);
  48. }
  49. /// <summary>
  50. /// Check if any thresholds were reached. if they were, execute them.
  51. /// </summary>
  52. public void Execute(EntityUid uid, DestructibleComponent component, DamageChangedEvent args)
  53. {
  54. foreach (var threshold in component.Thresholds)
  55. {
  56. if (threshold.Reached(args.Damageable, this))
  57. {
  58. RaiseLocalEvent(uid, new DamageThresholdReached(component, threshold), true);
  59. // Convert behaviors into string for logs
  60. var triggeredBehaviors = string.Join(", ", threshold.Behaviors.Select(b =>
  61. {
  62. if (b is DoActsBehavior doActsBehavior)
  63. {
  64. return $"{b.GetType().Name}:{doActsBehavior.Acts.ToString()}";
  65. }
  66. return b.GetType().Name;
  67. }));
  68. if (args.Origin != null)
  69. {
  70. _adminLogger.Add(LogType.Damaged, LogImpact.Medium,
  71. $"{ToPrettyString(args.Origin.Value):actor} caused {ToPrettyString(uid):subject} to trigger [{triggeredBehaviors}]");
  72. }
  73. else
  74. {
  75. _adminLogger.Add(LogType.Damaged, LogImpact.Medium,
  76. $"Unknown damage source caused {ToPrettyString(uid):subject} to trigger [{triggeredBehaviors}]");
  77. }
  78. threshold.Execute(uid, this, EntityManager, args.Origin);
  79. }
  80. // if destruction behavior (or some other deletion effect) occurred, don't run other triggers.
  81. if (EntityManager.IsQueuedForDeletion(uid) || Deleted(uid))
  82. return;
  83. }
  84. }
  85. public bool TryGetDestroyedAt(Entity<DestructibleComponent?> ent, [NotNullWhen(true)] out FixedPoint2? destroyedAt)
  86. {
  87. destroyedAt = null;
  88. if (!Resolve(ent, ref ent.Comp, false))
  89. return false;
  90. destroyedAt = DestroyedAt(ent, ent.Comp);
  91. return true;
  92. }
  93. // FFS this shouldn't be this hard. Maybe this should just be a field of the destructible component. Its not
  94. // like there is currently any entity that is NOT just destroyed upon reaching a total-damage value.
  95. /// <summary>
  96. /// Figure out how much damage an entity needs to have in order to be destroyed.
  97. /// </summary>
  98. /// <remarks>
  99. /// This assumes that this entity has some sort of destruction or breakage behavior triggered by a
  100. /// total-damage threshold.
  101. /// </remarks>
  102. public FixedPoint2 DestroyedAt(EntityUid uid, DestructibleComponent? destructible = null)
  103. {
  104. if (!Resolve(uid, ref destructible, logMissing: false))
  105. return FixedPoint2.MaxValue;
  106. // We have nested for loops here, but the vast majority of components only have one threshold with 1-3 behaviors.
  107. // Really, this should probably just be a property of the damageable component.
  108. var damageNeeded = FixedPoint2.MaxValue;
  109. foreach (var threshold in destructible.Thresholds)
  110. {
  111. if (threshold.Trigger is not DamageTrigger trigger)
  112. continue;
  113. foreach (var behavior in threshold.Behaviors)
  114. {
  115. if (behavior is DoActsBehavior actBehavior &&
  116. actBehavior.HasAct(ThresholdActs.Destruction | ThresholdActs.Breakage))
  117. {
  118. damageNeeded = Math.Min(damageNeeded.Float(), trigger.Damage);
  119. }
  120. }
  121. }
  122. return damageNeeded;
  123. }
  124. }
  125. // Currently only used for destructible integration tests. Unless other uses are found for this, maybe this should just be removed and the tests redone.
  126. /// <summary>
  127. /// Event raised when a <see cref="DamageThreshold"/> is reached.
  128. /// </summary>
  129. public sealed class DamageThresholdReached : EntityEventArgs
  130. {
  131. public readonly DestructibleComponent Parent;
  132. public readonly DamageThreshold Threshold;
  133. public DamageThresholdReached(DestructibleComponent parent, DamageThreshold threshold)
  134. {
  135. Parent = parent;
  136. Threshold = threshold;
  137. }
  138. }
  139. }