SpawnEntitiesBehavior.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Numerics;
  2. using Content.Server.Forensics;
  3. using Content.Server.Stack;
  4. using Content.Shared.Destructible.Thresholds;
  5. using Content.Shared.Prototypes;
  6. using Content.Shared.Stacks;
  7. using Robust.Server.GameObjects;
  8. using Robust.Shared.Prototypes;
  9. using Robust.Shared.Random;
  10. namespace Content.Server.Destructible.Thresholds.Behaviors
  11. {
  12. [Serializable]
  13. [DataDefinition]
  14. public sealed partial class SpawnEntitiesBehavior : IThresholdBehavior
  15. {
  16. /// <summary>
  17. /// Entities spawned on reaching this threshold, from a min to a max.
  18. /// </summary>
  19. [DataField]
  20. public Dictionary<EntProtoId, MinMax> Spawn = new();
  21. [DataField("offset")]
  22. public float Offset { get; set; } = 0.5f;
  23. [DataField("transferForensics")]
  24. public bool DoTransferForensics;
  25. [DataField]
  26. public bool SpawnInContainer;
  27. public void Execute(EntityUid owner, DestructibleSystem system, EntityUid? cause = null)
  28. {
  29. var tSys = system.EntityManager.System<TransformSystem>();
  30. var position = tSys.GetMapCoordinates(owner);
  31. var getRandomVector = () => new Vector2(system.Random.NextFloat(-Offset, Offset), system.Random.NextFloat(-Offset, Offset));
  32. var executions = 1;
  33. if (system.EntityManager.TryGetComponent<StackComponent>(owner, out var stack))
  34. {
  35. executions = stack.Count;
  36. }
  37. foreach (var (entityId, minMax) in Spawn)
  38. {
  39. for (var execution = 0; execution < executions; execution++)
  40. {
  41. var count = minMax.Min >= minMax.Max
  42. ? minMax.Min
  43. : system.Random.Next(minMax.Min, minMax.Max + 1);
  44. if (count == 0)
  45. continue;
  46. if (EntityPrototypeHelpers.HasComponent<StackComponent>(entityId, system.PrototypeManager, system.ComponentFactory))
  47. {
  48. var spawned = SpawnInContainer
  49. ? system.EntityManager.SpawnNextToOrDrop(entityId, owner)
  50. : system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector()));
  51. system.StackSystem.SetCount(spawned, count);
  52. TransferForensics(spawned, system, owner);
  53. }
  54. else
  55. {
  56. for (var i = 0; i < count; i++)
  57. {
  58. var spawned = SpawnInContainer
  59. ? system.EntityManager.SpawnNextToOrDrop(entityId, owner)
  60. : system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector()));
  61. TransferForensics(spawned, system, owner);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. public void TransferForensics(EntityUid spawned, DestructibleSystem system, EntityUid owner)
  68. {
  69. if (!DoTransferForensics ||
  70. !system.EntityManager.TryGetComponent<ForensicsComponent>(owner, out var forensicsComponent))
  71. return;
  72. var comp = system.EntityManager.EnsureComponent<ForensicsComponent>(spawned);
  73. comp.DNAs = forensicsComponent.DNAs;
  74. if (!system.Random.Prob(0.4f))
  75. return;
  76. comp.Fingerprints = forensicsComponent.Fingerprints;
  77. comp.Fibers = forensicsComponent.Fibers;
  78. }
  79. }
  80. }