Carry facts on emitters and percepts; refuse the reach bit and fact disagreement

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-25 22:50:44 +01:00
commit df2bea039b
2 changed files with 97 additions and 5 deletions

View file

@ -52,7 +52,10 @@ namespace Lodur
/// <summary>The game's tag for the thing (what kind it is). Lodur carries it and never interprets it; emitters sharing an id must share a kind.</summary>
public uint Kind;
public Emitter(Channel channel, float x, float y, float z, float radius, double intensity = 0.0, ulong id = 0, uint kind = 0)
/// <summary>The thing's facts as the game sees them, bit i is fact i. The game owns every bit but <see cref="Perception.InReachFact"/> (63), which <see cref="Perception.Sense"/> refuses; emitters sharing an id must share facts.</summary>
public ulong Facts;
public Emitter(Channel channel, float x, float y, float z, float radius, double intensity = 0.0, ulong id = 0, uint kind = 0, ulong facts = 0)
{
Channel = channel;
X = x; Y = y; Z = z;
@ -60,6 +63,7 @@ namespace Lodur
Intensity = intensity;
Id = id;
Kind = kind;
Facts = facts;
}
/// <summary>Intensity * max(0, 1 - distance / Radius) at the point; zero at and beyond the radius.</summary>
@ -113,6 +117,9 @@ namespace Lodur
public ulong Id { get; }
public uint Kind { get; }
/// <summary>The thing's facts as last sensed; a thing changed out of sight keeps its old facts.</summary>
public ulong Facts { get; }
/// <summary>Where the emitter that reached the puppet hardest was, the last tick the thing was sensed.</summary>
public float X { get; }
public float Y { get; }
@ -124,9 +131,9 @@ namespace Lodur
/// <summary>0 on a tick the thing is sensed.</summary>
public double SecondsSinceSensed { get; }
public Percept(ulong id, uint kind, float x, float y, float z, double salience, double secondsSinceSensed)
public Percept(ulong id, uint kind, float x, float y, float z, double salience, double secondsSinceSensed, ulong facts = 0)
{
Id = id; Kind = kind; X = x; Y = y; Z = z; Salience = salience; SecondsSinceSensed = secondsSinceSensed;
Id = id; Kind = kind; Facts = facts; X = x; Y = y; Z = z; Salience = salience; SecondsSinceSensed = secondsSinceSensed;
}
}
@ -156,6 +163,7 @@ namespace Lodur
private readonly double _memorySeconds;
private readonly ulong[] _pId; // percept slots; id 0 is free
private readonly uint[] _pKind;
private readonly ulong[] _pFacts;
private readonly float[] _pX, _pY, _pZ;
private readonly double[] _pSalience;
private readonly double[] _pSince;
@ -167,6 +175,9 @@ namespace Lodur
/// <summary>The top bit of an emitter id, reserved to Lodur's phantoms; <see cref="Sense"/> refuses a caller emitter that sets it.</summary>
public const ulong PhantomIdBit = 1UL << 63;
/// <summary>The top fact bit, Lodur's own: whether the thing is within reach. The planner is its only writer, deriving it per call from distance; <see cref="Sense"/> refuses an emitter that sets it.</summary>
public const ulong InReachFact = 1UL << 63;
/// <param name="senses">The nerve of the sensory channels; null for a body with no senses (the load stays zero).</param>
/// <param name="senseRegion">The region whose point the emitters are sensed at.</param>
public Perception(NerveParameters parameters, double tickSeconds, int regionCount, NerveParameters? senses = null, int senseRegion = 0, PerceptParameters? percepts = null)
@ -198,6 +209,7 @@ namespace Lodur
_memorySeconds = pp.MemorySeconds;
_pId = new ulong[pp.Capacity];
_pKind = new uint[pp.Capacity];
_pFacts = new ulong[pp.Capacity];
_pX = new float[pp.Capacity];
_pY = new float[pp.Capacity];
_pZ = new float[pp.Capacity];
@ -210,6 +222,7 @@ namespace Lodur
_senseRing = Array.Empty<double>();
_pId = Array.Empty<ulong>();
_pKind = Array.Empty<uint>();
_pFacts = Array.Empty<ulong>();
_pX = _pY = _pZ = Array.Empty<float>();
_pSalience = _pSince = Array.Empty<double>();
_pSensed = Array.Empty<bool>();
@ -244,13 +257,13 @@ namespace Lodur
/// <summary>The percept in the slot. A thing keeps its slot while it is remembered; a free slot reads as Id 0.</summary>
public Percept Percept(int slot) =>
new Percept(_pId[slot], _pKind[slot], _pX[slot], _pY[slot], _pZ[slot], _pSalience[slot], _pSince[slot]);
new Percept(_pId[slot], _pKind[slot], _pX[slot], _pY[slot], _pZ[slot], _pSalience[slot], _pSince[slot], _pFacts[slot]);
/// <summary>
/// Senses once per physics step. The spans are the caller's, one point per region. The sensory gain (default 1, the identity) multiplies the sense
/// channels' afferents as they leave the conduction delay, so a change of gain reaches <see cref="Load"/> on the next call it is passed to; the nociceptive region
/// afferents are never gated. A gain outside [0, 1] (NaN and infinity included) is refused,
/// as is an emitter that sets <see cref="PhantomIdBit"/> and two emitters of one id with different kinds.
/// as is an emitter that sets <see cref="PhantomIdBit"/> or <see cref="InReachFact"/> and two emitters of one id with different kinds or facts.
/// </summary>
public void Sense(ReadOnlySpan<float> x, ReadOnlySpan<float> y, ReadOnlySpan<float> z, ReadOnlySpan<BoxStimulus> sources,
ReadOnlySpan<Emitter> emitters = default, double sensoryGain = 1.0)
@ -281,6 +294,11 @@ namespace Lodur
throw new ArgumentException($"Emitter {i} sets the phantom id bit, which is reserved to Lodur.", nameof(emitters));
}
if ((emitters[i].Facts & InReachFact) != 0)
{
throw new ArgumentException($"Emitter {i} sets the in-reach fact bit, which is reserved to Lodur.", nameof(emitters));
}
if (id != 0)
{
for (var j = 0; j < i; j++)
@ -289,6 +307,11 @@ namespace Lodur
{
throw new ArgumentException($"Emitters {j} and {i} share id {id} with different kinds.", nameof(emitters));
}
if (emitters[j].Id == id && emitters[j].Facts != emitters[i].Facts)
{
throw new ArgumentException($"Emitters {j} and {i} share id {id} with different facts.", nameof(emitters));
}
}
}
}
@ -392,6 +415,7 @@ namespace Lodur
{
_pId[s] = 0;
_pKind[s] = 0;
_pFacts[s] = 0;
_pX[s] = 0f; _pY[s] = 0f; _pZ[s] = 0f;
_pSalience[s] = 0.0;
_pSince[s] = 0.0;
@ -473,6 +497,7 @@ namespace Lodur
{
_pId[slot] = id;
_pKind[slot] = kind;
_pFacts[slot] = 0;
_pSensed[slot] = false;
_pSalience[slot] = 0.0;
_pSince[slot] = 0.0;
@ -492,6 +517,7 @@ namespace Lodur
}
_pKind[slot] = e.Kind;
_pFacts[slot] = e.Facts;
_pSensed[slot] = true;
_pSince[slot] = 0.0;
}

View file

@ -219,6 +219,72 @@ public sealed class PerceptTests
Sense(p, new[] { At(0, kind: 1), At(0, kind: 2), At(4, kind: 3), At(4, kind: 3, channel: Channel.Visual) });
}
private static Emitter Fact(ulong id, ulong facts, float x = 0f, double intensity = 0.5, Channel channel = Channel.Audio) =>
new Emitter(channel, x, 0f, 0f, 2f, intensity, id, 7, facts);
[Fact]
public void Facts_are_remembered_as_last_sensed_and_cleared_on_forgetting_and_admission()
{
var p = Make(capacity: 1, memory: 0.05);
Sense(p, new[] { Fact(1, 0b101UL) });
Assert.Equal(0b101UL, p.Percept(0).Facts);
// Out of sight the facts stay as last sensed.
Sense(p, Array.Empty<Emitter>());
Assert.Equal(1UL, p.Percept(0).Id);
Assert.Equal(0b101UL, p.Percept(0).Facts);
// A fresh sensing replaces them.
Sense(p, new[] { Fact(1, 0b010UL) });
Assert.Equal(0b010UL, p.Percept(0).Facts);
// Forgetting clears them.
for (var i = 0; i < 20; i++) Sense(p, Array.Empty<Emitter>());
Assert.Equal(0UL, p.Percept(0).Id);
Assert.Equal(0UL, p.Percept(0).Facts);
// Another id taking the slot starts from 0 and then its own facts; an emitter with no facts reads 0.
Sense(p, new[] { Fact(2, 0b1UL) });
Sense(p, new[] { Fact(3, 0UL) });
Assert.Equal(3UL, p.Percept(0).Id);
Assert.Equal(0UL, p.Percept(0).Facts);
// Two emitters of one id with the same facts, on two channels, are fine.
Sense(p, new[] { Fact(3, 0b11UL), Fact(3, 0b11UL, channel: Channel.Visual) });
Assert.Equal(0b11UL, p.Percept(0).Facts);
// Eviction by a newcomer replaces the evicted thing's facts.
Sense(p, Array.Empty<Emitter>());
Sense(p, new[] { Fact(4, 0b100UL) });
Assert.Equal(4UL, p.Percept(0).Id);
Assert.Equal(0b100UL, p.Percept(0).Facts);
}
[Fact]
public void Sense_refuses_the_reach_bit_and_a_fact_disagreement_before_stepping()
{
var p = Make();
Sense(p, new[] { Fact(1, 0b1UL, intensity: 0.6) });
var load = p.Load;
var since = p.Percept(0).SecondsSinceSensed;
Assert.Throws<ArgumentException>(() => Sense(p, new[] { Fact(1, 0b1UL), Fact(2, Perception.InReachFact) }));
Assert.Throws<ArgumentException>(() => Sense(p, new[] { Fact(2, Perception.InReachFact | 1UL) }));
Assert.Throws<ArgumentException>(() => Sense(p, new[] { Fact(0, Perception.InReachFact) }));
Assert.Throws<ArgumentException>(() => Sense(p, new[] { Fact(1, 0b1UL), Fact(3, 0UL), Fact(1, 0b11UL) }));
Assert.Equal(load, p.Load);
Assert.Equal(since, p.Percept(0).SecondsSinceSensed);
Assert.Equal(0b1UL, p.Percept(0).Facts);
Assert.Equal(1, Occupied(p));
// The state did not step: the next good tick is the second, not the fifth.
Sense(p, Array.Empty<Emitter>());
Assert.Equal(Dt, p.Percept(0).SecondsSinceSensed);
// Anonymous emitters may disagree freely on facts; the top fact bit below 63 is the game's.
Sense(p, new[] { Fact(0, 1UL), Fact(0, 2UL), Fact(5, 1UL << 62) });
}
[Fact]
public void Percept_parameters_are_validated()
{