diff --git a/src/BadDay.Core/PainBoxParameters.cs b/src/BadDay.Core/PainBoxParameters.cs
index 518df6d..dc793e5 100644
--- a/src/BadDay.Core/PainBoxParameters.cs
+++ b/src/BadDay.Core/PainBoxParameters.cs
@@ -26,7 +26,7 @@ public sealed record PainBoxParameters
public Lodur.WithdrawalParameters Reflex { get; init; } = new();
public Lodur.HoldParameters Hold { get; init; } = new();
- // Per-seed trait spread (fraction): nociceptor gain (Nerve.Gain) and resolve drain (Drive.DrainTrait).
+ // Per-seed trait spread (fraction): nociceptor gain and resolve drain. The seed's draw multiplies Nerve.Gain and Drive.DrainTrait.
public double TraitJitter { get; init; } = 0.2;
public double SampleEverySeconds { get; init; } = 1.0;
diff --git a/src/BadDay.Core/PainBoxSession.cs b/src/BadDay.Core/PainBoxSession.cs
index 88bba0f..0e8c259 100644
--- a/src/BadDay.Core/PainBoxSession.cs
+++ b/src/BadDay.Core/PainBoxSession.cs
@@ -33,12 +33,13 @@ public sealed class PainBoxSession
_handRegion = handRegion;
_p = p;
var rng = new SplitMix64(seed);
- // Per-seed traits, drawn first and in fixed order so the stream never shifts.
+ // Per-seed traits, drawn first and in fixed order so the stream never shifts. They multiply the caller's
+ // Nerve.Gain and Drive.DrainTrait (1.0 by default), so a caller's own value is a baseline, never overwritten.
double gainTrait = 1.0 + p.TraitJitter * rng.NextSigned();
double drainTrait = 1.0 + p.TraitJitter * rng.NextSigned();
Seed = seed;
- Actor = new Actor(p.TickSeconds, regionCount, p.Nerve with { Gain = gainTrait }, p.Reflex,
- p.Drive with { DrainTrait = drainTrait }, p.Hold, holdPose, withdrawPose);
+ Actor = new Actor(p.TickSeconds, regionCount, p.Nerve with { Gain = p.Nerve.Gain * gainTrait }, p.Reflex,
+ p.Drive with { DrainTrait = p.Drive.DrainTrait * drainTrait }, p.Hold, holdPose, withdrawPose);
_box = new PainBox(p, rng);
_sources = new[] { box };
_maxTick = Ticks(p.MaxSeconds);
diff --git a/src/BadDay.Ymir/BodyPainBox.cs b/src/BadDay.Ymir/BodyPainBox.cs
index 7c96db2..e6bed47 100644
--- a/src/BadDay.Ymir/BodyPainBox.cs
+++ b/src/BadDay.Ymir/BodyPainBox.cs
@@ -12,9 +12,10 @@ public readonly record struct HandSample(float X, float Y, float Z, bool InBox);
///
/// The body run's result: the same timeline and outcome as the headless run, plus what only a body has.
-/// 's EndMs is the hand-out time when the hand came out.
+/// The outcome comes from the Actor's events (the run goes on to the deliberate withdrawal, then settles up to a second
+/// for the hand to come out), so 's EndMs is the later of the Withdrew event and the hand-out time.
///
-/// First step at or after the will broke with the hand point outside the box; null if it never came out.
+/// First step at or after the will broke with the hand point outside the box; null if it did not come out by the end of the settle. It can precede the Withdrew event.
/// Times the hand left the box between the end of the ramp and the break.
/// The hand after every step; the index is the tick.
public sealed record BodyPainBoxResult(
@@ -37,6 +38,12 @@ public static class BodyPainBox
private const float BoxHalf = 0.12f;
+ /// Physics substeps per 1/120 s step. The ragdoll gains were tuned against 4; the behaviour tests, not this number, are the rule.
+ private const int Substeps = 4;
+
+ /// After the deliberate withdrawal begins the run waits this long for the hand to come out.
+ private const double SettleSeconds = 1.0;
+
public static BodyPainBoxResult Run(PainBoxParameters p, ulong seed)
{
if (Math.Abs(p.TickSeconds - TickSeconds) > 1e-12)
@@ -73,36 +80,53 @@ public static class BodyPainBox
long? handOut = null;
int excursions = 0;
bool wasIn = false;
- long breakTick = -1;
+ long breakTick = -1, withdrewTick = -1;
+ long settleTicks = (long)Math.Round(SettleSeconds / p.TickSeconds);
+
+ HandSample ReadHand()
+ {
+ ragdoll.ReadRegions(x, y, z);
+ float hx = x[(int)Region.HandR], hy = y[(int)Region.HandR], hz = z[(int)Region.HandR];
+ return new HandSample(hx, hy, hz, session.Box.Contains(hx, hy, hz));
+ }
while (!session.Finished)
{
- if (session.Disabled()) break;
+ if (session.Disabled())
+ {
+ if (samples.Count == 0) samples.Add(ReadHand()); // disabled at tick 0: the track still holds the rest pose
+ break;
+ }
long tick = session.TickIndex;
ragdoll.ReadRegions(x, y, z);
var events = session.Tick(x, y, z, strengths, targets);
ragdoll.Drive(strengths, targets);
- world.Step((float)p.TickSeconds, 4);
+ world.Step((float)p.TickSeconds, Substeps);
if ((events & TickEvents.WillBroke) != 0) breakTick = tick;
- ragdoll.ReadRegions(x, y, z);
- var hand = (X: x[(int)Region.HandR], Y: y[(int)Region.HandR], Z: z[(int)Region.HandR]);
- var inBox = session.Box.Contains(hand.X, hand.Y, hand.Z);
- samples.Add(new HandSample(hand.X, hand.Y, hand.Z, inBox));
+ var sample = ReadHand();
+ var inBox = sample.InBox;
+ samples.Add(sample);
if (breakTick < 0)
{
if (tick > rampEnd && wasIn && !inBox) excursions++;
wasIn = inBox;
}
- else if (!inBox)
+ else if (!inBox && handOut is null)
{
handOut = session.Ms(tick);
- session.EndAt(tick);
+ }
+
+ // The run ends on the Actor's events, never on the hand: after the deliberate withdrawal, once the
+ // hand is out or the settle is over.
+ if (session.Withdrew)
+ {
+ if (withdrewTick < 0) withdrewTick = tick;
+ if (handOut is not null || tick - withdrewTick >= settleTicks) session.EndAt(tick);
}
}
- if (handOut is null && session.Withdrew) session.EndAt(session.TickIndex - 1);
return new BodyPainBoxResult(session.Result(), handOut, excursions, samples);
}
}
diff --git a/tests/BadDay.Tests/Program.cs b/tests/BadDay.Tests/Program.cs
index b0e9bcd..7e27f34 100644
--- a/tests/BadDay.Tests/Program.cs
+++ b/tests/BadDay.Tests/Program.cs
@@ -160,6 +160,58 @@ public static class Program
for (int i = 3 * Hz; i < r.Hand.Count; i++) Check(Dist(r.Hand[i]) <= 0.023f, $"drift {Dist(r.Hand[i]):F3} m at tick {i}");
}),
+ ("body: the stimulus and the flinches are the right hand's: the timeline's stimulus column is live while the hand is in the box", () =>
+ {
+ var r = BodyPainBox.Run(OnBody, 1);
+ var at3 = r.Run.Timeline.Single(e => e.Kind == EntryKind.Sample && e.TimeMs == 3000);
+ Check(at3.Stimulus > 0.3 && at3.Pain > 0.3, $"stimulus {at3.Stimulus}, pain {at3.Pain} at 3 s");
+ Check(r.Hand[3 * Hz].InBox && r.Run.Flinches > 0, "right hand not in the box, or no flinch");
+ }),
+
+ ("body: a heavy hold (flinch gain 1, shake 0.3) still ends by the Actor's events: WillBroke, one Withdrew row, hand out reported separately", () =>
+ {
+ var heavy = OnBody with { Hold = new Lodur.HoldParameters { FlinchGain = 1.0, StrainShake = 0.3 } };
+ for (ulong seed = 1; seed <= 6; seed++)
+ {
+ var r = BodyPainBox.Run(heavy, seed);
+ Check(r.Run.Outcome == Outcome.WillBroke, $"seed {seed}: outcome {r.Run.Outcome}, break {r.Run.TimeToBreakMs}, hand out {r.HandOutMs}");
+ var withdrew = r.Run.Timeline.Single(e => e.Kind == EntryKind.Withdrew);
+ Check(withdrew.TimeMs - r.Run.TimeToBreakMs == 100, $"seed {seed}: Withdrew row {withdrew.TimeMs - r.Run.TimeToBreakMs} ms after the break");
+ Check(r.Run.EndMs >= withdrew.TimeMs && (r.HandOutMs is null || r.Run.EndMs >= r.HandOutMs), $"seed {seed}: ended before the withdrawal or the hand-out");
+ }
+ }),
+
+ ("body: the flinch moves the hand: against a flinch-gain-0 control on the same seed the tracks part by over 2.3 cm within 200 ms of a flinch, and not before the first flinch", () =>
+ {
+ for (ulong seed = 1; seed <= 3; seed++)
+ {
+ var full = BodyPainBox.Run(OnBody, seed);
+ var control = BodyPainBox.Run(OnBody with { Hold = new Lodur.HoldParameters { FlinchGain = 0.0 } }, seed);
+ Check(full.Run.Flinches == control.Run.Flinches, $"seed {seed}: the nervous state changed with the arm");
+ var flinches = full.Run.Timeline.Where(e => e.Kind == EntryKind.Flinch).Select(e => Tick(e.TimeMs)).ToList();
+ float Gap(int i) => MathF.Sqrt(MathF.Pow(full.Hand[i].X - control.Hand[i].X, 2) + MathF.Pow(full.Hand[i].Y - control.Hand[i].Y, 2) + MathF.Pow(full.Hand[i].Z - control.Hand[i].Z, 2));
+ float after = flinches.Max(f => Enumerable.Range(f, Hz / 5 + 1).Max(Gap));
+ float before = Enumerable.Range(0, flinches[0]).Max(Gap);
+ Check(after > 0.023f, $"seed {seed}: the largest gap from the control after a flinch is {after:F4} m");
+ Check(before < after / 2, $"seed {seed}: the gap before the first flinch is {before:F4} m, after {after:F4} m");
+ }
+ }),
+
+ ("body: a box disabled at tick 0 ends at once with the will intact and a hand track to read", () =>
+ {
+ var r = BodyPainBox.Run(OnBody with { BoxDisableAtSeconds = 0.0 }, 1);
+ Check(r.Run.Outcome == Outcome.BoxDisabled && r.Hand.Count == 1 && !r.Hand[0].InBox, $"{r.Run.Outcome}, {r.Hand.Count} samples");
+ }),
+
+ ("the seed's trait multiplies the caller's nerve gain and drain trait instead of replacing them", () =>
+ {
+ Check(PainBoxSimulation.Run(Default with { Nerve = new Lodur.NerveParameters { Gain = 0.0 }, MaxSeconds = 20.0 }, 1).Outcome == Outcome.Endured, "gain 0 should leave no pain");
+ long Break(double gain) => PainBoxSimulation.Run(Default with { Nerve = new Lodur.NerveParameters { Gain = gain } }, 1).TimeToBreakMs ?? long.MaxValue;
+ Check(Break(2.0) < PainBoxSimulation.Run(Default, 1).TimeToBreakMs, "a caller's nerve gain of 2 did not shorten the break");
+ long Drain(double d) => PainBoxSimulation.Run(Default with { Drive = new Lodur.DriveParameters { DrainTrait = d } }, 1).TimeToBreakMs ?? long.MaxValue;
+ Check(Drain(2.0) < Drain(1.0) && Drain(1.0) == 14730, "a caller's drain trait did not scale the drain");
+ }),
+
("CLI run is deterministic and sweep reports a break-time line", () =>
{
string Run(params string[] a) { var w = new StringWriter(); BadDay.Cli.Program.Execute(a, w); return w.ToString(); }