Power switchable refactor (#20419)
Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
@@ -22,7 +22,7 @@ public sealed class PortableGeneratorSystem : SharedPortableGeneratorSystem
|
||||
[Dependency] private readonly AudioSystem _audio = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly GeneratorSystem _generator = default!;
|
||||
[Dependency] private readonly PowerSwitchableGeneratorSystem _switchableGenerator = default!;
|
||||
[Dependency] private readonly PowerSwitchableSystem _switchable = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -36,6 +36,8 @@ public sealed class PortableGeneratorSystem : SharedPortableGeneratorSystem
|
||||
SubscribeLocalEvent<PortableGeneratorComponent, PortableGeneratorStartMessage>(GeneratorStartMessage);
|
||||
SubscribeLocalEvent<PortableGeneratorComponent, PortableGeneratorStopMessage>(GeneratorStopMessage);
|
||||
SubscribeLocalEvent<PortableGeneratorComponent, PortableGeneratorSwitchOutputMessage>(GeneratorSwitchOutputMessage);
|
||||
|
||||
SubscribeLocalEvent<FuelGeneratorComponent, SwitchPowerCheckEvent>(OnSwitchPowerCheck);
|
||||
}
|
||||
|
||||
private void GeneratorSwitchOutputMessage(EntityUid uid, PortableGeneratorComponent component, PortableGeneratorSwitchOutputMessage args)
|
||||
@@ -47,7 +49,7 @@ public sealed class PortableGeneratorSystem : SharedPortableGeneratorSystem
|
||||
if (fuelGenerator.On)
|
||||
return;
|
||||
|
||||
_switchableGenerator.ToggleActiveOutput(uid, args.Session.AttachedEntity.Value);
|
||||
_switchable.Cycle(uid, args.Session.AttachedEntity.Value);
|
||||
}
|
||||
|
||||
private void GeneratorStopMessage(EntityUid uid, PortableGeneratorComponent component, PortableGeneratorStopMessage args)
|
||||
@@ -164,6 +166,12 @@ public sealed class PortableGeneratorSystem : SharedPortableGeneratorSystem
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSwitchPowerCheck(EntityUid uid, FuelGeneratorComponent comp, ref SwitchPowerCheckEvent args)
|
||||
{
|
||||
if (comp.On)
|
||||
args.DisableMessage = Loc.GetString("fuel-generator-verb-disable-on");
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
var query = EntityQueryEnumerator<PortableGeneratorComponent, FuelGeneratorComponent, AppearanceComponent>();
|
||||
|
||||
@@ -1,111 +0,0 @@
|
||||
using Content.Server.NodeContainer;
|
||||
using Content.Server.NodeContainer.EntitySystems;
|
||||
using Content.Server.Popups;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Power.Nodes;
|
||||
using Content.Shared.Power.Generator;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Power.Generator;
|
||||
|
||||
/// <summary>
|
||||
/// Implements power-switchable generators.
|
||||
/// </summary>
|
||||
/// <seealso cref="PowerSwitchableGeneratorComponent"/>
|
||||
/// <seealso cref="PortableGeneratorSystem"/>
|
||||
/// <seealso cref="GeneratorSystem"/>
|
||||
public sealed class PowerSwitchableGeneratorSystem : SharedPowerSwitchableGeneratorSystem
|
||||
{
|
||||
[Dependency] private readonly NodeGroupSystem _nodeGroup = default!;
|
||||
[Dependency] private readonly PopupSystem _popup = default!;
|
||||
[Dependency] private readonly AudioSystem _audio = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<PowerSwitchableGeneratorComponent, GetVerbsEvent<InteractionVerb>>(GetInteractionVerbs);
|
||||
}
|
||||
|
||||
private void GetInteractionVerbs(
|
||||
EntityUid uid,
|
||||
PowerSwitchableGeneratorComponent component,
|
||||
GetVerbsEvent<InteractionVerb> args)
|
||||
{
|
||||
if (!args.CanAccess || !args.CanInteract)
|
||||
return;
|
||||
|
||||
var isCurrentlyHV = component.ActiveOutput == PowerSwitchableGeneratorOutput.HV;
|
||||
var msg = isCurrentlyHV ? "power-switchable-generator-verb-mv" : "power-switchable-generator-verb-hv";
|
||||
|
||||
var isOn = TryComp(uid, out FuelGeneratorComponent? fuelGenerator) && fuelGenerator.On;
|
||||
|
||||
InteractionVerb verb = new()
|
||||
{
|
||||
Act = () =>
|
||||
{
|
||||
|
||||
var verbIsOn = TryComp(uid, out FuelGeneratorComponent? verbFuelGenerator) && verbFuelGenerator.On;
|
||||
if (verbIsOn)
|
||||
return;
|
||||
|
||||
ToggleActiveOutput(uid, args.User, component);
|
||||
},
|
||||
Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/zap.svg.192dpi.png")),
|
||||
Text = Loc.GetString(msg),
|
||||
};
|
||||
|
||||
if (isOn)
|
||||
{
|
||||
verb.Message = Loc.GetString("power-switchable-generator-verb-disable-on");
|
||||
verb.Disabled = true;
|
||||
}
|
||||
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
public void ToggleActiveOutput(EntityUid uid, EntityUid user, PowerSwitchableGeneratorComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
|
||||
var supplier = Comp<PowerSupplierComponent>(uid);
|
||||
var nodeContainer = Comp<NodeContainerComponent>(uid);
|
||||
var outputMV = (CableDeviceNode) nodeContainer.Nodes[component.NodeOutputMV];
|
||||
var outputHV = (CableDeviceNode) nodeContainer.Nodes[component.NodeOutputHV];
|
||||
|
||||
if (component.ActiveOutput == PowerSwitchableGeneratorOutput.HV)
|
||||
{
|
||||
component.ActiveOutput = PowerSwitchableGeneratorOutput.MV;
|
||||
supplier.Voltage = Voltage.Medium;
|
||||
|
||||
// Switching around the voltage on the power supplier is "enough",
|
||||
// but we also want to disconnect the cable nodes so it doesn't show up in power monitors etc.
|
||||
outputMV.Enabled = true;
|
||||
outputHV.Enabled = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
component.ActiveOutput = PowerSwitchableGeneratorOutput.HV;
|
||||
supplier.Voltage = Voltage.High;
|
||||
|
||||
outputMV.Enabled = false;
|
||||
outputHV.Enabled = true;
|
||||
}
|
||||
|
||||
_popup.PopupEntity(
|
||||
Loc.GetString("power-switchable-generator-switched-output"),
|
||||
uid,
|
||||
user);
|
||||
|
||||
_audio.Play(component.SwitchSound, Filter.Pvs(uid), uid, true);
|
||||
|
||||
Dirty(uid, component);
|
||||
|
||||
_nodeGroup.QueueReflood(outputMV);
|
||||
_nodeGroup.QueueReflood(outputHV);
|
||||
}
|
||||
}
|
||||
123
Content.Server/Power/Generator/PowerSwitchableSystem.cs
Normal file
123
Content.Server/Power/Generator/PowerSwitchableSystem.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using Content.Server.NodeContainer;
|
||||
using Content.Server.NodeContainer.EntitySystems;
|
||||
using Content.Server.Popups;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Power.Nodes;
|
||||
using Content.Shared.Power.Generator;
|
||||
using Content.Shared.Timing;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Power.Generator;
|
||||
|
||||
/// <summary>
|
||||
/// Implements server logic for power-switchable devices.
|
||||
/// </summary>
|
||||
/// <seealso cref="PowerSwitchableComponent"/>
|
||||
/// <seealso cref="PortableGeneratorSystem"/>
|
||||
/// <seealso cref="GeneratorSystem"/>
|
||||
public sealed class PowerSwitchableSystem : SharedPowerSwitchableSystem
|
||||
{
|
||||
[Dependency] private readonly AudioSystem _audio = default!;
|
||||
[Dependency] private readonly NodeGroupSystem _nodeGroup = default!;
|
||||
[Dependency] private readonly PopupSystem _popup = default!;
|
||||
[Dependency] private readonly UseDelaySystem _useDelay = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<PowerSwitchableComponent, GetVerbsEvent<InteractionVerb>>(GetVerbs);
|
||||
}
|
||||
|
||||
private void GetVerbs(EntityUid uid, PowerSwitchableComponent comp, GetVerbsEvent<InteractionVerb> args)
|
||||
{
|
||||
if (!args.CanAccess || !args.CanInteract)
|
||||
return;
|
||||
|
||||
var voltage = VoltageColor(GetNextVoltage(uid, comp));
|
||||
var msg = Loc.GetString("power-switchable-switch-voltage", ("voltage", voltage));
|
||||
|
||||
InteractionVerb verb = new()
|
||||
{
|
||||
Act = () =>
|
||||
{
|
||||
// don't need to check it again since if its disabled server wont let the verb act
|
||||
Cycle(uid, args.User, comp);
|
||||
},
|
||||
Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/zap.svg.192dpi.png")),
|
||||
Text = msg
|
||||
};
|
||||
|
||||
var ev = new SwitchPowerCheckEvent();
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
if (ev.DisableMessage != null)
|
||||
{
|
||||
verb.Message = ev.DisableMessage;
|
||||
verb.Disabled = true;
|
||||
}
|
||||
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cycles voltage then updates nodes and optionally power supplier to match it.
|
||||
/// </summary>
|
||||
public void Cycle(EntityUid uid, EntityUid user, PowerSwitchableComponent? comp = null)
|
||||
{
|
||||
if (!Resolve(uid, ref comp))
|
||||
return;
|
||||
|
||||
// no sound spamming
|
||||
if (TryComp<UseDelayComponent>(uid, out var useDelay) && _useDelay.ActiveDelay(uid))
|
||||
return;
|
||||
|
||||
comp.ActiveIndex = NextIndex(uid, comp);
|
||||
Dirty(uid, comp);
|
||||
|
||||
var voltage = GetVoltage(uid, comp);
|
||||
|
||||
if (TryComp<PowerSupplierComponent>(uid, out var supplier))
|
||||
{
|
||||
// convert to nodegroupid (goofy server Voltage enum is just alias for it)
|
||||
switch (voltage)
|
||||
{
|
||||
case SwitchableVoltage.HV:
|
||||
supplier.Voltage = Voltage.High;
|
||||
break;
|
||||
case SwitchableVoltage.MV:
|
||||
supplier.Voltage = Voltage.Medium;
|
||||
break;
|
||||
case SwitchableVoltage.LV:
|
||||
supplier.Voltage = Voltage.Apc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Switching around the voltage on the power supplier is "enough",
|
||||
// but we also want to disconnect the cable nodes so it doesn't show up in power monitors etc.
|
||||
var nodeContainer = Comp<NodeContainerComponent>(uid);
|
||||
foreach (var cable in comp.Cables)
|
||||
{
|
||||
var node = (CableDeviceNode) nodeContainer.Nodes[cable.Node];
|
||||
node.Enabled = cable.Voltage == voltage;
|
||||
_nodeGroup.QueueReflood(node);
|
||||
}
|
||||
|
||||
var popup = Loc.GetString(comp.SwitchText, ("voltage", VoltageString(voltage)));
|
||||
_popup.PopupEntity(popup, uid, user);
|
||||
|
||||
_audio.PlayPvs(comp.SwitchSound, uid);
|
||||
|
||||
_useDelay.BeginDelay(uid, useDelay);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised on a <see cref="PowerSwitchableComponent"/> to see if its verb should work.
|
||||
/// If <see cref="DisableMessage"/> is non-null, the verb is disabled with that as the message.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public record struct SwitchPowerCheckEvent(string? DisableMessage = null);
|
||||
Reference in New Issue
Block a user