202 lines
8.5 KiB
C#
202 lines
8.5 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using RimWorld;
|
||
|
|
using RimWorld.Planet;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxLaws
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// The logic behind the Cases tab (design: "Lawbook tab", Cases): cases newest first with who, what,
|
||
|
|
/// stage, when, punishment and status, the jump to the pawn, and "Arrest now" for a held case. Reads
|
||
|
|
/// the component only; "Arrest now" goes through Sentences.
|
||
|
|
/// </summary>
|
||
|
|
public static class CaseLog
|
||
|
|
{
|
||
|
|
/// <summary>Cases newest first, as a new list (see FillRows). Closed ones only when asked.</summary>
|
||
|
|
public static List<LawCase> Rows(bool includeClosed)
|
||
|
|
{
|
||
|
|
var rows = new List<LawCase>();
|
||
|
|
FillRows(rows, includeClosed);
|
||
|
|
return rows;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Fills the list with the cases newest first; among cases of one tick the higher severity first, then the
|
||
|
|
/// higher id. Closed ones only when asked. The Cases tab calls it on every GUI event with a list it keeps,
|
||
|
|
/// so it allocates nothing and sorts nothing it doesn't have to: cases are added in the order they open, so
|
||
|
|
/// walking them backwards is already in order, and the insertion sort only moves cases of one tick.
|
||
|
|
/// </summary>
|
||
|
|
public static void FillRows(List<LawCase> into, bool includeClosed)
|
||
|
|
{
|
||
|
|
if (into == null)
|
||
|
|
return;
|
||
|
|
into.Clear();
|
||
|
|
GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance;
|
||
|
|
if (comp == null)
|
||
|
|
return;
|
||
|
|
IReadOnlyList<LawCase> all = comp.Cases;
|
||
|
|
for (int i = all.Count - 1; i >= 0; i--)
|
||
|
|
{
|
||
|
|
LawCase c = all[i];
|
||
|
|
if (c == null || (!includeClosed && c.IsClosed))
|
||
|
|
continue;
|
||
|
|
int j = into.Count - 1;
|
||
|
|
into.Add(c);
|
||
|
|
while (j >= 0 && ShowsBefore(c, into[j]))
|
||
|
|
{
|
||
|
|
into[j + 1] = into[j];
|
||
|
|
j--;
|
||
|
|
}
|
||
|
|
into[j + 1] = c;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static bool ShowsBefore(LawCase a, LawCase b)
|
||
|
|
{
|
||
|
|
if (a.openTick != b.openTick)
|
||
|
|
return a.openTick > b.openTick;
|
||
|
|
if (a.severity != b.severity)
|
||
|
|
return a.severity > b.severity;
|
||
|
|
return a.id > b.id;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The offender's name, with the victim when the case has one.</summary>
|
||
|
|
public static string Who(LawCase c)
|
||
|
|
{
|
||
|
|
if (c == null)
|
||
|
|
return "";
|
||
|
|
string victim = c.VictimLabel;
|
||
|
|
return victim.NullOrEmpty() ? c.OffenderLabel : "BlaxLaws_CaseWhoVictim".Translate(c.OffenderLabel, victim).ToString();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The row's action, the def that matched when it differs, the count and the notes.</summary>
|
||
|
|
public static string What(LawCase c)
|
||
|
|
{
|
||
|
|
if (c == null)
|
||
|
|
return "";
|
||
|
|
Law law = c.CurrentLaw;
|
||
|
|
string text = law != null ? LawbookUi.ActionLabel(law) : LawbookUi.CategoryLabel(ActKey.CategoryOf(c.actKey)) + ": " + (ActKey.NameOf(c.actKey) ?? c.actKey);
|
||
|
|
string matched = MatchedLabel(c);
|
||
|
|
if (!matched.NullOrEmpty() && (law == null || !string.Equals(matched, law.Label, System.StringComparison.OrdinalIgnoreCase)))
|
||
|
|
text += " (" + matched + ")";
|
||
|
|
if (c.count > 1)
|
||
|
|
text += ", " + "BlaxLaws_CaseCount".Translate(c.count);
|
||
|
|
if ((c.notes & CaseNotes.OnTable) != 0)
|
||
|
|
text += ", " + "BlaxLaws_Note_OnTable".Translate();
|
||
|
|
if ((c.notes & CaseNotes.DugUp) != 0)
|
||
|
|
text += ", " + "BlaxLaws_Note_DugUp".Translate();
|
||
|
|
return text;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The label of the def that matched (which drug, which recipe, which break), or null.</summary>
|
||
|
|
public static string MatchedLabel(LawCase c)
|
||
|
|
{
|
||
|
|
string name = c?.matchedDef ?? c?.breakState;
|
||
|
|
if (name.NullOrEmpty())
|
||
|
|
return null;
|
||
|
|
string category = ActKey.CategoryOf(c.actKey);
|
||
|
|
ActEntry e = ActCatalogue.Get(ActKey.Of(category ?? "", name));
|
||
|
|
if (e != null)
|
||
|
|
return e.label;
|
||
|
|
// A dose during a binge matched an Ate def, not a Break def.
|
||
|
|
foreach (ActCategoryDef cat in ActCatalogue.Categories)
|
||
|
|
{
|
||
|
|
e = ActCatalogue.Get(ActKey.Of(cat.defName, name));
|
||
|
|
if (e != null)
|
||
|
|
return e.label;
|
||
|
|
}
|
||
|
|
return name;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string Stage(LawCase c) => c == null ? "" : LawLabels.Stage(c.stage);
|
||
|
|
|
||
|
|
/// <summary>"3 days ago", from the last act of the case.</summary>
|
||
|
|
public static string When(LawCase c)
|
||
|
|
{
|
||
|
|
if (c == null || Find.TickManager == null)
|
||
|
|
return "";
|
||
|
|
int ago = Find.TickManager.TicksGame - c.lastActTick;
|
||
|
|
if (ago < GenDate.TicksPerHour)
|
||
|
|
return "BlaxLaws_JustNow".Translate();
|
||
|
|
return "BlaxLaws_Ago".Translate(ago.ToStringTicksToPeriod(allowSeconds: false, shortForm: false, canUseDecimals: false));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The punishment the case runs or ran, with its days.</summary>
|
||
|
|
public static string Punishment(LawCase c)
|
||
|
|
{
|
||
|
|
if (c == null)
|
||
|
|
return "";
|
||
|
|
PunishmentDef active = Enforcement.ActiveOf(c);
|
||
|
|
Law law = c.LawFor(c.activeStage) ?? c.CurrentLaw;
|
||
|
|
PunishmentDef p = active ?? law?.PunishmentDef;
|
||
|
|
if (p == null)
|
||
|
|
return law?.punishment ?? "";
|
||
|
|
string text = p.LabelCap;
|
||
|
|
if (p.usesDays && law != null)
|
||
|
|
text += ", " + "BlaxLaws_DaysCount".Translate(law.days);
|
||
|
|
return text;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The status, with the outcome of a closed case, the countdown of a jailed one or why it waits.</summary>
|
||
|
|
public static string Status(LawCase c)
|
||
|
|
{
|
||
|
|
if (c == null)
|
||
|
|
return "";
|
||
|
|
string status = LawLabels.Status(c.status);
|
||
|
|
if (c.IsClosed)
|
||
|
|
return c.outcome.NullOrEmpty() ? status : CaseOutcomes.Label(c.outcome);
|
||
|
|
if (c.status == CaseStatus.Jailed && c.releaseTick >= 0 && Find.TickManager != null)
|
||
|
|
{
|
||
|
|
int left = c.releaseTick - Find.TickManager.TicksGame;
|
||
|
|
return left > 0
|
||
|
|
? "BlaxLaws_ReleaseIn".Translate(left.ToStringTicksToPeriod(allowSeconds: false, shortForm: false, canUseDecimals: false)).ToString()
|
||
|
|
: "BlaxLaws_ReleaseDue".Translate().ToString();
|
||
|
|
}
|
||
|
|
string wait = WaitReasons.Label(c.waitReason);
|
||
|
|
return wait.NullOrEmpty() ? status : status + ": " + wait;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// "Arrest now" shows for a case that waits on the hold: the jail of a role holder or a mechanitor with
|
||
|
|
/// mechs, which Laws doesn't arrest on its own (design: "Jail for N days", hold).
|
||
|
|
/// </summary>
|
||
|
|
public static bool CanArrestNow(LawCase c) =>
|
||
|
|
c != null && c.offender != null && !LawCase.IsGone(c.offender) && Sentences.CanArrestNow(c);
|
||
|
|
|
||
|
|
/// <summary>Why the case is held, for the button's tooltip.</summary>
|
||
|
|
public static string HoldReason(Pawn p) => JailFlow.HoldReason(p);
|
||
|
|
|
||
|
|
/// <summary>The "Arrest now" button: renews guilt and starts the arrest (Sentences.ArrestNow).</summary>
|
||
|
|
public static void ArrestNow(LawCase c)
|
||
|
|
{
|
||
|
|
if (!CanArrestNow(c))
|
||
|
|
return;
|
||
|
|
HookGuard.Run("Arrest now", () => Sentences.ArrestNow(c));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Whether the jump button can show the offender: alive or dead, on a map, carried, in a caravan.</summary>
|
||
|
|
public static bool CanJump(LawCase c)
|
||
|
|
{
|
||
|
|
Pawn p = c?.offender;
|
||
|
|
if (p == null || LawCase.IsGone(p))
|
||
|
|
return false;
|
||
|
|
return CameraJumper.CanJump(Target(p));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The jump button: selects the offender (its corpse when dead) the vanilla way.</summary>
|
||
|
|
public static void JumpTo(LawCase c)
|
||
|
|
{
|
||
|
|
if (!CanJump(c))
|
||
|
|
return;
|
||
|
|
HookGuard.Run("Jump to offender", () => CameraJumper.TryJumpAndSelect(Target(c.offender)));
|
||
|
|
}
|
||
|
|
|
||
|
|
private static GlobalTargetInfo Target(Pawn p)
|
||
|
|
{
|
||
|
|
if (p.Dead && p.Corpse != null)
|
||
|
|
return p.Corpse;
|
||
|
|
return p;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|