| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Components;
- using Content.Server.Xenoarchaeology.XenoArtifacts.Events;
- using Content.Shared.Mind.Components;
- using Content.Shared.Mobs.Components;
- using Content.Shared.Teleportation.Systems;
- using Robust.Shared.Collections;
- using Robust.Shared.Containers;
- using Robust.Shared.Random;
- namespace Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Systems;
- public sealed class PortalArtifactSystem : EntitySystem
- {
- [Dependency] private readonly IRobustRandom _random = default!;
- [Dependency] private readonly LinkedEntitySystem _link = default!;
- [Dependency] private readonly SharedTransformSystem _transform = default!;
- [Dependency] private readonly SharedContainerSystem _container = default!;
- public override void Initialize()
- {
- base.Initialize();
- SubscribeLocalEvent<PortalArtifactComponent, ArtifactActivatedEvent>(OnActivate);
- }
- private void OnActivate(Entity<PortalArtifactComponent> artifact, ref ArtifactActivatedEvent args)
- {
- var map = Transform(artifact).MapID;
- var validMinds = new ValueList<EntityUid>();
- var mindQuery = EntityQueryEnumerator<MindContainerComponent, MobStateComponent, TransformComponent, MetaDataComponent>();
- while (mindQuery.MoveNext(out var uid, out var mc, out _, out var xform, out var meta))
- {
- // 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
- if (mc.HasMind && !_container.IsEntityOrParentInContainer(uid, meta: meta, xform: xform) && xform.MapID == map)
- {
- validMinds.Add(uid);
- }
- }
- //this would only be 0 if there were a station full of AIs and no one else, in that case just stop this function
- if (validMinds.Count == 0)
- return;
- var firstPortal = Spawn(artifact.Comp.PortalProto, _transform.GetMapCoordinates(artifact));
- var target = _random.Pick(validMinds);
- var secondPortal = Spawn(artifact.Comp.PortalProto, _transform.GetMapCoordinates(target));
- //Manual position swapping, because the portal that opens doesn't trigger a collision, and doesn't teleport targets the first time.
- _transform.SwapPositions(target, artifact.Owner);
- _link.TryLink(firstPortal, secondPortal, true);
- }
- }
|