From 5a683ccc70c1cd0f0369ffa1168bf7a3773ecdf6 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 27 Nov 2026 01:58:41 +0100 Subject: [PATCH] Blood: a settle with no free wet lane pools into the nearest patch (Ymir d5f0567), spawns clear of live drops, the death tick's blood is shed; tests for repeated clicks, the free-lane clamp, the airway channels, blood after Finished (92/92) Co-Authored-By: Claude Sonnet 5.5 --- src/BadDay.Ymir/Blood.cs | 50 +++++++++--- src/BadDay.Ymir/OfficeBodySession.cs | 20 ++++- tests/BadDay.Tests/Program.cs | 118 +++++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 13 deletions(-) diff --git a/src/BadDay.Ymir/Blood.cs b/src/BadDay.Ymir/Blood.cs index 752a158..a11bb73 100644 --- a/src/BadDay.Ymir/Blood.cs +++ b/src/BadDay.Ymir/Blood.cs @@ -47,8 +47,9 @@ public sealed record BloodParameters /// it as drops of from where the wound is, carried by the part's pose, at most /// a tick; volume that cannot be shed yet waits as backlog and none is /// dropped. After every step a drop that has rested on something for ticks -/// settles into a wet patch, and the oldest supported drops settle until there is room for a full tick of emission, so -/// the live drops stay under . Ymir never settles by itself; this is the policy. +/// settles into a wet patch, and the oldest supported drops settle until the live drops are at most LiveCap - MaxDropsPerTick, so +/// they stay under . A lane Settle frees cannot be taken before the next Step, so a cap-bound run sheds fewer drops +/// than it could (measured 69% of 8 a tick at saturation; the rest waits as backlog, none is lost); a lethal bleed (2.5 drops a tick) never reaches it. Ymir never settles by itself; this is the policy. /// Allocation-free once the wound points are open. /// public sealed class Blood @@ -129,27 +130,34 @@ public sealed class Blood int n = 0; var v = _world.View; float r = _p.DropRadius; - for (int pass = 0; n < budget; pass++) + for (bool any = true; any && n < budget;) { - bool any = false; + any = false; for (int k = 0; k < _pointCount && n < budget; k++) { ref var w = ref _points[(_cursor + k) % _pointCount]; if (w.Volume < _dropVolume) continue; - w.Volume -= _dropVolume; - any = true; int lane = _ragdoll.BodyLane(w.Part); var q = new Quat(v.RotationX[lane], v.RotationY[lane], v.RotationZ[lane], v.RotationW[lane]); q.Rotate(w.Px, w.Py, w.Pz, out var px, out var py, out var pz); q.Rotate(w.Nx, w.Ny, w.Nz, out var nx, out var ny, out var nz); - // Outside the surface, and a drop shed in the same tick one diameter further out so none starts inside another. - float lift = r + 0.002f + pass * 2f * r; - var at = (v.PositionX[lane] + px + nx * lift, v.PositionY[lane] + py + ny * lift, v.PositionZ[lane] + pz + nz * lift); + // Outside the surface, and then outward until clear of every drop already live or shed this tick (coincident cuts, a wound that sheds + // faster than a drop leaves): overlapped fluid blows up. A point still blocked after a few diameters waits; its volume stays gathered. + float lift = r + 0.002f; + (float X, float Y, float Z) at = default; + bool clear = false; + for (int attempt = 0; attempt < 12 && !clear; attempt++, lift += r) + { + at = (v.PositionX[lane] + px + nx * lift, v.PositionY[lane] + py + ny * lift, v.PositionZ[lane] + pz + nz * lift); + clear = Clear(at, n, v); + } + if (!clear) continue; + w.Volume -= _dropVolume; + any = true; var velocity = (v.VelocityX[lane] + nx * _p.ExitSpeed, v.VelocityY[lane] + ny * _p.ExitSpeed, v.VelocityZ[lane] + nz * _p.ExitSpeed); _descs[n++] = new Box3DSoaParticleDesc(at, _mass, r, _phase, (uint)_p.Material, velocity: velocity); } - if (!any) break; } _cursor = (_cursor + 1) % _pointCount; if (n == 0) return; @@ -164,7 +172,27 @@ public sealed class Blood Emitted += n * _dropVolume; } - /// Settles the drops that have come to rest, then the oldest supported ones down to room for a full tick, after the step. + /// True when a drop at touches none of the live drops nor the drops of this tick. + private bool Clear((float X, float Y, float Z) at, int placed, Box3DSoaView v) + { + float min = 2f * _p.DropRadius * 0.999f; + float min2 = min * min; + for (int i = 0; i < placed; i++) + { + float dx = _descs[i].X - at.X, dy = _descs[i].Y - at.Y, dz = _descs[i].Z - at.Z; + if (dx * dx + dy * dy + dz * dz < min2) return false; + } + var px = v.ParticleX; var py = v.ParticleY; var pz = v.ParticleZ; + for (int i = 0; i < _liveCount; i++) + { + int lane = _live[i].Lane; + float dx = px[lane] - at.X, dy = py[lane] - at.Y, dz = pz[lane] - at.Z; + if (dx * dx + dy * dy + dz * dz < min2) return false; + } + return true; + } + + /// Settles the drops that have come to rest, then the oldest supported ones until at most LiveCap - MaxDropsPerTick are live, after the step. internal void AfterStep() { if (_liveCount == 0) return; diff --git a/src/BadDay.Ymir/OfficeBodySession.cs b/src/BadDay.Ymir/OfficeBodySession.cs index b0dbda1..62f4d16 100644 --- a/src/BadDay.Ymir/OfficeBodySession.cs +++ b/src/BadDay.Ymir/OfficeBodySession.cs @@ -22,7 +22,8 @@ public sealed class OfficeBodySession : IDisposable private readonly StressSession _session; private readonly Physiology _physiology; private readonly double _tickSeconds; - private readonly double[] _bleeds; + private readonly double[] _bleeds, _lastBleeds; + private readonly double _spillPerLoss; private readonly int _handL, _handR, _pelvisLane; private bool _disposed; @@ -39,6 +40,7 @@ public sealed class OfficeBodySession : IDisposable { var plan = _body.Plan; _bleeds = new double[plan.Parts.Count]; + _lastBleeds = new double[plan.Parts.Count]; _body.ReadSenses(); int head = plan.SenseIndex("Head"); _handL = plan.SenseIndex("Hand", Side.Left); @@ -46,6 +48,7 @@ public sealed class OfficeBodySession : IDisposable _pelvisLane = _body.BodyLane(plan.PartIndex("Pelvis")); var physiologyPlan = PhysiologyPlans.Humanoid(); _physiology = new Physiology(plan, physiologyPlan, p.SlowScale); + _spillPerLoss = physiologyPlan.Blood.NominalVolume * physiologyPlan.Blood.LethalLossFraction; // real m^3 for one unit of BloodLoss // The desk's emitters sit where the head does and are sensed there. _session = new StressSession(p with { Setting = p.Setting.Shifted(_body.X[head], _body.Y[head], _body.Z[head]) }, seed, plan.SensePoints.Count, OfficeBody.Poses, head, _physiology, plan, physiologyPlan); @@ -106,9 +109,22 @@ public sealed class OfficeBodySession : IDisposable long tick = _session.TickIndex; _body.ReadSenses(); var airway = _body.ReadAirway(); + double lossBefore = _physiology.BloodLoss; var events = _session.Tick(_body.X, _body.Y, _body.Z, _body.Strengths, _body.Targets, airway); if (events != TickEvents.None) _body.Enqueue(tick, events); - for (int part = 0; part < _bleeds.Length; part++) _bleeds[part] = _physiology.Bleed(part); + if (_physiology.Dead) + { + // Bleed() reads 0 once dead, but the dying step still spilled what it took from the blood: that volume, shared as the step before shared its bleed. + double last = 0.0; + for (int part = 0; part < _bleeds.Length; part++) last += _lastBleeds[part]; + double rate = (_physiology.BloodLoss - lossBefore) * _spillPerLoss / _tickSeconds; + for (int part = 0; part < _bleeds.Length; part++) _bleeds[part] = last > 0.0 && rate > 0.0 ? _lastBleeds[part] * rate / last : 0.0; + Array.Clear(_lastBleeds); + } + else + { + for (int part = 0; part < _bleeds.Length; part++) _lastBleeds[part] = _bleeds[part] = _physiology.Bleed(part); + } _body.DriveAndStep(_tickSeconds, _bleeds); return; } diff --git a/tests/BadDay.Tests/Program.cs b/tests/BadDay.Tests/Program.cs index 4069b10..eaa3db6 100644 --- a/tests/BadDay.Tests/Program.cs +++ b/tests/BadDay.Tests/Program.cs @@ -1389,6 +1389,7 @@ public static class Program } clock.Stop(); Check(wetAt15 > 0, "no wet patch by 15 s"); + // Measured: shipped peak 340 (319 before the spawn clearance), a settle by the cap alone (the cap-only-settle mutant) 504, this bound 384 between them. Check(peak <= bloodParameters.LiveCap * 3 / 4, $"{peak} drops were live at once: the cap, not the slow-settle rule, is carrying the blood (probe: 256 live at 300 drops/s)"); double wet = 0.0; var volume = s.View.WetPatchVolume; @@ -1456,6 +1457,123 @@ public static class Program Console.WriteLine($" airway: liquid at the opening from tick {atAirway}, in the airway from tick {liquid}, peak {maxLiquid * 1e6:F2} mL, occlusion {maxOcclusion:F3}"); }), + ("office blood is conserved to death and the death tick's blood is shed: one Knife (and three on one point), run to Finished, Emitted + Backlog == Gain x the real spill within 1e-9 m^3 (the dying step bleeds, though Bleed reads 0 once dead)", () => + { + var p = new StressParameters { TickSeconds = OfficeBody.TickSeconds }; + var blood = Lodur.PhysiologyPlans.Humanoid().Blood; + foreach (int cuts in new[] { 1, 3 }) + { + using var s = new OfficeBodySession(p, 1); + var hit = Hit(s, SeatedPuppet.Plan.PartIndex("LowerArm", Lodur.Side.Left)); + for (int i = 0; i < cuts; i++) Check(s.Wound(hit, WoundTool.Knife), "the knife was refused"); + while (!s.Finished) s.Step(); + Check(s.Dead, "the body lived"); + double visible = new BloodParameters().Gain * blood.NominalVolume * s.BloodLoss * blood.LethalLossFraction; + double shed = s.BloodEmitted + s.BloodBacklog; + Check(Math.Abs(shed - visible) < 1e-9, $"{cuts} cut(s): shed {shed * 1000.0:F4} L of {visible * 1000.0:F4} L"); + } + }), + + ("office blood stops when the run does: a living body still bleeding at Finished sheds nothing in the next 300 steps (the physiology no longer steps, so its last rates would run forever)", () => + { + var p = new StressParameters { TickSeconds = OfficeBody.TickSeconds }; + using var s = new OfficeBodySession(p, 1); + Check(s.Wound(Hit(s, SeatedPuppet.Plan.PartIndex("LowerArm", Lodur.Side.Left)), new WoundTool(new[] { "Skin", "Muscle" }, 3.0)), "the wound was refused"); + double before = 0.0; + while (!s.Finished) { s.Step(); if (s.TickIndex == 100) before = s.BloodEmitted; } + Check(!s.Dead && s.BloodEmitted > before + 1e-3, "the wound was not bleeding at the end of the run"); + double shed = s.BloodEmitted + s.BloodBacklog; + for (int t = 0; t < 300; t++) s.Step(); + Check(s.BloodEmitted + s.BloodBacklog == shed, $"{(s.BloodEmitted + s.BloodBacklog - shed) * 1e6:F3} mL more blood after Finished"); + }), + + ("office blood never throws or flies at repeated clicks: the same PartHit cut five or six times (lower arm, upper arm, thigh) and six coincident cuts on a shin run 20 s with every drop under 12 m/s, no refused Settle, and wet volume + live drops == Emitted within 1e-6 m^3", () => + { + var p = new StressParameters { TickSeconds = OfficeBody.TickSeconds }; + var drop = new BloodParameters().DropVolume; + foreach (var (label, cuts) in new[] { ("LowerArm", 5), ("UpperArm", 6), ("Thigh", 6), ("Shin", 6) }) + { + using var s = new OfficeBodySession(p, 1); + var hit = Hit(s, SeatedPuppet.Plan.PartIndex(label, Lodur.Side.Left)); + for (int i = 0; i < cuts; i++) Check(s.Wound(hit, WoundTool.Knife), $"{label} cut {i} was refused"); + double fastest = 0.0; + int peakWet = 0; + for (int t = 0; t < 20 * Hz; t++) + { + s.Step(); // a refused Settle or CreateParticles throws here + var v = s.View; + for (int i = 0; i < v.ParticleCount; i++) + { + if (v.ParticleLive[i] == 0u) continue; + fastest = Math.Max(fastest, Math.Sqrt(v.ParticleVx[i] * v.ParticleVx[i] + v.ParticleVy[i] * v.ParticleVy[i] + v.ParticleVz[i] * v.ParticleVz[i])); + } + int wets = 0; + for (int i = 0; i < v.WetCount; i++) if (v.WetLive[i] != 0u) wets++; + peakWet = Math.Max(peakWet, wets); + } + double wet = 0.0; + for (int i = 0; i < s.View.WetCount; i++) if (s.View.WetLive[i] != 0u) wet += s.View.WetPatchVolume[i]; + Console.WriteLine($" {label} x{cuts}: fastest drop {fastest:F2} m/s, peak {peakWet} wet patches, {s.BloodEmitted * 1000.0:F1} L shed, backlog {s.BloodBacklog * 1000.0:F2} L, live {s.LiveDrops}"); + Check(fastest < 12.0, $"{label} x{cuts}: a drop flew at {fastest:F1} m/s"); + Check(Math.Abs(wet + s.LiveDrops * drop - s.BloodEmitted) < 1e-6, $"{label} x{cuts}: wet {wet} + live {s.LiveDrops} drops is not emitted {s.BloodEmitted} m^3"); + } + }), + + ("blood never asks for a lane the world has not freed: LiveCap 24 and no calm settling (SettleTicks 100000), six wounds shedding 6 drops a tick for 1200 ticks, neither CreateParticles nor Settle refuses and the live drops stay at most 24", () => + { + var parameters = new BloodParameters { LiveCap = 24, SettleTicks = 100000 }; + using var body = new SeatedBody(parameters); + var plan = SeatedPuppet.Plan; + var bleeds = new double[plan.Parts.Count]; + foreach (var label in new[] { "LowerArm", "Thigh", "Shin" }) + { + foreach (var side in new[] { Lodur.Side.Left, Lodur.Side.Right }) + { + int part = plan.PartIndex(label, side); + var c = plan.Parts[part].Position; + PartHit? hit = null; + foreach (var (dx, dy, dz) in new[] { (1f, 0f, 0f), (-1f, 0f, 0f), (0f, 1f, 0f), (0f, -1f, 0f), (0f, 0f, 1f), (0f, 0f, -1f) }) + { + if (body.Pick(new BodyPoint(c.X - dx, c.Y - dy, c.Z - dz), new BodyPoint(dx, dy, dz)) is { } h && h.Part == part) { hit = h; break; } + } + Check(hit is not null, $"no hit on {label} {side}"); + body.OpenWound(part, hit!.Value.Point, hit.Value.Normal); + bleeds[part] = 6.0 * parameters.DropVolume * Hz / parameters.Gain / 6.0; + } + } + int peak = 0; + for (int t = 0; t < 1200; t++) + { + body.DriveAndStep(1.0 / Hz, bleeds); // a refused CreateParticles or Settle throws here + peak = Math.Max(peak, body.LiveDrops); + } + Check(peak <= 24, $"{peak} drops were live under a cap of 24"); + Check(body.BloodEmitted > 24 * parameters.DropVolume, "the cap was never reached: the test pins nothing"); + }), + + ("seated airway pins: the pooled liquid reaches Lodur (a wound above the mouth settling on the face reads Pooled), down is the world's gravity (an upright mouth has no drop), and the blood material is slippery (wet friction 0.1, friction 0.3)", () => + { + var plan = SeatedPuppet.Plan; + var mouth = plan.AirwayOpenings.First(a => a.Label == "Mouth"); + var c = plan.Parts[mouth.Part].Position; + using var body = new SeatedBody(new BloodParameters { SettleSpeed = 100f, SettleTicks = 1 }); // drops settle on first touch: they pool where they land + Check(body.ReadAirway().Drop == 0.0, $"an upright seated mouth has a drop of {body.ReadAirway().Drop} m"); + Check(body.View.MaterialWetFriction[1] == 0.1f && body.View.MaterialFriction[1] == 0.3f, $"material 1: friction {body.View.MaterialFriction[1]}, wet friction {body.View.MaterialWetFriction[1]}"); + var hit = body.Pick(new BodyPoint(c.X, c.Y + mouth.Offset.Y + 0.04f, c.Z + 1f), new BodyPoint(0f, 0f, -1.5f)); + Check(hit is { } h && h.Part == mouth.Part, "no hit on the face above the mouth"); + body.OpenWound(hit!.Value.Part, hit.Value.Point, hit.Value.Normal); + var bleeds = new double[plan.Parts.Count]; + bleeds[hit.Value.Part] = 0.0003; + double pooled = 0.0; + for (int t = 0; t < 300; t++) + { + body.DriveAndStep(1.0 / Hz, bleeds); + pooled = Math.Max(pooled, body.ReadAirway().Pooled); + } + Check(pooled > 1e-5, $"the pooled liquid at the mouth peaked at {pooled} m^3"); + Console.WriteLine($" airway: pooled peak {pooled * 1e6:F1} mL"); + }), + ("seated world identity: a SeatedBody (a particle system, no wound) steps to the same positions and rotations as the same world with no particle system, 1200 driven ticks", () => { using var body = new SeatedBody();