rimworld-blax-castes/Source/BlaxCastes/Caste.cs

209 lines
8.5 KiB
C#

using System.Collections.Generic;
using RimWorld;
using Verse;
namespace BlaxCastes
{
/// <summary>
/// The slots a caste can manage. The first five hold a vanilla policy or a medical care level
/// (CasteUtility.PolicySlots); Schedule holds 24 time assignment defNames, Work a priority per work
/// type, and Loadout a Weapons Tab Reborn loadout id.
/// </summary>
public enum CasteSlot
{
Apparel,
Food,
Drug,
Reading,
MedCare,
Schedule,
Work,
Loadout,
}
/// <summary>
/// A bundle of policies. Ids, load IDs, rename and duplicate work the vanilla Policy way. A null
/// policy slot, medCare -1, a null schedule, no work priorities or loadout -1 means the caste leaves
/// that slot alone. The later slots store defNames and ids, never def references, so a removed mod
/// leaves nothing unresolved in the save.
/// Read the slots freely; change them only through CasteManager, which keeps members in sync.
/// </summary>
public class Caste : Policy
{
public const int Unmanaged = -1;
private ApparelPolicy apparelPolicy;
private FoodPolicy foodPolicy;
private DrugPolicy drugPolicy;
private ReadingPolicy readingPolicy;
private int medCare = Unmanaged;
private List<string> schedule;
private Dictionary<string, int> workPriorities = new Dictionary<string, int>();
private bool workStartFromNothing;
private int loadoutId = Unmanaged;
protected override string LoadKey => "BlaxCaste";
public Caste()
{
}
public Caste(int id, string label) : base(id, label)
{
}
public ApparelPolicy ApparelPolicy => apparelPolicy;
public FoodPolicy FoodPolicy => foodPolicy;
public DrugPolicy DrugPolicy => drugPolicy;
public ReadingPolicy ReadingPolicy => readingPolicy;
/// <summary>-1 (Unmanaged), otherwise a MedicalCareCategory.</summary>
public int MedCare => medCare;
public MedicalCareCategory? MedCareCategory => medCare < 0 ? (MedicalCareCategory?)null : (MedicalCareCategory)medCare;
/// <summary>The 24 time assignment defNames, hour 0 first, or null when the caste leaves the schedule alone.</summary>
public IReadOnlyList<string> Schedule => schedule;
/// <summary>Work type defName to priority 0 (off) to 4. A work type that is not listed is left alone.</summary>
public IReadOnlyDictionary<string, int> WorkPriorities => workPriorities;
/// <summary>Applying the caste turns every work type off before writing the listed ones.</summary>
public bool WorkStartFromNothing => workStartFromNothing;
/// <summary>A Weapons Tab Reborn loadout id, or -1 (Unmanaged).</summary>
public int LoadoutId => loadoutId;
/// <summary>The priority the caste sets for a work type, or null when it leaves that type alone.</summary>
public int? WorkPriorityOf(string workTypeDefName)
{
if (workTypeDefName != null && workPriorities.TryGetValue(workTypeDefName, out int p))
return p;
return null;
}
/// <summary>
/// The slot's value: a Policy, a boxed int for MedCare and Loadout (-1 = unmanaged), the read-only
/// schedule list, the read-only work priorities (null when none are listed), or null.
/// </summary>
public object GetSlot(CasteSlot slot)
{
switch (slot)
{
case CasteSlot.Apparel: return apparelPolicy;
case CasteSlot.Food: return foodPolicy;
case CasteSlot.Drug: return drugPolicy;
case CasteSlot.Reading: return readingPolicy;
case CasteSlot.MedCare: return medCare;
case CasteSlot.Schedule: return schedule;
case CasteSlot.Work: return workPriorities.Count == 0 ? null : workPriorities;
case CasteSlot.Loadout: return loadoutId;
default: return null;
}
}
public bool IsManaged(CasteSlot slot)
{
switch (slot)
{
case CasteSlot.MedCare: return medCare >= 0;
case CasteSlot.Schedule: return schedule != null;
case CasteSlot.Work: return workPriorities.Count > 0;
case CasteSlot.Loadout: return loadoutId >= 0;
default: return GetSlot(slot) != null;
}
}
public bool AnyManaged()
{
foreach (CasteSlot slot in CasteUtility.AllSlots)
{
if (IsManaged(slot))
return true;
}
return false;
}
/// <summary>
/// Raw write without member sync. Only CasteManager and the starter castes call this. Collections
/// are always replaced by a new normalized copy, never changed in place, so a value read before
/// the write stays as it was.
/// </summary>
internal void SetSlotRaw(CasteSlot slot, object value)
{
switch (slot)
{
case CasteSlot.Apparel:
apparelPolicy = value as ApparelPolicy;
break;
case CasteSlot.Food:
foodPolicy = value as FoodPolicy;
break;
case CasteSlot.Drug:
drugPolicy = value as DrugPolicy;
break;
case CasteSlot.Reading:
readingPolicy = value as ReadingPolicy;
break;
case CasteSlot.MedCare:
medCare = CasteUtility.ToMedCare(value);
break;
case CasteSlot.Schedule:
schedule = ScheduleSlot.Normalize(value);
break;
case CasteSlot.Work:
workPriorities = WorkSlot.Normalize(value) ?? new Dictionary<string, int>();
break;
case CasteSlot.Loadout:
loadoutId = CasteUtility.ToLoadoutId(value);
break;
}
}
/// <summary>Raw write of the "Start from nothing" flag. It moves no member; it only changes later applies.</summary>
internal void SetWorkStartFromNothingRaw(bool value) => workStartFromNothing = value;
/// <summary>Copies every slot, not the id or the label (vanilla duplicate keeps the new policy's label).</summary>
public override void CopyFrom(Policy other)
{
if (other is Caste caste)
{
apparelPolicy = caste.apparelPolicy;
foodPolicy = caste.foodPolicy;
drugPolicy = caste.drugPolicy;
readingPolicy = caste.readingPolicy;
medCare = caste.medCare;
schedule = caste.schedule == null ? null : new List<string>(caste.schedule);
workPriorities = new Dictionary<string, int>(caste.workPriorities);
workStartFromNothing = caste.workStartFromNothing;
loadoutId = caste.loadoutId;
}
}
public override void ExposeData()
{
base.ExposeData();
Scribe_References.Look(ref apparelPolicy, "apparelPolicy");
Scribe_References.Look(ref foodPolicy, "foodPolicy");
Scribe_References.Look(ref drugPolicy, "drugPolicy");
Scribe_References.Look(ref readingPolicy, "readingPolicy");
Scribe_Values.Look(ref medCare, "medCare", Unmanaged);
Scribe_Collections.Look(ref schedule, "schedule", LookMode.Value);
Scribe_Collections.Look(ref workPriorities, "workPriorities", LookMode.Value, LookMode.Value);
Scribe_Values.Look(ref workStartFromNothing, "workStartFromNothing");
Scribe_Values.Look(ref loadoutId, "loadoutId", Unmanaged);
if (Scribe.mode == LoadSaveMode.PostLoadInit)
{
medCare = CasteUtility.ToMedCare(medCare);
workPriorities ??= new Dictionary<string, int>();
if (schedule != null && schedule.Count != ScheduleSlot.Hours)
schedule = null;
loadoutId = CasteUtility.ToLoadoutId(loadoutId);
// Unknown time assignments and work types are fixed, with a warning, by CasteManager.CheckSlots.
}
}
public override string ToString() => "Caste(" + id + ", " + label + ")";
}
}