PortalArtifactSystem.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Components;
  2. using Content.Server.Xenoarchaeology.XenoArtifacts.Events;
  3. using Content.Shared.Mind.Components;
  4. using Content.Shared.Mobs.Components;
  5. using Content.Shared.Teleportation.Systems;
  6. using Robust.Shared.Collections;
  7. using Robust.Shared.Containers;
  8. using Robust.Shared.Random;
  9. namespace Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Systems;
  10. public sealed class PortalArtifactSystem : EntitySystem
  11. {
  12. [Dependency] private readonly IRobustRandom _random = default!;
  13. [Dependency] private readonly LinkedEntitySystem _link = default!;
  14. [Dependency] private readonly SharedTransformSystem _transform = default!;
  15. [Dependency] private readonly SharedContainerSystem _container = default!;
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<PortalArtifactComponent, ArtifactActivatedEvent>(OnActivate);
  20. }
  21. private void OnActivate(Entity<PortalArtifactComponent> artifact, ref ArtifactActivatedEvent args)
  22. {
  23. var map = Transform(artifact).MapID;
  24. var validMinds = new ValueList<EntityUid>();
  25. var mindQuery = EntityQueryEnumerator<MindContainerComponent, MobStateComponent, TransformComponent, MetaDataComponent>();
  26. while (mindQuery.MoveNext(out var uid, out var mc, out _, out var xform, out var meta))
  27. {
  28. // check if the MindContainer has a Mind and if the entity is not in a container (this also auto excludes AI) and if they are on the same map
  29. if (mc.HasMind && !_container.IsEntityOrParentInContainer(uid, meta: meta, xform: xform) && xform.MapID == map)
  30. {
  31. validMinds.Add(uid);
  32. }
  33. }
  34. //this would only be 0 if there were a station full of AIs and no one else, in that case just stop this function
  35. if (validMinds.Count == 0)
  36. return;
  37. var firstPortal = Spawn(artifact.Comp.PortalProto, _transform.GetMapCoordinates(artifact));
  38. var target = _random.Pick(validMinds);
  39. var secondPortal = Spawn(artifact.Comp.PortalProto, _transform.GetMapCoordinates(target));
  40. //Manual position swapping, because the portal that opens doesn't trigger a collision, and doesn't teleport targets the first time.
  41. _transform.SwapPositions(target, artifact.Owner);
  42. _link.TryLink(firstPortal, secondPortal, true);
  43. }
  44. }