2023-03-06 06:12:08 +13:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using System.Linq;
|
2021-11-22 09:40:09 +13:00
|
|
|
using Content.Client.Examine;
|
2022-09-04 17:21:14 -07:00
|
|
|
using Content.Client.Gameplay;
|
2021-10-28 18:21:19 +13:00
|
|
|
using Content.Client.Popups;
|
|
|
|
|
using Content.Shared.Examine;
|
|
|
|
|
using Content.Shared.Tag;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Verbs;
|
2019-08-25 12:54:55 +02:00
|
|
|
using JetBrains.Annotations;
|
2021-10-28 18:21:19 +13:00
|
|
|
using Robust.Client.GameObjects;
|
|
|
|
|
using Robust.Client.Graphics;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Client.Player;
|
2021-11-22 09:40:09 +13:00
|
|
|
using Robust.Client.State;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.Map;
|
2022-09-06 13:40:31 +10:00
|
|
|
using Robust.Shared.Utility;
|
2018-11-21 20:58:11 +01:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Client.Verbs
|
2018-11-21 20:58:11 +01:00
|
|
|
{
|
2019-08-25 12:54:55 +02:00
|
|
|
[UsedImplicitly]
|
2021-06-29 15:56:07 +02:00
|
|
|
public sealed class VerbSystem : SharedVerbSystem
|
2018-11-21 20:58:11 +01:00
|
|
|
{
|
2021-10-28 18:21:19 +13:00
|
|
|
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
2021-11-22 09:40:09 +13:00
|
|
|
[Dependency] private readonly ExamineSystem _examineSystem = default!;
|
2022-02-08 14:08:11 +11:00
|
|
|
[Dependency] private readonly TagSystem _tagSystem = default!;
|
2021-11-22 09:40:09 +13:00
|
|
|
[Dependency] private readonly IStateManager _stateManager = default!;
|
2022-03-03 21:18:35 +11:00
|
|
|
[Dependency] private readonly EntityLookupSystem _entityLookup = default!;
|
2021-10-05 14:29:03 +11:00
|
|
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
|
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
/// <summary>
|
|
|
|
|
/// When a user right clicks somewhere, how large is the box we use to get entities for the context menu?
|
|
|
|
|
/// </summary>
|
2021-11-20 18:25:29 +13:00
|
|
|
public const float EntityMenuLookupSize = 0.25f;
|
2018-11-21 20:58:11 +01:00
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
2021-02-25 19:42:16 -06:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
/// <summary>
|
2021-10-28 18:21:19 +13:00
|
|
|
/// These flags determine what entities the user can see on the context menu.
|
2021-10-05 14:29:03 +11:00
|
|
|
/// </summary>
|
2021-10-28 18:21:19 +13:00
|
|
|
public MenuVisibility Visibility;
|
2018-11-21 20:58:11 +01:00
|
|
|
|
2023-01-07 21:24:52 -06:00
|
|
|
public Action<VerbsResponseEvent>? OnVerbsResponse;
|
|
|
|
|
|
2018-11-21 20:58:11 +01:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2020-09-06 16:11:53 +02:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
SubscribeNetworkEvent<VerbsResponseEvent>(HandleVerbResponse);
|
2021-02-25 19:42:16 -06:00
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
/// <summary>
|
2021-10-28 18:21:19 +13:00
|
|
|
/// Get all of the entities in an area for displaying on the context menu.
|
2021-10-05 14:29:03 +11:00
|
|
|
/// </summary>
|
2021-12-05 18:09:01 +01:00
|
|
|
public bool TryGetEntityMenuEntities(MapCoordinates targetPos, [NotNullWhen(true)] out List<EntityUid>? result)
|
2021-02-25 19:42:16 -06:00
|
|
|
{
|
2021-10-28 18:21:19 +13:00
|
|
|
result = null;
|
2020-10-26 12:11:32 +01:00
|
|
|
|
2022-09-04 17:21:14 -07:00
|
|
|
if (_stateManager.CurrentState is not GameplayStateBase gameScreenBase)
|
2021-11-22 09:40:09 +13:00
|
|
|
return false;
|
|
|
|
|
|
2024-02-13 22:48:39 +01:00
|
|
|
var player = _playerManager.LocalEntity;
|
2021-10-28 18:21:19 +13:00
|
|
|
if (player == null)
|
|
|
|
|
return false;
|
2018-11-21 20:58:11 +01:00
|
|
|
|
2021-11-22 09:40:09 +13:00
|
|
|
// If FOV drawing is disabled, we will modify the visibility option to ignore visiblity checks.
|
2021-10-28 18:21:19 +13:00
|
|
|
var visibility = _eyeManager.CurrentEye.DrawFov
|
|
|
|
|
? Visibility
|
|
|
|
|
: Visibility | MenuVisibility.NoFov;
|
2021-10-05 14:29:03 +11:00
|
|
|
|
2022-10-15 17:15:25 +13:00
|
|
|
|
|
|
|
|
// Get entities
|
|
|
|
|
List<EntityUid> entities;
|
|
|
|
|
|
2021-11-22 09:40:09 +13:00
|
|
|
// Do we have to do FoV checks?
|
|
|
|
|
if ((visibility & MenuVisibility.NoFov) == 0)
|
|
|
|
|
{
|
2023-01-05 18:29:27 +13:00
|
|
|
var entitiesUnderMouse = gameScreenBase.GetClickableEntities(targetPos).ToHashSet();
|
2021-12-05 18:09:01 +01:00
|
|
|
bool Predicate(EntityUid e) => e == player || entitiesUnderMouse.Contains(e);
|
2022-10-15 17:15:25 +13:00
|
|
|
|
|
|
|
|
// first check the general location.
|
2021-12-05 18:09:01 +01:00
|
|
|
if (!_examineSystem.CanExamine(player.Value, targetPos, Predicate))
|
2021-11-22 09:40:09 +13:00
|
|
|
return false;
|
2021-12-03 14:20:34 +01:00
|
|
|
|
2022-10-15 17:15:25 +13:00
|
|
|
TryComp(player.Value, out ExaminerComponent? examiner);
|
|
|
|
|
|
|
|
|
|
// Then check every entity
|
|
|
|
|
entities = new();
|
|
|
|
|
foreach (var ent in _entityLookup.GetEntitiesInRange(targetPos, EntityMenuLookupSize))
|
|
|
|
|
{
|
|
|
|
|
if (_examineSystem.CanExamine(player.Value, targetPos, Predicate, ent, examiner))
|
|
|
|
|
entities.Add(ent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
entities = _entityLookup.GetEntitiesInRange(targetPos, EntityMenuLookupSize).ToList();
|
|
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
if (entities.Count == 0)
|
|
|
|
|
return false;
|
2021-10-05 14:29:03 +11:00
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
if (visibility == MenuVisibility.All)
|
2020-07-23 00:56:30 +02:00
|
|
|
{
|
2021-10-28 18:21:19 +13:00
|
|
|
result = entities;
|
|
|
|
|
return true;
|
2020-07-23 00:56:30 +02:00
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
// remove any entities in containers
|
|
|
|
|
if ((visibility & MenuVisibility.InContainer) == 0)
|
2018-11-21 20:58:11 +01:00
|
|
|
{
|
2022-09-06 13:40:31 +10:00
|
|
|
for (var i = entities.Count - 1; i >= 0; i--)
|
2021-10-28 18:21:19 +13:00
|
|
|
{
|
2022-09-06 13:40:31 +10:00
|
|
|
var entity = entities[i];
|
|
|
|
|
|
|
|
|
|
if (ContainerSystem.IsInSameOrTransparentContainer(player.Value, entity))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
entities.RemoveSwap(i);
|
2021-10-28 18:21:19 +13:00
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
// remove any invisible entities
|
|
|
|
|
if ((visibility & MenuVisibility.Invisible) == 0)
|
2019-10-30 11:31:35 -04:00
|
|
|
{
|
2022-09-06 13:40:31 +10:00
|
|
|
var spriteQuery = GetEntityQuery<SpriteComponent>();
|
|
|
|
|
var tagQuery = GetEntityQuery<TagComponent>();
|
|
|
|
|
|
|
|
|
|
for (var i = entities.Count - 1; i >= 0; i--)
|
2021-10-28 18:21:19 +13:00
|
|
|
{
|
2022-09-06 13:40:31 +10:00
|
|
|
var entity = entities[i];
|
|
|
|
|
|
|
|
|
|
if (!spriteQuery.TryGetComponent(entity, out var spriteComponent) ||
|
|
|
|
|
!spriteComponent.Visible ||
|
|
|
|
|
_tagSystem.HasTag(entity, "HideContextMenu", tagQuery))
|
2021-10-28 18:21:19 +13:00
|
|
|
{
|
2022-09-06 13:40:31 +10:00
|
|
|
entities.RemoveSwap(i);
|
2021-10-28 18:21:19 +13:00
|
|
|
}
|
|
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
// Remove any entities that do not have LOS
|
|
|
|
|
if ((visibility & MenuVisibility.NoFov) == 0)
|
2018-11-21 20:58:11 +01:00
|
|
|
{
|
2024-02-28 00:51:20 +11:00
|
|
|
var xformQuery = GetEntityQuery<TransformComponent>();
|
|
|
|
|
var playerPos = xformQuery.GetComponent(player.Value).MapPosition;
|
2022-09-06 13:40:31 +10:00
|
|
|
|
|
|
|
|
for (var i = entities.Count - 1; i >= 0; i--)
|
2018-11-21 20:58:11 +01:00
|
|
|
{
|
2022-09-06 13:40:31 +10:00
|
|
|
var entity = entities[i];
|
|
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
if (!ExamineSystemShared.InRangeUnOccluded(
|
|
|
|
|
playerPos,
|
2024-02-28 00:51:20 +11:00
|
|
|
xformQuery.GetComponent(entity).MapPosition,
|
2021-10-28 18:21:19 +13:00
|
|
|
ExamineSystemShared.ExamineRange,
|
|
|
|
|
null))
|
|
|
|
|
{
|
2022-09-06 13:40:31 +10:00
|
|
|
entities.RemoveSwap(i);
|
2021-10-28 18:21:19 +13:00
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
if (entities.Count == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
result = entities;
|
|
|
|
|
return true;
|
2020-05-23 03:09:44 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-13 20:20:58 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Asks the server to send back a list of server-side verbs, for the given verb type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public SortedSet<Verb> GetVerbs(EntityUid target, EntityUid user, Type type, bool force = false)
|
|
|
|
|
{
|
2023-11-05 02:58:26 +11:00
|
|
|
return GetVerbs(GetNetEntity(target), user, new List<Type>() { type }, force);
|
2022-02-13 20:20:58 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
/// <summary>
|
|
|
|
|
/// Ask the server to send back a list of server-side verbs, and for now return an incomplete list of verbs
|
|
|
|
|
/// (only those defined locally).
|
|
|
|
|
/// </summary>
|
2023-11-05 02:58:26 +11:00
|
|
|
public SortedSet<Verb> GetVerbs(NetEntity target, EntityUid user, List<Type> verbTypes,
|
2021-12-30 14:11:30 -08:00
|
|
|
bool force = false)
|
2020-05-23 03:09:44 +02:00
|
|
|
{
|
2023-11-05 02:58:26 +11:00
|
|
|
if (!target.IsClientSide())
|
|
|
|
|
RaiseNetworkEvent(new RequestServerVerbsEvent(target, verbTypes, adminRequest: force));
|
2021-12-03 14:20:34 +01:00
|
|
|
|
2022-10-16 10:26:29 +13:00
|
|
|
// Some admin menu interactions will try get verbs for entities that have not yet been sent to the player.
|
2023-11-05 02:58:26 +11:00
|
|
|
if (!TryGetEntity(target, out var local))
|
2022-10-16 10:26:29 +13:00
|
|
|
return new();
|
|
|
|
|
|
2023-11-05 02:58:26 +11:00
|
|
|
return GetLocalVerbs(local.Value, user, verbTypes, force);
|
2020-05-23 03:09:44 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-05 02:58:26 +11:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
/// <summary>
|
2021-10-28 18:21:19 +13:00
|
|
|
/// Execute actions associated with the given verb.
|
2021-10-05 14:29:03 +11:00
|
|
|
/// </summary>
|
2021-10-28 18:21:19 +13:00
|
|
|
/// <remarks>
|
2021-12-16 23:42:02 +13:00
|
|
|
/// Unless this is a client-exclusive verb, this will also tell the server to run the same verb.
|
2021-10-28 18:21:19 +13:00
|
|
|
/// </remarks>
|
2022-02-10 15:30:59 +13:00
|
|
|
public void ExecuteVerb(EntityUid target, Verb verb)
|
2020-05-23 03:09:44 +02:00
|
|
|
{
|
2023-11-05 02:58:26 +11:00
|
|
|
ExecuteVerb(GetNetEntity(target), verb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute actions associated with the given verb.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Unless this is a client-exclusive verb, this will also tell the server to run the same verb.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public void ExecuteVerb(NetEntity target, Verb verb)
|
|
|
|
|
{
|
|
|
|
|
if ( _playerManager.LocalEntity is not {} user)
|
2021-12-16 23:42:02 +13:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// is this verb actually valid?
|
2021-10-28 18:21:19 +13:00
|
|
|
if (verb.Disabled)
|
|
|
|
|
{
|
2021-12-16 23:42:02 +13:00
|
|
|
// maybe send an informative pop-up message.
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(verb.Message))
|
2023-11-05 02:58:26 +11:00
|
|
|
_popupSystem.PopupEntity(verb.Message, user);
|
2021-12-16 23:42:02 +13:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
return;
|
2021-10-28 18:21:19 +13:00
|
|
|
}
|
2020-05-23 03:09:44 +02:00
|
|
|
|
2023-11-05 02:58:26 +11:00
|
|
|
if (verb.ClientExclusive || target.IsClientSide())
|
2021-12-16 23:42:02 +13:00
|
|
|
// is this a client exclusive (gui) verb?
|
2023-11-05 02:58:26 +11:00
|
|
|
ExecuteVerb(verb, user, GetEntity(target));
|
2021-12-16 23:42:02 +13:00
|
|
|
else
|
2023-11-05 02:58:26 +11:00
|
|
|
EntityManager.RaisePredictiveEvent(new ExecuteVerbEvent(target, verb));
|
2021-12-16 23:42:02 +13:00
|
|
|
}
|
2021-11-23 23:00:16 +13:00
|
|
|
|
2021-10-28 18:21:19 +13:00
|
|
|
private void HandleVerbResponse(VerbsResponseEvent msg)
|
2020-05-23 03:09:44 +02:00
|
|
|
{
|
2023-01-07 21:24:52 -06:00
|
|
|
OnVerbsResponse?.Invoke(msg);
|
2019-08-25 12:54:55 +02:00
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
}
|
2021-10-28 18:21:19 +13:00
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
|
public enum MenuVisibility
|
|
|
|
|
{
|
|
|
|
|
// What entities can a user see on the entity menu?
|
|
|
|
|
Default = 0, // They can only see entities in FoV.
|
|
|
|
|
NoFov = 1 << 0, // They ignore FoV restrictions
|
|
|
|
|
InContainer = 1 << 1, // They can see through containers.
|
|
|
|
|
Invisible = 1 << 2, // They can see entities without sprites and the "HideContextMenu" tag is ignored.
|
|
|
|
|
All = NoFov | InContainer | Invisible
|
|
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
}
|