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): legislature, coalition stability and elections

eyalgolanc6fe0ec212dd
diff --git a/sim/Statecraft.Sim.Tests/PoliticsTests.cs b/sim/Statecraft.Sim.Tests/PoliticsTests.cs
new file mode 100644
index 000000000..e5eaf2730
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/PoliticsTests.cs
@@ -0,0 +1,57 @@
+using System;
+using System.Collections.Generic;
+using Statecraft.Sim;
+
+public class PoliticsTests
+{
+ static readonly Content C = ContentLoader.Load(Fixture.Write());
+
+ [Fact]
+ public void CoalitionSeatsFollowGovernmentAndLegislatureOnDate()
+ {
+ Assert.Equal((3, 5), PoliticsSystem.CoalitionSeats(C, new DateTime(1948, 6, 1))); // gov1 [alpha] in council
+ Assert.Equal((4, 10), PoliticsSystem.CoalitionSeats(C, new DateTime(1949, 3, 1))); // gov1 [alpha] in house
+ Assert.Equal((10, 10), PoliticsSystem.CoalitionSeats(C, new DateTime(1949, 3, 10))); // gov2 [alpha,beta]
+ Assert.False(PoliticsSystem.HasMajority(C, new DateTime(1949, 3, 1)));
+ Assert.True(PoliticsSystem.HasMajority(C, new DateTime(1948, 6, 1)));
+ }
+
+ [Fact]
+ public void BudgetVoteRequirementIsPerLegislature()
+ {
+ Assert.False(PoliticsSystem.BudgetNeedsVote(C, new DateTime(1948, 6, 1)));
+ Assert.True(PoliticsSystem.BudgetNeedsVote(C, new DateTime(1949, 6, 1)));
+ }
+
+ [Fact]
+ public void VoteFailsWithoutMajorityAndPassesWithStableMajority()
+ {
+ var rng = new Rng(1);
+ var s = new GameState { Date = new DateTime(1949, 3, 1), CoalitionStability = 1.0 };
+ Assert.False(PoliticsSystem.Vote(C, s, ref rng));
+ s.Date = new DateTime(1949, 6, 1);
+ Assert.True(PoliticsSystem.Vote(C, s, ref rng));
+ }
+
+ [Fact]
+ public void StabilityMovesTowardApprovalAndCollapseEndsGame()
+ {
+ var s = new GameState { Date = new DateTime(1948, 6, 1), CoalitionStability = 1.0 };
+ EffectsSystem.Init(C, s); // approval 0.5
+ var why = new List<Cause>();
+ PoliticsSystem.UpdateStability(C, s, 0.25, why);
+ Assert.Equal(0.75, s.CoalitionStability, 6);
+ s.CoalitionStability = 0.1; s.Indicators["morale"] = 0.0;
+ PoliticsSystem.UpdateStability(C, s, 0.25, why);
+ Assert.Equal("coalition_collapse", s.GameOver);
+ }
+
+ [Fact]
+ public void ReportsNewLegislatureAndGovernment()
+ {
+ var why = new List<Cause>();
+ PoliticsSystem.ReportChanges(C, new DateTime(1949, 2, 1), new DateTime(1949, 5, 1), why);
+ Assert.Contains(why, w => w.Target == "house");
+ Assert.Contains(why, w => w.Target == "gov2");
+ }
+}
diff --git a/sim/Statecraft.Sim/Content.cs b/sim/Statecraft.Sim/Content.cs
index fea47a992..7e84b8a01 100644
--- a/sim/Statecraft.Sim/Content.cs
+++ b/sim/Statecraft.Sim/Content.cs
@@ -39,6 +39,7 @@ namespace Statecraft.Sim
{
public string Id { get; set; } = "";
public string Body { get; set; } = "";
+ public DateTime? Elected { get; set; }
public DateTime From { get; set; }
public DateTime? To { get; set; }
public int Size { get; set; }
diff --git a/sim/Statecraft.Sim/PoliticsSystem.cs b/sim/Statecraft.Sim/PoliticsSystem.cs
new file mode 100644
index 000000000..82b59f860
--- /dev/null
+++ b/sim/Statecraft.Sim/PoliticsSystem.cs
@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Statecraft.Sim
+{
+ public static class PoliticsSystem
+ {
+ public const double CollapseBelow = 0.2;
+
+ public static (int seats, int size) CoalitionSeats(Content c, DateTime date)
+ {
+ var leg = Offices.Legislature(c, date);
+ var gov = Offices.Government(c, date);
+ if (leg == null || gov == null) return (0, 0);
+ return (gov.Coalition.Sum(p => leg.Seats.TryGetValue(p, out var n) ? n : 0), leg.Size);
+ }
+
+ public static bool HasMajority(Content c, DateTime date)
+ {
+ var (seats, size) = CoalitionSeats(c, date);
+ return size > 0 && seats * 2 > size;
+ }
+
+ public static bool BudgetNeedsVote(Content c, DateTime date) => Offices.Legislature(c, date)?.BudgetApproval ?? false;
+
+ /// <summary>No majority: fails. Majority with stability >= 0.5: passes. Otherwise passes with probability 2 * stability.</summary>
+ public static bool Vote(Content c, GameState s, ref Rng rng)
+ {
+ if (!HasMajority(c, s.Date)) return false;
+ return s.CoalitionStability >= 0.5 || rng.NextDouble() < 2 * s.CoalitionStability;
+ }
+
+ public static void UpdateStability(Content c, GameState s, double yearFraction, List<Cause> why)
+ {
+ var old = s.CoalitionStability;
+ s.CoalitionStability = old + (EffectsSystem.Approval(c, s) - old) * Math.Min(1.0, yearFraction * 2);
+ if (Math.Abs(s.CoalitionStability - old) > 1e-9)
+ why.Add(new Cause("politics", "coalition_stability", s.CoalitionStability - old, $"coalition stability {s.CoalitionStability - old:+0.00;-0.00} (public approval)"));
+ if (s.CoalitionStability < CollapseBelow && s.GameOver == null)
+ {
+ s.GameOver = "coalition_collapse";
+ why.Add(new Cause("politics", "game_over", 0, "The coalition collapsed. Your premiership is over."));
+ }
+ }
+
+ public static void ReportChanges(Content c, DateTime from, DateTime to, List<Cause> why)
+ {
+ var l0 = Offices.Legislature(c, from); var l1 = Offices.Legislature(c, to);
+ if (l1 != null && l1 != l0)
+ {
+ var (seats, size) = CoalitionSeats(c, to);
+ var elected = l1.Elected.HasValue ? $", elected {l1.Elected:yyyy-MM-dd}" : "";
+ why.Add(new Cause("politics", l1.Id, 0, $"{l1.Body} convened ({l1.Size} seats{elected}); coalition holds {seats}/{size}"));
+ }
+ var g0 = Offices.Government(c, from); var g1 = Offices.Government(c, to);
+ if (g1 != null && g1 != g0)
+ why.Add(new Cause("politics", g1.Id, 0, $"{g1.Name} took office ({string.Join(", ", g1.Coalition)})"));
+ }
+ }
+}