diff --git a/src/BadDay.Cli/Program.cs b/src/BadDay.Cli/Program.cs index a84da66..c7bb11e 100644 --- a/src/BadDay.Cli/Program.cs +++ b/src/BadDay.Cli/Program.cs @@ -130,8 +130,12 @@ public static class Program if (b.RageTick is { } rage) { int end = b.HandL.Count; - output.WriteLine($"rage from {b.Run.Timeline.First(e => e.Kind == StressEntryKind.GoalChanged).TimeMs} ms for " + - $"{((end - rage) / (double)OfficeBody.Hz).ToString("F1", CultureInfo.InvariantCulture)} s; strikes {b.Strikes(rage, end)} " + + StressEntry? calm = b.Run.Timeline.Any(e => e.Kind == StressEntryKind.GoalChanged && e.Goal == OfficeGoal.Calm) + ? b.Run.Timeline.First(e => e.Kind == StressEntryKind.GoalChanged && e.Goal == OfficeGoal.Calm) : null; + int rageEnd = calm is null ? end : (int)Math.Round(calm.Value.TimeMs * OfficeBody.Hz / 1000.0); + string how = calm is null ? "clipped by --max, no calm" : $"to calm at {calm.Value.TimeMs} ms"; + output.WriteLine($"rage from {b.Run.Timeline.First(e => e.Kind == StressEntryKind.GoalChanged && e.Goal == OfficeGoal.Rage).TimeMs} ms for " + + $"{((rageEnd - rage) / (double)OfficeBody.Hz).ToString("F1", CultureInfo.InvariantCulture)} s ({how}); strikes {b.Strikes(rage, end)} " + $"(left {OfficeBody.CountStrikes(b.HandL, rage, end)}, right {OfficeBody.CountStrikes(b.HandR, rage, end)}), " + $"{b.Strikes(0, rage)} before it"); } diff --git a/src/BadDay.Core/OfficeBrain.cs b/src/BadDay.Core/OfficeBrain.cs index 6778495..0fc6709 100644 --- a/src/BadDay.Core/OfficeBrain.cs +++ b/src/BadDay.Core/OfficeBrain.cs @@ -4,7 +4,7 @@ using Lodur; namespace BadDay.Core; /// What the office timeline's goal column names: the goal in force, by 's index. Display only. -public enum OfficeGoal { KeepComposure, Rage } +public enum OfficeGoal { KeepComposure, Rage, Calm } /// /// The poses Office Fella's goals write, one rotation per muscle: the composed pose he holds while he keeps his @@ -21,18 +21,25 @@ public sealed record OfficePoses(Quat[] Composed, Quat[] Flinch, Quat[][] RageKe /// /// Office Fella's Brain: keep-composure (the job, pulling with ) until the will -/// breaks, then rage (a geyser: no pull, nothing held down, the rage keys in a loop). Authored as data; the Actor -/// commits the switch, so the timeline's goal column is read back from . +/// breaks, then rage (a geyser: no pull, nothing held down, the rage keys in a loop; it refills his will on entry, is +/// involuntary for its first and then costs will at +/// a second), until that will runs dry and breaks a second time, and then calm: +/// his composed pose at normal strength, for good (ruling lodur:ruling:fella-calm-after-rage). Calm is terminal as +/// data: it gates on Breaks, which never falls, and nothing outranks it. Authored as data; the Actor commits the +/// switches, so the timeline's goal column is read back from . /// public static class OfficeBrain { public const int ComposureIndex = 0; public const int RageIndex = 1; + public const int CalmIndex = 2; - /// The rage goal's id: what the scenario's release waits for. public const string RageId = "rage"; - public static OfficeGoal GoalAt(int index) => index == RageIndex ? OfficeGoal.Rage : OfficeGoal.KeepComposure; + /// The calm goal's id: what the scenario's release waits for (). + public const string CalmId = "calm"; + + public static OfficeGoal GoalAt(int index) => index switch { RageIndex => OfficeGoal.Rage, CalmIndex => OfficeGoal.Calm, _ => OfficeGoal.KeepComposure }; public static Brain Build(StressParameters p, OfficePoses poses) => new() { @@ -51,7 +58,10 @@ public static class OfficeBrain Id = RageId, 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) }, + EntryResolve = 1.0, + Effort = p.RageEffort, + InvoluntarySeconds = p.RageOnsetSeconds, Behaviour = new PoseBehaviour { Keys = poses.RageKeys, @@ -60,6 +70,14 @@ public static class OfficeBrain Hold = p.Hold with { HoldStrength = 1f, FlinchGain = 0.0, StrainShake = 0.0 }, }, }, + new GoalSpec + { + Id = CalmId, + Priority = 2.0, + When = new[] { new Condition(DriveQuantity.Breaks, ConditionOp.AtLeast, 2.0) }, + InvoluntarySeconds = double.PositiveInfinity, + Behaviour = new PoseBehaviour { Keys = new[] { poses.Composed }, FlinchPose = poses.Flinch, Hold = p.Hold }, + }, }, }; } diff --git a/src/BadDay.Core/StressParameters.cs b/src/BadDay.Core/StressParameters.cs index 670a4d4..90932ff 100644 --- a/src/BadDay.Core/StressParameters.cs +++ b/src/BadDay.Core/StressParameters.cs @@ -11,14 +11,20 @@ public sealed record StressParameters public double MaxSeconds { get; init; } = 180.0; public double SampleEverySeconds { get; init; } = 1.0; - /// How long the run goes on after the rage takes over, seconds [P]: the geyser is seen, then the run ends. + /// How long the run goes on after the release goal (calm) takes over, seconds [P]: the settle time. Calibrated on the body so the seated puppet is still by the end (hand speed under 0.05 m/s from 5 s after calm, seeds 1-5); the headless run ends at the same time, one rule for every path. public double ReleaseSeconds { get; init; } = 6.0; + /// What the rage costs his will per second while it is voluntary, resolve units [P]: the rage refills his will, and this is how fast it runs dry (about 20 s from full, so the rage lasts 5-20 s with the senses still pressing). + public double RageEffort { get; init; } = 0.05; + + /// How long after it takes over the rage is involuntary, seconds [P]: full weight and no cost, the snap itself, before it becomes something he sustains. + public double RageOnsetSeconds { get; init; } = 1.0; + /// Seconds from one rage key to the next [P]: wind-up left, strike left, wind-up right, strike right. public double RageKeySeconds { get; init; } = 0.25; /// The id of the Brain goal whose takeover starts the release: the run ends after that goal's GoalChanged, and a change to any other goal does not. The session refuses an id the Brain lacks. - public string ReleaseGoalId { get; init; } = OfficeBrain.RageId; + public string ReleaseGoalId { get; init; } = OfficeBrain.CalmId; public Setting Setting { get; init; } = Setting.Office; public Character Character { get; init; } = Character.OfficeFella; diff --git a/src/BadDay.Core/StressResult.cs b/src/BadDay.Core/StressResult.cs index b61be4e..50d550a 100644 --- a/src/BadDay.Core/StressResult.cs +++ b/src/BadDay.Core/StressResult.cs @@ -18,7 +18,7 @@ public sealed record StressResult( ulong Seed, /// WillBroke (the run goes on through the release) or Endured; the office has no box to disable. Outcome Outcome, - /// When the run ended: the release after the rage took over, or MaxSeconds. + /// When the run ended: the release after calm took over, or MaxSeconds. long EndMs, /// Time resolve hit zero, ms; null unless the will broke. long? TimeToBreakMs, diff --git a/src/BadDay.Core/StressSession.cs b/src/BadDay.Core/StressSession.cs index d04d7a4..a8212e0 100644 --- a/src/BadDay.Core/StressSession.cs +++ b/src/BadDay.Core/StressSession.cs @@ -9,8 +9,8 @@ namespace BadDay.Core; /// , and the recorder. The headless run, the sweep, the CLI and a client all drive it one /// per step, and the player's lever is . The session never writes drive /// state, computes load or arousal, or holds a stress field: rows are end-of-tick state read from the Actor, and -/// nothing reads them back. The rage is the Brain's (): the session goes on past the break, logs the -/// tick the Actor commits the switch, and ends ReleaseSeconds after the Brain's release goal () takes over, or at MaxSeconds. +/// nothing reads them back. The rage and the calm after it are the Brain's (): the session goes on past the break, logs each +/// tick the Actor commits a switch, and ends ReleaseSeconds after the Brain's release goal () takes over, or at MaxSeconds. /// public sealed class StressSession { @@ -84,7 +84,7 @@ public sealed class StressSession /// Ticks run so far; the tick the next runs. public long TickIndex => Actor.TickCount; - /// True once the release after the rage took over is over, or the time ran out. + /// True once the release after calm took over is over, or the time ran out. public bool Finished { get; private set; } /// @@ -121,7 +121,7 @@ public sealed class StressSession if ((events & TickEvents.WillBroke) != 0) { - _breakTick = tick; + if (_breakTick < 0) _breakTick = tick; // the snap; the rage's own break is a row, not the time to break _outcome = Outcome.WillBroke; Log(tick, StressEntryKind.WillBroke); } diff --git a/src/BadDay.Ymir/OfficeBody.cs b/src/BadDay.Ymir/OfficeBody.cs index 554ce16..96a9115 100644 --- a/src/BadDay.Ymir/OfficeBody.cs +++ b/src/BadDay.Ymir/OfficeBody.cs @@ -18,9 +18,9 @@ public readonly record struct BodyPoint(float X, float Y, float Z); public sealed record OfficeBodyResult( StressResult Run, IReadOnlyList HandL, IReadOnlyList HandR, IReadOnlyList Pelvis) { - /// The tick the rage took over (the GoalChanged row); null if the will held. - public int? RageTick => Run.Timeline.Any(e => e.Kind == StressEntryKind.GoalChanged) - ? (int)Math.Round(Run.Timeline.First(e => e.Kind == StressEntryKind.GoalChanged).TimeMs * OfficeBody.Hz / 1000.0) + /// The tick the rage took over (the GoalChanged row whose goal is Rage); null if the will held. + public int? RageTick => Run.Timeline.Any(e => e.Kind == StressEntryKind.GoalChanged && e.Goal == OfficeGoal.Rage) + ? (int)Math.Round(Run.Timeline.First(e => e.Kind == StressEntryKind.GoalChanged && e.Goal == OfficeGoal.Rage).TimeMs * OfficeBody.Hz / 1000.0) : null; /// Strikes by both hands from tick up to , see . @@ -31,7 +31,7 @@ public sealed record OfficeBodyResult( /// Office Fella on the seated puppet: 's world, the desk's emitters moved to where the head /// sits and sensed there, and the same the headless run drives. Per step: the body reports its /// points, the session decides (Lodur's Actor commits the Brain's goals, ), the ragdoll is driven, -/// the world steps. The run ends as the session ends, the release after the rage takes over. +/// the world steps. The run ends as the session ends, the release after calm takes over. /// public static class OfficeBody { diff --git a/tests/BadDay.Tests/Program.cs b/tests/BadDay.Tests/Program.cs index 1b078be..7328260 100644 --- a/tests/BadDay.Tests/Program.cs +++ b/tests/BadDay.Tests/Program.cs @@ -776,7 +776,7 @@ public static class Program } }), - ("office golden [P]: the rows up to the break are rung 3's bytes, the 50-seed sweep ends in its captured line, and the run's own bytes are captured", () => + ("office golden [P]: the rows up to the break are rung 3's bytes, the 50-seed sweep ends in its captured line, and the run's own bytes are captured (re-captured with the arc: the rage refills his will, a second break and calm follow)", () => { string Out(params string[] a) { @@ -792,37 +792,63 @@ public static class Program for (int i = 2; i <= brk; i++) rung3.Add(lines[i].Replace(" KeepComposure", i == brk ? " Withdraw" : " HoldHand")); rung3.Add(lines[^1]); Check(Hash(string.Join('\n', rung3) + "\n") == "66098b69834c3c8b5e5b694f0059b8ed566e31d332f36be7e57baf70ae650823", "the office's rows up to the break differ from rung 3's"); - Check(Hash(string.Join('\n', lines) + "\n") == "006f712ff35052ff3f1497d67ecd6d20bd679de6bb3922714695a709fffa6ce4", $"office --seed 1 changed: {Hash(string.Join('\n', lines) + "\n")}"); + Check(Hash(string.Join('\n', lines) + "\n") == "b4e56329126e49192d2212b5acfcb63bd5691845571ff727fafec6745e7c42fc", $"office --seed 1 changed: {Hash(string.Join('\n', lines) + "\n")}"); var last = Out("office-sweep", "--seed", "1", "--count", "50").TrimEnd('\n').Split('\n')[^1]; Check(last == "runs 50: broke 50, endured 0; time to break min/median/max ms: 72670/86570/113420; min resolve of endured runs: -", $"sweep line changed: {last}"); }), - ("office rage: the Actor switches once, 100 ms after the break, KeepComposure before and Rage from it, and the run ends ReleaseSeconds after the switch", () => + ("office arc: the snap, then a rage on a refilled will, a second break 5-20 s later, and calm for good: every switch 100 ms after its break, nothing after calm, and the run ends ReleaseSeconds after calm", () => { - foreach (ulong seed in new ulong[] { 1, 2, 3, 4, 5 }) + for (ulong seed = 1; seed <= 20; seed++) { var r = Office(new StressParameters(), seed); - var brk = r.Timeline.Single(e => e.Kind == StressEntryKind.WillBroke); - var sw = r.Timeline.Single(e => e.Kind == StressEntryKind.GoalChanged); - Check(sw.TimeMs - brk.TimeMs == 100 && brk.TimeMs == r.TimeToBreakMs, $"seed {seed}: the switch row is {sw.TimeMs - brk.TimeMs} ms after the break"); - Check(r.Timeline.Where(e => e.TimeMs < sw.TimeMs).All(e => e.Goal == OfficeGoal.KeepComposure), $"seed {seed}: a row before the switch is not KeepComposure"); - Check(r.Timeline.Where(e => e.TimeMs >= sw.TimeMs).All(e => e.Goal == OfficeGoal.Rage), $"seed {seed}: a row from the switch on is not Rage"); - Check(r.Outcome == Outcome.WillBroke && r.EndMs == sw.TimeMs + 6000, $"seed {seed}: ended at {r.EndMs}, the switch was at {sw.TimeMs}"); - Check(r.Timeline.Where(e => e.TimeMs >= brk.TimeMs).All(e => e.Resolve == 0.0), $"seed {seed}: resolve came back after the break"); + var breaks = r.Timeline.Where(e => e.Kind == StressEntryKind.WillBroke).ToList(); + var switches = r.Timeline.Where(e => e.Kind == StressEntryKind.GoalChanged).ToList(); + Check(breaks.Count == 2 && switches.Count == 2, $"seed {seed}: {breaks.Count} breaks and {switches.Count} switches, wanted 2 and 2"); + var rage = switches[0]; + var calm = switches[1]; + Check(breaks[0].TimeMs == r.TimeToBreakMs, $"seed {seed}: the time to break is not the first break"); + Check(rage.Goal == OfficeGoal.Rage && rage.TimeMs - breaks[0].TimeMs == 100 && rage.Resolve == 1.0, $"seed {seed}: the rage row is {rage.Goal} {rage.TimeMs - breaks[0].TimeMs} ms after the snap with resolve {rage.Resolve}"); + long lasted = breaks[1].TimeMs - breaks[0].TimeMs; + Check(lasted >= 5000 && lasted <= 20000, $"seed {seed}: the second break came {lasted} ms after the snap"); + Check(calm.Goal == OfficeGoal.Calm && calm.TimeMs - breaks[1].TimeMs == 100, $"seed {seed}: the calm row is {calm.Goal} {calm.TimeMs - breaks[1].TimeMs} ms after the second break"); + Check(r.Timeline.Where(e => e.TimeMs < rage.TimeMs).All(e => e.Goal == OfficeGoal.KeepComposure), $"seed {seed}: a row before the rage is not KeepComposure"); + Check(r.Timeline.Where(e => e.TimeMs >= rage.TimeMs && e.TimeMs < calm.TimeMs).All(e => e.Goal == OfficeGoal.Rage), $"seed {seed}: a row in the rage is not Rage"); + Check(r.Timeline.Where(e => e.TimeMs >= calm.TimeMs).All(e => e.Goal == OfficeGoal.Calm), $"seed {seed}: a row from calm on is not Calm"); + Check(r.Outcome == Outcome.WillBroke && r.EndMs == calm.TimeMs + 6000, $"seed {seed}: ended at {r.EndMs}, calm was at {calm.TimeMs}"); } var short_ = Office(new StressParameters { ReleaseSeconds = 2.0 }, 1); - Check(short_.EndMs == short_.Timeline.Single(e => e.Kind == StressEntryKind.GoalChanged).TimeMs + 2000, "ReleaseSeconds did not set the length of the rage"); + Check(short_.EndMs == short_.Timeline.Single(e => e.Kind == StressEntryKind.GoalChanged && e.Goal == OfficeGoal.Calm).TimeMs + 2000, "ReleaseSeconds did not set the settle time"); var clipped = Office(new StressParameters { MaxSeconds = 85.0 }, 1); - Check(clipped.Outcome == Outcome.WillBroke && clipped.EndMs == 85000 && clipped.Timeline.All(e => e.Kind != StressEntryKind.Endured), $"a release past MaxSeconds ended at {clipped.EndMs}"); + Check(clipped.Outcome == Outcome.WillBroke && clipped.EndMs == 85000 && clipped.Timeline.All(e => e.Kind != StressEntryKind.Endured && e.Goal != OfficeGoal.Calm), $"a release past MaxSeconds ended at {clipped.EndMs}"); + }), + + ("office calm is terminal: with the release moved off calm the run goes to MaxSeconds, and after the Calm row the goal never changes again, whatever the senses and the mending will do", () => + { + // Bursts of heavy load with quiet between: the will breaks, mends in the quiet, and breaks again, which the office's steady load never does. + var Bursty = Cause.Office with { Stressors = new[] { new Stressor("printer", "printer", 5.0, 0.0, 100.0, 40.0, 1.0, 0.0) } }; + int thirdBreaks = 0; + for (ulong seed = 1; seed <= 5; seed++) + { + var r = Office(new StressParameters { ReleaseGoalId = "keep-composure", Cause = Bursty }, seed); + var calm = r.Timeline.Single(e => e.Kind == StressEntryKind.GoalChanged && e.Goal == OfficeGoal.Calm); + Check(r.EndMs == 180000, $"seed {seed}: ended at {r.EndMs}"); + Check(r.Timeline.Count(e => e.Kind == StressEntryKind.GoalChanged) == 2, $"seed {seed}: a goal changed after calm"); + Check(r.Timeline.Where(e => e.TimeMs >= calm.TimeMs).All(e => e.Goal == OfficeGoal.Calm), $"seed {seed}: the goal column left Calm"); + thirdBreaks += r.Timeline.Count(e => e.Kind == StressEntryKind.WillBroke && e.TimeMs > calm.TimeMs); + } + Check(thirdBreaks > 0, "no run broke the will a third time after calm; the test no longer exercises the rule"); }), ("office release: the run ends on the Brain's release goal, by id, not on any goal change; an id the Brain lacks is refused", () => { var r = Office(new StressParameters { ReleaseGoalId = "keep-composure" }, 1); - Check(r.Timeline.Single(e => e.Kind == StressEntryKind.GoalChanged).TimeMs == 83380 && r.EndMs == 180000 && r.Outcome == Outcome.WillBroke, - $"releasing on a goal the Brain never switches to ended the run at {r.EndMs}"); + Check(r.EndMs == 180000 && r.Outcome == Outcome.WillBroke, $"releasing on a goal the Brain never switches to ended the run at {r.EndMs}"); + var rage = Office(new StressParameters { ReleaseGoalId = OfficeBrain.RageId }, 1); + var sw = rage.Timeline.First(e => e.Kind == StressEntryKind.GoalChanged); + Check(sw.Goal == OfficeGoal.Rage && rage.EndMs == sw.TimeMs + 6000, $"releasing on the rage ended the run at {rage.EndMs}, the rage was at {sw.TimeMs}"); bool threw = false; - try { NewOffice(new StressParameters { ReleaseGoalId = "calm" }, 1); } catch (ArgumentException) { threw = true; } + try { NewOffice(new StressParameters { ReleaseGoalId = "nowhere" }, 1); } catch (ArgumentException) { threw = true; } Check(threw, "a release goal the Brain lacks was accepted"); }), @@ -843,7 +869,9 @@ public static class Program Check(a.Run.Render() == b.Run.Render() && a.HandL.SequenceEqual(b.HandL) && a.HandR.SequenceEqual(b.HandR) && a.Pelvis.SequenceEqual(b.Pelvis), "two body runs differ"); Check(a.Run.Outcome == Outcome.WillBroke && a.RageTick is not null, $"outcome {a.Run.Outcome}"); int sw = a.RageTick!.Value; - Check(a.HandL.Count == sw + 6 * Hz + 1, $"the run has {a.HandL.Count} steps, the switch is at {sw}"); + var calmRow = a.Run.Timeline.Single(e => e.Kind == StressEntryKind.GoalChanged && e.Goal == OfficeGoal.Calm); + int calmTick = (int)Math.Round(calmRow.TimeMs * Hz / 1000.0); + Check(a.HandL.Count == calmTick + 6 * Hz + 1, $"the run has {a.HandL.Count} steps, calm is at {calmTick}"); double Rms(IReadOnlyList h, int from, int to) { double sum = 0; @@ -869,6 +897,30 @@ public static class Program Check(drift < 0.01f, $"the pelvis moved {drift:F4} m"); }), + ("office body: the rage weakens as it drains his will (each hand slower in its last 2 s than in the 2 s after onset), and he settles: no hand moves faster than 0.05 m/s over the run's last second", () => + { + var p = new StressParameters { TickSeconds = OfficeBody.TickSeconds }; + for (ulong seed = 1; seed <= 3; seed++) + { + var a = OfficeBody.Run(p, seed); + int rage = a.RageTick!.Value; + int calm = (int)Math.Round(a.Run.Timeline.Single(e => e.Kind == StressEntryKind.GoalChanged && e.Goal == OfficeGoal.Calm).TimeMs * Hz / 1000.0); + double Speed(IReadOnlyList h, int i) + { + double dx = h[i].X - h[i - 1].X, dy = h[i].Y - h[i - 1].Y, dz = h[i].Z - h[i - 1].Z; + return Math.Sqrt(dx * dx + dy * dy + dz * dz) * Hz; + } + double Rms(IReadOnlyList h, int from, int to) => Math.Sqrt(Enumerable.Range(from + 1, to - from - 1).Average(i => Speed(h, i) * Speed(h, i))); + foreach (var (name, hand) in new[] { ("left", a.HandL), ("right", a.HandR) }) + { + double onset = Rms(hand, rage, rage + 2 * Hz), late = Rms(hand, calm - 2 * Hz, calm); + Check(late < onset, $"seed {seed} {name} hand: {late:F3} m/s in the rage's last 2 s, {onset:F3} m/s in the 2 s after onset"); + double still = Enumerable.Range(hand.Count - Hz, Hz).Max(i => Speed(hand, i)); + Check(still < 0.05, $"seed {seed} {name} hand: {still:F3} m/s at its fastest in the run's last second"); + } + } + }), + ("office body: the desk's emitters are sensed at the head, so the body breaks within 0.5 s of the headless run (sensed at a hand it is 3.6 s off)", () => { var body = OfficeBody.Run(new StressParameters { TickSeconds = OfficeBody.TickSeconds }, 1).Run;