82 lines
3.9 KiB
C#
82 lines
3.9 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using System.Runtime.CompilerServices;
|
||
|
|
using RimWorld;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxLaws
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Letters, only when a colonist is jailed or the player has to act: no prisoner bed, nobody to enforce,
|
||
|
|
/// a role holder or mechanitor waiting for "Arrest now" (setting "Letters for jailings"). Vanilla letter
|
||
|
|
/// defs only: Laws adds no Letter class and no LetterDef, so nothing of Laws enters the letter archive's
|
||
|
|
/// save (design: "Data model").
|
||
|
|
/// </summary>
|
||
|
|
public static class CaseLetters
|
||
|
|
{
|
||
|
|
public const string KindNoEnforcer = "NoEnforcer";
|
||
|
|
public const string KindNoBed = "NoBed";
|
||
|
|
public const string KindHold = "Hold";
|
||
|
|
|
||
|
|
// Letters of each kind already sent per case in this session. Callers also send only when the
|
||
|
|
// case's saved wait reason changes, so a reload doesn't repeat a letter for the same wait.
|
||
|
|
private static readonly ConditionalWeakTable<LawCase, HashSet<string>> sent = new ConditionalWeakTable<LawCase, HashSet<string>>();
|
||
|
|
|
||
|
|
/// <summary>True the first time per case and kind in this session: one letter per wait, even when a case flips between waits.</summary>
|
||
|
|
public static bool Once(LawCase c, string kind)
|
||
|
|
{
|
||
|
|
if (c == null || kind == null)
|
||
|
|
return false;
|
||
|
|
return sent.GetOrCreateValue(c).Add(kind);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Sends the letter when "Letters for jailings" is on. The keys take the args as {0}, {1}, ...</summary>
|
||
|
|
public static void Send(LawCase c, string labelKey, string textKey, LetterDef def, params NamedArgument[] args)
|
||
|
|
{
|
||
|
|
if (!BlaxLawsMod.S.jailLetters || Find.LetterStack == null)
|
||
|
|
return;
|
||
|
|
LookTargets targets = c?.offender != null && c.offender.Spawned ? new LookTargets(c.offender) : LookTargets.Invalid;
|
||
|
|
Find.LetterStack.ReceiveLetter(labelKey.Translate(args), textKey.Translate(args), def ?? LetterDefOf.NeutralEvent, targets);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>A letter when letters are on, else a case message (Messages for cases).</summary>
|
||
|
|
public static void SendOrPost(LawCase c, string labelKey, string textKey, LetterDef def, params NamedArgument[] args)
|
||
|
|
{
|
||
|
|
if (BlaxLawsMod.S.jailLetters)
|
||
|
|
Send(c, labelKey, textKey, def, args);
|
||
|
|
else
|
||
|
|
CaseMessages.Post(c, textKey.Translate(args));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// "Jailed: Bob" at the capture: the act, the sentence and the release day. A case message instead
|
||
|
|
/// while "Letters for jailings" is off.
|
||
|
|
/// </summary>
|
||
|
|
public static void Jailed(LawCase c)
|
||
|
|
{
|
||
|
|
if (c == null)
|
||
|
|
return;
|
||
|
|
int now = Find.TickManager?.TicksGame ?? 0;
|
||
|
|
int left = c.releaseTick >= 0 ? c.releaseTick - now : JailFlow.SentenceTicks(c);
|
||
|
|
SendOrPost(c, "BlaxLaws_LetterJailedLabel", "BlaxLaws_LetterJailedText", LetterDefOf.NeutralEvent,
|
||
|
|
c.OffenderLabel, left.ToStringTicksToPeriod(allowSeconds: false, shortForm: false, canUseDecimals: false), c.Label, Sentences.ReleaseDate(c));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The letter that the case waits because nobody can ever enforce it.</summary>
|
||
|
|
public static void NoEnforcer(LawCase c) =>
|
||
|
|
Send(c, "BlaxLaws_LetterNoEnforcerLabel", "BlaxLaws_LetterNoEnforcerText", LetterDefOf.NegativeEvent, c.OffenderLabel, c.Label);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Vanilla guilt (Pawn_GuiltTracker.cs:46-61), saved and counted down by the game.</summary>
|
||
|
|
public static class Guilt
|
||
|
|
{
|
||
|
|
/// <summary>Guilty for at least these ticks. A longer guilt already on the pawn is never shortened.</summary>
|
||
|
|
public static void AtLeast(Pawn p, int ticks)
|
||
|
|
{
|
||
|
|
if (p?.guilt == null || ticks <= 0)
|
||
|
|
return;
|
||
|
|
if (p.guilt.TicksUntilInnocent < ticks)
|
||
|
|
p.guilt.Notify_Guilty(ticks);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|