using System.Collections.Generic;
using System.Runtime.CompilerServices;
using RimWorld;
using Verse;
namespace BlaxLaws
{
///
/// 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").
///
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> sent = new ConditionalWeakTable>();
/// True the first time per case and kind in this session: one letter per wait, even when a case flips between waits.
public static bool Once(LawCase c, string kind)
{
if (c == null || kind == null)
return false;
return sent.GetOrCreateValue(c).Add(kind);
}
/// Sends the letter when "Letters for jailings" is on. The keys take the args as {0}, {1}, ...
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);
}
/// A letter when letters are on, else a case message (Messages for cases).
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));
}
///
/// "Jailed: Bob" at the capture: the act, the sentence and the release day. A case message instead
/// while "Letters for jailings" is off.
///
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));
}
/// The letter that the case waits because nobody can ever enforce it.
public static void NoEnforcer(LawCase c) =>
Send(c, "BlaxLaws_LetterNoEnforcerLabel", "BlaxLaws_LetterNoEnforcerText", LetterDefOf.NegativeEvent, c.OffenderLabel, c.Label);
}
/// Vanilla guilt (Pawn_GuiltTracker.cs:46-61), saved and counted down by the game.
public static class Guilt
{
/// Guilty for at least these ticks. A longer guilt already on the pawn is never shortened.
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);
}
}
}