IBodyRun and the scenario seam: Main hosts the pain box or the office (one live run), a HUD scenario picker, the chamber hidden in the office

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-27 03:07:24 +01:00
commit f28c11a108
8 changed files with 90 additions and 25 deletions

View file

@ -25,8 +25,17 @@ public partial class Chamber : Node3D
/// <summary>The glow last drawn, 0 (dark) to 1.</summary>
public float Glow { get; private set; }
/// <summary>The office has no chamber: hidden, and the glow light it casts off.</summary>
public void Hide()
{
Visible = false;
Glow = 0f;
Look.Set("ps1_glow_energy", 0f);
}
public void Sync(BodyPainBoxSession session)
{
Visible = true;
double intensity = session.BoxIntensity; // 0 once the box is off
var box = session.Chamber;
Position = new Vector3(box.CenterX, box.CenterY, box.CenterZ);

View file

@ -17,6 +17,9 @@ public partial class Hud : CanvasLayer
public Hud(Main main) => _main = main;
private readonly VBoxContainer _chamberControls = new();
public OptionButton ScenarioPicker { get; } = new();
public SpinBox SeedBox { get; } = new() { MinValue = 0, MaxValue = 999999999, Step = 1, Value = 1 };
public Button ResetButton { get; } = new() { Text = "Reset" };
public HSlider IntensitySlider { get; } = new() { MinValue = 0, MaxValue = PainBox.MaxLevel, Step = 0.05, CustomMinimumSize = new Vector2(220, 24) };
@ -53,13 +56,19 @@ public partial class Hud : CanvasLayer
ResetButton.Pressed += () => _main.Reset((ulong)SeedBox.Value);
controls.AddChild(ResetButton);
controls.AddChild(new Label { Text = "Intensity", Modulate = Ink });
foreach (var scenario in System.Enum.GetValues<Scenario>()) ScenarioPicker.AddItem(scenario.ToString(), (int)scenario);
ScenarioPicker.Selected = (int)_main.Scenario;
ScenarioPicker.ItemSelected += i => _main.SetScenario((Scenario)ScenarioPicker.GetItemId((int)i));
controls.AddChild(ScenarioPicker);
controls.AddChild(_chamberControls);
_chamberControls.AddChild(new Label { Text = "Intensity", Modulate = Ink });
IntensitySlider.Value = _main.Intensity;
IntensitySlider.ValueChanged += v => _main.SetIntensity(v);
controls.AddChild(IntensitySlider);
_chamberControls.AddChild(IntensitySlider);
BoxOffButton.Pressed += () => _main.SwitchOff();
controls.AddChild(BoxOffButton);
_chamberControls.AddChild(BoxOffButton);
SlowToggle.Toggled += on => _main.SetSlowMotion(on);
controls.AddChild(SlowToggle);
}
@ -72,9 +81,17 @@ public partial class Hud : CanvasLayer
return;
}
var s = _main.Session;
var r = s.Result();
_chamberControls.Visible = _main.Scenario == Scenario.Chamber;
_text.Clear();
if (_main.Scenario == Scenario.Office)
{
_text.Append($"office seed {_main.Seed} t {_main.Run.TickIndex * OfficeBody.TickSeconds:F2} s (tick {_main.Run.TickIndex})\n");
_readout.Text = _text.ToString();
return;
}
var s = _main.PainBox;
var r = s.Result();
_text.Append($"seed {_main.Seed} t {s.TickIndex * BodyPainBox.TickSeconds:F2} s (tick {s.TickIndex})\n");
_text.Append($"box intensity {s.BoxIntensity:F2}{(s.BoxOn ? "" : " off")} (set {_main.Intensity:F2})\n");
_text.Append($"pain {s.Pain:F2} strain {s.Strain:F2} resolve {s.Resolve:F2}\n");

View file

@ -6,10 +6,13 @@ using Godot;
namespace BadDay.Client;
/// <summary>The runs the client hosts: the Synaptic Induction Chamber's pain box, or the office with its knife.</summary>
public enum Scenario { Chamber, Office }
/// <summary>
/// The client's root. Godot's physics frame decides when a tick happens (physics interpolation on, Godot's own
/// physics engine Dummy); <see cref="BodyPainBoxSession"/> decides everything a tick does. This node only steps it,
/// draws it and forwards the player's commands to it. A session that cannot start (the native library is missing, say)
/// physics engine Dummy); the live <see cref="IBodyRun"/> (the pain box or the office, one at a time) decides everything a tick does.
/// This node only steps whichever is live, draws it and forwards the player's commands to it. A session that cannot start (the native library is missing, say)
/// is reported once in <see cref="Failure"/> and the log, and nothing ticks until a Reset succeeds.
/// </summary>
public partial class Main : Node3D
@ -22,10 +25,19 @@ public partial class Main : Node3D
private Chamber _chamber = null!;
private OrbitCamera _camera = null!;
private Hud _hud = null!;
private BodyPainBoxSession? _session;
private IBodyRun? _run;
private double _intensity = new PainBoxParameters().BoxIntensity;
public BodyPainBoxSession Session => _session ?? throw new InvalidOperationException($"Main has no session ({Failure ?? "before _Ready"}).");
public Scenario Scenario { get; private set; } = Scenario.Chamber;
/// <summary>The live run, whichever scenario.</summary>
public IBodyRun Run => _run ?? throw new InvalidOperationException($"Main has no session ({Failure ?? "before _Ready"}).");
/// <summary>The live run as the pain box; throws in the office.</summary>
public BodyPainBoxSession PainBox => Run as BodyPainBoxSession ?? throw new InvalidOperationException("The live run is not the pain box.");
/// <summary>The live run as the office; throws in the chamber.</summary>
public OfficeBodySession Office => Run as OfficeBodySession ?? throw new InvalidOperationException("The live run is not the office.");
public BodyRenderer Bodies => _bodies;
public Chamber Chamber => _chamber;
public Hud Hud => _hud;
@ -37,6 +49,9 @@ public partial class Main : Node3D
/// <summary>The intensity the box is set to; the next Reset starts with it.</summary>
public double Intensity => _intensity;
/// <summary>The parameters of an office run: every default except the 1/120 s tick the body needs.</summary>
public static StressParameters OfficeParameters { get; } = new() { TickSeconds = OfficeBody.TickSeconds };
public bool SlowMotion { get; private set; }
/// <summary>Every event the session has reported since the last Reset, oldest first.</summary>
@ -96,28 +111,37 @@ public partial class Main : Node3D
{
if (IsInstanceValid(GetWindow())) GetWindow().SizeChanged -= SetLines;
Engine.PhysicsTicksPerSecond = StepsPerSecond(1.0);
_session?.Dispose();
_session = null;
_run?.Dispose();
_run = null;
}
public override void _PhysicsProcess(double delta)
{
if (_session is null) return;
_session.Step(); // one step of 1/120 s per physics frame; never Godot's delta, whatever the frame rate
if (_run is null) return;
_run.Step(); // one step of 1/120 s per physics frame; never Godot's delta, whatever the frame rate
Present(teleport: false);
}
/// <summary>Starts a new run from <paramref name="seed"/> at the current intensity, replacing the session.</summary>
/// <summary>Switches to <paramref name="scenario"/> and starts it at the current seed.</summary>
public void SetScenario(Scenario scenario)
{
Scenario = scenario;
Reset(Seed);
}
/// <summary>Starts a new run of the current scenario from <paramref name="seed"/> (the box at the current intensity), replacing the live run.</summary>
public void Reset(ulong seed)
{
_session?.Dispose();
_session = null;
_run?.Dispose();
_run = null;
Failure = null;
_events.Clear();
Seed = seed;
try
{
_session = new BodyPainBoxSession(ParametersFor(_intensity), seed);
_run = Scenario == Scenario.Office
? new OfficeBodySession(OfficeParameters, seed)
: new BodyPainBoxSession(ParametersFor(_intensity), seed);
}
catch (Exception e) // the session's start can fail in native code (library missing, ABI mismatch): report once, tick nothing
{
@ -131,11 +155,11 @@ public partial class Main : Node3D
/// <summary>Sets the box intensity now and for the next Reset.</summary>
public void SetIntensity(double level)
{
_session?.SetIntensity(level);
(_run as BodyPainBoxSession)?.SetIntensity(level);
_intensity = level;
}
public void SwitchOff() => _session?.SwitchOff();
public void SwitchOff() => (_run as BodyPainBoxSession)?.SwitchOff();
/// <summary>
/// Slow motion is fewer session steps per real second: Godot runs its physics frames at a quarter of the rate and each
@ -150,9 +174,9 @@ public partial class Main : Node3D
private void Present(bool teleport)
{
var s = Session;
var s = Run;
_bodies.Sync(s.View, teleport);
_chamber.Sync(s);
if (s is BodyPainBoxSession box) _chamber.Sync(box); else _chamber.Hide();
Span<BodyEvent> events = stackalloc BodyEvent[8];
int n;
while ((n = s.DrainEvents(events)) > 0)

View file

@ -30,7 +30,7 @@ public partial class RenderCheck : Node
private string? _frames;
private bool _done;
private BodyPainBoxSession Session => _main.Session;
private BodyPainBoxSession Session => _main.PainBox;
private int Block => Look.BlockSize(Height);
public override void _Ready()

View file

@ -26,7 +26,8 @@ public partial class Smoke : Node
private bool _done;
private Hud Hud => _main.Hud;
private BodyPainBoxSession Session => _main.Session;
private BodyPainBoxSession Session => _main.PainBox;
private OfficeBodySession Office => _main.Office;
public override void _Ready() => _ = RunAsync();

View file

@ -67,7 +67,7 @@ public static class BodyPainBox
/// client drains them with <see cref="DrainEvents"/>, so a frame that spans several steps loses none. After
/// <see cref="Dispose"/> every member throws <see cref="ObjectDisposedException"/>.
/// </summary>
public sealed class BodyPainBoxSession : IDisposable
public sealed class BodyPainBoxSession : IBodyRun
{
/// <summary>The box's half extent: the smallest 0.005 m step that keeps the hand in the box from the end of the ramp to the break on <see cref="BodyPainBox.Run"/> (on-body parameters, seed 1) at the hold pose below. Readings by half extent, ticks out from 2 s: 0.140 6, 0.145 3, 0.150 0, 0.155 0; excursions after the ramp: 0.150 1, 0.155 0.</summary>
private const float BoxHalf = 0.155f;

View file

@ -16,7 +16,7 @@ namespace BadDay.Ymir;
/// no strength) and the run ends a release after the death. After <see cref="Finished"/> <see cref="Step"/> only keeps
/// the body moving, as the chamber does.
/// </summary>
public sealed class OfficeBodySession : IDisposable
public sealed class OfficeBodySession : IBodyRun
{
private readonly SeatedBody _body;
private readonly StressSession _session;

View file

@ -12,6 +12,20 @@ public readonly record struct PartHit(int Part, BodyPoint Point, BodyPoint Norma
/// <summary>What the Actor reported on one tick of a body run; only ticks that reported something are queued.</summary>
public readonly record struct BodyEvent(long Tick, TickEvents Events);
/// <summary>What a client needs of any body run to step it, draw it and drain its events: the pain box and the office. A client holds exactly one.</summary>
public interface IBodyRun : IDisposable
{
/// <summary>The live lanes of the world, for rendering.</summary>
Box3DSoaView View { get; }
/// <summary>Ticks run so far; the tick the next <see cref="Step"/> runs.</summary>
long TickIndex { get; }
bool Finished { get; }
/// <summary>One step of 1/120 s.</summary>
void Step();
/// <summary>Moves the queued events (oldest first) into <paramref name="buffer"/>; returns how many.</summary>
int DrainEvents(Span<BodyEvent> buffer);
}
/// <summary>
/// The one owner of a BDS body world: the <see cref="Box3DSoaWorld"/> sized by <see cref="SeatedPuppet.Needs"/>, the
/// ragdoll <see cref="SeatedPuppet.Build"/> puts in it, the body's blood (<see cref="Blood"/>, the one writer of fluid particles and wet patches in the world), the sense-point, strength and target lanes, and the queue of