RandomCloneSpawnerSystem.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Content.Server.Cloning.Components;
  2. using Content.Shared.Mind;
  3. using Content.Shared.Mobs.Systems;
  4. using Robust.Shared.Prototypes;
  5. using Robust.Shared.Random;
  6. namespace Content.Server.Cloning;
  7. /// <summary>
  8. /// This deals with spawning and setting up a clone of a random crew member.
  9. /// </summary>
  10. public sealed class RandomCloneSpawnerSystem : EntitySystem
  11. {
  12. [Dependency] private readonly CloningSystem _cloning = default!;
  13. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  14. [Dependency] private readonly IRobustRandom _random = default!;
  15. [Dependency] private readonly SharedTransformSystem _transformSystem = default!;
  16. [Dependency] private readonly SharedMindSystem _mind = default!;
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. SubscribeLocalEvent<RandomCloneSpawnerComponent, MapInitEvent>(OnMapInit);
  21. }
  22. private void OnMapInit(Entity<RandomCloneSpawnerComponent> ent, ref MapInitEvent args)
  23. {
  24. QueueDel(ent.Owner);
  25. if (!_prototypeManager.TryIndex(ent.Comp.Settings, out var settings))
  26. {
  27. Log.Error($"Used invalid cloning settings {ent.Comp.Settings} for RandomCloneSpawner");
  28. return;
  29. }
  30. var allHumans = _mind.GetAliveHumans();
  31. if (allHumans.Count == 0)
  32. return;
  33. var bodyToClone = _random.Pick(allHumans).Comp.OwnedEntity;
  34. if (bodyToClone != null)
  35. _cloning.TryCloning(bodyToClone.Value, _transformSystem.GetMapCoordinates(ent.Owner), settings, out _);
  36. }
  37. }