Power switchable refactor (#20419)

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-10-25 13:57:23 +01:00
committed by GitHub
parent 9025ccaa9d
commit b6a8f5e780
11 changed files with 332 additions and 230 deletions

View File

@@ -0,0 +1,82 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Power.Generator;
/// <summary>
/// Enables a device to switch between HV, MV and LV connectors.
/// For generators this means changing output wires.
/// </summary>
/// <remarks>
/// Must have <c>CableDeviceNode</c>s for each output in <see cref="Outputs"/>.
/// If its a generator <c>PowerSupplierComponent</c> is also required.
/// </remarks>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(SharedPowerSwitchableSystem))]
public sealed partial class PowerSwitchableComponent : Component
{
/// <summary>
/// Index into <see cref="Cables"/> that the device is currently using.
/// </summary>
[DataField, AutoNetworkedField]
public int ActiveIndex;
/// <summary>
/// Sound that plays when the cable is switched.
/// </summary>
[DataField]
public SoundSpecifier? SwitchSound = new SoundPathSpecifier("/Audio/Machines/button.ogg");
/// <summary>
/// Locale id for text shown when examined.
/// It is given "voltage" as a colored voltage string.
/// </summary>
[DataField(required: true)]
public string ExamineText = string.Empty;
/// <summary>
/// Locale id for the popup shown when switching voltages.
/// It is given "voltage" as a colored voltage string.
/// </summary>
[DataField(required: true)]
public string SwitchText = string.Empty;
/// <summary>
/// Cable voltages and their nodes which can be cycled between.
/// Each node name must match a cable node in its <c>NodeContainer</c>.
/// </summary>
[DataField(required: true)]
public List<PowerSwitchableCable> Cables = new();
}
/// <summary>
/// Cable voltage and node name for cycling.
/// </summary>
[DataDefinition]
public sealed partial class PowerSwitchableCable
{
/// <summary>
/// Voltage that the cable uses.
/// </summary>
[DataField(required: true)]
public SwitchableVoltage Voltage;
/// <summary>
/// Name of the node for the cable.
/// Must be a <c>CableDeviceNode</c>
/// </summary>
[DataField(required: true)]
public string Node = string.Empty;
}
/// <summary>
/// Cable voltage to cycle between.
/// </summary>
[Serializable, NetSerializable]
public enum SwitchableVoltage : byte
{
HV,
MV,
LV
}

View File

@@ -1,61 +0,0 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Power.Generator;
/// <summary>
/// Enables a generator to switch between HV and MV output.
/// </summary>
/// <remarks>
/// Must have <c>CableDeviceNode</c>s for both <see cref="NodeOutputMV"/> and <see cref="NodeOutputHV"/>, and also a <c>PowerSupplierComponent</c>.
/// </remarks>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(SharedPowerSwitchableGeneratorSystem))]
public sealed partial class PowerSwitchableGeneratorComponent : Component
{
/// <summary>
/// Which output the portable generator is currently connected to.
/// </summary>
[DataField("activeOutput")]
[AutoNetworkedField]
public PowerSwitchableGeneratorOutput ActiveOutput { get; set; }
/// <summary>
/// Sound that plays when the output is switched.
/// </summary>
/// <returns></returns>
[DataField("switchSound")]
public SoundSpecifier? SwitchSound { get; set; }
/// <summary>
/// Which node is the MV output?
/// </summary>
[DataField("nodeOutputMV")]
public string NodeOutputMV { get; set; } = "output_mv";
/// <summary>
/// Which node is the HV output?
/// </summary>
[DataField("nodeOutputHV")]
public string NodeOutputHV { get; set; } = "output_hv";
}
/// <summary>
/// Possible power output for power-switchable generators.
/// </summary>
/// <seealso cref="PowerSwitchableGeneratorComponent"/>
[Serializable, NetSerializable]
public enum PowerSwitchableGeneratorOutput : byte
{
/// <summary>
/// The generator is set to connect to a high-voltage power network.
/// </summary>
HV,
/// <summary>
/// The generator is set to connect to a medium-voltage power network.
/// </summary>
MV
}

View File

@@ -1,23 +0,0 @@
using Content.Shared.Examine;
namespace Content.Shared.Power.Generator;
/// <summary>
/// Shared logic for power-switchable generators.
/// </summary>
/// <seealso cref="PowerSwitchableGeneratorComponent"/>
public abstract class SharedPowerSwitchableGeneratorSystem : EntitySystem
{
public override void Initialize()
{
SubscribeLocalEvent<PowerSwitchableGeneratorComponent, ExaminedEvent>(GeneratorExamined);
}
private void GeneratorExamined(EntityUid uid, PowerSwitchableGeneratorComponent component, ExaminedEvent args)
{
// Show which output is currently selected.
args.PushMarkup(Loc.GetString(
"power-switchable-generator-examine",
("output", component.ActiveOutput.ToString())));
}
}

View File

@@ -0,0 +1,72 @@
using Content.Shared.Examine;
namespace Content.Shared.Power.Generator;
/// <summary>
/// Shared logic for power-switchable devices.
/// </summary>
/// <seealso cref="PowerSwitchableComponent"/>
public abstract class SharedPowerSwitchableSystem : EntitySystem
{
public override void Initialize()
{
SubscribeLocalEvent<PowerSwitchableComponent, ExaminedEvent>(OnExamined);
}
private void OnExamined(EntityUid uid, PowerSwitchableComponent comp, ExaminedEvent args)
{
// Show which voltage is currently selected.
var voltage = VoltageColor(GetVoltage(uid, comp));
args.PushMarkup(Loc.GetString(comp.ExamineText, ("voltage", voltage)));
}
/// <summary>
/// Helper to get the colored markup string for a voltage type.
/// </summary>
public string VoltageColor(SwitchableVoltage voltage)
{
return Loc.GetString("power-switchable-voltage", ("voltage", VoltageString(voltage)));
}
/// <summary>
/// Converts from "hv" to "HV" since for some reason the enum gets made lowercase???
/// </summary>
public string VoltageString(SwitchableVoltage voltage)
{
return voltage.ToString().ToUpper();
}
/// <summary>
/// Returns index of the next cable type index to cycle to.
/// </summary>
public int NextIndex(EntityUid uid, PowerSwitchableComponent? comp = null)
{
if (!Resolve(uid, ref comp))
return 0;
// loop back at the end
return (comp.ActiveIndex + 1) % comp.Cables.Count;
}
/// <summary>
/// Returns the current cable voltage being used by a power-switchable device.
/// </summary>
public SwitchableVoltage GetVoltage(EntityUid uid, PowerSwitchableComponent? comp = null)
{
if (!Resolve(uid, ref comp))
return default;
return comp.Cables[comp.ActiveIndex].Voltage;
}
/// <summary>
/// Returns the cable's next voltage to cycle to being used by a power-switchable device.
/// </summary>
public SwitchableVoltage GetNextVoltage(EntityUid uid, PowerSwitchableComponent? comp = null)
{
if (!Resolve(uid, ref comp))
return default;
return comp.Cables[NextIndex(uid, comp)].Voltage;
}
}