133 lines
6.0 KiB
C#
133 lines
6.0 KiB
C#
|
|
using System;
|
||
|
|
using HarmonyLib;
|
||
|
|
using RimWorld;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxDefaults
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 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.
|
||
|
|
/// </summary>
|
||
|
|
public static class BillDefaults
|
||
|
|
{
|
||
|
|
/// <summary>The repeat modes a default may name, in menu order: the three Bill_Production.ShouldDoNow knows.</summary>
|
||
|
|
public static readonly string[] RepeatModeNames = { "RepeatCount", "TargetCount", "Forever" };
|
||
|
|
|
||
|
|
/// <summary>The store modes a default may name, in menu order. Never SpecificStockpile: it needs a save-specific group.</summary>
|
||
|
|
public static readonly string[] StoreModeNames = { "BestStockpile", "DropOnFloor" };
|
||
|
|
|
||
|
|
/// <summary>The saved "Every new bill" values, or null.</summary>
|
||
|
|
public static BillValues Values => DefaultsLibrary.Get<BillValues>(DefaultsKeys.EveryBill);
|
||
|
|
|
||
|
|
/// <summary>True for exactly Bill_Production and Bill_ProductionWithUft.</summary>
|
||
|
|
public static bool AppliesTo(Bill bill)
|
||
|
|
{
|
||
|
|
Type t = bill?.GetType();
|
||
|
|
return t == typeof(Bill_Production) || t == typeof(Bill_ProductionWithUft);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>When BillsOn, AppliesTo(bill) and values are saved: Apply.</summary>
|
||
|
|
public static void OnNewBill(Bill bill)
|
||
|
|
{
|
||
|
|
if (!BlaxDefaultsMod.BillsOn || !AppliesTo(bill))
|
||
|
|
return;
|
||
|
|
BillValues values = Values;
|
||
|
|
if (values == null)
|
||
|
|
return;
|
||
|
|
Apply((Bill_Production)bill, values);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 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.
|
||
|
|
/// </summary>
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The repeat mode def for a saved name: RepeatCount, TargetCount or Forever; null for any other name.</summary>
|
||
|
|
public static BillRepeatModeDef ResolveRepeatMode(string name)
|
||
|
|
{
|
||
|
|
if (name.NullOrEmpty())
|
||
|
|
return null;
|
||
|
|
BillRepeatModeDef def = DefDatabase<BillRepeatModeDef>.GetNamedSilentFail(name);
|
||
|
|
return def == BillRepeatModeDefOf.RepeatCount || def == BillRepeatModeDefOf.TargetCount || def == BillRepeatModeDefOf.Forever
|
||
|
|
? def
|
||
|
|
: null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The store mode def for a saved name: BestStockpile or DropOnFloor; null for any other name, SpecificStockpile included.</summary>
|
||
|
|
public static BillStoreModeDef ResolveStoreMode(string name)
|
||
|
|
{
|
||
|
|
if (name.NullOrEmpty())
|
||
|
|
return null;
|
||
|
|
BillStoreModeDef def = DefDatabase<BillStoreModeDef>.GetNamedSilentFail(name);
|
||
|
|
return def == BillStoreModeDefOf.BestStockpile || def == BillStoreModeDefOf.DropOnFloor ? def : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Whether "do until you have X" works for this bill, as the vanilla menu decides it.</summary>
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|