Files
OldThink/Content.Server/Pointing/EntitySystems/PointingSystem.cs

301 lines
11 KiB
C#
Raw Normal View History

using System.Linq;
2022-12-08 19:18:13 -06:00
using Content.Server.Administration.Logs;
2021-06-09 22:19:39 +02:00
using Content.Server.Pointing.Components;
2022-08-16 05:22:16 +01:00
using Content.Shared.Bed.Sleep;
2022-12-08 19:18:13 -06:00
using Content.Shared.Database;
using Content.Shared.Examine;
using Content.Shared.Eye;
2023-08-25 18:50:46 +10:00
using Content.Shared.Ghost;
using Content.Shared.IdentityManagement;
using Content.Shared.Input;
2021-11-02 18:38:47 +00:00
using Content.Shared.Interaction;
using Content.Shared.Mind;
using Content.Shared.Mobs.Systems;
using Content.Shared.Pointing;
2021-12-06 00:52:58 +01:00
using Content.Shared.Popups;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Enums;
using Robust.Shared.Input.Binding;
using Robust.Shared.Map;
using Robust.Shared.Player;
using Robust.Shared.Replays;
using Robust.Shared.Timing;
2021-06-09 22:19:39 +02:00
namespace Content.Server.Pointing.EntitySystems
{
[UsedImplicitly]
2022-09-17 00:37:15 +10:00
internal sealed class PointingSystem : SharedPointingSystem
{
[Dependency] private readonly IReplayRecordingManager _replay = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
2021-11-02 18:38:47 +00:00
[Dependency] private readonly RotateToFaceSystem _rotateToFaceSystem = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
2021-12-29 05:12:28 +11:00
[Dependency] private readonly VisibilitySystem _visibilitySystem = default!;
[Dependency] private readonly SharedMindSystem _minds = default!;
2022-12-08 19:18:13 -06:00
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
private static readonly TimeSpan PointDelay = TimeSpan.FromSeconds(0.5f);
/// <summary>
/// A dictionary of players to the last time that they
/// pointed at something.
/// </summary>
private readonly Dictionary<ICommonSession, TimeSpan> _pointers = new();
private const float PointingRange = 15f;
private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
{
if (e.NewStatus != SessionStatus.Disconnected)
{
return;
}
_pointers.Remove(e.Session);
}
// TODO: FOV
2021-12-05 18:09:01 +01:00
private void SendMessage(EntityUid source, IEnumerable<ICommonSession> viewers, EntityUid pointed, string selfMessage,
string viewerMessage, string? viewerPointedAtMessage = null)
{
var netSource = GetNetEntity(source);
foreach (var viewer in viewers)
{
2021-12-06 00:52:58 +01:00
if (viewer.AttachedEntity is not {Valid: true} viewerEntity)
{
continue;
}
var message = viewerEntity == source
? selfMessage
: viewerEntity == pointed && viewerPointedAtMessage != null
? viewerPointedAtMessage
: viewerMessage;
RaiseNetworkEvent(new PopupEntityEvent(message, PopupType.Small, netSource), viewerEntity);
}
_replay.RecordServerMessage(new PopupEntityEvent(viewerMessage, PopupType.Small, netSource));
}
2021-12-05 18:09:01 +01:00
public bool InRange(EntityUid pointer, EntityCoordinates coordinates)
{
2021-12-14 15:21:54 +13:00
if (HasComp<GhostComponent>(pointer))
{
2021-12-14 15:21:54 +13:00
return Transform(pointer).Coordinates.InRange(EntityManager, coordinates, 15);
}
else
{
return ExamineSystemShared.InRangeUnOccluded(pointer, coordinates, 15, predicate: e => e == pointer);
}
}
2021-12-06 00:52:58 +01:00
public bool TryPoint(ICommonSession? session, EntityCoordinates coords, EntityUid pointed)
{
if (session?.AttachedEntity is not { } player)
{
Log.Warning($"Player {session} attempted to point without any attached entity");
return false;
}
if (!coords.IsValid(EntityManager))
{
Log.Warning($"Player {ToPrettyString(player)} attempted to point at invalid coordinates: {coords}");
return false;
}
if (_pointers.TryGetValue(session, out var lastTime) &&
_gameTiming.CurTime < lastTime + PointDelay)
{
return false;
}
2021-12-14 15:21:54 +13:00
if (HasComp<PointingArrowComponent>(pointed))
2020-07-29 12:36:31 +02:00
{
// this is a pointing arrow. no pointing here...
return false;
}
2021-12-14 15:21:54 +13:00
// Checking mob state directly instead of some action blocker, as many action blockers are blocked for
// ghosts and there is no obvious choice for pointing.
if (_mobState.IsIncapacitated(player))
2021-12-14 15:21:54 +13:00
{
return false;
}
2022-08-16 05:22:16 +01:00
if (HasComp<SleepingComponent>(player))
{
return false;
}
if (!InRange(player, coords))
{
_popup.PopupEntity(Loc.GetString("pointing-system-try-point-cannot-reach"), player, player);
return false;
}
var mapCoords = coords.ToMap(EntityManager);
2021-11-02 18:38:47 +00:00
_rotateToFaceSystem.TryFaceCoordinates(player, mapCoords.Position);
var arrow = EntityManager.SpawnEntity("PointingArrow", coords);
2022-09-17 00:37:15 +10:00
if (TryComp<PointingArrowComponent>(arrow, out var pointing))
{
pointing.EndTime = _gameTiming.CurTime + TimeSpan.FromSeconds(4);
}
if (EntityQuery<PointingArrowAngeringComponent>().FirstOrDefault() != null)
{
if (TryComp<PointingArrowComponent>(arrow, out var pointingArrowComponent))
{
pointingArrowComponent.Rogue = true;
}
}
var layer = (int) VisibilityFlags.Normal;
2021-12-14 15:21:54 +13:00
if (TryComp(player, out VisibilityComponent? playerVisibility))
{
2021-12-29 05:12:28 +11:00
var arrowVisibility = EntityManager.EnsureComponent<VisibilityComponent>(arrow);
layer = playerVisibility.Layer;
_visibilitySystem.SetLayer(arrow, arrowVisibility, layer);
}
// Get players that are in range and whose visibility layer matches the arrow's.
bool ViewerPredicate(ICommonSession playerSession)
{
if (!_minds.TryGetMind(playerSession, out _, out var mind) ||
mind.CurrentEntity is not { Valid: true } ent ||
2021-12-14 15:21:54 +13:00
!TryComp(ent, out EyeComponent? eyeComp) ||
2021-12-06 00:52:58 +01:00
(eyeComp.VisibilityMask & layer) == 0)
return false;
2021-12-14 15:21:54 +13:00
return Transform(ent).MapPosition.InRange(Transform(player).MapPosition, PointingRange);
}
var viewers = Filter.Empty()
.AddWhere(session1 => ViewerPredicate(session1))
.Recipients;
string selfMessage;
string viewerMessage;
string? viewerPointedAtMessage = null;
var playerName = Identity.Entity(player, EntityManager);
2021-12-14 15:21:54 +13:00
if (Exists(pointed))
{
var pointedName = Identity.Entity(pointed, EntityManager);
2021-12-14 15:21:54 +13:00
selfMessage = player == pointed
? Loc.GetString("pointing-system-point-at-self")
2021-12-14 15:21:54 +13:00
: Loc.GetString("pointing-system-point-at-other", ("other", pointedName));
viewerMessage = player == pointed
2021-12-14 15:21:54 +13:00
? Loc.GetString("pointing-system-point-at-self-others", ("otherName", playerName), ("other", playerName))
: Loc.GetString("pointing-system-point-at-other-others", ("otherName", playerName), ("other", pointedName));
2021-12-14 15:21:54 +13:00
viewerPointedAtMessage = Loc.GetString("pointing-system-point-at-you-other", ("otherName", playerName));
2022-12-08 19:18:13 -06:00
var ev = new AfterPointedAtEvent(pointed);
RaiseLocalEvent(player, ref ev);
var gotev = new AfterGotPointedAtEvent(player);
RaiseLocalEvent(pointed, ref gotev);
2022-12-08 19:18:13 -06:00
_adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(player):user} pointed at {ToPrettyString(pointed):target} {Transform(pointed).Coordinates}");
}
else
{
2021-08-06 18:27:37 +02:00
TileRef? tileRef = null;
2022-12-08 19:18:13 -06:00
string? position = null;
2021-08-06 18:27:37 +02:00
if (_mapManager.TryFindGridAt(mapCoords, out var gridUid, out var grid))
2021-08-06 18:27:37 +02:00
{
position = $"EntId={gridUid} {grid.WorldToTile(mapCoords.Position)}";
2021-08-06 18:27:37 +02:00
tileRef = grid.GetTileRef(grid.WorldToTile(mapCoords.Position));
}
var tileDef = _tileDefinitionManager[tileRef?.Tile.TypeId ?? 0];
var name = Loc.GetString(tileDef.Name);
selfMessage = Loc.GetString("pointing-system-point-at-tile", ("tileName", name));
viewerMessage = Loc.GetString("pointing-system-other-point-at-tile", ("otherName", playerName), ("tileName", name));
2022-12-08 19:18:13 -06:00
_adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(player):user} pointed at {name} {(position == null ? mapCoords : position)}");
}
2021-12-06 00:52:58 +01:00
_pointers[session] = _gameTiming.CurTime;
SendMessage(player, viewers, pointed, selfMessage, viewerMessage, viewerPointedAtMessage);
return true;
}
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent<PointingAttemptEvent>(OnPointAttempt);
ECS verbs and update context menu (#4594) * Functioning ECS verbs Currently only ID card console works. * Changed verb types and allow ID card insertions * Verb GUI sorting and verb networking * More networking, and shared components * Clientside verbs work now. * Verb enums changed to bitmask flags * Verb Categories redo * Fix range check * GasTank Verb * Remove unnecessary bodypart verb * Buckle Verb * buckle & unbuckle verbs * Updated range checks * Item cabinet verbs * Add range user override * construction verb * Chemistry machine verbs * Climb Verb * Generalise pulled entity verbs * ViewVariables Verb * rejuvenate, delete, sentient, control verbs * Outfit verb * inrangeunoccluded and tubedirection verbs * attach-to verbs * remove unused verbs and move VV * Rename DebugVerbSystem * Ghost role and pointing verbs * Remove global verbs * Allow verbs to raise events * Changing categories and simplifying debug verbs * Add rotate and flip verbs * fix rejuvenate test * redo context menu * new Add Gas debug verb * Add Set Temperature debug verb * Uncuff verb * Disposal unit verbs * Add pickup verb * lock/unlock verb * Remove verb type, add specific verb events * rename verb messages -> events * Context menu displays verbs by interaction type * Updated context menu HandleMove previously, checked if entities moved 1 tile from click location. Now checks if entities moved out of view. Now you can actually right-click interact with yourself while walking! * Misc Verb menu GUI changes * Fix non-human/ghost verbs * Update types and categories * Allow non-ghost/human to open context menu * configuration verb * tagger verb * Morgue Verbs * Medical Scanner Verbs * Fix solution refactor merge issues * Fix context menu in-view check * Remove prepare GUI * Redo verb restrictions * Fix context menu UI * Disposal Verbs * Spill verb * Light verb * Hand Held light verb * power cell verbs * storage verbs and adding names to insert/eject * Pulling verb * Close context menu on verb execution * Strip verb * AmmoBox verb * fix pull verb * gun barrel verbs revolver verb energy weapon verbs Bolt action verb * Magazine gun barrel verbs * Add charger verbs * PDA verbs * Transfer amount verb * Add reagent verb * make alt-click use ECS verbs * Delete old verb files * Magboot verb * finalising tweaks * context menu visibility changes * code cleanup * Update AdminAddReagentUI.cs * Remove HasFlag * Consistent verb keys * Remove Linq, add comment * Fix in-inventory check * Update GUI text alignment and padding * Added close-menu option * Changed some "interaction" verbs to "activation" * Remove verb keys, use sorted sets * fix master merge * update some verb text * Undo Changes Remove some new verbs that can be added later undid some .ftl bugfixes, can and should be done separately * fix merge * Undo file rename * fix merge * Misc Cleanup * remove contraction * Fix keybinding issue * fix comment * merge fix * fix merge * fix merge * fix merge * fix merge * fix open-close verbs * adjust uncuff verb * fix merge and undo the renaming of SharedPullableComponent to PullableComponent. I'm tired of all of those merge conflicts
2021-10-05 14:29:03 +11:00
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
CommandBinds.Builder
.Bind(ContentKeyFunctions.Point, new PointerInputCmdHandler(TryPoint))
.Register<PointingSystem>();
}
private void OnPointAttempt(PointingAttemptEvent ev, EntitySessionEventArgs args)
ECS verbs and update context menu (#4594) * Functioning ECS verbs Currently only ID card console works. * Changed verb types and allow ID card insertions * Verb GUI sorting and verb networking * More networking, and shared components * Clientside verbs work now. * Verb enums changed to bitmask flags * Verb Categories redo * Fix range check * GasTank Verb * Remove unnecessary bodypart verb * Buckle Verb * buckle & unbuckle verbs * Updated range checks * Item cabinet verbs * Add range user override * construction verb * Chemistry machine verbs * Climb Verb * Generalise pulled entity verbs * ViewVariables Verb * rejuvenate, delete, sentient, control verbs * Outfit verb * inrangeunoccluded and tubedirection verbs * attach-to verbs * remove unused verbs and move VV * Rename DebugVerbSystem * Ghost role and pointing verbs * Remove global verbs * Allow verbs to raise events * Changing categories and simplifying debug verbs * Add rotate and flip verbs * fix rejuvenate test * redo context menu * new Add Gas debug verb * Add Set Temperature debug verb * Uncuff verb * Disposal unit verbs * Add pickup verb * lock/unlock verb * Remove verb type, add specific verb events * rename verb messages -> events * Context menu displays verbs by interaction type * Updated context menu HandleMove previously, checked if entities moved 1 tile from click location. Now checks if entities moved out of view. Now you can actually right-click interact with yourself while walking! * Misc Verb menu GUI changes * Fix non-human/ghost verbs * Update types and categories * Allow non-ghost/human to open context menu * configuration verb * tagger verb * Morgue Verbs * Medical Scanner Verbs * Fix solution refactor merge issues * Fix context menu in-view check * Remove prepare GUI * Redo verb restrictions * Fix context menu UI * Disposal Verbs * Spill verb * Light verb * Hand Held light verb * power cell verbs * storage verbs and adding names to insert/eject * Pulling verb * Close context menu on verb execution * Strip verb * AmmoBox verb * fix pull verb * gun barrel verbs revolver verb energy weapon verbs Bolt action verb * Magazine gun barrel verbs * Add charger verbs * PDA verbs * Transfer amount verb * Add reagent verb * make alt-click use ECS verbs * Delete old verb files * Magboot verb * finalising tweaks * context menu visibility changes * code cleanup * Update AdminAddReagentUI.cs * Remove HasFlag * Consistent verb keys * Remove Linq, add comment * Fix in-inventory check * Update GUI text alignment and padding * Added close-menu option * Changed some "interaction" verbs to "activation" * Remove verb keys, use sorted sets * fix master merge * update some verb text * Undo Changes Remove some new verbs that can be added later undid some .ftl bugfixes, can and should be done separately * fix merge * Undo file rename * fix merge * Misc Cleanup * remove contraction * Fix keybinding issue * fix comment * merge fix * fix merge * fix merge * fix merge * fix merge * fix open-close verbs * adjust uncuff verb * fix merge and undo the renaming of SharedPullableComponent to PullableComponent. I'm tired of all of those merge conflicts
2021-10-05 14:29:03 +11:00
{
var target = GetEntity(ev.Target);
if (TryComp(target, out TransformComponent? xform))
TryPoint(args.SenderSession, xform.Coordinates, target);
else
Log.Warning($"User {args.SenderSession} attempted to point at a non-existent entity uid: {ev.Target}");
ECS verbs and update context menu (#4594) * Functioning ECS verbs Currently only ID card console works. * Changed verb types and allow ID card insertions * Verb GUI sorting and verb networking * More networking, and shared components * Clientside verbs work now. * Verb enums changed to bitmask flags * Verb Categories redo * Fix range check * GasTank Verb * Remove unnecessary bodypart verb * Buckle Verb * buckle & unbuckle verbs * Updated range checks * Item cabinet verbs * Add range user override * construction verb * Chemistry machine verbs * Climb Verb * Generalise pulled entity verbs * ViewVariables Verb * rejuvenate, delete, sentient, control verbs * Outfit verb * inrangeunoccluded and tubedirection verbs * attach-to verbs * remove unused verbs and move VV * Rename DebugVerbSystem * Ghost role and pointing verbs * Remove global verbs * Allow verbs to raise events * Changing categories and simplifying debug verbs * Add rotate and flip verbs * fix rejuvenate test * redo context menu * new Add Gas debug verb * Add Set Temperature debug verb * Uncuff verb * Disposal unit verbs * Add pickup verb * lock/unlock verb * Remove verb type, add specific verb events * rename verb messages -> events * Context menu displays verbs by interaction type * Updated context menu HandleMove previously, checked if entities moved 1 tile from click location. Now checks if entities moved out of view. Now you can actually right-click interact with yourself while walking! * Misc Verb menu GUI changes * Fix non-human/ghost verbs * Update types and categories * Allow non-ghost/human to open context menu * configuration verb * tagger verb * Morgue Verbs * Medical Scanner Verbs * Fix solution refactor merge issues * Fix context menu in-view check * Remove prepare GUI * Redo verb restrictions * Fix context menu UI * Disposal Verbs * Spill verb * Light verb * Hand Held light verb * power cell verbs * storage verbs and adding names to insert/eject * Pulling verb * Close context menu on verb execution * Strip verb * AmmoBox verb * fix pull verb * gun barrel verbs revolver verb energy weapon verbs Bolt action verb * Magazine gun barrel verbs * Add charger verbs * PDA verbs * Transfer amount verb * Add reagent verb * make alt-click use ECS verbs * Delete old verb files * Magboot verb * finalising tweaks * context menu visibility changes * code cleanup * Update AdminAddReagentUI.cs * Remove HasFlag * Consistent verb keys * Remove Linq, add comment * Fix in-inventory check * Update GUI text alignment and padding * Added close-menu option * Changed some "interaction" verbs to "activation" * Remove verb keys, use sorted sets * fix master merge * update some verb text * Undo Changes Remove some new verbs that can be added later undid some .ftl bugfixes, can and should be done separately * fix merge * Undo file rename * fix merge * Misc Cleanup * remove contraction * Fix keybinding issue * fix comment * merge fix * fix merge * fix merge * fix merge * fix merge * fix open-close verbs * adjust uncuff verb * fix merge and undo the renaming of SharedPullableComponent to PullableComponent. I'm tired of all of those merge conflicts
2021-10-05 14:29:03 +11:00
}
public override void Shutdown()
{
base.Shutdown();
_playerManager.PlayerStatusChanged -= OnPlayerStatusChanged;
_pointers.Clear();
}
public override void Update(float frameTime)
{
2022-09-17 00:37:15 +10:00
var currentTime = _gameTiming.CurTime;
var query = AllEntityQuery<PointingArrowComponent>();
while (query.MoveNext(out var uid, out var component))
{
Update((uid, component), currentTime);
}
}
2022-09-17 00:37:15 +10:00
private void Update(Entity<PointingArrowComponent> pointing, TimeSpan currentTime)
2022-09-17 00:37:15 +10:00
{
// TODO: That pause PR
var component = pointing.Comp;
2022-09-17 00:37:15 +10:00
if (component.EndTime > currentTime)
return;
if (component.Rogue)
{
RemComp<PointingArrowComponent>(pointing);
EnsureComp<RoguePointingArrowComponent>(pointing);
2022-09-17 00:37:15 +10:00
return;
}
Del(pointing);
2022-09-17 00:37:15 +10:00
}
}
}