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): MapAction for transit camps and housing near settlements from 1950
eyalgolane51b387d0184
diff --git a/content/rules.yaml b/content/rules.yamlindex 80fc6c8ea..5abff5792 100644--- a/content/rules.yaml+++ b/content/rules.yaml@@ -2,11 +2,20 @@start: 1948-05-15 # first day covered by the CBS 1948 immigration series (aliyah.yaml)start_rung: 5 # the war is under way when the slice opensattention_per_turn: 4-attention_cost: { set_policy: 1, allocate_budget: 2, choose_event_option: 1, set_mobilization: 1, security_order: 1, appoint: 2 }+attention_cost: { set_policy: 1, allocate_budget: 2, choose_event_option: 1, set_mobilization: 1, security_order: 1, appoint: 2, map_action: 1 }# Coalition stability lost by replacing a minister (Cabinet.Appoint): a seat taken from the minister's party, or a# reshuffle within it.appoint_stability_cost_other_party: 0.06appoint_stability_cost_same_party: 0.02+# Map actions (MapActions): from 1950, on land within one hex of a settlement. Transit camps also need the ma'abarot+# policy (the first ma'abara, Kesalon, opened in May 1950: https://en.wikipedia.org/wiki/Ma'abarot, checked 2026-10-10).+# Costs (IL millions) and effects are game balance: a camp is cheap and grim, permanent housing dear and welcome.+map_actions_from: 1950-01-01+map_action_max_distance: 1+map_actions_per_turn: 2+map_actions:+ transit_camp: { cost: 1.5, housing: 0.02, morale: -0.005 }+ housing: { cost: 6, housing: 0.03, morale: 0.01 }rung_attention_step: 0.25rung_min_mobilization: [0.02, 0.05, 0.15, 0.4, 0.8]ceasefire_weeks: 52 # long enough that the historical armistice events end the war firstdiff --git a/docs/determinism.md b/docs/determinism.mdindex a3a8fb4b0..756b950f5 100644--- a/docs/determinism.md+++ b/docs/determinism.md@@ -22,8 +22,8 @@ with `GoldenRun.Seed = 1948` on the real `content/`, then steps with `Historical| Run | Ends | Hash ||---|---|---|-| to 1950-01-01 (`Hash1950`) | 1950-02-26, turn 45 | `549b49cb2337e5cc9c3e626fbddd4bade1ff68f0e8c6b8cea83dc2a85386b19b` |-| to 1952-01-01 (`Hash1952`) | 1952-02-26, turn 53 | `860928f23dd1658ef31c1d981e31595ce297853a51d6d7373b8a19b2037fd9e5` |+| to 1950-01-01 (`Hash1950`) | 1950-02-26, turn 45 | `f52fa804670c861b8945bb78ff483f9c9474bace247a8e6e97b63bd306c52b42` |+| to 1952-01-01 (`Hash1952`) | 1952-02-26, turn 53 | `d07cd3d27edb2afe761275ba7c1891fc179d9cb980059a752b99fd37845c0e52` |Each constant is written in three places that must agree:- `sim/Statecraft.Sim/GoldenRun.cs`, which Unity code and players compare against;@@ -53,3 +53,5 @@ rules or content, treat it as a determinism bug.| M3 Task 5 fix 1 | 1952 | `0359fa91` | forex_crisis_1951 now applies the documented -45 fx (FRUS d513) in both options, and capital_inflow_per_year is 200 || M3 Task 6 | 1950 | `549b49cb` | hash format adds the player's appointments (empty in the run); no state change || M3 Task 6 | 1952 | `860928f2` | same |+| M3 Task 8 | 1950 | `f52fa804` | hash format adds map placements (empty in the run); no state change |+| M3 Task 8 | 1952 | `d07cd3d2` | same |diff --git a/sim/Statecraft.Sim.Tests/DeterminismTests.cs b/sim/Statecraft.Sim.Tests/DeterminismTests.csindex 02eb2da8a..aafd49319 100644--- a/sim/Statecraft.Sim.Tests/DeterminismTests.cs+++ b/sim/Statecraft.Sim.Tests/DeterminismTests.cs@@ -19,7 +19,7 @@ public class DeterminismTests/// <summary>Any change to this value is a deliberate rules or content change: update it here, in/// GoldenRun.Hash1950 and in docs/determinism.md in the same commit.</summary>- public const string GoldenHash = "549b49cb2337e5cc9c3e626fbddd4bade1ff68f0e8c6b8cea83dc2a85386b19b";+ public const string GoldenHash = "f52fa804670c861b8945bb78ff483f9c9474bace247a8e6e97b63bd306c52b42";/// <summary>Real content, seed 1948, Historical mode, HistoricalAI until the date reaches 1950-01-01.</summary>public static GameState GoldenPlaythrough(Content c) => GoldenRun.Play(c, GoldenRun.Until1950);@@ -34,7 +34,7 @@ public class DeterminismTests}/// <summary>Same rule for the second golden run, which covers the M3 content range (to 1952-01-01).</summary>- public const string GoldenHash1952 = "860928f23dd1658ef31c1d981e31595ce297853a51d6d7373b8a19b2037fd9e5";+ public const string GoldenHash1952 = "d07cd3d27edb2afe761275ba7c1891fc179d9cb980059a752b99fd37845c0e52";[Fact]public void GoldenPlaythroughTo1952HashIsFixed()diff --git a/sim/Statecraft.Sim.Tests/Fixture.cs b/sim/Statecraft.Sim.Tests/Fixture.csindex 20c94cd8b..8303a722b 100644--- a/sim/Statecraft.Sim.Tests/Fixture.cs+++ b/sim/Statecraft.Sim.Tests/Fixture.cs@@ -81,10 +81,15 @@ indicators:base: 0.5- id: food_supplybase: 0.4+ - id: housing+ base: 0.3policies:- id: rationingdefault: 0.0office: finance_minister+ - id: maabarot+ default: 0.0+ office: finance_ministereffects:- id: rationing_foodsource: rationing@@ -114,9 +119,15 @@ actors:start: 1948-05-15start_rung: 5attention_per_turn: 3-attention_cost: { set_policy: 1, allocate_budget: 1, choose_event_option: 1, set_mobilization: 1, security_order: 1, appoint: 1 }+attention_cost: { set_policy: 1, allocate_budget: 1, choose_event_option: 1, set_mobilization: 1, security_order: 1, appoint: 1, map_action: 1 }appoint_stability_cost_other_party: 0.1appoint_stability_cost_same_party: 0.03+map_actions_from: 1948-06-01+map_action_max_distance: 1+map_actions_per_turn: 2+map_actions:+ transit_camp: { cost: 2, housing: 0.05, morale: -0.02 }+ housing: { cost: 8, housing: 0.08, morale: 0.01 }rung_attention_step: 0.25ceasefire_weeks: 4rung_min_mobilization: [0.02, 0.05, 0.15, 0.4, 0.8]diff --git a/sim/Statecraft.Sim.Tests/MapActionTests.cs b/sim/Statecraft.Sim.Tests/MapActionTests.csnew file mode 100644index 000000000..0b6612339--- /dev/null+++ b/sim/Statecraft.Sim.Tests/MapActionTests.cs@@ -0,0 +1,140 @@+using System;+using System.Collections.Generic;+using System.Linq;+using Statecraft.Sim;++/// <summary>Transit camps and housing projects on the fixture map (5 x 2 hexes, town_a on (1, 0), sea on (0, 0)).</summary>+public class MapActionTests+{+ static readonly Content C = ContentLoader.Load(Fixture.Write());++ /// <summary>A state on 1948-06-01 (map actions open) with the ma'abarot policy on unless told otherwise.</summary>+ static GameState Ready(bool maabarot = true)+ {+ var s = Simulation.NewGame(C, 42, Mode.Historical);+ s.Date = new DateTime(1948, 6, 1);+ s.Policies["maabarot"] = maabarot ? 1.0 : 0.0;+ return s;+ }++ [Theory]+ [InlineData(1, 0, 0)] [InlineData(2, 0, 1)] [InlineData(0, 1, 1)] [InlineData(3, 1, 3)] [InlineData(4, 0, 3)]+ public void AxialDistanceFromTownA(int q, int r, int expected) => Assert.Equal(expected, MapActions.Distance(1, 0, q, r));++ [Fact]+ public void ATransitCampCostsMoneyAndMovesHousingAndMorale()+ {+ var s = Ready();+ var housing = s.Indicators["housing"]; var morale = s.Indicators["morale"]; var treasury = s.Treasury;+ var why = new List<Cause>();+ Assert.Null(MapActions.Apply(C, s, "transit_camp", 2, 0, why));+ Assert.Equal(treasury - 2, s.Treasury, 9);+ Assert.Equal(housing + 0.05, s.Indicators["housing"], 9);+ Assert.Equal(morale - 0.02, s.Indicators["morale"], 9);+ Assert.Equal("transit_camp", MapActions.At(s, 2, 0)!.Action);+ Assert.Contains(why, w => w.System == "map" && w.Message == "a transit camp at (2, 0): cost 2.0, housing +0.050, morale -0.020");+ }++ [Fact]+ public void AShortfallIsBorrowed()+ {+ var s = Ready();+ s.Treasury = 3;+ var why = new List<Cause>();+ Assert.Null(MapActions.Apply(C, s, "housing", 1, 1, why));+ Assert.Equal(0, s.Treasury);+ Assert.Equal(5, s.Debt, 9);+ Assert.Contains(why, w => w.Message == "borrowed 5.00 to build a housing project");+ }++ [Fact]+ public void HousingReplacesATransitCamp()+ {+ var s = Ready();+ MapActions.Apply(C, s, "transit_camp", 2, 0, new List<Cause>());+ Assert.Null(MapActions.Apply(C, s, "housing", 2, 0, new List<Cause>()));+ Assert.Equal("housing", MapActions.At(s, 2, 0)!.Action);+ Assert.Single(s.Placements);+ }++ [Theory]+ [InlineData("airport", 2, 0, "Unknown map action 'airport'")]+ [InlineData("transit_camp", 0, 0, "There is no land hex at (0, 0)")]+ [InlineData("transit_camp", 7, 7, "There is no land hex at (7, 7)")]+ [InlineData("transit_camp", 4, 0, "(4, 0) is more than 1 hex from a settlement")]+ public void IllegalPlacementsAreRejectedWithAReason(string action, int q, int r, string reason)+ {+ var s = Ready();+ var before = s.Hash();+ Assert.Equal(reason, MapActions.Apply(C, s, action, q, r, new List<Cause>()));+ Assert.Equal(before, s.Hash());+ }++ [Fact]+ public void NothingBeforeTheStartDate()+ {+ var s = Ready();+ s.Date = new DateTime(1948, 5, 31);+ Assert.Equal("Transit camps and housing projects can be placed from 1948-06-01", MapActions.Check(C, s, "housing", 2, 0));+ }++ [Fact]+ public void TransitCampsNeedTheMaabarotPolicy()+ {+ var s = Ready(maabarot: false);+ Assert.Equal("Transit camps need the ma'abarot policy", MapActions.Check(C, s, "transit_camp", 2, 0));+ Assert.Null(MapActions.Check(C, s, "housing", 2, 0));+ }++ [Fact]+ public void AtMostTwoMapActionsInOneTurn()+ {+ var s = Ready();+ Assert.Null(MapActions.Apply(C, s, "housing", 2, 0, new List<Cause>()));+ Assert.Null(MapActions.Apply(C, s, "housing", 1, 1, new List<Cause>()));+ Assert.Equal("No more than 2 map actions in one turn", MapActions.Check(C, s, "housing", 0, 1));+ s.Date = s.Date.AddMonths(3); // the next turn+ Assert.Null(MapActions.Check(C, s, "housing", 0, 1));+ }++ [Fact]+ public void AnOccupiedHexIsRejected()+ {+ var s = Ready();+ MapActions.Apply(C, s, "housing", 2, 0, new List<Cause>());+ Assert.Equal("(2, 0) already has a housing project", MapActions.Check(C, s, "transit_camp", 2, 0));+ Assert.Equal("(2, 0) already has a housing project", MapActions.Check(C, s, "housing", 2, 0));+ }++ [Fact]+ public void TheCommandGoesThroughStepAndCostsAttention()+ {+ var s0 = Simulation.NewGame(C, 42, Mode.Historical);+ s0.Date = new DateTime(1948, 6, 1);+ // At war (rung 5) each command costs 2 of the 3 attention: the refused first order is refunded, so the second fits.+ var r = Simulation.Step(C, s0, new Command[] { new MapAction("housing", 4, 0), new MapAction("housing", 2, 0) });+ Assert.Equal("(4, 0) is more than 1 hex from a settlement", r.Rejected.Single().Reason);+ Assert.Equal("housing", MapActions.At(r.State, 2, 0)!.Action);+ Assert.Empty(s0.Placements); // Step never mutates its input+ Assert.NotEqual(Simulation.Step(C, s0, new Command[0]).State.Hash(), r.State.Hash());+ }++ [Fact]+ public void AnUnknownActionInRulesIsALoadError()+ {+ var bad = Fixture.Files["rules.yaml"].Replace(" housing: { cost: 8", " airport: { cost: 8");+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = bad })));+ Assert.Equal("map_actions.airport", ex.Field);+ }++ [Fact]+ public void RealContentOpensCampsWithTheMaabarotDecision()+ {+ var c = ContentLoader.Load(RealContentTests.ContentRoot());+ var s = GoldenRun.Play(c, new DateTime(1950, 9, 1));+ var tlv = c.SettlementHexes.Single(x => x.Id == "tel_aviv");+ Assert.Null(MapActions.Check(c, s, "transit_camp", tlv.Q, tlv.R));+ var s1949 = GoldenRun.Play(c, new DateTime(1949, 6, 1));+ Assert.Equal("Transit camps and housing projects can be placed from 1950-01-01", MapActions.Check(c, s1949, "housing", tlv.Q, tlv.R));+ }+}diff --git a/sim/Statecraft.Sim/AttentionSystem.cs b/sim/Statecraft.Sim/AttentionSystem.csindex bc1366868..522e670b6 100644--- a/sim/Statecraft.Sim/AttentionSystem.cs+++ b/sim/Statecraft.Sim/AttentionSystem.cs@@ -7,7 +7,7 @@ namespace Statecraft.Simpublic static class AttentionSystem{static readonly HashSet<string> Kinds = new HashSet<string>- { "set_policy", "allocate_budget", "choose_event_option", "set_mobilization", "security_order", "appoint" };+ { "set_policy", "allocate_budget", "choose_event_option", "set_mobilization", "security_order", "appoint", "map_action" };public static IReadOnlyCollection<string> CommandKinds => Kinds;diff --git a/sim/Statecraft.Sim/Commands.cs b/sim/Statecraft.Sim/Commands.csindex a7b53014b..d6aac43c2 100644--- a/sim/Statecraft.Sim/Commands.cs+++ b/sim/Statecraft.Sim/Commands.cs@@ -49,6 +49,14 @@ namespace Statecraft.Simpublic Appoint(string office, string person) { Office = office; Person = person; }}+ /// <summary>Builds <see cref="Action"/> (MapActions.TransitCamp or MapActions.Housing) on hex (Q, R).</summary>+ public sealed class MapAction : Command+ {+ public override string Kind => MapActions.Kind;+ public string Action { get; } public int Q { get; } public int R { get; }+ public MapAction(string action, int q, int r) { Action = action; Q = q; R = r; }+ }+public sealed class Rejection{public Command Command { get; } public string Reason { get; }diff --git a/sim/Statecraft.Sim/Content.cs b/sim/Statecraft.Sim/Content.csindex bded155be..c48f8c5b8 100644--- a/sim/Statecraft.Sim/Content.cs+++ b/sim/Statecraft.Sim/Content.cs@@ -133,11 +133,29 @@ namespace Statecraft.Simpublic double AppointStabilityCostOtherParty { get; set; }/// <summary>Coalition stability lost when an appointment replaces a minister with one of the same party.</summary>public double AppointStabilityCostSameParty { get; set; }+ /// <summary>First day map actions can be placed.</summary>+ public DateTime MapActionsFrom { get; set; }+ /// <summary>How far (in hexes) a map action may be from a hex that owns a settlement.</summary>+ public int MapActionMaxDistance { get; set; } = 1;+ /// <summary>How many map actions may be built in one turn.</summary>+ public int MapActionsPerTurn { get; set; } = 2;+ public Dictionary<string, MapActionRule> MapActions { get; set; } = new Dictionary<string, MapActionRule>();public double NaturalIncreasePerYear { get; set; }public Dictionary<string, double> InitialPopulation { get; set; } = new Dictionary<string, double>();public Dictionary<string, double> BudgetStart { get; set; } = new Dictionary<string, double>();}+ /// <summary>Price and effect of one map action (rules.yaml map_actions).</summary>+ public sealed class MapActionRule+ {+ /// <summary>Treasury cost, in treasury units.</summary>+ public double Cost { get; set; }+ /// <summary>Change to the housing indicator (absorption).</summary>+ public double Housing { get; set; }+ /// <summary>Change to the morale indicator (public support).</summary>+ public double Morale { get; set; }+ }+public sealed class AliyahPeriod{public DateTime From { get; set; }diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.csindex 46da18380..b7074853d 100644--- a/sim/Statecraft.Sim/ContentLoader.cs+++ b/sim/Statecraft.Sim/ContentLoader.cs@@ -373,6 +373,18 @@ namespace Statecraft.SimFinite("rules.yaml", key, v);Unit("rules.yaml", "appoint_stability_cost_other_party", r.AppointStabilityCostOtherParty);Unit("rules.yaml", "appoint_stability_cost_same_party", r.AppointStabilityCostSameParty);+ if (r.MapActionMaxDistance < 0) throw new ContentException("rules.yaml", "map_action_max_distance", "must be >= 0");+ if (r.MapActionsPerTurn < 1) throw new ContentException("rules.yaml", "map_actions_per_turn", "must be >= 1");+ foreach (var kv in r.MapActions)+ {+ if (!MapActions.Actions.Contains(kv.Key)) throw new ContentException("rules.yaml", $"map_actions.{kv.Key}", "unknown map action (transit_camp or housing)");+ if (!(kv.Value.Cost >= 0 && IsFinite(kv.Value.Cost))) throw new ContentException("rules.yaml", $"map_actions.{kv.Key}.cost", "must be a finite number >= 0");+ foreach (var (key, v) in new[] { ("housing", kv.Value.Housing), ("morale", kv.Value.Morale) })+ if (!(v >= -1 && v <= 1)) throw new ContentException("rules.yaml", $"map_actions.{kv.Key}.{key}", "must be a number from -1 to 1");+ }+ if (r.MapActions.Count > 0 && r.MapActionsFrom == default) throw new ContentException("rules.yaml", "map_actions_from", "required when map_actions are defined");+ if (r.MapActions.Count > 0 && (!indicators.Contains("housing") || !indicators.Contains("morale")))+ throw new ContentException("rules.yaml", "map_actions", "needs housing and morale indicators in model.yaml");if (r.FxCrisisFoodPerYear > 0) throw new ContentException("rules.yaml", "fx_crisis_food_per_year", "must be <= 0 (a crisis cuts food imports)");if (r.FxCrisisFoodPerYear < 0 && !indicators.Contains("food_supply")) throw new ContentException("rules.yaml", "fx_crisis_food_per_year", "needs a food_supply indicator in model.yaml");foreach (var kv in r.InitialPopulation) Count("rules.yaml", $"initial_population.{kv.Key}", kv.Value);diff --git a/sim/Statecraft.Sim/GameState.cs b/sim/Statecraft.Sim/GameState.csindex d776c0944..d3e44cf5b 100644--- a/sim/Statecraft.Sim/GameState.cs+++ b/sim/Statecraft.Sim/GameState.cs@@ -37,6 +37,8 @@ namespace Statecraft.Sim/// <summary>Player appointments by office id; valid while the government in AppointedUnder is in office (Cabinet).</summary>public Dictionary<string, Appointment> Appointments = new Dictionary<string, Appointment>();public string AppointedUnder = "";+ /// <summary>Map actions built so far, in order (MapActions).</summary>+ public List<Placement> Placements = new List<Placement>();public string? GameOver;public double TotalPopulation => Population.Values.Sum();@@ -54,6 +56,7 @@ namespace Statecraft.Simc.FiredEvents = new SortedSet<string>(FiredEvents, StringComparer.Ordinal);c.PendingEvents = new List<string>(PendingEvents);c.Appointments = new Dictionary<string, Appointment>(Appointments);+ c.Placements = new List<Placement>(Placements);return c;}@@ -83,6 +86,10 @@ namespace Statecraft.Simforeach (var kv in Appointments.OrderBy(k => k.Key, StringComparer.Ordinal))sb.Append(kv.Key).Append('=').Append(kv.Value.Person).Append('@').Append(kv.Value.From.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)).Append(';');sb.Append("}under=").Append(AppointedUnder).Append(';');+ sb.Append("map[");+ foreach (var p in Placements)+ sb.Append(p.Action).Append('@').Append(p.Q).Append(',').Append(p.R).Append('@').Append(p.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)).Append(';');+ sb.Append(']');sb.Append("over=").Append(GameOver ?? "");using var sha = SHA256.Create();var bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()));diff --git a/sim/Statecraft.Sim/GoldenRun.cs b/sim/Statecraft.Sim/GoldenRun.csindex f7b00e5e5..608873737 100644--- a/sim/Statecraft.Sim/GoldenRun.cs+++ b/sim/Statecraft.Sim/GoldenRun.cs@@ -11,12 +11,12 @@ namespace Statecraft.Sim/// <summary>Final hash of Play(content, Until1950). Changing it is a deliberate rules or content change:/// update it, DeterminismTests and docs/determinism.md in the same commit and say why in the message.</summary>- public const string Hash1950 = "549b49cb2337e5cc9c3e626fbddd4bade1ff68f0e8c6b8cea83dc2a85386b19b";+ public const string Hash1950 = "f52fa804670c861b8945bb78ff483f9c9474bace247a8e6e97b63bd306c52b42";public static readonly DateTime Until1950 = new DateTime(1950, 1, 1);/// <summary>Final hash of Play(content, Until1952): the M3 content range (to 31 December 1951).</summary>- public const string Hash1952 = "860928f23dd1658ef31c1d981e31595ce297853a51d6d7373b8a19b2037fd9e5";+ public const string Hash1952 = "d07cd3d27edb2afe761275ba7c1891fc179d9cb980059a752b99fd37845c0e52";public static readonly DateTime Until1952 = new DateTime(1952, 1, 1);diff --git a/sim/Statecraft.Sim/MapActions.cs b/sim/Statecraft.Sim/MapActions.csnew file mode 100644index 000000000..b560bf84d--- /dev/null+++ b/sim/Statecraft.Sim/MapActions.cs@@ -0,0 +1,85 @@+using System;+using System.Collections.Generic;+using System.Linq;++namespace Statecraft.Sim+{+ /// <summary>Something the player built on a hex. Immutable, so GameState.Clone can copy the list shallowly.</summary>+ public sealed class Placement+ {+ public string Action { get; }+ public int Q { get; }+ public int R { get; }+ public DateTime Date { get; }+ public Placement(string action, int q, int r, DateTime date) { Action = action; Q = q; R = r; Date = date; }+ }++ /// <summary>Map actions (M3 scope: absorption from 1950). A transit camp (ma'abara) or a housing project goes on a land+ /// hex within rules map_action_max_distance of a hex that owns a settlement (content/map/settlement_hexes.yaml). It+ /// costs treasury (a shortfall is borrowed, as in EconomySystem) and moves housing (absorption) and morale (support).+ /// At most rules map_actions_per_turn are built in one turn (upkeep and camp capacity are M4).</summary>+ public static class MapActions+ {+ public const string Kind = "map_action";+ public const string TransitCamp = "transit_camp";+ public const string Housing = "housing";+ public static readonly IReadOnlyList<string> Actions = new[] { TransitCamp, Housing };+ public const string MaabarotPolicy = "maabarot";++ /// <summary>Axial hex distance (Red Blob Games).</summary>+ public static int Distance(int q1, int r1, int q2, int r2) =>+ (Math.Abs(q1 - q2) + Math.Abs(q1 + r1 - q2 - r2) + Math.Abs(r1 - r2)) / 2;++ /// <summary>The placement on (q, r), or null.</summary>+ public static Placement? At(GameState s, int q, int r) => s.Placements.FirstOrDefault(p => p.Q == q && p.R == r);++ static string Name(string action) => action == TransitCamp ? "a transit camp" : "a housing project";++ /// <summary>Null when <paramref name="action"/> may be placed on (q, r) now, else the reason shown to the player.</summary>+ public static string? Check(Content c, GameState s, string action, int q, int r)+ {+ if (!Actions.Contains(action)) return $"Unknown map action '{action}'";+ var rule = c.Rules.MapActions.TryGetValue(action, out var x) ? x : null;+ if (rule == null) return $"Map action '{action}' is not available in this content";+ // Placements carry the date they were built, and the date only advances at the end of a turn.+ var built = s.Placements.Count(p => p.Date == s.Date);+ if (built >= c.Rules.MapActionsPerTurn)+ return FormattableString.Invariant($"No more than {c.Rules.MapActionsPerTurn} map actions in one turn");+ if (s.Date < c.Rules.MapActionsFrom)+ return FormattableString.Invariant($"Transit camps and housing projects can be placed from {c.Rules.MapActionsFrom:yyyy-MM-dd}");+ if (action == TransitCamp && !(s.Policies.TryGetValue(MaabarotPolicy, out var m) && m >= 0.5))+ return "Transit camps need the ma'abarot policy";+ var hex = c.HexAt(q, r);+ if (hex == null || !hex.Land) return FormattableString.Invariant($"There is no land hex at ({q}, {r})");+ var max = c.Rules.MapActionMaxDistance;+ if (!c.SettlementHexes.Any(sh => Distance(q, r, sh.Q, sh.R) <= max))+ return FormattableString.Invariant($"({q}, {r}) is more than {max} {(max == 1 ? "hex" : "hexes")} from a settlement");+ var here = At(s, q, r);+ if (here != null && !(action == Housing && here.Action == TransitCamp))+ return FormattableString.Invariant($"({q}, {r}) already has {Name(here.Action)}");+ return null;+ }++ /// <summary>Returns null and builds, or the reason (state untouched). Housing on a transit camp replaces the camp.</summary>+ public static string? Apply(Content c, GameState s, string action, int q, int r, List<Cause> why)+ {+ var reason = Check(c, s, action, q, r);+ if (reason != null) return reason;+ var rule = c.Rules.MapActions[action];+ s.Placements.RemoveAll(p => p.Q == q && p.R == r);+ s.Placements.Add(new Placement(action, q, r, s.Date));+ s.Treasury -= rule.Cost;+ if (s.Treasury < 0)+ {+ s.Debt += -s.Treasury;+ why.Add(new Cause("economy", "debt", -s.Treasury, FormattableString.Invariant($"borrowed {-s.Treasury:0.00} to build {Name(action)}")));+ s.Treasury = 0;+ }+ var housing = rule.Housing == 0 ? 0 : EffectsSystem.Shift(c, s, "housing", rule.Housing);+ var morale = rule.Morale == 0 ? 0 : EffectsSystem.Shift(c, s, "morale", rule.Morale);+ why.Add(new Cause("map", action, 1, FormattableString.Invariant(+ $"{Name(action)} at ({q}, {r}): cost {rule.Cost:0.0}, housing {housing:+0.000;-0.000;+0.000}, morale {morale:+0.000;-0.000;+0.000}")));+ return null;+ }+ }+}diff --git a/sim/Statecraft.Sim/Simulation.cs b/sim/Statecraft.Sim/Simulation.csindex f7a63ab98..a615170d2 100644--- a/sim/Statecraft.Sim/Simulation.cs+++ b/sim/Statecraft.Sim/Simulation.cs@@ -108,6 +108,8 @@ namespace Statecraft.Simreturn SecuritySystem.SetMobilization(c, s, m.Level, why);case Appoint a:return Cabinet.Appoint(c, s, a.Office, a.Person, why);+ case MapAction m:+ return MapActions.Apply(c, s, m.Action, m.Q, m.R, why);case SecurityOrder o:// long arithmetic: an extreme delta clamps to the ladder's end instead of wrapping around.SecuritySystem.SetRung(c, s, (int)Math.Max(1L, Math.Min(5L, (long)s.Rung + o.Delta)), why);