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

208 lines
9.1 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using RimWorld;
using Verse;
namespace BlaxCastes
{
/// <summary>
/// Reads and writes a pawn's policy slots through the vanilla setters, with the checks that decide
/// which slots a pawn can take. CasteManager decides when to write; nothing else should call Apply.
/// </summary>
public static class CasteUtility
{
public static readonly CasteSlot[] AllSlots = { CasteSlot.Apparel, CasteSlot.Food, CasteSlot.Drug, CasteSlot.Reading, CasteSlot.MedCare };
/// <summary>Player faction (colonists, slaves, dead colonists) or a prisoner of the player.</summary>
public static bool IsPlayerSide(Pawn pawn)
{
if (pawn == null)
return false;
return pawn.Faction == Faction.OfPlayer || pawn.IsPrisonerOfColony;
}
/// <summary>Humanlike, still around, and player-side.</summary>
public static bool CanBeMember(Pawn pawn) =>
pawn != null && !GameComponent_BlaxCastes.IsGone(pawn) && pawn.RaceProps != null && pawn.RaceProps.Humanlike && IsPlayerSide(pawn);
private static bool PoliciesDisabled(Pawn pawn) => pawn.IsMutant && pawn.mutant.Def.disablePolicies;
/// <summary>Apparel, drug and reading: tracker, not a quest lodger, not a prisoner, no policy-less mutant.</summary>
private static bool CanTakeOwnPolicies(Pawn pawn) =>
!pawn.IsQuestLodger() && !pawn.IsPrisoner && !PoliciesDisabled(pawn);
/// <summary>Whether the slot can be written on this pawn right now.</summary>
public static bool CanUse(Pawn pawn, CasteSlot slot)
{
if (pawn == null || (pawn.Destroyed && !pawn.Dead))
return false;
switch (slot)
{
case CasteSlot.Apparel:
return pawn.outfits != null && CanTakeOwnPolicies(pawn) && !(pawn.IsMutant && pawn.mutant.Def.disableApparel);
case CasteSlot.Drug:
return pawn.drugs != null && CanTakeOwnPolicies(pawn);
case CasteSlot.Reading:
return pawn.reading != null && CanTakeOwnPolicies(pawn);
case CasteSlot.Food:
return pawn.foodRestriction != null && pawn.foodRestriction.Configurable
&& !pawn.DevelopmentalStage.Baby() && !PoliciesDisabled(pawn);
case CasteSlot.MedCare:
return pawn.playerSettings != null && pawn.RaceProps != null && pawn.RaceProps.IsFlesh;
default:
return false;
}
}
/// <summary>The pawn's current value for a slot (a Policy, or a boxed int for MedCare), or null if it has none.</summary>
public static object CurrentValue(Pawn pawn, CasteSlot slot)
{
if (pawn == null)
return null;
switch (slot)
{
case CasteSlot.Apparel: return pawn.outfits?.CurrentApparelPolicy;
case CasteSlot.Food: return pawn.foodRestriction?.CurrentFoodPolicy;
case CasteSlot.Drug: return pawn.drugs?.CurrentPolicy;
case CasteSlot.Reading: return pawn.reading?.CurrentPolicy;
case CasteSlot.MedCare: return pawn.playerSettings == null ? (object)null : (int)pawn.playerSettings.medCare;
default: return null;
}
}
/// <summary>Equality for slot values: policies by reference, med care by value.</summary>
public static bool SameValue(object a, object b)
{
if (a is int ia && b is int ib)
return ia == ib;
return ReferenceEquals(a, b);
}
/// <summary>
/// Writes every managed slot of the caste the pawn can take, through the vanilla setters, and
/// returns those slots. A policy that is no longer in its database is skipped and unmanaged.
/// Does nothing while the "enabled" setting is off.
/// </summary>
public static List<CasteSlot> Apply(Pawn pawn, Caste caste)
{
var applied = new List<CasteSlot>();
if (!BlaxCastesMod.Enabled || pawn == null || caste == null || GameComponent_BlaxCastes.IsGone(pawn))
return applied;
bool missing = false;
foreach (CasteSlot slot in AllSlots)
{
if (!caste.IsManaged(slot))
continue;
object value = caste.GetSlot(slot);
if (slot != CasteSlot.MedCare && !PolicyExists(value as Policy))
{
missing = true;
continue;
}
if (WriteSlot(pawn, slot, value))
applied.Add(slot);
}
if (missing)
CasteManager.UnmanageMissingPolicies(caste);
return applied;
}
/// <summary>Writes one slot value if the pawn can take it. Null policies and medCare -1 are never written.</summary>
internal static bool WriteSlot(Pawn pawn, CasteSlot slot, object value)
{
if (!CanUse(pawn, slot))
return false;
switch (slot)
{
case CasteSlot.Apparel:
if (!(value is ApparelPolicy apparel))
return false;
pawn.outfits.CurrentApparelPolicy = apparel;
return true;
case CasteSlot.Food:
if (!(value is FoodPolicy food))
return false;
pawn.foodRestriction.CurrentFoodPolicy = food;
return true;
case CasteSlot.Drug:
if (!(value is DrugPolicy drug))
return false;
pawn.drugs.CurrentPolicy = drug;
return true;
case CasteSlot.Reading:
if (!(value is ReadingPolicy reading))
return false;
pawn.reading.CurrentPolicy = reading;
return true;
case CasteSlot.MedCare:
int care = ToMedCare(value);
if (care < 0)
return false;
pawn.playerSettings.medCare = (MedicalCareCategory)care;
return true;
default:
return false;
}
}
/// <summary>True if the policy is still in its game database.</summary>
public static bool PolicyExists(Policy policy)
{
Game game = Current.Game;
if (policy == null || game == null)
return false;
switch (policy)
{
case ApparelPolicy a: return game.outfitDatabase?.AllOutfits?.Contains(a) == true;
case FoodPolicy f: return game.foodRestrictionDatabase?.AllFoodRestrictions?.Contains(f) == true;
case DrugPolicy d: return game.drugPolicyDatabase?.AllPolicies?.Contains(d) == true;
case ReadingPolicy r: return game.readingPolicyDatabase?.AllReadingPolicies?.Contains(r) == true;
default: return false;
}
}
/// <summary>A med care slot value: -1 for unmanaged, otherwise a valid MedicalCareCategory.</summary>
public static int ToMedCare(object value)
{
int v;
switch (value)
{
case null: return Caste.Unmanaged;
case MedicalCareCategory c: v = (int)c; break;
case int i: v = i; break;
case byte b: v = b; break;
default: return Caste.Unmanaged;
}
return v >= (int)MedicalCareCategory.NoCare && v <= (int)MedicalCareCategory.Best ? v : Caste.Unmanaged;
}
/// <summary>Which slot type a value belongs in; false for a value that fits no slot.</summary>
public static bool FitsSlot(CasteSlot slot, object value)
{
if (value == null)
return true;
switch (slot)
{
case CasteSlot.Apparel: return value is ApparelPolicy;
case CasteSlot.Food: return value is FoodPolicy;
case CasteSlot.Drug: return value is DrugPolicy;
case CasteSlot.Reading: return value is ReadingPolicy;
case CasteSlot.MedCare: return value is int || value is MedicalCareCategory || value is byte;
default: return false;
}
}
public static string SlotLabel(CasteSlot slot) => ("BlaxCastes_Slot" + slot).Translate();
/// <summary>A slot value as the player sees it: the policy label, the med care label, or "unmanaged".</summary>
public static string ValueLabel(CasteSlot slot, object value)
{
if (slot == CasteSlot.MedCare)
{
int care = ToMedCare(value);
return care < 0 ? (string)"BlaxCastes_Unmanaged".Translate() : ((MedicalCareCategory)care).GetLabel();
}
return value is Policy p ? p.label : (string)"BlaxCastes_Unmanaged".Translate();
}
}
}