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
{
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 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 GameTicker _ticker = default!;
[Dependency] private readonly SharedMindSystem _mindSystem = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly TransformSystem _transformSystem = 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()
{
@@ -72,10 +73,10 @@ namespace Content.Server.Ghost
if (args.Handled)
return;
var ents = _lookup.GetEntitiesInRange(args.Performer, component.BooRadius);
var entities = _lookup.GetEntitiesInRange(args.Performer, component.BooRadius);
var booCounter = 0;
foreach (var ent in ents)
foreach (var ent in entities)
{
var handled = DoGhostBooEvent(ent);
@@ -92,7 +93,7 @@ namespace Content.Server.Ghost
private void OnRelayMoveInput(EntityUid uid, GhostOnMoveComponent component, ref MoveInputEvent args)
{
// Let's not ghost if our mind is visiting...
if (EntityManager.HasComponent<VisitingMindComponent>(uid))
if (HasComp<VisitingMindComponent>(uid))
return;
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)
{
// 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)
{
_visibilitySystem.AddLayer(visibility, (int) VisibilityFlags.Ghost, false);
_visibilitySystem.RemoveLayer(visibility, (int) VisibilityFlags.Normal, false);
_visibilitySystem.RefreshVisibility(visibility);
_visibilitySystem.AddLayer(uid, visibility, (int) VisibilityFlags.Ghost, false);
_visibilitySystem.RemoveLayer(uid, visibility, (int) VisibilityFlags.Normal, false);
_visibilitySystem.RefreshVisibility(uid, visibilityComponent: visibility);
}
if (EntityManager.TryGetComponent(uid, out EyeComponent? eye))
{
_eye.SetVisibilityMask(uid, eye.VisibilityMask | (int) VisibilityFlags.Ghost, eye);
}
SetCanSeeGhosts(uid, true);
var time = _gameTiming.CurTime;
component.TimeOfDeath = time;
@@ -140,24 +138,32 @@ namespace Content.Server.Ghost
private void OnGhostShutdown(EntityUid uid, GhostComponent component, ComponentShutdown args)
{
// 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.
if (TryComp(uid, out VisibilityComponent? visibility))
{
// Entity can't be seen by ghosts anymore.
if (EntityManager.TryGetComponent(uid, out VisibilityComponent? visibility))
{
_visibilitySystem.RemoveLayer(visibility, (int) VisibilityFlags.Ghost, false);
_visibilitySystem.AddLayer(visibility, (int) VisibilityFlags.Normal, false);
_visibilitySystem.RefreshVisibility(visibility);
}
// Entity can't see ghosts anymore.
if (EntityManager.TryGetComponent(uid, out EyeComponent? eye))
{
_eye.SetVisibilityMask(uid, eye.VisibilityMask & ~(int) VisibilityFlags.Ghost, eye);
}
_actions.RemoveAction(uid, component.ActionEntity);
_visibilitySystem.RemoveLayer(uid, visibility, (int) VisibilityFlags.Ghost, false);
_visibilitySystem.AddLayer(uid, visibility, (int) VisibilityFlags.Normal, false);
_visibilitySystem.RefreshVisibility(uid, visibilityComponent: visibility);
}
// Entity can't see ghosts anymore.
SetCanSeeGhosts(uid, false);
_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)
@@ -170,6 +176,8 @@ namespace Content.Server.Ghost
args.PushMarkup(deathTimeInfo);
}
#region Ghost Deletion
private void OnMindRemovedMessage(EntityUid uid, GhostComponent component, MindRemovedMessage args)
{
DeleteEntity(uid);
@@ -185,10 +193,36 @@ namespace Content.Server.Ghost
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)
{
if (args.SenderSession.AttachedEntity is not {Valid: true} entity ||
!EntityManager.HasComponent<GhostComponent>(entity))
if (args.SenderSession.AttachedEntity is not {Valid: true} entity
|| !HasComp<GhostComponent>(entity))
{
Log.Warning($"User {args.SenderSession.Name} sent a {nameof(GhostWarpsRequestEvent)} without being a ghost.");
return;
@@ -198,24 +232,10 @@ namespace Content.Server.Ghost
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)
{
if (args.SenderSession.AttachedEntity is not {Valid: true} attached ||
!EntityManager.TryGetComponent(attached, out GhostComponent? ghost))
if (args.SenderSession.AttachedEntity is not {Valid: true} attached
|| !TryComp(attached, out GhostComponent? _))
{
Log.Warning($"User {args.SenderSession.Name} tried to warp to {msg.Target} without being a ghost.");
return;
@@ -223,34 +243,25 @@ namespace Content.Server.Ghost
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}");
return;
}
if (TryComp(target, out WarpPointComponent? warp) && warp.Follow
|| HasComp<MobStateComponent>(target))
if ((TryComp(target, out WarpPointComponent? warp) && warp.Follow) || HasComp<MobStateComponent>(target))
{
_followerSystem.StartFollowingEntity(attached, target);
return;
_followerSystem.StartFollowingEntity(attached, target);
return;
}
var xform = Transform(attached);
xform.Coordinates = Transform(target).Coordinates;
xform.AttachToGridOrMap();
_transformSystem.SetCoordinates(attached, xform, Transform(target).Coordinates);
_transformSystem.AttachToGridOrMap(attached, xform);
if (TryComp(attached, out PhysicsComponent? 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()
{
var allQuery = AllEntityQuery<WarpPointComponent>();
@@ -258,9 +269,7 @@ namespace Content.Server.Ghost
while (allQuery.MoveNext(out var uid, out var warp))
{
if (warp.Location != null)
{
yield return new GhostWarp(GetNetEntity(uid), warp.Location, true);
}
}
}
@@ -268,21 +277,23 @@ namespace Content.Server.Ghost
{
foreach (var player in _playerManager.Sessions)
{
if (player.AttachedEntity is {Valid: true} attached)
{
if (attached == except) continue;
if (player.AttachedEntity is not {Valid: true} attached)
continue;
TryComp<MindContainerComponent>(attached, out var mind);
if (attached == except) continue;
var jobName = _jobs.MindTryGetJobName(mind?.Mind);
var playerInfo = $"{EntityManager.GetComponent<MetaDataComponent>(attached).EntityName} ({jobName})";
TryComp<MindContainerComponent>(attached, out var mind);
if (_mobState.IsAlive(attached) || _mobState.IsCritical(attached))
yield return new GhostWarp(GetNetEntity(attached), playerInfo, false);
}
var jobName = _jobs.MindTryGetJobName(mind?.Mind);
var playerInfo = $"{Comp<MetaDataComponent>(attached).EntityName} ({jobName})";
if (_mobState.IsAlive(attached) || _mobState.IsCritical(attached))
yield return new GhostWarp(GetNetEntity(attached), playerInfo, false);
}
}
#endregion
private void OnEntityStorageInsertAttempt(EntityUid uid, GhostComponent comp, ref InsertIntoEntityStorageAttemptEvent args)
{
args.Cancelled = true;
@@ -293,19 +304,20 @@ namespace Content.Server.Ghost
/// </summary>
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)
{
_visibilitySystem.AddLayer(vis, (int) VisibilityFlags.Normal, false);
_visibilitySystem.RemoveLayer(vis, (int) VisibilityFlags.Ghost, false);
_visibilitySystem.AddLayer(uid, vis, (int) VisibilityFlags.Normal, false);
_visibilitySystem.RemoveLayer(uid, vis, (int) VisibilityFlags.Ghost, false);
}
else
{
_visibilitySystem.AddLayer(vis, (int) VisibilityFlags.Ghost, false);
_visibilitySystem.RemoveLayer(vis, (int) VisibilityFlags.Normal, false);
_visibilitySystem.AddLayer(uid, vis, (int) VisibilityFlags.Ghost, 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 Description => "Toggles ghost visibility";
public string Help => $"{Command}";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (shell.Player == null)
@@ -335,9 +348,7 @@ namespace Content.Server.Ghost
if (uid == null
|| !entityManager.HasComponent<GhostComponent>(uid)
|| !entityManager.TryGetComponent<EyeComponent>(uid, out var eyeComponent))
{
return;
}
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/=threadsafe/@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_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Trasen/@EntryIndexedValue">True</s:Boolean>