Files
tbd-station-14/Content.Server/GameObjects/EntitySystems/Click/ExamineSystem.cs
Pieter-Jan Briers 1a92d08399 Improves examine code
Examining now has larger range. Ghosts have no range limit.
Fixed some messy code and some bad netcode.
2019-07-19 10:45:55 +02:00

110 lines
3.4 KiB
C#

using Content.Shared.GameObjects.EntitySystemMessages;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
namespace Content.Server.GameObjects.EntitySystems
{
public interface IExamine
{
/// <summary>
/// Returns an status examine value for components appended to the end of the description of the entity
/// </summary>
void Examine(FormattedMessage message);
}
public class ExamineSystem : ExamineSystemShared
{
#pragma warning disable 649
[Dependency] private IEntityManager _entityManager;
[Dependency] private IPlayerManager _playerManager;
#pragma warning restore 649
private static readonly FormattedMessage _entityNotFoundMessage;
static ExamineSystem()
{
_entityNotFoundMessage = new FormattedMessage();
_entityNotFoundMessage.AddText("That entity doesn't exist");
}
public override void Initialize()
{
base.Initialize();
IoCManager.InjectDependencies(this);
}
public override void RegisterMessageTypes()
{
base.RegisterMessageTypes();
RegisterMessageType<ExamineSystemMessages.RequestExamineInfoMessage>();
}
private FormattedMessage GetExamineText(IEntity entity)
{
var message = new FormattedMessage();
var doNewline = false;
//Add an entity description if one is declared
if (!string.IsNullOrEmpty(entity.Description))
{
message.AddText(entity.Description);
doNewline = true;
}
message.PushColor(Color.DarkGray);
var subMessage = new FormattedMessage();
//Add component statuses from components that report one
foreach (var examineComponents in entity.GetAllComponents<IExamine>())
{
examineComponents.Examine(subMessage);
if (subMessage.Tags.Count == 0)
continue;
if (doNewline)
{
message.AddText("\n");
doNewline = false;
}
message.AddMessage(subMessage);
}
message.Pop();
return message;
}
public override void HandleNetMessage(INetChannel channel, EntitySystemMessage message)
{
base.HandleNetMessage(channel, message);
if (!(message is ExamineSystemMessages.RequestExamineInfoMessage request))
return;
var session = _playerManager.GetSessionByChannel(channel);
var playerEnt = session.AttachedEntity;
if (playerEnt == null
|| !_entityManager.TryGetEntity(request.EntityUid, out var entity)
|| !CanExamine(playerEnt, entity))
{
RaiseNetworkEvent(new ExamineSystemMessages.ExamineInfoResponseMessage(
request.EntityUid, _entityNotFoundMessage), channel);
return;
}
var text = GetExamineText(entity);
RaiseNetworkEvent(new ExamineSystemMessages.ExamineInfoResponseMessage(request.EntityUid, text), channel);
}
}
}