]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Create DeviceNetworkJammerComponent & System as a general way for entities to act...
authornikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com>
Mon, 25 Mar 2024 01:59:16 +0000 (03:59 +0200)
committerGitHub <noreply@github.com>
Mon, 25 Mar 2024 01:59:16 +0000 (21:59 -0400)
* Add DeviceNetworkJammerComponent & System

Allows for entities to "jam" DeviceNetwork packets.

Whenever a device attempts to send a packet, the
DeviceNetworkJammerSystem listens for the BeforePacketSentEvent.
From there if any entity with the jammer component is within range of
either the sender or receiver of the packet the event will be cancelled.
Additionally jammers can only block packets in certain networks. If a
packet is not being transmitted in one of the networks it can block then
even if the jammer is in range the event will not be cancelled.

The range is stored in the jammer component along with the networks it
can jam.

Jammable network ids are stored as strings which seems to be how custom
networks are stored (E.g. network ids for suit sensors).

To allow for all of this, the BeforePacketSentEvent was modified to
provide the NetworkId.

* Make JammerSystem for the radio jammer use the DeviceNetworkJammer. Remove redundant event.

* Replace calls to TryDistance with InRange

Content.Server/DeviceNetwork/Systems/DeviceNetworkJammerSystem.cs [new file with mode: 0644]
Content.Server/DeviceNetwork/Systems/DeviceNetworkSystem.cs
Content.Server/DeviceNetwork/Systems/SingletonDeviceNetServerSystem.cs
Content.Server/Medical/SuitSensors/SuitSensorComponent.cs
Content.Server/Medical/SuitSensors/SuitSensorSystem.cs
Content.Server/Radio/EntitySystems/JammerSystem.cs
Content.Shared/DeviceNetwork/Components/DeviceNetworkJammerComponent.cs [new file with mode: 0644]

diff --git a/Content.Server/DeviceNetwork/Systems/DeviceNetworkJammerSystem.cs b/Content.Server/DeviceNetwork/Systems/DeviceNetworkJammerSystem.cs
new file mode 100644 (file)
index 0000000..3d38205
--- /dev/null
@@ -0,0 +1,38 @@
+using Content.Server.DeviceNetwork.Components;
+using Content.Shared.DeviceNetwork.Components;
+using Robust.Server.GameObjects;
+
+namespace Content.Server.DeviceNetwork.Systems;
+
+public sealed class DeviceNetworkJammerSystem : EntitySystem
+{
+    [Dependency] private TransformSystem _transform = default!;
+    public override void Initialize()
+    {
+        base.Initialize();
+
+        SubscribeLocalEvent<TransformComponent, BeforePacketSentEvent>(BeforePacketSent);
+    }
+
+    private void BeforePacketSent(EntityUid uid, TransformComponent xform, BeforePacketSentEvent ev)
+    {
+        if (ev.Cancelled)
+            return;
+
+        var query = EntityQueryEnumerator<DeviceNetworkJammerComponent, TransformComponent>();
+
+        while (query.MoveNext(out _, out var jammerComp, out var jammerXform))
+        {
+            if (!jammerComp.JammableNetworks.Contains(ev.NetworkId))
+                continue;
+
+            if (jammerXform.Coordinates.InRange(EntityManager, _transform, ev.SenderTransform.Coordinates, jammerComp.Range)
+                || jammerXform.Coordinates.InRange(EntityManager, _transform, xform.Coordinates, jammerComp.Range))
+            {
+                ev.Cancel();
+                return;
+            }
+        }
+    }
+
+}
index 83967c9bbd41437f454c1cd5038f98b114114b39..20ee7a5dd1bdfc48ca124cac8e49b49a6ab3b6b7 100644 (file)
@@ -351,13 +351,14 @@ namespace Content.Server.DeviceNetwork.Systems
 
             var xform = Transform(packet.Sender);
 
-            BeforePacketSentEvent beforeEv = new(packet.Sender, xform, _transformSystem.GetWorldPosition(xform));
+            var senderPos = _transformSystem.GetWorldPosition(xform);
 
             foreach (var connection in connections)
             {
                 if (connection.Owner == packet.Sender)
                     continue;
 
+                BeforePacketSentEvent beforeEv = new(packet.Sender, xform, senderPos, connection.NetIdEnum.ToString());
                 RaiseLocalEvent(connection.Owner, beforeEv, false);
 
                 if (!beforeEv.Cancelled)
@@ -386,11 +387,17 @@ namespace Content.Server.DeviceNetwork.Systems
         /// </summary>
         public readonly Vector2 SenderPosition;
 
-        public BeforePacketSentEvent(EntityUid sender, TransformComponent xform, Vector2 senderPosition)
+        /// <summary>
+        /// The network the packet will be sent to.
+        /// </summary>
+        public readonly string NetworkId;
+
+        public BeforePacketSentEvent(EntityUid sender, TransformComponent xform, Vector2 senderPosition, string networkId)
         {
             Sender = sender;
             SenderTransform = xform;
             SenderPosition = senderPosition;
+            NetworkId = networkId;
         }
     }
 
index fd62514d006bc3f35ceb2c5a2d63a6c159afd7ce..cdc083feacd1271f4b3f85ae918bac39b17d2a39 100644 (file)
@@ -1,4 +1,5 @@
-using Content.Server.DeviceNetwork.Components;
+using System.Diagnostics.CodeAnalysis;
+using Content.Server.DeviceNetwork.Components;
 using Content.Server.Medical.CrewMonitoring;
 using Content.Server.Power.Components;
 using Content.Server.Station.Systems;
@@ -38,7 +39,7 @@ public sealed class SingletonDeviceNetServerSystem : EntitySystem
     /// <param name="address">The address of the active server if it exists</param>
     /// <typeparam name="TComp">The component type that determines what type of server you're getting the address of</typeparam>
     /// <returns>True if there is an active serve. False otherwise</returns>
-    public bool TryGetActiveServerAddress<TComp>(EntityUid stationId, out string? address) where TComp : IComponent
+    public bool TryGetActiveServerAddress<TComp>(EntityUid stationId, [NotNullWhen(true)] out string? address) where TComp : IComponent
     {
         var servers = EntityQueryEnumerator<
             SingletonDeviceNetServerComponent,
index 8d75d3840afe3109b9f7f4f71ce0f55c3895b33a..9079655c80c15add2ed9a7dc0a628ccaf1aba5b8 100644 (file)
@@ -87,9 +87,3 @@ public sealed partial class SuitSensorComponent : Component
     [DataField, ViewVariables]
     public bool PreviousControlsLocked = false;
 }
-
-[ByRefEvent]
-public record struct SuitSensorsSendAttemptEvent
-{
-    public bool Cancelled;
-};
index f19b3d5b81407e77beb78398d3d7a8cd67d839ad..b807b63e21f6687cf2a4ab0f006e1bb3bde9f8ff 100644 (file)
@@ -73,11 +73,6 @@ public sealed class SuitSensorSystem : EntitySystem
             // TODO: This would cause imprecision at different tick rates.
             sensor.NextUpdate = curTime + sensor.UpdateRate;
 
-            var canEv = new SuitSensorsSendAttemptEvent();
-            RaiseLocalEvent(uid, ref canEv);
-            if (canEv.Cancelled)
-                continue;
-
             // get sensor status
             var status = GetSensorState(uid, sensor);
             if (status == null)
index fdf02f94df5cbde4e1471c748ee7f645c8d2e517..1258cc10fa0eed0fa687fc577a86305b8e04ab02 100644 (file)
@@ -1,8 +1,12 @@
-using Content.Server.Medical.SuitSensors;
+using Content.Server.DeviceNetwork.Components;
+using Content.Server.DeviceNetwork.Systems;
+using Content.Server.Medical.CrewMonitoring;
 using Content.Server.Popups;
 using Content.Server.Power.EntitySystems;
 using Content.Server.PowerCell;
 using Content.Server.Radio.Components;
+using Content.Server.Station.Systems;
+using Content.Shared.DeviceNetwork.Components;
 using Content.Shared.Examine;
 using Content.Shared.Interaction;
 using Content.Shared.PowerCell.Components;
@@ -15,6 +19,8 @@ public sealed class JammerSystem : EntitySystem
     [Dependency] private readonly BatterySystem _battery = default!;
     [Dependency] private readonly PopupSystem _popup = default!;
     [Dependency] private readonly SharedTransformSystem _transform = default!;
+    [Dependency] private readonly StationSystem _stationSystem = default!;
+    [Dependency] private readonly SingletonDeviceNetServerSystem _singletonServerSystem = default!;
 
     public override void Initialize()
     {
@@ -24,7 +30,6 @@ public sealed class JammerSystem : EntitySystem
         SubscribeLocalEvent<ActiveRadioJammerComponent, PowerCellChangedEvent>(OnPowerCellChanged);
         SubscribeLocalEvent<RadioJammerComponent, ExaminedEvent>(OnExamine);
         SubscribeLocalEvent<RadioSendAttemptEvent>(OnRadioSendAttempt);
-        SubscribeLocalEvent<SuitSensorComponent, SuitSensorsSendAttemptEvent>(OnSensorSendAttempt);
     }
 
     public override void Update(float frameTime)
@@ -36,6 +41,7 @@ public sealed class JammerSystem : EntitySystem
                 !_battery.TryUseCharge(batteryUid.Value, jam.Wattage * frameTime, battery))
             {
                 RemComp<ActiveRadioJammerComponent>(uid);
+                RemComp<DeviceNetworkJammerComponent>(uid);
             }
         }
     }
@@ -48,10 +54,19 @@ public sealed class JammerSystem : EntitySystem
         if (activated)
         {
             EnsureComp<ActiveRadioJammerComponent>(uid);
+            var stationId = _stationSystem.GetOwningStation(uid);
+            if (stationId != null && _singletonServerSystem.TryGetActiveServerAddress<CrewMonitoringServerComponent>(stationId.Value, out var netId))
+            {
+                EnsureComp<DeviceNetworkJammerComponent>(uid, out var jammingComp);
+                jammingComp.Range = comp.Range;
+                jammingComp.JammableNetworks.Add(netId);
+                Dirty(uid, jammingComp);
+            }
         }
         else
         {
             RemComp<ActiveRadioJammerComponent>(uid);
+            RemComp<DeviceNetworkJammerComponent>(uid);
         }
         var state = Loc.GetString(activated ? "radio-jammer-component-on-state" : "radio-jammer-component-off-state");
         var message = Loc.GetString("radio-jammer-component-on-use", ("state", state));
@@ -84,14 +99,6 @@ public sealed class JammerSystem : EntitySystem
         }
     }
 
-    private void OnSensorSendAttempt(EntityUid uid, SuitSensorComponent comp, ref SuitSensorsSendAttemptEvent args)
-    {
-        if (ShouldCancelSend(uid))
-        {
-            args.Cancelled = true;
-        }
-    }
-
     private bool ShouldCancelSend(EntityUid sourceUid)
     {
         var source = Transform(sourceUid).Coordinates;
diff --git a/Content.Shared/DeviceNetwork/Components/DeviceNetworkJammerComponent.cs b/Content.Shared/DeviceNetwork/Components/DeviceNetworkJammerComponent.cs
new file mode 100644 (file)
index 0000000..75de0cb
--- /dev/null
@@ -0,0 +1,24 @@
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.DeviceNetwork.Components;
+
+/// <summary>
+/// Allow entities to jam DeviceNetwork packets.
+/// </summary>
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class DeviceNetworkJammerComponent : Component
+{
+    /// <summary>
+    /// Range where packets will be jammed. This is checked both against the sender and receiver.
+    /// </summary>
+    [DataField, AutoNetworkedField]
+    public float Range = 5.0f;
+
+    /// <summary>
+    /// Device networks that can be jammed. For a list of default NetworkIds see DeviceNetIdDefaults on Content.Server.
+    /// Network ids are not guaranteed to be limited to DeviceNetIdDefaults.
+    /// </summary>
+    [DataField, AutoNetworkedField]
+    public HashSet<string> JammableNetworks = [];
+
+}