Sentry turrets - Part 2: Basic prototype (#35031)

This commit is contained in:
chromiumboy
2025-02-23 07:39:44 -06:00
committed by GitHub
parent b742afbe2c
commit 066c773965
8 changed files with 500 additions and 263 deletions

View File

@@ -22,6 +22,7 @@ namespace Content.Server.Power.EntitySystems
[Dependency] private readonly PowerNetConnectorSystem _powerNetConnector = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IParallelManager _parMan = default!;
[Dependency] private readonly BatterySystem _battery = default!;
private readonly PowerState _powerState = new();
private readonly HashSet<PowerNet> _powerNetReconnectQueue = new();
@@ -278,7 +279,7 @@ namespace Content.Server.Power.EntitySystems
// Send events where necessary.
// TODO: Instead of querying ALL power components every tick, and then checking if an event needs to be
// raised, should probably assemble a list of entity Uids during the actual solver steps.
UpdateApcPowerReceiver();
UpdateApcPowerReceiver(frameTime);
UpdatePowerConsumer();
UpdateNetworkBattery();
}
@@ -306,10 +307,12 @@ namespace Content.Server.Power.EntitySystems
_powerNetReconnectQueue.Clear();
}
private void UpdateApcPowerReceiver()
private void UpdateApcPowerReceiver(float frameTime)
{
var appearanceQuery = GetEntityQuery<AppearanceComponent>();
var metaQuery = GetEntityQuery<MetaDataComponent>();
var apcBatteryQuery = GetEntityQuery<ApcPowerReceiverBatteryComponent>();
var enumerator = AllEntityQuery<ApcPowerReceiverComponent>();
while (enumerator.MoveNext(out var uid, out var apcReceiver))
{
@@ -318,6 +321,42 @@ namespace Content.Server.Power.EntitySystems
|| MathHelper.CloseToPercent(apcReceiver.NetworkLoad.ReceivingPower,
apcReceiver.Load));
// Check if the entity has an internal battery
if (apcBatteryQuery.TryComp(uid, out var apcBattery) && TryComp<BatteryComponent>(uid, out var battery))
{
apcReceiver.Load = apcBattery.IdleLoad;
// Try to draw power from the battery if there isn't sufficient external power
var requireBattery = !powered && !apcReceiver.PowerDisabled;
if (requireBattery)
{
_battery.SetCharge(uid, battery.CurrentCharge - apcBattery.IdleLoad * frameTime, battery);
}
// Otherwise try to charge the battery
else if (powered && !_battery.IsFull(uid, battery))
{
apcReceiver.Load += apcBattery.BatteryRechargeRate * apcBattery.BatteryRechargeEfficiency;
_battery.SetCharge(uid, battery.CurrentCharge + apcBattery.BatteryRechargeRate * frameTime, battery);
}
// Enable / disable the battery if the state changed
var enableBattery = requireBattery && battery.CurrentCharge > 0;
if (apcBattery.Enabled != enableBattery)
{
apcBattery.Enabled = enableBattery;
var apcBatteryEv = new ApcPowerReceiverBatteryChangedEvent(enableBattery);
RaiseLocalEvent(uid, ref apcBatteryEv);
_appearance.SetData(uid, PowerDeviceVisuals.BatteryPowered, enableBattery);
}
powered |= enableBattery;
}
// If new value is the same as the old, then exit
if (!apcReceiver.Recalculate && apcReceiver.Powered == powered)
continue;