using System; using System.Collections.Generic; using Verse; namespace BlaxLaws { /// /// A punishment (Defs/BlaxLaws_Punishments.xml). The defName is what a row saves, so it never changes. /// fits says which entry kinds and stages offer it; the worker carries it out. /// public class PunishmentDef : Def { public Type workerClass = typeof(PunishmentWorker); /// Dropdown order; lower first. public int order; /// The row's Days field applies (Guilty, Jail, Subdue then jail). public bool usesDays; /// /// Never replaces or stops what a case already runs, and never becomes the case's active punishment /// (Warn, Guilty: "Warn and Guilty run alongside"). /// public bool runsAlongside; public List fits = new List(); [Unsaved] private PunishmentWorker workerInt; public PunishmentWorker Worker { get { if (workerInt == null) { workerInt = (PunishmentWorker)Activator.CreateInstance(workerClass ?? typeof(PunishmentWorker)); workerInt.def = this; } return workerInt; } } /// The entry kind and stage a row has, as fits names them. public static PunishmentFit FitOf(ActEntry entry, LawStage stage) { if (entry == null || !entry.IsBreak) return PunishmentFit.Act; if (entry.hostileBreak) return stage == LawStage.Planning ? PunishmentFit.HostileBreakPlanning : PunishmentFit.HostileBreakDidIt; return stage == LawStage.Planning ? PunishmentFit.BreakPlanning : PunishmentFit.BreakDidIt; } public bool Fits(ActEntry entry, LawStage stage) => entry != null && entry.HasStage(stage) && fits != null && fits.Contains(FitOf(entry, stage)); public override IEnumerable ConfigErrors() { foreach (string e in base.ConfigErrors()) yield return e; if (workerClass == null || !typeof(PunishmentWorker).IsAssignableFrom(workerClass)) yield return "workerClass must be a PunishmentWorker"; if (fits == null || fits.Count == 0) yield return "fits names no entry kind"; } } /// /// Carries out a punishment. Enforcement calls Start from the queue a tick after the act was filed, /// never inside a hook, and never while "Enforce laws" is off. Every call is wrapped by Enforcement in /// a HookGuard, so a throw logs once and the game goes on. /// /// One enforcement per case: when a case's active punishment is a different, unfinished one and the new /// one doesn't run alongside, Enforcement calls the old worker's Stop first (it ends its job and keeps /// LawCase.enforcer), then the new worker's Start, which takes over with the same enforcer. /// public class PunishmentWorker { public PunishmentDef def; /// /// Starts the punishment for the stage. Set the case's status (Enforcing, Jailed, or close it with /// GameComponent_BlaxLaws.CloseCase), its enforcer and waitReason as needed. Default: nothing. /// public virtual void Start(LawCase c, Law law, LawStage stage) { } /// /// Every 250 ticks while this is the case's active punishment and the case isn't closed: retry a wait /// (no bed, no enforcer, danger), re-issue a lost job, hand over to a capture, give an old bed back. /// public virtual void Sweep(LawCase c) { } /// Every 2500 ticks while the case is Jailed under this punishment: the sentence check. public virtual void SentenceSweep(LawCase c) { } /// Ends this punishment's running job, because a later stage takes over or the law was deleted. Keeps LawCase.enforcer. public virtual void Stop(LawCase c) { } } }