Skip to content

Commit

Permalink
Mirror: Create DeviceNetworkJammerComponent & System as a general way…
Browse files Browse the repository at this point in the history
… for entities to act as jammers (#382)

## Mirror of PR #26342: [Create DeviceNetworkJammerComponent & System as
a general way for entities to act as
jammers](space-wizards/space-station-14#26342)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `266cc85f57c883b3a604a66da91d94bb1e18ec5d`

PR opened by <img
src="https://avatars.githubusercontent.com/u/32041239?v=4"
width="16"/><a href="https://github.com/nikthechampiongr">
nikthechampiongr</a> at 2024-03-22 19:17:23 UTC

---

PR changed 7 files with 91 additions and 25 deletions.

The PR had the following labels:
- Status: Needs Review


---

<details open="true"><summary><h1>Original Body</h1></summary>

> <!-- Please read these guidelines before opening your PR:
https://docs.spacestation14.io/en/getting-started/pr-guideline -->
> <!-- The text between the arrows are comments - they will not be
visible on your PR. -->
> 
> ## About the PR
> <!-- What did you change in this PR? -->
> This PR creates the DeviceNetworkJammerComponent & System as a general
way for entities to act as jammers for the DeviceNetwork.
> 
> Additionally it adjusts the JammerSystem for the radio jammer to use
this, and rips out the previous special code for jamming suit sensors.
> 
> As a result of this, the radio jammer can now additionally jam the
suit sensor server itself from receiving DeviceNetwork packets.
> 
> ## Why / Balance
> <!-- Why was it changed? Link any discussions or issues here. Please
discuss how this would affect game balance. -->
> This pr was requested by a maintainer.
> 
> Additionally a general jamming system can have some utility in the
future. E.g. an artifact effect.
> ## Technical details
> <!-- If this is a code change, summarize at high level how your new
code works. This makes it easier to review. -->
> 
> 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.
> 
> The JammerSystem now needs to get the networkid for suit sensors for
use in the DeviceNetwork. The previous event subscription for jamming
suit sensors have been removed along with the event.
> 
> I wish I could use this to jam radios as well but radios are magic and
do not use device networks.
> 
> ## Media
> <!-- 
> PRs which make ingame changes (adding clothing, items, new features,
etc) are required to have media attached that showcase the changes.
> Small fixes/refactors are exempt.
> Any media may be used in SS14 progress reports, with clear credit
given.
> 
> If you're unsure whether your PR will require media, ask a maintainer.
> 
> Check the box below to confirm that you have in fact seen this (put an
X in the brackets, like [X]):
> -->
> 
> - [x] I have added screenshots/videos to this PR showcasing its
changes ingame, **or** this PR does not require an ingame showcase
> 
> **Changelog**
> <!--
> Make players aware of new features and changes that could affect how
they play the game by adding a Changelog entry. Please read the
Changelog guidelines located at:
https://docs.spacestation14.io/en/getting-started/pr-guideline#changelog
> -->
> 
> <!--
> Make sure to take this Changelog template out of the comment block in
order for it to show up.
> 🆑
> - add: Added fun!
> - remove: Removed fun!
> - tweak: Changed fun!
> - fix: Fixed fun!
> -->
> no cl no fun


</details>

Signed-off-by: VMSolidus <[email protected]>
Co-authored-by: SimpleStation14 <Unknown>
Co-authored-by: VMSolidus <[email protected]>
  • Loading branch information
SimpleStation14 and VMSolidus authored May 29, 2024
1 parent 6ca11bd commit 2fb4c5e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 4 deletions.
38 changes: 38 additions & 0 deletions Content.Server/DeviceNetwork/Systems/DeviceNetworkJammerSystem.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

}
11 changes: 9 additions & 2 deletions Content.Server/DeviceNetwork/Systems/DeviceNetworkSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,14 @@ private void SendToConnections(ReadOnlySpan<DeviceNetworkComponent> connections,

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)
Expand Down Expand Up @@ -386,11 +387,17 @@ public sealed class BeforePacketSentEvent : CancellableEntityEventArgs
/// </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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,7 +39,7 @@ public bool IsActiveServer(EntityUid serverId, SingletonDeviceNetServerComponent
/// <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,
Expand Down
17 changes: 17 additions & 0 deletions Content.Server/Radio/EntitySystems/JammerSystem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using Content.Server.DeviceNetwork.Components;
using Content.Server.DeviceNetwork.Systems;
using Content.Server.Medical.CrewMonitoring;
using Content.Server.Medical.SuitSensors;
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;
Expand All @@ -15,6 +20,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()
{
Expand All @@ -36,6 +43,7 @@ public override void Update(float frameTime)
!_battery.TryUseCharge(batteryUid.Value, jam.Wattage * frameTime, battery))
{
RemComp<ActiveRadioJammerComponent>(uid);
RemComp<DeviceNetworkJammerComponent>(uid);
}
}
}
Expand All @@ -48,10 +56,19 @@ private void OnActivate(EntityUid uid, RadioJammerComponent comp, ActivateInWorl
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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = [];

}

0 comments on commit 2fb4c5e

Please sign in to comment.