diff --git a/src/Lodur.Core/Actor.cs b/src/Lodur.Core/Actor.cs
index 2d492aa..b6ca217 100644
--- a/src/Lodur.Core/Actor.cs
+++ b/src/Lodur.Core/Actor.cs
@@ -7,9 +7,9 @@ namespace Lodur
public enum TickEvents
{
None = 0,
- /// A reflex leaked past the suppression the will still held. After the will breaks nothing is suppressed, so there is no flinch.
+ /// A reflex leaked past the suppression the will still held. With no will nothing is suppressed, so there is no flinch.
Flinch = 1,
- /// Resolve reached zero this tick.
+ /// Resolve reached zero this tick: the will broke (again, if it had mended).
WillBroke = 2,
/// The Brain's chosen goal took over, its deliberate latency after the choice.
GoalChanged = 4,
@@ -43,6 +43,11 @@ namespace Lodur
NerveParameters? senses = null, int senseRegion = 0)
{
brain.Validate();
+ if (!(drive.WillMendsAt > 0.0 && drive.WillMendsAt <= 1.0))
+ {
+ throw new ArgumentException("WillMendsAt must be in (0, 1].");
+ }
+
_dt = tickSeconds;
_driveP = drive;
_brain = brain;
@@ -100,8 +105,8 @@ namespace Lodur
ReadOnlySpan emitters = default)
{
var events = TickEvents.None;
- // A flinch is the leak of a suppressed reflex; once the will has broken nothing is suppressed, so none is reported.
- var holding = !_drive.WillBroken;
+ // A flinch is the leak of a suppressed reflex; with no will nothing is suppressed, so none is reported.
+ var holding = _drive.Will > 0.0;
_perception.Sense(regionX, regionY, regionZ, sources, emitters);
diff --git a/src/Lodur.Core/Brain.cs b/src/Lodur.Core/Brain.cs
index 095a1ca..90aad44 100644
--- a/src/Lodur.Core/Brain.cs
+++ b/src/Lodur.Core/Brain.cs
@@ -10,8 +10,10 @@ namespace Lodur
Strain,
Arousal,
Resolve,
- /// 1 once the will has broken, else 0.
+ /// 1 while the will is broken now (it mends), else 0.
WillBroken,
+ /// How many times the will has broken: the one-way latch, for a goal that must hold after a mend.
+ Breaks,
}
public enum ConditionOp
@@ -31,6 +33,7 @@ namespace Lodur
DriveQuantity.Strain => drive.Strain,
DriveQuantity.Arousal => drive.Arousal,
DriveQuantity.Resolve => drive.Resolve,
+ DriveQuantity.Breaks => (double)drive.Breaks,
_ => drive.WillBroken ? 1.0 : 0.0,
};
return Op == ConditionOp.AtLeast ? v >= Threshold : v < Threshold;
diff --git a/src/Lodur.Core/DriveState.cs b/src/Lodur.Core/DriveState.cs
index f6a6fd4..a164c0a 100644
--- a/src/Lodur.Core/DriveState.cs
+++ b/src/Lodur.Core/DriveState.cs
@@ -27,13 +27,17 @@ namespace Lodur
/// Resolve recovers only while arousal is below this, as well as pain being low.
public double ResolveRecoversBelowArousal { get; init; } = 0.1;
+
+ /// Once the will has broken it mends when resolve climbs back to this, in (0, 1]; it can then break again.
+ public double WillMendsAt { get; init; } = 0.25;
}
///
/// Layer 3: per-character state that evolves every tick. Pain rises with afferent input, strain with the
/// reflex held down, arousal with sensory load; resolve drains under all three and recovers when pain and
- /// arousal are both low. Resolve reaching zero breaks
- /// the will, and a broken will never mends. Only the owning steps it.
+ /// arousal are both low. Resolve reaching zero breaks the will (counted in ); the broken will
+ /// mends when resolve climbs back to and can break again. Resolve keeps
+ /// evolving through a break. Only the owning steps it.
///
public struct DriveState
{
@@ -43,27 +47,47 @@ namespace Lodur
/// Sensory load carried over time, 0..1; it rises under load and decays when quiet.
public double Arousal { get; private set; }
public double Resolve { get; private set; }
+
+ /// True from a break until the will mends: broken now, not "has ever broken" (see ).
public bool WillBroken { get; private set; }
+ /// How many times the will has broken.
+ public int Breaks { get; private set; }
+
+ /// The fraction of resolve that attention lets through, 0..1. A slot: nothing lowers it yet (lodur-startle-attention fills it).
+ public double Attention => 1.0;
+
+ /// The ceiling on the will that wakefulness sets, 0..1. A slot: nothing lowers it yet (lodur-physiology fills it).
+ public double Consciousness => 1.0;
+
+ /// The will that is available to hold reflexes down and to power voluntary behaviour: resolve through attention, capped by consciousness.
+ public double Will => Math.Min(Resolve * Attention, Consciousness);
+
internal static DriveState Fresh => new DriveState { Resolve = 1.0 };
- /// Steps one tick; returns true on the tick the will breaks. is the active goal's: it scales resolve drain by (1 - pull).
+ /// Steps one tick; returns true on the tick the will breaks (each break, not only the first). is the active goal's: it scales resolve drain by (1 - pull).
internal bool Step(DriveParameters p, double dt, double afferent, double strainLoad, double load, double pull)
{
Arousal = Math.Min(Math.Max(Arousal + dt * (p.ArousalRise * load * (1.0 - Arousal) - p.ArousalDecay * Arousal), 0.0), 1.0);
Pain = Math.Min(Math.Max(Pain + dt * (p.PainRise * afferent * (1.0 - Pain) - p.PainDecay * Pain), 0.0), 1.0);
Strain = Math.Max(0.0, Strain + dt * (strainLoad - p.StrainRecover * Strain));
- if (WillBroken)
- {
- return false;
- }
-
var drain = (p.ResolveDrainPain * Pain + p.ResolveDrainStrain * Strain + p.ResolveDrainArousal * Arousal) * (1.0 - pull) * p.DrainTrait;
var recovery = Pain < p.ResolveRecoversBelowPain && Arousal < p.ResolveRecoversBelowArousal ? p.ResolveRecovery * (1.0 - Resolve) : 0.0;
Resolve = Math.Min(Math.Max(Resolve + dt * (recovery - drain), 0.0), 1.0);
+ if (WillBroken)
+ {
+ if (Resolve >= p.WillMendsAt)
+ {
+ WillBroken = false;
+ }
+
+ return false;
+ }
+
if (Resolve <= 0.0)
{
WillBroken = true;
+ Breaks++;
return true;
}
diff --git a/src/Lodur.Core/WithdrawalReflex.cs b/src/Lodur.Core/WithdrawalReflex.cs
index 1ca0a70..f20c113 100644
--- a/src/Lodur.Core/WithdrawalReflex.cs
+++ b/src/Lodur.Core/WithdrawalReflex.cs
@@ -38,7 +38,7 @@ namespace Lodur
/// Steps once; returns true on a flinch (leak over the threshold, outside the refractory window).
public bool Step(double afferent, in DriveState drive)
{
- var suppression = drive.WillBroken ? 0.0 : _p.MaxSuppression * drive.Resolve;
+ var suppression = _p.MaxSuppression * drive.Will;
Leak = afferent * (1.0 - suppression);
StrainLoad = afferent * suppression;
var flinch = Leak > _p.FlinchLeakThreshold && _tick - _lastFlinch >= _refractory;
diff --git a/tests/Lodur.Core.Tests/BrainTests.cs b/tests/Lodur.Core.Tests/BrainTests.cs
index 7ae1451..84a9a29 100644
--- a/tests/Lodur.Core.Tests/BrainTests.cs
+++ b/tests/Lodur.Core.Tests/BrainTests.cs
@@ -292,6 +292,7 @@ public sealed class BrainTests
DriveQuantity.Strain => d.Strain,
DriveQuantity.Arousal => d.Arousal,
DriveQuantity.Resolve => d.Resolve,
+ DriveQuantity.Breaks => d.Breaks,
_ => d.WillBroken ? 1.0 : 0.0,
};
@@ -305,7 +306,7 @@ public sealed class BrainTests
/// quantity itself first crossed, and no other quantity read the same way may cross on that tick (or the scenario
/// could not tell a misread from the right read).
///
- private static void GateFollowsOnly(Func Tick, Func Drive)> rig, DriveQuantity q, ConditionOp op, double threshold, long ticks)
+ private static void GateFollowsOnly(Func Tick, Func Drive)> rig, DriveQuantity q, ConditionOp op, double threshold, long ticks, DriveQuantity? sameTickOk = null)
{
var (tick, drive) = rig(Gated(new Condition(q, op, threshold)));
bool Crossed(DriveState d, DriveQuantity of) => op == ConditionOp.AtLeast ? Read(d, of) >= threshold : Read(d, of) < threshold;
@@ -324,7 +325,7 @@ public sealed class BrainTests
Assert.Equal(own + Latency, changed);
foreach (var of in Enum.GetValues())
{
- if (of != q) Assert.False(firstOf.TryGetValue(of, out var other) && other == own, $"{of} crosses on the same tick as {q}: the scenario cannot tell them apart");
+ if (of != q && of != sameTickOk) Assert.False(firstOf.TryGetValue(of, out var other) && other == own, $"{of} crosses on the same tick as {q}: the scenario cannot tell them apart");
}
}
@@ -342,7 +343,8 @@ public sealed class BrainTests
[InlineData(DriveQuantity.Resolve, ConditionOp.Below, 0.9)]
[InlineData(DriveQuantity.WillBroken, ConditionOp.AtLeast, 1.0)]
public void Each_drive_quantity_a_condition_reads_is_that_quantity_in_the_pain_box(DriveQuantity q, ConditionOp op, double threshold) =>
- GateFollowsOnly(PainRig, q, op, threshold, 1700);
+ // The first break moves Breaks and WillBroken on one tick; WillTests reads Breaks at its second break, where they part.
+ GateFollowsOnly(PainRig, q, op, threshold, 1700, DriveQuantity.Breaks);
[Fact]
public void Arousal_a_condition_reads_is_arousal_where_pain_strain_and_resolve_do_not_move_with_it()
diff --git a/tests/Lodur.Core.Tests/PainBoxReplay.cs b/tests/Lodur.Core.Tests/PainBoxReplay.cs
index 4fe932c..8b7b99b 100644
--- a/tests/Lodur.Core.Tests/PainBoxReplay.cs
+++ b/tests/Lodur.Core.Tests/PainBoxReplay.cs
@@ -39,7 +39,7 @@ internal static class PainBoxBrain
new GoalSpec
{
Id = "release", Priority = 1.0, Pull = pull,
- When = new[] { new Condition(DriveQuantity.WillBroken, ConditionOp.AtLeast, 1.0) },
+ When = new[] { new Condition(DriveQuantity.Breaks, ConditionOp.AtLeast, 1.0) },
Behaviour = new PoseBehaviour
{
Keys = new[] { Withdraw }, FlinchPose = Withdraw,
diff --git a/tests/Lodur.Core.Tests/WillTests.cs b/tests/Lodur.Core.Tests/WillTests.cs
new file mode 100644
index 0000000..732773b
--- /dev/null
+++ b/tests/Lodur.Core.Tests/WillTests.cs
@@ -0,0 +1,134 @@
+using Lodur;
+
+namespace Lodur.Core.Tests;
+
+/// The will breaks when resolve reaches zero, mends when it climbs back to WillMendsAt, and can break again.
+public sealed class WillTests
+{
+ private static Brain Gate(DriveQuantity q, double threshold) => new()
+ {
+ Goals = new[]
+ {
+ new GoalSpec { Id = "objective", Behaviour = new PoseBehaviour { Keys = new[] { PainBoxBrain.Hold }, FlinchPose = PainBoxBrain.Withdraw } },
+ new GoalSpec
+ {
+ Id = "release", Priority = 1.0,
+ When = new[] { new Condition(q, ConditionOp.AtLeast, threshold) },
+ Behaviour = new PoseBehaviour { Keys = new[] { PainBoxBrain.Withdraw }, FlinchPose = PainBoxBrain.Withdraw },
+ },
+ },
+ };
+
+ /// Ticks the box on (or off) until the predicate holds or the cap runs out; counts WillBroke events on the way.
+ private static long Until(PainBoxReplay r, bool boxOn, Func done, ref int breaks, long cap = 40000)
+ {
+ for (long i = 0; i < cap; i++)
+ {
+ if ((r.Tick(boxOn) & TickEvents.WillBroke) != 0) breaks++;
+ if (done(r.Actor.Drive)) return i;
+ }
+
+ Assert.Fail("the predicate never held");
+ return -1;
+ }
+
+ [Fact]
+ public void A_will_that_mends_can_break_again_and_each_break_is_reported_and_counted_but_resolve_parked_at_zero_breaks_once()
+ {
+ var r = new PainBoxReplay(1, brain: Gate(DriveQuantity.Breaks, 99.0));
+ var breaks = 0;
+ Until(r, true, d => d.WillBroken, ref breaks);
+ Assert.Equal(1, breaks);
+ Assert.Equal(1, r.Actor.Drive.Breaks);
+
+ for (var i = 0; i < 3000; i++) r.Tick(true); // held under the box, resolve parked at zero
+ Assert.Equal(0.0, r.Actor.Drive.Resolve);
+ Assert.True(r.Actor.Drive.WillBroken);
+
+ for (var i = 0; i < 3000; i++) if ((r.Tick(true) & TickEvents.WillBroke) != 0) breaks++;
+ Assert.Equal(1, breaks);
+
+ Until(r, false, d => !d.WillBroken, ref breaks);
+ Assert.True(r.Actor.Drive.Resolve >= new DriveParameters().WillMendsAt);
+ Assert.Equal(1, r.Actor.Drive.Breaks);
+
+ Until(r, true, d => d.WillBroken, ref breaks);
+ Assert.Equal(2, breaks);
+ Assert.Equal(2, r.Actor.Drive.Breaks);
+ }
+
+ [Fact]
+ public void A_mend_waits_for_resolve_to_reach_WillMendsAt_not_merely_to_leave_zero()
+ {
+ var mends = new DriveParameters { WillMendsAt = 0.6 };
+ var r = new PainBoxReplay(1, drive: mends, brain: Gate(DriveQuantity.Breaks, 99.0));
+ var breaks = 0;
+ Until(r, true, d => d.WillBroken, ref breaks);
+ Until(r, false, d => d.Resolve > 0.0, ref breaks);
+ var mended = Until(r, false, d => !d.WillBroken, ref breaks);
+ Assert.True(mended > 0, "the will mended the moment resolve left zero");
+ Assert.True(r.Actor.Drive.Resolve >= 0.6);
+ }
+
+ [Theory]
+ [InlineData(DriveQuantity.Breaks, true)]
+ [InlineData(DriveQuantity.WillBroken, false)]
+ public void A_release_gated_on_Breaks_holds_after_the_will_mends_and_one_gated_on_WillBroken_returns_to_the_objective(DriveQuantity q, bool holds)
+ {
+ var r = new PainBoxReplay(1, brain: Gate(q, 1.0));
+ var breaks = 0;
+ Until(r, true, d => d.WillBroken, ref breaks);
+ for (var i = 0; i < 50; i++) r.Tick(true);
+ Assert.Equal(1, r.Actor.GoalIndex);
+
+ Until(r, false, d => !d.WillBroken, ref breaks);
+ for (var i = 0; i < 100; i++) r.Tick(false);
+ Assert.Equal(holds ? 1 : 0, r.Actor.GoalIndex);
+ }
+
+ [Fact]
+ public void A_condition_on_Breaks_at_two_follows_the_second_break_by_the_latency_not_the_first()
+ {
+ var r = new PainBoxReplay(1, brain: Gate(DriveQuantity.Breaks, 2.0));
+ var breaks = 0;
+ Until(r, true, d => d.WillBroken, ref breaks);
+ for (var i = 0; i < 100; i++) r.Tick(true);
+ Assert.Equal(0, r.Actor.GoalIndex);
+
+ Until(r, false, d => !d.WillBroken, ref breaks);
+ Until(r, true, d => d.Breaks == 2, ref breaks);
+ var second = r.Actor.TickCount;
+ long changed = -1;
+ for (var i = 0; i < 30 && changed < 0; i++)
+ {
+ if ((r.Tick(true) & TickEvents.GoalChanged) != 0) changed = r.Actor.TickCount;
+ }
+
+ Assert.Equal(second + 10, changed);
+ }
+
+ [Fact]
+ public void Will_is_resolve_bit_for_bit_while_attention_and_consciousness_are_slots_that_read_one()
+ {
+ var r = new PainBoxReplay(3);
+ for (var i = 0; i < 2500; i++)
+ {
+ r.Tick(true);
+ var d = r.Actor.Drive;
+ Assert.Equal(1.0, d.Attention);
+ Assert.Equal(1.0, d.Consciousness);
+ Assert.Equal(BitConverter.DoubleToInt64Bits(d.Resolve), BitConverter.DoubleToInt64Bits(d.Will));
+ }
+ }
+
+ [Theory]
+ [InlineData(0.0)]
+ [InlineData(-0.1)]
+ [InlineData(1.01)]
+ [InlineData(double.NaN)]
+ public void A_WillMendsAt_outside_zero_exclusive_to_one_is_refused(double mendsAt) =>
+ Assert.Throws(() => new PainBoxReplay(1, drive: new DriveParameters { WillMendsAt = mendsAt }));
+
+ [Fact]
+ public void A_WillMendsAt_of_one_is_allowed() => _ = new PainBoxReplay(1, drive: new DriveParameters { WillMendsAt = 1.0 });
+}
diff --git a/tests/Lodur.Tests/ConnectedPainBoxTests.cs b/tests/Lodur.Tests/ConnectedPainBoxTests.cs
index 85c89e1..4e92b36 100644
--- a/tests/Lodur.Tests/ConnectedPainBoxTests.cs
+++ b/tests/Lodur.Tests/ConnectedPainBoxTests.cs
@@ -57,7 +57,7 @@ internal sealed class ConnectedPainBox : IDisposable
new GoalSpec
{
Id = "release", Priority = 1.0, Pull = 0.5,
- When = new[] { new Condition(DriveQuantity.WillBroken, ConditionOp.AtLeast, 1.0) },
+ When = new[] { new Condition(DriveQuantity.Breaks, ConditionOp.AtLeast, 1.0) },
Behaviour = new PoseBehaviour
{
Keys = new[] { withdrawPose }, FlinchPose = withdrawPose,