2020-05-31 19:29:06 +01:00
using Content.Server.Utility ;
using Content.Shared.GameObjects.EntitySystemMessages ;
2019-07-19 10:45:04 +02:00
using Content.Shared.GameObjects.EntitySystems ;
2019-04-15 21:11:38 -06:00
using Robust.Server.Interfaces.Player ;
2020-03-25 11:16:57 +01:00
using Robust.Shared.GameObjects ;
2020-05-31 19:29:06 +01:00
using Robust.Shared.GameObjects.Systems ;
2019-04-15 21:11:38 -06:00
using Robust.Shared.Interfaces.GameObjects ;
using Robust.Shared.IoC ;
using Robust.Shared.Maths ;
using Robust.Shared.Utility ;
2018-05-09 09:34:26 -05:00
2020-07-06 14:27:03 -07:00
namespace Content.Server.GameObjects.EntitySystems.Click
2018-05-09 09:34:26 -05:00
{
public interface IExamine
{
/// <summary>
2020-05-31 19:29:06 +01:00
/// Returns a status examine value for components appended to the end of the description of the entity
2018-05-09 09:34:26 -05:00
/// </summary>
2020-05-31 19:29:06 +01:00
/// <param name="inDetailsRange">Whether the examiner is within the 'Details' range, allowing you to show information logically only availabe when close to the examined entity.</param>
void Examine ( FormattedMessage message , bool inDetailsRange ) ;
2018-05-09 09:34:26 -05:00
}
2019-07-19 10:45:04 +02:00
public class ExamineSystem : ExamineSystemShared
2018-05-09 09:34:26 -05:00
{
2019-04-09 17:33:53 +02:00
#pragma warning disable 649
[Dependency] private IEntityManager _entityManager ;
#pragma warning restore 649
private static readonly FormattedMessage _entityNotFoundMessage ;
2020-05-31 19:29:06 +01:00
private const float ExamineDetailsRange = 3f ;
2019-04-09 17:33:53 +02:00
static ExamineSystem ( )
2018-08-08 11:43:49 -07:00
{
2019-04-09 17:33:53 +02:00
_entityNotFoundMessage = new FormattedMessage ( ) ;
_entityNotFoundMessage . AddText ( "That entity doesn't exist" ) ;
2018-08-08 11:43:49 -07:00
}
2018-05-09 09:34:26 -05:00
2019-04-09 17:33:53 +02:00
public override void Initialize ( )
2018-05-09 09:34:26 -05:00
{
2019-04-09 17:33:53 +02:00
base . Initialize ( ) ;
2018-05-09 09:34:26 -05:00
2020-02-19 17:08:59 -08:00
SubscribeNetworkEvent < ExamineSystemMessages . RequestExamineInfoMessage > ( ExamineInfoRequest ) ;
2018-08-08 11:43:49 -07:00
2020-02-18 19:43:54 -08:00
IoCManager . InjectDependencies ( this ) ;
2019-04-09 17:33:53 +02:00
}
2018-05-09 09:34:26 -05:00
2020-05-31 19:29:06 +01:00
private static FormattedMessage GetExamineText ( IEntity entity , IEntity examiner )
2019-04-09 17:33:53 +02:00
{
var message = new FormattedMessage ( ) ;
var doNewline = false ;
2018-05-09 09:34:26 -05:00
//Add an entity description if one is declared
2019-04-09 17:33:53 +02:00
if ( ! string . IsNullOrEmpty ( entity . Description ) )
2018-05-09 09:34:26 -05:00
{
2019-04-09 17:33:53 +02:00
message . AddText ( entity . Description ) ;
doNewline = true ;
2018-05-09 09:34:26 -05:00
}
2019-04-09 17:33:53 +02:00
message . PushColor ( Color . DarkGray ) ;
2020-05-31 19:29:06 +01:00
var inDetailsRange = Get < SharedInteractionSystem > ( )
. InRangeUnobstructed ( examiner . Transform . MapPosition , entity . Transform . MapPosition ,
2020-07-06 14:27:03 -07:00
ExamineDetailsRange , predicate : entity0 = > entity0 = = examiner | | entity0 = = entity , ignoreInsideBlocker : true ) ;
2020-05-31 19:29:06 +01:00
2018-05-09 09:34:26 -05:00
//Add component statuses from components that report one
2020-05-31 19:29:06 +01:00
foreach ( var examineComponent in entity . GetAllComponents < IExamine > ( ) )
2018-05-09 09:34:26 -05:00
{
2019-08-19 22:27:25 +02:00
var subMessage = new FormattedMessage ( ) ;
2020-05-31 19:29:06 +01:00
examineComponent . Examine ( subMessage , inDetailsRange ) ;
2019-04-09 17:33:53 +02:00
if ( subMessage . Tags . Count = = 0 )
2018-08-08 11:43:49 -07:00
continue ;
2019-04-09 17:33:53 +02:00
if ( doNewline )
message . AddText ( "\n" ) ;
2019-08-19 22:27:25 +02:00
2019-04-09 17:33:53 +02:00
message . AddMessage ( subMessage ) ;
2019-08-19 22:27:25 +02:00
doNewline = true ;
2018-05-09 09:34:26 -05:00
}
2019-04-09 17:33:53 +02:00
message . Pop ( ) ;
return message ;
}
2020-03-25 11:16:57 +01:00
private void ExamineInfoRequest ( ExamineSystemMessages . RequestExamineInfoMessage request , EntitySessionEventArgs eventArgs )
2019-04-09 17:33:53 +02:00
{
2020-03-25 11:16:57 +01:00
var player = ( IPlayerSession ) eventArgs . SenderSession ;
var session = eventArgs . SenderSession ;
2019-05-16 14:41:43 -07:00
var playerEnt = session . AttachedEntity ;
2020-03-25 11:16:57 +01:00
var channel = player . ConnectedClient ;
2019-05-16 14:41:43 -07:00
2019-07-19 10:45:04 +02:00
if ( playerEnt = = null
| | ! _entityManager . TryGetEntity ( request . EntityUid , out var entity )
| | ! CanExamine ( playerEnt , entity ) )
2019-05-16 14:41:43 -07:00
{
RaiseNetworkEvent ( new ExamineSystemMessages . ExamineInfoResponseMessage (
2019-07-19 10:45:04 +02:00
request . EntityUid , _entityNotFoundMessage ) , channel ) ;
2019-05-16 14:41:43 -07:00
return ;
2019-04-09 17:33:53 +02:00
}
2019-05-16 14:41:43 -07:00
2020-05-31 19:29:06 +01:00
var text = GetExamineText ( entity , player . AttachedEntity ) ;
2019-07-19 10:45:04 +02:00
RaiseNetworkEvent ( new ExamineSystemMessages . ExamineInfoResponseMessage ( request . EntityUid , text ) , channel ) ;
2018-05-09 09:34:26 -05:00
}
}
}