2020-08-29 20:46:42 +10:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using System.Linq;
|
2021-10-05 14:29:03 +11:00
|
|
|
using Content.Shared.Examine;
|
|
|
|
|
using Content.Shared.Interaction.Helpers;
|
|
|
|
|
using Content.Shared.Tag;
|
2020-08-29 20:46:42 +10:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-04-06 13:31:07 +10:00
|
|
|
using Robust.Shared.IoC;
|
2020-08-29 20:46:42 +10:00
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
using Robust.Shared.Maths;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Shared.Verbs
|
2020-08-29 20:46:42 +10:00
|
|
|
{
|
|
|
|
|
public class SharedVerbSystem : EntitySystem
|
|
|
|
|
{
|
2021-09-16 13:02:10 +10:00
|
|
|
[Dependency] private readonly IEntityLookup _lookup = default!;
|
|
|
|
|
|
2020-08-29 20:46:42 +10:00
|
|
|
/// <summary>
|
2021-10-05 14:29:03 +11:00
|
|
|
/// Get all of the entities in an area for displaying on the context menu.
|
2020-08-29 20:46:42 +10:00
|
|
|
/// </summary>
|
2021-10-05 14:29:03 +11:00
|
|
|
/// <param name="buffer">Whether we should slightly extend the entity search area.</param>
|
|
|
|
|
public bool TryGetContextEntities(IEntity player, MapCoordinates targetPos,
|
|
|
|
|
[NotNullWhen(true)] out List<IEntity>? contextEntities, bool buffer = false, bool ignoreVisibility = false)
|
2020-08-29 20:46:42 +10:00
|
|
|
{
|
|
|
|
|
contextEntities = null;
|
2020-08-30 11:37:06 +02:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
// Check if we have LOS to the clicked-location.
|
|
|
|
|
if (!ignoreVisibility && !player.InRangeUnOccluded(targetPos, range: ExamineSystemShared.ExamineRange))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Get entities
|
|
|
|
|
var length = buffer ? 1.0f : 0.5f;
|
2021-09-16 13:02:10 +10:00
|
|
|
var entities = _lookup.GetEntitiesIntersecting(
|
|
|
|
|
targetPos.MapId,
|
|
|
|
|
Box2.CenteredAround(targetPos.Position, (length, length)))
|
|
|
|
|
.ToList();
|
2020-08-30 11:37:06 +02:00
|
|
|
|
2021-09-16 13:02:10 +10:00
|
|
|
if (entities.Count == 0) return false;
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
if (ignoreVisibility)
|
|
|
|
|
{
|
|
|
|
|
contextEntities = entities;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-08-30 11:37:06 +02:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
// perform visibility checks
|
2021-09-16 13:02:10 +10:00
|
|
|
var playerPos = player.Transform.MapPosition;
|
2021-10-05 14:29:03 +11:00
|
|
|
foreach (var entity in entities.ToList())
|
|
|
|
|
{
|
|
|
|
|
if (entity.HasTag("HideContextMenu"))
|
|
|
|
|
{
|
|
|
|
|
entities.Remove(entity);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-09-16 13:02:10 +10:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!ExamineSystemShared.InRangeUnOccluded(
|
|
|
|
|
playerPos,
|
|
|
|
|
entity.Transform.MapPosition,
|
|
|
|
|
ExamineSystemShared.ExamineRange,
|
|
|
|
|
null) )
|
|
|
|
|
{
|
|
|
|
|
entities.Remove(entity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entities.Count == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
contextEntities = entities;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raises a number of events in order to get all verbs of the given type(s)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Dictionary<VerbType, SortedSet<Verb>> GetVerbs(IEntity target, IEntity user, VerbType verbTypes)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<VerbType, SortedSet<Verb>> verbs = new();
|
|
|
|
|
|
|
|
|
|
if ((verbTypes & VerbType.Interaction) == VerbType.Interaction)
|
2020-08-30 11:37:06 +02:00
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
GetInteractionVerbsEvent getVerbEvent = new(user, target);
|
|
|
|
|
RaiseLocalEvent(target.Uid, getVerbEvent);
|
|
|
|
|
verbs.Add(VerbType.Interaction, getVerbEvent.Verbs);
|
2020-08-30 11:37:06 +02:00
|
|
|
}
|
2020-08-29 20:46:42 +10:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
if ((verbTypes & VerbType.Activation) == VerbType.Activation)
|
|
|
|
|
{
|
|
|
|
|
GetActivationVerbsEvent getVerbEvent = new(user, target);
|
|
|
|
|
RaiseLocalEvent(target.Uid, getVerbEvent);
|
|
|
|
|
verbs.Add(VerbType.Activation, getVerbEvent.Verbs);
|
|
|
|
|
}
|
2020-10-30 04:52:37 +01:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
if ((verbTypes & VerbType.Alternative) == VerbType.Alternative)
|
|
|
|
|
{
|
|
|
|
|
GetAlternativeVerbsEvent getVerbEvent = new(user, target);
|
|
|
|
|
RaiseLocalEvent(target.Uid, getVerbEvent);
|
|
|
|
|
verbs.Add(VerbType.Alternative, getVerbEvent.Verbs);
|
|
|
|
|
}
|
2020-08-29 20:46:42 +10:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
if ((verbTypes & VerbType.Other) == VerbType.Other)
|
2020-08-29 20:46:42 +10:00
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
GetOtherVerbsEvent getVerbEvent = new(user, target);
|
|
|
|
|
RaiseLocalEvent(target.Uid, getVerbEvent);
|
|
|
|
|
verbs.Add(VerbType.Other, getVerbEvent.Verbs);
|
2020-08-29 20:46:42 +10:00
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
return verbs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute actions associated with the given verb.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This will try to call delegates and raise any events for the given verb.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public bool TryExecuteVerb(Verb verb)
|
|
|
|
|
{
|
|
|
|
|
var executed = false;
|
|
|
|
|
|
|
|
|
|
// Maybe run a delegate
|
|
|
|
|
if (verb.Act != null)
|
|
|
|
|
{
|
|
|
|
|
executed = true;
|
|
|
|
|
verb.Act.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Maybe raise a local event
|
|
|
|
|
if (verb.LocalVerbEventArgs != null)
|
|
|
|
|
{
|
|
|
|
|
executed = true;
|
|
|
|
|
if (verb.LocalEventTarget.IsValid())
|
|
|
|
|
RaiseLocalEvent(verb.LocalEventTarget, verb.LocalVerbEventArgs);
|
|
|
|
|
else
|
|
|
|
|
RaiseLocalEvent(verb.LocalVerbEventArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// maybe raise a network event
|
|
|
|
|
if (verb.NetworkVerbEventArgs != null)
|
|
|
|
|
{
|
|
|
|
|
executed = true;
|
|
|
|
|
RaiseNetworkEvent(verb.NetworkVerbEventArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return false if all of these were null
|
|
|
|
|
return executed;
|
2020-08-29 20:46:42 +10:00
|
|
|
}
|
|
|
|
|
}
|
2020-08-30 11:37:06 +02:00
|
|
|
}
|