diff --git a/src/Lodur.Core/Perception.cs b/src/Lodur.Core/Perception.cs index c1e4f5a..876726b 100644 --- a/src/Lodur.Core/Perception.cs +++ b/src/Lodur.Core/Perception.cs @@ -26,7 +26,44 @@ namespace Lodur MathF.Abs(x - CenterX) <= HalfX && MathF.Abs(y - CenterY) <= HalfY && MathF.Abs(z - CenterZ) <= HalfZ; } - /// The nerve's tunables: threshold, sensitisation under sustained stimulus, conduction delay, and a per-character gain. + /// The sensory channels a point emitter can speak on. Each has its own stimulus, sensitisation and nerve. + public enum Channel + { + Audio = 0, + Visual = 1, + Interrupt = 2, + } + + /// + /// A game stimulus at a point: sound, flicker, a notification. The game writes its position and intensity every + /// tick; Lodur decides what reaches the sense region. What arrives is the intensity falling off linearly to zero + /// at the radius. + /// + public struct Emitter + { + public Channel Channel; + public float X, Y, Z; + public float Radius; + public double Intensity; + + public Emitter(Channel channel, float x, float y, float z, float radius, double intensity = 0.0) + { + Channel = channel; + X = x; Y = y; Z = z; + Radius = radius; + Intensity = intensity; + } + + /// Intensity * max(0, 1 - distance / Radius) at the point; zero at and beyond the radius. + public double Received(float x, float y, float z) + { + double dx = x - X, dy = y - Y, dz = z - Z; + var distance = Math.Sqrt(dx * dx + dy * dy + dz * dz); + return Radius > 0f && distance < Radius ? Intensity * (1.0 - distance / Radius) : 0.0; + } + } + + /// The nerve's tunables, the transduction of any channel, noxious or sensory: threshold, sensitisation under sustained stimulus, conduction delay, and a per-character gain. public sealed record NerveParameters { public double Threshold { get; init; } = 0.1; @@ -35,14 +72,15 @@ namespace Lodur public double SensitisationCap { get; init; } = 2.0; public double ConductionDelaySeconds { get; init; } = 0.06; - /// Per-character nociceptor gain (the consumer draws its trait spread). + /// Per-character gain (the consumer draws its trait spread). public double Gain { get; init; } = 1.0; } /// /// Layer 1 for one character, a fixed number of body regions. Each tick it takes one point per region and the /// game's stimulus sources and produces, per region, the stimulus there and the afferent signal after - /// threshold, sensitisation and conduction delay. Everything is allocated in the constructor. + /// threshold, sensitisation and conduction delay. With a sensory nerve it also senses point emitters at one + /// region on three channels and combines the channels into one load. Everything is allocated in the constructor. /// public sealed class Perception { @@ -54,9 +92,20 @@ namespace Lodur private readonly double[] _sens; private readonly double[] _stimulus; private readonly double[] _afferent; + private readonly NerveParameters? _senses; + private readonly int _senseRegion; + private readonly int _senseSlots; + private readonly double[] _senseRing; // afferent in flight per channel, channel-major + private readonly double[] _senseSens = new double[ChannelCount]; + private readonly double[] _senseStimulus = new double[ChannelCount]; + private readonly double[] _senseAfferent = new double[ChannelCount]; private long _tick; - public Perception(NerveParameters parameters, double tickSeconds, int regionCount) + private const int ChannelCount = 3; + + /// The nerve of the sensory channels; null for a body with no senses (the load stays zero). + /// The region whose point the emitters are sensed at. + public Perception(NerveParameters parameters, double tickSeconds, int regionCount, NerveParameters? senses = null, int senseRegion = 0) { _p = parameters; _dt = tickSeconds; @@ -68,6 +117,23 @@ namespace Lodur Array.Fill(_sens, 1.0); _stimulus = new double[regionCount]; _afferent = new double[regionCount]; + _senses = senses; + _senseRegion = senseRegion; + if (senses != null) + { + if ((uint)senseRegion >= (uint)regionCount) + { + throw new ArgumentOutOfRangeException(nameof(senseRegion), $"The sense region must be one of {regionCount} regions."); + } + + _senseSlots = Math.Max(1, (int)Math.Round(senses.ConductionDelaySeconds / tickSeconds)); + _senseRing = new double[ChannelCount * _senseSlots]; + Array.Fill(_senseSens, 1.0); + } + else + { + _senseRing = Array.Empty(); + } } public int RegionCount => _regions; @@ -78,8 +144,18 @@ namespace Lodur /// The afferent signal arriving this tick: what the nerve took in a conduction delay ago. public double Afferent(int region) => _afferent[region]; + /// The sensory signals combined: 1 - the product over channels of (1 - afferent, capped at 1). Zero with no senses or no emitters. + public double Load { get; private set; } + + /// The largest intensity of the channel's emitters at the sense region's point this tick. + public double ChannelStimulus(Channel channel) => _senseStimulus[(int)channel]; + + /// The channel's afferent arriving this tick, after threshold, sensitisation and conduction delay. + public double ChannelAfferent(Channel channel) => _senseAfferent[(int)channel]; + /// Senses once per physics step. The spans are the caller's, one point per region. - public void Sense(ReadOnlySpan x, ReadOnlySpan y, ReadOnlySpan z, ReadOnlySpan sources) + public void Sense(ReadOnlySpan x, ReadOnlySpan y, ReadOnlySpan z, ReadOnlySpan sources, + ReadOnlySpan emitters = default) { if (x.Length != _regions || y.Length != _regions || z.Length != _regions) { @@ -109,7 +185,43 @@ namespace Lodur _stimulus[r] = stimulus; } + if (_senses != null) + { + SenseEmitters(x[_senseRegion], y[_senseRegion], z[_senseRegion], emitters); + } + _tick++; } + + private void SenseEmitters(float x, float y, float z, ReadOnlySpan emitters) + { + var senses = _senses!; + var slot = (int)(_tick % _senseSlots); + Array.Clear(_senseStimulus, 0, ChannelCount); + for (var i = 0; i < emitters.Length; i++) + { + var c = (int)emitters[i].Channel; + var received = emitters[i].Received(x, y, z); + if (received > _senseStimulus[c]) + { + _senseStimulus[c] = received; + } + } + + var quiet = 1.0; + for (var c = 0; c < ChannelCount; c++) + { + var above = Math.Max(0.0, _senseStimulus[c] - senses.Threshold); + var sens = _senseSens[c] + _dt * (senses.SensitisationGrowth * above - senses.SensitisationRecovery * (_senseSens[c] - 1.0)); + sens = Math.Min(Math.Max(sens, 1.0), senses.SensitisationCap); + _senseSens[c] = sens; + var ring = c * _senseSlots + slot; + _senseAfferent[c] = _senseRing[ring]; // written one conduction delay ago + _senseRing[ring] = above * sens * senses.Gain; + quiet *= 1.0 - Math.Min(1.0, _senseAfferent[c]); + } + + Load = 1.0 - quiet; + } } } diff --git a/tests/Lodur.Core.Tests/SensoryTests.cs b/tests/Lodur.Core.Tests/SensoryTests.cs new file mode 100644 index 0000000..343516e --- /dev/null +++ b/tests/Lodur.Core.Tests/SensoryTests.cs @@ -0,0 +1,140 @@ +using Lodur; + +namespace Lodur.Core.Tests; + +public sealed class SensoryPerceptionTests +{ + private const double Dt = 0.01; + private static readonly float[] Zero = { 0f }; + private static readonly BoxStimulus[] NoBoxes = System.Array.Empty(); + + /// A sensory nerve with sensitisation off, so afferent = (stimulus - threshold) * gain. + private static NerveParameters Plain(double threshold = 0.05, double delay = 0.0, double gain = 1.0) => + new NerveParameters { Threshold = threshold, SensitisationGrowth = 0.0, ConductionDelaySeconds = delay, Gain = gain }; + + private static Perception Senses(NerveParameters senses, int regions = 1, int senseRegion = 0) => + new Perception(new NerveParameters(), Dt, regions, senses, senseRegion); + + private static void Sense(Perception p, Emitter[] emitters) => p.Sense(Zero, Zero, Zero, NoBoxes, emitters); + + [Fact] + public void An_emitter_is_full_at_its_position_half_at_half_its_radius_and_zero_at_and_beyond_the_radius() + { + var e = new Emitter(Channel.Audio, 1f, 2f, 3f, 4f, 0.8); + Assert.Equal(0.8, e.Received(1f, 2f, 3f), 12); + Assert.Equal(0.4, e.Received(3f, 2f, 3f), 12); // 2 m along x: half the radius + Assert.Equal(0.4, e.Received(1f, 2f, 5f), 12); // distance is Euclidean, not per axis + Assert.Equal(0.0, e.Received(5f, 2f, 3f)); // exactly at the radius + Assert.Equal(0.0, e.Received(9f, 2f, 3f)); + Assert.Equal(0.0, new Emitter(Channel.Audio, 0f, 0f, 0f, 0f, 0.8).Received(0f, 0f, 0f)); // no radius, no reach + } + + [Fact] + public void Within_a_channel_the_larger_emitter_wins_and_across_channels_the_signals_saturate() + { + var p = Senses(Plain()); + Sense(p, new[] { new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 0.3), new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 0.5) }); + Sense(p, new[] { new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 0.3), new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 0.5) }); + Assert.Equal(0.5, p.ChannelStimulus(Channel.Audio), 12); + Assert.Equal(0.45, p.ChannelAfferent(Channel.Audio), 12); // not 0.3 + 0.5, not the sum of two afferents + + var q = Senses(Plain()); + var both = new[] { new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 0.5), new Emitter(Channel.Visual, 0f, 0f, 0f, 1f, 0.35) }; + Sense(q, both); + Sense(q, both); + double a = 0.45, b = 0.30; + Assert.Equal(a, q.ChannelAfferent(Channel.Audio), 12); + Assert.Equal(b, q.ChannelAfferent(Channel.Visual), 12); + Assert.Equal(0.0, q.ChannelAfferent(Channel.Interrupt)); + Assert.Equal(1.0 - (1.0 - a) * (1.0 - b), q.Load, 12); + Assert.True(q.Load < a + b); + + // One afferent past 1 is capped before combining, so the load never leaves 0..1. + var r = Senses(Plain(gain: 5.0)); + var loud = new[] { new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 0.5), new Emitter(Channel.Interrupt, 0f, 0f, 0f, 1f, 0.2) }; + Sense(r, loud); + Sense(r, loud); + Assert.Equal(1.0, r.Load); + } + + [Theory] + [InlineData(0.01, 0.06, 6)] + [InlineData(0.005, 0.06, 12)] + [InlineData(0.01, 0.0, 1)] + public void A_step_emitters_afferent_arrives_exactly_the_conduction_delay_later(double dt, double delay, int ticks) + { + var p = new Perception(new NerveParameters(), dt, 1, Plain(delay: delay), 0); + var quiet = new[] { new Emitter(Channel.Visual, 0f, 0f, 0f, 1f, 0.01) }; // under the threshold + for (var i = 0; i < 20; i++) { Sense(p, quiet); Assert.Equal(0.0, p.Load); } + + var loud = new[] { new Emitter(Channel.Visual, 0f, 0f, 0f, 1f, 0.8) }; + for (var i = 0; i < ticks; i++) { Sense(p, loud); Assert.Equal(0.0, p.Load); } // still in flight + Sense(p, loud); + Assert.True(p.Load > 0.0); + Assert.Equal(0.0, p.ChannelAfferent(Channel.Audio)); + } + + [Fact] + public void A_sustained_emitter_sensitises_its_channel_as_a_noxious_stimulus_does() + { + var p = Senses(new NerveParameters { Threshold = 0.1 }); + var e = new[] { new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 0.8) }; + for (var i = 0; i < 20000; i++) Sense(p, e); + Assert.Equal(0.7 * 2.0, p.ChannelAfferent(Channel.Audio), 9); // (stimulus - threshold) * SensitisationCap + } + + [Fact] + public void Only_the_sense_regions_point_decides_what_the_emitters_reach() + { + float[] x = { 0f, 0f }, y = { 0f, 0f }, z = { 0f, 0f }; + var emitters = new[] { new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 0.8) }; + Perception Make() => Senses(Plain(), regions: 2, senseRegion: 1); + + // The sense region (1) is far from the emitter; region 0 sitting on it changes nothing. + var far = Make(); + x[0] = 0f; x[1] = 5f; + for (var i = 0; i < 5; i++) far.Sense(x, y, z, NoBoxes, emitters); + Assert.Equal(0.0, far.ChannelStimulus(Channel.Audio)); + Assert.Equal(0.0, far.Load); + + // Moving the sense region next to it does. + x[1] = 0f; + for (var i = 0; i < 5; i++) far.Sense(x, y, z, NoBoxes, emitters); + Assert.Equal(0.8, far.ChannelStimulus(Channel.Audio), 12); + Assert.True(far.Load > 0.0); + + // And with region 0 far away and region 1 on it, it still reaches. + var near = Make(); + x[0] = 5f; x[1] = 0f; + for (var i = 0; i < 5; i++) near.Sense(x, y, z, NoBoxes, emitters); + Assert.Equal(0.8, near.ChannelStimulus(Channel.Audio), 12); + } + + [Fact] + public void Without_senses_or_without_emitters_the_load_and_every_channel_stay_zero() + { + var deaf = new Perception(new NerveParameters(), Dt, 1); + var loud = new[] { new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 1.0) }; + var hearing = Senses(Plain()); + for (var i = 0; i < 10000; i++) + { + deaf.Sense(Zero, Zero, Zero, NoBoxes, loud); + hearing.Sense(Zero, Zero, Zero, NoBoxes); + Assert.Equal(0.0, deaf.Load); + Assert.Equal(0.0, hearing.Load); + } + + foreach (var c in new[] { Channel.Audio, Channel.Visual, Channel.Interrupt }) + { + Assert.Equal(0.0, deaf.ChannelStimulus(c)); + Assert.Equal(0.0, deaf.ChannelAfferent(c)); + Assert.Equal(0.0, hearing.ChannelAfferent(c)); + } + } + + [Fact] + public void A_sense_region_outside_the_body_is_refused() + { + Assert.Throws(() => Senses(Plain(), regions: 2, senseRegion: 2)); + } +}