Make ghost component ECS (#4159)

* Make ghost component ECS

* Remove players and locations properties from ghost component

* Address reviews

* One more doc

* Fix client ghost component state method error
This commit is contained in:
DrSmugleaf
2021-06-18 09:56:23 +02:00
committed by GitHub
parent 86ff812428
commit 4f4d203d2e
10 changed files with 481 additions and 401 deletions

View File

@@ -1,187 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.Players;
using Content.Server.Visible;
using Content.Server.Warps;
using Content.Shared.Examine;
using Content.Shared.Ghost;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Network;
using Robust.Shared.Players;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
#nullable enable
namespace Content.Server.Ghost.Components
{
[RegisterComponent]
public class GhostComponent : SharedGhostComponent, IExamine
public class GhostComponent : SharedGhostComponent
{
private bool _canReturnToBody = true;
private TimeSpan _timeOfDeath = TimeSpan.Zero;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IGameTiming _gameTimer = default!;
[ViewVariables(VVAccess.ReadWrite)]
public bool CanReturnToBody
{
get => _canReturnToBody;
set
{
_canReturnToBody = value;
Dirty();
}
}
/// <inheritdoc />
protected override void Startup()
{
base.Startup();
// Allow this entity to be seen by other ghosts.
Owner.EnsureComponent<VisibilityComponent>().Layer |= (int) VisibilityFlags.Ghost;
// Allows this entity to see other ghosts.
Owner.EnsureComponent<EyeComponent>().VisibilityMask |= (uint) VisibilityFlags.Ghost;
_timeOfDeath = _gameTimer.RealTime;
}
/// <inheritdoc />
protected override void Shutdown()
{
//Perf: If the entity is deleting itself, no reason to change these back.
if(Owner.LifeStage < EntityLifeStage.Terminating)
{
// Entity can't be seen by ghosts anymore.
if (Owner.TryGetComponent<VisibilityComponent>(out var visComp))
visComp.Layer &= ~(int) VisibilityFlags.Ghost;
// Entity can't see ghosts anymore.
if (Owner.TryGetComponent<EyeComponent>(out var eyeComp))
eyeComp.VisibilityMask &= ~(uint) VisibilityFlags.Ghost;
}
base.Shutdown();
}
public override ComponentState GetComponentState(ICommonSession player) => new GhostComponentState(CanReturnToBody);
public override void HandleNetworkMessage(ComponentMessage message, INetChannel netChannel, ICommonSession? session = null!)
{
base.HandleNetworkMessage(message, netChannel, session);
switch (message)
{
case ReturnToBodyComponentMessage:
{
if (!Owner.TryGetComponent(out ActorComponent? actor) ||
!CanReturnToBody)
{
break;
}
if (netChannel == actor.PlayerSession.ConnectedClient)
{
var o = actor.PlayerSession.ContentData()!.Mind;
o?.UnVisit();
}
break;
}
case GhostWarpToLocationRequestMessage warp:
{
if (session?.AttachedEntity != Owner)
{
break;
}
foreach (var warpPoint in FindWaypoints())
{
if (warp.Name == warpPoint.Location)
{
Owner.Transform.Coordinates = warpPoint.Owner.Transform.Coordinates;
break;
}
}
Logger.Warning($"User {session.Name} tried to warp to an invalid warp: {warp.Name}");
break;
}
case GhostWarpToTargetRequestMessage target:
{
if (session?.AttachedEntity != Owner)
{
break;
}
if (!Owner.TryGetComponent(out ActorComponent? actor))
{
break;
}
if (!Owner.EntityManager.TryGetEntity(target.Target, out var entity))
{
Logger.Warning($"User {session.Name} tried to warp to an invalid entity id: {target.Target}");
break;
}
if (!_playerManager.TryGetSessionByChannel(actor.PlayerSession.ConnectedClient, out var player) ||
player.AttachedEntity != entity)
{
break;
}
Owner.Transform.Coordinates = entity.Transform.Coordinates;
break;
}
case GhostRequestPlayerNameData _:
var playerNames = new Dictionary<EntityUid, string>();
foreach (var names in _playerManager.GetAllPlayers())
{
if (names.AttachedEntity != null && names.UserId != netChannel.UserId)
{
playerNames.Add(names.AttachedEntity.Uid,names.AttachedEntity.Name);
}
}
SendNetworkMessage(new GhostReplyPlayerNameData(playerNames));
break;
case GhostRequestWarpPointData _:
var warpPoints = FindWaypoints();
var warpName = new List<string>();
foreach (var point in warpPoints)
{
if (point.Location == null)
{
continue;
}
warpName.Add(point.Location);
}
SendNetworkMessage(new GhostReplyWarpPointData(warpName));
break;
}
}
private List<WarpPointComponent> FindWaypoints()
{
var comp = IoCManager.Resolve<IComponentManager>();
return comp.EntityQuery<WarpPointComponent>(true).ToList();
}
public void Examine(FormattedMessage message, bool inDetailsRange)
{
var timeSinceDeath = _gameTimer.RealTime.Subtract(_timeOfDeath);
//If we've been dead for longer than 1 minute use minutes, otherwise use seconds. Ignore the improper plurals.
var deathTimeInfo = timeSinceDeath.Minutes > 0 ? Loc.GetString($"{timeSinceDeath.Minutes} minutes ago") : Loc.GetString($"{timeSinceDeath.Seconds} seconds ago");
message.AddMarkup(Loc.GetString("Died [color=yellow]{0}[/color].", deathTimeInfo));
}
public TimeSpan TimeOfDeath { get; set; } = TimeSpan.Zero;
}
}

View File

@@ -1,19 +1,93 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Ghost.Components;
using Content.Server.Mind.Components;
using Content.Server.Players;
using Content.Server.Visible;
using Content.Server.Warps;
using Content.Shared.Examine;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Ghost;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Timing;
namespace Content.Server.Ghost
{
[UsedImplicitly]
public class GhostSystem : EntitySystem
public class GhostSystem : SharedGhostSystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GhostComponent, ComponentStartup>(OnGhostStartup);
SubscribeLocalEvent<GhostComponent, ComponentShutdown>(OnGhostShutdown);
SubscribeLocalEvent<GhostComponent, ExaminedEvent>(OnGhostExamine);
SubscribeLocalEvent<GhostComponent, MindRemovedMessage>(OnMindRemovedMessage);
SubscribeLocalEvent<GhostComponent, MindUnvisitedMessage>(OnMindUnvisitedMessage);
SubscribeNetworkEvent<GhostWarpsRequestEvent>(OnGhostWarpsRequest);
SubscribeNetworkEvent<GhostReturnToBodyRequest>(OnGhostReturnToBodyRequest);
SubscribeNetworkEvent<GhostWarpToLocationRequestEvent>(OnGhostWarpToLocationRequest);
SubscribeNetworkEvent<GhostWarpToTargetRequestEvent>(OnGhostWarpToTargetRequest);
}
private void OnGhostStartup(EntityUid uid, GhostComponent component, ComponentStartup args)
{
// Allow this entity to be seen by other ghosts.
if (component.Owner.TryGetComponent(out VisibilityComponent? visibility))
{
visibility.Layer |= (int) VisibilityFlags.Ghost;
visibility.Layer &= ~(int) VisibilityFlags.Normal;
}
if (component.Owner.TryGetComponent(out EyeComponent? eye))
{
eye.VisibilityMask |= (uint) VisibilityFlags.Ghost;
}
component.TimeOfDeath = _gameTiming.RealTime;
}
private void OnGhostShutdown(EntityUid uid, GhostComponent component, ComponentShutdown args)
{
// Perf: If the entity is deleting itself, no reason to change these back.
if (component.Owner.LifeStage < EntityLifeStage.Terminating)
{
// Entity can't be seen by ghosts anymore.
if (component.Owner.TryGetComponent(out VisibilityComponent? visibility))
{
visibility.Layer &= ~(int) VisibilityFlags.Ghost;
visibility.Layer |= (int) VisibilityFlags.Normal;
}
// Entity can't see ghosts anymore.
if (component.Owner.TryGetComponent(out EyeComponent? eye))
{
eye.VisibilityMask &= ~(uint) VisibilityFlags.Ghost;
}
}
}
private void OnGhostExamine(EntityUid uid, GhostComponent component, ExaminedEvent args)
{
var timeSinceDeath = _gameTiming.RealTime.Subtract(component.TimeOfDeath);
var deathTimeInfo = timeSinceDeath.Minutes > 0
? Loc.GetString("comp-ghost-examine-time-minutes", ("minutes", timeSinceDeath.Minutes))
: Loc.GetString("comp-ghost-examine-time-seconds", ("seconds", timeSinceDeath.Seconds));
args.Message.AddMarkup(deathTimeInfo);
}
private void OnMindRemovedMessage(EntityUid uid, GhostComponent component, MindRemovedMessage args)
@@ -26,10 +100,76 @@ namespace Content.Server.Ghost
DeleteEntity(uid);
}
private void OnGhostWarpsRequest(GhostWarpsRequestEvent msg, EntitySessionEventArgs args)
{
var entity = args.SenderSession.AttachedEntity;
if (entity == null ||
!entity.HasComponent<GhostComponent>())
{
Logger.Warning($"User {args.SenderSession.Name} sent a {nameof(GhostWarpsRequestEvent)} without being a ghost.");
return;
}
var response = new GhostWarpsResponseEvent(GetLocationNames().ToList(), GetPlayerWarps(entity.Uid));
RaiseNetworkEvent(response, args.SenderSession.ConnectedClient);
}
private void OnGhostReturnToBodyRequest(GhostReturnToBodyRequest msg, EntitySessionEventArgs args)
{
var entity = args.SenderSession.AttachedEntity;
if (entity == null ||
!entity.TryGetComponent(out GhostComponent? ghost) ||
!ghost.CanReturnToBody ||
!entity.TryGetComponent(out ActorComponent? actor))
{
Logger.Warning($"User {args.SenderSession.Name} sent an invalid {nameof(GhostReturnToBodyRequest)}");
return;
}
actor.PlayerSession.ContentData()!.Mind?.UnVisit();
}
private void OnGhostWarpToLocationRequest(GhostWarpToLocationRequestEvent msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity == null ||
!args.SenderSession.AttachedEntity.TryGetComponent(out GhostComponent? ghost))
{
Logger.Warning($"User {args.SenderSession.Name} tried to warp to {msg.Name} without being a ghost.");
return;
}
if (FindLocation(msg.Name) is { } warp)
{
ghost.Owner.Transform.Coordinates = warp.Owner.Transform.Coordinates;
}
Logger.Warning($"User {args.SenderSession.Name} tried to warp to an invalid warp: {msg.Name}");
}
private void OnGhostWarpToTargetRequest(GhostWarpToTargetRequestEvent msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity == null ||
!args.SenderSession.AttachedEntity.TryGetComponent(out GhostComponent? ghost))
{
Logger.Warning($"User {args.SenderSession.Name} tried to warp to {msg.Target} without being a ghost.");
return;
}
if (!EntityManager.TryGetEntity(msg.Target, out var entity))
{
Logger.Warning($"User {args.SenderSession.Name} tried to warp to an invalid entity id: {msg.Target}");
return;
}
ghost.Owner.Transform.Coordinates = entity.Transform.Coordinates;
}
private void DeleteEntity(EntityUid uid)
{
if (!EntityManager.TryGetEntity(uid, out var entity)
|| entity.Deleted == true
|| entity.Deleted
|| entity.LifeStage == EntityLifeStage.Terminating)
return;
@@ -37,5 +177,46 @@ namespace Content.Server.Ghost
mind.GhostOnShutdown = false;
entity.Delete();
}
private IEnumerable<string> GetLocationNames()
{
foreach (var warp in ComponentManager.EntityQuery<WarpPointComponent>())
{
if (warp.Location != null)
{
yield return warp.Location;
}
}
}
private WarpPointComponent? FindLocation(string name)
{
foreach (var warp in ComponentManager.EntityQuery<WarpPointComponent>(true))
{
if (warp.Location == name)
{
return warp;
}
}
return null;
}
private Dictionary<EntityUid, string> GetPlayerWarps(EntityUid except)
{
var players = new Dictionary<EntityUid, string>();
foreach (var player in _playerManager.GetAllPlayers())
{
if (player.AttachedEntity != null)
{
players.Add(player.AttachedEntity.Uid, player.AttachedEntity.Name);
}
}
players.Remove(except);
return players;
}
}
}