DestructibleThresholdActivationTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System.Linq;
  2. using Content.Server.Destructible;
  3. using Content.Server.Destructible.Thresholds;
  4. using Content.Server.Destructible.Thresholds.Behaviors;
  5. using Content.Server.Destructible.Thresholds.Triggers;
  6. using Content.Shared.Damage;
  7. using Content.Shared.Damage.Prototypes;
  8. using Content.Shared.FixedPoint;
  9. using Robust.Shared.Audio;
  10. using Robust.Shared.Audio.Systems;
  11. using Robust.Shared.GameObjects;
  12. using Robust.Shared.Prototypes;
  13. using static Content.IntegrationTests.Tests.Destructible.DestructibleTestPrototypes;
  14. namespace Content.IntegrationTests.Tests.Destructible
  15. {
  16. [TestFixture]
  17. [TestOf(typeof(DestructibleComponent))]
  18. [TestOf(typeof(DamageThreshold))]
  19. public sealed class DestructibleThresholdActivationTest
  20. {
  21. [Test]
  22. public async Task Test()
  23. {
  24. await using var pair = await PoolManager.GetServerClient();
  25. var server = pair.Server;
  26. var sEntityManager = server.ResolveDependency<IEntityManager>();
  27. var sPrototypeManager = server.ResolveDependency<IPrototypeManager>();
  28. var sEntitySystemManager = server.ResolveDependency<IEntitySystemManager>();
  29. var audio = sEntitySystemManager.GetEntitySystem<SharedAudioSystem>();
  30. var testMap = await pair.CreateTestMap();
  31. EntityUid sDestructibleEntity = default;
  32. DamageableComponent sDamageableComponent = null;
  33. DestructibleComponent sDestructibleComponent = null;
  34. TestDestructibleListenerSystem sTestThresholdListenerSystem = null;
  35. DamageableSystem sDamageableSystem = null;
  36. await server.WaitPost(() =>
  37. {
  38. var coordinates = testMap.GridCoords;
  39. sDestructibleEntity = sEntityManager.SpawnEntity(DestructibleEntityId, coordinates);
  40. sDamageableComponent = sEntityManager.GetComponent<DamageableComponent>(sDestructibleEntity);
  41. sDestructibleComponent = sEntityManager.GetComponent<DestructibleComponent>(sDestructibleEntity);
  42. sTestThresholdListenerSystem = sEntitySystemManager.GetEntitySystem<TestDestructibleListenerSystem>();
  43. sTestThresholdListenerSystem.ThresholdsReached.Clear();
  44. sDamageableSystem = sEntitySystemManager.GetEntitySystem<DamageableSystem>();
  45. });
  46. await server.WaitRunTicks(5);
  47. await server.WaitAssertion(() =>
  48. {
  49. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Is.Empty);
  50. });
  51. await server.WaitAssertion(() =>
  52. {
  53. var bluntDamage = new DamageSpecifier(sPrototypeManager.Index<DamageTypePrototype>("TestBlunt"), 10);
  54. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage, true);
  55. // No thresholds reached yet, the earliest one is at 20 damage
  56. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Is.Empty);
  57. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage, true);
  58. // Only one threshold reached, 20
  59. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Has.Count.EqualTo(1));
  60. // Threshold 20
  61. var msg = sTestThresholdListenerSystem.ThresholdsReached[0];
  62. var threshold = msg.Threshold;
  63. // Check that it matches the YAML prototype
  64. Assert.Multiple(() =>
  65. {
  66. Assert.That(threshold.Behaviors, Is.Empty);
  67. Assert.That(threshold.Trigger, Is.Not.Null);
  68. Assert.That(threshold.Triggered, Is.True);
  69. });
  70. sTestThresholdListenerSystem.ThresholdsReached.Clear();
  71. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage * 3, true);
  72. // One threshold reached, 50, since 20 already triggered before and it has not been healed below that amount
  73. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Has.Count.EqualTo(1));
  74. // Threshold 50
  75. msg = sTestThresholdListenerSystem.ThresholdsReached[0];
  76. threshold = msg.Threshold;
  77. // Check that it matches the YAML prototype
  78. Assert.That(threshold.Behaviors, Has.Count.EqualTo(3));
  79. var soundThreshold = (PlaySoundBehavior) threshold.Behaviors[0];
  80. var spawnThreshold = (SpawnEntitiesBehavior) threshold.Behaviors[1];
  81. var actsThreshold = (DoActsBehavior) threshold.Behaviors[2];
  82. Assert.Multiple(() =>
  83. {
  84. Assert.That(actsThreshold.Acts, Is.EqualTo(ThresholdActs.Breakage));
  85. Assert.That(spawnThreshold.Spawn, Is.Not.Null);
  86. Assert.That(spawnThreshold.Spawn, Has.Count.EqualTo(1));
  87. Assert.That(spawnThreshold.Spawn.Single().Key, Is.EqualTo(SpawnedEntityId));
  88. Assert.That(spawnThreshold.Spawn.Single().Value.Min, Is.EqualTo(1));
  89. Assert.That(spawnThreshold.Spawn.Single().Value.Max, Is.EqualTo(1));
  90. Assert.That(threshold.Trigger, Is.Not.Null);
  91. Assert.That(threshold.Triggered, Is.True);
  92. });
  93. sTestThresholdListenerSystem.ThresholdsReached.Clear();
  94. // Damage for 50 again, up to 100 now
  95. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage * 5, true);
  96. // No thresholds reached as they weren't healed below the trigger amount
  97. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Is.Empty);
  98. // Set damage to 0
  99. sDamageableSystem.SetAllDamage(sDestructibleEntity, sDamageableComponent, 0);
  100. // Damage for 100, up to 100
  101. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage * 10, true);
  102. // Two thresholds reached as damage increased past the previous, 20 and 50
  103. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Has.Count.EqualTo(2));
  104. sTestThresholdListenerSystem.ThresholdsReached.Clear();
  105. // Heal the entity for 40 damage, down to 60
  106. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage * -4, true);
  107. // ThresholdsLookup don't work backwards
  108. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Is.Empty);
  109. // Damage for 10, up to 70
  110. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage, true);
  111. // Not enough healing to de-trigger a threshold
  112. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Is.Empty);
  113. // Heal by 30, down to 40
  114. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage * -3, true);
  115. // ThresholdsLookup don't work backwards
  116. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Is.Empty);
  117. // Damage up to 50 again
  118. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage, true);
  119. // The 50 threshold should have triggered again, after being healed
  120. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Has.Count.EqualTo(1));
  121. msg = sTestThresholdListenerSystem.ThresholdsReached[0];
  122. threshold = msg.Threshold;
  123. // Check that it matches the YAML prototype
  124. Assert.That(threshold.Behaviors, Has.Count.EqualTo(3));
  125. soundThreshold = (PlaySoundBehavior) threshold.Behaviors[0];
  126. spawnThreshold = (SpawnEntitiesBehavior) threshold.Behaviors[1];
  127. actsThreshold = (DoActsBehavior) threshold.Behaviors[2];
  128. // Check that it matches the YAML prototype
  129. Assert.Multiple(() =>
  130. {
  131. Assert.That(actsThreshold.Acts, Is.EqualTo(ThresholdActs.Breakage));
  132. Assert.That(spawnThreshold.Spawn, Is.Not.Null);
  133. Assert.That(spawnThreshold.Spawn, Has.Count.EqualTo(1));
  134. Assert.That(spawnThreshold.Spawn.Single().Key, Is.EqualTo(SpawnedEntityId));
  135. Assert.That(spawnThreshold.Spawn.Single().Value.Min, Is.EqualTo(1));
  136. Assert.That(spawnThreshold.Spawn.Single().Value.Max, Is.EqualTo(1));
  137. Assert.That(threshold.Trigger, Is.Not.Null);
  138. Assert.That(threshold.Triggered, Is.True);
  139. });
  140. // Reset thresholds reached
  141. sTestThresholdListenerSystem.ThresholdsReached.Clear();
  142. // Heal all damage
  143. sDamageableSystem.SetAllDamage(sDestructibleEntity, sDamageableComponent, 0);
  144. // Damage up to 50
  145. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage * 5, true);
  146. Assert.Multiple(() =>
  147. {
  148. // Check that the total damage matches
  149. Assert.That(sDamageableComponent.TotalDamage, Is.EqualTo(FixedPoint2.New(50)));
  150. // Both thresholds should have triggered
  151. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Has.Exactly(2).Items);
  152. });
  153. // Verify the first one, should be the lowest one (20)
  154. msg = sTestThresholdListenerSystem.ThresholdsReached[0];
  155. var trigger = (DamageTrigger) msg.Threshold.Trigger;
  156. Assert.Multiple(() =>
  157. {
  158. Assert.That(trigger, Is.Not.Null);
  159. Assert.That(trigger.Damage, Is.EqualTo(20));
  160. });
  161. threshold = msg.Threshold;
  162. // Check that it matches the YAML prototype
  163. Assert.That(threshold.Behaviors, Is.Empty);
  164. // Verify the second one, should be the highest one (50)
  165. msg = sTestThresholdListenerSystem.ThresholdsReached[1];
  166. trigger = (DamageTrigger) msg.Threshold.Trigger;
  167. Assert.Multiple(() =>
  168. {
  169. Assert.That(trigger, Is.Not.Null);
  170. Assert.That(trigger.Damage, Is.EqualTo(50));
  171. });
  172. threshold = msg.Threshold;
  173. Assert.That(threshold.Behaviors, Has.Count.EqualTo(3));
  174. soundThreshold = (PlaySoundBehavior) threshold.Behaviors[0];
  175. spawnThreshold = (SpawnEntitiesBehavior) threshold.Behaviors[1];
  176. actsThreshold = (DoActsBehavior) threshold.Behaviors[2];
  177. // Check that it matches the YAML prototype
  178. Assert.Multiple(() =>
  179. {
  180. Assert.That(actsThreshold.Acts, Is.EqualTo(ThresholdActs.Breakage));
  181. Assert.That(spawnThreshold.Spawn, Is.Not.Null);
  182. Assert.That(spawnThreshold.Spawn, Has.Count.EqualTo(1));
  183. Assert.That(spawnThreshold.Spawn.Single().Key, Is.EqualTo(SpawnedEntityId));
  184. Assert.That(spawnThreshold.Spawn.Single().Value.Min, Is.EqualTo(1));
  185. Assert.That(spawnThreshold.Spawn.Single().Value.Max, Is.EqualTo(1));
  186. Assert.That(threshold.Trigger, Is.Not.Null);
  187. Assert.That(threshold.Triggered, Is.True);
  188. });
  189. // Reset thresholds reached
  190. sTestThresholdListenerSystem.ThresholdsReached.Clear();
  191. // Heal the entity completely
  192. sDamageableSystem.SetAllDamage(sDestructibleEntity, sDamageableComponent, 0);
  193. // Check that the entity has 0 damage
  194. Assert.That(sDamageableComponent.TotalDamage, Is.EqualTo(FixedPoint2.Zero));
  195. // Set both thresholds to only trigger once
  196. foreach (var destructibleThreshold in sDestructibleComponent.Thresholds)
  197. {
  198. Assert.That(destructibleThreshold.Trigger, Is.Not.Null);
  199. destructibleThreshold.TriggersOnce = true;
  200. }
  201. // Damage the entity up to 50 damage again
  202. sDamageableSystem.TryChangeDamage(sDestructibleEntity, bluntDamage * 5, true);
  203. Assert.Multiple(() =>
  204. {
  205. // Check that the total damage matches
  206. Assert.That(sDamageableComponent.TotalDamage, Is.EqualTo(FixedPoint2.New(50)));
  207. // No thresholds should have triggered as they were already triggered before, and they are set to only trigger once
  208. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Is.Empty);
  209. });
  210. // Set both thresholds to trigger multiple times
  211. foreach (var destructibleThreshold in sDestructibleComponent.Thresholds)
  212. {
  213. Assert.That(destructibleThreshold.Trigger, Is.Not.Null);
  214. destructibleThreshold.TriggersOnce = false;
  215. }
  216. Assert.Multiple(() =>
  217. {
  218. // Check that the total damage matches
  219. Assert.That(sDamageableComponent.TotalDamage, Is.EqualTo(FixedPoint2.New(50)));
  220. // They shouldn't have been triggered by changing TriggersOnce
  221. Assert.That(sTestThresholdListenerSystem.ThresholdsReached, Is.Empty);
  222. });
  223. });
  224. await pair.CleanReturnAsync();
  225. }
  226. }
  227. }