rimworld-blax-laws/Source/BlaxLaws/Enforcement/PunishmentDef.cs

109 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using Verse;
namespace BlaxLaws
{
/// <summary>
/// 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.
/// </summary>
public class PunishmentDef : Def
{
public Type workerClass = typeof(PunishmentWorker);
/// <summary>Dropdown order; lower first.</summary>
public int order;
/// <summary>The row's Days field applies (Guilty, Jail, Subdue then jail).</summary>
public bool usesDays;
/// <summary>
/// Never replaces or stops what a case already runs, and never becomes the case's active punishment
/// (Warn, Guilty: "Warn and Guilty run alongside").
/// </summary>
public bool runsAlongside;
public List<PunishmentFit> fits = new List<PunishmentFit>();
[Unsaved]
private PunishmentWorker workerInt;
public PunishmentWorker Worker
{
get
{
if (workerInt == null)
{
workerInt = (PunishmentWorker)Activator.CreateInstance(workerClass ?? typeof(PunishmentWorker));
workerInt.def = this;
}
return workerInt;
}
}
/// <summary>The entry kind and stage a row has, as fits names them.</summary>
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<string> 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";
}
}
/// <summary>
/// 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.
/// </summary>
public class PunishmentWorker
{
public PunishmentDef def;
/// <summary>
/// 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.
/// </summary>
public virtual void Start(LawCase c, Law law, LawStage stage)
{
}
/// <summary>
/// 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.
/// </summary>
public virtual void Sweep(LawCase c)
{
}
/// <summary>Every 2500 ticks while the case is Jailed under this punishment: the sentence check.</summary>
public virtual void SentenceSweep(LawCase c)
{
}
/// <summary>Ends this punishment's running job, because a later stage takes over or the law was deleted. Keeps LawCase.enforcer.</summary>
public virtual void Stop(LawCase c)
{
}
}
}