Fix bugs resulting in quantum APC visual states (#29475)

There were TWO bugs here

FIRST, APCs *did* update their visual state on initialization, but at that point the relevant power state hasn't been initialized yet, so it always returns a bogus result. There aren't guaranteed to be subsequent power updates that actually trigger the APC to update so this can get it stuck.

Fixed by just deferring the on-init update to be after the first update tick, which is itself ordered to be after power update.

SECOND: Once I fixed that, I ran into the issue that APCs created at *server startup* also fail to update, because the throttling system (to prevent frequent APC updates) thinks the LastChargeStateTime was at server startup.

Fixed by making that variable nullable so it defaults to null.

Also removed the useless datafields on the "last update" fields. These are all just used to cache and throttle updates, something that should not be persisted to a map file.
This commit is contained in:
Pieter-Jan Briers
2024-06-29 04:07:00 +02:00
committed by GitHub
parent cb6aeb4f96
commit 797aebb161
2 changed files with 28 additions and 15 deletions

View File

@@ -32,7 +32,7 @@ public sealed class ApcSystem : EntitySystem
UpdatesAfter.Add(typeof(PowerNetSystem));
SubscribeLocalEvent<ApcComponent, BoundUIOpenedEvent>(OnBoundUiOpen);
SubscribeLocalEvent<ApcComponent, MapInitEvent>(OnApcInit);
SubscribeLocalEvent<ApcComponent, ComponentStartup>(OnApcStartup);
SubscribeLocalEvent<ApcComponent, ChargeChangedEvent>(OnBatteryChargeChanged);
SubscribeLocalEvent<ApcComponent, ApcToggleMainBreakerMessage>(OnToggleMainBreaker);
SubscribeLocalEvent<ApcComponent, GotEmaggedEvent>(OnEmagged);
@@ -50,6 +50,11 @@ public sealed class ApcSystem : EntitySystem
apc.LastUiUpdate = _gameTiming.CurTime;
UpdateUIState(uid, apc, battery);
}
if (apc.NeedStateUpdate)
{
UpdateApcState(uid, apc, battery);
}
}
}
@@ -59,9 +64,11 @@ public sealed class ApcSystem : EntitySystem
UpdateApcState(uid, component);
}
private void OnApcInit(EntityUid uid, ApcComponent component, MapInitEvent args)
private static void OnApcStartup(EntityUid uid, ApcComponent component, ComponentStartup args)
{
UpdateApcState(uid, component);
// We cannot update immediately, as various network/battery state is not valid yet.
// Defer until the next tick.
component.NeedStateUpdate = true;
}
//Update the HasAccess var for UI to read
@@ -119,15 +126,18 @@ public sealed class ApcSystem : EntitySystem
if (!Resolve(uid, ref apc, ref battery, false))
return;
var newState = CalcChargeState(uid, battery.NetworkBattery);
if (newState != apc.LastChargeState && apc.LastChargeStateTime + ApcComponent.VisualsChangeDelay < _gameTiming.CurTime)
if (apc.LastChargeStateTime == null || apc.LastChargeStateTime + ApcComponent.VisualsChangeDelay < _gameTiming.CurTime)
{
apc.LastChargeState = newState;
apc.LastChargeStateTime = _gameTiming.CurTime;
if (TryComp(uid, out AppearanceComponent? appearance))
var newState = CalcChargeState(uid, battery.NetworkBattery);
if (newState != apc.LastChargeState)
{
_appearance.SetData(uid, ApcVisuals.ChargeState, newState, appearance);
apc.LastChargeState = newState;
apc.LastChargeStateTime = _gameTiming.CurTime;
if (TryComp(uid, out AppearanceComponent? appearance))
{
_appearance.SetData(uid, ApcVisuals.ChargeState, newState, appearance);
}
}
}
@@ -137,6 +147,8 @@ public sealed class ApcSystem : EntitySystem
apc.LastExternalState = extPowerState;
UpdateUIState(uid, apc, battery);
}
apc.NeedStateUpdate = false;
}
public void UpdateUIState(EntityUid uid,