Add a component for upgrading power consumption in machines (#11777)
Co-authored-by: 0x6273 <0x40@keemail.me>
This commit is contained in:
47
Content.Server/Power/Components/UpgradePowerDrawComponent.cs
Normal file
47
Content.Server/Power/Components/UpgradePowerDrawComponent.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Content.Shared.Construction.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.Power.Components;
|
||||
|
||||
/// <summary>
|
||||
/// This is used for machines whose power draw
|
||||
/// can be decreased through machine part upgrades.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class UpgradePowerDrawComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The base power draw of the machine.
|
||||
/// Prioritizes hv/mv draw over lv draw.
|
||||
/// Value is initializezd on map init from <see cref="ApcPowerReceiverComponent"/>
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public float BaseLoad;
|
||||
|
||||
/// <summary>
|
||||
/// The machine part that affects the power draw.
|
||||
/// </summary>
|
||||
[DataField("machinePartPowerDraw", customTypeSerializer: typeof(PrototypeIdSerializer<MachinePartPrototype>)), ViewVariables(VVAccess.ReadWrite)]
|
||||
public string MachinePartPowerDraw = "Capacitor";
|
||||
|
||||
/// <summary>
|
||||
/// The multiplier used for scaling the power draw.
|
||||
/// </summary>
|
||||
[DataField("powerDrawMultiplier", required: true), ViewVariables(VVAccess.ReadWrite)]
|
||||
public float PowerDrawMultiplier = 1f;
|
||||
|
||||
/// <summary>
|
||||
/// What type of scaling is being used?
|
||||
/// </summary>
|
||||
[DataField("scaling", required: true), ViewVariables(VVAccess.ReadWrite)]
|
||||
public PowerDrawScalingType Scaling;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The different types of scaling that are available
|
||||
/// </summary>
|
||||
public enum PowerDrawScalingType : byte
|
||||
{
|
||||
Linear,
|
||||
Exponential
|
||||
}
|
||||
49
Content.Server/Power/EntitySystems/UpgradePowerDrawSystem.cs
Normal file
49
Content.Server/Power/EntitySystems/UpgradePowerDrawSystem.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Content.Server.Construction;
|
||||
using Content.Server.Power.Components;
|
||||
|
||||
namespace Content.Server.Power.EntitySystems;
|
||||
|
||||
/// <summary>
|
||||
/// This handles using upgraded machine parts
|
||||
/// to modify the power load of a machine.
|
||||
/// </summary>
|
||||
public sealed class UpgradePowerDrawSystem : EntitySystem
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<UpgradePowerDrawComponent, MapInitEvent>(OnMapInit);
|
||||
SubscribeLocalEvent<UpgradePowerDrawComponent, RefreshPartsEvent>(OnRefreshParts);
|
||||
}
|
||||
|
||||
private void OnMapInit(EntityUid uid, UpgradePowerDrawComponent component, MapInitEvent args)
|
||||
{
|
||||
if (TryComp<PowerConsumerComponent>(uid, out var powa))
|
||||
component.BaseLoad = powa.DrawRate;
|
||||
else if (TryComp<ApcPowerReceiverComponent>(uid, out var powa2))
|
||||
component.BaseLoad = powa2.Load;
|
||||
}
|
||||
|
||||
private void OnRefreshParts(EntityUid uid, UpgradePowerDrawComponent component, RefreshPartsEvent args)
|
||||
{
|
||||
var load = component.BaseLoad;
|
||||
var rating = args.PartRatings[component.MachinePartPowerDraw];
|
||||
switch (component.Scaling)
|
||||
{
|
||||
case PowerDrawScalingType.Linear:
|
||||
load += component.PowerDrawMultiplier * (rating - 1);
|
||||
break;
|
||||
case PowerDrawScalingType.Exponential:
|
||||
load *= MathF.Pow(component.PowerDrawMultiplier, rating - 1);
|
||||
break;
|
||||
default:
|
||||
Logger.Error($"invalid power scaling type for {ToPrettyString(uid)}.");
|
||||
load = 0;
|
||||
break;
|
||||
}
|
||||
if (TryComp<ApcPowerReceiverComponent>(uid, out var powa))
|
||||
powa.Load = load;
|
||||
if (TryComp<PowerConsumerComponent>(uid, out var powa2))
|
||||
powa2.DrawRate = load;
|
||||
}
|
||||
}
|
||||
@@ -30,3 +30,6 @@
|
||||
- type: Wires
|
||||
BoardName: "chem_dispenser"
|
||||
LayoutId: chem_dispenser
|
||||
- type: UpgradePowerDraw
|
||||
powerDrawMultiplier: 0.75
|
||||
scaling: Exponential
|
||||
|
||||
@@ -19,6 +19,9 @@
|
||||
- type: Appearance
|
||||
- type: ApcPowerReceiver
|
||||
powerLoad: 1000
|
||||
- type: UpgradePowerDraw
|
||||
powerDrawMultiplier: 0.75
|
||||
scaling: Exponential
|
||||
- type: ExtensionCableReceiver
|
||||
- type: Damageable
|
||||
damageContainer: Inorganic
|
||||
|
||||
Reference in New Issue
Block a user