From 2097c031f8a132962e75bf242382abe680736f43 Mon Sep 17 00:00:00 2001 From: Simon <63975668+Simyon264@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:27:20 +0200 Subject: [PATCH] Fix radar --- .../Components/SuspicionRadarComponent.cs | 6 +- Content.Server/_SSS/SuspicionRadarSystem.cs | 92 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 Content.Server/_SSS/SuspicionRadarSystem.cs diff --git a/Content.Server/_SSS/SuspicionGameRule/Components/SuspicionRadarComponent.cs b/Content.Server/_SSS/SuspicionGameRule/Components/SuspicionRadarComponent.cs index 7534329a0d..ab54b20b35 100644 --- a/Content.Server/_SSS/SuspicionGameRule/Components/SuspicionRadarComponent.cs +++ b/Content.Server/_SSS/SuspicionGameRule/Components/SuspicionRadarComponent.cs @@ -1,4 +1,8 @@ namespace Content.Server._SSS.SuspicionGameRule.Components; [RegisterComponent] -public sealed partial class SuspicionRadarComponent : Component; +public sealed partial class SuspicionRadarComponent : Component +{ + [DataField] + public bool ShowTraitors { get; set; } = true; +} diff --git a/Content.Server/_SSS/SuspicionRadarSystem.cs b/Content.Server/_SSS/SuspicionRadarSystem.cs new file mode 100644 index 0000000000..33709b0737 --- /dev/null +++ b/Content.Server/_SSS/SuspicionRadarSystem.cs @@ -0,0 +1,92 @@ +using Content.Server._SSS.SuspicionGameRule.Components; +using Content.Server.Mind; +using Content.Server.Roles; +using Content.Shared._SSS.RadarOverlay; +using Content.Shared.Implants.Components; +using Content.Shared.Mobs; +using Content.Shared.Mobs.Systems; +using Robust.Server.GameObjects; +using Robust.Server.Player; + +namespace Content.Server._SSS; + +public sealed class SuspicionRadarSystem : EntitySystem +{ + [Dependency] private readonly RoleSystem _roleSystem = default!; + [Dependency] private readonly MindSystem _mindSystem = default!; + [Dependency] private readonly MobStateSystem _mobStateSystem = default!; + [Dependency] private readonly TransformSystem _transformSystem = default!; + [Dependency] private readonly IPlayerManager _playerManager = default!; + + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnPing); + SubscribeLocalEvent(OnMobStateChanged); + } + + private void OnMobStateChanged(MobStateChangedEvent ev) + { + if (ev.NewMobState is not (MobState.Dead or MobState.Critical)) + return; + + if (!_playerManager.TryGetSessionByEntity(ev.Target, out var session)) + return; + + RaiseNetworkEvent(new OnRadarOverlayToggledEvent(false), session); + } + + private void OnPing(EntityUid implantUid, SubdermalImplantComponent implant, PingRadarImplantEvent args) + { + var players = EntityQueryEnumerator(); + var radarInfo = new List(); + var uid = implant.ImplantedEntity; + if (uid == null) + { + Log.Error($"Implant {implantUid} has no implanted entity."); // how + return; + } + + if (!_playerManager.TryGetSessionByEntity(uid.Value, out var session)) + { + Log.Error($"Failed to locate player session for radar ping."); + return; + } + + var component = Comp(implantUid); + + RaiseNetworkEvent(new OnRadarOverlayToggledEvent(true), session); + + while (players.MoveNext(out var player, out var playerComponent)) + { + if (player == uid) + continue; + + if (_mobStateSystem.IsDead(player)) + continue; + + var mind = _mindSystem.GetMind(player); + if (mind == null) + continue; + + if (!_roleSystem.MindHasRole(mind.Value, out var role)) + continue; + + var roleComp = role.Value.Comp2; + var position = _transformSystem.GetWorldPosition(player); + + var color = roleComp.Role switch + { + SuspicionRole.Traitor when component.ShowTraitors => Color.Red, + SuspicionRole.Detective => Color.Blue, + _ => Color.Green + }; + + radarInfo.Add(new RadarInfo(color, position)); + } + + RaiseNetworkEvent(new RadarOverlayUpdatedEvent(radarInfo.ToArray()), session); + args.Handled = true; + } +}