Minor station AI code cleanup (#34476)
This commit is contained in:
@@ -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<TelephoneComponent>(stationAiCore, out var stationAiTelephone) &&
|
||||
TryComp<TelephoneComponent>(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<StationAiHeldComponent>(args.Actor, out var stationAiHeld) ||
|
||||
!_stationAiSystem.TryGetStationAiCore((args.Actor, stationAiHeld), out var stationAiCore))
|
||||
!_stationAiSystem.TryGetCore(args.Actor, out var stationAiCore))
|
||||
return;
|
||||
|
||||
if (TryComp<TelephoneComponent>(stationAiCore, out var telephone))
|
||||
_telephoneSystem.EndTelephoneCalls((stationAiCore.Value, telephone));
|
||||
_telephoneSystem.EndTelephoneCalls((stationAiCore, telephone));
|
||||
}
|
||||
|
||||
private void OnHolopadActivateProjector(Entity<HolopadComponent> 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<HolopadComponent>(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<HolopadComponent> holopad, ref TelephoneMessageSentEvent args)
|
||||
@@ -381,13 +381,13 @@ public sealed class HolopadSystem : SharedHolopadSystem
|
||||
if (!TryComp<StationAiHeldComponent>(entity, out var entityStationAiHeld))
|
||||
return;
|
||||
|
||||
if (!_stationAiSystem.TryGetStationAiCore((entity, entityStationAiHeld), out var stationAiCore))
|
||||
if (!_stationAiSystem.TryGetCore(entity, out var stationAiCore))
|
||||
return;
|
||||
|
||||
if (!TryComp<TelephoneComponent>(stationAiCore, out var stationAiCoreTelephone))
|
||||
return;
|
||||
|
||||
_telephoneSystem.EndTelephoneCalls((stationAiCore.Value, stationAiCoreTelephone));
|
||||
_telephoneSystem.EndTelephoneCalls((stationAiCore, stationAiCoreTelephone));
|
||||
}
|
||||
|
||||
private void AddToggleProjectorVerb(Entity<HolopadComponent> entity, ref GetVerbsEvent<AlternativeVerb> args)
|
||||
@@ -407,8 +407,8 @@ public sealed class HolopadSystem : SharedHolopadSystem
|
||||
if (!TryComp<StationAiHeldComponent>(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<StationAiHeldComponent>(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<TelephoneComponent>(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<StationAiHeldComponent>(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<TelephoneComponent>(stationAiCore, out var stationAiTelephone))
|
||||
@@ -635,7 +635,7 @@ public sealed class HolopadSystem : SharedHolopadSystem
|
||||
if (!TryComp<HolopadComponent>(stationAiCore, out var stationAiHolopad))
|
||||
return;
|
||||
|
||||
var source = new Entity<TelephoneComponent>(stationAiCore.Value, stationAiTelephone);
|
||||
var source = new Entity<TelephoneComponent>(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<UserInterfaceComponent>(entity, out var entityUserInterfaceComponent))
|
||||
|
||||
@@ -40,12 +40,12 @@ public sealed class StationAiSystem : SharedStationAiSystem
|
||||
var query = EntityManager.EntityQueryEnumerator<StationAiCoreComponent, TransformComponent>();
|
||||
while (query.MoveNext(out var ent, out var entStationAiCore, out var entXform))
|
||||
{
|
||||
var stationAiCore = new Entity<StationAiCoreComponent>(ent, entStationAiCore);
|
||||
var stationAiCore = new Entity<StationAiCoreComponent?>(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);
|
||||
|
||||
@@ -56,7 +56,7 @@ public abstract partial class SharedStationAiSystem
|
||||
/// <summary>
|
||||
/// Tries to get the entity held in the AI core using StationAiCore.
|
||||
/// </summary>
|
||||
private bool TryGetHeld(Entity<StationAiCoreComponent?> entity, out EntityUid held)
|
||||
public bool TryGetHeld(Entity<StationAiCoreComponent?> entity, out EntityUid held)
|
||||
{
|
||||
held = EntityUid.Invalid;
|
||||
|
||||
@@ -74,23 +74,19 @@ public abstract partial class SharedStationAiSystem
|
||||
/// <summary>
|
||||
/// Tries to get the entity held in the AI using StationAiHolder.
|
||||
/// </summary>
|
||||
private bool TryGetHeldFromHolder(Entity<StationAiHolderComponent?> entity, out EntityUid held)
|
||||
public bool TryGetHeld(Entity<StationAiHolderComponent?> entity, out EntityUid held)
|
||||
{
|
||||
held = EntityUid.Invalid;
|
||||
TryComp<StationAiCoreComponent>(entity.Owner, out var stationAiCore);
|
||||
|
||||
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;
|
||||
|
||||
held = container.ContainedEntities[0];
|
||||
return true;
|
||||
return TryGetHeld((entity.Owner, stationAiCore), out held);
|
||||
}
|
||||
|
||||
private bool TryGetCore(EntityUid ent, out Entity<StationAiCoreComponent?> core)
|
||||
public bool TryGetCore(EntityUid entity, out Entity<StationAiCoreComponent?> core)
|
||||
{
|
||||
var xform = Transform(entity);
|
||||
var meta = MetaData(entity);
|
||||
var ent = new Entity<TransformComponent?, MetaDataComponent?>(entity, xform, meta);
|
||||
|
||||
if (!_containers.TryGetContainingContainer(ent, out var container) ||
|
||||
container.ID != StationAiCoreComponent.Container ||
|
||||
!TryComp(container.Owner, out StationAiCoreComponent? coreComp) ||
|
||||
|
||||
@@ -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<StationAiCoreComponent> ent, bool isRemote)
|
||||
public void SwitchRemoteEntityMode(Entity<StationAiCoreComponent?> entity, bool isRemote)
|
||||
{
|
||||
if (isRemote == ent.Comp.Remote)
|
||||
if (entity.Comp?.Remote == null || entity.Comp.Remote == isRemote)
|
||||
return;
|
||||
|
||||
var ent = new Entity<StationAiCoreComponent>(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<StationAiHeldComponent?> ent, [NotNullWhen(true)] out Entity<StationAiCoreComponent>? parentEnt)
|
||||
{
|
||||
parentEnt = null;
|
||||
var parent = Transform(ent).ParentUid;
|
||||
|
||||
if (!parent.IsValid())
|
||||
return false;
|
||||
|
||||
if (!TryComp<StationAiCoreComponent>(parent, out var stationAiCore))
|
||||
return false;
|
||||
|
||||
parentEnt = new Entity<StationAiCoreComponent>(parent, stationAiCore);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetInsertedAI(Entity<StationAiCoreComponent> ent, [NotNullWhen(true)] out Entity<StationAiHeldComponent>? insertedAi)
|
||||
{
|
||||
insertedAi = null;
|
||||
var insertedEnt = GetInsertedAI(ent);
|
||||
|
||||
if (TryComp<StationAiHeldComponent>(insertedEnt, out var stationAiHeld))
|
||||
{
|
||||
insertedAi = (insertedEnt.Value, stationAiHeld);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed partial class JumpToCoreEvent : InstantActionEvent
|
||||
|
||||
Reference in New Issue
Block a user