EB-2: felt spend - turrets burn a shared Charge pool, ledger-fed Fabricator mints it from Ore

Mined Ore now has an ongoing sink: a ledger-fed Fabricator converts Ore->Charge
(1 Ore -> 3 Charge / 30t) and turrets spend Charge per shot, soft-failing (no
shot, no cooldown burn) when the shared pool runs dry.

- ResourceId.Charge=4 rides the existing [GhostField] StorageEntry ledger (no new wire).
- TurretFireSystem: single ledger resolve + atomic spend / soft-fail / partial-refund.
- Fabricator.InputFromLedger (byte, server-only) feeds input from the shared ledger,
  read live in-loop so two machines split a finite pool; both modes deposit to ledger.
- HudSystem: violet Charge chip + global quiet-turret cue when siege && Charge==0.
- StorageMath.TotalOf backs the affordability read; catalog re-enables the Fabricator (4 entries).

See DR-033.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-12 19:14:52 -07:00
parent e04cdea44f
commit 2da29783fd
10 changed files with 94 additions and 28 deletions
@@ -75,16 +75,21 @@ namespace ProjectM.Server
byte inId = fab.ValueRO.InResourceId;
int inAmount = fab.ValueRO.InAmount;
// Input-limited: never produce more than the buffered input affords (no mint-from-nothing). A
// zero/negative recipe input amount is treated as unsatisfiable rather than dividing by zero.
int affordable = inAmount > 0
? MachineSlotMath.TotalOf(input, inId) / inAmount
: 0;
// Input-limited: never produce more than the available input affords (no mint-from-nothing). EB-2:
// a ledger-fed Fabricator (InputFromLedger != 0) sources its input from the SHARED ledger (read LIVE
// here so a 2nd ledger-fed Fabricator sees the 1st's same-tick withdrawal) instead of MachineInput;
// both modes deposit the output to the ledger. A zero/negative input amount is unsatisfiable.
bool fromLedger = fab.ValueRO.InputFromLedger != 0;
int available = fromLedger ? StorageMath.TotalOf(ledger, inId) : MachineSlotMath.TotalOf(input, inId);
int affordable = inAmount > 0 ? available / inAmount : 0;
int runs = math.min(cycles, affordable);
if (runs > 0)
{
MachineSlotMath.Withdraw(input, inId, inAmount * runs);
if (fromLedger)
StorageMath.Withdraw(ledger, inId, inAmount * runs);
else
MachineSlotMath.Withdraw(input, inId, inAmount * runs);
StorageMath.Deposit(ledger, (ushort)fab.ValueRO.OutResourceId, fab.ValueRO.OutAmount * runs);
}
@@ -28,6 +28,7 @@ namespace ProjectM.Server
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<NetworkTime>();
state.RequireForUpdate<ResourceLedger>();
state.RequireForUpdate(state.GetEntityQuery(ComponentType.ReadOnly<Turret>()));
}
@@ -59,6 +60,12 @@ namespace ProjectM.Server
return;
}
// EB-2: resolve the shared ledger ONCE (NEVER GetSingleton<StorageEntry> — a 2nd StorageEntry buffer
// exists on the base container). Turrets withdraw Charge from it sequentially (a finite pool split in
// query order; later turrets soft-fail when it empties).
var ledgerEntity = SystemAPI.GetSingletonEntity<ResourceLedger>();
var ledger = SystemAPI.GetBuffer<StorageEntry>(ledgerEntity);
var ecb = new EntityCommandBuffer(Allocator.Temp);
foreach (var (ps, turret, xform, region) in
@@ -92,14 +99,26 @@ namespace ProjectM.Server
if (best >= 0)
{
ecb.AppendToBuffer(huskEntities[best], new DamageEvent
// EB-2 felt spend: a shot costs Charge from the shared ledger. Gate BOTH the damage AND the
// cooldown advance on a SUCCESSFUL withdraw — out of Charge = SOFT-FAIL (no shot, no cooldown
// burn, so the turret fires the instant Charge returns). Refund a partial (cost>1 underflow).
int cost = math.max(1, Tuning.TurretChargeCostPerShot);
int got = StorageMath.Withdraw(ledger, ResourceId.Charge, cost);
if (got >= cost)
{
Amount = turret.ValueRO.Damage,
SourceNetworkId = -1,
SourceTick = TickUtil.NonZero(now),
});
uint cd = (uint)math.max(1, turret.ValueRO.CooldownTicks);
ps.ValueRW.NextTick = TickUtil.NonZero(now + cd);
ecb.AppendToBuffer(huskEntities[best], new DamageEvent
{
Amount = turret.ValueRO.Damage,
SourceNetworkId = -1,
SourceTick = TickUtil.NonZero(now),
});
uint cd = (uint)math.max(1, turret.ValueRO.CooldownTicks);
ps.ValueRW.NextTick = TickUtil.NonZero(now + cd);
}
else if (got > 0)
{
StorageMath.Deposit(ledger, ResourceId.Charge, got); // never consume Charge without firing
}
}
}