diff --git a/src/BadDay.Core/ChamberBrain.cs b/src/BadDay.Core/ChamberBrain.cs index ccaef26..b8368a5 100644 --- a/src/BadDay.Core/ChamberBrain.cs +++ b/src/BadDay.Core/ChamberBrain.cs @@ -14,6 +14,9 @@ public static class ChamberBrain public const int HoldIndex = 0; public const int WithdrawIndex = 1; + /// The withdraw goal's id: what the pain box's release waits for (). + public const string WithdrawId = "withdraw"; + /// The objective's pull for the payment: 0 for nothing, 0.5 at , toward 1 for more. public static double Pull(PainBoxParameters p) { @@ -27,8 +30,10 @@ public static class ChamberBrain /// /// hold-hand: always eligible, priority 0, the payment's pull, the hold pose with the reflex leak and strain shake - /// pulling toward the withdraw pose. withdraw: eligible once the will has broken, priority 1, no pull, the withdraw - /// pose with no leak or shake. + /// pulling toward the withdraw pose. withdraw: eligible once the will has broken (, which + /// never falls, so the goal is one-way: the will mending does not give the hand back), priority 1, no pull, involuntary + /// for good (the withdrawal is the body's, it costs no will and does not weaken as the will mends), the withdraw pose + /// with no leak or shake. /// public static Brain Build(PainBoxParameters p, ReadOnlySpan hold, ReadOnlySpan withdraw) { @@ -48,10 +53,11 @@ public static class ChamberBrain }, new GoalSpec { - Id = "withdraw", + Id = WithdrawId, Priority = 1.0, Pull = 0.0, - When = new[] { new Condition(DriveQuantity.WillBroken, ConditionOp.AtLeast, 1.0) }, + When = new[] { new Condition(DriveQuantity.Breaks, ConditionOp.AtLeast, 1.0) }, + InvoluntarySeconds = double.PositiveInfinity, Behaviour = new PoseBehaviour { Keys = new[] { withdrawPose }, diff --git a/src/BadDay.Core/PainBoxParameters.cs b/src/BadDay.Core/PainBoxParameters.cs index 0353e59..171be6b 100644 --- a/src/BadDay.Core/PainBoxParameters.cs +++ b/src/BadDay.Core/PainBoxParameters.cs @@ -34,5 +34,8 @@ public sealed record PainBoxParameters // 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; + /// The id of the Brain goal whose takeover is the withdrawal: the session's Withdrew, its Withdrew row and its WillBroke outcome come from that goal's GoalChanged and no other. The session refuses an id the Brain lacks. + public string ReleaseGoalId { get; init; } = ChamberBrain.WithdrawId; + public double SampleEverySeconds { get; init; } = 1.0; } diff --git a/src/BadDay.Core/PainBoxSession.cs b/src/BadDay.Core/PainBoxSession.cs index 027e9a7..45fa062 100644 --- a/src/BadDay.Core/PainBoxSession.cs +++ b/src/BadDay.Core/PainBoxSession.cs @@ -19,6 +19,7 @@ public sealed class PainBoxSession private readonly long _maxTick; private readonly long _sampleEvery; private readonly int _handRegion; + private readonly int _releaseGoal; private double _stimulus; private long _breakTick = -1; private int _flinches; @@ -40,6 +41,9 @@ public sealed class PainBoxSession Seed = seed; Actor = new Actor(p.TickSeconds, regionCount, p.Nerve with { Gain = p.Nerve.Gain * gainTrait }, p.Reflex, p.Drive with { DrainTrait = p.Drive.DrainTrait * drainTrait }, ChamberBrain.Build(p, holdPose, withdrawPose)); + _releaseGoal = -1; + for (int g = 0; g < Actor.Brain.Goals.Count; g++) if (Actor.Brain.Goals[g].Id == p.ReleaseGoalId) _releaseGoal = g; + if (_releaseGoal < 0) throw new ArgumentException($"the Brain has no goal '{p.ReleaseGoalId}' to release on"); _box = new PainBox(p, rng); _sources = new[] { box }; _maxTick = Ticks(p.MaxSeconds); @@ -59,7 +63,7 @@ public sealed class PainBoxSession /// True once the run is over: the box was disabled, the time ran out, or the caller ended it. public bool Finished { get; private set; } - /// True from the tick the Brain's withdraw goal takes over, its deliberate latency after the will breaks. + /// True from the tick the Brain's release goal (, the withdraw goal) takes over, its deliberate latency after the will breaks. public bool Withdrew { get; private set; } /// @@ -77,12 +81,12 @@ public sealed class PainBoxSession /// /// The disable check, before ticking: if the box switches off now while the will still holds, the run ends - /// with the will intact and this returns true. A will already broken is not given back by the box going off. + /// with the will intact and this returns true. A will that has broken is not given back by the box going off, even once it mends. /// public bool Disabled() { long tick = TickIndex; - if (tick != _box.DisableTick || Actor.Drive.WillBroken) return false; + if (tick != _box.DisableTick || Actor.Drive.Breaks > 0) return false; _stimulus = 0.0; _outcome = Outcome.BoxDisabled; _endTick = tick; @@ -114,7 +118,7 @@ public sealed class PainBoxSession Log(tick, EntryKind.WillBroke); } if (tick % _sampleEvery == 0) Log(tick, EntryKind.Sample); - if ((events & TickEvents.GoalChanged) != 0) + if ((events & TickEvents.GoalChanged) != 0 && Actor.GoalIndex == _releaseGoal) { Withdrew = true; _outcome = Outcome.WillBroke; diff --git a/src/BadDay.Ymir/BodyPainBox.cs b/src/BadDay.Ymir/BodyPainBox.cs index 7e04122..9b2c103 100644 --- a/src/BadDay.Ymir/BodyPainBox.cs +++ b/src/BadDay.Ymir/BodyPainBox.cs @@ -194,7 +194,7 @@ public sealed class BodyPainBoxSession : IDisposable _hand = sample; if (!ticked) return; - if ((events & TickEvents.WillBroke) != 0) _breakTick = tick; + if ((events & TickEvents.WillBroke) != 0 && _breakTick < 0) _breakTick = tick; // the snap: a later break after a mend is not the first bool inBox = sample.InBox; if (_breakTick < 0) diff --git a/tests/BadDay.Tests/Program.cs b/tests/BadDay.Tests/Program.cs index 0cd5bb0..1b078be 100644 --- a/tests/BadDay.Tests/Program.cs +++ b/tests/BadDay.Tests/Program.cs @@ -12,6 +12,8 @@ public static class Program { private static readonly PainBoxParameters Default = new(); private static readonly PainBoxParameters OnBody = new() { TickSeconds = BodyPainBox.TickSeconds }; + /// OnBody with a will that costs no strength (WillCurve toward 0 makes Will^WillCurve 1): the arm's strength is the same whatever the will, so a flinch-gain-0 control feeds nothing different back into the drive. The flinch test's premise. + private static readonly PainBoxParameters Steady = OnBody with { Drive = new Lodur.DriveParameters { WillCurve = 1e-9 } }; private const int Hz = 120; private static readonly (string Name, Action Body)[] Tests = @@ -210,8 +212,8 @@ public static class Program { 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); + var full = BodyPainBox.Run(Steady, seed); + var control = BodyPainBox.Run(Steady 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)); @@ -222,6 +224,37 @@ public static class Program } }), + ("chamber: the withdrawal is one-way: ticked 90 s past it with the box off and the will mended, the goal stays Withdraw, and a box going off does not end the run as if the will held", () => + { + var pose = new[] { Lodur.Quat.Identity }; + var session = new PainBoxSession(Default, 1, new Lodur.BoxStimulus(0f, 0f, 0f, 1f, 1f, 1f), 1, pose, pose); + var point = new float[1]; + var strengths = new float[1]; + var targets = new Quat[1]; + while (!session.Withdrew) session.Tick(point, point, point, strengths, targets); + session.SwitchOff(); + bool mended = false; + for (int i = 0; i < 9000; i++) + { + session.Tick(point, point, point, strengths, targets); + mended |= !session.Actor.Drive.WillBroken; + Check(session.Actor.GoalIndex == ChamberBrain.WithdrawIndex, $"the goal left Withdraw {i} ticks after the withdrawal"); + } + Check(mended, "the will never mended: the test no longer exercises the rule"); + Check(!session.Actor.Drive.WillBroken && session.Actor.Drive.Breaks > 0, "the will should be mended and the break still counted"); + Check(!session.Disabled(), "a box going off ended the run of a will that had broken and mended"); + }), + + ("chamber release: the withdrawal is the Brain's release goal by id, not any goal change; an id the Brain lacks is refused", () => + { + var r = PainBoxSimulation.Run(Default with { ReleaseGoalId = "hold-hand", MaxSeconds = 30.0 }, 1); + Check(r.Outcome == Outcome.Endured && r.Timeline.All(e => e.Kind != EntryKind.Withdrew) && r.TimeToBreakMs == 14730, + $"releasing on a goal the Brain never switches to: outcome {r.Outcome}, break {r.TimeToBreakMs}"); + bool threw = false; + try { PainBoxSimulation.Run(Default with { ReleaseGoalId = "nowhere" }, 1); } catch (ArgumentException) { threw = true; } + Check(threw, "a release goal the Brain lacks was accepted"); + }), + ("body: hand-out is the start of a sustained exit: under five non-default holds over 30 seeds a reported hand-out is never followed by re-entry and starts at the first step out", () => { var sets = new (string Name, PainBoxParameters P)[]