using System; using System.Collections.Generic; using RimWorld; using Verse; namespace BlaxLaws { /// /// The rules the category workers apply to the loaded defs (design: "Act catalogue and detection", How), /// and the built-in include and exclude lists. The lists of an ActCategoryDef in XML add to the built-in /// ones, so another mod can name its own states or events with no code. /// /// Checked against the 1.7 defs and decompiled code: /// - Every MentalBreakDef names a colonist state (Core, Royalty, Ideology and Biotech /// Defs/MentalStateDefs); animal, baby and mechanoid states come only from code (MentalStateDefOf.Manhunter, /// Roaming, CocoonDisturbed, BerserkMechanoid; Biotech's baby fits). The exclude list guards against a /// modded break naming one of them. Catatonic and RunWild name no state (MentalStates_Mood.xml), so no /// hook ever sees them and they stay out. /// - Time records never pass Increment or AddTo: they are added straight to the record map every 80 ticks /// (RimWorld/Pawn_RecordsTracker.cs:60-70). /// - Corpse defs carry an ingestible even for mechanoids (RimWorld/ThingDefGenerator_Corpses.cs:165-169), and /// plants carry one for grazing animals; neither is anything a colonist eats. /// public static class CatalogueRules { /// /// Break states colonists reach without a MentalBreakDef: berserk (pulsers, scaria, a refused arrest), the /// trance berserk a pawn casts on itself, the warcall berserk, and states from hediffs. Hook A skips a state /// another pawn's ability forced on the pawn (a psycast, Neuroquake), so those never file. A name that /// isn't loaded (its DLC is off) is skipped. /// public static readonly string[] BreakInclude = { "Berserk", "BerserkPermanent", "BerserkWarcall", "BerserkTrance", "WanderConfused", "Wander_Psychotic_Short", "CubeSculpting", "Terror", }; /// /// Animal, baby and mechanoid states, the panic flight of enemies (Verse.AI/MentalStateHandler.cs:207-214, /// only for pawns outside the colony) and the social fight, which is the special act StartedSocialFight. /// public static readonly string[] BreakExclude = { "Manhunter", "ManhunterPermanent", "ManhunterBloodRain", "Roaming", "CocoonDisturbed", "BerserkMechanoid", "Crying", "Giggling", "SocialFighting", "PanicFlee", }; /// /// Faction-level history events: goodwill, trade, quests, raids, charity and pollution reach a faction, /// not a colonist (Core Defs/Goodwill, Ideology Precepts_Charity, Precepts_Raiders, Precepts_Relics, /// Biotech HistoryEventDefs.xml). /// public static readonly string[] RecordedActExclude = { // Core Defs/Goodwill/GoodwillEvents_Diplomatic.xml "GaveGift", "PeaceTalksBackfire", "PeaceTalksDisaster", "PeaceTalksSuccess", "PeaceTalksTriumph", "RequestedMilitaryAid", "RequestedOrbitalTrader", "RequestedTrader", "Traded", // GoodwillEvents_Misc.xml "AttackedBuilding", "DebugGoodwill", "LaborersMissedShuttle", "ReachNaturalGoodwill", "RitualDone", "ShuttleDestroyed", "UsedForbiddenThing", "UsedHarmfulAbility", // GoodwillEvents_Pawns.xml "AttackedMember", "FriendlyExitedMapTended", "GuiltyPrisonerDied", "InnocentPrisonerDied", "MemberCaptured", "MemberCrushed", "MemberExitedMapHealthy", "MemberKilled", "MemberNeutrallyDied", "MemberSold", "MemberStripped", "PerformedHarmfulSurgery", "PrisonerDied", "UsedHarmfulItem", "XenogermAbsorbed", // GoodwillEvents_Quests.xml "AcceptedDeserter", "FailedToKeepLodgersHappy", "MemberMissedShuttle", "MonumentConstructionExpired", "MonumentConstructionMapRemoved", "MonumentDestroyed", "QuestGoodwillReward", "QuestPawnArrested", "QuestPawnLost", "QuestPrisonerEnslaved", "QuestPrisonerRecruited", "ShuttleCommanderMissedShuttle", "ShuttleGuardsMissedShuttle", // GoodwillEvents_World.xml "AttackedCaravan", "AttackedSettlement", "DestroyedEnemyBase", "SettlementProximity", // Core HistoryEventDefs.xml and Ideology Precepts_Charity.xml "CharityRefused_ThreatReward_Joiner", "CharityFulfilled_Beggars", "CharityFulfilled_HospitalityRefugees", "CharityFulfilled_IntroWimp", "CharityFulfilled_RefugeePodCrash", "CharityFulfilled_ShuttleCrashRescue", "CharityFulfilled_ThreatReward_Joiner", "CharityFulfilled_WandererJoins", "CharityRefused_Beggars", "CharityRefused_Beggars_Betrayed", "CharityRefused_HospitalityRefugees", "CharityRefused_IntroWimp", "CharityRefused_Pilgrims", "CharityRefused_Pilgrims_Betrayed", "CharityRefused_RefugeePodCrash", "CharityRefused_ShuttleCrashRescue", "CharityRefused_WandererJoins", // Ideology Precepts_Raiders.xml, Precepts_Proselytizing.xml, Precepts_Ranching.xml, Precepts_Relics.xml "PlayerRaidedSomeone", "Raided", "Raided_BanditCamp", "Raided_WorkSite", "RelicHuntSuccess", "ReliquaryPilgrimsSuccess", "HospitalitySuccess_Animals", "RelicDestroyed", "RelicLost", // Biotech HistoryEventDefs.xml "PollutedNearbySite", "PollutedBase", "ToxicWasteDumping", }; private static readonly HashSet breakExclude = new HashSet(BreakExclude); private static readonly HashSet recordedActExclude = new HashSet(RecordedActExclude); /// A def name the built-in list or the category's XML list leaves out. public static bool Excluded(ActCategoryDef cat, string defName) { if (defName == null) return true; if (cat != null && cat.Excludes(defName)) return true; switch (cat?.defName) { case ActCategories.Break: return breakExclude.Contains(defName); case ActCategories.RecordedAct: return recordedActExclude.Contains(defName); default: return false; } } // ---- what each category holds --------------------------------------------------------- /// /// Ate: anything with an ingestible a colonist can eat or drink. Plants (grazing) and the corpses of /// races without flesh (mechanoids) are left out. /// public static bool IsEdible(ThingDef def) { if (def?.ingestible == null) return false; if (def.category == ThingCategory.Plant) return false; if (def.IsCorpse && def.ingestible.sourceDef?.race != null && !def.ingestible.sourceDef.race.IsFlesh) return false; return true; } /// Used: a ThingDef whose comps hold a CompProperties_Usable (subclasses included). public static bool IsUsable(ThingDef def) { if (def?.comps == null) return false; for (int i = 0; i < def.comps.Count; i++) { if (def.comps[i] is CompProperties_Usable) return true; } return false; } /// Records: Int and Float records count acts; Time records count time. public static bool CountsActs(RecordDef def) => def != null && (def.type == RecordType.Int || def.type == RecordType.Float); // ---- building entries ---------------------------------------------------------------- /// /// Single-def entries for the defs the rule found plus the include names, minus the exclude names, /// each def once, with labels told apart (see Disambiguate). /// public static List EntriesFor(ActCategoryDef cat, IEnumerable byRule, IEnumerable builtInInclude = null) where T : Def { var defs = new List(); var seen = new HashSet(); void Take(T d) { if (d != null && !Excluded(cat, d.defName) && seen.Add(d)) defs.Add(d); } if (byRule != null) { foreach (T d in byRule) Take(d); } if (builtInInclude != null) { foreach (string name in builtInInclude) Take(DefDatabase.GetNamedSilentFail(name)); } if (cat?.include != null) { foreach (string name in cat.include) Take(DefDatabase.GetNamedSilentFail(name)); } var entries = new List(defs.Count); foreach (T d in defs) entries.Add(ActEntry.ForDef(cat.defName, d)); Disambiguate(entries); return entries; } /// /// Entries of one category that share a label (three states read "tantrum", two "binging on drug") /// get the def's name in brackets, "Tantrum (Targeted Tantrum)". An entry whose def name reads as its /// label keeps the plain label. The picker searches labels and def names, so both stay findable. /// public static void Disambiguate(List entries) { var byLabel = new Dictionary>(StringComparer.OrdinalIgnoreCase); foreach (ActEntry e in entries) { if (e?.def == null || e.label == null) continue; if (!byLabel.TryGetValue(e.label, out List same)) byLabel[e.label] = same = new List(); same.Add(e); } foreach (List same in byLabel.Values) { if (same.Count < 2) continue; foreach (ActEntry e in same) { string readable = Readable(e.def.defName); if (Squash(readable) == Squash(e.label)) continue; e.label = "BlaxLaws_CatalogueLabelWithName".Translate(e.label, readable); } } } /// "Binging_DrugExtreme" to "Binging Drug Extreme". public static string Readable(string defName) => defName.NullOrEmpty() ? defName : GenText.SplitCamelCase(defName.Replace('_', ' ')).Replace(" ", " ").Trim(); private static string Squash(string s) => s == null ? "" : s.Replace(" ", "").Replace("-", "").ToLowerInvariant(); } }