Power Consumers Rebalance: Simple Dynamic Power Loading (#41961)

* initial commit

* misc additions and fixes

* final tests and additions

* cleanup 1

* fix tests and add a test

* fix tests AGAIN

* abject horror and misery

* cleanup

* cleanup 2

* address some issues
This commit is contained in:
ArtisticRoomba
2026-06-29 12:43:37 +02:00
committed by Coryler
parent 18bb4fe1fa
commit 11dc186941
21 changed files with 229 additions and 28 deletions
@@ -0,0 +1,59 @@
using System.Linq;
using Content.Server.Power.Components;
using Content.Shared.Power.Components;
using Content.Shared.Power.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.Prototypes;
namespace Content.IntegrationTests.Tests.Power;
[TestFixture, TestOf(typeof(SharedPowerStateSystem))]
public sealed class PowerStatePrototypeTest
{
/// <summary>
/// Asserts that the <see cref="SharedApcPowerReceiverComponent"/>'s load is the same
/// as the idle or working power draw from <see cref="PowerStateComponent"/>,
/// depending on the current power state.
/// </summary>
[Test]
public async Task AssertApcPowerMatchesPowerState()
{
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;
var protoMan = server.ResolveDependency<IPrototypeManager>();
var entMan = server.ResolveDependency<IEntityManager>();
await server.WaitAssertion(() =>
{
Assert.Multiple(delegate
{
foreach (var prototype in protoMan.EnumeratePrototypes<EntityPrototype>()
.Where(p => !p.Abstract)
.Where(p => !pair.IsTestPrototype(p)))
{
if (!prototype.TryGetComponent<PowerStateComponent>(out var powerStateComp, entMan.ComponentFactory))
continue;
// LESSON LEARNED:
// ENSURE THAT THE COMPONENT YOU ARE TRYING TO GET IS THE SERVER-SIDE VARIANT
if (!prototype.TryGetComponent<ApcPowerReceiverComponent>(out var powerReceiverComp, entMan.ComponentFactory))
{
Assert.Fail(
$"Entity prototype '{prototype.ID}' has a PowerStateComponent but is missing the required ApcPowerReceiverComponent.");
}
var expectedLoad = powerStateComp.IsWorking
? powerStateComp.WorkingPowerDraw
: powerStateComp.IdlePowerDraw;
Assert.That(powerReceiverComp.Load,
Is.EqualTo(expectedLoad),
$"Entity prototype '{prototype.ID}' has mismatched power draw between PowerStateComponent and SharedApcPowerReceiverComponent.");
}
});
});
await pair.CleanReturnAsync();
}
}
@@ -56,7 +56,7 @@ public sealed class PowerStateTest
Assert.That(receiver.Load, Is.EqualTo(powerState.IdlePowerDraw).Within(0.01f));
});
var system = entManager.System<PowerStateSystem>();
var system = entManager.System<SharedPowerStateSystem>();
system.SetWorkingState((ent, powerState), true);
Assert.Multiple(() =>
@@ -93,7 +93,7 @@ public sealed class PowerStateTest
var receiver = entManager.GetComponent<Server.Power.Components.ApcPowerReceiverComponent>(ent);
var powerState = entManager.GetComponent<PowerStateComponent>(ent);
var system = entManager.System<PowerStateSystem>();
var system = entManager.System<SharedPowerStateSystem>();
Entity<PowerStateComponent> newEnt = (ent, powerState);
Assert.Multiple(() =>
@@ -146,7 +146,7 @@ public sealed class PowerStateTest
var receiver = entManager.GetComponent<Server.Power.Components.ApcPowerReceiverComponent>(ent);
var powerState = entManager.GetComponent<PowerStateComponent>(ent);
var system = entManager.System<PowerStateSystem>();
var system = entManager.System<SharedPowerStateSystem>();
Entity<PowerStateComponent> valueTuple = (ent, powerState);
Assert.Multiple(() =>