CatStation/Content.Server/Radio/EntitySystems/NotifyOnNonFunctioningSystem.cs
Fildrance 1a5e9acb24
Extract notify on non-functioning from emitter (#45346)
* refactor: extracted notify-on-non-functioning feature from emitter component

* refactor: revert server side PowerStateSystem usage of SubscribeLocalEvent - it is not calling base Initialize, skip SetWorkingState raising event on init

* refactor: ensureAPC is now conditional for powerstate

* refactor: fix double notifiation on destruction and on un-powering for emitter

* refactor: added LocTurnedOff for NotifyOnNonFunctioningComponent, fix double alert on destruction, removed emitter-deconstructed-broadcast as it is unused (cannot deconstruct withou unanchor)

* refactor: modernize server PowerStateSystem subscription

* refactor: update and modernize PowerStatePrototypeTest

* refactor: give APE specific radio message upon unlocking

* refactor: extracted PowerStateSystem.SetPowerLoad to split init from triggering is-working change event

* Update Content.Server/Power/EntitySystems/PowerStateSystem.cs

---------

Co-authored-by: AJCM-git <60196617+AJCM-git@users.noreply.github.com>
2026-08-16 23:08:58 +00:00

104 lines
3.9 KiB
C#

using Content.Server.Pinpointer;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.Radio.Components;
using Content.Shared.Construction;
using Content.Shared.Destructible;
using Content.Shared.Lock;
using Content.Shared.Power.EntitySystems;
using Robust.Shared.Utility;
namespace Content.Server.Radio.EntitySystems;
/// <summary>
/// System for sending radio notification upon entity becoming
/// non-functioning - unpowered / deconstructed / destroyed.
/// </summary>
public sealed partial class NotifyOnNonFunctioningSystem : EntitySystem
{
[Dependency] private RadioSystem _radio = default!;
[Dependency] private NavMapSystem _navMap = default!;
[Dependency] private PowerStateSystem _powerState = default!;
/// <summary> Notify on entity destruction. </summary>
[SubscribeLocalEvent]
private void OnDestruction(Entity<NotifyOnNonFunctioningComponent> ent, ref DestructionEventArgs args)
{
if (ent.Comp.LocDestroyed.HasValue)
AlertRadioIfWasWorking(ent, ent.Comp.LocDestroyed);
}
/// <summary> Notify on deconstruction. </summary>
[SubscribeLocalEvent]
private void OnDeconstructed(Entity<NotifyOnNonFunctioningComponent> ent, ref MachineDeconstructedEvent args)
{
if (ent.Comp.LocDeconstructed.HasValue)
AlertRadioIfWasWorking(ent, ent.Comp.LocDeconstructed);
}
/// <summary> Notify on unlocking already locked entity. </summary>
[SubscribeLocalEvent]
private void OnLockToggled(Entity<NotifyOnNonFunctioningComponent> ent, ref LockToggledEvent args)
{
if (args.Locked || !ent.Comp.LocUnlocked.HasValue)
return;
AlertRadioIfWasWorking(ent, ent.Comp.LocUnlocked);
}
/// <summary> Notify on turning off. </summary>
[SubscribeLocalEvent]
private void OnIsWorkingChanges(Entity<NotifyOnNonFunctioningComponent> ent, ref PowerStateChanged args)
{
// deleted entity is working change should be handled during other events
if (args.IsWorking || !ent.Comp.LocTurnedOff.HasValue || TerminatingOrDeleted(ent))
return;
AlertRadio(ent, ent.Comp.LocTurnedOff);
}
/// <summary> Notify on unanchoring. </summary>
[SubscribeLocalEvent]
private void OnAnchorStateChanged(Entity<NotifyOnNonFunctioningComponent> ent, ref AnchorStateChangedEvent args)
{
if (args.Anchored || !ent.Comp.LocUnanchored.HasValue || TerminatingOrDeleted(ent))
return;
AlertRadioIfWasWorking(ent, ent.Comp.LocUnanchored);
}
[SubscribeLocalEvent]
private void ReceivedChanged(Entity<NotifyOnNonFunctioningComponent> ent, ref PowerConsumerReceivedChanged args)
{
if (!ent.Comp.LocUnpowered.HasValue ||!_powerState.GetWorkingState(ent.Owner))
return;
if (args.ReceivedPower < args.DrawRate)
AlertRadioIfWasWorking(ent, ent.Comp.LocUnpowered, true);
}
private void AlertRadioIfWasWorking(Entity<NotifyOnNonFunctioningComponent> ent, string locString, bool ignorePower = false)
{
if (!_powerState.GetWorkingState(ent.Owner))
return;
AlertRadio(ent, locString, ignorePower);
}
private void AlertRadio(Entity<NotifyOnNonFunctioningComponent> ent, string locString, bool ignorePower = false)
{
if (ent.Comp.RequirePowered && !ignorePower)
{
if (TryComp<ApcPowerReceiverComponent>(ent, out var apc) && !apc.Powered)
return;
if (TryComp<PowerConsumerComponent>(ent, out var consumer) && consumer.DrawRate > consumer.ReceivedPower)
return;
}
var locationInfo = FormattedMessage.RemoveMarkupOrThrow(_navMap.GetNearestBeaconString(ent.Owner));
var message = Loc.GetString(locString, ("location", locationInfo));
_radio.SendRadioMessage(ent.Owner, message, ent.Comp.RadioChannel, ent.Owner);
}
}