Blax Laws 1.0.0: tests for every shipped claim, escape and custody fixes
- Tests for the end-to-end subdue and jail, halved pain, the role hold,
every way a sentence ends, pruning, the nobody-to-enforce letter and
performance; each fails when its feature is broken.
- A jailed colonist that escapes closes as Escaped, also when the world
deletes it before the sentence check or a save falls in between. One
sold, given away, kidnapped or lost with a map closes with its own
message instead of "escaped".
- The release fallback also frees a prisoner on a caravan.
- An arrest waits while the offender carries someone, and ends an
offender job that holds reservations, so no reservation errors.
- Release text says a colonist, not a warden: Basic work also releases
prisoners.
- Test harness: test order, fixture hardening, roof cleanup and logging.
2026-12-03 14:46:28 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using BlaxLaws ;
using RimWorld ;
using Verse ;
namespace BlaxLawsTests
{
/// <summary>
/// Subdue on a pawn whose pain is halved (design: "Subdue", Tests: "one with halved pain needs twice the
/// hits"). A normal adult goes into pain shock at 0.8 (Pawn_HealthTracker.cs:95-104), about 64 points of
/// bruises at 0.0125 pain per point (Hediffs_Local_Injuries.xml:395, Hediff_Injury.cs:128-139). Halved
/// pain doubles that to about 128, which still fits under the subdue's total cap of nine tenths of the
/// lethal sum, 135 on a human (SubdueHits.LethalShare; Pawn_HealthTracker.cs:73, 705-719). So the halved
/// pawn is the one that gets close to the cap before it goes down.
///
/// Core halves pain with a yayo or flake high (painFactor 0.5, Psychite_Yayo.xml:107); Biotech with the
/// reduced pain gene (GeneDefs_Spectrum.xml:724). The pain factors multiply into PainTotal
/// (HediffSet.cs:1328-1365).
/// </summary>
public static class SubduePainTests
{
/// <summary>The halved pawn's subdue needs about twice the hits of a normal one (about 1000 ticks).</summary>
public const int HalvedSubdueTicks = 2 * EnforceFixtures . SubdueTicks ;
/// <summary>Points to down a halved-pain pawn over points to down a normal one: about 128 over 64 to 72.</summary>
public const float MinPointsRatio = 1.7f ;
/// <summary>Hits to down a halved-pain pawn over hits to down a normal one: about 21 over 11.</summary>
public const float MinHitsRatio = 1.4f ;
public const int MaxDirectHits = 300 ;
// ---- fixtures ----------------------------------------------------------------------
/// <summary>
/// A spawned adult colonist with normal pain (EnforceFixtures.Offender) and nothing that moves where it
/// goes down: no injury, scar, missing or added part, no pain at all, the base pain shock threshold
/// and a health scale of 1. An old scar counts toward the total cap and carries less pain per point
/// than a bruise, so it would eat into the room a halved-pain pawn needs.
/// </summary>
public static Pawn Unhurt ( IntVec3 ? at = null )
{
float baseShock = StatDefOf . PainShockThreshold . defaultBaseValue ;
for ( int i = 0 ; i < 30 ; i + + )
{
Pawn p = EnforceFixtures . Offender ( at ) ;
// Old wounds and scars healed: an ideoligion with scarification gives every colonist it generates
// its scars (Ideology), so a world can have no unscarred colonist at all.
foreach ( Hediff_Injury old in p . health . hediffSet . hediffs . OfType < Hediff_Injury > ( ) . ToList ( ) )
p . health . RemoveHediff ( old ) ;
bool clean = ! p . health . hediffSet . hediffs . Any ( h = > h is Hediff_Injury | | h is Hediff_MissingPart | | h is Hediff_AddedPart ) & &
p . health . hediffSet . PainTotal < = 0.0001f & &
Math . Abs ( p . GetStatValue ( StatDefOf . PainShockThreshold ) - baseShock ) < 0.001f & &
! p . kindDef . ignoresPainShock & &
Math . Abs ( p . HealthScale - 1f ) < 0.001f ;
if ( clean )
return p ;
p . Destroy ( DestroyMode . Vanish ) ;
}
T . Fail ( "no generated colonist was unhurt with normal pain and the base pain shock threshold" ) ;
return null ;
}
/// <summary>Halves the pawn's pain with a full yayo high (Core).</summary>
public static void HalveWithYayo ( Pawn p )
{
Hediff high = HediffMaker . MakeHediff ( DefDatabase < HediffDef > . GetNamed ( "YayoHigh" ) , p ) ;
high . Severity = 1f ;
p . health . AddHediff ( high ) ;
CheckHalved ( p , "a yayo high" ) ;
}
/// <summary>Halves the pawn's pain with the reduced pain gene. Skips the test without Biotech.</summary>
public static void HalveWithGene ( Pawn p )
{
T . NeedsDlc ( "Biotech" , ModsConfig . BiotechActive ) ;
GeneDef gene = DefDatabase < GeneDef > . GetNamedSilentFail ( "Pain_Reduced" ) ;
T . NotNull ( gene , "the Pain_Reduced gene" ) ;
T . NotNull ( p . genes , "the pawn's genes" ) ;
p . genes . AddGene ( gene , xenogene : true ) ;
CheckHalved ( p , "the reduced pain gene" ) ;
}
private static void CheckHalved ( Pawn p , string how )
{
float factor = EnforceFixtures . PainFactor ( p ) ;
T . Check ( Math . Abs ( factor - 0.5f ) < 0.001f , "fixture: " + how + " gives a pain factor of " + factor . ToString ( "0.###" ) + ", not 0.5" ) ;
T . Check ( Math . Abs ( p . GetStatValue ( StatDefOf . PainShockThreshold ) - StatDefOf . PainShockThreshold . defaultBaseValue ) < 0.001f ,
"fixture: " + how + " changed the pain shock threshold" ) ;
}
/// <summary>Bruise points that put a pawn with this pain factor into pain shock.</summary>
public static float PointsToDown ( Pawn p , float painFactor ) = >
p . GetStatValue ( StatDefOf . PainShockThreshold ) * p . HealthScale / ( EnforceFixtures . Bruise . injuryProps . painPerSeverity * painFactor ) ;
/// <summary>The pawn's injuries, all of them: the fixtures start unhurt.</summary>
public static float InjurySum ( Pawn p ) = > p . health . hediffSet . hediffs . OfType < Hediff_Injury > ( ) . Sum ( h = > h . Severity ) ;
/// <summary>Nine tenths of the injury sum vanilla kills at (Pawn_HealthTracker.cs:73, 705-719).</summary>
public static float Cap ( Pawn p ) = > 150f * p . HealthScale * 0.9f ;
/// <summary>
/// Hits with SubdueHits.Hit until the pawn is in pain shock, without ticking. Bruised legs and feet can
/// down a pawn before that, when its Moving capacity drops under what standing needs; vanilla downs it
/// then, but the points it took say nothing about its pain, so the hits go on to the pain shock. Fails
/// when the subdue has nothing left to hit first. Returns the hits.
/// </summary>
public static int HitUntilDown ( Pawn enforcer , Pawn target , string who )
{
int hits = 0 ;
int downedAt = - 1 ;
while ( ! target . health . InPainShock & & hits < MaxDirectHits )
{
if ( SubdueHits . PickPart ( target , out _ ) = = null )
{
T . Fail ( who + ": nothing left to hit after " + hits + " hits at " + InjurySum ( target ) . ToString ( "0.###" ) + " points (cap " +
Cap ( target ) . ToString ( "0.###" ) + "), pain " + target . health . hediffSet . PainTotal . ToString ( "0.###" ) + ", not in pain shock" +
( downedAt > = 0 ? "; downed by hit " + downedAt + ", " + Capacities ( target ) : "" ) ) ;
}
float before = InjurySum ( target ) ;
SubdueHits . Hit ( enforcer , target ) ;
T . Check ( ! target . Dead , who + ": died on hit " + ( hits + 1 ) ) ;
T . Check ( InjurySum ( target ) > before , who + ": hit " + ( hits + 1 ) + " added no injury" ) ;
hits + + ;
if ( downedAt < 0 & & target . Downed & & ! target . health . InPainShock )
downedAt = hits ;
}
if ( downedAt > = 0 )
T . Info ( who + ": downed by hit " + downedAt + " before the pain shock (" + Capacities ( target ) + "); hit on to the pain shock" ) ;
T . Check ( target . Downed , who + ": not downed after " + hits + " hits" ) ;
T . Check ( target . health . InPainShock , who + ": not in pain shock after " + hits + " hits, pain " + target . health . hediffSet . PainTotal . ToString ( "0.###" ) +
" of " + target . GetStatValue ( StatDefOf . PainShockThreshold ) . ToString ( "0.###" ) ) ;
return hits ;
}
/// <summary>The capacities that can down a pawn besides pain: Moving and Consciousness.</summary>
private static string Capacities ( Pawn p ) = >
"Moving " + p . health . capacities . GetLevel ( PawnCapacityDefOf . Moving ) . ToString ( "0.###" ) +
", Consciousness " + p . health . capacities . GetLevel ( PawnCapacityDefOf . Consciousness ) . ToString ( "0.###" ) ;
/// <summary>Alive, bruises only, none on the head or neck, every hit part at half its HP or more (unrounded), the sum under the cap.</summary>
public static void CheckHeld ( Pawn p , HashSet < Hediff_Injury > before , string who )
{
T . Check ( ! p . Dead , who + ": dead" ) ;
List < Hediff_Injury > fresh = EnforceFixtures . NewInjuries ( p , before ) ;
T . Check ( fresh . Count > 0 , who + ": no injuries" ) ;
List < string > notBruises = fresh . Where ( h = > h . def ! = EnforceFixtures . Bruise ) . Select ( EnforceFixtures . Describe ) . ToList ( ) ;
T . Check ( notBruises . Count = = 0 , who + ": injuries that aren't bruises: " + string . Join ( "; " , notBruises ) ) ;
List < string > onHead = fresh . Where ( h = > h . Part = = null | | SubdueHits . InHeadOrNeck ( p , h . Part ) ) . Select ( EnforceFixtures . Describe ) . ToList ( ) ;
T . Check ( onHead . Count = = 0 , who + ": hits on the head or neck: " + string . Join ( "; " , onHead ) ) ;
var low = new List < string > ( ) ;
foreach ( BodyPartRecord part in fresh . Select ( h = > h . Part ) . Distinct ( ) )
{
float max = part . def . GetMaxHealth ( p ) ;
float hp = EnforceFixtures . PartHealth ( p , part ) ;
if ( hp < max * 0.5f - 0.001f )
low . Add ( part . Label + " at " + hp . ToString ( "0.###" ) + " of " + max ) ;
}
T . Check ( low . Count = = 0 , who + ": " + low . Count + " hit part(s) below half their HP: " + string . Join ( "; " , low ) ) ;
float sum = InjurySum ( p ) ;
T . Check ( sum < = Cap ( p ) + 0.001f , who + ": injuries add up to " + sum . ToString ( "0.###" ) + ", over " + Cap ( p ) . ToString ( "0.###" ) ) ;
}
/// <summary>
/// A normal and a halved-pain colonist hit directly until each is down; then the halved one is hit
/// until nothing is left. Ticks nothing, so no healing, no job of the pawn and no drug moves the numbers.
/// </summary>
private static void DirectCompare ( Action < Pawn > halve , string how )
{
Pawn enforcer = EnforceFixtures . Warden ( ) ;
Pawn normal = Unhurt ( EnforceFixtures . CellNear ( enforcer . Position , 2f , 12f ) ) ;
Pawn halved = Unhurt ( EnforceFixtures . CellNear ( enforcer . Position , 2f , 12f ) ) ;
halve ( halved ) ;
HashSet < Hediff_Injury > beforeNormal = EnforceFixtures . Injuries ( normal ) ;
HashSet < Hediff_Injury > beforeHalved = EnforceFixtures . Injuries ( halved ) ;
int normalHits = HitUntilDown ( enforcer , normal , "normal pain" ) ;
float normalPoints = InjurySum ( normal ) ;
int halvedHits = HitUntilDown ( enforcer , halved , how ) ;
float halvedPoints = InjurySum ( halved ) ;
T . Info ( how + ": down after " + halvedHits + " hits, " + halvedPoints . ToString ( "0.#" ) + " points; normal pain after " + normalHits +
" hits, " + normalPoints . ToString ( "0.#" ) + " points" ) ;
float normalDown = PointsToDown ( normal , 1f ) ;
T . Check ( normalPoints > = normalDown - 0.01f & & normalPoints < normalDown + SubdueHits . MaxDamage + 0.01f ,
"normal pain: downed at " + normalPoints . ToString ( "0.###" ) + " points, expected " + normalDown . ToString ( "0.#" ) + " plus at most one hit" ) ;
T . Check ( halvedPoints > = MinPointsRatio * normalPoints ,
how + ": downed at " + halvedPoints . ToString ( "0.###" ) + " points, under " + MinPointsRatio + " times the " + normalPoints . ToString ( "0.###" ) + " of normal pain" ) ;
T . Check ( halvedHits > = MinHitsRatio * normalHits ,
how + ": downed after " + halvedHits + " hits, under " + MinHitsRatio + " times the " + normalHits + " of normal pain" ) ;
// Down before the total cap: the cap leaves room for halved pain.
CheckHeld ( halved , beforeHalved , how + " when downed" ) ;
CheckHeld ( normal , beforeNormal , "normal pain when downed" ) ;
// The beating goes on until nothing is left to hit: the cap, not death, ends it.
int more = 0 ;
while ( more < MaxDirectHits & & SubdueHits . PickPart ( halved , out _ ) ! = null )
{
SubdueHits . Hit ( enforcer , halved ) ;
more + + ;
}
T . Check ( SubdueHits . PickPart ( halved , out _ ) = = null , how + ": parts still had room after " + more + " more hits" ) ;
CheckHeld ( halved , beforeHalved , how + " after " + more + " more hits" ) ;
// The part caps add up to about 235 on a human, so the total cap is what ends it, within a point.
T . Check ( InjurySum ( halved ) > = Cap ( halved ) - 1.001f ,
how + ": the beating ended at " + InjurySum ( halved ) . ToString ( "0.###" ) + " points, short of the total cap " + Cap ( halved ) . ToString ( "0.###" ) ) ;
}
// ---- direct hits -------------------------------------------------------------------
[BlaxTest("subdue pain: a colonist on a yayo high (pain halved) goes down by subdue hits after about twice the bruise points and clearly more hits than one with normal pain, before the nine-tenths cap; hit on, it stays alive, under the cap, every hit part at half its HP or more")]
public static void HalvedByYayoDirect ( )
{
DirectCompare ( HalveWithYayo , "a yayo high" ) ;
}
[BlaxTest("subdue pain: a colonist with the reduced pain gene goes down by subdue hits after about twice the bruise points and clearly more hits than one with normal pain, before the nine-tenths cap; hit on, it stays alive, under the cap, every hit part at half its HP or more (needs Biotech)")]
public static void HalvedByGeneDirect ( )
{
T . NeedsDlc ( "Biotech" , ModsConfig . BiotechActive ) ;
DirectCompare ( HalveWithGene , "the reduced pain gene" ) ;
}
// ---- the subdue job ----------------------------------------------------------------
private sealed class BingeRun
{
public LawCase c ;
public HashSet < Hediff_Injury > before ;
public int hits ;
}
/// <summary>
/// A binging offender, a warden beside it and a planning Subdue case queued; ticks until it is downed,
/// the case closes or the ticks run out. Counts the job's hits across every subdue driver the case's
/// enforcer runs; the downing hit ends the job inside its tick, so it is added when the pawn is down.
/// </summary>
private static BingeRun Binge ( Pawn offender , int ticks )
{
EnforceFixtures . Warden ( EnforceFixtures . CellNear ( offender . Position , 2f , 5f ) ) ;
var run = new BingeRun { before = EnforceFixtures . Injuries ( offender ) } ;
EnforceFixtures . ForceBreak ( offender , EnforceFixtures . DrugBinge , causedByMood : true ) ;
run . c = EnforceFixtures . BreakCase ( offender , "Subdue" , LawStage . Planning , 3 ) ;
Enforcement . Enqueue ( run . c , LawStage . Planning ) ;
int finished = 0 ;
JobDriver_Subdue last = null ;
int lastHits = 0 ;
EnforceFixtures . RunUntil ( ( ) = >
{
JobDriver_Subdue d = run . c . enforcer ? . jobs ? . curDriver as JobDriver_Subdue ;
if ( d ! = last )
{
finished + = lastHits ;
last = d ;
lastHits = 0 ;
}
if ( d ! = null )
lastHits = d . Hits ;
return offender . Downed | | run . c . IsClosed ;
} , ticks ) ;
run . hits = finished + lastHits + ( offender . Downed ? 1 : 0 ) ;
return run ;
}
[BlaxTest("subdue pain: the subdue job downs a binging colonist on a yayo high (pain halved) after about twice the bruise points and clearly more hits than a binger with normal pain, closes the case as subdued, every hit part at half its HP or more and the bruises under the nine-tenths cap")]
public static void HalvedByYayoJob ( )
{
// Only the subdue hits the offenders: no social fight and no random break of a bystander. No drug to
// binge on either: a smokeleaf dose cuts the halved pawn's pain to 0.45, which needs more points than the cap.
2026-12-03 15:32:47 +00:00
// No predator hunts a colonist: a wild arctic wolf once hunted the binger, the danger rightly called off the
// subdue, and the wolf killed it. Every kill is noted, and SubdueStory names injuries the subdue didn't make.
Blax Laws 1.0.0: tests for every shipped claim, escape and custody fixes
- Tests for the end-to-end subdue and jail, halved pain, the role hold,
every way a sentence ends, pruning, the nobody-to-enforce letter and
performance; each fails when its feature is broken.
- A jailed colonist that escapes closes as Escaped, also when the world
deletes it before the sentence check or a save falls in between. One
sold, given away, kidnapped or lost with a map closes with its own
message instead of "escaped".
- The release fallback also frees a prisoner on a caravan.
- An arrest waits while the offender carries someone, and ends an
offender job that holds reservations, so no reservation errors.
- Release text says a colonist, not a warden: Basic work also releases
prisoners.
- Test harness: test order, fixture hardening, roof cleanup and logging.
2026-12-03 14:46:28 +00:00
Fixtures . LogKillsDuringTest ( ) ;
2026-12-03 15:32:47 +00:00
EnforceFixtures . NoPredatorsHuntColonists ( ) ;
Blax Laws 1.0.0: tests for every shipped claim, escape and custody fixes
- Tests for the end-to-end subdue and jail, halved pain, the role hold,
every way a sentence ends, pruning, the nobody-to-enforce letter and
performance; each fails when its feature is broken.
- A jailed colonist that escapes closes as Escaped, also when the world
deletes it before the sentence check or a save falls in between. One
sold, given away, kidnapped or lost with a map closes with its own
message instead of "escaped".
- The release fallback also frees a prisoner on a caravan.
- An arrest waits while the offender carries someone, and ends an
offender job that holds reservations, so no reservation errors.
- Release text says a colonist, not a warden: Basic work also releases
prisoners.
- Test harness: test order, fixture hardening, roof cleanup and logging.
2026-12-03 14:46:28 +00:00
DetectionKit . QuietSocialFights ( ) ;
DetectionKit . ClearDrugsOnMap ( ) ;
BingeRun n = SubduedInPainShock ( ( ) = > Unhurt ( ) , EnforceFixtures . SubdueTicks , "normal pain" , out Pawn normal ) ;
float normalPoints = InjurySum ( normal ) ;
BingeRun h = SubduedInPainShock ( ( ) = >
{
Pawn p = Unhurt ( EnforceFixtures . CellNear ( Fixtures . PawnCell ( ) , 0f , 20f ) ) ;
HalveWithYayo ( p ) ;
return p ;
} , HalvedSubdueTicks , "yayo high" , out Pawn halved ) ;
float halvedPoints = InjurySum ( halved ) ;
T . Info ( "yayo high: down after " + h . hits + " hits, " + halvedPoints . ToString ( "0.#" ) + " points; normal pain after " + n . hits + " hits, " +
normalPoints . ToString ( "0.#" ) + " points" ) ;
T . Check ( halvedPoints > = MinPointsRatio * normalPoints ,
"yayo high: downed at " + halvedPoints . ToString ( "0.###" ) + " points, under " + MinPointsRatio + " times the " + normalPoints . ToString ( "0.###" ) + " of normal pain" ) ;
T . Check ( n . hits > 0 & & h . hits > = MinHitsRatio * n . hits ,
"yayo high: downed after " + h . hits + " hits, under " + MinHitsRatio + " times the " + n . hits + " of normal pain" ) ;
CheckHeld ( halved , h . before , "yayo high" ) ;
CheckHeld ( normal , n . before , "normal pain" ) ;
}
/// <summary>
/// A binger the subdue job downs and whose case closes as subdued, in pain shock. Bruised legs and feet can
/// down a pawn before its pain does (its Moving capacity drops under what standing needs), and then the
/// points it took say nothing about its pain: that pawn still has to be subdued and held, and the next
/// binger is tried, at most three.
/// </summary>
private static BingeRun SubduedInPainShock ( Func < Pawn > make , int ticks , string who , out Pawn pawn )
{
for ( int attempt = 1 ; attempt < = 3 ; attempt + + )
{
pawn = make ( ) ;
BingeRun run = Binge ( pawn , ticks ) ;
T . Check ( pawn . Downed , who + ": not downed within " + ticks + " ticks (dead " + pawn . Dead + ", hediffs " + string . Join ( " " , pawn . health . hediffSet . hediffs . Where ( x = > ! ( x is Hediff_Injury ) ) . Select ( x = > x . def . defName ) ) + "); " + EnforceFixtures . SubdueStory ( run . c , pawn , run . before ) ) ;
T . Check ( run . c . IsClosed & & run . c . outcome = = CaseOutcomes . Subdued , who + ": the case after the subdue: " + run . c ) ;
if ( pawn . health . InPainShock )
return run ;
CheckHeld ( pawn , run . before , who + ", downed before the pain shock" ) ;
T . Info ( who + ": binger " + attempt + " was downed before the pain shock (" + Capacities ( pawn ) + ", pain " +
pawn . health . hediffSet . PainTotal . ToString ( "0.###" ) + "); the next one is tried" ) ;
}
T . Fail ( who + ": three bingers in a row were downed before the pain shock" ) ;
pawn = null ;
return null ;
}
}
}