From 276463876355907a1363317d1423aa6fe100c524 Mon Sep 17 00:00:00 2001 From: chromiumboy <50505512+chromiumboy@users.noreply.github.com> Date: Mon, 17 Feb 2025 23:26:02 -0600 Subject: [PATCH] Minor station AI code cleanup (#34476) --- Content.Server/Holopad/HolopadSystem.cs | 56 +++++++++---------- .../Silicons/StationAi/StationAiSystem.cs | 6 +- .../StationAi/SharedStationAiSystem.Held.cs | 22 +++----- .../StationAi/SharedStationAiSystem.cs | 39 ++----------- 4 files changed, 45 insertions(+), 78 deletions(-) diff --git a/Content.Server/Holopad/HolopadSystem.cs b/Content.Server/Holopad/HolopadSystem.cs index 3e99137a2d..51cb3876fe 100644 --- a/Content.Server/Holopad/HolopadSystem.cs +++ b/Content.Server/Holopad/HolopadSystem.cs @@ -115,13 +115,13 @@ public sealed class HolopadSystem : SharedHolopadSystem if (source != null) { // Close any AI request windows - if (_stationAiSystem.TryGetStationAiCore(args.Actor, out var stationAiCore) && stationAiCore != null) + if (_stationAiSystem.TryGetCore(args.Actor, out var stationAiCore)) _userInterfaceSystem.CloseUi(receiver.Owner, HolopadUiKey.AiRequestWindow, args.Actor); // Try to warn the AI if the source of the call is out of its range if (TryComp(stationAiCore, out var stationAiTelephone) && TryComp(source, out var sourceTelephone) && - !_telephoneSystem.IsSourceInRangeOfReceiver((stationAiCore.Value.Owner, stationAiTelephone), (source.Value.Owner, sourceTelephone))) + !_telephoneSystem.IsSourceInRangeOfReceiver((stationAiCore.Owner, stationAiTelephone), (source.Value.Owner, sourceTelephone))) { _popupSystem.PopupEntity(Loc.GetString("holopad-ai-is-unable-to-reach-holopad"), receiver, args.Actor); return; @@ -150,11 +150,11 @@ public sealed class HolopadSystem : SharedHolopadSystem // If the user is an AI, end all calls originating from its // associated core to ensure that any broadcasts will end if (!TryComp(args.Actor, out var stationAiHeld) || - !_stationAiSystem.TryGetStationAiCore((args.Actor, stationAiHeld), out var stationAiCore)) + !_stationAiSystem.TryGetCore(args.Actor, out var stationAiCore)) return; if (TryComp(stationAiCore, out var telephone)) - _telephoneSystem.EndTelephoneCalls((stationAiCore.Value, telephone)); + _telephoneSystem.EndTelephoneCalls((stationAiCore, telephone)); } private void OnHolopadActivateProjector(Entity entity, ref HolopadActivateProjectorMessage args) @@ -176,17 +176,17 @@ public sealed class HolopadSystem : SharedHolopadSystem // Link the AI to the holopad they are broadcasting from LinkHolopadToUser(source, args.Actor); - if (!_stationAiSystem.TryGetStationAiCore((args.Actor, stationAiHeld), out var stationAiCore) || - stationAiCore.Value.Comp.RemoteEntity == null || + if (!_stationAiSystem.TryGetCore(args.Actor, out var stationAiCore) || + stationAiCore.Comp?.RemoteEntity == null || !TryComp(stationAiCore, out var stationAiCoreHolopad)) return; // Execute the broadcast, but have it originate from the AI core - ExecuteBroadcast((stationAiCore.Value, stationAiCoreHolopad), args.Actor); + ExecuteBroadcast((stationAiCore, stationAiCoreHolopad), args.Actor); // Switch the AI's perspective from free roaming to the target holopad - _xformSystem.SetCoordinates(stationAiCore.Value.Comp.RemoteEntity.Value, Transform(source).Coordinates); - _stationAiSystem.SwitchRemoteEntityMode(stationAiCore.Value, false); + _xformSystem.SetCoordinates(stationAiCore.Comp.RemoteEntity.Value, Transform(source).Coordinates); + _stationAiSystem.SwitchRemoteEntityMode(stationAiCore, false); return; } @@ -220,10 +220,10 @@ public sealed class HolopadSystem : SharedHolopadSystem reachableAiCores.Add((receiverUid, receiverTelephone)); - if (!_stationAiSystem.TryGetInsertedAI((receiver, receiverStationAiCore), out var insertedAi)) + if (!_stationAiSystem.TryGetHeld((receiver, receiverStationAiCore), out var insertedAi)) continue; - if (_userInterfaceSystem.TryOpenUi(receiverUid, HolopadUiKey.AiRequestWindow, insertedAi.Value.Owner)) + if (_userInterfaceSystem.TryOpenUi(receiverUid, HolopadUiKey.AiRequestWindow, insertedAi)) LinkHolopadToUser(entity, args.Actor); } @@ -274,8 +274,8 @@ public sealed class HolopadSystem : SharedHolopadSystem return; // Auto-close the AI request window - if (_stationAiSystem.TryGetInsertedAI((entity, stationAiCore), out var insertedAi)) - _userInterfaceSystem.CloseUi(entity.Owner, HolopadUiKey.AiRequestWindow, insertedAi.Value.Owner); + if (_stationAiSystem.TryGetHeld((entity, stationAiCore), out var insertedAi)) + _userInterfaceSystem.CloseUi(entity.Owner, HolopadUiKey.AiRequestWindow, insertedAi); } private void OnTelephoneMessageSent(Entity holopad, ref TelephoneMessageSentEvent args) @@ -381,13 +381,13 @@ public sealed class HolopadSystem : SharedHolopadSystem if (!TryComp(entity, out var entityStationAiHeld)) return; - if (!_stationAiSystem.TryGetStationAiCore((entity, entityStationAiHeld), out var stationAiCore)) + if (!_stationAiSystem.TryGetCore(entity, out var stationAiCore)) return; if (!TryComp(stationAiCore, out var stationAiCoreTelephone)) return; - _telephoneSystem.EndTelephoneCalls((stationAiCore.Value, stationAiCoreTelephone)); + _telephoneSystem.EndTelephoneCalls((stationAiCore, stationAiCoreTelephone)); } private void AddToggleProjectorVerb(Entity entity, ref GetVerbsEvent args) @@ -407,8 +407,8 @@ public sealed class HolopadSystem : SharedHolopadSystem if (!TryComp(user, out var userAiHeld)) return; - if (!_stationAiSystem.TryGetStationAiCore((user, userAiHeld), out var stationAiCore) || - stationAiCore.Value.Comp.RemoteEntity == null) + if (!_stationAiSystem.TryGetCore(user, out var stationAiCore) || + stationAiCore.Comp?.RemoteEntity == null) return; AlternativeVerb verb = new() @@ -595,17 +595,17 @@ public sealed class HolopadSystem : SharedHolopadSystem { // Check if the associated holopad user is an AI if (TryComp(entity.Comp.User, out var stationAiHeld) && - _stationAiSystem.TryGetStationAiCore((entity.Comp.User.Value, stationAiHeld), out var stationAiCore)) + _stationAiSystem.TryGetCore(entity.Comp.User.Value, out var stationAiCore)) { // Return the AI eye to free roaming - _stationAiSystem.SwitchRemoteEntityMode(stationAiCore.Value, true); + _stationAiSystem.SwitchRemoteEntityMode(stationAiCore, true); // If the AI core is still broadcasting, end its calls - if (entity.Owner != stationAiCore.Value.Owner && + if (entity.Owner != stationAiCore.Owner && TryComp(stationAiCore, out var stationAiCoreTelephone) && - _telephoneSystem.IsTelephoneEngaged((stationAiCore.Value.Owner, stationAiCoreTelephone))) + _telephoneSystem.IsTelephoneEngaged((stationAiCore.Owner, stationAiCoreTelephone))) { - _telephoneSystem.EndTelephoneCalls((stationAiCore.Value.Owner, stationAiCoreTelephone)); + _telephoneSystem.EndTelephoneCalls((stationAiCore.Owner, stationAiCoreTelephone)); } } @@ -625,8 +625,8 @@ public sealed class HolopadSystem : SharedHolopadSystem if (!TryComp(user, out var userAiHeld)) return; - if (!_stationAiSystem.TryGetStationAiCore((user, userAiHeld), out var stationAiCore) || - stationAiCore.Value.Comp.RemoteEntity == null) + if (!_stationAiSystem.TryGetCore(user, out var stationAiCore) || + stationAiCore.Comp?.RemoteEntity == null) return; if (!TryComp(stationAiCore, out var stationAiTelephone)) @@ -635,7 +635,7 @@ public sealed class HolopadSystem : SharedHolopadSystem if (!TryComp(stationAiCore, out var stationAiHolopad)) return; - var source = new Entity(stationAiCore.Value, stationAiTelephone); + var source = new Entity(stationAiCore, stationAiTelephone); // Check if the AI is unable to activate the projector (unlikely this will ever pass; its just a safeguard) if (!_telephoneSystem.IsSourceInRangeOfReceiver(source, receiver)) @@ -658,11 +658,11 @@ public sealed class HolopadSystem : SharedHolopadSystem if (!_telephoneSystem.IsSourceConnectedToReceiver(source, receiver)) return; - LinkHolopadToUser((stationAiCore.Value, stationAiHolopad), user); + LinkHolopadToUser((stationAiCore, stationAiHolopad), user); // Switch the AI's perspective from free roaming to the target holopad - _xformSystem.SetCoordinates(stationAiCore.Value.Comp.RemoteEntity.Value, Transform(entity).Coordinates); - _stationAiSystem.SwitchRemoteEntityMode(stationAiCore.Value, false); + _xformSystem.SetCoordinates(stationAiCore.Comp.RemoteEntity.Value, Transform(entity).Coordinates); + _stationAiSystem.SwitchRemoteEntityMode(stationAiCore, false); // Open the holopad UI if it hasn't been opened yet if (TryComp(entity, out var entityUserInterfaceComponent)) diff --git a/Content.Server/Silicons/StationAi/StationAiSystem.cs b/Content.Server/Silicons/StationAi/StationAiSystem.cs index 054712059e..baec24bf73 100644 --- a/Content.Server/Silicons/StationAi/StationAiSystem.cs +++ b/Content.Server/Silicons/StationAi/StationAiSystem.cs @@ -40,12 +40,12 @@ public sealed class StationAiSystem : SharedStationAiSystem var query = EntityManager.EntityQueryEnumerator(); while (query.MoveNext(out var ent, out var entStationAiCore, out var entXform)) { - var stationAiCore = new Entity(ent, entStationAiCore); + var stationAiCore = new Entity(ent, entStationAiCore); - if (!TryGetInsertedAI(stationAiCore, out var insertedAi) || !TryComp(insertedAi, out ActorComponent? actor)) + if (!TryGetHeld(stationAiCore, out var insertedAi) || !TryComp(insertedAi, out ActorComponent? actor)) continue; - if (stationAiCore.Comp.RemoteEntity == null || stationAiCore.Comp.Remote) + if (stationAiCore.Comp?.RemoteEntity == null || stationAiCore.Comp.Remote) continue; var xform = Transform(stationAiCore.Comp.RemoteEntity.Value); diff --git a/Content.Shared/Silicons/StationAi/SharedStationAiSystem.Held.cs b/Content.Shared/Silicons/StationAi/SharedStationAiSystem.Held.cs index c9279b0215..8acfb56376 100644 --- a/Content.Shared/Silicons/StationAi/SharedStationAiSystem.Held.cs +++ b/Content.Shared/Silicons/StationAi/SharedStationAiSystem.Held.cs @@ -56,7 +56,7 @@ public abstract partial class SharedStationAiSystem /// /// Tries to get the entity held in the AI core using StationAiCore. /// - private bool TryGetHeld(Entity entity, out EntityUid held) + public bool TryGetHeld(Entity entity, out EntityUid held) { held = EntityUid.Invalid; @@ -74,23 +74,19 @@ public abstract partial class SharedStationAiSystem /// /// Tries to get the entity held in the AI using StationAiHolder. /// - private bool TryGetHeldFromHolder(Entity entity, out EntityUid held) + public bool TryGetHeld(Entity entity, out EntityUid held) { - held = EntityUid.Invalid; - - if (!Resolve(entity.Owner, ref entity.Comp)) - return false; - - if (!_containers.TryGetContainer(entity.Owner, StationAiHolderComponent.Container, out var container) || - container.ContainedEntities.Count == 0) - return false; + TryComp(entity.Owner, out var stationAiCore); - held = container.ContainedEntities[0]; - return true; + return TryGetHeld((entity.Owner, stationAiCore), out held); } - private bool TryGetCore(EntityUid ent, out Entity core) + public bool TryGetCore(EntityUid entity, out Entity core) { + var xform = Transform(entity); + var meta = MetaData(entity); + var ent = new Entity(entity, xform, meta); + if (!_containers.TryGetContainingContainer(ent, out var container) || container.ID != StationAiCoreComponent.Container || !TryComp(container.Owner, out StationAiCoreComponent? coreComp) || diff --git a/Content.Shared/Silicons/StationAi/SharedStationAiSystem.cs b/Content.Shared/Silicons/StationAi/SharedStationAiSystem.cs index 4937e6e84c..4cf36f560e 100644 --- a/Content.Shared/Silicons/StationAi/SharedStationAiSystem.cs +++ b/Content.Shared/Silicons/StationAi/SharedStationAiSystem.cs @@ -20,7 +20,6 @@ using Content.Shared.Verbs; using Robust.Shared.Audio; using Robust.Shared.Audio.Systems; using Robust.Shared.Containers; -using Robust.Shared.GameObjects; using Robust.Shared.Map; using Robust.Shared.Map.Components; using Robust.Shared.Network; @@ -274,7 +273,7 @@ public abstract partial class SharedStationAiSystem : EntitySystem return; } - if (TryGetHeldFromHolder((args.Target.Value, targetHolder), out var held) && _timing.CurTime > intelliComp.NextWarningAllowed) + if (TryGetHeld((args.Target.Value, targetHolder), out var held) && _timing.CurTime > intelliComp.NextWarningAllowed) { intelliComp.NextWarningAllowed = _timing.CurTime + intelliComp.WarningDelay; AnnounceIntellicardUsage(held, intelliComp.WarningSound); @@ -349,11 +348,13 @@ public abstract partial class SharedStationAiSystem : EntitySystem AttachEye(ent); } - public void SwitchRemoteEntityMode(Entity ent, bool isRemote) + public void SwitchRemoteEntityMode(Entity entity, bool isRemote) { - if (isRemote == ent.Comp.Remote) + if (entity.Comp?.Remote == null || entity.Comp.Remote == isRemote) return; + var ent = new Entity(entity.Owner, entity.Comp); + ent.Comp.Remote = isRemote; EntityCoordinates? coords = ent.Comp.RemoteEntity != null ? Transform(ent.Comp.RemoteEntity.Value).Coordinates : null; @@ -530,36 +531,6 @@ public abstract partial class SharedStationAiSystem : EntitySystem return _blocker.CanComplexInteract(entity.Owner); } - - public bool TryGetStationAiCore(Entity ent, [NotNullWhen(true)] out Entity? parentEnt) - { - parentEnt = null; - var parent = Transform(ent).ParentUid; - - if (!parent.IsValid()) - return false; - - if (!TryComp(parent, out var stationAiCore)) - return false; - - parentEnt = new Entity(parent, stationAiCore); - - return true; - } - - public bool TryGetInsertedAI(Entity ent, [NotNullWhen(true)] out Entity? insertedAi) - { - insertedAi = null; - var insertedEnt = GetInsertedAI(ent); - - if (TryComp(insertedEnt, out var stationAiHeld)) - { - insertedAi = (insertedEnt.Value, stationAiHeld); - return true; - } - - return false; - } } public sealed partial class JumpToCoreEvent : InstantActionEvent -- 2.51.2