using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using BlaxCastes; using HarmonyLib; using RimWorld; using Verse; namespace BlaxCastesTests { /// /// The schedule, work priorities and weapon loadout slots: what they write, who they skip, the Follow /// rule, the checks against missing defs and loadouts, and the Caste column in the Schedule and Work tabs. /// The loadout tests need Weapons Tab Reborn (run-tests.ps1 -Compat) and skip without it, except the one /// that checks the slot stays off without the mod. /// public static class SlotTests { // ---- schedule helpers -------------------------------------------------------------------- /// Work from 22 to 5, joy 6 and 7, sleep 8 to 15, anything the rest: nothing like the starting schedule. private static List NightSchedule() { var names = new List(); for (int h = 0; h < ScheduleSlot.Hours; h++) { TimeAssignmentDef d = h >= 22 || h <= 5 ? TimeAssignmentDefOf.Work : h <= 7 ? TimeAssignmentDefOf.Joy : h <= 15 ? TimeAssignmentDefOf.Sleep : TimeAssignmentDefOf.Anything; names.Add(d.defName); } return names; } private static string Hours(Pawn p) => ScheduleSlot.Summary(ScheduleSlot.Of(p)); private static void CheckSchedule(Pawn p, IReadOnlyList names, string who) { T.Check(ScheduleSlot.HasTimetable(p), who + " has no timetable"); for (int h = 0; h < ScheduleSlot.Hours; h++) { string mine = p.timetable.GetAssignment(h).defName; T.Check(mine == names[h], $"{who} hour {h}: {mine}, the caste says {names[h]} ({who}: {Hours(p)})"); } } // ---- work helpers ------------------------------------------------------------------------ private static WorkTypeDef Work(string defName) => Fixtures.NeedDef(DefDatabase.GetNamedSilentFail(defName), "work type " + defName); private static WorkTypeDef Growing => Work("Growing"); private static WorkTypeDef Firefighter => Work("Firefighter"); private static WorkTypeDef Cooking => Work("Cooking"); /// A pawn from the factory that can do every given work type (backstories and traits can disable some). private static Pawn PawnThatCan(Func make, params WorkTypeDef[] types) { for (int i = 0; i < 12; i++) { Pawn p = make(); Fixtures.Unqueue(p); if (WorkSlot.HasWorkSettings(p) && types.All(t => !p.WorkTypeIsDisabled(t))) return p; } T.Fail("no generated pawn could do " + string.Join(", ", types.Select(t => t.defName)) + " in 12 tries"); return null; } /// Runs the body with the game's manual priorities switch set, and puts it back afterwards. private static void WithManualPriorities(bool on, Action body) { PlaySettings play = Find.PlaySettings; bool before = play.useWorkPriorities; play.useWorkPriorities = on; try { body(); } finally { play.useWorkPriorities = before; } } private static int Priority(Pawn p, WorkTypeDef def) => p.workSettings.GetPriority(def); private static Dictionary AllPriorities(Pawn p) => DefDatabase.AllDefsListForReading.ToDictionary(d => d, d => p.workSettings.GetPriority(d)); private static void CheckWorkSlotUsable() { if (WorkSlot.ActiveManager() != null) T.Skip(WorkSlot.ActiveManager() + " is active, so the work slot is off"); T.Check(WorkSlot.Active, "the work slot is off (manageWorkPriorities " + Fixtures.Settings.manageWorkPriorities + ")"); } // ---- loadout helpers (Weapons Tab Reborn, by reflection) --------------------------------- private static void NeedWeaponsTab() { if (!WeaponsTab.ModActive) T.Skip("needs Weapons Tab Reborn (run-tests.ps1 -Compat)"); T.Check(WeaponsTab.Available, "Weapons Tab Reborn is active but its loadout API is not available (see the warning in the log)"); } private static object LoadoutDatabase() { Type dictionary = AccessTools.TypeByName(WeaponsTab.ApiTypeName); T.NotNull(dictionary, WeaponsTab.ApiTypeName); object db = AccessTools.Field(dictionary, "CurrentLoadoutDatabase")?.GetValue(null); T.NotNull(db, "Weapons Tab Reborn's loadout database (its Game patch did not run?)"); return db; } /// A new Weapons Tab Reborn loadout with this label; returns its id. private static int MakeLoadout(string label) { object db = LoadoutDatabase(); object loadout = AccessTools.Method(db.GetType(), "MakeNewLoadout").Invoke(db, null); T.NotNull(loadout, "MakeNewLoadout result"); AccessTools.Field(loadout.GetType(), "label").SetValue(loadout, label); return (int)AccessTools.Field(loadout.GetType(), "uniqueID").GetValue(loadout); } /// Deletes a loadout the way Weapons Tab Reborn's window does (refused while a live pawn uses it). private static AcceptanceReport DeleteLoadout(int id) { object db = LoadoutDatabase(); var all = (System.Collections.IList)AccessTools.Property(db.GetType(), "AllLoadouts").GetValue(db); object loadout = all.Cast().FirstOrDefault(l => (int)AccessTools.Field(l.GetType(), "uniqueID").GetValue(l) == id); T.NotNull(loadout, "loadout " + id); return (AcceptanceReport)AccessTools.Method(db.GetType(), "TryDelete").Invoke(db, new[] { loadout }); } private static void SetLoadoutIdRaw(Caste c, int id) => AccessTools.Field(typeof(Caste), "loadoutId").SetValue(c, id); private static void CheckSlots() => AccessTools.Method(typeof(CasteManager), "CheckSlots").Invoke(null, new object[] { Fixtures.Comp }); // ---- schedule ---------------------------------------------------------------------------- [BlaxTest("schedule: a caste's night schedule sets all 24 hours on a colonist and on a slave")] public static void ScheduleOnColonistAndSlave() { List night = NightSchedule(); Caste c = Fixtures.NewCaste("Night shift"); T.Equal(0, CasteManager.SetSlot(c, CasteSlot.Schedule, night), "members moved on a caste without members"); T.Check(c.IsManaged(CasteSlot.Schedule) && c.Schedule.SequenceEqual(night), "the caste does not hold the night schedule: " + ScheduleSlot.Summary(c.Schedule)); Pawn colonist = Fixtures.Colonist(); T.Check(!ScheduleSlot.Matches(colonist, night), "setup: the colonist already works nights"); T.Check(CasteManager.Assign(colonist, c, false, JoinReason.Manual), "Assign failed for the colonist"); CheckSchedule(colonist, night, "colonist"); T.Equal(0, CasteManager.Overrides(colonist).Count, "colonist's overrides right after Assign"); Pawn slave = Fixtures.ColonySlave(); // skips here without Ideology; the colonist part passed T.Check(CasteManager.Assign(slave, c, true, JoinReason.NewSlave), "Assign failed for the slave"); CheckSchedule(slave, night, "slave"); } [BlaxTest("schedule: a prisoner skips the schedule without errors, even with a timetable; its food is still set")] public static void SchedulePrisonerSkips() { Pawn p = Fixtures.Prisoner(); T.Info("a captured raider's timetable: " + (p.timetable == null ? "none" : "present")); T.Check(!CasteUtility.CanUse(p, CasteSlot.Schedule), "CanUse(Schedule) is true for a prisoner without a timetable"); Caste c = Fixtures.NewCaste("Prisoner night shift"); CasteManager.SetSlot(c, CasteSlot.Schedule, NightSchedule()); FoodPolicy food = Fixtures.Need(Fixtures.Food(p.foodRestriction.CurrentFoodPolicy == Fixtures.Food("FoodRestrictionPaste") ? "FoodRestrictionSimple" : "FoodRestrictionPaste"), "food policy"); CasteManager.SetSlot(c, CasteSlot.Food, food); T.Check(CasteManager.Assign(p, c, true, JoinReason.NewPrisoner), "Assign failed for the prisoner"); T.Check(p.foodRestriction.CurrentFoodPolicy == food, "the prisoner's food was not set next to the skipped schedule"); // An arrested colonist keeps a timetable; the slot still skips it. p.timetable ??= new Pawn_TimetableTracker(p); string[] before = ScheduleSlot.Of(p); List applied = CasteUtility.Apply(p, c); T.Check(!applied.Contains(CasteSlot.Schedule), "Apply wrote the schedule on a prisoner"); T.Check(ScheduleSlot.Of(p).SequenceEqual(before), "the prisoner's timetable changed: " + Hours(p)); T.Check(!CasteManager.Overrides(p).Contains(CasteSlot.Schedule), "a prisoner's schedule counts as an override"); } [BlaxTest("schedule: Follow rule on the whole day: A follows a schedule change, B with one hour changed keeps its own; the tooltip names Schedule")] public static void ScheduleFollowRule() { List start = ScheduleSlot.Default(); Caste c = Fixtures.NewCaste("Schedule follow"); CasteManager.SetSlot(c, CasteSlot.Schedule, start); 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"); CheckSchedule(a, start, "A"); b.timetable.SetAssignment(12, TimeAssignmentDefOf.Joy); // by hand List overrides = CasteManager.Overrides(b); T.Check(overrides.Count == 1 && overrides[0] == CasteSlot.Schedule, "B's overrides: " + string.Join(", ", overrides)); string tip = PawnColumnWorker_BlaxCaste.CellTip(b); T.Check(tip.Contains(CasteUtility.SlotLabel(CasteSlot.Schedule)), "B's tooltip does not name the schedule: " + tip); string[] bBefore = ScheduleSlot.Of(b); List night = NightSchedule(); T.Equal(1, CasteManager.SetSlot(c, CasteSlot.Schedule, night), "members moved by the schedule change"); CheckSchedule(a, night, "A"); T.Check(ScheduleSlot.Of(b).SequenceEqual(bBefore), "B lost its own schedule: " + Hours(b)); // Painting one hour in the manager moves the followers the same way. T.Equal(1, Dialog_ManageCastes.PaintHour(c, 16, TimeAssignmentDefOf.Joy), "members moved by painting an hour"); T.Equal(TimeAssignmentDefOf.Joy, a.timetable.GetAssignment(16), "A's hour 16 after painting"); T.Check(ScheduleSlot.Of(b).SequenceEqual(bBefore), "painting changed B's schedule: " + Hours(b)); // Apply to all members writes the caste's schedule over B's hand change. CasteManager.ApplyToAll(c); CheckSchedule(b, c.Schedule, "B after Apply to all"); // Leaving the schedule alone moves nobody. T.Equal(0, CasteManager.SetSlot(c, CasteSlot.Schedule, null), "members moved by leaving the schedule alone"); T.Check(!c.IsManaged(CasteSlot.Schedule), "the schedule is still managed"); T.Equal(TimeAssignmentDefOf.Joy, a.timetable.GetAssignment(16), "A's schedule changed when the caste let go of it"); } [BlaxTest("schedule: an unknown time assignment becomes Anything (on set, on apply and in the load check, with one warning); Meditate is stored either way and applies as Anything without Royalty")] public static void ScheduleUnknownName() { const string unknown = "BlaxCastesNoSuchAssignment"; T.ExpectWarning("Unknown hours"); string anything = TimeAssignmentDefOf.Anything.defName; List names = NightSchedule(); names[2] = unknown; names[3] = "Meditate"; Caste c = Fixtures.NewCaste("Unknown hours"); CasteManager.SetSlot(c, CasteSlot.Schedule, names); T.Equal(anything, c.Schedule[2], "stored hour with an unknown assignment"); T.Equal("Meditate", c.Schedule[3], "stored Meditate hour (Royalty " + ModsConfig.RoyaltyActive + ")"); string meditate = ModsConfig.RoyaltyActive ? "Meditate" : anything; Pawn p = Fixtures.Colonist(); T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed"); T.Equal(anything, p.timetable.GetAssignment(2).defName, "pawn's hour 2"); T.Equal(meditate, p.timetable.GetAssignment(3).defName, "pawn's hour 3"); // A name that went missing after it was stored (a removed mod): Apply writes Anything, the load check fixes the slot. List stored = c.Schedule.ToList(); stored[5] = unknown; AccessTools.Field(typeof(Caste), "schedule").SetValue(c, stored); p.timetable.SetAssignment(5, TimeAssignmentDefOf.Sleep); T.Check(CasteUtility.Apply(p, c).Contains(CasteSlot.Schedule), "Apply skipped the schedule with an unknown hour"); T.Equal(anything, p.timetable.GetAssignment(5).defName, "pawn's hour 5 from an unknown stored name"); T.Equal(0, CasteManager.Overrides(p).Count, "an unknown stored hour counts as an override"); CheckSlots(); T.Equal(anything, c.Schedule[5], "stored hour 5 after the load check"); T.Equal("Meditate", c.Schedule[3], "stored Meditate hour after the load check"); } [BlaxTest("work: a work type nothing knows (a removed mod) is dropped by the load check with one warning; the caste's other types stay")] public static void WorkUnknownName() { const string unknown = "BlaxCastesNoSuchWork"; T.ExpectWarning(unknown); Caste c = Fixtures.NewCaste("Unknown work"); CasteManager.SetWorkPriority(c, Growing.defName, 1); var stored = new Dictionary(c.WorkPriorities) { [unknown] = 2 }; AccessTools.Field(typeof(Caste), "workPriorities").SetValue(c, stored); T.Check(WorkSlot.Summary(c.WorkPriorities).Contains(unknown), "setup: the unknown type is not in the summary: " + WorkSlot.Summary(c.WorkPriorities)); CheckSlots(); T.Check(!c.WorkPriorities.ContainsKey(unknown), "the unknown work type is still listed after the load check"); T.Equal(1, c.WorkPriorityOf(Growing.defName) ?? -1, "Growing after the load check"); } // ---- work priorities --------------------------------------------------------------------- [BlaxTest("work: a slave in a caste with Growing 1 and Firefighting 1 has both; every other work type is unchanged")] public static void WorkOnSlave() { CheckWorkSlotUsable(); WithManualPriorities(true, () => { Pawn s = PawnThatCan(() => Fixtures.ColonySlave(), Growing, Firefighter); s.workSettings.SetPriority(Growing, 3); s.workSettings.SetPriority(Firefighter, 4); Dictionary before = AllPriorities(s); Caste c = Fixtures.NewCaste("Field slave"); CasteManager.SetWorkPriority(c, Growing.defName, 1); CasteManager.SetWorkPriority(c, Firefighter.defName, 1); T.Check(c.IsManaged(CasteSlot.Work) && c.WorkPriorities.Count == 2, "the caste's work priorities: " + WorkSlot.Summary(c.WorkPriorities)); T.Check(CasteManager.Assign(s, c, true, JoinReason.NewSlave), "Assign failed for the slave"); T.Equal(1, Priority(s, Growing), "slave's Growing"); T.Equal(1, Priority(s, Firefighter), "slave's Firefighting"); var changed = before.Where(kv => kv.Key != Growing && kv.Key != Firefighter && Priority(s, kv.Key) != kv.Value) .Select(kv => kv.Key.defName + " " + kv.Value + " to " + Priority(s, kv.Key)).ToList(); T.Check(changed.Count == 0, "other work types changed: " + string.Join(", ", changed)); T.Equal(0, CasteManager.Overrides(s).Count, "slave's overrides right after Assign"); }); } [BlaxTest("work: a work type the pawn cannot do stays 0, with no error; the others are set")] public static void WorkDisabledTypeSkipped() { CheckWorkSlotUsable(); WithManualPriorities(true, () => { Pawn p = null; WorkTypeDef disabled = null; for (int i = 0; i < 12 && disabled == null; i++) { p = Fixtures.Colonist(); Fixtures.Unqueue(p); disabled = DefDatabase.AllDefsListForReading.FirstOrDefault(w => p.WorkTypeIsDisabled(w)); } if (disabled == null) { TraitDef pyromaniac = DefDatabase.GetNamedSilentFail("Pyromaniac"); T.NotNull(pyromaniac, "no colonist with a disabled work type in 12 tries, and no Pyromaniac trait"); p.story.traits.GainTrait(new Trait(pyromaniac)); p.Notify_DisabledWorkTypesChanged(); disabled = DefDatabase.AllDefsListForReading.FirstOrDefault(w => p.WorkTypeIsDisabled(w)); T.NotNull(disabled, "a disabled work type after adding Pyromaniac"); } WorkTypeDef able = DefDatabase.AllDefsListForReading.FirstOrDefault(w => !p.WorkTypeIsDisabled(w) && w != disabled); T.NotNull(able, "a work type the pawn can do"); T.Equal(0, Priority(p, disabled), "setup: the disabled type's priority"); T.Info("disabled: " + disabled.defName + ", set: " + able.defName); Caste c = Fixtures.NewCaste("Disabled work"); CasteManager.SetWorkPriority(c, disabled.defName, 1); CasteManager.SetWorkPriority(c, able.defName, 2); T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed"); T.Equal(0, Priority(p, disabled), "the disabled type's priority after Assign"); T.Equal(2, Priority(p, able), "the other type's priority"); T.Check(!CasteManager.Overrides(p).Contains(CasteSlot.Work), "a disabled type counts as an override"); // Changing the caste's value for the disabled type reaches nobody and logs nothing. CasteManager.SetWorkPriority(c, disabled.defName, 3); T.Equal(0, Priority(p, disabled), "the disabled type's priority after a caste change"); }); } [BlaxTest("work: Start from nothing leaves only the caste's work types on, plus Patient and Bed rest as they were; ticking it moves nobody")] public static void WorkStartFromNothing() { CheckWorkSlotUsable(); List selfCare = WorkSlot.SelfCareTypes(); T.Check(selfCare.Contains(Work("Patient")) && selfCare.Contains(Work("PatientBedRest")), "self-care work types: " + string.Join(", ", selfCare.Select(t => t.defName))); T.Check(!selfCare.Contains(Firefighter) && !selfCare.Any(t => t.defName == "Hauling" || t.defName == "Cleaning"), "work types kept by Start from nothing: " + string.Join(", ", selfCare.Select(t => t.defName))); string tip = Dialog_ManageCastes.StartFromNothingTip(); T.Check(selfCare.All(t => tip.Contains(WorkSlot.TypeLabel(t))), "the Start from nothing tooltip does not name the kept types: " + tip); WithManualPriorities(true, () => { Pawn p = PawnThatCan(Fixtures.Colonist, Growing, Cooking); p.workSettings.SetPriority(Cooking, 2); int activeBefore = DefDatabase.AllDefsListForReading.Count(w => Priority(p, w) > 0); T.Check(activeBefore > 1, "setup: the colonist does only " + activeBefore + " work types"); Dictionary selfCareBefore = selfCare.ToDictionary(t => t, t => Priority(p, t)); T.Check(selfCareBefore.Values.All(v => v > 0), "setup: Patient or Bed rest is off: " + string.Join(", ", selfCareBefore.Select(kv => kv.Key.defName + " " + kv.Value))); Caste c = Fixtures.NewCaste("Only growing"); CasteManager.SetWorkPriority(c, Growing.defName, 1); Pawn member = PawnThatCan(Fixtures.Colonist, Growing); T.Check(CasteManager.Assign(member, c, false, JoinReason.Manual), "Assign failed for the member"); Dictionary memberBefore = AllPriorities(member); T.Check(CasteManager.SetWorkStartFromNothing(c, true) && c.WorkStartFromNothing, "Start from nothing not set"); T.Check(AllPriorities(member).All(kv => memberBefore[kv.Key] == kv.Value), "ticking Start from nothing changed a member's priorities"); T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed"); var wrong = DefDatabase.AllDefsListForReading .Where(w => Priority(p, w) != (w == Growing ? 1 : selfCareBefore.TryGetValue(w, out int kept) ? kept : 0)) .Select(w => w.defName + " " + Priority(p, w)).ToList(); T.Check(wrong.Count == 0, "after Start from nothing: " + string.Join(", ", wrong)); T.Equal(0, CasteManager.Overrides(p).Count, "overrides right after Start from nothing"); }); } [BlaxTest("work: with manual priorities off, a nonzero caste value turns the work on and 0 turns it off; turning them on detaches nobody")] public static void WorkManualPrioritiesOff() { CheckWorkSlotUsable(); Pawn p = null; Caste c = null; WithManualPriorities(false, () => { p = PawnThatCan(Fixtures.Colonist, Growing, Cooking); p.workSettings.SetPriority(Growing, 0); p.workSettings.SetPriority(Cooking, 3); c = Fixtures.NewCaste("Simple work"); CasteManager.SetWorkPriority(c, Growing.defName, 1); CasteManager.SetWorkPriority(c, Cooking.defName, 0); T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed"); T.Check(p.workSettings.WorkIsActive(Growing), "Growing is not on"); T.Equal(3, Priority(p, Growing), "Growing with manual priorities off"); T.Equal(0, Priority(p, Cooking), "Cooking with manual priorities off"); T.Equal(0, CasteManager.Overrides(p).Count, "overrides with manual priorities off (1 and on are the same)"); T.Check(Dialog_ManageCastes.WorkNote() == (string)"BlaxCastes_WorkManualOff".Translate(), "manager note: " + Dialog_ManageCastes.WorkNote()); }); // The caste's own number was stored, so turning manual priorities on leaves the member following. WithManualPriorities(true, () => { T.Equal(1, Priority(p, Growing), "Growing once manual priorities are on (the caste's number)"); T.Equal(0, CasteManager.Overrides(p).Count, "overrides once manual priorities are on: " + string.Join(", ", CasteManager.Overrides(p))); T.Equal(1, CasteManager.SetWorkPriority(c, Growing.defName, 2), "members moved by Growing 1 to 2 after the switch"); T.Equal(2, Priority(p, Growing), "Growing after the caste change"); }); } [BlaxTest("work: Follow rule per work type: A follows each change, B keeps the type it changed by hand; the tooltip names it")] public static void WorkFollowRule() { CheckWorkSlotUsable(); WithManualPriorities(true, () => { Caste c = Fixtures.NewCaste("Work follow"); CasteManager.SetWorkPriority(c, Growing.defName, 1); CasteManager.SetWorkPriority(c, Cooking.defName, 2); Pawn a = PawnThatCan(Fixtures.Colonist, Growing, Cooking); Pawn b = PawnThatCan(Fixtures.Colonist, Growing, Cooking); T.Check(CasteManager.Assign(a, c, false, JoinReason.Manual) && CasteManager.Assign(b, c, false, JoinReason.Manual), "Assign failed"); b.workSettings.SetPriority(Growing, 3); // by hand List overrides = CasteManager.Overrides(b); T.Check(overrides.Count == 1 && overrides[0] == CasteSlot.Work, "B's overrides: " + string.Join(", ", overrides)); List types = CasteManager.WorkOverrides(b); T.Check(types.Count == 1 && types[0] == Growing, "B's work overrides: " + string.Join(", ", types.Select(t => t.defName))); string tip = PawnColumnWorker_BlaxCaste.CellTip(b); T.Check(tip.Contains(WorkSlot.TypeLabel(Growing)), "B's tooltip does not name Growing: " + tip); T.Equal(1, CasteManager.SetWorkPriority(c, Growing.defName, 2), "members moved by the Growing change"); T.Equal(2, Priority(a, Growing), "A's Growing"); T.Equal(3, Priority(b, Growing), "B's hand-set Growing"); T.Equal(2, CasteManager.SetWorkPriority(c, Cooking.defName, 1), "members moved by the Cooking change"); T.Check(Priority(a, Cooking) == 1 && Priority(b, Cooking) == 1, "Cooking: A " + Priority(a, Cooking) + ", B " + Priority(b, Cooking)); // The manager's cycle button goes 2 -> 3 and moves A only. T.Equal(1, Dialog_ManageCastes.CycleWork(c, Growing), "members moved by the cycle button"); T.Equal(3, c.WorkPriorityOf(Growing.defName) ?? -1, "Growing after the cycle button"); T.Equal(3, Priority(a, Growing), "A's Growing after the cycle button"); // Leaving a type alone moves nobody. T.Equal(0, CasteManager.SetWorkPriority(c, Cooking.defName, null), "members moved by leaving Cooking alone"); T.Check(c.WorkPriorityOf(Cooking.defName) == null, "Cooking is still listed"); T.Check(Priority(a, Cooking) == 1 && Priority(b, Cooking) == 1, "leaving Cooking alone changed a member"); // Apply to all members clears B's hand change. b.workSettings.SetPriority(Growing, 4); CasteManager.ApplyToAll(c); T.Equal(3, Priority(b, Growing), "B's Growing after Apply to all"); }); } [BlaxTest("work: clicking the cycle button through other values moves only the members that followed before the first click")] public static void WorkCycleRunKeepsHandChanges() { CheckWorkSlotUsable(); WithManualPriorities(true, () => { Caste c = Fixtures.NewCaste("Work cycle run"); CasteManager.SetWorkPriority(c, Growing.defName, 3); Pawn a = PawnThatCan(Fixtures.Colonist, Growing); Pawn b = PawnThatCan(Fixtures.Colonist, Growing); Pawn d = PawnThatCan(Fixtures.Colonist, Growing); foreach (Pawn p in new[] { a, b, d }) T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "setup: Assign failed"); b.workSettings.SetPriority(Growing, 1); // by hand d.workSettings.SetPriority(Growing, 4); // by hand // 3 -> 4 -> off -> leave alone -> 1 -> 2, the way the button gets from 3 to 2. var run = new Dialog_ManageCastes.WorkCycle(); var moved = new List(); for (int i = 0; i < 5; i++) moved.Add(run.Click(c, Growing)); T.Equal(2, c.WorkPriorityOf(Growing.defName) ?? -1, "Growing after five clicks"); T.Equal("1,1,0,1,1", string.Join(",", moved), "members moved per click (A only; nobody while leaving alone)"); T.Equal(2, Priority(a, Growing), "A's Growing (followed)"); T.Equal(1, Priority(b, Growing), "B's hand-set Growing"); T.Equal(4, Priority(d, Growing), "D's hand-set Growing (the run passed 4)"); // A change from elsewhere ends the run: the next click takes the followers anew. d.workSettings.SetPriority(Growing, 2); T.Equal(2, CasteManager.SetWorkPriority(c, Growing.defName, 1), "members moved by Growing 2 to 1 from elsewhere (A and D)"); T.Equal(1, Priority(a, Growing), "A after Growing 2 to 1 from elsewhere"); T.Equal(1, Priority(d, Growing), "D followed 2 to 1"); T.Equal(3, run.Click(c, Growing), "members moved by the next click (A, B and D all on 1 now)"); }); } [BlaxTest("work: pawns Assign by status puts into the caste during a cycle run follow the run's next clicks; a hand-set member stays out")] public static void WorkCycleRunTakesAssignedByStatus() { CheckWorkSlotUsable(); WithManualPriorities(true, () => { Caste c = Fixtures.NewCaste("Work cycle assign by status"); CasteManager.SetWorkPriority(c, Growing.defName, 3); T.Check(CasteManager.SetJoinRow(JoinReason.NewColonist, JoinRowMode.SpecificCaste, c), "setup: SetJoinRow failed"); Pawn a = PawnThatCan(Fixtures.Colonist, Growing); Pawn b = PawnThatCan(Fixtures.Colonist, Growing); foreach (Pawn p in new[] { a, b }) T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "setup: Assign failed"); b.workSettings.SetPriority(Growing, 4); // by hand, on a value the run passes through var run = new Dialog_ManageCastes.WorkCycle(); T.Equal(1, run.Click(c, Growing), "members moved by 3 -> 4 (A)"); Pawn u = PawnThatCan(Fixtures.Colonist, Growing); T.Check(CasteManager.PawnsToAssignByStatus().Contains(u), "setup: Assign by status does not offer the new colonist"); T.Check(Dialog_ManageCastes.AssignByStatus(run) >= 1, "Assign by status assigned nobody"); T.Check(Fixtures.Comp.CasteOf(u) == c, "the new colonist is not in the caste after Assign by status"); T.Equal(4, Priority(u, Growing), "U's Growing after Assign by status (the caste's value)"); run.Click(c, Growing); T.Equal(0, c.WorkPriorityOf(Growing.defName) ?? -1, "Growing after 4 -> off"); T.Equal(0, Priority(a, Growing), "A followed 4 -> off"); T.Equal(0, Priority(u, Growing), "U, assigned during the run, followed 4 -> off"); T.Equal(4, Priority(b, Growing), "B's hand-set 4 (it was not a follower when the run started)"); }); } [BlaxTest("work: the cycle runs leave alone, 1, 2, 3, 4, off and back; labels are -, numbers and off")] public static void WorkCycle() { int? v = null; var seen = new List(); for (int i = 0; i < 6; i++) { v = WorkSlot.Next(v); seen.Add(v?.ToString() ?? "null"); } T.Equal("1,2,3,4,0,null", string.Join(",", seen), "forward cycle from leave alone"); for (int i = 0; i < 6; i++) v = WorkSlot.Previous(v); T.Check(v == null, "six steps back did not come back to leave alone: " + v); T.Equal((string)"BlaxCastes_WorkLeaveAloneShort".Translate(), WorkSlot.PriorityLabel(null), "label for leave alone"); T.Equal((string)"BlaxCastes_WorkOff".Translate(), WorkSlot.PriorityLabel(0), "label for off"); T.Equal("2", WorkSlot.PriorityLabel(2), "label for 2"); List types = WorkSlot.Types(); T.Check(types.Count > 0 && types.All(t => t.visible), "the manager's work types are not the visible ones"); T.Check(types.SequenceEqual(types.OrderByDescending(t => t.naturalPriority)), "the manager's work types are not in the Work tab's order"); } [BlaxTest("work: an active work manager, or the setting off, greys the slot out and Castes writes no priority")] public static void WorkManagerTurnsSlotOff() { CheckWorkSlotUsable(); WithManualPriorities(true, () => { Pawn p = PawnThatCan(Fixtures.Colonist, Growing); p.workSettings.SetPriority(Growing, 4); Caste c = Fixtures.NewCaste("Managed elsewhere"); CasteManager.SetWorkPriority(c, Growing.defName, 1); WorkSlot.SimulatedActive.Add(Compat.FreeWill); try { T.Equal("Free Will", WorkSlot.ActiveManager(), "detected work manager"); T.Check(!WorkSlot.Active && !CasteUtility.SlotActive(CasteSlot.Work), "the work slot is on next to Free Will"); T.Check(Dialog_ManageCastes.WorkNote()?.Contains("Free Will") == true, "manager note: " + Dialog_ManageCastes.WorkNote()); T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed"); T.Equal(4, Priority(p, Growing), "Growing written next to Free Will"); T.Check(!CasteManager.Overrides(p).Contains(CasteSlot.Work), "work counts as an override while the slot is off"); } finally { WorkSlot.SimulatedActive.Remove(Compat.FreeWill); } Fixtures.Settings.manageWorkPriorities = false; T.Check(!WorkSlot.Active, "the work slot is on with manageWorkPriorities off"); T.Equal((string)"BlaxCastes_WorkSettingOff".Translate(), Dialog_ManageCastes.WorkNote(), "manager note with the setting off"); CasteManager.ResetToCaste(p); T.Equal(4, Priority(p, Growing), "Growing written with manageWorkPriorities off"); Fixtures.Settings.manageWorkPriorities = true; CasteManager.ResetToCaste(p); T.Equal(1, Priority(p, Growing), "Growing after turning the setting back on and Reset to caste"); }); } // ---- weapon loadout ---------------------------------------------------------------------- [BlaxTest("loadout: without Weapons Tab Reborn the slot is off, the row is hidden and a stored id is kept but not applied")] public static void LoadoutWithoutMod() { if (WeaponsTab.ModActive) T.Skip("Weapons Tab Reborn is active in this run"); T.Check(!WeaponsTab.Available && !CasteUtility.SlotActive(CasteSlot.Loadout), "the loadout slot is on without Weapons Tab Reborn"); T.Check(!Dialog_ManageCastes.ShowsLoadoutRow, "the manager shows the loadout row without Weapons Tab Reborn"); T.ExpectWarning("Weapon loadout 1 "); Caste c = Fixtures.NewCaste("No weapons tab"); T.Equal(0, CasteManager.SetSlot(c, CasteSlot.Loadout, 1), "members moved by setting a loadout without the mod"); T.Check(!c.IsManaged(CasteSlot.Loadout), "a loadout was stored without the mod"); // A save made with the mod keeps its id while the mod is away. SetLoadoutIdRaw(c, 3); Pawn p = Fixtures.Colonist(); T.Check(!CasteUtility.CanUse(p, CasteSlot.Loadout), "CanUse(Loadout) without the mod"); T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed"); T.Check(!CasteUtility.Apply(p, c).Contains(CasteSlot.Loadout), "Apply wrote a loadout without the mod"); CheckSlots(); T.Equal(3, c.LoadoutId, "the stored loadout id after the load check without the mod"); T.Equal(0, CasteManager.Overrides(p).Count, "overrides from a loadout without the mod"); } [BlaxTest("loadout: with Weapons Tab Reborn a caste sets its loadout on a colonist and a slave; a prisoner is skipped")] public static void LoadoutSetsIt() { NeedWeaponsTab(); int rifle = MakeLoadout("Blax test rifles"); T.Check(WeaponsTab.Exists(rifle) && WeaponsTab.NameOf(rifle) == "Blax test rifles", "the new loadout is not visible through the API"); T.Check(WeaponsTab.Loadouts().Any(l => l.Key == rifle), "the new loadout is not in the manager's list"); Caste c = Fixtures.NewCaste("Guards"); CasteManager.SetSlot(c, CasteSlot.Loadout, rifle); T.Equal(rifle, c.LoadoutId, "the caste's loadout"); T.Equal("Blax test rifles", CasteUtility.ValueLabel(CasteSlot.Loadout, c.LoadoutId), "the loadout's label in the manager"); Pawn colonist = Fixtures.Colonist(); T.Check(WeaponsTab.LoadoutOf(colonist) != rifle, "setup: the colonist already has the new loadout"); T.Check(CasteManager.Assign(colonist, c, false, JoinReason.Manual), "Assign failed for the colonist"); T.Equal((int?)rifle, WeaponsTab.LoadoutOf(colonist), "colonist's loadout"); T.Equal(0, CasteManager.Overrides(colonist).Count, "colonist's overrides right after Assign"); Pawn prisoner = Fixtures.Prisoner(); T.Check(!CasteUtility.CanUse(prisoner, CasteSlot.Loadout), "CanUse(Loadout) is true for a prisoner"); CasteManager.Assign(prisoner, c, true, JoinReason.NewPrisoner); T.Check(!CasteUtility.Apply(prisoner, c).Contains(CasteSlot.Loadout), "Apply wrote a loadout on a prisoner"); Pawn slave = Fixtures.ColonySlave(); // skips here without Ideology; the colonist part passed T.Check(CasteManager.Assign(slave, c, true, JoinReason.NewSlave), "Assign failed for the slave"); T.Equal((int?)rifle, WeaponsTab.LoadoutOf(slave), "slave's loadout"); } [BlaxTest("loadout: Follow rule: A follows a loadout change, B with a loadout set by hand keeps it")] public static void LoadoutFollowRule() { NeedWeaponsTab(); int first = MakeLoadout("Blax test first"); int other = MakeLoadout("Blax test other"); int? fallback = WeaponsTab.DefaultLoadoutId(); T.NotNull(fallback, "default loadout id"); Caste c = Fixtures.NewCaste("Loadout follow"); CasteManager.SetSlot(c, CasteSlot.Loadout, 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(WeaponsTab.SetLoadout(b, other), "setting B's loadout by hand failed"); List overrides = CasteManager.Overrides(b); T.Check(overrides.Count == 1 && overrides[0] == CasteSlot.Loadout, "B's overrides: " + string.Join(", ", overrides)); T.Equal(1, CasteManager.SetSlot(c, CasteSlot.Loadout, fallback.Value), "members moved by the loadout change"); T.Equal(fallback, WeaponsTab.LoadoutOf(a), "A's loadout"); T.Equal((int?)other, WeaponsTab.LoadoutOf(b), "B's hand-set loadout"); T.Equal(0, CasteManager.SetSlot(c, CasteSlot.Loadout, Caste.Unmanaged), "members moved by leaving the loadout alone"); T.Equal(fallback, WeaponsTab.LoadoutOf(a), "A's loadout after the caste let go of it"); } [BlaxTest("loadout: a deleted loadout becomes unmanaged (on apply and in the load check), without errors")] public static void LoadoutDeleted() { NeedWeaponsTab(); T.AllowWarning("no longer in the game"); // the deleted loadouts, one warning per caste int doomed = MakeLoadout("Blax test doomed"); Caste c = Fixtures.NewCaste("Doomed loadout"); CasteManager.SetSlot(c, CasteSlot.Loadout, doomed); int doomed2 = MakeLoadout("Blax test doomed too"); Caste c2 = Fixtures.NewCaste("Doomed loadout too"); CasteManager.SetSlot(c2, CasteSlot.Loadout, doomed2); T.Check(c.IsManaged(CasteSlot.Loadout) && c2.IsManaged(CasteSlot.Loadout), "setup: loadouts not managed"); AcceptanceReport deleted = DeleteLoadout(doomed); T.Check(deleted.Accepted, "Weapons Tab Reborn refused to delete the loadout: " + deleted.Reason); T.Check(!WeaponsTab.Exists(doomed), "the deleted loadout still exists"); T.Check(!WeaponsTab.Loadouts().Any(l => l.Key == doomed), "the deleted loadout is still in the manager's list"); T.Check(CasteUtility.ValueLabel(CasteSlot.Loadout, doomed).Contains(doomed.ToString()), "label of a missing loadout: " + CasteUtility.ValueLabel(CasteSlot.Loadout, doomed)); Pawn p = Fixtures.Colonist(); int? before = WeaponsTab.LoadoutOf(p); T.Check(CasteManager.Assign(p, c, false, JoinReason.Manual), "Assign failed"); T.Equal(before, WeaponsTab.LoadoutOf(p), "the pawn's loadout after assigning a caste with a deleted loadout"); T.Check(!c.IsManaged(CasteSlot.Loadout), "the deleted loadout is still managed after Apply"); T.Check(DeleteLoadout(doomed2).Accepted, "Weapons Tab Reborn refused to delete the second loadout"); CheckSlots(); T.Check(!c2.IsManaged(CasteSlot.Loadout), "the deleted loadout is still managed after the load check"); } private static FieldInfo LoadoutDatabaseField() { Type dictionary = AccessTools.TypeByName(WeaponsTab.ApiTypeName); T.NotNull(dictionary, WeaponsTab.ApiTypeName); FieldInfo field = AccessTools.Field(dictionary, "CurrentLoadoutDatabase"); T.NotNull(field, "LoadoutDictionary.CurrentLoadoutDatabase"); return field; } [BlaxTest("loadout: without Weapons Tab Reborn's loadout database (a save made without the mod) the database is created; nothing turns off or is unmanaged")] public static void LoadoutDatabaseMissing() { NeedWeaponsTab(); T.AllowWarning("Weapons Tab Reborn"); // a failed or missing loadout API turns the slot off with a warning FieldInfo field = LoadoutDatabaseField(); object saved = field.GetValue(null); int? first = WeaponsTab.DefaultLoadoutId(); T.NotNull(first, "the default loadout id"); Caste c = Fixtures.NewCaste("Loadout before the database"); CasteManager.SetSlot(c, CasteSlot.Loadout, first.Value); T.Equal(first.Value, c.LoadoutId, "setup: the caste's loadout"); try { // After loading a save without the mod's data its database is null until its tab or job giver runs. field.SetValue(null, null); CheckSlots(); T.Check(WeaponsTab.Available, "the load check with no database turned the loadout slot off"); T.Check(c.IsManaged(CasteSlot.Loadout) && c.LoadoutId == first.Value, "the load check with no database unmanaged the caste's loadout (" + c.LoadoutId + ")"); T.NotNull(field.GetValue(null), "the loadout database was not created"); field.SetValue(null, null); T.NotNull(WeaponsTab.DefaultLoadoutId(), "the default loadout with no database (ticking the managed box)"); field.SetValue(null, null); T.Check(WeaponsTab.Loadouts().Count > 0, "the loadout list with no database (the manager's dropdown) is empty"); T.Check(WeaponsTab.Available, "the loadout slot turned off"); } finally { field.SetValue(null, saved); } } [BlaxTest("loadout: a Weapons Tab Reborn call that fails turns the slot off and never counts as a deleted loadout")] public static void LoadoutCallFailureIsNotMissing() { NeedWeaponsTab(); T.AllowWarning("Weapons Tab Reborn"); // a failed or missing loadout API turns the slot off with a warning FieldInfo field = LoadoutDatabaseField(); FieldInfo ensureField = AccessTools.Field(typeof(WeaponsTab), "ensureDatabase"); T.NotNull(ensureField, "WeaponsTab.ensureDatabase"); object saved = field.GetValue(null); object ensure = ensureField.GetValue(null); int? first = WeaponsTab.DefaultLoadoutId(); T.NotNull(first, "the default loadout id"); Caste c = Fixtures.NewCaste("Loadout through a failed call"); CasteManager.SetSlot(c, CasteSlot.Loadout, first.Value); try { // No database and no way to create it: the API call throws inside the existence check. field.SetValue(null, null); ensureField.SetValue(null, null); CheckSlots(); T.Check(!WeaponsTab.Available, "setup: the failed call did not turn the slot off"); T.Check(c.IsManaged(CasteSlot.Loadout) && c.LoadoutId == first.Value, "a failed call counted as a deleted loadout (" + c.LoadoutId + ")"); } finally { ensureField.SetValue(null, ensure); field.SetValue(null, saved); AccessTools.Method(typeof(WeaponsTab), "ResetForGame").Invoke(null, null); } T.Check(WeaponsTab.Available, "the loadout slot did not come back after ResetForGame"); } // ---- every slot -------------------------------------------------------------------------- [BlaxTest("slots: duplicating a caste copies the schedule, work priorities, Start from nothing and loadout as separate copies")] public static void DuplicateCopiesNewSlots() { Caste src = Fixtures.NewCaste("New slot source"); CasteManager.SetSlot(src, CasteSlot.Schedule, NightSchedule()); CasteManager.SetWorkPriority(src, Growing.defName, 1); CasteManager.SetWorkStartFromNothing(src, true); if (WeaponsTab.Available) CasteManager.SetSlot(src, CasteSlot.Loadout, WeaponsTab.DefaultLoadoutId() ?? Caste.Unmanaged); Caste dup = CasteManager.DuplicateCaste(src); Fixtures.Track(dup); foreach (CasteSlot slot in CasteUtility.AllSlots) T.Check(CasteUtility.SameValue(src.GetSlot(slot), dup.GetSlot(slot)), $"{slot}: source {CasteUtility.ValueLabel(slot, src.GetSlot(slot))}, duplicate {CasteUtility.ValueLabel(slot, dup.GetSlot(slot))}"); T.Check(dup.WorkStartFromNothing, "Start from nothing not copied"); Dialog_ManageCastes.PaintHour(dup, 0, TimeAssignmentDefOf.Sleep); CasteManager.SetWorkPriority(dup, Growing.defName, 4); T.Equal(TimeAssignmentDefOf.Work.defName, src.Schedule[0], "painting the duplicate changed the source's schedule"); T.Equal(1, src.WorkPriorityOf(Growing.defName) ?? -1, "changing the duplicate's work changed the source's"); } [BlaxTest("slots: the starter castes leave the schedule, work priorities and loadout alone")] public static void StarterCastesLeaveNewSlotsAlone() { var starters = Fixtures.Comp.Castes.Where(c => c.label == StarterCastes.ColonistLabel || c.label == StarterCastes.SlaveLabel || c.label == StarterCastes.PrisonerLabel).ToList(); if (starters.Count == 0) T.Skip("this game has no starter castes (seedStarterCastes off?)"); foreach (Caste c in starters) { T.Check(!c.IsManaged(CasteSlot.Schedule) && !c.IsManaged(CasteSlot.Work) && !c.IsManaged(CasteSlot.Loadout) && !c.WorkStartFromNothing, c.label + " manages " + string.Join(", ", new[] { CasteSlot.Schedule, CasteSlot.Work, CasteSlot.Loadout }.Where(c.IsManaged))); } } [BlaxTest("slots: every string the new slots use is in Keyed, and the slots have labels")] public static void Strings() { string[] keys = { "BlaxCastes_SlotSchedule", "BlaxCastes_SlotWork", "BlaxCastes_SlotLoadout", "BlaxCastes_LoadoutUnknown", "BlaxCastes_WorkOff", "BlaxCastes_WorkLeaveAloneShort", "BlaxCastes_WorkManagerActive", "BlaxCastes_LoadoutTip", "BlaxCastes_LoadoutUnavailable", "BlaxCastes_LoadoutSettingOff", "BlaxCastes_ScheduleTip", "BlaxCastes_ScheduleUnmanagedTip", "BlaxCastes_ScheduleCopyFromPawn", "BlaxCastes_ScheduleCopyFromPawnTip", "BlaxCastes_ScheduleLeaveAloneTip", "BlaxCastes_ScheduleNoPawns", "BlaxCastes_WorkStartFromNothing", "BlaxCastes_WorkStartFromNothingTip", "BlaxCastes_WorkTypeTip", "BlaxCastes_WorkSettingOff", "BlaxCastes_WorkManualOff", "BlaxCastes_CellOverrideSchedule", "BlaxCastes_CellOverrideWork", "BlaxCastes_CellOverrideWorkType", }; var missing = keys.Where(k => !k.CanTranslate()).ToList(); T.Check(missing.Count == 0, "strings missing from Keyed: " + string.Join(", ", missing)); } [BlaxTest("schedule: Copy from pawn lists colonists and slaves with a schedule and copies all 24 hours")] public static void ScheduleCopyFromPawn() { Pawn source = Fixtures.Colonist(); List night = NightSchedule(); for (int h = 0; h < ScheduleSlot.Hours; h++) source.timetable.SetAssignment(h, ScheduleSlot.Resolve(night[h])); Pawn prisoner = Fixtures.Prisoner(); List listed = Dialog_ManageCastes.SchedulePawns(); T.Check(listed.Contains(source), "Copy from pawn does not list a colonist"); T.Check(!listed.Contains(prisoner), "Copy from pawn lists a prisoner"); Caste c = Fixtures.NewCaste("Copied schedule"); Pawn member = Fixtures.Colonist(); CasteManager.Assign(member, c, false, JoinReason.Manual); T.Equal(1, Dialog_ManageCastes.CopyScheduleFrom(c, source), "members moved by Copy from pawn"); T.Check(c.Schedule.SequenceEqual(night), "the caste's schedule after Copy from pawn: " + ScheduleSlot.Summary(c.Schedule)); CheckSchedule(member, night, "member"); } [BlaxTest("schedule: the manager's brushes share the row, wide enough for Recreation (about 68 px in the Small font plus the 4 px border)")] public static void ScheduleBrushWidth() { // The manager's scroll view is about 598 px wide at its starting size. float width = Dialog_ManageCastes.BrushWidthFor(598f); T.Check(width >= 88f && width <= 96f, "brush width in the manager: " + width); T.Equal(64f, Dialog_ManageCastes.BrushWidthFor(200f), "brush width in a very narrow window"); } // ---- the Caste column in the Schedule and Work tabs -------------------------------------- private static string ColumnList(PawnTableDef table) => string.Join(", ", table.columns.Select(c => c?.defName ?? "null")); /// /// Right after the timetable: the Schedule tab draws its time assignment selector over the top of the /// header from x 0 to about 476 px, which covers every column left of the timetable (name 80 px or /// more, ideo, xenotype, copy and paste, then the timetable of 360 px or more). /// private static void CheckScheduleColumn(PawnColumnDef col) { PawnTableDef table = CasteColumns.ScheduleTable; int at = table.columns.IndexOf(col); int timetable = table.columns.FindIndex(c => c?.defName == "Timetable"); T.Check(at >= 0, "Caste column not in the Schedule table: " + ColumnList(table)); T.Check(timetable >= 0, "the Schedule table has no timetable: " + ColumnList(table)); T.Equal(timetable + 1, at, "Caste column index, right after the timetable (columns: " + ColumnList(table) + ")"); T.Equal(1, table.columns.Count(c => c == col), "times the Caste column is in the Schedule table"); } private static void CheckWorkColumn(PawnColumnDef col) { PawnTableDef table = CasteColumns.WorkTable; int at = table.columns.IndexOf(col); int firstWork = table.columns.FindIndex(c => c?.workType != null); T.Check(at >= 0, "Caste column not in the Work table: " + ColumnList(table)); T.Check(firstWork > at, "Caste column not left of the work columns: " + ColumnList(table)); int copyPaste = table.columns.FindIndex(c => c?.defName == "CopyPasteWorkPriorities"); if (copyPaste >= 0) T.Equal(at + 1, copyPaste, "copy and paste column index (columns: " + ColumnList(table) + ")"); T.Equal(1, table.columns.Count(c => c == col), "times the Caste column is in the Work table"); } [BlaxTest("ui: in the Work tab the Caste column keeps the header at the work columns' height; elsewhere it asks for its 65 px")] public static void WorkHeaderHeight() { PawnColumnDef col = CasteColumns.Column; T.NotNull(col, "PawnColumnDef " + PawnColumnWorker_BlaxCaste.DefName); var work = new PawnTable_PlayerPawns(PawnTableDefOf.Work, () => Enumerable.Empty(), 1200, 800); var assign = new PawnTable_PlayerPawns(PawnTableDefOf.Assign, () => Enumerable.Empty(), 1200, 800); var schedule = new PawnTable_PlayerPawnsTimetable(PawnTableDefOf.Restrict, () => Enumerable.Empty(), 1200, 800); T.Check(PawnColumnWorker_BlaxCaste.IsWorkTable(work), "the Work tab's table is not recognised"); T.Check(!PawnColumnWorker_BlaxCaste.IsWorkTable(assign) && !PawnColumnWorker_BlaxCaste.IsWorkTable(schedule), "another table counts as the Work tab's"); PawnColumnDef workColumn = PawnTableDefOf.Work.columns.FirstOrDefault(c => c?.workType != null); T.NotNull(workColumn, "a work priority column in the Work table"); int workHeight = workColumn.Worker.GetMinHeaderHeight(work); int mine = col.Worker.GetMinHeaderHeight(work); T.Check(mine <= workHeight, "the Caste column asks for " + mine + " px in the Work tab, the work columns for " + workHeight); T.Equal(PawnColumnWorker_BlaxCaste.TopAreaHeight, col.Worker.GetMinHeaderHeight(assign), "header height in the Assign tab"); T.Equal(PawnColumnWorker_BlaxCaste.TopAreaHeight, col.Worker.GetMinHeaderHeight(schedule), "header height in the Schedule tab"); } [BlaxTest("ui: at startup the Caste column sits right after the timetable in the Schedule table and left of the work columns in the Work table")] public static void ScheduleAndWorkColumns() { if (!Fixtures.Settings.showAssignColumn) T.Skip("showAssignColumn is off in this run"); PawnColumnDef col = CasteColumns.Column; T.NotNull(col, "PawnColumnDef " + PawnColumnWorker_BlaxCaste.DefName); CheckScheduleColumn(col); CheckWorkColumn(col); T.Info("Schedule: " + ColumnList(CasteColumns.ScheduleTable)); } [BlaxTest("ui: showAssignColumn off removes the column from all three tables; on puts each back in place")] public static void ColumnsToggle() { PawnColumnDef col = CasteColumns.Column; T.NotNull(col, "PawnColumnDef " + PawnColumnWorker_BlaxCaste.DefName); var had = CasteColumns.Tables.ToDictionary(t => t, t => t.columns.Contains(col)); try { CasteColumns.ApplyAll(false); T.Check(CasteColumns.Tables.All(t => !t.columns.Contains(col)), "ApplyAll(false) left the column in a table"); T.Check(CasteColumns.ApplyAll(true), "ApplyAll(true) changed nothing"); CheckScheduleColumn(col); CheckWorkColumn(col); T.Check(!CasteColumns.ApplyAll(true), "adding present columns reported a change"); } finally { foreach (KeyValuePair kv in had) { if (!kv.Value) kv.Key.columns.Remove(col); } } } // ---- save and reload --------------------------------------------------------------------- // Runs last among this class's phase-1 tests (source order). [BlaxTest("slots: set up a caste with a schedule, work priorities and a loadout for the reload")] public static void ReloadSetup() { Caste c = Fixtures.KeepCaste(Fixtures.NewCaste("Reload slots")); CasteManager.SetSlot(c, CasteSlot.Schedule, NightSchedule()); Pawn member = null; WithManualPriorities(true, () => { member = Fixtures.Keep(PawnThatCan(Fixtures.Colonist, Growing, Firefighter)); CasteManager.SetWorkPriority(c, Growing.defName, 1); CasteManager.SetWorkPriority(c, Firefighter.defName, 1); CasteManager.SetWorkStartFromNothing(c, true); if (WeaponsTab.Available) { int loadout = MakeLoadout("Blax test reload"); CasteManager.SetSlot(c, CasteSlot.Loadout, loadout); T.Carry["slotsLoadout"] = loadout.ToString(); } T.Check(CasteManager.Assign(member, c, false, JoinReason.Manual), "Assign failed"); }); T.Equal(0, CasteManager.Overrides(member).Count, "member's overrides before the save"); T.Carry["slotsCaste"] = c.id.ToString(); T.Carry["slotsMember"] = member.ThingID; } [BlaxTest("slots: the schedule, work priorities, Start from nothing and loadout survive save and reload, on the caste and the member", phase: 2)] public static void SlotsSurvive() { T.Check(T.Carry.TryGetValue("slotsCaste", out string id), "phase 1 did not set up the caste"); Caste c = Fixtures.Comp.Castes.FirstOrDefault(x => x.id.ToString() == id); T.NotNull(c, "caste " + id + " after reload"); List night = NightSchedule(); T.Check(c.Schedule != null && c.Schedule.SequenceEqual(night), "schedule after reload: " + ScheduleSlot.Summary(c.Schedule)); T.Equal(2, c.WorkPriorities.Count, "work types after reload (" + WorkSlot.Summary(c.WorkPriorities) + ")"); T.Equal(1, c.WorkPriorityOf(Growing.defName) ?? -1, "Growing after reload"); T.Equal(1, c.WorkPriorityOf(Firefighter.defName) ?? -1, "Firefighting after reload"); T.Check(c.WorkStartFromNothing, "Start from nothing lost over the reload"); Pawn member = Fixtures.FindPawnById(T.Carry["slotsMember"]); T.NotNull(member, "member after reload"); T.Check(Fixtures.Comp.CasteOf(member) == c, "member's caste after reload: " + Fixtures.Comp.CasteOf(member)); CheckSchedule(member, night, "member"); if (CasteUtility.CanUse(member, CasteSlot.Work)) T.Check(WorkSlot.MatchesAll(member, c.WorkPriorities), "member's work after reload: " + WorkSlot.Summary(WorkSlot.Of(member, activeOnly: true))); if (T.Carry.TryGetValue("slotsLoadout", out string loadout)) { T.Equal(loadout, c.LoadoutId.ToString(), "loadout after reload"); T.Equal(loadout, WeaponsTab.LoadoutOf(member)?.ToString(), "member's loadout after reload"); } else { T.Check(!c.IsManaged(CasteSlot.Loadout), "a loadout appeared without Weapons Tab Reborn"); } T.Equal(0, CasteManager.Overrides(member).Count, "member's overrides after reload"); } } }