using Unity.Entities; namespace ProjectM.Simulation { /// /// Pure, deterministic deposit/withdraw/total helpers for a machine's server-only / /// staging buffers — the byte-id, non-replicated twin of /// (which serves the [GhostField] global ledger). No RNG/wall-clock. DynamicBuffer is /// a handle, so mutations apply to the underlying entity buffer. Overloaded per buffer type because the two /// element types are deliberately distinct (a machine can carry both without a singleton-buffer clash). Deposit /// is a no-op for count <= 0 or resource id 0; Withdraw clamps to available and drops a row at zero. /// public static class MachineSlotMath { // ---- MachineOutput ---- public static void Deposit(DynamicBuffer buffer, byte resourceId, int count) { if (count <= 0 || resourceId == 0) return; for (int i = 0; i < buffer.Length; i++) if (buffer[i].ResourceId == resourceId) { var e = buffer[i]; e.Count += count; buffer[i] = e; return; } buffer.Add(new MachineOutput { ResourceId = resourceId, Count = count }); } public static int Withdraw(DynamicBuffer buffer, byte resourceId, int count) { if (count <= 0 || resourceId == 0) return 0; for (int i = 0; i < buffer.Length; i++) if (buffer[i].ResourceId == resourceId) { var e = buffer[i]; int taken = e.Count < count ? e.Count : count; e.Count -= taken; if (e.Count <= 0) buffer.RemoveAt(i); else buffer[i] = e; return taken; } return 0; } public static int TotalOf(DynamicBuffer buffer, byte resourceId) { int total = 0; for (int i = 0; i < buffer.Length; i++) if (buffer[i].ResourceId == resourceId) total += buffer[i].Count; return total; } // ---- MachineInput ---- public static void Deposit(DynamicBuffer buffer, byte resourceId, int count) { if (count <= 0 || resourceId == 0) return; for (int i = 0; i < buffer.Length; i++) if (buffer[i].ResourceId == resourceId) { var e = buffer[i]; e.Count += count; buffer[i] = e; return; } buffer.Add(new MachineInput { ResourceId = resourceId, Count = count }); } public static int Withdraw(DynamicBuffer buffer, byte resourceId, int count) { if (count <= 0 || resourceId == 0) return 0; for (int i = 0; i < buffer.Length; i++) if (buffer[i].ResourceId == resourceId) { var e = buffer[i]; int taken = e.Count < count ? e.Count : count; e.Count -= taken; if (e.Count <= 0) buffer.RemoveAt(i); else buffer[i] = e; return taken; } return 0; } public static int TotalOf(DynamicBuffer buffer, byte resourceId) { int total = 0; for (int i = 0; i < buffer.Length; i++) if (buffer[i].ResourceId == resourceId) total += buffer[i].Count; return total; } } }