mirror of
https://github.com/space-syndicate/space-station-14.git
synced 2026-10-11 19:31:12 +03:00
Signed-off-by: Prole <172158352+Prole0@users.noreply.github.com> Co-authored-by: PJBot <pieterjan.briers+bot@gmail.com> Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> Co-authored-by: Samuka-C <47865393+Samuka-C@users.noreply.github.com> Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com> Co-authored-by: Partmedia <kevinz5000@gmail.com> Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com> Co-authored-by: themias <89101928+themias@users.noreply.github.com> Co-authored-by: Victor Shen <71985089+Vexerot@users.noreply.github.com> Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com> Co-authored-by: Milon <milonpl.git@proton.me> Co-authored-by: Kirus59 <145689588+Kirus59@users.noreply.github.com> Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com> Co-authored-by: Stomf <5dorkydorks@gmail.com> Co-authored-by: drakewill-CRL <46307022+drakewill-CRL@users.noreply.github.com> Co-authored-by: PraxisMapper <praxismapper@gmail.com> Co-authored-by: EmoGarbage404 <retron404@gmail.com> Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com> Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com> Co-authored-by: IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com> Co-authored-by: TytosB <54259736+TytosB@users.noreply.github.com> Co-authored-by: abadaba695 <spacestation13thingy@gmail.com> Co-authored-by: kosticia <kosticia46@gmail.com> Co-authored-by: Thinbug <101073555+Thinbug0@users.noreply.github.com> Co-authored-by: pathetic meowmeow <uhhadd@gmail.com> Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com> Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com> Co-authored-by: ActiveMammmoth <140334666+ActiveMammmoth@users.noreply.github.com> Co-authored-by: Myra <vasilis@pikachu.systems> Co-authored-by: Whatstone <166147148+whatston3@users.noreply.github.com> Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com> Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com> Co-authored-by: Gentleman-Bird <dcgreen406@gmail.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: BIGZi0348 <svalker0348@gmail.com> Co-authored-by: LaCumbiaDelCoronavirus <90893484+LaCumbiaDelCoronavirus@users.noreply.github.com> Co-authored-by: imatsoup <93290208+imatsoup@users.noreply.github.com> Co-authored-by: Matthew Herber <32679887+happyrobot33@users.noreply.github.com> Co-authored-by: Ertanic <36124833+Ertanic@users.noreply.github.com> Co-authored-by: MissKay1994 <15877268+MissKay1994@users.noreply.github.com> Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com> Co-authored-by: eoineoineoin <helloworld@eoinrul.es> Co-authored-by: Tiniest Shark <head.rebel@yahoo.com> Co-authored-by: nikitosych <boriszyn@gmail.com> Co-authored-by: Tayrtahn <tayrtahn@gmail.com> Co-authored-by: Perry Fraser <perryprog@users.noreply.github.com> Co-authored-by: YoungThug <ramialanbagy@gmail.com> Co-authored-by: beck-thompson <107373427+beck-thompson@users.noreply.github.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com> Co-authored-by: Southbridge <7013162+southbridge-fur@users.noreply.github.com> Co-authored-by: Vladislav Suchkov <20380250+murolem@users.noreply.github.com> Co-authored-by: Prole <172158352+Prole0@users.noreply.github.com> Co-authored-by: Unkn0wn_Gh0st <shadowstalkermll@gmail.com> Co-authored-by: 3nderall <101940324+3nderall@users.noreply.github.com> Co-authored-by: Radezolid <snappednexus@gmail.com> Co-authored-by: J <billsmith116@gmail.com> Co-authored-by: Ghagliiarghii <68826635+Ghagliiarghii@users.noreply.github.com> Co-authored-by: chromiumboy <50505512+chromiumboy@users.noreply.github.com> Co-authored-by: youtissoum <51883137+youtissoum@users.noreply.github.com> Co-authored-by: Minemoder5000 <minemoder50000@gmail.com> Co-authored-by: Spanky <scott@wearejacob.com> Co-authored-by: Spessmann <156740760+Spessmann@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: brainfood1183 <113240905+brainfood1183@users.noreply.github.com> Co-authored-by: Deerstop <edainturner@gmail.com> Co-authored-by: B_Kirill <153602297+B-Kirill@users.noreply.github.com> Co-authored-by: archee1 <archee3@hotmail.co.uk> Co-authored-by: Cojoke <83733158+Cojoke-dot@users.noreply.github.com> Co-authored-by: Quantum-cross <7065792+Quantum-cross@users.noreply.github.com> Co-authored-by: poklj <compgeek223@gmail.com> Co-authored-by: Krunklehorn <42424291+Krunklehorn@users.noreply.github.com> Co-authored-by: OnyxTheBrave <131422822+OnyxTheBrave@users.noreply.github.com> Co-authored-by: UpAndLeaves <92269094+Alpha-Two@users.noreply.github.com> Co-authored-by: Flareguy <78941145+Flareguy@users.noreply.github.com> Co-authored-by: Zalycon <84675130+Zalycon@users.noreply.github.com> Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com> Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com> Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com> Co-authored-by: ScarKy0 <scarky0@onet.eu> Co-authored-by: Dmitry <57028746+dimm00n@users.noreply.github.com>
373 lines
15 KiB
C#
373 lines
15 KiB
C#
using System.Linq;
|
|
using Content.Server.Administration.Logs;
|
|
using Content.Server.Pointing.Components;
|
|
using Content.Shared.CCVar;
|
|
using Content.Shared.Database;
|
|
using Content.Shared.Examine;
|
|
using Content.Shared.Eye;
|
|
using Content.Shared.Ghost;
|
|
using Content.Shared.IdentityManagement;
|
|
using Content.Shared.Input;
|
|
using Content.Shared.Interaction;
|
|
using Content.Shared.Inventory;
|
|
using Content.Shared.Mind;
|
|
using Content.Shared.Pointing;
|
|
using Content.Shared.Popups;
|
|
using JetBrains.Annotations;
|
|
using Robust.Server.GameObjects;
|
|
using Robust.Server.Player;
|
|
using Robust.Shared.Configuration;
|
|
using Robust.Shared.Containers;
|
|
using Robust.Shared.Enums;
|
|
using Robust.Shared.GameStates;
|
|
using Robust.Shared.Input.Binding;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Player;
|
|
using Robust.Shared.Replays;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Content.Server.Pointing.EntitySystems
|
|
{
|
|
[UsedImplicitly]
|
|
internal sealed class PointingSystem : SharedPointingSystem
|
|
{
|
|
[Dependency] private readonly IConfigurationManager _config = default!;
|
|
[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!;
|
|
[Dependency] private readonly RotateToFaceSystem _rotateToFaceSystem = default!;
|
|
[Dependency] private readonly SharedContainerSystem _container = default!;
|
|
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
|
[Dependency] private readonly VisibilitySystem _visibilitySystem = default!;
|
|
[Dependency] private readonly SharedMindSystem _minds = default!;
|
|
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
|
[Dependency] private readonly SharedMapSystem _map = default!;
|
|
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
|
[Dependency] private readonly ExamineSystemShared _examine = default!;
|
|
|
|
private 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 GetCompState(Entity<PointingArrowComponent> entity, ref ComponentGetState args)
|
|
{
|
|
args.State = new SharedPointingArrowComponentState
|
|
{
|
|
StartPosition = entity.Comp.StartPosition,
|
|
EndTime = entity.Comp.EndTime
|
|
};
|
|
}
|
|
|
|
private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
|
|
{
|
|
if (e.NewStatus != SessionStatus.Disconnected)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_pointers.Remove(e.Session);
|
|
}
|
|
|
|
// TODO: FOV
|
|
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)
|
|
{
|
|
if (viewer.AttachedEntity is not {Valid: true} viewerEntity)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var message = viewerEntity == source
|
|
? selfMessage
|
|
: viewerEntity == pointed && viewerPointedAtMessage != null
|
|
? viewerPointedAtMessage
|
|
: viewerMessage;
|
|
|
|
// Someone pointing at YOU is slightly more important
|
|
var popupType = viewerEntity == pointed ? PopupType.Medium : PopupType.Small;
|
|
|
|
RaiseNetworkEvent(new PopupEntityEvent(message, popupType, netSource), viewerEntity);
|
|
}
|
|
|
|
_replay.RecordServerMessage(new PopupEntityEvent(viewerMessage, PopupType.Small, netSource));
|
|
}
|
|
|
|
public bool InRange(EntityUid pointer, EntityCoordinates coordinates)
|
|
{
|
|
if (HasComp<GhostComponent>(pointer))
|
|
{
|
|
return _transform.InRange(Transform(pointer).Coordinates, coordinates, 15);
|
|
}
|
|
else
|
|
{
|
|
return _examine.InRangeUnOccluded(pointer, coordinates, 15, predicate: e => e == pointer);
|
|
}
|
|
}
|
|
|
|
public bool TryPoint(ICommonSession? session, EntityCoordinates coordsPointed, EntityUid pointed)
|
|
{
|
|
if (session?.AttachedEntity is not { } player)
|
|
{
|
|
Log.Warning($"Player {session} attempted to point without any attached entity");
|
|
return false;
|
|
}
|
|
|
|
if (!coordsPointed.IsValid(EntityManager))
|
|
{
|
|
Log.Warning($"Player {ToPrettyString(player)} attempted to point at invalid coordinates: {coordsPointed}");
|
|
return false;
|
|
}
|
|
|
|
if (_pointers.TryGetValue(session, out var lastTime) &&
|
|
_gameTiming.CurTime < lastTime + _pointDelay)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (HasComp<PointingArrowComponent>(pointed))
|
|
{
|
|
// this is a pointing arrow. no pointing here...
|
|
return false;
|
|
}
|
|
|
|
if (!CanPoint(player))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!InRange(player, coordsPointed))
|
|
{
|
|
_popup.PopupEntity(Loc.GetString("pointing-system-try-point-cannot-reach"), player, player);
|
|
return false;
|
|
}
|
|
var mapCoordsPointed = _transform.ToMapCoordinates(coordsPointed);
|
|
_rotateToFaceSystem.TryFaceCoordinates(player, mapCoordsPointed.Position);
|
|
|
|
var arrow = EntityManager.SpawnEntity("PointingArrow", coordsPointed);
|
|
|
|
if (TryComp<PointingArrowComponent>(arrow, out var pointing))
|
|
{
|
|
pointing.StartPosition = _transform.ToCoordinates((arrow, Transform(arrow)), _transform.ToMapCoordinates(Transform(player).Coordinates)).Position;
|
|
pointing.EndTime = _gameTiming.CurTime + PointDuration;
|
|
|
|
Dirty(arrow, pointing);
|
|
}
|
|
|
|
if (EntityQuery<PointingArrowAngeringComponent>().FirstOrDefault() != null)
|
|
{
|
|
if (TryComp<PointingArrowComponent>(arrow, out var pointingArrowComponent))
|
|
{
|
|
pointingArrowComponent.Rogue = true;
|
|
}
|
|
}
|
|
|
|
var layer = (int) VisibilityFlags.Normal;
|
|
if (TryComp(player, out VisibilityComponent? playerVisibility))
|
|
{
|
|
var arrowVisibility = EntityManager.EnsureComponent<VisibilityComponent>(arrow);
|
|
layer = playerVisibility.Layer;
|
|
_visibilitySystem.SetLayer((arrow, arrowVisibility), (ushort) 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 ||
|
|
!TryComp(ent, out EyeComponent? eyeComp) ||
|
|
(eyeComp.VisibilityMask & layer) == 0)
|
|
return false;
|
|
|
|
return _transform.GetMapCoordinates(ent).InRange(_transform.GetMapCoordinates(player), PointingRange);
|
|
}
|
|
|
|
var viewers = Filter.Empty()
|
|
.AddWhere(session1 => ViewerPredicate(session1))
|
|
.Recipients;
|
|
|
|
string selfMessage;
|
|
string viewerMessage;
|
|
string? viewerPointedAtMessage = null;
|
|
var playerName = Identity.Entity(player, EntityManager);
|
|
|
|
if (Exists(pointed))
|
|
{
|
|
var pointedName = Identity.Entity(pointed, EntityManager);
|
|
|
|
EntityUid? containingInventory = null;
|
|
// Search up through the target's containing containers until we find an inventory
|
|
var inventoryQuery = GetEntityQuery<InventoryComponent>();
|
|
foreach (var container in _container.GetContainingContainers(pointed))
|
|
{
|
|
if (inventoryQuery.HasComp(container.Owner))
|
|
{
|
|
// We want the innermost inventory, since that's the "owner" of the item
|
|
containingInventory = container.Owner;
|
|
break;
|
|
}
|
|
}
|
|
|
|
var pointingAtSelf = player == pointed;
|
|
|
|
// Are we in a mob's inventory?
|
|
if (containingInventory != null)
|
|
{
|
|
var item = pointed;
|
|
var itemName = Identity.Entity(item, EntityManager);
|
|
|
|
// Target the pointing at the item's holder
|
|
pointed = containingInventory.Value;
|
|
pointedName = Identity.Entity(pointed, EntityManager);
|
|
var pointingAtOwnItem = player == pointed;
|
|
|
|
if (pointingAtOwnItem)
|
|
{
|
|
// You point at your item
|
|
selfMessage = Loc.GetString("pointing-system-point-in-own-inventory-self", ("item", itemName));
|
|
// Urist McPointer points at his item
|
|
viewerMessage = Loc.GetString("pointing-system-point-in-own-inventory-others", ("item", itemName), ("pointer", playerName));
|
|
}
|
|
else
|
|
{
|
|
// You point at Urist McHands' item
|
|
selfMessage = Loc.GetString("pointing-system-point-in-other-inventory-self", ("item", itemName), ("wearer", pointedName));
|
|
// Urist McPointer points at Urist McWearer's item
|
|
viewerMessage = Loc.GetString("pointing-system-point-in-other-inventory-others", ("item", itemName), ("pointer", playerName), ("wearer", pointedName));
|
|
// Urist McPointer points at your item
|
|
viewerPointedAtMessage = Loc.GetString("pointing-system-point-in-other-inventory-target", ("item", itemName), ("pointer", playerName));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
selfMessage = pointingAtSelf
|
|
// You point at yourself
|
|
? Loc.GetString("pointing-system-point-at-self")
|
|
// You point at Urist McTarget
|
|
: Loc.GetString("pointing-system-point-at-other", ("other", pointedName));
|
|
|
|
viewerMessage = pointingAtSelf
|
|
// Urist McPointer points at himself
|
|
? Loc.GetString("pointing-system-point-at-self-others", ("otherName", playerName), ("other", playerName))
|
|
// Urist McPointer points at Urist McTarget
|
|
: Loc.GetString("pointing-system-point-at-other-others", ("otherName", playerName), ("other", pointedName));
|
|
|
|
// Urist McPointer points at you
|
|
viewerPointedAtMessage = Loc.GetString("pointing-system-point-at-you-other", ("otherName", playerName));
|
|
}
|
|
|
|
var ev = new AfterPointedAtEvent(pointed);
|
|
RaiseLocalEvent(player, ref ev);
|
|
var gotev = new AfterGotPointedAtEvent(player);
|
|
RaiseLocalEvent(pointed, ref gotev);
|
|
|
|
_adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(player):user} pointed at {ToPrettyString(pointed):target} {Transform(pointed).Coordinates}");
|
|
}
|
|
else
|
|
{
|
|
TileRef? tileRef = null;
|
|
string? position = null;
|
|
|
|
if (_mapManager.TryFindGridAt(mapCoordsPointed, out var gridUid, out var grid))
|
|
{
|
|
position = $"EntId={gridUid} {_map.WorldToTile(gridUid, grid, mapCoordsPointed.Position)}";
|
|
tileRef = _map.GetTileRef(gridUid, grid, _map.WorldToTile(gridUid, grid, mapCoordsPointed.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));
|
|
|
|
_adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(player):user} pointed at {name} {(position == null ? mapCoordsPointed : position)}");
|
|
}
|
|
|
|
_pointers[session] = _gameTiming.CurTime;
|
|
|
|
SendMessage(player, viewers, pointed, selfMessage, viewerMessage, viewerPointedAtMessage);
|
|
|
|
return true;
|
|
}
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SubscribeLocalEvent<PointingArrowComponent, ComponentGetState>(GetCompState);
|
|
|
|
SubscribeNetworkEvent<PointingAttemptEvent>(OnPointAttempt);
|
|
|
|
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
|
|
|
|
CommandBinds.Builder
|
|
.Bind(ContentKeyFunctions.Point, new PointerInputCmdHandler(TryPoint))
|
|
.Register<PointingSystem>();
|
|
|
|
Subs.CVar(_config, CCVars.PointingCooldownSeconds, v => _pointDelay = TimeSpan.FromSeconds(v), true);
|
|
}
|
|
|
|
private void OnPointAttempt(PointingAttemptEvent ev, EntitySessionEventArgs args)
|
|
{
|
|
var target = GetEntity(ev.Target);
|
|
|
|
if (TryComp(target, out TransformComponent? xformTarget))
|
|
TryPoint(args.SenderSession, xformTarget.Coordinates, target);
|
|
else
|
|
Log.Warning($"User {args.SenderSession} attempted to point at a non-existent entity uid: {ev.Target}");
|
|
}
|
|
|
|
public override void Shutdown()
|
|
{
|
|
base.Shutdown();
|
|
|
|
_playerManager.PlayerStatusChanged -= OnPlayerStatusChanged;
|
|
_pointers.Clear();
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
var currentTime = _gameTiming.CurTime;
|
|
|
|
var query = AllEntityQuery<PointingArrowComponent>();
|
|
while (query.MoveNext(out var uid, out var component))
|
|
{
|
|
Update((uid, component), currentTime);
|
|
}
|
|
}
|
|
|
|
private void Update(Entity<PointingArrowComponent> pointing, TimeSpan currentTime)
|
|
{
|
|
// TODO: That pause PR
|
|
var component = pointing.Comp;
|
|
if (component.EndTime > currentTime)
|
|
return;
|
|
|
|
if (component.Rogue)
|
|
{
|
|
RemComp<PointingArrowComponent>(pointing);
|
|
EnsureComp<RoguePointingArrowComponent>(pointing);
|
|
return;
|
|
}
|
|
|
|
Del(pointing);
|
|
}
|
|
}
|
|
}
|