diff --git a/src/BadDay.Ymir/BodyPainBox.cs b/src/BadDay.Ymir/BodyPainBox.cs index 910f2b1..4abf72e 100644 --- a/src/BadDay.Ymir/BodyPainBox.cs +++ b/src/BadDay.Ymir/BodyPainBox.cs @@ -53,12 +53,9 @@ public static class BodyPainBox } } -/// What the Actor reported on one tick of a ; only ticks that reported something are queued. -public readonly record struct BodyEvent(long Tick, TickEvents Events); - /// -/// One body run, stepped by its caller: BDS creates the Ymir world ('s ground, stool and ragdoll, the box volume around the right hand's hold -/// point) and is its only stepper; the nervous session inside it is never exposed, so nothing else can tick it. +/// One body run, stepped by its caller: it holds a , the one owner of the world, and adds the box volume around the right hand's hold +/// point; the nervous session inside it is never exposed, so nothing else can tick it. /// Per : the box writes its intensity, the body reports its hand points, Lodur's Actor decides, /// the ragdoll is driven, the world steps. 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. After @@ -79,15 +76,8 @@ public sealed class BodyPainBoxSession : IDisposable private const double SettleSeconds = 1.0; private readonly PainBoxParameters _p; - private readonly Box3DSoaWorld _world; - private readonly Ragdoll _ragdoll; + private readonly SeatedBody _body; private readonly PainBoxSession _session; - private readonly Queue _events = new(); - private readonly float[] _x; - private readonly float[] _y; - private readonly float[] _z; - private readonly float[] _strengths; - private readonly Quat[] _targets; private readonly int _handR; private readonly long _rampEnd; private readonly long _settleTicks; @@ -108,18 +98,11 @@ public sealed class BodyPainBoxSession : IDisposable } _p = p; - var needs = SeatedPuppet.Needs; - _world = new Box3DSoaWorld(capacity: needs.Bodies, jointCapacity: needs.Joints); + _body = new SeatedBody(); try { - _ragdoll = SeatedPuppet.Build(_world); - var plan = _ragdoll.Plan; + var plan = _body.Plan; int senses = plan.SensePoints.Count, joints = plan.Joints.Count; - _x = new float[senses]; - _y = new float[senses]; - _z = new float[senses]; - _strengths = new float[joints]; - _targets = new Quat[joints]; _handR = plan.SenseIndex("Hand", Side.Right); int shoulderR = plan.JointIndex("Shoulder", Side.Right), elbowR = plan.JointIndex("Elbow", Side.Right); @@ -139,7 +122,7 @@ public sealed class BodyPainBoxSession : IDisposable } catch { - _world.Dispose(); + _body.Dispose(); throw; } @@ -150,7 +133,7 @@ public sealed class BodyPainBoxSession : IDisposable } /// The live lanes of the world, for rendering. Read-only: nothing outside this session writes them. - public Box3DSoaView View { get { Live(); return _world.View; } } + public Box3DSoaView View { get { Live(); return _body.View; } } /// The right hand after the latest step (the rest pose before the first). public HandSample Hand { get { Live(); return _hand; } } @@ -191,14 +174,13 @@ public sealed class BodyPainBoxSession : IDisposable var events = TickEvents.None; if (ticked) { - _ragdoll.ReadSensePoints(_x, _y, _z); - events = _session.Tick(_x, _y, _z, _strengths, _targets); - if (events != TickEvents.None) _events.Enqueue(new BodyEvent(tick, events)); + _body.ReadSenses(); + events = _session.Tick(_body.X, _body.Y, _body.Z, _body.Strengths, _body.Targets); + if (events != TickEvents.None) _body.Enqueue(tick, events); } if (ticked || wasFinished) { - _ragdoll.Drive(_strengths, _targets); // after Finished: the last strengths and targets again - _world.Step((float)_p.TickSeconds, SeatedPuppet.Substeps); + _body.DriveAndStep(_p.TickSeconds); // after Finished: the last strengths and targets again } var sample = ReadHand(); // the rest pose when the box was off at tick 0 @@ -233,17 +215,8 @@ public sealed class BodyPainBoxSession : IDisposable } } - /// - /// Moves the queued events (oldest first) into and returns how many it wrote; the rest stay - /// queued for the next call. Allocation-free once the queue has grown to the client's drain interval. - /// - public int DrainEvents(Span buffer) - { - Live(); - int n = 0; - while (n < buffer.Length && _events.Count > 0) buffer[n++] = _events.Dequeue(); - return n; - } + /// Moves the queued events (oldest first) into ; see . + public int DrainEvents(Span buffer) => _body.DrainEvents(buffer); /// Sets the box intensity from the next tick; refuses a level outside 0 to . public void SetIntensity(double level) @@ -271,15 +244,15 @@ public sealed class BodyPainBoxSession : IDisposable { if (_disposed) return; _disposed = true; - _world.Dispose(); + _body.Dispose(); } private void Live() => ObjectDisposedException.ThrowIf(_disposed, this); private HandSample ReadHand() { - _ragdoll.ReadSensePoints(_x, _y, _z); - float hx = _x[_handR], hy = _y[_handR], hz = _z[_handR]; + _body.ReadSenses(); + float hx = _body.X[_handR], hy = _body.Y[_handR], hz = _body.Z[_handR]; return new HandSample(hx, hy, hz, _session.Box.Contains(hx, hy, hz)); } } diff --git a/src/BadDay.Ymir/SeatedBody.cs b/src/BadDay.Ymir/SeatedBody.cs new file mode 100644 index 0000000..66c0411 --- /dev/null +++ b/src/BadDay.Ymir/SeatedBody.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using Lodur; +using Lodur.Body.Ymir; +using Ymir.Box3D; + +namespace BadDay.Ymir; + +/// What the Actor reported on one tick of a body run; only ticks that reported something are queued. +public readonly record struct BodyEvent(long Tick, TickEvents Events); + +/// +/// The one owner of a BDS body world: the sized by , the +/// ragdoll puts in it, the sense-point, strength and target lanes, and the queue of +/// the Actor's events. Every body run (the pain box, the office) holds one and decides nothing here: per tick the run +/// calls , lets its scenario write and from the +/// points, then . No other code in BDS creates or steps a world. After +/// every member throws . +/// +public sealed class SeatedBody : IDisposable +{ + private readonly Box3DSoaWorld _world; + private readonly Ragdoll _ragdoll; + private readonly float[] _x, _y, _z; + private readonly float[] _strengths; + private readonly Quat[] _targets; + private readonly Queue _events = new(); + private bool _disposed; + + public SeatedBody() + { + var needs = SeatedPuppet.Needs; + _world = new Box3DSoaWorld(capacity: needs.Bodies, jointCapacity: needs.Joints); + try + { + _ragdoll = SeatedPuppet.Build(_world); + var plan = _ragdoll.Plan; + _x = new float[plan.SensePoints.Count]; + _y = new float[plan.SensePoints.Count]; + _z = new float[plan.SensePoints.Count]; + _strengths = new float[plan.Joints.Count]; + _targets = new Quat[plan.Joints.Count]; + } + catch + { + _world.Dispose(); + throw; + } + } + + public BodyPlan Plan => _ragdoll.Plan; + + /// The ragdoll, for reading the lane of a part (); the world is stepped only through . + public Ragdoll Ragdoll { get { Live(); return _ragdoll; } } + + /// The live lanes of the world, for rendering. Read-only: nothing outside this body writes them. + public Box3DSoaView View { get { Live(); return _world.View; } } + + /// The sense points as of the latest . + public ReadOnlySpan X { get { Live(); return _x; } } + public ReadOnlySpan Y { get { Live(); return _y; } } + public ReadOnlySpan Z { get { Live(); return _z; } } + + /// The muscle strengths and targets the scenario writes and applies; they persist between ticks. + public Span Strengths { get { Live(); return _strengths; } } + public Span Targets { get { Live(); return _targets; } } + + /// Reads the body's sense points from the world. + public void ReadSenses() + { + Live(); + _ragdoll.ReadSensePoints(_x, _y, _z); + } + + /// Drives the ragdoll with the current strengths and targets, then steps the world one seconds. + public void DriveAndStep(double dt) + { + Live(); + _ragdoll.Drive(_strengths, _targets); + _world.Step((float)dt, SeatedPuppet.Substeps); + } + + public void Enqueue(long tick, TickEvents events) + { + Live(); + _events.Enqueue(new BodyEvent(tick, events)); + } + + /// + /// Moves the queued events (oldest first) into and returns how many it wrote; the rest stay + /// queued for the next call. Allocation-free once the queue has grown to the client's drain interval. + /// + public int DrainEvents(Span buffer) + { + Live(); + int n = 0; + while (n < buffer.Length && _events.Count > 0) buffer[n++] = _events.Dequeue(); + return n; + } + + /// Frees the world; a second call does nothing. + public void Dispose() + { + if (_disposed) return; + _disposed = true; + _world.Dispose(); + } + + private void Live() => ObjectDisposedException.ThrowIf(_disposed, this); +}