using System; using HarmonyLib; using RimWorld; using Verse; namespace BlaxDefaults { /// /// Values for every new bill. Work package "bills" (design.md: "Values for every new bill"). A postfix on /// BillUtility.MakeNewBill, whose only vanilla callers are the Bills tab's add menu and the dev colony maker. /// Applies to exactly Bill_Production and Bill_ProductionWithUft: Bill_Autonomous, Bill_ProductionMech and /// Bill_ResurrectMech also derive from Bill_Production (through Bill_Autonomous and Bill_Mech) and must be /// skipped, so the check is on the exact type. Surgery (Bill_Medical), pasted bills (Bill.Clone) and loaded /// bills never reach MakeNewBill. /// public static class BillDefaults { /// The repeat modes a default may name, in menu order: the three Bill_Production.ShouldDoNow knows. public static readonly string[] RepeatModeNames = { "RepeatCount", "TargetCount", "Forever" }; /// The store modes a default may name, in menu order. Never SpecificStockpile: it needs a save-specific group. public static readonly string[] StoreModeNames = { "BestStockpile", "DropOnFloor" }; /// The saved "Every new bill" values, or null. public static BillValues Values => DefaultsLibrary.Get(DefaultsKeys.EveryBill); /// True for exactly Bill_Production and Bill_ProductionWithUft. public static bool AppliesTo(Bill bill) { Type t = bill?.GetType(); return t == typeof(Bill_Production) || t == typeof(Bill_ProductionWithUft); } /// When BillsOn, AppliesTo(bill) and values are saved: Apply. public static void OnNewBill(Bill bill) { if (!BlaxDefaultsMod.BillsOn || !AppliesTo(bill)) return; BillValues values = Values; if (values == null) return; Apply((Bill_Production)bill, values); } /// /// Writes every set value onto the bill: repeatMode (TargetCount only where /// recipe.WorkerCounter.CanCountProducts(bill), else RepeatCount with the count), repeatCount, /// targetCount, pauseWhenSatisfied, unpauseWhenYouHave, ingredientSearchRadius, allowedSkillRange, /// SetStoreMode(BestStockpile or DropOnFloor, never SpecificStockpile), includeEquipped, /// includeTainted, hpRange, qualityRange. /// public static void Apply(Bill_Production bill, BillValues values) { if (bill == null || values == null) return; BillRepeatModeDef repeat = ResolveRepeatMode(values.repeatMode); if (repeat != null) { // The check the vanilla repeat menu makes (BillRepeatModeUtility.MakeConfigFloatMenu). if (repeat == BillRepeatModeDefOf.TargetCount && !CanCountProducts(bill)) repeat = BillRepeatModeDefOf.RepeatCount; bill.repeatMode = repeat; } if (values.setRepeatCount) bill.repeatCount = values.repeatCount; if (values.setTargetCount) bill.targetCount = values.targetCount; if (values.setPauseWhenSatisfied) bill.pauseWhenSatisfied = values.pauseWhenSatisfied; if (values.setUnpauseWhenYouHave) bill.unpauseWhenYouHave = values.unpauseWhenYouHave; if (values.setIngredientSearchRadius) bill.ingredientSearchRadius = values.ingredientSearchRadius; if (values.setAllowedSkillRange) bill.allowedSkillRange = values.allowedSkillRange; BillStoreModeDef store = ResolveStoreMode(values.storeMode); if (store != null) bill.SetStoreMode(store); if (values.setIncludeEquipped) bill.includeEquipped = values.includeEquipped; if (values.setIncludeTainted) bill.includeTainted = values.includeTainted; if (values.setHpRange) bill.hpRange = values.hpRange; if (values.setQualityRange) bill.qualityRange = values.qualityRange; } /// The repeat mode def for a saved name: RepeatCount, TargetCount or Forever; null for any other name. public static BillRepeatModeDef ResolveRepeatMode(string name) { if (name.NullOrEmpty()) return null; BillRepeatModeDef def = DefDatabase.GetNamedSilentFail(name); return def == BillRepeatModeDefOf.RepeatCount || def == BillRepeatModeDefOf.TargetCount || def == BillRepeatModeDefOf.Forever ? def : null; } /// The store mode def for a saved name: BestStockpile or DropOnFloor; null for any other name, SpecificStockpile included. public static BillStoreModeDef ResolveStoreMode(string name) { if (name.NullOrEmpty()) return null; BillStoreModeDef def = DefDatabase.GetNamedSilentFail(name); return def == BillStoreModeDefOf.BestStockpile || def == BillStoreModeDefOf.DropOnFloor ? def : null; } /// Whether "do until you have X" works for this bill, as the vanilla menu decides it. public static bool CanCountProducts(Bill_Production bill) => bill?.recipe?.WorkerCounter != null && bill.recipe.WorkerCounter.CanCountProducts(bill); } [HarmonyPatch(typeof(BillUtility), nameof(BillUtility.MakeNewBill))] public static class Patch_BillUtility_MakeNewBill { public static void Postfix(Bill __result) { try { BillDefaults.OnNewBill(__result); } catch (Exception e) { HookGuard.Report("BillUtility.MakeNewBill", e); } } } }