using System.Collections.Generic; using RimWorld; using Verse; namespace BlaxCastes { /// /// 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. /// public enum CasteSlot { Apparel, Food, Drug, Reading, MedCare, Schedule, Work, Loadout, } /// /// 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. /// 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 schedule; private Dictionary workPriorities = new Dictionary(); 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; /// -1 (Unmanaged), otherwise a MedicalCareCategory. public int MedCare => medCare; public MedicalCareCategory? MedCareCategory => medCare < 0 ? (MedicalCareCategory?)null : (MedicalCareCategory)medCare; /// The 24 time assignment defNames, hour 0 first, or null when the caste leaves the schedule alone. public IReadOnlyList Schedule => schedule; /// Work type defName to priority 0 (off) to 4. A work type that is not listed is left alone. public IReadOnlyDictionary WorkPriorities => workPriorities; /// Applying the caste turns every work type off before writing the listed ones. public bool WorkStartFromNothing => workStartFromNothing; /// A Weapons Tab Reborn loadout id, or -1 (Unmanaged). public int LoadoutId => loadoutId; /// The priority the caste sets for a work type, or null when it leaves that type alone. public int? WorkPriorityOf(string workTypeDefName) { if (workTypeDefName != null && workPriorities.TryGetValue(workTypeDefName, out int p)) return p; return null; } /// /// 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. /// 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; } /// /// 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. /// 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(); break; case CasteSlot.Loadout: loadoutId = CasteUtility.ToLoadoutId(value); break; } } /// Raw write of the "Start from nothing" flag. It moves no member; it only changes later applies. internal void SetWorkStartFromNothingRaw(bool value) => workStartFromNothing = value; /// Copies every slot, not the id or the label (vanilla duplicate keeps the new policy's label). 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(caste.schedule); workPriorities = new Dictionary(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(); 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; // names of an official DLC that is off are kept for when it is back. } } public override string ToString() => "Caste(" + id + ", " + label + ")"; } }