A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Add internal farmer and pioneer profession helpers, fixture-only (Refs #1693) (#1869)
Eyal Golanb1cada2a2f12
diff --git a/changelog.d/1693-profession-helpers.md b/changelog.d/1693-profession-helpers.mdnew file mode 100644index 000000000..9a5b99897--- /dev/null+++ b/changelog.d/1693-profession-helpers.md@@ -0,0 +1 @@+- Refs #1693: add internal, fixture-only civilian profession helpers for actual-work XP, next-turn promotion, matching-course preservation and Agreed catalogue quotes. Production integration and payments remain held; legacy/gdd-0.1 behavior and content are unchanged.diff --git a/docs/rules/mvp.md b/docs/rules/mvp.mdindex 8b2992dc4..d57675363 100644--- a/docs/rules/mvp.md+++ b/docs/rules/mvp.md@@ -160,9 +160,9 @@ R-SITE-003: Each turn, in ordinal site id order, every site being built has one## Veteran pioneers-R-VET-001: In step 5, after its site's or segment's progress (R-COR-002), a halutz unit whose site or corridor segment is being built gains one turn of service, the turn it is built included; an idle unit gains none. At `veteran_halutz_service` turns of service it becomes a `veteran_halutz` at the end of that turn, keeping its id, hex, service and project, and a cause says so. A veteran gains no more service. A unit's service is written into the state hash only when it is not 0 (D-D9; Q-VET-1, owner, 17 October 2026, #782). A halutz or veteran can return to civilian life and keep its service or its status (R-QUAL-003).+R-VET-001: Under legacy/gdd-0.1, in step 5, after its site's or segment's progress (R-COR-002), a halutz unit whose site or corridor segment is being built gains one turn of service, the turn it is built included; an idle unit gains none. At `veteran_halutz_service` turns of service it becomes a `veteran_halutz` at the end of that turn, keeping its id, hex, service and project, and a cause says so. A veteran gains no more service. A unit's service is written into the state hash only when it is not 0 (D-D9; Q-VET-1, owner, 17 October 2026, #782). A halutz or veteran can return to civilian life and keep its service or its status (R-QUAL-003). In gdd-0.3 synthetic fixtures only, the pure profession helper proposes one actual-work XP increment for a received free civilian with a matching Work assignment and completed staffed-farming evidence regardless of output or advancement, or evidence of advancing an on-site pioneer project. Adopted sources at `605277dfa511dc512562ececeb273b8393b21d35`: `docs/gdd/training.md:17`, "Farmers earn work XP only on staffed farming turns. Pioneers earn it only while advancing an on-site pioneer project."; `docs/gdd/balance.md:70`, "Work XP counts actual qualifying turns, not output or work quantity." Multiple matching results count once per evaluation; zero pioneer advancement, wrong profession/target, generic work, travel, idle, teaching and courses give none. Both novice and trained farmer/pioneer become veteran at the Agreed catalogue `profession.work_to_veteran` threshold (currently 9). Craftsmen do not use this path. The caller must use one turn-start snapshot and commit at most once at turn end; turn scheduling, authoritative evidence production and cross-call deduplication remain held. No game-loop caller is added.-R-VET-002: A veteran's site or corridor segment being built has `veteran_progress` turns less to go each turn, against a halutz's 1; one with that many or fewer left is built that turn. A veteran does the same pioneer work as a halutz (R-SITE-001) (D-D9; Q-VET-2, owner, 17 October 2026, #782).+R-VET-002: Under legacy/gdd-0.1, a veteran's site or corridor segment being built has `veteran_progress` turns less to go each turn, against a halutz's 1; one with that many or fewer left is built that turn. A veteran does the same pioneer work as a halutz (R-SITE-001) (D-D9; Q-VET-2, owner, 17 October 2026, #782). In gdd-0.3 synthetic fixtures only, output quotes read the turn-start tier from Agreed catalogue `pioneer.work_rate` (novice/trained/veteran: 0.75/1/1.5) or `farmer.output` (2/3/5). Promotion returns a separate record for the next turn, preserving identity, assignment, placement, equipment and independent military history. No project progress or farming yield is applied by the helper; absent, Deferred or malformed values fail closed without legacy fallback.### Return to civilian life and reassignment (#1511)@@ -182,6 +182,8 @@ R-QUAL-006: A Train order (R-TRAIN-001) takes the city's unqualified civilians fR-QUAL-007: Attention is that of the matching existing order (owner ruling 1): `return_craftsmen` and `reassign_craftsmen` cost what `assign_workers` does (0.5), and `reassign_pioneers` and the `return_civilian` unit order cost what a unit order does (0), all design values in `rules.yaml`. The qualified counts are written into the state hash only when not 0 (`,qcraftsmen=N,qhalutz=S:S,qveterans=N` after the other city fields), so a game without them hashes as before. The saves write `retcraft CITY N`, `regcraft CITY N`, `regpioneer CITY TRACK N` and `order UNIT return_civilian`; scenarios use `return_craftsmen`, `reassign_craftsmen`, `reassign_pioneers` and `unit_order` with `order: return_civilian`.+R-QUAL-008: Region-independent gdd-0.3 fixture helpers only (coordinator decision, 26 Oct 2026, Q5): a matching farmer/pioneer course may propose trained tier while retaining actual-work XP, veteran tier and every other record field. Cross-profession qualification is refused without mutation. This transform does not decide course eligibility, completion or release. Tools quotes read only Agreed `profession.farmer_tools` / `profession.pioneer_tools` (currently 3 / 5); no acquisition, charge, credit, refund or paid-flag change occurs. All helpers are internal and require an initialized exact-gdd-0.3 fixture. Money policy, once-only acquisition, lock-in policy, craftsman progression and production integration remain held with their owners.+R-SITE-004: Every designated site has one or more kinds from a fixed list: `yishuv`, `field`, `swamp`, `military_camp` and `hakhshara` (Phase 4, Q-FIELD-1, Q-SWAMP-1, Q-CAMP-3). A pioneer action is accepted only at a site of its kind; any other is refused with the state unchanged and the Attention refunded. A yishuv site has a founding date inside the MVP's years and no other kind has one. A camp site (`military_camp` or `hakhshara`) names a city of `cities.yaml`, the city it trains for (Q-CAMP-1), and no other kind names one. The loader refuses any other row, naming the file and field.R-SITE-005: At most one `yishuv` site sits in a hex, so a hex shows at most one new yishuv. A site of another kind may share a hex with a yishuv site, as a yishuv's field site does (Q-SITE-3).diff --git a/sim/Statecraft.Sim.Tests/CivilianProfessionRulesTests.cs b/sim/Statecraft.Sim.Tests/CivilianProfessionRulesTests.csnew file mode 100644index 000000000..0298df90a--- /dev/null+++ b/sim/Statecraft.Sim.Tests/CivilianProfessionRulesTests.cs@@ -0,0 +1,218 @@+using Statecraft.Sim;++// Synthetic fixtures only. These tests do not exercise production farming, projects, payments or replay.+public class CivilianProfessionRulesTests+{+ static Content Catalogue()+ {+ var c = PopulationLedgerTests.Content();+ c.Gdd03Balance = ContentLoader.Load(RealContentTests.ContentRoot()).Gdd03Balance;+ return c;+ }+ static PopulationUnit Person(CivilianProfession profession, CivilianTier tier, int xp,+ PopulationAssignmentKind assignment = PopulationAssignmentKind.Work)+ => new PopulationUnit(0, assignment == PopulationAssignmentKind.Course ? PopulationStatus.Trainee : PopulationStatus.FreeCivilian,+ 6, new PopulationLocation("fixture"),+ assignment == PopulationAssignmentKind.Unassigned ? PopulationAssignment.Unassigned : new PopulationAssignment(assignment, "fixture-target"),+ profession, tier, xp, "", 7, new PopulationEquipment("fixture-tools", true, "fixture-arms", true));+ static GameState State(Content c, PopulationUnit p) => PopulationLedger.CreateFixture(c,+ new PopulationBindings(new[] { new PopulationLocation("fixture") },+ new[] { new PopulationTarget("fixture-target", p.Assignment.Kind == PopulationAssignmentKind.Unassigned+ ? PopulationAssignmentKind.Work : p.Assignment.Kind, "fixture",+ p.Assignment.Kind == PopulationAssignmentKind.Course ? 3 : (int?)null) },+ Array.Empty<string>(), Array.Empty<string>()),+ new[] { new PopulationInventoryRow("synthetic", 0, p) });+ static CivilianWorkEvidence Work(CivilianProfession p, decimal amount = 0.75m)+ => new CivilianWorkEvidence(0, "fixture-target", p == CivilianProfession.Farmer+ ? CivilianWorkKind.StaffedFarming : CivilianWorkKind.PioneerAdvancement, amount);++ // R-VET-001/002: a staffed farming turn or fractional pioneer advancement counts once; ninth output retains the old tier.+ [Theory]+ [InlineData(CivilianProfession.Farmer, CivilianTier.Novice)]+ [InlineData(CivilianProfession.Farmer, CivilianTier.Trained)]+ [InlineData(CivilianProfession.Pioneer, CivilianTier.Novice)]+ [InlineData(CivilianProfession.Pioneer, CivilianTier.Trained)]+ public void NinthWorkingTurnPromotesOnlyTheReturnedNextTurnRecord(CivilianProfession profession, CivilianTier tier)+ {+ var c = Catalogue(); var s = State(c, Person(profession, tier, 8)); var before = s.Describe();+ var p = s.PopulationLedger.Get(0);+ var expectedRate = profession == CivilianProfession.Farmer ? (tier == CivilianTier.Novice ? 2m : 3m)+ : (tier == CivilianTier.Novice ? 0.75m : 1m);+ Assert.Equal(expectedRate, CivilianProfessionRules.OutputAtTurnStart(c, s, 0));+ var next = CivilianProfessionRules.AfterWork(c, s, 0, new[] { Work(profession, 0.75m), Work(profession) });+ Assert.Equal(9, next.WorkXp); Assert.Equal(CivilianTier.Veteran, next.Tier);+ Assert.Equal(before, s.Describe()); Assert.Equal(tier, p.Tier);+ Assert.Equal(p.Id, next.Id); Assert.Same(p.Location, next.Location); Assert.Same(p.Assignment, next.Assignment);+ Assert.Same(p.Equipment, next.Equipment); Assert.Equal(p.CombatXp, next.CombatXp);+ Assert.Equal(p.ReceptionTicks, next.ReceptionTicks); Assert.Equal(p.Qualification, next.Qualification);+ s.PopulationLedger.Replace(next);+ Assert.Equal(profession == CivilianProfession.Farmer ? 5m : 1.5m, CivilianProfessionRules.OutputAtTurnStart(c, s, 0));+ }++ // R-VET-001: completed staffed farming counts even with zero output; the source snapshot stays unchanged.+ [Fact] public void CompletedStaffedFarmingTurnWithZeroOutputEarnsOneXp()+ {+ var c = Catalogue(); var s = State(c, Person(CivilianProfession.Farmer, CivilianTier.Novice, 0));+ var before = s.Describe(); var p = s.PopulationLedger.Get(0);+ var next = CivilianProfessionRules.AfterWork(c, s, 0, new[] { Work(CivilianProfession.Farmer, 0) });+ Assert.Equal(1, next.WorkXp); Assert.Equal(CivilianTier.Novice, next.Tier);+ Assert.Equal(0, p.WorkXp); Assert.Same(p, s.PopulationLedger.Get(0)); Assert.Equal(before, s.Describe());+ }++ // R-VET-001, R-POP-002: matching work requires completed reception before XP or promotion.+ [Theory]+ [InlineData(CivilianProfession.Farmer, 0)]+ [InlineData(CivilianProfession.Farmer, 3)]+ [InlineData(CivilianProfession.Farmer, 4)]+ [InlineData(CivilianProfession.Farmer, 5)]+ [InlineData(CivilianProfession.Farmer, 6)]+ [InlineData(CivilianProfession.Pioneer, 0)]+ [InlineData(CivilianProfession.Pioneer, 3)]+ [InlineData(CivilianProfession.Pioneer, 4)]+ [InlineData(CivilianProfession.Pioneer, 5)]+ [InlineData(CivilianProfession.Pioneer, 6)]+ public void ReceptionMustFinishBeforeMatchingWorkEarnsXp(CivilianProfession profession, int receptionTicks)+ {+ var c = Catalogue(); var s = State(c, Person(profession, CivilianTier.Novice, 8).WithReception(receptionTicks));+ var before = s.Describe(); var p = s.PopulationLedger.Get(0);+ var next = CivilianProfessionRules.AfterWork(c, s, 0, new[] { Work(profession) });+ if (receptionTicks < 6)+ {+ Assert.Same(p, next); Assert.Equal(8, next.WorkXp); Assert.Equal(CivilianTier.Novice, next.Tier);+ }+ else+ {+ Assert.NotSame(p, next); Assert.Equal(9, next.WorkXp); Assert.Equal(CivilianTier.Veteran, next.Tier);+ }+ Assert.Same(p, s.PopulationLedger.Get(0)); Assert.Equal(8, p.WorkXp); Assert.Equal(CivilianTier.Novice, p.Tier);+ Assert.Equal(before, s.Describe());+ }++ // R-VET-001: no service shortcut; pioneers need matching positive project-advancement evidence.+ [Theory]+ [InlineData(PopulationAssignmentKind.Unassigned)]+ [InlineData(PopulationAssignmentKind.Teaching)]+ [InlineData(PopulationAssignmentKind.Course)]+ [InlineData(PopulationAssignmentKind.Work)]+ public void IneligibleOrUnproductiveTurnsDoNotAdvance(PopulationAssignmentKind assignment)+ {+ var c = Catalogue(); var s = State(c, Person(CivilianProfession.Pioneer, CivilianTier.Novice, 6, assignment));+ var before = s.Describe();+ var evidence = assignment == PopulationAssignmentKind.Work+ ? new[] { Work(CivilianProfession.Pioneer, 0), Work(CivilianProfession.Farmer),+ new CivilianWorkEvidence(0, "fixture-target", CivilianWorkKind.GenericWork, 1),+ new CivilianWorkEvidence(0, "other-target", CivilianWorkKind.PioneerAdvancement, 1) }+ : new[] { Work(CivilianProfession.Pioneer) };+ var next = CivilianProfessionRules.AfterWork(c, s, 0, evidence);+ Assert.Same(s.PopulationLedger.Get(0), next); Assert.Equal(before, s.Describe());+ Assert.Same(next, CivilianProfessionRules.AfterWork(c, s, 0, Array.Empty<CivilianWorkEvidence>()));+ }++ // R-VET-001: interruption does not erase history; craftsman progression is owned by #1698.+ [Fact] public void InterruptedWorkAndCraftsmenNeverUseServiceOrCourseTicksAsXp()+ {+ var c = Catalogue(); var s = State(c, Person(CivilianProfession.Pioneer, CivilianTier.Novice, 6));+ var next = CivilianProfessionRules.AfterWork(c, s, 0, new[] { Work(CivilianProfession.Pioneer) });+ Assert.Equal(7, next.WorkXp); Assert.Equal(CivilianTier.Novice, next.Tier);+ s.PopulationLedger.Replace(next); s.PopulationLedger.ChangeAssignment(0, PopulationAssignment.Unassigned);+ Assert.Equal(7, CivilianProfessionRules.AfterWork(c, s, 0, new[] { Work(CivilianProfession.Pioneer) }).WorkXp);+ s.PopulationLedger.ChangeAssignment(0, new PopulationAssignment(PopulationAssignmentKind.Work, "fixture-target"));+ Assert.Equal(8, CivilianProfessionRules.AfterWork(c, s, 0, new[] { Work(CivilianProfession.Pioneer) }).WorkXp);+ var craft = State(c, Person(CivilianProfession.Craftsman, CivilianTier.Apprentice, 8));+ Assert.Same(craft.PopulationLedger.Get(0), CivilianProfessionRules.AfterWork(c, craft, 0, new[] { Work(CivilianProfession.Pioneer) }));+ }++ // R-QUAL-008: matching qualification preserves work/equipment; switching is refused without mutation.+ [Theory] [InlineData(CivilianTier.Novice)] [InlineData(CivilianTier.Trained)] [InlineData(CivilianTier.Veteran)]+ public void MatchingCoursePreservesHistoryAndNeverDowngrades(CivilianTier tier)+ {+ var c = Catalogue(); var s = State(c, Person(CivilianProfession.Pioneer, tier, 8, PopulationAssignmentKind.Course));+ var before = s.Describe(); var p = s.PopulationLedger.Get(0);+ var next = CivilianProfessionRules.AfterMatchingCourse(c, s, 0, CivilianProfession.Pioneer);+ Assert.Equal(tier == CivilianTier.Veteran ? tier : CivilianTier.Trained, next.Tier);+ Assert.Equal(8, next.WorkXp); Assert.Equal(p.Id, next.Id); Assert.Same(p.Equipment, next.Equipment);+ Assert.Same(p.Assignment, next.Assignment); Assert.Equal(p.CombatXp, next.CombatXp);+ Assert.Throws<ArgumentException>(() => CivilianProfessionRules.AfterMatchingCourse(c, s, 0, CivilianProfession.Farmer));+ Assert.Equal(before, s.Describe());+ }++ // R-QUAL-008: this is a catalogue quote, not an affordability/credit decision or a payment.+ [Fact] public void ToolsQuotesReadOnlyAgreedCatalogueAndNeverTouchMoney()+ {+ var c = Catalogue(); var s = State(c, Person(CivilianProfession.Pioneer, CivilianTier.Novice, 0));+ var before = s.Describe();+ Assert.Equal(3, CivilianProfessionRules.ToolsPrice(c, s, CivilianProfession.Farmer));+ Assert.Equal(5, CivilianProfessionRules.ToolsPrice(c, s, CivilianProfession.Pioneer));+ Assert.Throws<ArgumentException>(() => CivilianProfessionRules.ToolsPrice(c, s, CivilianProfession.Craftsman));+ Assert.Equal(before, s.Describe());+ }++ // R-VET-001/002, R-QUAL-008: missing/Deferred/invalid data never falls back to old rules.+ [Theory] [InlineData("profession.work_to_veteran")] [InlineData("profession.pioneer_tools")]+ [InlineData("pioneer.work_rate")] [InlineData("farmer.output")]+ public void InvalidCatalogueFailsClosed(string key)+ {+ foreach (var fault in new[] { "missing", "Deferred", "zero", "shape", "expression", "ruleset" })+ {+ var c = Catalogue(); var profession = key == "farmer.output" ? CivilianProfession.Farmer : CivilianProfession.Pioneer;+ var s = State(c, Person(profession, CivilianTier.Novice, 8)); var before = s.Describe();+ if (fault == "missing") c.Gdd03Balance!.Values.Remove(key);+ else if (fault == "Deferred") c.Gdd03Balance!.Values[key].Basis = "Deferred";+ else if (fault == "zero") c.Gdd03Balance!.Values[key].Values[0] = 0;+ else if (fault == "shape") c.Gdd03Balance!.Values[key].Values.Add(1);+ else if (fault == "expression") c.Gdd03Balance!.Values[key].Expression = "Deferred";+ else c.Gdd03Balance!.Ruleset = SaveGame.GddRuleset;+ Assert.Throws<ContentException>(() => {+ if (key == "profession.work_to_veteran") CivilianProfessionRules.AfterWork(c, s, 0, new[] { Work(profession) });+ else if (key == "profession.pioneer_tools") CivilianProfessionRules.ToolsPrice(c, s, profession);+ else CivilianProfessionRules.OutputAtTurnStart(c, s, 0);+ });+ Assert.Equal(before, s.Describe());+ }+ }++ // R-POP-001/002, R-QUAL-008: internal helpers require an initialized exact-identity fixture.+ [Theory] [InlineData("legacy")] [InlineData("gdd-0.1")] [InlineData("gdd-0.3")]+ public void UninitializedGameCannotUseHelpers(string identity)+ {+ var c = PopulationLedgerTests.Content(identity); var s = new GameState(c); var before = s.Describe();+ Assert.Throws<InvalidOperationException>(() => CivilianProfessionRules.ToolsPrice(c, s, CivilianProfession.Farmer));+ Assert.Throws<InvalidOperationException>(() => CivilianProfessionRules.AfterWork(c, s, 0, Array.Empty<CivilianWorkEvidence>()));+ Assert.Throws<InvalidOperationException>(() => CivilianProfessionRules.OutputAtTurnStart(c, s, 0));+ Assert.Throws<InvalidOperationException>(() => CivilianProfessionRules.AfterMatchingCourse(c, s, 0, CivilianProfession.Pioneer));+ Assert.Equal(before, s.Describe());+ }++ // R-VET-001, R-POP-002: evidence validation is a helper contract; malformed evidence is refused atomically before a proposal.+ [Fact] public void InvalidEvidenceAndUnknownIdsRefuseWithoutMutation()+ {+ var c = Catalogue(); var s = State(c, Person(CivilianProfession.Pioneer, CivilianTier.Novice, 8));+ var before = s.Describe();+ Assert.Equal("populationId", Assert.Throws<ArgumentOutOfRangeException>(() =>+ new CivilianWorkEvidence(-1, "fixture-target", CivilianWorkKind.PioneerAdvancement, 1)).ParamName);+ Assert.Equal("advancement", Assert.Throws<ArgumentOutOfRangeException>(() =>+ new CivilianWorkEvidence(0, "fixture-target", CivilianWorkKind.PioneerAdvancement, -1)).ParamName);+ Assert.Throws<ArgumentException>(() => new CivilianWorkEvidence(0, "fixture-target", (CivilianWorkKind)99, 1));+ Assert.Throws<ArgumentException>(() => CivilianProfessionRules.AfterWork(c, s, 99, new[] { Work(CivilianProfession.Pioneer) }));+ Assert.Throws<ArgumentException>(() => CivilianProfessionRules.AfterWork(c, s, 0,+ new[] { Work(CivilianProfession.Pioneer), new CivilianWorkEvidence(1, "fixture-target", CivilianWorkKind.PioneerAdvancement, 1) }));+ Assert.Equal(before, s.Describe());+ }+ // R-VET-001/002, R-POP-002/003: stable ordering, atomic refusal and clone guarantees; checked overflow is helper validation.+ [Fact] public void EvaluationIsOrderIndependentAndOverflowCannotPartiallyCommit()+ {+ var c = Catalogue(); var s = State(c, Person(CivilianProfession.Pioneer, CivilianTier.Novice, 8));+ var evidence = new[] { Work(CivilianProfession.Pioneer), Work(CivilianProfession.Pioneer, 0), Work(CivilianProfession.Pioneer) };+ var before = s.Hash(); var clone = s.Clone();+ var a = CivilianProfessionRules.AfterWork(c, s, 0, evidence);+ var b = CivilianProfessionRules.AfterWork(c, clone, 0, evidence.Reverse());+ Assert.Equal(a.WorkXp, b.WorkXp); Assert.Equal(a.Tier, b.Tier);+ s.PopulationLedger.Replace(a); clone.PopulationLedger.Replace(b);+ Assert.Equal(s.Hash(), clone.Hash()); Assert.NotEqual(before, s.Hash());+ var full = State(c, Person(CivilianProfession.Pioneer, CivilianTier.Veteran, int.MaxValue));+ var fullBefore = full.Describe();+ Assert.Throws<OverflowException>(() => CivilianProfessionRules.AfterWork(c, full, 0, evidence));+ Assert.Equal(fullBefore, full.Describe());+ }++}diff --git a/sim/Statecraft.Sim/CivilianProfessionRules.cs b/sim/Statecraft.Sim/CivilianProfessionRules.csnew file mode 100644index 000000000..932f03193--- /dev/null+++ b/sim/Statecraft.Sim/CivilianProfessionRules.cs@@ -0,0 +1,112 @@+using System;+using System.Collections.Generic;++namespace Statecraft.Sim+{+ // Internal until producer integration is approved. Evidence must describe completed work, not intent.+ internal enum CivilianWorkKind { GenericWork, StaffedFarming, PioneerAdvancement }++ internal sealed class CivilianWorkEvidence+ {+ internal int PopulationId { get; }+ internal string Target { get; }+ internal CivilianWorkKind Kind { get; }+ internal decimal Advancement { get; }+ internal CivilianWorkEvidence(int populationId, string target, CivilianWorkKind kind, decimal advancement)+ {+ if (populationId < 0) throw new ArgumentOutOfRangeException(nameof(populationId));+ if (advancement < 0) throw new ArgumentOutOfRangeException(nameof(advancement));+ if (string.IsNullOrWhiteSpace(target)) throw new ArgumentException("Work needs its explicit assignment target.");+ PopulationText.ValidateText(target);+ if (kind != CivilianWorkKind.GenericWork && kind != CivilianWorkKind.StaffedFarming && kind != CivilianWorkKind.PioneerAdvancement)+ throw new ArgumentException("Unknown civilian work kind.");+ PopulationId = populationId; Target = target; Kind = kind; Advancement = advancement;+ }+ }++ /// <summary>Pure region-independent fixture rules. No caller in the game, payment, assignment or resolver.+ /// Outputs are proposed immutable replacements, not mutations or replay commands. One evaluation uses the+ /// turn-start ledger and ALL completed-work evidence for one id. The future resolver must commit once at+ /// turn end; feeding a returned record back within the same turn would incorrectly credit a second turn.</summary>+ internal static class CivilianProfessionRules+ {+ static void RequireFixture(Content c, GameState s)+ {+ if (!c.UsesPopulationLedger || !s.UsesPopulationLedger || !s.PopulationLedger.IsInitialized)+ throw new InvalidOperationException("Civilian profession helpers require an initialized gdd-0.3 fixture.");+ }+ static bool FarmerOrPioneer(CivilianProfession profession)+ => profession == CivilianProfession.Farmer || profession == CivilianProfession.Pioneer;+ static PopulationUnit WithTierAndXp(PopulationUnit p, CivilianTier tier, int xp)+ => new PopulationUnit(p.Id, p.Status, p.ReceptionTicks, p.Location, p.Assignment,+ p.Profession, tier, xp, p.Qualification, p.CombatXp, p.Equipment, p.Captor, p.Home);++ // R-VET-001: at most one XP increment in this pure evaluation, irrespective of evidence count/order.+ internal static PopulationUnit AfterWork(Content c, GameState turnStart, int id, IEnumerable<CivilianWorkEvidence> evidence)+ {+ RequireFixture(c, turnStart);+ var p = turnStart.PopulationLedger.Get(id);+ if (evidence == null) throw new ArgumentNullException(nameof(evidence));+ bool worked = false;+ foreach (var e in evidence)+ {+ if (e == null || e.PopulationId != id) throw new ArgumentException("Evidence must name the evaluated population id.");+ bool matching = (p.Profession == CivilianProfession.Farmer && e.Kind == CivilianWorkKind.StaffedFarming)+ || (p.Profession == CivilianProfession.Pioneer && e.Kind == CivilianWorkKind.PioneerAdvancement);+ // R-VET-001: completed staffed farming counts regardless of output; pioneers must advance a project.+ if (matching && e.Target == p.Assignment.Target+ && (e.Kind == CivilianWorkKind.StaffedFarming || e.Advancement > 0)) worked = true;+ }+ if (!worked || !FarmerOrPioneer(p.Profession) || p.Status != PopulationStatus.FreeCivilian+ || p.Assignment.Kind != PopulationAssignmentKind.Work+ || p.ReceptionTicks != c.RequirePopulationInteger("reception.turns")) return p;+ int threshold = c.RequirePopulationInteger("profession.work_to_veteran");+ int xp = checked(p.WorkXp + 1);+ var tier = xp >= threshold ? CivilianTier.Veteran : p.Tier;+ return WithTierAndXp(p, tier, xp);+ }++ // R-VET-002: always read the snapshot tier, never the proposed end-of-turn replacement.+ internal static decimal OutputAtTurnStart(Content c, GameState turnStart, int id)+ {+ RequireFixture(c, turnStart);+ var p = turnStart.PopulationLedger.Get(id);+ if (!FarmerOrPioneer(p.Profession)) throw new ArgumentException("Only farmer/pioneer output is defined here.");+ string key = p.Profession == CivilianProfession.Farmer ? "farmer.output" : "pioneer.work_rate";+ var catalogue = c.Gdd03Balance;+ if (catalogue == null || catalogue.Ruleset != SaveGame.Gdd03Ruleset || catalogue.Values == null+ || !catalogue.Values.TryGetValue(key, out var row) || row == null || row.Basis != "Agreed"+ || row.Values == null || row.Values.Count != 3 || row.Parts == null || row.Parts.Count != 0+ || !string.IsNullOrEmpty(row.Expression) || row.Values.Exists(x => x <= 0))+ throw new ContentException(Gdd03BalanceCatalogue.FileName, "values." + key,+ "requires positive Agreed novice/trained/veteran values; no fallback");+ int index = p.Tier switch {+ CivilianTier.Novice => 0, CivilianTier.Trained => 1, CivilianTier.Veteran => 2,+ _ => throw new ArgumentException("Invalid farmer/pioneer tier.") };+ return row.Values[index];+ }++ // R-QUAL-008: qualification transform only. Course eligibility/completion, fees and release are NOT inferred.+ internal static PopulationUnit AfterMatchingCourse(Content c, GameState s, int id, CivilianProfession courseProfession)+ {+ RequireFixture(c, s);+ var p = s.PopulationLedger.Get(id);+ if (!FarmerOrPioneer(courseProfession) || p.Profession != courseProfession+ || p.Status != PopulationStatus.Trainee || p.Assignment.Kind != PopulationAssignmentKind.Course)+ throw new ArgumentException("Only a matching farmer/pioneer course can propose a trained tier.");+ return p.Tier == CivilianTier.Veteran || p.Tier == CivilianTier.Trained+ ? p : WithTierAndXp(p, CivilianTier.Trained, p.WorkXp);+ }++ // R-QUAL-008: quote ONLY. #1702 owns affordability/credit; no acquisition or payment is performed.+ internal static int ToolsPrice(Content c, GameState s, CivilianProfession profession)+ {+ RequireFixture(c, s);+ string key = profession switch {+ CivilianProfession.Farmer => "profession.farmer_tools",+ CivilianProfession.Pioneer => "profession.pioneer_tools",+ _ => throw new ArgumentException("No farmer/pioneer tools price for this profession.") };+ return c.RequirePopulationInteger(key);+ }+ }+}