A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Sim: camp loss cancels basic training instead of refusing it (Fixes #2463) (#2721)
Beri992fa1dde4ff
diff --git a/changelog.d/2463-camp-loss.md b/changelog.d/2463-camp-loss.mdnew file mode 100644index 000000000..55c5eff55--- /dev/null+++ b/changelog.d/2463-camp-loss.md@@ -0,0 +1,7 @@+### Fixed++- gdd-0.3 basic training (synthetic fixture, no game caller yet): a military camp that stops operating during a course no+ longer stops the sim with a "Deferred" error. Its next course turn cancels every course at that camp at once, as a+ cancel does: the people stay in place as civilians without a job, Arms the course issued go back to the spare pool,+ Arms a recruit already carried stay with them, and Money and debt are not refunded (27M-9, R-TRAIN-002; Fixes #2463).+ gdd-0.1 and the goldens are unchanged.diff --git a/sim/Statecraft.Sim.Tests/Gdd03BasicTrainingFixtureTests.cs b/sim/Statecraft.Sim.Tests/Gdd03BasicTrainingFixtureTests.csindex 8ff9d9d38..3e92a7502 100644--- a/sim/Statecraft.Sim.Tests/Gdd03BasicTrainingFixtureTests.cs+++ b/sim/Statecraft.Sim.Tests/Gdd03BasicTrainingFixtureTests.cs@@ -669,6 +669,74 @@ public class Gdd03BasicTrainingFixtureTestsl.Reconcile();}+ // R-TRAIN-002 (docs/rules/mvp.md): "`CancelTraining(cohort)` returns the cohort's people to its city as civilians without+ // a job and refunds none of the Money paid"; docs/GDD.md:374 (27M-9): "After a cancellation or camp loss, people go to+ // their city as civilians without a job"; docs/GDD.md:372: "Money and tools are not refunded and Arms return to the pool".+ // A camp that stops operating cancels every course at it in one step: both cohorts' records stay in place as civilians+ // without a job, fixture-issued items go back to the spare Arms, the carried item stays, Money and debt are unchanged, and+ // another camp's trainee is untouched. The loss is applied by the next Advance of any trainee there, the would-be+ // graduation turn included, so no one graduates. Maintainer reading (not an owner ruling): the loss cancels before the+ // month ordinal is checked, as a lost training centre does (R-TRAIN-007).+ [Fact] public void CampLossCancelsEveryTraineeAtTheCamp()+ {+ var c = Catalogue(); var s = State(c); var l = s.PopulationLedger; var camps = Camps();+ var f = new Gdd03BasicTrainingFixture(c, s, camps, Formations(), 10, 1m);+ f.Start(new[] { 0, 1 }, CampNorth, 0);+ f.Start(new[] { 2, 3 }, CampNorth, 0);+ f.Start(new[] { 5 }, CampSouth, 0);+ f.Advance(0, 0); f.Advance(0, 1); f.Advance(2, 0);+ Assert.Equal(2, l.Get(0).Assignment.ProgressTicks);+ Assert.Equal(6, f.SpareArms); Assert.Equal(0m, f.Money); Assert.Equal(1.5m, f.Debt);+ int total = f.SpareArms + Issued(s), count = l.Units.Count, next = l.NextPopulationId;+ var before = new[] { 0, 1, 2, 3 }.Select(l.Get).ToArray();++ camps[0].Operating = false;+ f.Advance(0, 2);++ foreach (var old in before)+ {+ var p = l.Get(old.Id);+ Assert.Equal(PopulationStatus.FreeCivilian, p.Status);+ Assert.Equal(PopulationAssignmentKind.Unassigned, p.Assignment.Kind);+ Assert.Null(p.Assignment.Target); Assert.Equal(0, p.Assignment.ProgressTicks);+ Assert.Same(old.Location, p.Location); Assert.Equal("", p.Qualification);+ Assert.Equal(old.Equipment.ToolsId, p.Equipment.ToolsId);+ }+ Assert.Null(l.Get(0).Equipment.MilitaryId); Assert.Null(l.Get(1).Equipment.MilitaryId); Assert.Null(l.Get(2).Equipment.MilitaryId);+ Assert.Equal(Carried, l.Get(3).Equipment.MilitaryId); Assert.True(l.Get(3).Equipment.MilitaryPaid);+ Assert.Equal(9, f.SpareArms); Assert.Equal(total, f.SpareArms + Issued(s));+ Assert.Equal(0m, f.Money); Assert.Equal(1.5m, f.Debt);+ Assert.Equal(PopulationStatus.Trainee, l.Get(5).Status);+ Assert.Empty(Soldiers(s, FormationNorthA)); Assert.Empty(Soldiers(s, FormationNorthB));+ Assert.Equal(count, l.Units.Count); Assert.Equal(next, l.NextPopulationId);+ l.Reconcile();++ Unchanged(s, f, () => f.Advance(1, 2), typeof(ArgumentException));+ Unchanged(s, f, () => f.Cancel(2), typeof(ArgumentException));+ Unchanged(s, f, () => f.Start(new[] { 0 }, CampNorth, 2), typeof(ArgumentException));+ f.Advance(5, 0);+ Assert.Equal(1, l.Get(5).Assignment.ProgressTicks);+ }++ // R-TRAIN-002 (docs/rules/mvp.md): "A cohort that does not exist, or has finished, is refused with the state unchanged";+ // 27M-9 camp loss. Every affected trainee is checked before the first write: when one of them no longer holds the item+ // this fixture issued (as CancelIsRefusedWhenTheIssuedItemWasReplaced), the camp loss is refused and no trainee, Arms,+ // Money or debt changes. Maintainer reading (not an owner ruling): the refusal covers the whole camp, not only that record.+ [Fact] public void CampLossThatCannotReleaseOneTraineeChangesNothing()+ {+ var c = Catalogue(); var s = State(c); var l = s.PopulationLedger; var camps = Camps();+ var f = new Gdd03BasicTrainingFixture(c, s, camps, Formations(), 5, 10m);+ f.Start(new[] { 0, 1, 3 }, CampNorth, 0);+ l.Replace(WithMilitary(l.Get(1), "fixture-swapped-item"));+ camps[0].Operating = false;++ Unchanged(s, f, () => f.Advance(0, 1), typeof(InvalidOperationException));++ foreach (var id in new[] { 0, 1, 3 }) Assert.Equal(PopulationStatus.Trainee, l.Get(id).Status);+ Assert.Equal(Gdd03BasicTrainingFixture.IssuedPrefix + "0", l.Get(0).Equipment.MilitaryId);+ Assert.Equal(3, f.SpareArms);+ }+static void Graduate(Gdd03BasicTrainingFixture f, int id, int enrolment, int turns = 3){for (int month = enrolment; month < enrolment + turns; month++) f.Advance(id, month);diff --git a/sim/Statecraft.Sim/Gdd03BasicTrainingFixture.cs b/sim/Statecraft.Sim/Gdd03BasicTrainingFixture.csindex 10af0941f..ab155ae63 100644--- a/sim/Statecraft.Sim/Gdd03BasicTrainingFixture.cs+++ b/sim/Statecraft.Sim/Gdd03BasicTrainingFixture.cs@@ -6,12 +6,13 @@ using System.Linq;namespace Statecraft.Sim{/// <summary>Synthetic military camp facts supplied by a test: the camp's course target id, its region and whether it- /// operates. Real camps are #1719's; this fixture only reads these facts.</summary>+ /// operates. Real camps are #1719's; this fixture only reads these facts. A test sets <see cref="Operating"/> false to+ /// stand for a camp lost during a course (27M-9).</summary>internal sealed class Gdd03FixtureMilitaryCamp{internal string Id { get; }internal string Region { get; }- internal bool Operating { get; }+ internal bool Operating { get; set; }internal Gdd03FixtureMilitaryCamp(string id, string region, bool operating){if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(region))@@ -45,15 +46,14 @@ namespace Statecraft.Sim/// Arms (ARMS-1, ARMS-2, 27M-7, R-TRAIN-001, R-ARMS-003), synthetic fixtures only. No command, save, loader, resolver,/// Food or UI caller. Like <see cref="Gdd03ProjectBuildFixture"/> it owns its spare Arms, Money and Debt, so the ledger/// never touches resources (R-POP-002). Cancel returns an issued item to the spare Arms (R-TRAIN-002, R-ARMS-003,- /// docs/GDD.md:372). The last course turn graduates the record into its cohort's formation (R-TRAIN-003, R-UNIT-003), and- /// each formation it graduated pays upkeep from its graduation month (27M-8, R-UNIT-010).</summary>+ /// docs/GDD.md:372), and a camp that stops operating cancels every course at it the same way (27M-9). The last course+ /// turn graduates the record into its cohort's formation (R-TRAIN-003, R-UNIT-003), and each formation it graduated pays upkeep from its graduation month (27M-8, R-UNIT-010).</summary>internal sealed class Gdd03BasicTrainingFixture{/// <summary>Issued item ids are this prefix plus the population id. It is disjoint from the census and units_1948/// source prefixes (<see cref="HistoricalPopulation.CensusPrefix"/>, <see cref="HistoricalPopulation.FormationPrefix"/>),/// so no id can equal a starting formation's item; a record holds at most one military item, so no two records are issued the same id.</summary>internal const string IssuedPrefix = "basic_training/";- const string CampLoss = "Deferred: a camp that stops operating during a course (#1699, 27M-9).";readonly GameState state;readonly SortedDictionary<string, Gdd03FixtureMilitaryCamp> camps = new SortedDictionary<string, Gdd03FixtureMilitaryCamp>(StringComparer.Ordinal);@@ -192,7 +192,11 @@ namespace Statecraft.Sim/// <summary>R-TRAIN-001 "counting the turn it was ordered in": count one course turn a month ordinal, the enrolment/// month included, each ordinal once and never an earlier one. The turn that reaches <c>military.basic_turns</c>- /// graduates the record (<see cref="Graduate"/>).</summary>+ /// graduates the record (<see cref="Graduate"/>). At a camp that stops operating the turn counts nothing and cancels+ /// every course at that camp at once, as <see cref="Cancel"/> does (R-TRAIN-002; docs/GDD.md:374, 27M-9: "After a+ /// cancellation or camp loss, people go to their city as civilians without a job"), the would-be graduation turn+ /// included. Maintainer reading (not an owner ruling): the loss is checked before the month ordinal, as a lost+ /// training centre's is (R-TRAIN-007), and it cancels only the trainees this fixture started.</summary>internal void Advance(int traineeId, int monthOrdinal){ValidateInventory();@@ -200,7 +204,11 @@ namespace Statecraft.Simif (trainee.Status != PopulationStatus.Trainee || trainee.Assignment.Target == null|| !camps.TryGetValue(trainee.Assignment.Target, out var camp) || !months.TryGetValue(traineeId, out var known))throw new ArgumentException("Not a trainee this fixture started.");- if (!camp.Operating) throw new NotSupportedException(CampLoss);+ if (!camp.Operating)+ {+ Release(Trainees(camp.Id).Where(p => months.ContainsKey(p.Id)).ToArray());+ return;+ }if (monthOrdinal < known.enrolment || monthOrdinal <= known.lastCounted)throw new ArgumentException("This month ordinal was already counted or is before enrolment.");int progress = checked(trainee.Assignment.ProgressTicks + 1);@@ -224,20 +232,32 @@ namespace Statecraft.Simif (trainee.Status != PopulationStatus.Trainee || trainee.Assignment.Kind != PopulationAssignmentKind.Course|| trainee.Assignment.Target == null || !camps.ContainsKey(trainee.Assignment.Target) || !months.ContainsKey(traineeId))throw new ArgumentException("Not a trainee this fixture started.");- var e = trainee.Equipment;- bool refund = issued.Contains(traineeId);- // Maintainer call (not an owner ruling): no rule covers a trainee whose issued item is gone, and refusing neither- // refunds an item the record no longer holds nor loses that Arms. In the tests only a direct ledger write gets here.- if (refund && e.MilitaryId != IssuedPrefix + traineeId.ToString(CultureInfo.InvariantCulture))- throw new InvalidOperationException("Trainee " + traineeId.ToString(CultureInfo.InvariantCulture) + " no longer holds the item this fixture issued.");- int spare = refund ? checked(SpareArms + 1) : SpareArms;- var person = !refund ? trainee : new PopulationUnit(trainee.Id, trainee.Status, trainee.ReceptionTicks, trainee.Location,- trainee.Assignment, trainee.Profession, trainee.Tier, trainee.WorkXp, trainee.Qualification, trainee.CombatXp,- new PopulationEquipment(e.ToolsId, e.ToolsPaid, null, false), trainee.Captor, trainee.Home);- Commit(new[] { person.WithPlacement(PopulationStatus.FreeCivilian, trainee.Location, PopulationAssignment.Unassigned,- trainee.Captor, trainee.Home) });+ Release(new[] { trainee });+ }++ // Cancel's release of the given trainees, all or nothing: every record is checked and the spare stock computed+ // before the first write, so a camp loss that cannot release one trainee changes no record, Arms, Money or Debt.+ void Release(IReadOnlyList<PopulationUnit> trainees)+ {+ int spare = SpareArms; var released = new List<PopulationUnit>();+ foreach (var trainee in trainees)+ {+ var e = trainee.Equipment;+ bool refund = issued.Contains(trainee.Id);+ // Maintainer call (not an owner ruling): no rule covers a trainee whose issued item is gone, and refusing neither+ // refunds an item the record no longer holds nor loses that Arms. In the tests only a direct ledger write gets here.+ if (refund && e.MilitaryId != IssuedPrefix + trainee.Id.ToString(CultureInfo.InvariantCulture))+ throw new InvalidOperationException("Trainee " + trainee.Id.ToString(CultureInfo.InvariantCulture) + " no longer holds the item this fixture issued.");+ if (refund) spare = checked(spare + 1);+ var person = !refund ? trainee : new PopulationUnit(trainee.Id, trainee.Status, trainee.ReceptionTicks, trainee.Location,+ trainee.Assignment, trainee.Profession, trainee.Tier, trainee.WorkXp, trainee.Qualification, trainee.CombatXp,+ new PopulationEquipment(e.ToolsId, e.ToolsPaid, null, false), trainee.Captor, trainee.Home);+ released.Add(person.WithPlacement(PopulationStatus.FreeCivilian, trainee.Location, PopulationAssignment.Unassigned,+ trainee.Captor, trainee.Home));+ }+ Commit(released);SpareArms = spare;- issued.Remove(traineeId); months.Remove(traineeId); cohortOf.Remove(traineeId);+ foreach (var trainee in trainees) { issued.Remove(trainee.Id); months.Remove(trainee.Id); cohortOf.Remove(trainee.Id); }}/// <summary>R-TRAIN-003 and R-UNIT-003 under gdd-0.3: the month that counts the last course turn releases the