Sensor: one reset of the swept set; a ping belongs to its zone; PaintModeTests pin the ending step and the return inbox

The idle _reached.Clear() at the end of Execute is deleted: Ping() is the one per-ping reset
(ASecondPingCrossesTheSameShipAgain). A sweeping tick drops reached subjects that left the zone,
and a ping ends when its pinger is in another zone than it began in. New tests: the ending step's
band, every return credited, an inactive ally posted none, Deactivate clearing the inbox, a ping
ending on a zone change, and a leave-and-return subject crossed again.

Not yet verified: no verify job has run on this commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-11 16:42:16 +02:00
commit d2709d550e
2 changed files with 144 additions and 11 deletions

View file

@ -68,8 +68,13 @@ public class Sensor : Behavior, IEventBehavior, IPowerConsumer
private float _pingRadius;
// The subjects this ping's wavefront has reached, in the beam or not; each is crossed at most once per ping.
// Ping() is the one reset. A sweeping tick drops every subject no longer in the zone, so a subject that
// leaves and comes back inside the front is reached again, as a ship arriving inside the front is.
private readonly HashSet<Entity> _reached = new HashSet<Entity>();
// The zone the ping began in: a ping belongs to it, and ends when the pinger is anywhere else.
private Zone _pingZone;
public float Cooldown
{
get => saturate(_pingCooldown);
@ -134,6 +139,7 @@ public class Sensor : Behavior, IEventBehavior, IPowerConsumer
{
Entity.SetSignature(this, SignatureChannel.Emission, Evaluate(_data.PingVisibility), Item); // interim: emissions-heard replaces this with a range-gated emission
_pinging = true;
_pingZone = Entity.Zone;
_pingCooldown = 1;
_pingLerp = 0;
_pingRadius = 0;
@ -158,16 +164,14 @@ public class Sensor : Behavior, IEventBehavior, IPowerConsumer
// instance never reaches PowerBus.Step (see Ping()) and so never accrues charge.
if (Item != null)
_capacitor.AddCharge(_capacitor.RequestedFill(dt) * Item.PowerSupply);
if (_pinging && Entity.Zone != _pingZone) EndPing();
var sweeping = _pinging;
if (_pinging)
{
_pingLerp += dt / _data.PingDuration;
_pingRadius = lerp(0, Evaluate(_data.PingRange), pow(_pingLerp, _data.PingRadiusExponent));
if (_pingLerp > 1)
{
_pinging = false;
OnPingEnd?.Invoke();
}
_reached.IntersectWith(Entity.Zone.Entities);
if (_pingLerp > 1) EndPing();
}
_pingCooldown -= dt / Evaluate(_data.PingCooldown);
@ -222,10 +226,15 @@ public class Sensor : Behavior, IEventBehavior, IPowerConsumer
//Context.Log($"{entity.Name} visibility {(int)(previous * 100)}% -> {(int)(next * 100)}%");
Entity.EntityInfoGathered[entity] = next;
}
if (!_pinging) _reached.Clear();
return true;
}
private void EndPing()
{
_pinging = false;
OnPingEnd?.Invoke();
}
// Mining Cut 3 (docs/mining-cut-refresh.md): the one detection gain rule -- the info one observation of a
// target adds. A ping adds `visibility × sensitivity × PingBoost × distance` once; a passive tick adds
// `visibility × sensitivity × curve(angle/π) × dt / distance`. `response` is PingBoost for a ping and the

View file

@ -43,6 +43,14 @@ public sealed class PaintModeTests : IDisposable
Name = "Skiff", HullType = HullType.Ship, Shape = hullShape, Durability = 1000, Mass = 1000,
Hardpoints = { new HardpointData { Type = HardpointType.Sensors, Position = HardpointCell, Shape = new Shape() } }
});
var twinShape = new Shape(3, 3);
foreach (var cell in twinShape.AllCoordinates) twinShape[cell] = true;
cache.Upsert(new HullData
{
Name = "Twin", HullType = HullType.Ship, Shape = twinShape, Durability = 1000, Mass = 1000,
Hardpoints = { new HardpointData { Type = HardpointType.Sensors, Position = HardpointCell, Shape = new Shape() },
new HardpointData { Type = HardpointType.Sensors, Position = new int2(1, 0), Shape = new Shape() } }
});
cache.Upsert(SensorGear("Omni", sensitivity: 2f, pingBoost: .0001f, beamWidth: null));
cache.Upsert(SensorGear("Beam90", sensitivity: 2f, pingBoost: .0001f, beamWidth: 90f));
cache.Upsert(SensorGear("Ear", sensitivity: 3f, pingBoost: .0002f, beamWidth: null));
@ -101,21 +109,27 @@ public sealed class PaintModeTests : IDisposable
private int _nextLot = 2;
// A ship at `position`; a ship without a `sensor` is a bare hull. Every ship is lit at a constant visibility of 5.
private Ship NewShip(float3 position, string sensor = null, Faction faction = null, bool factionless = false)
private Ship NewShip(float3 position, string sensor = null, Faction faction = null, bool factionless = false, string hull = "Skiff", string sensor2 = null, Zone zone = null)
{
var hullRef = _items.ItemData.RefOf<ItemData>(_items.ItemData.GetByName<HullData>("Skiff"));
var ship = new Ship(_items, _zone, new EquippableItem { Data = hullRef, Durability = 1000, Lot = 1 }, new EntitySettings())
zone ??= _zone;
var hullRef = _items.ItemData.RefOf<ItemData>(_items.ItemData.GetByName<HullData>(hull));
var ship = new Ship(_items, zone, new EquippableItem { Data = hullRef, Durability = 1000, Lot = 1 }, new EntitySettings())
{ Faction = factionless ? null : faction ?? new Faction { Name = "F" + Guid.NewGuid().ToString("N") } };
if (sensor != null)
{
var gearRef = _items.ItemData.RefOf<ItemData>(_items.ItemData.GetByName<GearData>(sensor));
Assert.True(ship.TryEquip(new EquippableItem { Data = gearRef, Durability = 1, Lot = _nextLot++ }, HardpointCell));
}
_zone.Entities.Add(ship);
if (sensor2 != null)
{
var gearRef2 = _items.ItemData.RefOf<ItemData>(_items.ItemData.GetByName<GearData>(sensor2));
Assert.True(ship.TryEquip(new EquippableItem { Data = gearRef2, Durability = 1, Lot = _nextLot++ }, new int2(1, 0)));
}
zone.Entities.Add(ship);
ship.Activate();
ship.Position = position;
ship.SetSignature(new object(), SignatureChannel.Reflection, 5f, null);
_zone.Update(0f); // brings the gear online
zone.Update(0f); // brings the gear online
_ships.Add(ship);
return ship;
}
@ -591,6 +605,116 @@ public sealed class PaintModeTests : IDisposable
Assert.Equal(2, painted);
}
// The ping's last step reaches PingRange; a ship in that step's band (the front is at 292.4 before it and 300
// after) is still swept: the step that ends the ping sweeps.
[Fact]
public void AShipInTheEndingStepsBandIsStillSwept()
{
var a = NewShip(float3(0, 0, 0), "Omni");
var s = NewShip(float3(0, 0, 295));
Settle();
SensorOf(a).Ping();
var paints = 0;
for (var tick = 1; tick <= 26; tick++) { Tick(.1f); paints += s.IncomingPaints.Count; }
Assert.Equal(1, paints);
Near(5 * 2 * .0001f * 295, Track(a, s));
}
// An ally holding two returns off one subject (two pingers on one allied ship) credits each: 2 x 5 x 3 x .0002 x
// sqrt(100 x 50).
[Fact]
public void AnAllyCreditsEveryReturnItHolds()
{
var a = NewShip(float3(0, 0, 0), "Omni", hull: "Twin", sensor2: "Omni");
var c = NewShip(float3(30, 0, 140), "Ear", a.Faction);
var t = NewShip(float3(0, 0, 100));
Settle();
foreach (var s in a.GetBehaviors<Sensor>()) s.Ping();
for (var tick = 1; tick <= 12; tick++) Tick(.1f);
Near(2 * 5 * 3 * .0002f * sqrt(100f * 50f), Track(c, t));
}
// Ticks until the pinger has credited the subject (the crossing tick, on which the returns are posted to allies).
private void TickToTheCrossing(Ship pinger, Ship subject)
{
for (var tick = 1; tick <= 40 && Track(pinger, subject) == 0f; tick++) Tick(.1f);
Assert.True(Track(pinger, subject) > 0f);
}
// A return posted to an inactive ally is dropped: the ally, active again, hears nothing of that ping.
[Fact]
public void AnInactiveAllyIsPostedNoReturn()
{
var a = NewShip(float3(0, 0, 0), "Omni");
var t = NewShip(float3(0, 0, 100));
var c = NewShip(float3(30, 0, 140), "Ear", a.Faction);
Settle();
c.Deactivate();
SensorOf(a).Ping();
TickToTheCrossing(a, t);
c.Activate();
Tick(.1f);
Assert.Equal(0f, Track(c, t));
}
// Deactivating a ship clears the returns it holds: readmitted, it hears nothing of a ping it was docked through.
[Fact]
public void ADeactivatedAllyKeepsNoReturnsForItsNextAdmission()
{
var a = NewShip(float3(0, 0, 0), "Omni");
var t = NewShip(float3(0, 0, 100));
var c = NewShip(float3(30, 0, 140), "Ear", a.Faction);
Settle();
SensorOf(a).Ping();
TickToTheCrossing(a, t); // the crossing tick posted a return into c
c.Deactivate();
c.Activate();
Tick(.1f);
Assert.Equal(0f, Track(c, t));
}
// A ping belongs to the zone it began in: a pinger that moves to another zone mid-ping ends it, and the ships of
// the new zone inside the radius it had reached are not swept.
[Fact]
public void APingEndsWhenThePingerLeavesItsZone()
{
var a = NewShip(float3(0, 0, 0), "Omni");
var z2 = new Zone(_items, new PlanetSettings(), new ZonePack(), new GalaxyZone { Name = "Zone 2", Owner = null }, null);
var near = NewShip(float3(0, 0, 50), zone: z2);
var far = NewShip(float3(0, 0, 250), zone: z2);
Settle();
SensorOf(a).Ping();
for (var tick = 1; tick <= 3; tick++) Tick(.1f);
Assert.True(SensorOf(a).PingRadius > 50f); // the front had passed 50 m by the move
a.Deactivate(); _zone.Entities.Remove(a); a.Zone = z2; z2.Admit(a, false);
var paints = 0;
for (var tick = 1; tick <= 24; tick++) { z2.Update(.1f); paints += near.IncomingPaints.Count + far.IncomingPaints.Count; }
Assert.Equal(0, paints);
Assert.Equal(0f, Track(a, near));
Assert.Equal(0f, Track(a, far));
}
// The swept set is the zone members the front has crossed: a subject that leaves after its crossing and returns
// while the front is still past it is crossed again, as a ship arriving inside the front is.
[Fact]
public void ASubjectThatLeavesAndReturnsIsCrossedAgain()
{
var a = NewShip(float3(0, 0, 0), "Omni");
var t = NewShip(float3(0, 0, 100));
Settle();
SensorOf(a).Ping();
var paints = 0;
for (var tick = 1; tick <= 30; tick++)
{
if (tick == 5) { _zone.Entities.Remove(t); t.Deactivate(); }
if (tick == 8) { _zone.Entities.Add(t); t.Activate(); }
Tick(.1f);
paints += t.IncomingPaints.Count;
}
Assert.Equal(2, paints);
Near(.1f, Track(a, t));
}
private static MessagePackSerializerOptions WireOptions => CultDocumentMessagePackSerialization.OptionsFor(typeof(ZonePack).Assembly);
// BeamWidth is slot 12 of the sensor record, the fifth union arm of BehaviorData, and the JSON property