Cleanup GhostSystem (#17526)

This commit is contained in:
ShadowCommander
2023-09-15 22:21:33 -07:00
committed by GitHub
parent f7d7f5cc7f
commit 425af9e742
2 changed files with 101 additions and 89 deletions

View File

@@ -26,21 +26,22 @@ using Robust.Shared.Timing;
namespace Content.Server.Ghost namespace Content.Server.Ghost
{ {
public sealed partial class GhostSystem : SharedGhostSystem public sealed class GhostSystem : SharedGhostSystem
{ {
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly SharedEyeSystem _eye = default!;
[Dependency] private readonly FollowerSystem _followerSystem = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!; [Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly JobSystem _jobs = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly MindSystem _minds = default!;
[Dependency] private readonly SharedMindSystem _mindSystem = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!; [Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly GameTicker _ticker = default!; [Dependency] private readonly GameTicker _ticker = default!;
[Dependency] private readonly SharedMindSystem _mindSystem = default!; [Dependency] private readonly TransformSystem _transformSystem = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly VisibilitySystem _visibilitySystem = default!; [Dependency] private readonly VisibilitySystem _visibilitySystem = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly FollowerSystem _followerSystem = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly SharedEyeSystem _eye = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly MindSystem _minds = default!;
[Dependency] private readonly JobSystem _jobs = default!;
public override void Initialize() public override void Initialize()
{ {
@@ -72,10 +73,10 @@ namespace Content.Server.Ghost
if (args.Handled) if (args.Handled)
return; return;
var ents = _lookup.GetEntitiesInRange(args.Performer, component.BooRadius); var entities = _lookup.GetEntitiesInRange(args.Performer, component.BooRadius);
var booCounter = 0; var booCounter = 0;
foreach (var ent in ents) foreach (var ent in entities)
{ {
var handled = DoGhostBooEvent(ent); var handled = DoGhostBooEvent(ent);
@@ -92,7 +93,7 @@ namespace Content.Server.Ghost
private void OnRelayMoveInput(EntityUid uid, GhostOnMoveComponent component, ref MoveInputEvent args) private void OnRelayMoveInput(EntityUid uid, GhostOnMoveComponent component, ref MoveInputEvent args)
{ {
// Let's not ghost if our mind is visiting... // Let's not ghost if our mind is visiting...
if (EntityManager.HasComponent<VisitingMindComponent>(uid)) if (HasComp<VisitingMindComponent>(uid))
return; return;
if (!_minds.TryGetMind(uid, out var mindId, out var mind) || mind.IsVisitingEntity) if (!_minds.TryGetMind(uid, out var mindId, out var mind) || mind.IsVisitingEntity)
@@ -107,19 +108,16 @@ namespace Content.Server.Ghost
private void OnGhostStartup(EntityUid uid, GhostComponent component, ComponentStartup args) private void OnGhostStartup(EntityUid uid, GhostComponent component, ComponentStartup args)
{ {
// Allow this entity to be seen by other ghosts. // Allow this entity to be seen by other ghosts.
var visibility = EntityManager.EnsureComponent<VisibilityComponent>(uid); var visibility = EnsureComp<VisibilityComponent>(uid);
if (_ticker.RunLevel != GameRunLevel.PostRound) if (_ticker.RunLevel != GameRunLevel.PostRound)
{ {
_visibilitySystem.AddLayer(visibility, (int) VisibilityFlags.Ghost, false); _visibilitySystem.AddLayer(uid, visibility, (int) VisibilityFlags.Ghost, false);
_visibilitySystem.RemoveLayer(visibility, (int) VisibilityFlags.Normal, false); _visibilitySystem.RemoveLayer(uid, visibility, (int) VisibilityFlags.Normal, false);
_visibilitySystem.RefreshVisibility(visibility); _visibilitySystem.RefreshVisibility(uid, visibilityComponent: visibility);
} }
if (EntityManager.TryGetComponent(uid, out EyeComponent? eye)) SetCanSeeGhosts(uid, true);
{
_eye.SetVisibilityMask(uid, eye.VisibilityMask | (int) VisibilityFlags.Ghost, eye);
}
var time = _gameTiming.CurTime; var time = _gameTiming.CurTime;
component.TimeOfDeath = time; component.TimeOfDeath = time;
@@ -140,24 +138,32 @@ namespace Content.Server.Ghost
private void OnGhostShutdown(EntityUid uid, GhostComponent component, ComponentShutdown args) private void OnGhostShutdown(EntityUid uid, GhostComponent component, ComponentShutdown args)
{ {
// Perf: If the entity is deleting itself, no reason to change these back. // Perf: If the entity is deleting itself, no reason to change these back.
if (!Terminating(uid)) if (Terminating(uid))
{ return;
// Entity can't be seen by ghosts anymore. // Entity can't be seen by ghosts anymore.
if (EntityManager.TryGetComponent(uid, out VisibilityComponent? visibility)) if (TryComp(uid, out VisibilityComponent? visibility))
{ {
_visibilitySystem.RemoveLayer(visibility, (int) VisibilityFlags.Ghost, false); _visibilitySystem.RemoveLayer(uid, visibility, (int) VisibilityFlags.Ghost, false);
_visibilitySystem.AddLayer(visibility, (int) VisibilityFlags.Normal, false); _visibilitySystem.AddLayer(uid, visibility, (int) VisibilityFlags.Normal, false);
_visibilitySystem.RefreshVisibility(visibility); _visibilitySystem.RefreshVisibility(uid, visibilityComponent: visibility);
} }
// Entity can't see ghosts anymore. // Entity can't see ghosts anymore.
if (EntityManager.TryGetComponent(uid, out EyeComponent? eye)) SetCanSeeGhosts(uid, false);
{
_eye.SetVisibilityMask(uid, eye.VisibilityMask & ~(int) VisibilityFlags.Ghost, eye);
}
_actions.RemoveAction(uid, component.ActionEntity); _actions.RemoveAction(uid, component.ActionEntity);
} }
private void SetCanSeeGhosts(EntityUid uid, bool canSee, EyeComponent? eyeComponent = null)
{
if (!Resolve(uid, ref eyeComponent, false))
return;
if (canSee)
_eye.SetVisibilityMask(uid, eyeComponent.VisibilityMask | (int) VisibilityFlags.Ghost, eyeComponent);
else
_eye.SetVisibilityMask(uid, eyeComponent.VisibilityMask & ~(int) VisibilityFlags.Ghost, eyeComponent);
} }
private void OnGhostExamine(EntityUid uid, GhostComponent component, ExaminedEvent args) private void OnGhostExamine(EntityUid uid, GhostComponent component, ExaminedEvent args)
@@ -170,6 +176,8 @@ namespace Content.Server.Ghost
args.PushMarkup(deathTimeInfo); args.PushMarkup(deathTimeInfo);
} }
#region Ghost Deletion
private void OnMindRemovedMessage(EntityUid uid, GhostComponent component, MindRemovedMessage args) private void OnMindRemovedMessage(EntityUid uid, GhostComponent component, MindRemovedMessage args)
{ {
DeleteEntity(uid); DeleteEntity(uid);
@@ -185,10 +193,36 @@ namespace Content.Server.Ghost
DeleteEntity(uid); DeleteEntity(uid);
} }
private void DeleteEntity(EntityUid uid)
{
if (Deleted(uid) || Terminating(uid))
return;
QueueDel(uid);
}
#endregion
private void OnGhostReturnToBodyRequest(GhostReturnToBodyRequest msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not {Valid: true} attached
|| !TryComp(attached, out GhostComponent? ghost)
|| !ghost.CanReturnToBody
|| !TryComp(attached, out ActorComponent? actor))
{
Log.Warning($"User {args.SenderSession.Name} sent an invalid {nameof(GhostReturnToBodyRequest)}");
return;
}
_mindSystem.UnVisit(actor.PlayerSession);
}
#region Warp
private void OnGhostWarpsRequest(GhostWarpsRequestEvent msg, EntitySessionEventArgs args) private void OnGhostWarpsRequest(GhostWarpsRequestEvent msg, EntitySessionEventArgs args)
{ {
if (args.SenderSession.AttachedEntity is not {Valid: true} entity || if (args.SenderSession.AttachedEntity is not {Valid: true} entity
!EntityManager.HasComponent<GhostComponent>(entity)) || !HasComp<GhostComponent>(entity))
{ {
Log.Warning($"User {args.SenderSession.Name} sent a {nameof(GhostWarpsRequestEvent)} without being a ghost."); Log.Warning($"User {args.SenderSession.Name} sent a {nameof(GhostWarpsRequestEvent)} without being a ghost.");
return; return;
@@ -198,24 +232,10 @@ namespace Content.Server.Ghost
RaiseNetworkEvent(response, args.SenderSession.ConnectedClient); RaiseNetworkEvent(response, args.SenderSession.ConnectedClient);
} }
private void OnGhostReturnToBodyRequest(GhostReturnToBodyRequest msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not {Valid: true} attached ||
!EntityManager.TryGetComponent(attached, out GhostComponent? ghost) ||
!ghost.CanReturnToBody ||
!EntityManager.TryGetComponent(attached, out ActorComponent? actor))
{
Log.Warning($"User {args.SenderSession.Name} sent an invalid {nameof(GhostReturnToBodyRequest)}");
return;
}
_mindSystem.UnVisit(actor.PlayerSession);
}
private void OnGhostWarpToTargetRequest(GhostWarpToTargetRequestEvent msg, EntitySessionEventArgs args) private void OnGhostWarpToTargetRequest(GhostWarpToTargetRequestEvent msg, EntitySessionEventArgs args)
{ {
if (args.SenderSession.AttachedEntity is not {Valid: true} attached || if (args.SenderSession.AttachedEntity is not {Valid: true} attached
!EntityManager.TryGetComponent(attached, out GhostComponent? ghost)) || !TryComp(attached, out GhostComponent? _))
{ {
Log.Warning($"User {args.SenderSession.Name} tried to warp to {msg.Target} without being a ghost."); Log.Warning($"User {args.SenderSession.Name} tried to warp to {msg.Target} without being a ghost.");
return; return;
@@ -223,34 +243,25 @@ namespace Content.Server.Ghost
var target = GetEntity(msg.Target); var target = GetEntity(msg.Target);
if (!EntityManager.EntityExists(target)) if (!Exists(target))
{ {
Log.Warning($"User {args.SenderSession.Name} tried to warp to an invalid entity id: {msg.Target}"); Log.Warning($"User {args.SenderSession.Name} tried to warp to an invalid entity id: {msg.Target}");
return; return;
} }
if (TryComp(target, out WarpPointComponent? warp) && warp.Follow if ((TryComp(target, out WarpPointComponent? warp) && warp.Follow) || HasComp<MobStateComponent>(target))
|| HasComp<MobStateComponent>(target))
{ {
_followerSystem.StartFollowingEntity(attached, target); _followerSystem.StartFollowingEntity(attached, target);
return; return;
} }
var xform = Transform(attached); var xform = Transform(attached);
xform.Coordinates = Transform(target).Coordinates; _transformSystem.SetCoordinates(attached, xform, Transform(target).Coordinates);
xform.AttachToGridOrMap(); _transformSystem.AttachToGridOrMap(attached, xform);
if (TryComp(attached, out PhysicsComponent? physics)) if (TryComp(attached, out PhysicsComponent? physics))
_physics.SetLinearVelocity(attached, Vector2.Zero, body: physics); _physics.SetLinearVelocity(attached, Vector2.Zero, body: physics);
} }
private void DeleteEntity(EntityUid uid)
{
if (Deleted(uid) || Terminating(uid))
return;
QueueDel(uid);
}
private IEnumerable<GhostWarp> GetLocationWarps() private IEnumerable<GhostWarp> GetLocationWarps()
{ {
var allQuery = AllEntityQuery<WarpPointComponent>(); var allQuery = AllEntityQuery<WarpPointComponent>();
@@ -258,30 +269,30 @@ namespace Content.Server.Ghost
while (allQuery.MoveNext(out var uid, out var warp)) while (allQuery.MoveNext(out var uid, out var warp))
{ {
if (warp.Location != null) if (warp.Location != null)
{
yield return new GhostWarp(GetNetEntity(uid), warp.Location, true); yield return new GhostWarp(GetNetEntity(uid), warp.Location, true);
} }
} }
}
private IEnumerable<GhostWarp> GetPlayerWarps(EntityUid except) private IEnumerable<GhostWarp> GetPlayerWarps(EntityUid except)
{ {
foreach (var player in _playerManager.Sessions) foreach (var player in _playerManager.Sessions)
{ {
if (player.AttachedEntity is {Valid: true} attached) if (player.AttachedEntity is not {Valid: true} attached)
{ continue;
if (attached == except) continue; if (attached == except) continue;
TryComp<MindContainerComponent>(attached, out var mind); TryComp<MindContainerComponent>(attached, out var mind);
var jobName = _jobs.MindTryGetJobName(mind?.Mind); var jobName = _jobs.MindTryGetJobName(mind?.Mind);
var playerInfo = $"{EntityManager.GetComponent<MetaDataComponent>(attached).EntityName} ({jobName})"; var playerInfo = $"{Comp<MetaDataComponent>(attached).EntityName} ({jobName})";
if (_mobState.IsAlive(attached) || _mobState.IsCritical(attached)) if (_mobState.IsAlive(attached) || _mobState.IsCritical(attached))
yield return new GhostWarp(GetNetEntity(attached), playerInfo, false); yield return new GhostWarp(GetNetEntity(attached), playerInfo, false);
} }
} }
}
#endregion
private void OnEntityStorageInsertAttempt(EntityUid uid, GhostComponent comp, ref InsertIntoEntityStorageAttemptEvent args) private void OnEntityStorageInsertAttempt(EntityUid uid, GhostComponent comp, ref InsertIntoEntityStorageAttemptEvent args)
{ {
@@ -293,19 +304,20 @@ namespace Content.Server.Ghost
/// </summary> /// </summary>
public void MakeVisible(bool visible) public void MakeVisible(bool visible)
{ {
foreach (var (_, vis) in EntityQuery<GhostComponent, VisibilityComponent>()) var entityQuery = EntityQueryEnumerator<GhostComponent, VisibilityComponent>();
while (entityQuery.MoveNext(out var uid, out _, out var vis))
{ {
if (visible) if (visible)
{ {
_visibilitySystem.AddLayer(vis, (int) VisibilityFlags.Normal, false); _visibilitySystem.AddLayer(uid, vis, (int) VisibilityFlags.Normal, false);
_visibilitySystem.RemoveLayer(vis, (int) VisibilityFlags.Ghost, false); _visibilitySystem.RemoveLayer(uid, vis, (int) VisibilityFlags.Ghost, false);
} }
else else
{ {
_visibilitySystem.AddLayer(vis, (int) VisibilityFlags.Ghost, false); _visibilitySystem.AddLayer(uid, vis, (int) VisibilityFlags.Ghost, false);
_visibilitySystem.RemoveLayer(vis, (int) VisibilityFlags.Normal, false); _visibilitySystem.RemoveLayer(uid, vis, (int) VisibilityFlags.Normal, false);
} }
_visibilitySystem.RefreshVisibility(vis); _visibilitySystem.RefreshVisibility(uid, visibilityComponent: vis);
} }
} }
@@ -324,6 +336,7 @@ namespace Content.Server.Ghost
public string Command => "toggleghosts"; public string Command => "toggleghosts";
public string Description => "Toggles ghost visibility"; public string Description => "Toggles ghost visibility";
public string Help => $"{Command}"; public string Help => $"{Command}";
public void Execute(IConsoleShell shell, string argStr, string[] args) public void Execute(IConsoleShell shell, string argStr, string[] args)
{ {
if (shell.Player == null) if (shell.Player == null)
@@ -335,9 +348,7 @@ namespace Content.Server.Ghost
if (uid == null if (uid == null
|| !entityManager.HasComponent<GhostComponent>(uid) || !entityManager.HasComponent<GhostComponent>(uid)
|| !entityManager.TryGetComponent<EyeComponent>(uid, out var eyeComponent)) || !entityManager.TryGetComponent<EyeComponent>(uid, out var eyeComponent))
{
return; return;
}
entityManager.System<EyeSystem>().SetVisibilityMask(uid.Value, eyeComponent.VisibilityMask ^ (int) VisibilityFlags.Ghost, eyeComponent); entityManager.System<EyeSystem>().SetVisibilityMask(uid.Value, eyeComponent.VisibilityMask ^ (int) VisibilityFlags.Ghost, eyeComponent);
} }

View File

@@ -675,6 +675,7 @@ public sealed class $CLASS$ : Shared$CLASS$ {
<s:Boolean x:Key="/Default/UserDictionary/Words/=Thonk/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Thonk/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=threadsafe/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=threadsafe/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=tickrate/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=tickrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=toggleghosts/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Toolshed/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Toolshed/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Toolshed_0027s/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Toolshed_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Trasen/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Trasen/@EntryIndexedValue">True</s:Boolean>