PhysiologyPlans.Humanoid: tissue stacks, blood, blood-loss stages, Cut and Bruise for the 19-part body

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-25 20:48:20 +01:00
commit bbaf8af493
2 changed files with 178 additions and 0 deletions

View file

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
namespace Lodur
{
/// <summary>Physiology content in code, until it lives in a store; the only place tissue, organ and affliction labels are spelled.</summary>
public static class PhysiologyPlans
{
private const double Day = 86400.0;
// Vascularity is the blood a wound in the tissue loses relative to muscle (play-tuned placeholders): skin and bone are thin, organs rich.
private const double SkinV = 0.2, MuscleV = 1.0, BoneV = 0.3, BrainV = 1.0, HeartV = 4.0, LungV = 2.0, LiverV = 3.0, KidneyV = 2.0, GutV = 1.0;
private static TissueDef Skin(double hp) => new TissueDef("Skin", hp, SkinV);
private static TissueDef Muscle(double hp) => new TissueDef("Muscle", hp, MuscleV, Motor: 1.0);
private static TissueDef Bone(string label, double hp) => new TissueDef(label, hp, BoneV);
private static Effect Curve(EffectTarget target, EffectOp op, EffectDriver driver, params (double X, double Y)[] curve) => new Effect(target, op, driver, curve);
/// <summary>
/// The 19-part humanoid of <see cref="BodyPlans.Humanoid"/>: tissue stacks outside-in with RimWorld's human part HP, the
/// blood, the stages of blood loss, and the Cut and Bruise afflictions. Numbers not cited are play-tuned placeholders.
/// </summary>
public static PhysiologyPlan Humanoid()
{
var parts = new List<PartTissuesDef>();
void Stack(string part, Side side, params TissueDef[] tissues) => parts.Add(new PartTissuesDef(part, side, tissues));
Stack("Pelvis", Side.Center, Skin(10), Muscle(30), Bone("Pelvis", 25), new TissueDef("Gut", 20, GutV));
Stack("Torso", Side.Center,
Skin(10), Muscle(40), Bone("Ribcage", 25),
new TissueDef("Heart", 15, HeartV, Vital: true, BloodPumping: 1.0),
new TissueDef("LungLeft", 15, LungV), new TissueDef("LungRight", 15, LungV),
new TissueDef("Liver", 20, LiverV), new TissueDef("KidneyLeft", 15, KidneyV), new TissueDef("KidneyRight", 15, KidneyV));
// The neck's muscle carries the Neck joint's motor weight: the head has to be a joint child with a muscle (spec discrepancy, report deviation).
Stack("Head", Side.Center, Skin(5), Muscle(25), Bone("Skull", 25), new TissueDef("Brain", 10, BrainV, Vital: true, Consciousness: 1.0));
foreach (var side in new[] { Side.Left, Side.Right })
{
Stack("UpperArm", side, Skin(8), Muscle(30), Bone("Humerus", 25));
Stack("LowerArm", side, Skin(8), Muscle(25), Bone("Radius", 20));
Stack("Thigh", side, Skin(10), Muscle(30), Bone("Femur", 25));
Stack("Shin", side, Skin(8), Muscle(25), Bone("Tibia", 25));
Stack("Foot", side, Skin(5), Muscle(20), Bone("Bones", 25));
Stack("Palm", side, Skin(5), Muscle(20), Bone("Metacarpals", 15));
Stack("Thumb", side, Skin(3), Muscle(8), Bone("Bone", 8));
Stack("Fingers", side, Skin(3), Muscle(8), Bone("Bone", 8));
}
// RimWorld's blood-loss stages (research rw-blood-bleeding) as step offsets on consciousness, by fraction of the lethal loss.
var bloodLoss = Curve(EffectTarget.Consciousness, EffectOp.Offset, EffectDriver.BloodLoss,
(0.0, 0.0), (0.15, 0.0), (0.15, -0.1), (0.30, -0.1), (0.30, -0.2), (0.45, -0.2), (0.45, -0.4), (0.60, -0.4), (0.60, -0.9), (1.0, -0.9));
var afflictions = new[]
{
// 1 mL/s per HP of severity (play-tuned), clotting with age (play-tuned); heals 10 HP per slow day (RimWorld).
new AfflictionDef("Cut", PhysiologyClock.Slow, -10.0 / Day, true, new[]
{
Curve(EffectTarget.Bleed, EffectOp.Offset, EffectDriver.Severity, (0.0, 0.0), (100.0, 1e-4)),
Curve(EffectTarget.Bleed, EffectOp.Factor, EffectDriver.Age, (0.0, 1.0), (60.0, 0.5), (300.0, 0.1)),
}),
new AfflictionDef("Bruise", PhysiologyClock.Slow, -20.0 / Day, true, Array.Empty<Effect>()),
};
// ATLS class IV past 40% loss (research df-and-atls) puts the lethal fraction at 0.5 (play-tuned); regrowth is RimWorld's 0.333 per slow day.
return new PhysiologyPlan(parts, afflictions, new[] { bloodLoss }, new BloodDef(0.005, 0.5, 0.333 / Day), 64);
}
}
}

View file

@ -0,0 +1,110 @@
using Lodur;
namespace Lodur.Core.Tests;
/// <summary>The humanoid's physiology content resolves against the humanoid body and behaves as the content says.</summary>
public sealed class PhysiologyPlansTests
{
private static readonly BodyPlan Body = BodyPlans.Humanoid();
private static Physiology Spawn() => new(Body, PhysiologyPlans.Humanoid(), 20);
private static int Cut => PhysiologyPlans.Humanoid().AfflictionIndex("Cut");
private static int Bruise => PhysiologyPlans.Humanoid().AfflictionIndex("Bruise");
[Fact]
public void The_humanoid_resolves_and_spawns_whole()
{
var p = Spawn();
Assert.Equal(Body.Joints.Count, p.JointCount);
Assert.Equal(1.0, p.Consciousness);
Assert.Equal(1.0, p.BloodPumping);
Assert.Equal(0.005, p.BloodVolume);
for (var j = 0; j < p.JointCount; j++) Assert.Equal(1.0, p.MotorCeiling(j));
Assert.False(p.Dead);
}
[Fact]
public void The_brain_on_the_head_and_the_heart_on_the_torso_are_vital_and_a_skull_is_not()
{
var head = Body.PartIndex("Head");
var torso = Body.PartIndex("Torso");
var brain = Spawn();
Assert.True(brain.Afflict(Cut, head, brain.TissueIndex(head, "Brain"), 11));
brain.Step(0.01);
Assert.Equal(DeathCause.VitalTissue, brain.Cause);
var heart = Spawn();
Assert.True(heart.Afflict(Cut, torso, heart.TissueIndex(torso, "Heart"), 16));
heart.Step(0.01);
Assert.Equal(DeathCause.VitalTissue, heart.Cause);
var skull = Spawn();
Assert.True(skull.Afflict(Bruise, head, skull.TissueIndex(head, "Skull"), 26));
skull.Step(0.01);
Assert.False(skull.Dead);
Assert.Equal(0.0, skull.Integrity(head, skull.TissueIndex(head, "Skull")));
var lung = Spawn();
Assert.True(lung.Afflict(Bruise, torso, lung.TissueIndex(torso, "LungLeft"), 16));
lung.Step(0.01);
Assert.False(lung.Dead); // an organ with no vital flag yet; the airway cut gives lungs their function
}
[Fact]
public void Every_side_of_every_limb_has_its_own_stack()
{
var p = Spawn();
foreach (var side in new[] { Side.Left, Side.Right })
{
var shin = Body.PartIndex("Shin", side);
Assert.True(p.Afflict(Bruise, shin, p.TissueIndex(shin, "Tibia"), 26));
}
p.Step(0.01);
Assert.Equal(0.0, p.Integrity(Body.PartIndex("Shin", Side.Left), p.TissueIndex(Body.PartIndex("Shin", Side.Left), "Tibia")));
Assert.Equal(0.0, p.Integrity(Body.PartIndex("Shin", Side.Right), p.TissueIndex(Body.PartIndex("Shin", Side.Right), "Tibia")));
Assert.Equal(1.0, p.Integrity(Body.PartIndex("Thigh", Side.Left), 0));
}
[Fact]
public void Blood_loss_stages_drop_consciousness_by_the_ruled_steps()
{
var p = Spawn();
var torso = Body.PartIndex("Torso");
var muscle = p.TissueIndex(torso, "Muscle");
for (var i = 0; i < 3; i++) Assert.True(p.Afflict(Cut, torso, muscle, 100)); // 3 x 100 HP of cut: a wound far past the muscle's 40
void ReachAndRead(double loss, double expected)
{
for (var i = 0; i < 100000 && p.BloodLoss < loss; i++) p.Step(0.01);
Assert.False(p.Dead);
Assert.True(p.BloodLoss >= loss && p.BloodLoss < loss + 0.05, $"loss {p.BloodLoss}");
p.Step(0.01); // the next step reads the blood the last one left
Assert.Equal(expected, p.Consciousness, 12);
}
// 0.5 x 0.16 of the nominal volume lost reads the 0.15 stage; 0.31 reads the 0.30 stage.
ReachAndRead(0.16, 0.9);
ReachAndRead(0.31, 0.8);
}
[Fact]
public void A_cut_bleeds_and_clots_and_a_bruise_does_not_bleed()
{
var p = Spawn();
var torso = Body.PartIndex("Torso");
var muscle = p.TissueIndex(torso, "Muscle");
Assert.True(p.Afflict(Bruise, torso, muscle, 40));
p.Step(0.01);
Assert.Equal(0.0, p.Bleed(torso));
Assert.True(p.Afflict(Cut, torso, muscle, 50));
p.Step(0.01);
var fresh = p.Bleed(torso);
Assert.InRange(fresh, 4.9e-5, 5e-5); // 1 mL/s per HP of severity (5e-5 m^3/s at 50), x vascularity 1 x pumping 1, a hair clotted
for (var i = 0; i < 100; i++) p.Step(0.1); // 10 s at slow scale 20 is 200 clock seconds
Assert.True(p.Bleed(torso) < fresh * 0.5);
}
}