diff --git a/src/Lodur.Core/Actor.cs b/src/Lodur.Core/Actor.cs index 84a4e18..07e5502 100644 --- a/src/Lodur.Core/Actor.cs +++ b/src/Lodur.Core/Actor.cs @@ -37,12 +37,13 @@ namespace Lodur public Actor( double tickSeconds, int regionCount, NerveParameters nerve, WithdrawalParameters withdrawal, DriveParameters drive, HoldParameters hold, - ReadOnlySpan holdPose, ReadOnlySpan withdrawPose) + ReadOnlySpan holdPose, ReadOnlySpan withdrawPose, + NerveParameters? senses = null, int senseRegion = 0) { _dt = tickSeconds; _driveP = drive; _withdrawLatency = Math.Max(1, (long)Math.Round(withdrawal.LatencySeconds / tickSeconds)); - _perception = new Perception(nerve, tickSeconds, regionCount); + _perception = new Perception(nerve, tickSeconds, regionCount, senses, senseRegion); _reflexes = new WithdrawalReflex[regionCount]; for (var r = 0; r < regionCount; r++) { @@ -67,19 +68,28 @@ namespace Lodur public double Afferent(int region) => _perception.Afferent(region); + /// The sensory load this tick: the channels' afferents combined, 0..1. + public double Load => _perception.Load; + + public double ChannelStimulus(Channel channel) => _perception.ChannelStimulus(channel); + + public double ChannelAfferent(Channel channel) => _perception.ChannelAfferent(channel); + /// /// Runs one physics step's worth of nervous system. The point spans hold one entry per region; the strengths - /// and targets spans one entry per muscle, written for the body to follow. + /// and targets spans one entry per muscle, written for the body to follow. The emitters are the game's point + /// sources, sensed at the sense region's point. /// public TickEvents Tick( ReadOnlySpan regionX, ReadOnlySpan regionY, ReadOnlySpan regionZ, - ReadOnlySpan sources, Span strengths, Span targets) + ReadOnlySpan sources, Span strengths, Span targets, + 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; - _perception.Sense(regionX, regionY, regionZ, sources); + _perception.Sense(regionX, regionY, regionZ, sources, emitters); double afferent = 0.0, strainLoad = 0.0, leak = 0.0; for (var r = 0; r < _reflexes.Length; r++) @@ -95,7 +105,7 @@ namespace Lodur } } - if (_drive.Step(_driveP, _dt, afferent, strainLoad)) + if (_drive.Step(_driveP, _dt, afferent, strainLoad, _perception.Load)) { events |= TickEvents.WillBroke; _withdrawTick = _tick + _withdrawLatency; diff --git a/src/Lodur.Core/DriveState.cs b/src/Lodur.Core/DriveState.cs index ed55aac..205c5e9 100644 --- a/src/Lodur.Core/DriveState.cs +++ b/src/Lodur.Core/DriveState.cs @@ -2,7 +2,7 @@ using System; namespace Lodur { - /// The drive's tunables: how pain and strain accumulate, and how they drain the will. + /// The drive's tunables: how pain, strain and arousal accumulate, and how they drain the will. public sealed record DriveParameters { public double PainRise { get; init; } = 1.0; @@ -18,25 +18,42 @@ namespace Lodur /// Per-character multiplier on resolve drain (the consumer draws its trait spread). public double DrainTrait { get; init; } = 1.0; + + /// How fast arousal rises toward 1 under full sensory load. + public double ArousalRise { get; init; } = 0.5; + + /// How fast arousal falls back once the load stops. + public double ArousalDecay { get; init; } = 0.1; + + /// Resolve drained per second at full arousal, before the objective's pull and the drain trait scale it. + public double ResolveDrainArousal { get; init; } = 0.1; + + /// Resolve recovers only while arousal is below this, as well as pain being low. + public double ResolveRecoversBelowArousal { get; init; } = 0.1; } /// /// Layer 3: per-character state that evolves every tick. Pain rises with afferent input, strain with the - /// reflex held down, resolve drains under both and recovers when pain is low. Resolve reaching zero breaks + /// 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. /// public struct DriveState { public double Pain { get; private set; } 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 Resolve { get; private set; } public bool WillBroken { get; private set; } internal static DriveState Fresh => new DriveState { Resolve = 1.0 }; /// Steps one tick; returns true on the tick the will breaks. - internal bool Step(DriveParameters p, double dt, double afferent, double strainLoad) + internal bool Step(DriveParameters p, double dt, double afferent, double strainLoad, double load) { + 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) @@ -44,8 +61,8 @@ namespace Lodur return false; } - var drain = (p.ResolveDrainPain * Pain + p.ResolveDrainStrain * Strain) * (1.0 - p.ObjectivePull) * p.DrainTrait; - var recovery = Pain < p.ResolveRecoversBelowPain ? p.ResolveRecovery * (1.0 - Resolve) : 0.0; + var drain = (p.ResolveDrainPain * Pain + p.ResolveDrainStrain * Strain + p.ResolveDrainArousal * Arousal) * (1.0 - p.ObjectivePull) * 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 (Resolve <= 0.0) { diff --git a/tests/Lodur.Core.Tests/ArousalTests.cs b/tests/Lodur.Core.Tests/ArousalTests.cs new file mode 100644 index 0000000..92d3325 --- /dev/null +++ b/tests/Lodur.Core.Tests/ArousalTests.cs @@ -0,0 +1,187 @@ +using Lodur; + +namespace Lodur.Core.Tests; + +/// An Actor with one region at the origin, a sensory nerve with sensitisation off, and a settable emitter list. +internal sealed class SensoryRig +{ + public const double Dt = 0.01; + + public readonly Actor Actor; + public readonly Emitter[] Emitters; + public readonly float[] Strengths = new float[2]; + public readonly Quat[] Targets = new Quat[2]; + private readonly float[] _p = { 0f }; + + /// False builds the Actor without senses, so the emitters have no one to reach. + public SensoryRig(Emitter[] emitters, double pull = 0.5, double drainTrait = 1.0, DriveParameters? drive = null, bool heard = true) + { + Emitters = emitters; + Actor = new Actor(Dt, 1, new NerveParameters(), new WithdrawalParameters(), + (drive ?? new DriveParameters()) with { ObjectivePull = pull, DrainTrait = drainTrait }, new HoldParameters(), + PainBoxReplay.Hold, PainBoxReplay.Withdraw, + heard ? new NerveParameters { Threshold = 0.05, SensitisationGrowth = 0.0 } : null, 0); + } + + /// One audio emitter on the sense region whose afferent settles at (intensity - 0.05). + public static Emitter[] One(double intensity) => new[] { new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, intensity) }; + + public TickEvents Tick() => Actor.Tick(_p, _p, _p, ReadOnlySpan.Empty, Strengths, Targets, Emitters); + + public long TicksToBreak(double maxSeconds) + { + var max = (long)Math.Round(maxSeconds / Dt); + for (long i = 0; i < max; i++) + { + if ((Tick() & TickEvents.WillBroke) != 0) return i; + } + + return -1; + } +} + +public sealed class ArousalTests +{ + private const double Load = 0.5; // an emitter of 0.55 through a 0.05 threshold, no sensitisation, gain 1 + + [Fact] + public void Arousal_rises_under_constant_load_toward_its_equilibrium_and_decays_at_the_decay_rate_after_it_stops() + { + var p = new DriveParameters(); + var rig = new SensoryRig(SensoryRig.One(0.55), pull: 1.0); // pull 1: no drain, so only arousal is watched + double last = 0; + for (var i = 0; i < 20000; i++) + { + rig.Tick(); + Assert.True(rig.Actor.Drive.Arousal >= last - 1e-15, "arousal never falls under constant load"); + last = rig.Actor.Drive.Arousal; + } + + Assert.Equal(Load, rig.Actor.Load, 12); + var equilibrium = p.ArousalRise * Load / (p.ArousalRise * Load + p.ArousalDecay); + Assert.Equal(equilibrium, rig.Actor.Drive.Arousal, 9); + + rig.Emitters[0].Intensity = 0.0; + while (rig.Actor.Load > 0.0) rig.Tick(); // the last of the signal in flight arrives + var a0 = rig.Actor.Drive.Arousal; + Assert.True(a0 > 0.5 * equilibrium); + for (var i = 0; i < 1000; i++) rig.Tick(); + Assert.Equal(a0 * Math.Pow(1.0 - SensoryRig.Dt * p.ArousalDecay, 1000), rig.Actor.Drive.Arousal, 12); + } + + [Fact] + public void Under_the_same_load_a_weaker_pull_breaks_first_pull_one_never_drains_and_the_trait_scales_the_drain() + { + long Break(double pull, double trait = 1.0) => new SensoryRig(SensoryRig.One(0.55), pull, trait).TicksToBreak(3000.0); + + long p3 = Break(0.3), p6 = Break(0.6); + Assert.True(p3 > 0 && p3 < p6, $"pull: {p3} {p6}"); + + var held = new SensoryRig(SensoryRig.One(0.55), pull: 1.0); + Assert.Equal(-1, held.TicksToBreak(3000.0)); + Assert.Equal(1.0, held.Actor.Drive.Resolve); + + long t2 = Break(0.6, 2.0); + Assert.True(t2 > 0 && t2 < p6, $"trait: {t2} {p6}"); + Assert.InRange((double)t2 / p6, 0.35, 0.6); // double the drain, about half the time (arousal needs a moment to rise) + } + + [Fact] + public void Resolve_does_not_recover_while_arousal_is_at_the_recovery_ceiling_or_above_and_does_after() + { + var drive = new DriveParameters { ResolveRecovery = 0.5 }; // strong enough that a gate on pain alone would show at once + var ceiling = drive.ResolveRecoversBelowArousal; + var rig = new SensoryRig(SensoryRig.One(0.55), pull: 0.5, drive: drive); + double minResolve = 1.0; + for (var i = 0; i < 2000; i++) { rig.Tick(); minResolve = Math.Min(minResolve, rig.Actor.Drive.Resolve); } + Assert.True(minResolve < 0.95 && !rig.Actor.Drive.WillBroken); + + rig.Emitters[0].Intensity = 0.0; + var rosePastCeiling = false; + var settled = false; + var last = rig.Actor.Drive.Resolve; + for (var i = 0; i < 20000; i++) + { + rig.Tick(); + var a = rig.Actor.Drive.Arousal; + var r = rig.Actor.Drive.Resolve; + if (a >= ceiling) + { + Assert.True(r <= last, $"resolve rose at arousal {a}"); + settled = true; + } + else if (settled && r > last) + { + rosePastCeiling = true; + } + + last = r; + } + + Assert.True(settled && rosePastCeiling, "resolve recovers once arousal is below the ceiling"); + Assert.True(rig.Actor.Drive.Resolve > minResolve); + } + + [Fact] + public void Without_emitters_or_without_senses_arousal_and_load_stay_exactly_zero() + { + var silent = new SensoryRig(Array.Empty()); + var deaf = new SensoryRig(SensoryRig.One(1.0), heard: false); + for (var i = 0; i < 10000; i++) + { + Assert.Equal(TickEvents.None, silent.Tick()); + Assert.Equal(TickEvents.None, deaf.Tick()); + Assert.Equal(0.0, silent.Actor.Drive.Arousal); + Assert.Equal(0.0, deaf.Actor.Drive.Arousal); + Assert.Equal(0.0, silent.Actor.Load); + Assert.Equal(0.0, deaf.Actor.Load); + } + + Assert.Equal(1.0, silent.Actor.Drive.Resolve); + Assert.Equal(1.0, deaf.Actor.Drive.Resolve); + } + + private static Emitter[] Three() => new[] + { + new Emitter(Channel.Audio, 0.2f, 0f, 0f, 1f, 0.5), + new Emitter(Channel.Visual, 0f, 0.3f, 0f, 1f, 0.4), + new Emitter(Channel.Interrupt, 0f, 0f, 0f, 1f, 0.6), + }; + + [Fact] + public void Tick_with_three_emitters_allocates_nothing_in_steady_state_and_actors_do_not_share_state() + { + var r = new SensoryRig(Three()); + r.Tick(); + var before = GC.GetAllocatedBytesForCurrentThread(); + for (var i = 0; i < 10000; i++) r.Tick(); + Assert.Equal(0L, GC.GetAllocatedBytesForCurrentThread() - before); + + const int n = 2500; + var a1 = new SensoryRig(Three()); + var b1 = new SensoryRig(SensoryRig.One(0.4), pull: 0.2, drainTrait: 1.3); + var a2 = new SensoryRig(Three()); + var b2 = new SensoryRig(SensoryRig.One(0.4), pull: 0.2, drainTrait: 1.3); + var ea = new TickEvents[n]; + var eb = new TickEvents[n]; + for (var i = 0; i < n; i++) { ea[i] = a1.Tick(); eb[i] = b1.Tick(); } + for (var i = 0; i < n; i++) Assert.Equal(ea[i], a2.Tick()); + for (var i = 0; i < n; i++) Assert.Equal(eb[i], b2.Tick()); + + static void Same(SensoryRig x, SensoryRig y) + { + Assert.Equal(x.Actor.Drive.Arousal, y.Actor.Drive.Arousal); + Assert.Equal(x.Actor.Drive.Resolve, y.Actor.Drive.Resolve); + Assert.Equal(x.Actor.Load, y.Actor.Load); + foreach (var c in new[] { Channel.Audio, Channel.Visual, Channel.Interrupt }) + { + Assert.Equal(x.Actor.ChannelStimulus(c), y.Actor.ChannelStimulus(c)); + Assert.Equal(x.Actor.ChannelAfferent(c), y.Actor.ChannelAfferent(c)); + } + } + + Same(a1, a2); + Same(b1, b2); + Assert.True(a1.Actor.Drive.Arousal > 0.0); + } +}