343 lines
14 KiB
C#
343 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using RimWorld;
|
|
using Verse;
|
|
|
|
namespace BlaxCastes
|
|
{
|
|
/// <summary>Which player-side status a pawn has, for join classification.</summary>
|
|
public enum JoinSide
|
|
{
|
|
None,
|
|
Colonist,
|
|
Slave,
|
|
Prisoner,
|
|
}
|
|
|
|
/// <summary>
|
|
/// Join defaults: puts a pawn that joins into the caste of its join row.
|
|
///
|
|
/// The join hooks (JoinHooks: Pawn.SetFaction, Pawn_GuestTracker.SetGuestStatus, Pawn.PreTraded)
|
|
/// classify the join where it happens and call Enqueue with the innermost scope's reason, or with
|
|
/// StatusFallback. The queue lives in GameComponent_BlaxCastes.JoinQueue, so it survives a save.
|
|
/// GameComponentTick calls ApplyQueue every tick, which applies entries queued on an earlier tick:
|
|
/// never inside SetFaction or SetGuestStatus, and only while the pawn is still player-side.
|
|
///
|
|
/// Reason to row, first match wins: the scope ("bought": the final status picks bought as slave or
|
|
/// bought as colonist; a row id is used as it is), then the status fallback from the final status:
|
|
/// new colonist, new slave or new prisoner.
|
|
/// </summary>
|
|
public static class JoinDefaults
|
|
{
|
|
/// <summary>Queue reason when no scope covered the join: the final status picks the row.</summary>
|
|
public const string StatusFallback = "status";
|
|
|
|
// Pawns that left the player side, checked on the next ApplyQueue. Not saved: the 2500-tick
|
|
// sweep ends any membership a save cut short.
|
|
private static readonly HashSet<Pawn> left = new HashSet<Pawn>();
|
|
private static Game leftGame;
|
|
|
|
private static GameComponent_BlaxCastes Comp => GameComponent_BlaxCastes.Instance;
|
|
|
|
private static int Now => Find.TickManager?.TicksGame ?? 0;
|
|
|
|
/// <summary>Castes is on and the join defaults setting is on.</summary>
|
|
public static bool Active =>
|
|
BlaxCastesMod.Enabled && (BlaxCastesMod.Settings == null || BlaxCastesMod.Settings.enableJoinDefaults);
|
|
|
|
// ---- classification ----------------------------------------------------------------
|
|
|
|
public static JoinSide SideOf(Pawn pawn)
|
|
{
|
|
if (pawn == null)
|
|
return JoinSide.None;
|
|
Pawn_GuestTracker guest = pawn.guest;
|
|
return Classify(pawn.Faction, guest != null, guest != null ? guest.guestStatusInt : GuestStatus.Guest, guest?.HostFaction);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Player faction: slave, prisoner of the colony (an arrested colonist) or colonist (also one held
|
|
/// by another faction, so it keeps its caste when it comes back). Another faction: prisoner of the
|
|
/// colony or nothing.
|
|
/// </summary>
|
|
internal static JoinSide Classify(Faction faction, bool hasGuest, GuestStatus status, Faction host)
|
|
{
|
|
Faction player = Faction.OfPlayerSilentFail;
|
|
if (player == null)
|
|
return JoinSide.None;
|
|
bool prisonerOfColony = hasGuest && status == GuestStatus.Prisoner && host == player;
|
|
if (faction == player)
|
|
{
|
|
if (hasGuest && status == GuestStatus.Slave)
|
|
return JoinSide.Slave;
|
|
return prisonerOfColony ? JoinSide.Prisoner : JoinSide.Colonist;
|
|
}
|
|
return prisonerOfColony ? JoinSide.Prisoner : JoinSide.None;
|
|
}
|
|
|
|
/// <summary>Humanlike, not a baby, not a quest lodger, not a mutant without policies. Others never join a caste.</summary>
|
|
public static bool Eligible(Pawn pawn) =>
|
|
pawn != null && pawn.RaceProps != null && pawn.RaceProps.Humanlike
|
|
&& !pawn.DevelopmentalStage.Baby() && !pawn.IsQuestLodger()
|
|
&& !(pawn.IsMutant && pawn.mutant.Def.disablePolicies);
|
|
|
|
/// <summary>A scope reason or a row id, as opposed to the status fallback.</summary>
|
|
public static bool IsSpecific(string reason) => !reason.NullOrEmpty() && reason != StatusFallback;
|
|
|
|
/// <summary>
|
|
/// The join row a queued reason resolves to for the pawn's status now, or null when the pawn has
|
|
/// no player-side status.
|
|
/// </summary>
|
|
public static string RowFor(string reason, Pawn pawn)
|
|
{
|
|
JoinSide side = SideOf(pawn);
|
|
if (reason == JoinScopes.Bought)
|
|
{
|
|
if (side == JoinSide.Slave)
|
|
return JoinReason.BoughtSlave;
|
|
if (side == JoinSide.Colonist)
|
|
return JoinReason.BoughtColonist;
|
|
}
|
|
else if (JoinReason.IsRow(reason))
|
|
{
|
|
return reason;
|
|
}
|
|
switch (side)
|
|
{
|
|
case JoinSide.Colonist: return JoinReason.NewColonist;
|
|
case JoinSide.Slave: return JoinReason.NewSlave;
|
|
case JoinSide.Prisoner: return JoinReason.NewPrisoner;
|
|
default: return null;
|
|
}
|
|
}
|
|
|
|
// ---- queue -------------------------------------------------------------------------
|
|
|
|
/// <summary>The pawn's waiting join, or null.</summary>
|
|
public static QueuedJoin QueuedFor(Pawn pawn)
|
|
{
|
|
GameComponent_BlaxCastes comp = Comp;
|
|
if (comp == null || pawn == null)
|
|
return null;
|
|
List<QueuedJoin> queue = comp.JoinQueue;
|
|
for (int i = 0; i < queue.Count; i++)
|
|
{
|
|
if (queue[i] != null && queue[i].pawn == pawn)
|
|
return queue[i];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>True while the pawn waits for its join to be applied (the column's "pending").</summary>
|
|
public static bool IsQueued(Pawn pawn) => QueuedFor(pawn) != null;
|
|
|
|
/// <summary>
|
|
/// Queues a join for the next tick: a scope reason, a row id or StatusFallback. One entry per pawn;
|
|
/// a specific reason replaces a status fallback, never the other way round. Does nothing while
|
|
/// Castes or the join defaults are off, and for pawns that never join a caste.
|
|
/// </summary>
|
|
public static void Enqueue(Pawn pawn, string reason)
|
|
{
|
|
if (!Active || pawn == null)
|
|
return;
|
|
GameComponent_BlaxCastes comp = Comp;
|
|
if (comp == null || GameComponent_BlaxCastes.IsGone(pawn) || !Eligible(pawn))
|
|
return;
|
|
if (reason.NullOrEmpty())
|
|
reason = StatusFallback;
|
|
left.Remove(pawn);
|
|
int now = Now;
|
|
QueuedJoin queued = QueuedFor(pawn);
|
|
if (queued == null)
|
|
{
|
|
comp.JoinQueue.Add(new QueuedJoin(pawn, reason, now));
|
|
return;
|
|
}
|
|
if (IsSpecific(reason) || !IsSpecific(queued.reason))
|
|
queued.reason = reason;
|
|
if (now > queued.tick)
|
|
queued.tick = now;
|
|
}
|
|
|
|
/// <summary>A pawn left the player side. Its membership ends on the next tick if it has not come back.</summary>
|
|
internal static void NoteLeft(Pawn pawn)
|
|
{
|
|
if (pawn == null || Current.Game == null)
|
|
return;
|
|
if (leftGame != Current.Game)
|
|
{
|
|
left.Clear();
|
|
leftGame = Current.Game;
|
|
}
|
|
left.Add(pawn);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Applies the joins queued on an earlier tick and ends the memberships of pawns that left.
|
|
/// Called by GameComponent_BlaxCastes every tick.
|
|
/// </summary>
|
|
public static void ApplyQueue()
|
|
{
|
|
GameComponent_BlaxCastes comp = Comp;
|
|
if (comp == null || Find.TickManager == null)
|
|
return;
|
|
EndLeftMemberships();
|
|
List<QueuedJoin> queue = comp.JoinQueue;
|
|
if (queue.Count == 0)
|
|
return;
|
|
|
|
int now = Now;
|
|
List<QueuedJoin> due = null;
|
|
foreach (QueuedJoin q in queue)
|
|
{
|
|
if (q == null || q.tick < now)
|
|
(due ??= new List<QueuedJoin>()).Add(q);
|
|
}
|
|
if (due == null)
|
|
return;
|
|
queue.RemoveAll(q => q == null || q.tick < now);
|
|
|
|
Dictionary<Caste, List<Pawn>> joined = null;
|
|
foreach (QueuedJoin q in due)
|
|
{
|
|
if (q == null)
|
|
continue;
|
|
try
|
|
{
|
|
Caste caste = ApplyOne(comp, q);
|
|
if (caste != null)
|
|
{
|
|
joined ??= new Dictionary<Caste, List<Pawn>>();
|
|
if (!joined.TryGetValue(caste, out List<Pawn> pawns))
|
|
joined[caste] = pawns = new List<Pawn>();
|
|
pawns.Add(q.pawn);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
JoinHooks.LogOnce("Applying a join", e);
|
|
}
|
|
}
|
|
if (joined != null)
|
|
SendMessages(joined);
|
|
}
|
|
|
|
/// <summary>Applies one join. Returns the caste the pawn joined, or null.</summary>
|
|
private static Caste ApplyOne(GameComponent_BlaxCastes comp, QueuedJoin q)
|
|
{
|
|
Pawn pawn = q.pawn;
|
|
if (GameComponent_BlaxCastes.IsGone(pawn) || pawn.Dead || pawn.Destroyed)
|
|
return null;
|
|
if (!CasteUtility.IsPlayerSide(pawn))
|
|
{
|
|
// Sold, released or gone over to another faction before the join was applied.
|
|
CasteManager.EndMembership(pawn);
|
|
return null;
|
|
}
|
|
if (!Active || !Eligible(pawn))
|
|
return null;
|
|
|
|
CasteMembership membership = comp.MembershipOf(pawn);
|
|
// A caste picked by hand after the join was queued wins over the join row.
|
|
if (membership != null && !membership.auto && membership.reason != JoinReason.CasteDeleted && membership.tick >= q.tick)
|
|
return null;
|
|
|
|
string row = RowFor(q.reason, pawn);
|
|
if (row == null)
|
|
return null;
|
|
Caste caste = comp.ResolveRow(row);
|
|
if (caste != null)
|
|
return CasteManager.Assign(pawn, caste, true, row) ? caste : null;
|
|
|
|
if (membership?.caste != null)
|
|
LeaveCaste(pawn, membership.caste);
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The pawn's join row is No caste while it was in a caste: it leaves the caste. With revertOnLeave
|
|
/// the caste's managed slots go back to the database defaults and medical care is reset, so a
|
|
/// recruited prisoner does not keep the prisoner food.
|
|
/// </summary>
|
|
private static void LeaveCaste(Pawn pawn, Caste caste)
|
|
{
|
|
CasteManager.EndMembership(pawn);
|
|
if (!BlaxCastesMod.Enabled || (BlaxCastesMod.Settings != null && !BlaxCastesMod.Settings.revertOnLeave))
|
|
return;
|
|
foreach (CasteSlot slot in CasteUtility.AllSlots)
|
|
{
|
|
if (slot == CasteSlot.MedCare || !caste.IsManaged(slot))
|
|
continue;
|
|
object value = DatabaseDefault(slot);
|
|
if (value != null)
|
|
CasteUtility.WriteSlot(pawn, slot, value);
|
|
}
|
|
pawn.playerSettings?.ResetMedicalCare();
|
|
}
|
|
|
|
private static object DatabaseDefault(CasteSlot slot)
|
|
{
|
|
Game game = Current.Game;
|
|
switch (slot)
|
|
{
|
|
case CasteSlot.Apparel: return game?.outfitDatabase?.DefaultOutfit();
|
|
case CasteSlot.Food: return game?.foodRestrictionDatabase?.DefaultFoodRestriction();
|
|
case CasteSlot.Drug: return game?.drugPolicyDatabase?.DefaultDrugPolicy();
|
|
case CasteSlot.Reading: return game?.readingPolicyDatabase?.DefaultReadingPolicy();
|
|
default: return null;
|
|
}
|
|
}
|
|
|
|
private static void EndLeftMemberships()
|
|
{
|
|
if (left.Count == 0)
|
|
return;
|
|
if (leftGame != Current.Game)
|
|
{
|
|
left.Clear();
|
|
return;
|
|
}
|
|
List<Pawn> pawns = left.ToList();
|
|
left.Clear();
|
|
foreach (Pawn pawn in pawns)
|
|
{
|
|
try
|
|
{
|
|
if (!GameComponent_BlaxCastes.IsGone(pawn) && !pawn.Dead && !CasteUtility.IsPlayerSide(pawn))
|
|
CasteManager.EndMembership(pawn);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
JoinHooks.LogOnce("Ending a membership", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>One message per caste per tick, so buying ten slaves does not post ten messages.</summary>
|
|
private static void SendMessages(Dictionary<Caste, List<Pawn>> joined)
|
|
{
|
|
if (BlaxCastesMod.Settings != null && !BlaxCastesMod.Settings.joinMessages)
|
|
return;
|
|
foreach (KeyValuePair<Caste, List<Pawn>> kv in joined)
|
|
{
|
|
List<Pawn> pawns = kv.Value;
|
|
string text = MessageText(kv.Key, pawns);
|
|
if (!text.NullOrEmpty())
|
|
Messages.Message(text, new LookTargets(pawns), MessageTypeDefOf.NeutralEvent, historical: false);
|
|
}
|
|
}
|
|
|
|
/// <summary>The join message for pawns that joined one caste.</summary>
|
|
public static string MessageText(Caste caste, List<Pawn> pawns)
|
|
{
|
|
if (caste == null || pawns == null || pawns.Count == 0)
|
|
return null;
|
|
if (pawns.Count == 1)
|
|
return "BlaxCastes_JoinedCaste".Translate(pawns[0].Named("PAWN"), caste.label.Named("CASTE"));
|
|
string names = pawns.Select(p => p.LabelShort).ToCommaList(useAnd: true);
|
|
return "BlaxCastes_JoinedCasteMany".Translate(pawns.Count.Named("COUNT"), caste.label.Named("CASTE"), names.Named("PAWNS"));
|
|
}
|
|
}
|
|
}
|