Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

feat(sim): budget, economy and foreign-currency reserves

eyalgolan3a8ccc35817f
diff --git a/sim/Statecraft.Sim.Tests/EconomyTests.cs b/sim/Statecraft.Sim.Tests/EconomyTests.cs
new file mode 100644
index 000000000..3000a94ae
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/EconomyTests.cs
@@ -0,0 +1,87 @@
+using System;
+using System.Collections.Generic;
+using Statecraft.Sim;
+
+public class EconomyTests
+{
+ static readonly Content C = ContentLoader.Load(Fixture.Write());
+
+ static GameState Fresh()
+ {
+ var s = new GameState { Date = new DateTime(1948, 6, 1) };
+ EconomySystem.Init(C, s);
+ s.Population["veterans"] = 1000;
+ return s;
+ }
+
+ [Fact]
+ public void InitCopiesStartValues()
+ {
+ var s = Fresh();
+ Assert.Equal(100, s.Treasury); Assert.Equal(50, s.FxReserves); Assert.Equal(1.0, s.Budget["finance_minister"]);
+ }
+
+ [Fact]
+ public void QuarterOfTaxSpendingAndTrade()
+ {
+ var s = Fresh();
+ EconomySystem.Apply(C, s, 0.25, new List<Cause>());
+ Assert.Equal(100 + 0.1 - 0.125, s.Treasury, 9); // tax 0.0004*1000/4, spend 0.0005*1000/4
+ Assert.Equal(50 + 5 - 0.025, s.FxReserves, 9); // exports 20/4, imports 0.0001*1000/4
+ }
+
+ [Fact]
+ public void MobilizationCostsTreasuryAndDeficitBecomesDebt()
+ {
+ var s = Fresh(); s.Treasury = 1; s.Mobilization = 1.0;
+ EconomySystem.Apply(C, s, 0.25, new List<Cause>());
+ Assert.Equal(0, s.Treasury);
+ Assert.Equal(10 + 0.025 - 1, s.Debt, 9);
+ }
+
+ [Fact]
+ public void FxCrisisHasHysteresis()
+ {
+ var s = Fresh(); var why = new List<Cause>();
+ s.FxReserves = -10; EconomySystem.Apply(C, s, 0.25, why);
+ Assert.Contains("fx_crisis", s.ActiveSituations);
+ s.FxReserves = 5; EconomySystem.Apply(C, s, 0.25, why); // 5 + 4.975 < 12.5 (quarter of start): still on
+ Assert.Contains("fx_crisis", s.ActiveSituations);
+ s.FxReserves = 20; EconomySystem.Apply(C, s, 0.25, why);
+ Assert.DoesNotContain("fx_crisis", s.ActiveSituations);
+ }
+
+ [Theory]
+ [InlineData("finance_minister", 1.0, null)]
+ [InlineData("finance_minister", 0.9, "Budget shares must sum to 1")]
+ [InlineData("chief_rabbi", 1.0, "Unknown office 'chief_rabbi'")]
+ [InlineData("finance_minister", -1.0, "Budget shares must sum to 1")]
+ public void AllocateValidates(string office, double share, string? error)
+ {
+ var s = Fresh();
+ Assert.Equal(error, EconomySystem.Allocate(C, s, new Dictionary<string, double> { [office] = share }));
+ }
+
+ [Fact]
+ public void BudgetShareScalesEffects()
+ {
+ var s = Fresh();
+ Assert.Equal(1.0, EffectsSystem.BudgetFactor(s, "finance_minister")); // equal split is neutral
+ s.Budget = new Dictionary<string, double> { ["finance_minister"] = 0.0, ["defense_minister"] = 1.0 };
+ Assert.Equal(0.5, EffectsSystem.BudgetFactor(s, "finance_minister"));
+ Assert.Equal(1.5, EffectsSystem.BudgetFactor(s, "defense_minister"));
+ }
+}
+
+public class EconomyNegativeShareTests
+{
+ [Fact]
+ public void NegativeShareRejectedEvenIfSumIsOne()
+ {
+ var c = ContentLoader.Load(Fixture.Write());
+ var s = new GameState();
+ EconomySystem.Init(c, s);
+ var err = EconomySystem.Allocate(c, s, new System.Collections.Generic.Dictionary<string, double> { ["finance_minister"] = 1.5, ["defense_minister"] = -0.5 });
+ Assert.Equal("Budget shares cannot be negative", err);
+ }
+}
diff --git a/sim/Statecraft.Sim/EconomySystem.cs b/sim/Statecraft.Sim/EconomySystem.cs
new file mode 100644
index 000000000..238375531
--- /dev/null
+++ b/sim/Statecraft.Sim/EconomySystem.cs
@@ -0,0 +1,55 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Statecraft.Sim
+{
+ public static class EconomySystem
+ {
+ public const string FxCrisis = "fx_crisis";
+
+ public static void Init(Content c, GameState s)
+ {
+ s.Treasury = c.Rules.TreasuryStart;
+ s.FxReserves = c.Rules.FxStart;
+ s.Budget = new Dictionary<string, double>(c.Rules.BudgetStart);
+ }
+
+ public static void Apply(Content c, GameState s, double yearFraction, List<Cause> why)
+ {
+ var r = c.Rules; var pop = s.TotalPopulation;
+ var revenue = r.TaxPerCapitaPerYear * pop * yearFraction;
+ var spending = r.SpendingPerCapitaPerYear * pop * yearFraction;
+ var mobilization = r.MobilizationCostPerYear * s.Mobilization * yearFraction;
+ s.Treasury += revenue - spending - mobilization;
+ why.Add(new Cause("economy", "treasury", revenue - spending - mobilization,
+ $"treasury {revenue - spending - mobilization:+0.00;-0.00} (tax {revenue:0.00}, spending {spending:0.00}, reserve call-up {mobilization:0.00})"));
+ if (s.Treasury < 0)
+ {
+ s.Debt += -s.Treasury;
+ why.Add(new Cause("economy", "debt", -s.Treasury, $"borrowed {-s.Treasury:0.00} to cover the deficit"));
+ s.Treasury = 0;
+ }
+
+ var exports = r.ExportsPerYear * yearFraction;
+ var imports = r.ImportsPerCapitaPerYear * pop * yearFraction;
+ s.FxReserves += exports - imports;
+ why.Add(new Cause("economy", "fx_reserves", exports - imports, $"foreign currency {exports - imports:+0.00;-0.00} (exports {exports:0.00}, imports {imports:0.00})"));
+
+ bool on = s.ActiveSituations.Contains(FxCrisis);
+ if (!on && s.FxReserves < 0) { s.ActiveSituations.Add(FxCrisis); why.Add(new Cause("economy", FxCrisis, 1, "Foreign currency reserves are exhausted")); }
+ else if (on && s.FxReserves > 0.25 * r.FxStart) { s.ActiveSituations.Remove(FxCrisis); why.Add(new Cause("economy", FxCrisis, -1, "Foreign currency crisis eased")); }
+ }
+
+ /// <summary>Returns null on success, or the reason the allocation was rejected.</summary>
+ public static string? Allocate(Content c, GameState s, Dictionary<string, double> shares)
+ {
+ var unknown = shares.Keys.FirstOrDefault(k => c.Offices.All(o => o.Id != k));
+ if (unknown != null) return $"Unknown office '{unknown}'";
+ if (Math.Abs(shares.Values.Sum() - 1.0) > 1e-6) return "Budget shares must sum to 1";
+ if (shares.Values.Any(v => v < 0)) return "Budget shares cannot be negative";
+ s.Budget = new Dictionary<string, double>(shares);
+ return null;
+ }
+ }
+}
diff --git a/sim/Statecraft.Sim/EffectsSystem.cs b/sim/Statecraft.Sim/EffectsSystem.cs
index 75688f17b..3065c95bc 100644
--- a/sim/Statecraft.Sim/EffectsSystem.cs
+++ b/sim/Statecraft.Sim/EffectsSystem.cs
@@ -13,19 +13,28 @@ namespace Statecraft.Sim
foreach (var e in c.Effects) s.EffectContributions[e.Id] = 0;
}
- /// <summary>0.5 + competence/20: a 10-competence minister delivers the full effect, a 0 delivers half.</summary>
- public static double Multiplier(Content c, string office, DateTime date)
+ /// <summary>Competence (0.5 + competence/20) times budget factor. No office: 1.0.</summary>
+ public static double Multiplier(Content c, GameState s, string office)
{
if (office == "") return 1.0;
- var holder = Offices.Holder(c, office, date);
- return holder == null ? 0.5 : 0.5 + holder.Competence / 20.0;
+ var holder = Offices.Holder(c, office, s.Date);
+ var competence = holder == null ? 0.5 : 0.5 + holder.Competence / 20.0;
+ return competence * BudgetFactor(s, office);
+ }
+
+ /// <summary>An equal split of the budget is neutral (1.0); a ministry's factor runs from 0.5 (no money) to 1.5.</summary>
+ public static double BudgetFactor(GameState s, string office)
+ {
+ if (s.Budget.Count == 0) return 1.0;
+ var share = s.Budget.TryGetValue(office, out var v) ? v : 0.0;
+ return Math.Min(1.5, 0.5 + share * s.Budget.Count / 2.0);
}
public static void Apply(Content c, GameState s, List<Cause> why)
{
// Read every source before writing anything, so effect order cannot change results.
var targets = c.Effects.ToDictionary(e => e.Id, e =>
- e.K * (s.Policies.TryGetValue(e.Source, out var p) ? p : s.Indicators[e.Source]) * Multiplier(c, e.Office, s.Date));
+ e.K * (s.Policies.TryGetValue(e.Source, out var p) ? p : s.Indicators[e.Source]) * Multiplier(c, s, e.Office));
foreach (var e in c.Effects)
{
var old = s.EffectContributions[e.Id];