Cut 5: priority tiers replace PowerBus's single shared GrantRatio
PowerTiers.cs names five tiers (Critical/High/Medium/Low/Utility) and their per-behaviour-kind defaults: Radiator is Critical (the closest thing this game has to life support -- a starved radiator cascades into wear and shutdown across the whole hull), Shield is High, Thruster/AetherDrive are Medium, ConstantWeapon/InstantWeapon are Low, and Sensor/EnergyDraw are Utility. IPowerConsumer.DefaultPowerTier lets each behaviour own its own default. EquippableItem.PowerTier (Key 12) is the stored, player-choosable tier on the unit rather than the design; EquippedItem's constructor seeds a kind default the first time a unit is equipped, only when the field is still PowerTiers.Unassigned, and never again -- a tier is a stored choice, not something the bus re-derives. PowerBus.AllocateTiers walks tiers lowest-number-first: a tier is granted in full before the next tier sees anything left, an empty tier passes its whole share down, and within a tier every consumer rations by the same grant/demand fraction so unequal requests still divide proportionally. A new TierGrantRatio[] exposes each tier's own ratio for a future UI (Cut 8) to read starvation from directly, instead of reconstructing it from every item's PowerSupply. Deletes EquippedItem.SortPosition, IOrderedBehavior and Reactor.Order (its only implementer): the bus already steps before any equipment's Execute, so equip/behaviour order never decided who got fed even before this cut, and nothing else needed the interface (subject to the map's O3 operator check). docs/stats-and-power-cut.md Cut 5 / §1.3. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
08c3bb8c64
commit
55249beb98
15 changed files with 192 additions and 28 deletions
|
|
@ -11,9 +11,14 @@ using static CultMath.math;
|
|||
// Entity.TryConsumeEnergy/CanConsumeEnergy: at HEAD every draw succeeded as long as one reactor was online, and
|
||||
// any shortfall was silently taxed onto the reactor as heat instead of ever being refused (§0.5). The bus makes
|
||||
// the ceiling real: demand beyond what this tick's generation and stored capacitor charge can cover goes unmet.
|
||||
// There are no tiers yet (Cut 5 adds them), so a shortfall rations the whole pool by the same fraction for every
|
||||
// consumer alike -- the death of the old "whoever equipped first drains the capacitors first" hidden priority
|
||||
// (§0.5), not a replacement for it.
|
||||
//
|
||||
// Cut 5 (docs/stats-and-power-cut.md §1.3) replaces the single shared fraction with priority tiers
|
||||
// (PowerTiers.cs): the total this tick's supply can cover (TotalGrant, unchanged by tiers -- see below) is
|
||||
// walked tier by tier, lowest number first. A tier is fed in full before the next tier sees anything left; a
|
||||
// tier with no demand simply passes its whole share down; within a tier, every consumer's own request is
|
||||
// rationed by the same fraction, so two requests of unequal size still divide proportionally. This is the death
|
||||
// of the old "whoever equipped first drains the capacitors first" hidden priority (§0.5), replaced with an
|
||||
// authored one instead of no priority at all.
|
||||
//
|
||||
// Bus capacitors are charged and drained only here (§0b): Reactor no longer touches them, and neither does any
|
||||
// IPowerConsumer. The four instant draws the cut map names (a burst, a shot, a ping, a hit taken) are
|
||||
|
|
@ -32,16 +37,34 @@ public class PowerBus
|
|||
public float TotalGeneration { get; private set; }
|
||||
public float TotalDemand { get; private set; }
|
||||
|
||||
// Energy actually delivered this tick: generation first, then whatever stored capacitor charge covers the
|
||||
// rest, capped at demand. Equal to TotalDemand whenever supply covers it; equal to (generation + available
|
||||
// charge) otherwise -- never more, which TryConsumeEnergy could never promise (a reactor always paid the
|
||||
// remainder as heat, so overdraw was invisible at this level).
|
||||
// Energy actually delivered this tick, summed across every tier: generation first, then whatever stored
|
||||
// capacitor charge covers the rest, capped at demand. Equal to TotalDemand whenever supply covers it; equal
|
||||
// to (generation + available charge) otherwise -- never more, which TryConsumeEnergy could never promise (a
|
||||
// reactor always paid the remainder as heat, so overdraw was invisible at this level). Which consumers that
|
||||
// total actually reaches is what tiering (below) decides; this figure is unaffected by tiers -- the ceiling
|
||||
// is a property of supply, not of priority.
|
||||
public float TotalGrant { get; private set; }
|
||||
|
||||
// Fraction of every consumer's own request actually granted this tick, shared by every consumer alike (no
|
||||
// tiers yet). Also written onto every consuming EquippedItem.PowerSupply.
|
||||
// Aggregate fraction of all demand granted this tick (TotalGrant / TotalDemand) -- a fleet-wide summary, not
|
||||
// what any one consumer necessarily received. Still written onto every consuming EquippedItem.PowerSupply
|
||||
// when every tier in play happens to land on the same ratio (single-tier loadouts, the common case in the
|
||||
// Cut 3/4 test fixtures), but a multi-tier shortfall gives different tiers different ratios -- read
|
||||
// TierGrantRatio or the item's own PowerSupply for that.
|
||||
public float GrantRatio { get; private set; } = 1f;
|
||||
|
||||
// Cut 5: this tick's within-tier grant ratio, indexed by PowerTiers.Critical..Utility. 1f means that tier's
|
||||
// demand was fully met (including a tier with no demand at all, per the array's own default-init); anything
|
||||
// below 1f is exactly the "starved" state a future UI (Cut 8) reads instead of reconstructing it from
|
||||
// per-item PowerSupply. A tier below a starved one is always 0f -- §1.3's "a tier boundary does not leak".
|
||||
public float[] TierGrantRatio { get; } = InitFullTiers();
|
||||
|
||||
private static float[] InitFullTiers()
|
||||
{
|
||||
var ratios = new float[PowerTiers.Count];
|
||||
for (var i = 0; i < ratios.Length; i++) ratios[i] = 1f;
|
||||
return ratios;
|
||||
}
|
||||
|
||||
// What Reactor.Execute reads to run its own heat/throttle arithmetic (Cut 3: "the arithmetic survives, moved
|
||||
// under the bus's numbers"). Positive: demand left unmet after generation AND stored charge, split evenly
|
||||
// across online reactors for overload heat. Negative: generation left over after demand and after topping up
|
||||
|
|
@ -49,11 +72,22 @@ public class PowerBus
|
|||
// a reported total, not a sink (§1.2).
|
||||
public float NetDraw { get; private set; }
|
||||
|
||||
// One equipped item's power request for this tick, captured once so PowerRequest -- which some behaviours
|
||||
// (Shield, InstantWeapon, Sensor) implement by refreshing their own cached stats -- is never called twice in
|
||||
// one Step.
|
||||
private readonly struct Draw
|
||||
{
|
||||
public readonly EquippedItem Item;
|
||||
public readonly int Tier;
|
||||
public readonly float Request;
|
||||
public Draw(EquippedItem item, int tier, float request) { Item = item; Tier = tier; Request = request; }
|
||||
}
|
||||
|
||||
public void Step(float dt)
|
||||
{
|
||||
var reactors = new List<Reactor>();
|
||||
var capacitors = new List<Capacitor>();
|
||||
var consumers = new List<IPowerConsumer>();
|
||||
var draws = new List<Draw>();
|
||||
foreach (var item in _entity.Equipment)
|
||||
foreach (var behavior in item.Behaviors)
|
||||
{
|
||||
|
|
@ -67,11 +101,20 @@ public class PowerBus
|
|||
break;
|
||||
}
|
||||
if (behavior is IPowerConsumer consumer)
|
||||
consumers.Add(consumer);
|
||||
{
|
||||
// Cut 5 (docs/stats-and-power-cut.md §1.3): the item's own stored choice wins once it has one;
|
||||
// PowerTiers.Unassigned only survives past EquippedItem's constructor for a consumer added to
|
||||
// the game after this unit was minted, which the constructor never saw -- fall back to the
|
||||
// behaviour's own default rather than stranding it at an invalid tier.
|
||||
var tier = item.EquippableItem.PowerTier;
|
||||
if (tier == PowerTiers.Unassigned) tier = consumer.DefaultPowerTier;
|
||||
tier = clamp(tier, 0, PowerTiers.Count - 1);
|
||||
draws.Add(new Draw(item, tier, max(0f, consumer.PowerRequest(dt))));
|
||||
}
|
||||
}
|
||||
|
||||
TotalGeneration = reactors.Sum(r => r.Generation(dt));
|
||||
TotalDemand = consumers.Sum(c => max(0f, c.PowerRequest(dt)));
|
||||
TotalDemand = draws.Sum(d => d.Request);
|
||||
|
||||
var availableCharge = capacitors.Sum(c => c.Charge);
|
||||
var preCapacitorNet = TotalDemand - TotalGeneration;
|
||||
|
|
@ -92,9 +135,33 @@ public class PowerBus
|
|||
TotalGrant = TotalDemand - overload;
|
||||
GrantRatio = TotalDemand <= 1e-4f ? 1f : saturate(TotalGrant / TotalDemand);
|
||||
|
||||
foreach (var item in _entity.Equipment)
|
||||
if (item.Behaviors.Any(b => b is IPowerConsumer))
|
||||
item.PowerSupply = GrantRatio;
|
||||
AllocateTiers(draws);
|
||||
}
|
||||
|
||||
// Cut 5 (docs/stats-and-power-cut.md §1.3): walks tiers lowest-number-first, feeding each in full before the
|
||||
// next sees anything. remaining only ever shrinks, and once it hits 0 every later tier's ratio is 0 by the
|
||||
// same division -- a lower tier can never take from a higher one (§1.3 "a tier boundary does not leak"),
|
||||
// because a higher tier's share is committed (remaining -= grant) before a lower tier is even considered.
|
||||
// Within a tier every consumer shares that tier's one ratio against its own request, so two unequal requests
|
||||
// still divide proportionally -- the same property Cut 3 proved for the whole ship, now proved per tier.
|
||||
private void AllocateTiers(List<Draw> draws)
|
||||
{
|
||||
var tierDemand = new float[PowerTiers.Count];
|
||||
foreach (var draw in draws) tierDemand[draw.Tier] += draw.Request;
|
||||
|
||||
var remaining = TotalGrant;
|
||||
var ratios = TierGrantRatio;
|
||||
for (var tier = 0; tier < PowerTiers.Count; tier++)
|
||||
{
|
||||
var demand = tierDemand[tier];
|
||||
if (demand <= 1e-4f) { ratios[tier] = 1f; continue; }
|
||||
var grant = min(demand, remaining);
|
||||
ratios[tier] = saturate(grant / demand);
|
||||
remaining -= grant;
|
||||
}
|
||||
|
||||
foreach (var draw in draws)
|
||||
draw.Item.PowerSupply = ratios[draw.Tier];
|
||||
}
|
||||
|
||||
// Mirrors Entity.TryConsumeEnergy's old do/while exactly (draw evenly across every capacitor that still has
|
||||
|
|
|
|||
Loading…
Reference in a new issue