using System.Collections.Generic; using System.Linq; using RimWorld; using Verse; namespace BlaxCastes { /// /// The ready-made Colonist, Slave and Prisoner castes, wired to the join rows. /// - Colonist: the database's current default apparel, food, drug and reading policy (which already /// carry the ideo drug rule and any earlier "Make default"). Medical care unmanaged. /// - Slave (Ideology only): the Slave outfit, Simple food, No drugs. Reading stays unmanaged, because /// slave children need books to learn; medical care stays unmanaged, so the game's default care for /// slaves stays in charge. Seeded without Ideology, a save gets it on its first load with Ideology. /// - Prisoner: Simple food. /// Policies are found by their translated starting labels, No drugs by its source def. A miss leaves /// the slot unmanaged; Castes never creates vanilla policies itself, with one exception: the Slave /// caste a save gets on its first load with Ideology also gets the Slave outfit when the save has none /// (AddSlaveCasteIfOwed). /// Rows: bought as colonist and new colonist use Default caste (Colonist); bought as slave and new /// slave use Slave; new prisoner uses Prisoner. /// public static class StarterCastes { public const string NoDrugsSourceDef = "NoDrugs"; private static bool SettingOn => BlaxCastesMod.Settings == null || BlaxCastesMod.Settings.seedStarterCastes; public static string ColonistLabel => "BlaxCastes_StarterColonist".Translate(); public static string SlaveLabel => "BlaxCastes_StarterSlave".Translate(); public static string PrisonerLabel => "BlaxCastes_StarterPrisoner".Translate(); // ---- policy lookups ------------------------------------------------------------------ public static ApparelPolicy SlaveOutfit() => Current.Game?.outfitDatabase?.AllOutfits?.FirstOrDefault(o => o != null && o.label == (string)"OutfitSlave".Translate()); public static FoodPolicy SimpleFood() => Current.Game?.foodRestrictionDatabase?.AllFoodRestrictions?.FirstOrDefault(f => f != null && f.label == (string)"FoodRestrictionSimple".Translate()); public static DrugPolicy NoDrugs() => Current.Game?.drugPolicyDatabase?.AllPolicies?.FirstOrDefault(d => d?.sourceDef != null && d.sourceDef.defName == NoDrugsSourceDef); /// True for the game's Slave outfit (by its starting label), which has no cold protection. public static bool IsSlaveOutfit(Policy policy) => policy is ApparelPolicy a && a.label == (string)"OutfitSlave".Translate(); /// Ideology is active and the game has no Slave outfit (renamed or deleted): the manager offers to create one. public static bool CanCreateSlaveOutfit => ModsConfig.IdeologyActive && Current.Game?.outfitDatabase != null && SlaveOutfit() == null; /// /// The manager's "Create Slave outfit" button: a new apparel policy named like the game's starting /// Slave outfit that allows the apparel the game tags for slaves, the way a new game sets it up. /// On the player's click, and for the Slave caste a save is owed (AddSlaveCasteIfOwed); other seeding /// never creates policies. Returns the existing outfit if there is one, or null without Ideology. /// public static ApparelPolicy CreateSlaveOutfit() { if (!ModsConfig.IdeologyActive) return null; ApparelPolicy existing = SlaveOutfit(); OutfitDatabase db = Current.Game?.outfitDatabase; if (existing != null || db == null) return existing; ApparelPolicy policy = db.MakeNewOutfit(); policy.label = "OutfitSlave".Translate(); policy.filter.SetDisallowAll(); if (SpecialThingFilterDefOf.AllowDeadmansApparel != null) policy.filter.SetAllow(SpecialThingFilterDefOf.AllowDeadmansApparel, allow: false); foreach (ThingDef def in DefDatabase.AllDefs) { if (def.apparel?.defaultOutfitTags != null && def.apparel.defaultOutfitTags.Contains("Slave")) policy.filter.SetAllow(def, allow: true); } return policy; } // ---- seeding ------------------------------------------------------------------------- /// /// A new game: adds the starter castes and wires the rows, unless the setting is off or the /// component already has castes. Returns true if it seeded. /// public static bool SeedNewGame(GameComponent_BlaxCastes comp) { if (comp == null || !SettingOn || comp.StarterCastesSeeded) return false; if (comp.Castes.Count > 0) { comp.MarkStarterCastesSeeded(); return false; } Seed(comp); return true; } /// /// A loaded save: adds the starter castes once per save, only if the save has no castes. A save /// that already has castes is marked, so deleting them later does not bring the starters back. /// Nobody is moved. Returns true if it seeded. /// public static bool SeedOnLoad(GameComponent_BlaxCastes comp) => SeedNewGame(comp); /// Creates the castes in the component and wires the rows. Writes the component directly, so it works on any component. internal static void Seed(GameComponent_BlaxCastes comp) { Game game = Current.Game; Caste colonist = NewCaste(comp, ColonistLabel); SetIfFound(colonist, CasteSlot.Apparel, game?.outfitDatabase?.DefaultOutfit()); SetIfFound(colonist, CasteSlot.Food, game?.foodRestrictionDatabase?.DefaultFoodRestriction()); SetIfFound(colonist, CasteSlot.Drug, game?.drugPolicyDatabase?.DefaultDrugPolicy()); SetIfFound(colonist, CasteSlot.Reading, game?.readingPolicyDatabase?.DefaultReadingPolicy()); Caste slave = ModsConfig.IdeologyActive ? NewSlaveCaste(comp) : null; Caste prisoner = NewCaste(comp, PrisonerLabel); SetIfFound(prisoner, CasteSlot.Food, SimpleFood()); comp.SetDefaultRaw(colonist); comp.SetRowRaw(JoinReason.BoughtColonist, JoinRowMode.DefaultCaste, null); comp.SetRowRaw(JoinReason.NewColonist, JoinRowMode.DefaultCaste, null); if (slave != null) { comp.SetRowRaw(JoinReason.BoughtSlave, JoinRowMode.SpecificCaste, slave); comp.SetRowRaw(JoinReason.NewSlave, JoinRowMode.SpecificCaste, slave); } comp.SetRowRaw(JoinReason.NewPrisoner, JoinRowMode.SpecificCaste, prisoner); comp.MarkStarterCastesSeeded(); // Without Ideology the Slave caste is owed: the first load with Ideology adds it (AddSlaveCasteIfOwed). comp.SetSlaveCasteOwed(slave == null); List missing = new List(); if (slave != null) missing.AddRange(MissingSlaveSlots(slave)); if (!prisoner.IsManaged(CasteSlot.Food)) missing.Add(PrisonerLabel + ": " + CasteUtility.SlotLabel(CasteSlot.Food)); if (missing.Count > 0) Log.Message("[Blax Castes] Starter castes added; policies not found (renamed or removed), left unmanaged: " + string.Join(", ", missing)); } /// The Slave caste: the Slave outfit, Simple food, No drugs, each where the game has it. private static Caste NewSlaveCaste(GameComponent_BlaxCastes comp) { Caste slave = NewCaste(comp, SlaveLabel); SetIfFound(slave, CasteSlot.Apparel, SlaveOutfit()); SetIfFound(slave, CasteSlot.Food, SimpleFood()); SetIfFound(slave, CasteSlot.Drug, NoDrugs()); return slave; } private static IEnumerable MissingSlaveSlots(Caste slave) { foreach (CasteSlot slot in new[] { CasteSlot.Apparel, CasteSlot.Food, CasteSlot.Drug }) { if (!slave.IsManaged(slot)) yield return SlaveLabel + ": " + CasteUtility.SlotLabel(slot); } } /// /// A save whose starter castes were added without Ideology, loaded with Ideology: adds the Slave caste /// once, like a new game would have it, and picks it in the bought-as-slave and new-slave rows that are /// still at No caste. The game makes its Slave outfit only in a new game with Ideology /// (OutfitDatabase.GenerateStartingOutfits) and loads the saved outfit list over it, so such a save /// usually has none: the new caste then gets one made like the game's (CreateSlaveOutfit), the one /// policy Castes creates without a click. A caste already named Slave is used as it is instead of a /// second one, and when both slave rows already pick a caste nothing is added. Nobody moves. Does /// nothing while seedStarterCastes is off (the caste stays owed). Returns the caste the rows now pick, or null. /// public static Caste AddSlaveCasteIfOwed(GameComponent_BlaxCastes comp) { if (comp == null || !comp.SlaveCasteOwed || !ModsConfig.IdeologyActive || !SettingOn) return null; comp.SetSlaveCasteOwed(false); List openRows = new[] { JoinReason.BoughtSlave, JoinReason.NewSlave } .Where(r => comp.GetRow(r).mode == JoinRowMode.NoCaste).ToList(); if (openRows.Count == 0) return null; Caste slave = comp.Castes.FirstOrDefault(c => c != null && c.label == SlaveLabel); if (slave == null) { if (SlaveOutfit() == null) CreateSlaveOutfit(); slave = NewSlaveCaste(comp); List missing = MissingSlaveSlots(slave).ToList(); if (missing.Count > 0) Log.Message("[Blax Castes] Slave caste added; policies not found (renamed or removed), left unmanaged: " + string.Join(", ", missing)); } foreach (string row in openRows) comp.SetRowRaw(row, JoinRowMode.SpecificCaste, slave); return slave; } /// The one message for a save that just got the Slave caste (AddSlaveCasteIfOwed). public static void ShowSlaveCasteAddedMessage(Caste slave) { string text = "BlaxCastes_StarterSlaveAddedMessage".Translate(slave.label.Named("CASTE")); LongEventHandler.ExecuteWhenFinished(() => Messages.Message(text, MessageTypeDefOf.NeutralEvent)); } private static Caste NewCaste(GameComponent_BlaxCastes comp, string label) { var caste = new Caste(comp.TakeNextId(), label); comp.AddCaste(caste); return caste; } /// No members exist yet, so the raw write needs no member sync. private static void SetIfFound(Caste caste, CasteSlot slot, Policy policy) { if (policy != null && CasteUtility.PolicyExists(policy)) caste.SetSlotRaw(slot, policy); } /// The one message for a save that just got the starter castes. public static void ShowSeededMessage(GameComponent_BlaxCastes comp) { string text = SeededMessageText(comp); LongEventHandler.ExecuteWhenFinished(() => Messages.Message(text, MessageTypeDefOf.NeutralEvent)); } /// /// The seeding message. It points to Assign > Manage castes, or, while the Caste column is not in the /// Assign tab (so that button is not there), to the mod settings, which open the same window. The /// column is there as the game started (CasteColumns.Shown), whatever the setting says until a restart. /// public static string SeededMessageText(GameComponent_BlaxCastes comp) { string names = comp.Castes.Select(c => c.label).ToCommaList(useAnd: true); string key = CasteColumns.Shown ? "BlaxCastes_StarterSeededMessage" : "BlaxCastes_StarterSeededMessageNoColumn"; return key.Translate(names.Named("CASTES")); } } }