using System.Collections.Generic; using RimWorld; using RimWorld.Planet; using Verse; namespace BlaxLaws { /// /// 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. /// public static class CaseLog { /// Cases newest first, as a new list (see FillRows). Closed ones only when asked. public static List Rows(bool includeClosed) { var rows = new List(); FillRows(rows, includeClosed); return rows; } /// /// 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. /// public static void FillRows(List into, bool includeClosed) { if (into == null) return; into.Clear(); GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance; if (comp == null) return; IReadOnlyList 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; } /// The offender's name, with the victim when the case has one. 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(); } /// The row's action, the def that matched when it differs, the count and the notes. 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; } /// The label of the def that matched (which drug, which recipe, which break), or null. 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); /// "3 days ago", from the last act of the case. 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)); } /// The punishment the case runs or ran, with its days. 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; } /// The status, with the outcome of a closed case, the countdown of a jailed one or why it waits. 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; } /// /// "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). /// public static bool CanArrestNow(LawCase c) => c != null && c.offender != null && !LawCase.IsGone(c.offender) && Sentences.CanArrestNow(c); /// Why the case is held, for the button's tooltip. public static string HoldReason(Pawn p) => JailFlow.HoldReason(p); /// The "Arrest now" button: renews guilt and starts the arrest (Sentences.ArrestNow). public static void ArrestNow(LawCase c) { if (!CanArrestNow(c)) return; HookGuard.Run("Arrest now", () => Sentences.ArrestNow(c)); } /// Whether the jump button can show the offender: alive or dead, on a map, carried, in a caravan. public static bool CanJump(LawCase c) { Pawn p = c?.offender; if (p == null || LawCase.IsGone(p)) return false; return CameraJumper.CanJump(Target(p)); } /// The jump button: selects the offender (its corpse when dead) the vanilla way. 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; } } }