Files
ss14-wega/Content.Server/DeviceLinking/Systems/PowerSensorSystem.cs
T
Eveloop 22a547c7c8 Use dependency injection for EntityQuery<T>s in Content.Server (#43566)
* Replace usages of EntityQuery with dependency injected ones in Content.Server

* Remove unused EntityQuery dependencies

* Restore CheckPressureAndFire override method

* Revert EntityQuery refactor changes to DisposalTubeSystem.cs & DisposableSystem.cs

* Infer Transform/Metadata directly instead of passing (#43479)

* Resolve RA0049, RA0051 errors (#43479)

* Resolve RA0049, RA0051 errors (#43479)

* Apply suggestions from code review

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
2026-05-17 15:59:37 +00:00

124 lines
4.4 KiB
C#

using Content.Server.DeviceLinking.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.Power.Nodes;
using Content.Server.Power.NodeGroups;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.NodeContainer;
using Content.Shared.Popups;
using Content.Shared.Power.Generator;
using Content.Shared.Timing;
using Content.Shared.Tools.Systems;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Map.Components;
using Robust.Shared.Timing;
namespace Content.Server.DeviceLinking.Systems;
public sealed partial class PowerSensorSystem : EntitySystem
{
[Dependency] private DeviceLinkSystem _deviceLink = default!;
[Dependency] private IGameTiming _timing = default!;
[Dependency] private PowerNetSystem _powerNet = default!;
[Dependency] private SharedAudioSystem _audio = default!;
[Dependency] private SharedPopupSystem _popup = default!;
[Dependency] private SharedToolSystem _tool = default!;
[Dependency] private UseDelaySystem _useDelay = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PowerSensorComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<PowerSensorComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<PowerSensorComponent, InteractUsingEvent>(OnInteractUsing);
}
public override void Update(float deltaTime)
{
var query = EntityQueryEnumerator<PowerSensorComponent>();
while (query.MoveNext(out var uid, out var comp))
{
var now = _timing.CurTime;
if (comp.NextCheck > now)
continue;
comp.NextCheck = now + comp.CheckDelay;
UpdateOutputs(uid, comp);
}
}
private void OnInit(EntityUid uid, PowerSensorComponent comp, ComponentInit args)
{
_deviceLink.EnsureSourcePorts(uid, comp.ChargingPort, comp.DischargingPort);
}
private void OnExamined(EntityUid uid, PowerSensorComponent comp, ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
args.PushMarkup(Loc.GetString("power-sensor-examine", ("output", comp.Output)));
}
private void OnInteractUsing(EntityUid uid, PowerSensorComponent comp, InteractUsingEvent args)
{
if (args.Handled || !_tool.HasQuality(args.Used, comp.SwitchQuality))
return;
// no sound spamming
if (TryComp<UseDelayComponent>(uid, out var useDelay)
&& !_useDelay.TryResetDelay((uid, useDelay), true))
return;
// switch between input and output mode.
comp.Output = !comp.Output;
// since the battery to be checked changed the output probably has too, update it
UpdateOutputs(uid, comp);
// notify the user
_audio.PlayPvs(comp.SwitchSound, uid);
var msg = Loc.GetString("power-sensor-switch", ("output", comp.Output));
_popup.PopupEntity(msg, uid, args.User);
}
private void UpdateOutputs(EntityUid uid, PowerSensorComponent comp)
{
// get power stats on the power network that's been switched to
var powerSwitchable = Comp<PowerSwitchableComponent>(uid);
var cable = powerSwitchable.Cables[powerSwitchable.ActiveIndex];
var nodeContainer = Comp<NodeContainerComponent>(uid);
var deviceNode = (CableDeviceNode) nodeContainer.Nodes[cable.Node];
// update state based on the power stats retrieved from the selected power network
var xform = Transform(uid);
if (!TryComp(xform.GridUid, out MapGridComponent? grid))
return;
if (deviceNode.NodeGroup == null)
return;
var group = (IBasePowerNet) deviceNode.NodeGroup;
var stats = _powerNet.GetNetworkStatistics(group.NetworkNode);
var charge = comp.Output ? stats.OutStorageCurrent : stats.InStorageCurrent;
var chargingState = charge > comp.LastCharge;
var dischargingState = charge < comp.LastCharge;
comp.LastCharge = charge;
// send new signals if changed
if (comp.ChargingState != chargingState)
{
comp.ChargingState = chargingState;
_deviceLink.SendSignal(uid, comp.ChargingPort, chargingState);
}
if (comp.DischargingState != dischargingState)
{
comp.DischargingState = dischargingState;
_deviceLink.SendSignal(uid, comp.DischargingPort, dischargingState);
}
}
}