Files
tbd-station-14/Content.Server/Medical/SuitSensors/SuitSensorSystem.cs
kosticia 9ecb8333f2 Predict suitsensor system (#39325)
* adwadsdwasadwas

* dev

* fix

* review

* some more cleanup

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
2025-08-11 17:35:11 +02:00

92 lines
3.1 KiB
C#

using Content.Server.DeviceNetwork.Systems;
using Content.Server.Emp;
using Content.Server.Medical.CrewMonitoring;
using Content.Shared.DeviceNetwork.Components;
using Content.Shared.Medical.SuitSensor;
using Content.Shared.Medical.SuitSensors;
using Robust.Shared.Timing;
namespace Content.Server.Medical.SuitSensors;
public sealed class SuitSensorSystem : SharedSuitSensorSystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
[Dependency] private readonly SingletonDeviceNetServerSystem _singletonServerSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SuitSensorComponent, EmpPulseEvent>(OnEmpPulse);
SubscribeLocalEvent<SuitSensorComponent, EmpDisabledRemoved>(OnEmpFinished);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var curTime = _gameTiming.CurTime;
var sensors = EntityQueryEnumerator<SuitSensorComponent, DeviceNetworkComponent>();
while (sensors.MoveNext(out var uid, out var sensor, out var device))
{
if (device.TransmitFrequency is null)
continue;
// check if sensor is ready to update
if (curTime < sensor.NextUpdate)
continue;
if (!CheckSensorAssignedStation((uid, sensor)))
continue;
sensor.NextUpdate += sensor.UpdateRate;
// get sensor status
var status = GetSensorState((uid, sensor));
if (status == null)
continue;
//Retrieve active server address if the sensor isn't connected to a server
if (sensor.ConnectedServer == null)
{
if (!_singletonServerSystem.TryGetActiveServerAddress<CrewMonitoringServerComponent>(sensor.StationId!.Value, out var address))
continue;
sensor.ConnectedServer = address;
}
// Send it to the connected server
var payload = SuitSensorToPacket(status);
// Clear the connected server if its address isn't on the network
if (!_deviceNetworkSystem.IsAddressPresent(device.DeviceNetId, sensor.ConnectedServer))
{
sensor.ConnectedServer = null;
continue;
}
_deviceNetworkSystem.QueuePacket(uid, sensor.ConnectedServer, payload, device: device);
}
}
private void OnEmpPulse(Entity<SuitSensorComponent> ent, ref EmpPulseEvent args)
{
args.Affected = true;
args.Disabled = true;
ent.Comp.PreviousMode = ent.Comp.Mode;
SetSensor(ent.AsNullable(), SuitSensorMode.SensorOff, null);
ent.Comp.PreviousControlsLocked = ent.Comp.ControlsLocked;
ent.Comp.ControlsLocked = true;
}
private void OnEmpFinished(Entity<SuitSensorComponent> ent, ref EmpDisabledRemoved args)
{
SetSensor(ent.AsNullable(), ent.Comp.PreviousMode, null);
ent.Comp.ControlsLocked = ent.Comp.PreviousControlsLocked;
}
}