using System.Collections.Generic; using System.Linq; using BlaxCastes; using RimWorld; using Verse; namespace BlaxCastesTests { /// Applying castes to pawns: the per-slot checks, the Follow rule, the sweep and the enabled switch. public static class CasteTests { // ---- policies used across tests ------------------------------------------------------ /// The Slave outfit with Ideology, otherwise the Soldier outfit (both differ from Anything, Worker and Nudist). private static ApparelPolicy SlaveOrSoldier() => Fixtures.Need(Fixtures.Outfit(ModsConfig.IdeologyActive ? "OutfitSlave" : "OutfitSoldier"), "Slave or Soldier outfit"); private static ApparelPolicy Worker() => Fixtures.Need(Fixtures.Outfit("OutfitWorker"), "Worker outfit"); private static ApparelPolicy Nudist() => Fixtures.Need(Fixtures.Outfit("OutfitNudist"), "Nudist outfit"); private static FoodPolicy Simple() => Fixtures.Need(Fixtures.Food("FoodRestrictionSimple"), "Simple food policy"); private static FoodPolicy Paste() => Fixtures.Need(Fixtures.Food("FoodRestrictionPaste"), "Paste food policy"); private static DrugPolicy NoDrugs() => Fixtures.Need(Fixtures.Drug("NoDrugs"), "No drugs policy (sourceDef NoDrugs)"); private static DrugPolicy Social() => Fixtures.Need(Fixtures.Drug("SocialDrugs"), "Social drugs policy (sourceDef SocialDrugs)"); private static ReadingPolicy NoneReading() => Fixtures.Need(Fixtures.Reading("NoneReadingPolicy"), "None reading policy"); private static ReadingPolicy AllReading() => Fixtures.Need(Fixtures.Reading("AllReadingPolicy"), "All reading policy"); /// A caste managing all five slots with the given values. private static Caste FullCaste(string label, ApparelPolicy apparel, FoodPolicy food, DrugPolicy drug, ReadingPolicy reading, MedicalCareCategory care) { Caste c = Fixtures.NewCaste(label); CasteManager.SetSlot(c, CasteSlot.Apparel, apparel); CasteManager.SetSlot(c, CasteSlot.Food, food); CasteManager.SetSlot(c, CasteSlot.Drug, drug); CasteManager.SetSlot(c, CasteSlot.Reading, reading); CasteManager.SetSlot(c, CasteSlot.MedCare, care); foreach (CasteSlot slot in CasteUtility.PolicySlots) T.Check(c.IsManaged(slot), "setup: " + slot + " slot of " + c.label + " is not managed after SetSlot"); return c; } private static string Values(Pawn p) => string.Join(", ", CasteUtility.AllSlots.Select(s => s + "=" + CasteUtility.ValueLabel(s, CasteUtility.CurrentValue(p, s)))); // ---- tests --------------------------------------------------------------------------- // Runs first in the whole suite (class and source order), before any fixture joins the colony. [BlaxTest("starter castes: a new game has Colonist, Slave (Ideology) and Prisoner, wired rows, and the starting colonists in Colonist with unchanged policies")] public static void NewGameState() { GameComponent_BlaxCastes comp = Fixtures.Comp; Game game = Current.Game; T.NotNull(comp, "GameComponent_BlaxCastes.Instance"); T.Check(comp.StarterCastesSeeded, "the new game is not marked as seeded"); T.Equal(ModsConfig.IdeologyActive ? 3 : 2, comp.Castes.Count, "castes in a new game (" + string.Join(", ", comp.Castes.Select(c => c.label)) + ")"); Caste colonist = comp.Castes.FirstOrDefault(c => c.label == StarterCastes.ColonistLabel); Caste slave = comp.Castes.FirstOrDefault(c => c.label == StarterCastes.SlaveLabel); Caste prisoner = comp.Castes.FirstOrDefault(c => c.label == StarterCastes.PrisonerLabel); T.NotNull(colonist, "Colonist caste"); T.NotNull(prisoner, "Prisoner caste"); T.Check(comp.DefaultCaste == colonist, "the default caste is " + comp.DefaultCaste + ", not Colonist"); // Colonist: the database defaults, medical care unmanaged. T.Check(colonist.ApparelPolicy == game.outfitDatabase.DefaultOutfit(), "Colonist apparel: " + colonist.ApparelPolicy?.label); T.Check(colonist.FoodPolicy == game.foodRestrictionDatabase.DefaultFoodRestriction(), "Colonist food: " + colonist.FoodPolicy?.label); T.Check(colonist.DrugPolicy == game.drugPolicyDatabase.DefaultDrugPolicy(), "Colonist drugs: " + colonist.DrugPolicy?.label); T.Check(colonist.ReadingPolicy == game.readingPolicyDatabase.DefaultReadingPolicy(), "Colonist reading: " + colonist.ReadingPolicy?.label); T.Check(!colonist.IsManaged(CasteSlot.MedCare), "Colonist manages medical care"); // Prisoner: Simple food only. T.Check(prisoner.FoodPolicy == Simple(), "Prisoner food: " + prisoner.FoodPolicy?.label); T.Check(!prisoner.IsManaged(CasteSlot.Apparel) && !prisoner.IsManaged(CasteSlot.Drug) && !prisoner.IsManaged(CasteSlot.Reading) && !prisoner.IsManaged(CasteSlot.MedCare), "Prisoner manages more than food"); // Rows. T.Equal(JoinRowMode.DefaultCaste, comp.GetRow(JoinReason.BoughtColonist).mode, "bought-as-colonist row"); T.Equal(JoinRowMode.DefaultCaste, comp.GetRow(JoinReason.NewColonist).mode, "new-colonist row"); T.Check(comp.ResolveRow(JoinReason.NewPrisoner) == prisoner, "new-prisoner row does not resolve to Prisoner"); if (slave != null) { // Slave: the database's Slave outfit, Simple food, No drugs by source def; reading and medical care unmanaged. T.Check(slave.ApparelPolicy != null && slave.ApparelPolicy == StarterCastes.SlaveOutfit(), "Slave apparel: " + slave.ApparelPolicy?.label); T.Check(game.outfitDatabase.AllOutfits.Contains(slave.ApparelPolicy), "Slave apparel is not the database's own outfit"); T.Check(slave.FoodPolicy == Simple(), "Slave food: " + slave.FoodPolicy?.label); T.Check(slave.DrugPolicy != null && slave.DrugPolicy.sourceDef?.defName == "NoDrugs", "Slave drugs: " + slave.DrugPolicy?.label); T.Check(!slave.IsManaged(CasteSlot.Reading) && !slave.IsManaged(CasteSlot.MedCare), "Slave manages reading or medical care"); T.Check(comp.ResolveRow(JoinReason.BoughtSlave) == slave, "bought-as-slave row does not resolve to Slave"); T.Check(comp.ResolveRow(JoinReason.NewSlave) == slave, "new-slave row does not resolve to Slave"); } else { T.Check(!ModsConfig.IdeologyActive, "no Slave caste although Ideology is active"); T.Equal(JoinRowMode.NoCaste, comp.GetRow(JoinReason.BoughtSlave).mode, "bought-as-slave row without Ideology"); T.Equal(JoinRowMode.NoCaste, comp.GetRow(JoinReason.NewSlave).mode, "new-slave row without Ideology"); } T.Equal(JoinReason.Rows.Length, comp.JoinRows.Count, "join rows in a new game"); // Starting colonists: Colonist members, set by the new-colonist row, with no overrides. List starting = Fixtures.Map.mapPawns.FreeColonists.Where(p => JoinDefaults.Eligible(p)).ToList(); T.Check(starting.Count > 0, "no starting colonists on the map"); foreach (Pawn p in starting) { CasteMembership m = comp.MembershipOf(p); T.Check(m != null && m.caste == colonist && m.auto && m.reason == JoinReason.NewColonist, p.LabelShort + "'s membership: " + (m == null ? "none" : $"caste={m.caste}, auto={m.auto}, reason={m.reason}")); T.Equal(0, CasteManager.Overrides(p).Count, p.LabelShort + "'s overrides (" + Values(p) + ")"); } T.Equal(0, comp.JoinQueue.Count(q => q != null && starting.Contains(q.pawn)), "starting colonists still queued"); T.Info("starting colonists: " + string.Join(", ", starting.Select(p => p.LabelShort))); } [BlaxTest("castes: assigning sets apparel, food, drugs, reading and medical care on a colonist")] public static void ApplyFiveSlots() { ApparelPolicy outfit = SlaveOrSoldier(); FoodPolicy food = Simple(); DrugPolicy drug = NoDrugs(); ReadingPolicy reading = NoneReading(); const MedicalCareCategory care = MedicalCareCategory.HerbalOrWorse; Pawn p = Fixtures.Colonist(); // Start from values the caste doesn't use (a generated colonist's ideo can already give it No drugs). p.outfits.CurrentApparelPolicy = Worker(); p.foodRestriction.CurrentFoodPolicy = Paste(); p.drugs.CurrentPolicy = Social(); p.reading.CurrentPolicy = AllReading(); p.playerSettings.medCare = MedicalCareCategory.Best; T.Info("before: " + Values(p)); T.Check(p.outfits.CurrentApparelPolicy != outfit && p.foodRestriction.CurrentFoodPolicy != food && p.drugs.CurrentPolicy != drug && p.reading.CurrentPolicy != reading && p.playerSettings.medCare != care, "setup: the colonist already has one of the caste's values, so the test proves nothing: " + Values(p)); Caste c = FullCaste("Slave test", outfit, food, drug, reading, care); T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign returned false for a colonist"); T.Check(p.outfits.CurrentApparelPolicy == outfit, "apparel: " + Values(p)); T.Check(p.foodRestriction.CurrentFoodPolicy == food, "food: " + Values(p)); T.Check(p.drugs.CurrentPolicy == drug, "drugs: " + Values(p)); T.Check(p.reading.CurrentPolicy == reading, "reading: " + Values(p)); T.Equal(care, p.playerSettings.medCare, "medical care"); CasteMembership m = Fixtures.Comp.MembershipOf(p); T.NotNull(m, "membership"); T.Check(m.caste == c && !m.auto && m.reason == JoinReason.Manual, $"membership: caste={m.caste}, auto={m.auto}, reason={m.reason}"); T.Equal(0, CasteManager.Overrides(p).Count, "overrides right after Assign"); } [BlaxTest("castes: a colony slave gets all five slots (Ideology)")] public static void SlaveGetsFiveSlots() { Pawn p = Fixtures.ColonySlave(); T.Check(p.outfits != null && p.drugs != null && p.reading != null, "setup: slave lacks a policy tracker"); p.outfits.CurrentApparelPolicy = Worker(); p.drugs.CurrentPolicy = Social(); p.reading.CurrentPolicy = AllReading(); p.foodRestriction.CurrentFoodPolicy = Paste(); p.playerSettings.medCare = MedicalCareCategory.Best; Caste c = FullCaste("Slave caste test", SlaveOrSoldier(), Simple(), NoDrugs(), NoneReading(), MedicalCareCategory.NoMeds); T.Check(CasteManager.Assign(p, c, true, JoinReason.NewSlave), "Assign returned false for a colony slave"); T.Check(p.outfits.CurrentApparelPolicy == SlaveOrSoldier() && p.foodRestriction.CurrentFoodPolicy == Simple() && p.drugs.CurrentPolicy == NoDrugs() && p.reading.CurrentPolicy == NoneReading(), "slave's policies: " + Values(p)); T.Equal(MedicalCareCategory.NoMeds, p.playerSettings.medCare, "slave's medical care"); } [BlaxTest("castes: a player prisoner gets food and medical care only; its outfit, drug and reading trackers are untouched")] public static void PrisonerFoodAndCareOnly() { Pawn p = Fixtures.Prisoner(); // An arrested colonist keeps the trackers a colonist has; a captured raider has none. p.outfits ??= new Pawn_OutfitTracker(p); p.drugs ??= new Pawn_DrugPolicyTracker(p); p.reading ??= new Pawn_ReadingTracker(p); p.outfits.CurrentApparelPolicy = Worker(); p.drugs.CurrentPolicy = Social(); p.reading.CurrentPolicy = AllReading(); T.NotNull(p.foodRestriction, "prisoner's food tracker"); T.NotNull(p.playerSettings, "prisoner's player settings"); FoodPolicy food = p.foodRestriction.CurrentFoodPolicy == Paste() ? Simple() : Paste(); MedicalCareCategory care = p.playerSettings.medCare == MedicalCareCategory.NoMeds ? MedicalCareCategory.NormalOrWorse : MedicalCareCategory.NoMeds; Caste c = FullCaste("Prisoner test", Nudist(), food, NoDrugs(), NoneReading(), care); T.Check(CasteManager.Assign(p, c, true, JoinReason.NewPrisoner), "Assign returned false for a player prisoner"); T.Check(p.foodRestriction.CurrentFoodPolicy == food, "food not set: " + Values(p)); T.Equal(care, p.playerSettings.medCare, "medical care"); T.Check(p.outfits.CurrentApparelPolicy == Worker(), "prisoner's outfit changed: " + Values(p)); T.Check(p.drugs.CurrentPolicy == Social(), "prisoner's drug policy changed: " + Values(p)); T.Check(p.reading.CurrentPolicy == AllReading(), "prisoner's reading policy changed: " + Values(p)); T.Equal(0, CasteManager.Overrides(p).Count, "overrides on a prisoner (slots it can't take are never overrides)"); } [BlaxTest("castes: a quest lodger gets food and medical care, not apparel, drugs or reading")] public static void QuestLodgerSkips() { Pawn p = Fixtures.QuestLodger(); p.outfits.CurrentApparelPolicy = Worker(); p.drugs.CurrentPolicy = Social(); p.reading.CurrentPolicy = AllReading(); p.foodRestriction.CurrentFoodPolicy = Paste(); p.playerSettings.medCare = MedicalCareCategory.Best; Caste c = FullCaste("Lodger test", Nudist(), Simple(), NoDrugs(), NoneReading(), MedicalCareCategory.NoMeds); T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign returned false for a quest lodger"); T.Check(p.outfits.CurrentApparelPolicy == Worker(), "lodger's outfit changed: " + Values(p)); T.Check(p.drugs.CurrentPolicy == Social(), "lodger's drug policy changed: " + Values(p)); T.Check(p.reading.CurrentPolicy == AllReading(), "lodger's reading policy changed: " + Values(p)); T.Check(p.foodRestriction.CurrentFoodPolicy == Simple(), "lodger's food not set: " + Values(p)); T.Equal(MedicalCareCategory.NoMeds, p.playerSettings.medCare, "lodger's medical care"); } [BlaxTest("castes: a baby gets apparel, drugs, reading and medical care, not food")] public static void BabySkipsFood() { Pawn p = Fixtures.Baby(); T.Check(p.outfits != null && p.drugs != null && p.reading != null && p.playerSettings != null && p.foodRestriction != null, "setup: baby lacks a tracker: outfits=" + (p.outfits != null) + " drugs=" + (p.drugs != null) + " reading=" + (p.reading != null) + " playerSettings=" + (p.playerSettings != null) + " food=" + (p.foodRestriction != null)); p.outfits.CurrentApparelPolicy = Worker(); p.drugs.CurrentPolicy = Social(); p.reading.CurrentPolicy = AllReading(); p.foodRestriction.CurrentFoodPolicy = Paste(); p.playerSettings.medCare = MedicalCareCategory.Best; Caste c = FullCaste("Baby test", Nudist(), Simple(), NoDrugs(), NoneReading(), MedicalCareCategory.NoMeds); T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign returned false for a baby"); T.Check(p.foodRestriction.CurrentFoodPolicy == Paste(), "baby's food changed: " + Values(p)); T.Check(p.outfits.CurrentApparelPolicy == Nudist(), "baby's outfit not set: " + Values(p)); T.Check(p.drugs.CurrentPolicy == NoDrugs(), "baby's drug policy not set: " + Values(p)); T.Check(p.reading.CurrentPolicy == NoneReading(), "baby's reading policy not set: " + Values(p)); T.Equal(MedicalCareCategory.NoMeds, p.playerSettings.medCare, "baby's medical care"); } [BlaxTest("castes: changing a slot moves members on the old value and keeps hand changes (Follow rule)")] public static void FollowRule() { ApparelPolicy first = SlaveOrSoldier(); Caste c = Fixtures.NewCaste("Follow test"); CasteManager.SetSlot(c, CasteSlot.Apparel, first); Pawn a = Fixtures.Colonist(); Pawn b = Fixtures.Colonist(); T.Check(CasteManager.Assign(a, c, false, JoinReason.Manual) && CasteManager.Assign(b, c, false, JoinReason.Manual), "Assign failed"); T.Check(a.outfits.CurrentApparelPolicy == first && b.outfits.CurrentApparelPolicy == first, "setup: members not on " + first.label); b.outfits.CurrentApparelPolicy = Worker(); // by hand List overrides = CasteManager.Overrides(b); T.Check(overrides.Count == 1 && overrides[0] == CasteSlot.Apparel, "B's overrides: " + string.Join(", ", overrides)); int moved = CasteManager.SetSlot(c, CasteSlot.Apparel, Nudist()); T.Equal(1, moved, "members moved by the apparel change"); T.Check(a.outfits.CurrentApparelPolicy == Nudist(), "A did not follow the caste: " + a.outfits.CurrentApparelPolicy.label); T.Check(b.outfits.CurrentApparelPolicy == Worker(), "B lost its hand-set outfit: " + b.outfits.CurrentApparelPolicy.label); T.Check(c.ApparelPolicy == Nudist(), "caste slot not changed"); // A slot that was unmanaged has no old value: every member gets it. moved = CasteManager.SetSlot(c, CasteSlot.Food, Simple()); T.Equal(2, moved, "members moved when the food slot became managed"); T.Check(a.foodRestriction.CurrentFoodPolicy == Simple() && b.foodRestriction.CurrentFoodPolicy == Simple(), "food not set on both members"); // Medical care follows the same rule. CasteManager.SetSlot(c, CasteSlot.MedCare, MedicalCareCategory.HerbalOrWorse); b.playerSettings.medCare = MedicalCareCategory.Best; moved = CasteManager.SetSlot(c, CasteSlot.MedCare, MedicalCareCategory.NoMeds); T.Equal(1, moved, "members moved by the medical care change"); T.Equal(MedicalCareCategory.NoMeds, a.playerSettings.medCare, "A's medical care"); T.Equal(MedicalCareCategory.Best, b.playerSettings.medCare, "B's hand-set medical care"); // Unmanaging a slot changes nobody. moved = CasteManager.SetSlot(c, CasteSlot.Food, null); T.Equal(0, moved, "members moved by unmanaging food"); T.Check(!c.IsManaged(CasteSlot.Food), "food slot still managed"); T.Check(a.foodRestriction.CurrentFoodPolicy == Simple() && b.foodRestriction.CurrentFoodPolicy == Simple(), "unmanaging food changed a member"); } [BlaxTest("castes: with the enabled setting off, assigning and slot changes write no policy")] public static void DisabledAppliesNothing() { Pawn p = Fixtures.Colonist(); Caste c = FullCaste("Disabled test", SlaveOrSoldier(), Simple(), NoDrugs(), NoneReading(), MedicalCareCategory.HerbalOrWorse); string before = Values(p); Fixtures.Settings.enabled = false; T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign returned false while disabled"); T.Equal(before, Values(p), "policies after Assign while disabled"); T.Equal(0, CasteUtility.Apply(p, c).Count, "slots Apply wrote while disabled"); T.Equal(0, CasteManager.SetSlot(c, CasteSlot.Apparel, Nudist()), "members moved by SetSlot while disabled"); T.Equal(before, Values(p), "policies after SetSlot while disabled"); T.Check(Fixtures.Comp.CasteOf(p) == c, "membership not recorded while disabled"); Fixtures.Settings.enabled = true; T.Check(CasteManager.ResetToCaste(p), "ResetToCaste returned false"); T.Check(p.outfits.CurrentApparelPolicy == Nudist() && p.foodRestriction.CurrentFoodPolicy == Simple(), "re-enabled ResetToCaste did not apply: " + Values(p)); } [BlaxTest("castes: the 2500-tick sweep ends the membership of a pawn that left the faction")] public static void SweepEndsMembership() { Caste c = Fixtures.NewCaste("Sweep test"); CasteManager.SetSlot(c, CasteSlot.Food, Simple()); Pawn leaver = Fixtures.Colonist(); Pawn stayer = Fixtures.Colonist(); foreach (Pawn p in new[] { leaver, stayer }) T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed for " + p.LabelShort); Faction other = Fixtures.FriendlyFaction(); T.NotNull(other, "a friendly faction"); leaver.SetFaction(other); T.Check(Fixtures.Comp.MembershipOf(leaver) != null, "membership ended before the sweep ran"); Fixtures.TickThroughSweep(); T.Check(Fixtures.Comp.MembershipOf(leaver) == null, "the sweep kept the membership of a pawn of " + other.Name); T.Check(Fixtures.Comp.CasteOf(stayer) == c, "the sweep ended a colonist's membership"); // A player prisoner stays a member. Swept directly: a fixture prisoner outside a prison cell makes // vanilla's prisoner job givers throw when the game ticks. Pawn prisoner = Fixtures.Prisoner(); T.Check(CasteManager.Assign(prisoner, c, false, JoinReason.Manual), "Assign failed for the prisoner"); Fixtures.Comp.Sweep(); T.Check(Fixtures.Comp.CasteOf(prisoner) == c, "the sweep ended a player prisoner's membership"); prisoner.guest.SetGuestStatus(null); prisoner.SetFaction(other); Fixtures.Comp.Sweep(); T.Check(Fixtures.Comp.MembershipOf(prisoner) == null, "the sweep kept the membership of a released prisoner of " + other.Name); } [BlaxTest("castes: a policy removed from its database is skipped and unmanaged, without errors")] public static void MissingPolicySkipped() { ApparelPolicy temp = Current.Game.outfitDatabase.MakeNewOutfit(); temp.label = "BlaxCastes test outfit"; Caste c = Fixtures.NewCaste("Missing policy test"); CasteManager.SetSlot(c, CasteSlot.Apparel, temp); CasteManager.SetSlot(c, CasteSlot.Food, Simple()); T.Check(c.ApparelPolicy == temp, "setup: apparel slot not set"); Pawn p = Fixtures.Colonist(); ApparelPolicy before = p.outfits.CurrentApparelPolicy; Current.Game.outfitDatabase.AllOutfits.Remove(temp); // a mod deleting it without TryDelete T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed"); T.Check(p.outfits.CurrentApparelPolicy == before, "the removed outfit was written: " + p.outfits.CurrentApparelPolicy.label); T.Check(p.foodRestriction.CurrentFoodPolicy == Simple(), "the food slot was not written next to the missing outfit"); T.Check(!c.IsManaged(CasteSlot.Apparel), "the missing outfit is still managed"); } } }