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

239 lines
10 KiB
C#

using System.Collections.Generic;
using RimWorld;
using Verse;
namespace BlaxCastes
{
/// <summary>
/// Every change to castes, memberships, join rows and the default caste goes through here, so the
/// rules (member sync, delete fallbacks, the enabled switch) hold no matter which window or hook
/// made the change. Methods do nothing and return false or null when no game is loaded.
/// </summary>
public static class CasteManager
{
private static GameComponent_BlaxCastes Comp => GameComponent_BlaxCastes.Instance;
private static int Now => Find.TickManager?.TicksGame ?? 0;
// ---- castes ------------------------------------------------------------------------
/// <summary>A new caste with the next free id, labelled "Caste N", managing nothing.</summary>
public static Caste CreateCaste(string label = null)
{
GameComponent_BlaxCastes comp = Comp;
if (comp == null)
return null;
int id = comp.TakeNextId();
var caste = new Caste(id, label.NullOrEmpty() ? (string)"BlaxCastes_NewCasteLabel".Translate(id) : label);
comp.AddCaste(caste);
return caste;
}
/// <summary>A new caste with every slot copied from the source, the vanilla duplicate way.</summary>
public static Caste DuplicateCaste(Caste source)
{
GameComponent_BlaxCastes comp = Comp;
if (comp == null || source == null)
return null;
Caste copy = CreateCaste();
copy.CopyFrom(source);
return copy;
}
/// <summary>
/// Removes the caste. Its members become casteless and keep their current policies, join rows
/// that point at it fall back to No caste, and it stops being the default.
/// </summary>
public static bool DeleteCaste(Caste caste)
{
GameComponent_BlaxCastes comp = Comp;
if (comp == null || !comp.HasCaste(caste))
return false;
foreach (Pawn member in comp.MembersOf(caste))
{
if (CasteUtility.CanBeMember(member))
comp.SetMembership(member, new CasteMembership(null, false, JoinReason.CasteDeleted, Now));
else
comp.RemoveMembership(member);
}
foreach (JoinRow row in comp.JoinRows)
{
if (row.mode == JoinRowMode.SpecificCaste && row.caste == caste)
comp.SetRowRaw(row.reason, JoinRowMode.NoCaste, null);
}
if (comp.DefaultCaste == caste)
comp.SetDefaultRaw(null);
comp.RemoveCaste(caste);
return true;
}
/// <summary>Sets the caste that "Default caste" rows use. Null clears it, so those rows act as No caste.</summary>
public static bool SetDefault(Caste caste)
{
GameComponent_BlaxCastes comp = Comp;
if (comp == null || (caste != null && !comp.HasCaste(caste)))
return false;
comp.SetDefaultRaw(caste);
return true;
}
/// <summary>
/// Changes one slot. Members still on the old value get the new one; members on another value
/// keep it (the Follow rule). A slot that was unmanaged had no old value, so every member gets
/// the new one. Unmanaging a slot changes no pawn. Returns how many members were moved.
/// </summary>
public static int SetSlot(Caste caste, CasteSlot slot, object newValue)
{
GameComponent_BlaxCastes comp = Comp;
if (comp == null || !comp.HasCaste(caste))
return 0;
if (!CasteUtility.FitsSlot(slot, newValue))
{
Log.Warning("[Blax Castes] " + newValue.GetType().Name + " does not fit the " + slot + " slot of " + caste.label + ".");
return 0;
}
if (slot == CasteSlot.MedCare)
newValue = CasteUtility.ToMedCare(newValue);
else if (newValue != null && !CasteUtility.PolicyExists((Policy)newValue))
{
Log.Warning("[Blax Castes] Policy " + ((Policy)newValue).label + " is not in the game's database; " + caste.label + " keeps its " + slot + " slot.");
return 0;
}
object oldValue = caste.GetSlot(slot);
bool wasManaged = caste.IsManaged(slot);
if (CasteUtility.SameValue(oldValue, newValue))
return 0;
caste.SetSlotRaw(slot, newValue);
if (!caste.IsManaged(slot) || !BlaxCastesMod.Enabled)
return 0;
int moved = 0;
foreach (Pawn member in comp.MembersOf(caste))
{
if (GameComponent_BlaxCastes.IsGone(member) || !CasteUtility.CanUse(member, slot))
continue;
if (wasManaged && !CasteUtility.SameValue(CasteUtility.CurrentValue(member, slot), oldValue))
continue;
if (CasteUtility.WriteSlot(member, slot, newValue))
moved++;
}
return moved;
}
// ---- memberships -------------------------------------------------------------------
/// <summary>
/// Puts the pawn into the caste and writes every managed slot it can take. auto: a join row did
/// it, not the player. A null caste makes the pawn casteless. False if the pawn can't be a member
/// (not humanlike, gone, or not player faction or player prisoner) or the caste isn't in this game.
/// </summary>
public static bool Assign(Pawn pawn, Caste caste, bool auto, string reason)
{
if (caste == null)
return MakeCasteless(pawn, auto, reason);
GameComponent_BlaxCastes comp = Comp;
if (comp == null || !comp.HasCaste(caste) || !CasteUtility.CanBeMember(pawn))
return false;
comp.SetMembership(pawn, new CasteMembership(caste, auto, reason ?? JoinReason.Manual, Now));
CasteUtility.Apply(pawn, caste);
return true;
}
/// <summary>Casteless by choice: the pawn leaves its caste and keeps its current policies.</summary>
public static bool MakeCasteless(Pawn pawn, bool auto = false, string reason = JoinReason.Manual)
{
GameComponent_BlaxCastes comp = Comp;
if (comp == null || !CasteUtility.CanBeMember(pawn))
return false;
comp.SetMembership(pawn, new CasteMembership(null, auto, reason ?? JoinReason.Manual, Now));
return true;
}
/// <summary>Re-writes every managed slot of the pawn's caste, clearing its overrides.</summary>
public static bool ResetToCaste(Pawn pawn)
{
GameComponent_BlaxCastes comp = Comp;
Caste caste = comp?.CasteOf(pawn);
if (caste == null || !CasteUtility.CanBeMember(pawn))
return false;
CasteUtility.Apply(pawn, caste);
return true;
}
/// <summary>Removes the pawn's record, as if it never had a caste. Its policies stay as they are.</summary>
public static bool EndMembership(Pawn pawn)
{
GameComponent_BlaxCastes comp = Comp;
return comp != null && comp.RemoveMembership(pawn);
}
/// <summary>
/// The managed slots of the pawn's caste where the pawn has another value (a hand change or a
/// vanilla reset). Slots the pawn can't take are never overrides.
/// </summary>
public static List<CasteSlot> Overrides(Pawn pawn)
{
var result = new List<CasteSlot>();
Caste caste = Comp?.CasteOf(pawn);
if (caste == null)
return result;
foreach (CasteSlot slot in CasteUtility.AllSlots)
{
if (!caste.IsManaged(slot) || !CasteUtility.CanUse(pawn, slot))
continue;
if (!CasteUtility.SameValue(CasteUtility.CurrentValue(pawn, slot), caste.GetSlot(slot)))
result.Add(slot);
}
return result;
}
// ---- join rows ---------------------------------------------------------------------
/// <summary>Sets what a join row does. SpecificCaste needs a caste of this game.</summary>
public static bool SetJoinRow(string reason, JoinRowMode mode, Caste caste = null)
{
GameComponent_BlaxCastes comp = Comp;
if (comp == null || reason.NullOrEmpty())
return false;
if (mode == JoinRowMode.SpecificCaste && !comp.HasCaste(caste))
return false;
comp.SetRowRaw(reason, mode, caste);
return true;
}
// ---- save safety -------------------------------------------------------------------
/// <summary>Unmanages every slot whose policy left its database, with one warning per caste.</summary>
internal static void CheckPolicies(GameComponent_BlaxCastes comp)
{
if (comp == null)
return;
foreach (Caste caste in comp.Castes)
UnmanageMissingPolicies(caste);
}
/// <summary>Unmanages the caste's slots whose policy is no longer in its database. Returns how many.</summary>
internal static int UnmanageMissingPolicies(Caste caste)
{
if (caste == null || Current.Game == null)
return 0;
List<string> missing = null;
foreach (CasteSlot slot in CasteUtility.AllSlots)
{
if (slot == CasteSlot.MedCare || !(caste.GetSlot(slot) is Policy policy))
continue;
if (CasteUtility.PolicyExists(policy))
continue;
(missing ??= new List<string>()).Add(slot + " (" + policy.label + ")");
caste.SetSlotRaw(slot, null);
}
if (missing == null)
return 0;
Log.Warning("[Blax Castes] Caste \"" + caste.label + "\": " + string.Join(", ", missing)
+ " no longer in the game's policies. The slot is now unmanaged.");
return missing.Count;
}
}
}