diff --git a/src/Lodur.Core/Perception.cs b/src/Lodur.Core/Perception.cs
index d72ce57..52d0785 100644
--- a/src/Lodur.Core/Perception.cs
+++ b/src/Lodur.Core/Perception.cs
@@ -52,7 +52,10 @@ namespace Lodur
/// 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.
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)
+ /// The thing's facts as the game sees them, bit i is fact i. The game owns every bit but (63), which refuses; emitters sharing an id must share facts.
+ 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;
}
/// Intensity * max(0, 1 - distance / Radius) at the point; zero at and beyond the radius.
@@ -113,6 +117,9 @@ namespace Lodur
public ulong Id { get; }
public uint Kind { get; }
+ /// The thing's facts as last sensed; a thing changed out of sight keeps its old facts.
+ public ulong Facts { get; }
+
/// Where the emitter that reached the puppet hardest was, the last tick the thing was sensed.
public float X { get; }
public float Y { get; }
@@ -124,9 +131,9 @@ namespace Lodur
/// 0 on a tick the thing is sensed.
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
/// The top bit of an emitter id, reserved to Lodur's phantoms; refuses a caller emitter that sets it.
public const ulong PhantomIdBit = 1UL << 63;
+ /// 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; refuses an emitter that sets it.
+ public const ulong InReachFact = 1UL << 63;
+
/// 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, 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();
_pId = Array.Empty();
_pKind = Array.Empty();
+ _pFacts = Array.Empty();
_pX = _pY = _pZ = Array.Empty();
_pSalience = _pSince = Array.Empty();
_pSensed = Array.Empty();
@@ -244,13 +257,13 @@ namespace Lodur
/// The percept in the slot. A thing keeps its slot while it is remembered; a free slot reads as Id 0.
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]);
///
/// 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 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 and two emitters of one id with different kinds.
+ /// as is an emitter that sets or and two emitters of one id with different kinds or facts.
///
public void Sense(ReadOnlySpan x, ReadOnlySpan y, ReadOnlySpan z, ReadOnlySpan sources,
ReadOnlySpan 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;
}
diff --git a/tests/Lodur.Core.Tests/PerceptTests.cs b/tests/Lodur.Core.Tests/PerceptTests.cs
index b58ca30..b9b5211 100644
--- a/tests/Lodur.Core.Tests/PerceptTests.cs
+++ b/tests/Lodur.Core.Tests/PerceptTests.cs
@@ -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());
+ 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());
+ 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());
+ 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(() => Sense(p, new[] { Fact(1, 0b1UL), Fact(2, Perception.InReachFact) }));
+ Assert.Throws(() => Sense(p, new[] { Fact(2, Perception.InReachFact | 1UL) }));
+ Assert.Throws(() => Sense(p, new[] { Fact(0, Perception.InReachFact) }));
+ Assert.Throws(() => 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());
+ 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()
{