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.

Add typed farming rates and synthetic field output (Refs #1694) (#1882)

Eyal Golan8a19878ea477
diff --git a/changelog.d/1694-synthetic-fields.md b/changelog.d/1694-synthetic-fields.md
new file mode 100644
index 000000000..e429717c6
--- /dev/null
+++ b/changelog.d/1694-synthetic-fields.md
@@ -0,0 +1 @@
+- Add typed farming rates and a detached synthetic staffed-field output evaluator for GDD 0.3 groundwork.
diff --git a/sim/Statecraft.Sim.Tests/Gdd03SyntheticFieldTests.cs b/sim/Statecraft.Sim.Tests/Gdd03SyntheticFieldTests.cs
new file mode 100644
index 000000000..7d52e88fe
--- /dev/null
+++ b/sim/Statecraft.Sim.Tests/Gdd03SyntheticFieldTests.cs
@@ -0,0 +1,175 @@
+using Statecraft.Sim;
+using System.Globalization;
+
+public sealed class Gdd03SyntheticFieldTests
+{
+ // R-FIELD-002: fixture arithmetic only; these tests do not exercise a turn path.
+ static Gdd03BalanceCatalogue Catalogue() => ContentLoader.Load(RealContentTests.ContentRoot()).Gdd03Balance!;
+ static Gdd03SyntheticFieldInput Field(CivilianTier? tier = CivilianTier.Trained,
+ bool prepared = true, bool workable = true, bool friendly = true, bool eligible = true) =>
+ new Gdd03SyntheticFieldInput("synthetic-field", prepared, workable, friendly, eligible, tier);
+
+ [Theory]
+ [InlineData(CivilianTier.Novice, 2)]
+ [InlineData(CivilianTier.Trained, 3)]
+ [InlineData(CivilianTier.Veteran, 5)]
+ public void R_FIELD_002_StaffedOutputUsesFrozenTier(CivilianTier tier, int expected)
+ {
+ var rates = Gdd03FieldRates.FromCatalogue(Catalogue());
+ var input = Field(tier);
+ var output = Gdd03SyntheticFieldEvaluator.Evaluate(rates, input);
+ Assert.Equal(1, rates.SlotsPerField);
+ Assert.Equal(expected, output.Food);
+ Assert.Equal(0, output.Money);
+ Assert.True(output.HasSyntheticWork);
+ Assert.Equal(tier, input.FrozenTier);
+ Assert.Equal(output.Food, Gdd03SyntheticFieldEvaluator.Evaluate(rates, input).Food);
+ }
+
+ [Theory]
+ [InlineData(false, true, true, true)]
+ [InlineData(true, false, true, true)]
+ [InlineData(true, true, false, true)]
+ [InlineData(true, true, true, false)]
+ [InlineData(false, false, false, false)]
+ public void R_FIELD_002_ExplicitInvalidFlagsProduceNothing(bool prepared, bool workable, bool friendly, bool eligible)
+ {
+ var output = Gdd03SyntheticFieldEvaluator.Evaluate(Gdd03FieldRates.FromCatalogue(Catalogue()),
+ Field(prepared: prepared, workable: workable, friendly: friendly, eligible: eligible));
+ Assert.Equal(0, output.Food);
+ Assert.Equal(0, output.Money);
+ Assert.False(output.HasSyntheticWork);
+ }
+
+ [Fact]
+ public void R_FIELD_002_UnstaffedPreparedFieldProducesNothing()
+ {
+ var output = Gdd03SyntheticFieldEvaluator.Evaluate(Gdd03FieldRates.FromCatalogue(Catalogue()), Field(null));
+ Assert.Equal(0, output.Food);
+ Assert.Equal(0, output.Money);
+ Assert.False(output.HasSyntheticWork);
+ }
+
+ [Theory]
+ [InlineData(CivilianTier.None)]
+ [InlineData(CivilianTier.Apprentice)]
+ [InlineData(CivilianTier.Expert)]
+ [InlineData((CivilianTier)999)] // Test-only synthetic invalid enum.
+ public void R_FIELD_002_MalformedTierRefusesEvenWhenFlagsDisableWork(CivilianTier tier)
+ {
+ Assert.Throws<ArgumentException>(() => Field(tier, prepared: false));
+ }
+
+ [Theory]
+ [InlineData(null)] [InlineData("")] [InlineData(" ")]
+ public void R_FIELD_002_RequiresExplicitSyntheticFieldId(string? id)
+ {
+ Assert.Throws<ArgumentException>(() => new Gdd03SyntheticFieldInput(id!, true, true, true, true, null));
+ }
+
+ [Theory]
+ [InlineData("he-IL")] [InlineData("en-US")]
+ public void R_FIELD_002_SnapshotsTypedRatesWithoutCultureDependentText(string culture)
+ {
+ var previous = CultureInfo.CurrentCulture;
+ var previousUi = CultureInfo.CurrentUICulture;
+ try
+ {
+ CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
+ CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
+ var catalogue = Catalogue();
+ // Test-only synthetic rates, deliberately different from the catalogue's 2/3/5.
+ catalogue.Values["farmer.output"].Values = new List<decimal> { 7m, 11m, 13m };
+ var rates = Gdd03FieldRates.FromCatalogue(catalogue);
+ catalogue.Values["farmer.output"].Values[0] = 99m; // Test-only mutation after snapshot.
+ Assert.Equal(7, Gdd03SyntheticFieldEvaluator.Evaluate(rates, Field(CivilianTier.Novice)).Food);
+ Assert.Equal(11, Gdd03SyntheticFieldEvaluator.Evaluate(rates, Field(CivilianTier.Trained)).Food);
+ Assert.Equal(13, Gdd03SyntheticFieldEvaluator.Evaluate(rates, Field(CivilianTier.Veteran)).Food);
+ }
+ finally
+ {
+ CultureInfo.CurrentCulture = previous;
+ CultureInfo.CurrentUICulture = previousUi;
+ }
+ }
+
+ [Theory]
+ [InlineData("farmer.output")] [InlineData("field.slots")]
+ public void R_FIELD_002_MissingOrDeferredRatesNeverFallBack(string key)
+ {
+ var catalogue = Catalogue();
+ catalogue.Values.Remove(key);
+ AssertKey(catalogue, key);
+ catalogue.Values[key] = new Gdd03DesignValue { Basis = "Deferred", Expression = "fixture-only-unresolved", Note = "synthetic" };
+ AssertKey(catalogue, key);
+ }
+
+ [Theory]
+ [InlineData("farmer.output")] [InlineData("field.slots")]
+ public void R_FIELD_002_RejectsMalformedRowShapes(string key)
+ {
+ Action<Gdd03DesignValue>[] mutations = {
+ row => row.Basis = "Unknown",
+ row => row.Note = "",
+ row => row.Values = null!,
+ row => row.Values.Clear(),
+ row => row.Values.Add(1m), // Test-only malformed arity.
+ row => row.Expression = "fixture-only-symbol",
+ row => row.Parts = null!,
+ row => row.Parts["fixture-only-part"] = new Gdd03DesignValue()
+ };
+ foreach (var mutate in mutations)
+ {
+ var catalogue = Catalogue();
+ mutate(catalogue.Values[key]);
+ AssertKey(catalogue, key);
+ }
+ var nullRow = Catalogue();
+ nullRow.Values[key] = null!;
+ AssertKey(nullRow, key);
+ }
+
+ [Theory]
+ [InlineData("-1")] [InlineData("1.5")] [InlineData("2147483648")]
+ public void R_FIELD_002_RejectsNegativeFractionalAndOverflowFood(string number)
+ {
+ // Test-only malformed numbers; parse explicitly with InvariantCulture under either locale.
+ var catalogue = Catalogue();
+ catalogue.Values["farmer.output"].Values[0] = decimal.Parse(number, CultureInfo.InvariantCulture);
+ AssertKey(catalogue, "farmer.output");
+ }
+
+ [Theory]
+ [InlineData("0")] [InlineData("2")] [InlineData("1.5")]
+ public void R_FIELD_002_RejectsUnsupportedSlotCounts(string number)
+ {
+ var catalogue = Catalogue();
+ catalogue.Values["field.slots"].Values[0] = decimal.Parse(number, CultureInfo.InvariantCulture);
+ AssertKey(catalogue, "field.slots");
+ }
+
+ [Fact]
+ public void R_FIELD_002_RejectsWrongIdentityAndNullArguments()
+ {
+ var catalogue = Catalogue();
+ var rates = Gdd03FieldRates.FromCatalogue(catalogue);
+ foreach (var identity in new[] { SaveGame.GddRuleset, "legacy", "gdd-0.3-extra" })
+ {
+ catalogue.Ruleset = identity;
+ Assert.Throws<ContentException>(() => Gdd03FieldRates.FromCatalogue(catalogue));
+ }
+ Assert.Throws<ArgumentNullException>(() => Gdd03FieldRates.FromCatalogue(null!));
+ catalogue.Ruleset = SaveGame.Gdd03Ruleset;
+ catalogue.Values = null!;
+ Assert.Throws<ContentException>(() => Gdd03FieldRates.FromCatalogue(catalogue));
+ Assert.Throws<ArgumentNullException>(() => Gdd03SyntheticFieldEvaluator.Evaluate(null!, Field()));
+ Assert.Throws<ArgumentNullException>(() => Gdd03SyntheticFieldEvaluator.Evaluate(rates, null!));
+ }
+
+ static void AssertKey(Gdd03BalanceCatalogue catalogue, string key)
+ {
+ var error = Assert.Throws<ContentException>(() => Gdd03FieldRates.FromCatalogue(catalogue));
+ Assert.Equal(Gdd03BalanceCatalogue.FileName, error.File);
+ Assert.Contains(key, error.Message);
+ }
+}
diff --git a/sim/Statecraft.Sim/Gdd03SyntheticFields.cs b/sim/Statecraft.Sim/Gdd03SyntheticFields.cs
new file mode 100644
index 000000000..6ec09cb30
--- /dev/null
+++ b/sim/Statecraft.Sim/Gdd03SyntheticFields.cs
@@ -0,0 +1,111 @@
+using System;
+
+namespace Statecraft.Sim
+{
+ /// <summary>Detached typed catalogue snapshot for synthetic field evaluation only.
+ /// Construction does not enable farming in any ruleset or validate unrelated catalogue rows.</summary>
+ public sealed class Gdd03FieldRates
+ {
+ readonly int noviceFood;
+ readonly int trainedFood;
+ readonly int veteranFood;
+ public int SlotsPerField { get; }
+
+ Gdd03FieldRates(int novice, int trained, int veteran, int slots)
+ {
+ noviceFood = novice; trainedFood = trained; veteranFood = veteran; SlotsPerField = slots;
+ }
+
+ // R-FIELD-002: content/gdd03-balance.yaml:142-145,198-201.
+ public static Gdd03FieldRates FromCatalogue(Gdd03BalanceCatalogue catalogue)
+ {
+ if (catalogue == null) throw new ArgumentNullException(nameof(catalogue));
+ if (catalogue.Ruleset != SaveGame.Gdd03Ruleset)
+ throw new ContentException(Gdd03BalanceCatalogue.FileName, "ruleset", "requires exact gdd-0.3 catalogue identity");
+ var food = ReadIntegers(catalogue, "farmer.output", 3);
+ var slots = ReadIntegers(catalogue, "field.slots", 1);
+ // This single-field contract represents the catalogued one farmer per field, not multi-worker scaling.
+ if (slots[0] != 1)
+ throw new ContentException(Gdd03BalanceCatalogue.FileName, "values.field.slots", "requires one farmer per field");
+ return new Gdd03FieldRates(food[0], food[1], food[2], slots[0]);
+ }
+
+ static int[] ReadIntegers(Gdd03BalanceCatalogue catalogue, string key, int count)
+ {
+ var at = "values." + key;
+ if (catalogue.Values == null || !catalogue.Values.TryGetValue(key, out var row) || row == null
+ || (row.Basis != "Agreed" && row.Basis != "Default") || string.IsNullOrWhiteSpace(row.Note)
+ || row.Values == null || row.Values.Count != count || !string.IsNullOrEmpty(row.Expression)
+ || row.Parts == null || row.Parts.Count != 0)
+ throw new ContentException(Gdd03BalanceCatalogue.FileName, at, "requires settled exact integer values with provenance; no Deferred or fallback values");
+ var values = new int[count];
+ for (int i = 0; i < count; i++)
+ {
+ var value = row.Values[i];
+ if (value < 0 || value > int.MaxValue || decimal.Truncate(value) != value)
+ throw new ContentException(Gdd03BalanceCatalogue.FileName, at, "requires nonnegative Food/slot integers within Int32 range");
+ values[i] = checked((int)value);
+ }
+ return values;
+ }
+
+ internal int FoodFor(CivilianTier tier) => tier switch {
+ CivilianTier.Novice => noviceFood,
+ CivilianTier.Trained => trainedFood,
+ CivilianTier.Veteran => veteranFood,
+ _ => throw new ArgumentException("Requires a frozen farmer tier.", nameof(tier))
+ };
+ }
+
+ /// <summary>Explicit fixture facts, not assignment authority or inferred terrain/control.
+ /// Eligible is supplied by the fixture; no reception, tools, profession or exclusivity policy is decided here.
+ /// A null frozen tier means unstaffed. The caller supplies the current tier without promotion.</summary>
+ public sealed class Gdd03SyntheticFieldInput
+ {
+ public string SyntheticFieldId { get; }
+ public bool Prepared { get; }
+ public bool Workable { get; }
+ public bool Friendly { get; }
+ public bool Eligible { get; }
+ public CivilianTier? FrozenTier { get; }
+
+ public Gdd03SyntheticFieldInput(string syntheticFieldId, bool prepared, bool workable,
+ bool friendly, bool eligible, CivilianTier? frozenTier)
+ {
+ if (string.IsNullOrWhiteSpace(syntheticFieldId))
+ throw new ArgumentException("An explicit synthetic field id is required.", nameof(syntheticFieldId));
+ if (frozenTier.HasValue && frozenTier != CivilianTier.Novice
+ && frozenTier != CivilianTier.Trained && frozenTier != CivilianTier.Veteran)
+ throw new ArgumentException("Use a farmer tier or null for an unstaffed fixture.", nameof(frozenTier));
+ SyntheticFieldId = syntheticFieldId; Prepared = prepared; Workable = workable;
+ Friendly = friendly; Eligible = eligible; FrozenTier = frozenTier;
+ }
+ }
+
+ /// <summary>Arithmetic result only. HasSyntheticWork is not a work receipt, XP credit or deduplication key.</summary>
+ public sealed class Gdd03SyntheticFieldOutput
+ {
+ public int Food { get; }
+ public int Money => 0; // GDD 605277df docs/gdd/cities.md:13: farming creates no Money.
+ public bool HasSyntheticWork { get; }
+ internal Gdd03SyntheticFieldOutput(int food, bool hasSyntheticWork)
+ {
+ Food = food; HasSyntheticWork = hasSyntheticWork;
+ }
+ }
+
+ /// <summary>Pure single-field fixture evaluator. No state mutation, population table, batch occupancy,
+ /// control transition, work receipt or Food settlement. No shipping system calls this API.</summary>
+ public static class Gdd03SyntheticFieldEvaluator
+ {
+ // R-FIELD-002: synthetic arithmetic subset of GDD 605277df docs/gdd/cities.md:13.
+ public static Gdd03SyntheticFieldOutput Evaluate(Gdd03FieldRates rates, Gdd03SyntheticFieldInput input)
+ {
+ if (rates == null) throw new ArgumentNullException(nameof(rates));
+ if (input == null) throw new ArgumentNullException(nameof(input));
+ if (!input.Prepared || !input.Workable || !input.Friendly || !input.Eligible || !input.FrozenTier.HasValue)
+ return new Gdd03SyntheticFieldOutput(0, false);
+ return new Gdd03SyntheticFieldOutput(rates.FoodFor(input.FrozenTier.Value), true);
+ }
+ }
+}