diff --git a/docs/lodur-target.md b/docs/lodur-target.md
index feb814e..33c6462 100644
--- a/docs/lodur-target.md
+++ b/docs/lodur-target.md
@@ -68,6 +68,13 @@ Signal flows one way per tick, in this order; each layer has one owner. Layers 1
`HoldStrength x min(1, Will^WillCurve + leak x FlinchGain)`, an involuntary one at `HoldStrength`, so a failing
will slackens the layer while a reflex that gets past it still moves the muscle
(`lodur:ruling:bds-sandbox-arenas-will-economy`).
+ A goal can also carry a sensory gain and an arousal target (`SensoryGain`, `EntryArousal`), reached linearly over `EntrySeconds`
+ from their values at takeover (0, the default, is at the takeover tick; the Start goal's apply at construction). The gain multiplies the sense
+ channels' afferents as they leave the conduction delay, so sensitisation still follows the world; `Load` is built from the gated afferents and
+ `ChannelStimulus` stays the world's. The nociceptive region afferents, the withdrawal reflex and its flinch are never gated. The Actor owns the gain
+ in force (`DriveState.SensoryGain`) and, through the ramp, arousal; after it the drive owns arousal again. Calm uses it to silence the office's
+ stressors and drain the stress bar, tailing off over a few seconds rather than cutting
+ (`lodur:ruling:stress-bar-shows-arousal-calm-silences`, `lodur:ruling:flinch-tail-after-stimulus`).
A built Brain owns copies of its data and reads back as read-only views. Considerations extend it.
5. **Body.** Follows targets with per-muscle strength in the game's physics world. v1 is GameCult's own
PD-tracking active ragdoll on **Ymir**, GameCult's boundary around a pinned Box3D, read and driven through
diff --git a/src/Lodur.Core/Actor.cs b/src/Lodur.Core/Actor.cs
index cf7dcfb..981aa2f 100644
--- a/src/Lodur.Core/Actor.cs
+++ b/src/Lodur.Core/Actor.cs
@@ -29,6 +29,7 @@ namespace Lodur
private readonly long _latency;
private readonly long[] _dwell;
private readonly long[] _onset;
+ private readonly long[] _entryTicks;
private readonly Perception _perception;
private readonly WithdrawalReflex[] _reflexes;
private DriveState _drive = DriveState.Fresh;
@@ -37,6 +38,10 @@ namespace Lodur
private int _pending = -1;
private long _switchTick;
private long _goalSince;
+ // The entry ramp of the goal in force: its length in ticks (-1: none, as for the Start goal) and the gain and arousal it started from.
+ private long _rampTicks = -1;
+ private double _rampGain0;
+ private double _rampArousal0;
public Actor(
double tickSeconds, int regionCount,
@@ -51,6 +56,10 @@ namespace Lodur
_brain = brain;
_goal = brain.Start;
_drive.SensoryGain = brain.Goals[brain.Start].SensoryGain;
+ if (brain.Goals[brain.Start].EntryArousal is double startArousal)
+ {
+ _drive.Arousal = startArousal;
+ }
// The Start goal takes over at construction: its entry effect applies like any other goal's.
if (brain.Goals[brain.Start].EntryResolve is double startEntry)
{
@@ -59,6 +68,7 @@ namespace Lodur
_latency = Math.Max(1, (long)Math.Round(brain.DeliberateLatencySeconds / tickSeconds));
_dwell = new long[brain.Goals.Count];
_onset = new long[brain.Goals.Count];
+ _entryTicks = new long[brain.Goals.Count];
for (var g = 0; g < _dwell.Length; g++)
{
_dwell[g] = (long)Math.Round(brain.Goals[g].MinDwellSeconds / tickSeconds);
@@ -66,6 +76,9 @@ namespace Lodur
// A quotient past the long range (+infinity included) is involuntary throughout: the cast of an out-of-range double is not defined across runtimes.
var onsetTicks = brain.Goals[g].InvoluntarySeconds / tickSeconds;
_onset[g] = onsetTicks >= long.MaxValue ? long.MaxValue : (long)Math.Round(onsetTicks);
+ // The entry ramp is rounded once as well; EntrySeconds is finite, but its quotient can still pass the long range.
+ var entryTicks = brain.Goals[g].EntrySeconds / tickSeconds;
+ _entryTicks[g] = entryTicks >= long.MaxValue ? long.MaxValue : (long)Math.Round(entryTicks);
}
_perception = new Perception(nerve, tickSeconds, regionCount, senses, senseRegion);
@@ -166,13 +179,29 @@ namespace Lodur
_pending = -1;
_goalSince = _tick;
events |= TickEvents.GoalChanged;
- _drive.SensoryGain = _brain.Goals[_goal].SensoryGain;
+ _rampTicks = _entryTicks[_goal];
+ _rampGain0 = _drive.SensoryGain;
+ _rampArousal0 = _drive.Arousal;
if (_brain.Goals[_goal].EntryResolve is double entry)
{
_drive.Enter(_driveP, entry);
}
}
+ // For EntrySeconds after a takeover the Actor, not the drive, takes the gain (and the arousal, if the goal declares one) from their values at
+ // takeover to the goal's targets, linearly; it ends exactly on them, and after it the drive owns arousal again. Sense reads the gain at the start of the next tick.
+ var sinceGoal = _tick - _goalSince;
+ if (sinceGoal <= _rampTicks)
+ {
+ var goal = _brain.Goals[_goal];
+ var k = _rampTicks == 0 ? 1.0 : (double)sinceGoal / _rampTicks;
+ _drive.SensoryGain = _rampGain0 + (goal.SensoryGain - _rampGain0) * k;
+ if (goal.EntryArousal is double target)
+ {
+ _drive.Arousal = _rampArousal0 + (target - _rampArousal0) * k;
+ }
+ }
+
// Every voluntary layer is held by will, in contest with the reflex; an involuntary one (the onset of a goal) is not.
var secondsInGoal = (_tick - _goalSince) * _dt;
var weight = _tick - _goalSince >= _onset[_goal] ? Math.Pow(_drive.Will, _driveP.WillCurve) : 1.0;
diff --git a/src/Lodur.Core/Brain.cs b/src/Lodur.Core/Brain.cs
index a90c635..e3f2c38 100644
--- a/src/Lodur.Core/Brain.cs
+++ b/src/Lodur.Core/Brain.cs
@@ -77,6 +77,19 @@ namespace Lodur
///
public double SensoryGain { get; init; } = 1.0;
+ ///
+ /// The arousal this goal brings, in [0, 1]; null leaves arousal to the drive. Calm drops it so the stress bar drains
+ /// (lodur:ruling:stress-bar-shows-arousal-calm-silences). Reached over , then the drive owns arousal again.
+ ///
+ public double? EntryArousal { get; init; }
+
+ ///
+ /// How long after it takes over the goal's and are reached, finite and not negative, default 0 (at the
+ /// takeover tick): a linear ramp from their values at takeover, rounded to whole ticks once like the onset. The aftershock tail
+ /// of lodur:ruling:flinch-tail-after-stimulus. It does not ramp , which stays a snap. For the Start goal the targets apply at construction.
+ ///
+ public double EntrySeconds { get; init; }
+
///
/// How long after it takes over the goal runs without will (rounded to whole ticks once, like the dwell), not negative, default 0 (voluntary from the first tick);
/// +infinity is involuntary throughout. While involuntary the behaviour writes at full weight and pays no Effort;
@@ -212,6 +225,16 @@ namespace Lodur
throw new ArgumentException($"Goal {goal.Id}: SensoryGain must be in [0, 1].");
}
+ if (goal.EntryArousal is double entryArousal && !(entryArousal >= 0.0 && entryArousal <= 1.0))
+ {
+ throw new ArgumentException($"Goal {goal.Id}: EntryArousal must be in [0, 1].");
+ }
+
+ if (!(goal.EntrySeconds >= 0.0 && double.IsFinite(goal.EntrySeconds)))
+ {
+ throw new ArgumentException($"Goal {goal.Id}: EntrySeconds must be finite and not negative.");
+ }
+
if (!(goal.Effort >= 0.0 && double.IsFinite(goal.Effort)))
{
throw new ArgumentException($"Goal {goal.Id}: Effort must be finite and not negative.");
diff --git a/src/Lodur.Core/DriveState.cs b/src/Lodur.Core/DriveState.cs
index caa3016..0fe8cfe 100644
--- a/src/Lodur.Core/DriveState.cs
+++ b/src/Lodur.Core/DriveState.cs
@@ -81,7 +81,7 @@ namespace Lodur
public double Strain { get; private set; }
/// Sensory load carried over time, 0..1; it rises under load and decays when quiet.
- public double Arousal { get; private set; }
+ public double Arousal { get; internal set; }
public double Resolve { get; private set; }
/// True from a break until the will mends: broken now, not "has ever broken" (see ).
diff --git a/tests/Lodur.Core.Tests/EntryRampTests.cs b/tests/Lodur.Core.Tests/EntryRampTests.cs
new file mode 100644
index 0000000..8f2ec9f
--- /dev/null
+++ b/tests/Lodur.Core.Tests/EntryRampTests.cs
@@ -0,0 +1,196 @@
+using Lodur;
+
+namespace Lodur.Core.Tests;
+
+///
+/// The entry ramp (lodur:ruling:flinch-tail-after-stimulus): a goal's arousal and sensory-gain targets are reached linearly over EntrySeconds from
+/// their values at takeover, so the stress bar and the flinch drive tail off rather than cut; after it the drive owns arousal.
+///
+public sealed class EntryRampTests
+{
+ private const double Dt = 0.01;
+ private const double Afferent = 0.5; // an emitter of 0.55 through a 0.05 threshold, no sensitisation, gain 1
+ private static readonly PoseBehaviour Pose = new() { Keys = new[] { PainBoxBrain.Hold }, FlinchPose = PainBoxBrain.Withdraw };
+
+ /// Stress is the Start goal; calm takes over once arousal reaches 0.2 and holds (its dwell outlasts every test).
+ private static Brain StressThenCalm(double entrySeconds, double gain = 0.0, double? arousal = 0.0) => new()
+ {
+ Goals = new[]
+ {
+ new GoalSpec { Id = "stress", Behaviour = Pose },
+ new GoalSpec
+ {
+ Id = "calm", Priority = 1.0, MinDwellSeconds = 1000.0, SensoryGain = gain, EntryArousal = arousal, EntrySeconds = entrySeconds,
+ When = new[] { new Condition(DriveQuantity.Arousal, ConditionOp.AtLeast, 0.2) }, Behaviour = Pose,
+ },
+ },
+ };
+
+ private sealed class Rig
+ {
+ public readonly Actor Actor;
+ private readonly Emitter[] _emitters = { new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 0.55) };
+ private readonly BoxStimulus[] _box = { new BoxStimulus(0f, 0f, 0f, 1f, 1f, 1f) };
+ private readonly float[] _p = { 0f };
+ private readonly float[] _strengths = new float[2];
+ private readonly Quat[] _targets = new Quat[2];
+
+ public Rig(Brain brain) =>
+ Actor = new Actor(Dt, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(), brain,
+ new NerveParameters { Threshold = 0.05, SensitisationGrowth = 0.0 }, 0);
+
+ public double Pain { set => _box[0].Intensity = value; }
+
+ public TickEvents Tick() => Actor.Tick(_p, _p, _p, _box, _strengths, _targets, _emitters);
+
+ /// Ticks until the goal at takes over; the takeover tick has just run.
+ public void UntilTakeover(int goal)
+ {
+ for (var i = 0; i < 20000; i++)
+ {
+ if ((Tick() & TickEvents.GoalChanged) != 0 && Actor.GoalIndex == goal) return;
+ }
+
+ Assert.Fail("the goal never took over");
+ }
+ }
+
+ [Fact]
+ public void A_three_second_ramp_takes_arousal_and_load_linearly_to_exactly_zero_on_its_last_tick_and_not_before_and_they_stay_zero_under_steady_emitters()
+ {
+ var rig = new Rig(StressThenCalm(3.0));
+ rig.UntilTakeover(1);
+ var a0 = rig.Actor.Drive.Arousal;
+ Assert.True(a0 >= 0.2);
+ Assert.Equal(1.0, rig.Actor.Drive.SensoryGain);
+ const int ticks = 300;
+ for (var k = 1; k <= ticks + 500; k++)
+ {
+ rig.Tick();
+ var ramp = Math.Max(0.0, 1.0 - (double)k / ticks);
+ // Sense reads the gain the ramp left at the end of the previous tick.
+ var senseGain = Math.Max(0.0, 1.0 - (double)(k - 1) / ticks);
+ Assert.Equal(a0 * ramp, rig.Actor.Drive.Arousal, 12);
+ Assert.Equal(ramp, rig.Actor.Drive.SensoryGain, 12);
+ Assert.Equal(Afferent * senseGain, rig.Actor.Load, 12);
+ Assert.Equal(0.55, rig.Actor.ChannelStimulus(Channel.Audio), 12); // the world keeps sounding
+ if (k < ticks) Assert.True(rig.Actor.Drive.Arousal > 0.0 && rig.Actor.Drive.SensoryGain > 0.0, $"still above zero at {k * Dt:0.00} s");
+ if (k >= ticks)
+ {
+ Assert.Equal(0.0, rig.Actor.Drive.Arousal);
+ Assert.Equal(0.0, rig.Actor.Drive.SensoryGain);
+ }
+
+ if (k > ticks) Assert.Equal(0.0, rig.Actor.Load);
+ }
+ }
+
+ [Fact]
+ public void A_ramp_of_zero_seconds_reaches_the_targets_on_the_takeover_tick()
+ {
+ var rig = new Rig(StressThenCalm(0.0));
+ rig.UntilTakeover(1);
+ Assert.Equal(0.0, rig.Actor.Drive.Arousal);
+ Assert.Equal(0.0, rig.Actor.Drive.SensoryGain);
+ for (var i = 0; i < 300; i++)
+ {
+ rig.Tick();
+ Assert.Equal(0.0, rig.Actor.Load);
+ Assert.Equal(0.0, rig.Actor.Drive.Arousal);
+ }
+ }
+
+ [Fact]
+ public void A_goal_without_an_entry_arousal_ramps_only_the_gain_and_the_drive_keeps_arousal()
+ {
+ var rig = new Rig(StressThenCalm(2.0, gain: 0.0, arousal: null));
+ rig.UntilTakeover(1);
+ var before = rig.Actor.Drive.Arousal;
+ rig.Tick();
+ // One tick of the drive's own law under the gate (the sense still reads gain 1 on the first tick after takeover): arousal moves by it, not by any ramp.
+ var p = new DriveParameters();
+ var expected = Math.Min(Math.Max(before + Dt * (p.ArousalRise * Afferent * (1.0 - before) - p.ArousalDecay * before), 0.0), 1.0);
+ Assert.Equal(expected, rig.Actor.Drive.Arousal, 12);
+ for (var i = 0; i < 6000; i++) rig.Tick();
+ Assert.Equal(0.0, rig.Actor.Drive.SensoryGain);
+ Assert.True(rig.Actor.Drive.Arousal < 0.01, "with the gain at zero the drive decays arousal on its own");
+ }
+
+ [Fact]
+ public void A_goal_that_takes_over_mid_ramp_ramps_from_where_the_last_ramp_had_got_to()
+ {
+ var brain = new Brain
+ {
+ Goals = new[]
+ {
+ new GoalSpec { Id = "stress", Behaviour = Pose },
+ new GoalSpec
+ {
+ Id = "calm", Priority = 1.0, MinDwellSeconds = 1000.0, SensoryGain = 0.0, EntryArousal = 0.0, EntrySeconds = 3.0,
+ When = new[] { new Condition(DriveQuantity.Arousal, ConditionOp.AtLeast, 0.2) }, Behaviour = Pose,
+ },
+ new GoalSpec
+ {
+ Id = "alert", Priority = 2.0, SensoryGain = 1.0, EntryArousal = 0.9, EntrySeconds = 2.0,
+ When = new[] { new Condition(DriveQuantity.Pain, ConditionOp.AtLeast, 0.05) }, Behaviour = Pose,
+ },
+ },
+ };
+ var rig = new Rig(brain);
+ rig.UntilTakeover(1);
+ for (var i = 0; i < 100; i++) rig.Tick(); // 1 s into the 3 s ramp
+ Assert.True(rig.Actor.Drive.SensoryGain is > 0.0 and < 1.0);
+ rig.Pain = 0.8;
+ rig.UntilTakeover(2);
+ var g0 = rig.Actor.Drive.SensoryGain;
+ var a0 = rig.Actor.Drive.Arousal;
+ Assert.True(g0 is > 0.0 and < 0.7, "alert took over partway down calm's ramp");
+ for (var k = 1; k <= 200; k++)
+ {
+ rig.Tick();
+ var f = k / 200.0;
+ Assert.Equal(g0 + (1.0 - g0) * f, rig.Actor.Drive.SensoryGain, 12);
+ Assert.Equal(a0 + (0.9 - a0) * f, rig.Actor.Drive.Arousal, 12);
+ }
+
+ Assert.Equal(1.0, rig.Actor.Drive.SensoryGain);
+ }
+
+ [Fact]
+ public void The_start_goals_targets_hold_from_construction_and_under_steady_emitters()
+ {
+ var brain = new Brain { Goals = new[] { new GoalSpec { Id = "calm", SensoryGain = 0.0, EntryArousal = 0.0, EntrySeconds = 3.0, Behaviour = Pose } } };
+ var rig = new Rig(brain);
+ Assert.Equal(0.0, rig.Actor.Drive.SensoryGain);
+ Assert.Equal(0.0, rig.Actor.Drive.Arousal);
+ for (var i = 0; i < 600; i++)
+ {
+ rig.Tick();
+ Assert.Equal(0.0, rig.Actor.Load);
+ Assert.Equal(0.0, rig.Actor.Drive.Arousal);
+ }
+
+ var warm = new Rig(new Brain { Goals = new[] { new GoalSpec { Id = "wound", EntryArousal = 0.6, Behaviour = Pose } } });
+ Assert.Equal(0.6, warm.Actor.Drive.Arousal);
+ }
+
+ [Fact]
+ public void EntryArousal_outside_zero_to_one_and_a_negative_NaN_or_infinite_EntrySeconds_are_refused_naming_the_goal()
+ {
+ void Refused(GoalSpec g)
+ {
+ var ex = Assert.Throws(() => new Actor(Dt, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(), new Brain { Goals = new[] { g } }));
+ Assert.Contains("bad", ex.Message);
+ }
+
+ GoalSpec Base() => new() { Id = "bad", Behaviour = Pose };
+ Refused(Base() with { EntryArousal = -0.01 });
+ Refused(Base() with { EntryArousal = 1.01 });
+ Refused(Base() with { EntryArousal = double.NaN });
+ Refused(Base() with { EntrySeconds = -0.01 });
+ Refused(Base() with { EntrySeconds = double.NaN });
+ Refused(Base() with { EntrySeconds = double.PositiveInfinity });
+ _ = new Actor(Dt, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(), new Brain { Goals = new[] { Base() with { EntryArousal = 0.0, EntrySeconds = 5.0 } } });
+ _ = new Actor(Dt, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(), new Brain { Goals = new[] { Base() with { EntryArousal = 1.0 } } });
+ }
+}