diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index e9c8fef96..7fb5d4174 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -414,27 +414,27 @@ public abstract class Entity // Mining Cut 3 (Q14 A): this entity's info on a chunk, computed when asked and stored nowhere. It is the value // the per-tick sensor rule (Sensor.Execute, through Sensor.Gain) would settle at for a target this bright, - // here, now (PassiveInfoPerSignature, scaled by the brightness), capped at 1. + // here, now (PassiveInfo at the chunk's brightness), capped at 1. public float ChunkInfo(ChunkId chunk) { var visibility = Zone.ChunkVisibility(chunk); if (visibility <= 0f) return 0f; - return saturate(visibility * PassiveInfoPerSignature(Zone.ChunkPose(chunk.Field, chunk.Index).xy)); + return saturate(PassiveInfo(Zone.ChunkPose(chunk.Field, chunk.Index).xy, visibility)); } - // The info a target of unit signature at `position` would settle at under this entity's passive sensing, before + // The info a target of `signature` at `position` would settle at under this entity's passive sensing, before // the cap at 1. Each active sensor applies x <- saturate(x + G_i dt)(1 - k dt) every tick, so with n sensors the // info settles where the gain balances the decay: sum(G_i) / (n k). A ping adds nothing that lasts, so only the - // passive gain enters. It is linear in the signature below saturation, which is what lets one value answer for - // every signature at a place: ChunkInfo scales it by a chunk's brightness, Sense by each act's strength. Zero + // passive gain enters. ChunkInfo asks it for a chunk's brightness; Sense asks it for a unit signature once per + // ship, because it is linear in the signature below saturation and so answers for every act at a place. Zero // with no active sensor. - public float PassiveInfoPerSignature(float2 position) + public float PassiveInfo(float2 position, float signature) { var rate = 0f; var sensors = 0; foreach (var sensor in ActiveSensors()) { - rate += sensor.PassiveRate(1f, position); + rate += sensor.PassiveRate(signature, position); sensors++; } return sensors == 0 ? 0f : rate / (sensors * ItemManager.GameplaySettings.TargetInfoDecay); @@ -865,7 +865,9 @@ public abstract class Entity foreach (var paint in _paintInbox) { _incomingPaints.Add(paint.Warning); - RecordReveal(paint.Emitter, paint.Item, SignatureChannel.Emission); + // An emitter that left the zone (died, warped, docked) since it painted is no longer a subject: its + // warning still lasts its tick, but it is not recorded as a contact. + if (Zone.Entities.Contains(paint.Emitter)) RecordReveal(paint.Emitter, paint.Item, SignatureChannel.Emission); } _paintInbox.Clear(); _pingReturns.Clear(); @@ -899,15 +901,12 @@ public abstract class Entity book[item] = new Reveal(item, channel, normalize((subject.Position - Position).xz), Zone.Time); } - // What this observer has seen `subject`'s items do: each item's latest act, filtered on read to items still on - // the subject and not destroyed (Durability >= .01, as FireControl ranks gear). - public IEnumerable RevealsOf(Entity subject) - { - if (!_reveals.TryGetValue(subject, out var book)) yield break; - foreach (var reveal in book.Values) - if (reveal.Item.Entity == subject && subject.Equipment.Contains(reveal.Item) && reveal.Item.EquippableItem.Durability >= .01f) - yield return reveal; - } + // What this observer has seen `subject`'s items do: each item's latest act, and nothing the subject's present + // gear or Durability says (ruling contact-reads-book-aim-reads-live): an item destroyed out of this observer's + // sight stays revealed until the subject leaves the zone or this observer deactivates. Whether an item can be + // aimed at now is FireControl.IsRevealed's question, not this one. + public IEnumerable RevealsOf(Entity subject) => + _reveals.TryGetValue(subject, out var book) ? book.Values : Enumerable.Empty(); // The one derivation of how much this observer holds of `subject`: see Contact. public Contact ContactOf(Entity subject) @@ -929,12 +928,15 @@ public abstract class Entity return new Contact(detected, tracked, detected && any, info >= settings.TargetGearInfoThreshold, bearing, info); } - // What this observer's instruments receive of other ships' acts this tick (Update calls it once, after the - // equipment loop): every light signature of an item that would alone settle this observer's passive info above + // What this observer's instruments receive of other ships' acts this tick (Zone.Update calls it once, after every + // entity has updated): every light signature of an item that would alone settle this observer's passive info above // TargetDetectionInfoThreshold is revealed (ruling visibility-per-capability-reveal). Hull radiation names no - // item and only feeds the track; an Emission entry is never light. One PassiveInfoPerSignature per ship. + // item and only feeds the track; an Emission entry is never light. One PassiveInfo per ship. Zone.Update calls it + // for every entity after all of them have updated, so what a ship shows is what every observer receives on that + // tick whatever order the zone lists them in. internal void Sense() { + if (!_active) return; var threshold = ItemManager.GameplaySettings.TargetDetectionInfoThreshold; var entities = Zone.Entities; for (var i = 0; i < entities.Count; i++) @@ -945,7 +947,7 @@ public abstract class Entity foreach (var signature in other._visibilitySources.Values) { if (signature.Item == null || signature.Channel == SignatureChannel.Emission) continue; - if (perSignature < 0f) perSignature = PassiveInfoPerSignature(other.Position.xz); + if (perSignature < 0f) perSignature = PassiveInfo(other.Position.xz, 1f); if (signature.Strength * perSignature > threshold) RecordReveal(other, signature.Item, signature.Channel); } } @@ -1655,8 +1657,6 @@ public abstract class Entity equippedItem.Update(delta); } - Sense(); - foreach (var message in Messages.Keys.ToArray()) { Messages[message] = Messages[message] - delta; diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index 4aa2063cc..6ce39a3c3 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -229,6 +229,7 @@ public class Zone var entities = Entities.ToArray(); foreach (var entity in entities) entity.PublishInbox(); foreach (var entity in entities) entity.Update(deltaTime); + foreach (var entity in entities) entity.Sense(); StepMines(deltaTime); StepClouds(deltaTime); diff --git a/tests/Aetheria.Shared.Tests/ContactRevealTests.cs b/tests/Aetheria.Shared.Tests/ContactRevealTests.cs index 7ed3f962d..d2fced007 100644 --- a/tests/Aetheria.Shared.Tests/ContactRevealTests.cs +++ b/tests/Aetheria.Shared.Tests/ContactRevealTests.cs @@ -23,7 +23,7 @@ using int2 = CultMath.int2; // one passive sensor has sensitivity 2 and a flat curve, so a signature of strength S at planar distance d settles // its info at S x 2 / (d x TargetInfoDecay), and is revealed exactly when that exceeds TargetDetectionInfoThreshold // (.1). Signatures decay at once (VisibilityDecay 100), so an act is a single tick's light and builds no track. -// Subjects are added before the observer: they update first, so the observer senses the light on the tick it is made. +// The observer senses after every ship has updated, so the zone's list order never decides when an act is revealed. public sealed class ContactRevealTests : IDisposable { private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-contact-" + Guid.NewGuid().ToString("N")); @@ -514,14 +514,163 @@ public sealed class ContactRevealTests : IDisposable Fire(e, guns[1]); Assert.Equal(2, o.RevealsOf(e).Count()); - guns[0].EquippableItem.Durability = 0f; // destroyed - - Assert.Same(guns[1], Assert.Single(o.RevealsOf(e)).Item); - _zone.Entities.Remove(e); _zone.Entities.Add(e); Assert.Empty(o.RevealsOf(e)); // gone with the subject, and not back with it + Assert.Equal(0, RevealBookSubjects(o)); // and the book no longer holds the ship at all + } + + private static int RevealBookSubjects(Entity observer) => + ((System.Collections.IDictionary) typeof(Entity).GetField("_reveals", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(observer)).Count; + + // Ruling contact-reads-book-aim-reads-live: what an observer knows of a ship comes from its own book. A gun + // destroyed by a third party out of its sight changes none of its levels and stays in its reveal list, while + // the aiming read, which asks about the item now, refuses the dead gun and still serves the live one. + [Fact] + public void GearDestroyedOutOfSightLeavesWhatTheObserverKnowsAlone() + { + var e = Add(float3(0, 0, 150), new[] { "Gun", "Gun" }); + var o = Add(float3(0, 0, 0), new[] { "Eye" }); + var guns = Guns(e); + Fire(e, guns[0]); + Fire(e, guns[1]); + var before = o.ContactOf(e); + Assert.True(before.Detected && before.Classified && !before.Tracked, "fixture: a bearing and a class, no track"); + o.EntityInfoGathered[e] = 1f; // the aiming read needs info to resolve the target's gear + Assert.True(FireControl.IsRevealed(o, guns[0]) && FireControl.IsRevealed(o, guns[1]), "fixture: both guns are aimable"); + + guns[0].EquippableItem.Durability = 0f; // destroyed by someone else; o saw nothing + + Assert.Equal(2, o.RevealsOf(e).Count()); + var after = o.ContactOf(e); + Assert.True(after.Detected); + Assert.True(after.Classified); + Assert.Equal(before.Bearing.x, after.Bearing.x); + Assert.Equal(before.Bearing.y, after.Bearing.y); + Assert.False(FireControl.IsRevealed(o, guns[0])); + Assert.True(FireControl.IsRevealed(o, guns[1])); + + guns[1].EquippableItem.Durability = 0f; // the last of its gear dies unseen too + + Assert.Equal(2, o.RevealsOf(e).Count()); + Assert.True(o.ContactOf(e).Detected); + Assert.True(o.ContactOf(e).Classified); + } + + // A paint inboxed in the tick its emitter leaves the zone is published the next tick, and is no longer the + // act of a subject in the zone: it warns for its tick and records no reveal, so the observer holds no ghost. + [Fact] + public void APaintFromAShipThatHasLeftTheZoneRevealsNothing() + { + var locker = Add(float3(0, 0, 200), new[] { "Locker" }); + var o = Add(float3(0, 0, 0), new[] { "Eye" }); + locker.SetIff(o, true); + locker.EntityInfoGathered[o] = 1f; + locker.SetTarget(o); + locker.Aim = normalize(o.Position - locker.Position); + for (var tick = 0; tick < 10 && !o.RevealsOf(locker).Any(); tick++) Tick(.5f); + Assert.Single(o.RevealsOf(locker)); // fixture: the lock paints every tick, and the first one is recorded + + _zone.Entities.Remove(locker); // dies, warps or docks while its latest paint is still in o's inbox + Assert.Empty(o.RevealsOf(locker)); + Tick(.1f); + + Assert.Single(o.IncomingPaints); // fixture: the stale paint was published + Assert.Empty(o.RevealsOf(locker)); + Assert.False(o.ContactOf(locker).Detected); + Assert.Equal(0, RevealBookSubjects(o)); + } + + // Ruling sensor-stat-set 4: a painted target always learns the emitter's class, whichever the mode. A ping + // crossing the observer (a Search paint) reveals the pinger's sensor item, with channel Emission and the + // bearing of the pinger. + [Fact] + public void ASearchPaintRevealsThePingersSensor() + { + var p = Add(float3(0, 0, 100), new[] { "Eye" }); + var o = Add(float3(0, 0, 0), new[] { "Eye" }); + var item = p.Equipment.Single(i => i.Behaviors.Any(b => b is Sensor)); + var sensor = (Sensor) item.Behaviors.Single(b => b is Sensor); + Tick(.01f); + sensor.Ping(); + + var modes = new List(); + for (var tick = 0; tick < 30; tick++) + { + Tick(.1f); + modes.AddRange(o.IncomingPaints.Select(paint => paint.Mode)); + } + + Assert.Equal(new[] { PaintMode.Search }, modes); // fixture: the sweep painted o once, as a search + var reveal = Assert.Single(o.RevealsOf(p)); + Assert.Same(item, reveal.Item); + Assert.Equal(SignatureChannel.Emission, reveal.Channel); + AssertBearing(0f, 1f, reveal.Bearing); + } + + // An observer's own gear is not another ship's act: a ship that fires its own gun, with its own sensor at the + // very place of the light, records nothing of itself, while a neighbour does record the gun. + [Fact] + public void AnObserverNeverRecordsItsOwnGear() + { + var self = Add(float3(0, 0, 0), new[] { "Gun", "Eye" }); + var neighbour = Add(float3(0, 0, 30), new[] { "Eye" }); + + Fire(self, Guns(self)[0]); + + Assert.Single(neighbour.RevealsOf(self)); // fixture: the gun's light reaches a sensor + Assert.Empty(self.RevealsOf(self)); + Assert.False(self.ContactOf(self).Detected); + } + + // The zone's list order is not a rule of the game: two observers equidistant from a gun, one listed before the + // gun's ship and one after, record the same reveal with the same bearing and the same clock. + [Fact] + public void AnActIsRevealedOnItsOwnTickWhateverTheListOrder() + { + var before = Add(float3(-30, 0, 40), new[] { "Eye" }); + var e = Add(float3(30, 0, 40), new[] { "Gun" }); + var after = Add(float3(0, 0, 0), new[] { "Eye" }); + + var at = Fire(e, Guns(e)[0]); + + foreach (var observer in new Entity[] { before, after }) + { + var reveal = Assert.Single(observer.RevealsOf(e)); + Assert.Equal(at, reveal.Time); + var toward = normalize((e.Position - observer.Position).xz); + AssertBearing(toward.x, toward.y, reveal.Bearing); + } + } + + // ---- A quiet ship ---- + + // Ruling quiet-gear-identifiable-hull-layout-known: a ship that has shown nothing is Detected while it is + // tracked and Identified once the track reaches the gear threshold, with no act in any book; with no reveal + // it is not Classified. + [Theory] + [InlineData(.05f, false, false)] + [InlineData(.5f, true, false)] + [InlineData(.79f, true, false)] + [InlineData(.8f, true, true)] + [InlineData(1f, true, true)] + public void AQuietTrackedShipIsDetectedAndIdentifiedByTheTrackAlone(float info, bool tracked, bool identified) + { + var e = Add(float3(30, 0, 40), new[] { "Gun" }); // armed, but it never fires + var o = Add(float3(0, 0, 0), new[] { "Eye" }); + + o.EntityInfoGathered[e] = info; + + Assert.Empty(o.RevealsOf(e)); + var contact = o.ContactOf(e); + Assert.Equal(tracked, o.VisibleEntities.Contains(e)); + Assert.Equal(tracked, contact.Tracked); + Assert.Equal(tracked, contact.Detected); + Assert.False(contact.Classified); + Assert.Equal(identified, contact.Identified); + Assert.Equal(info, contact.Info); + if (tracked) AssertBearing(.6f, .8f, contact.Bearing); } [Fact] @@ -540,16 +689,17 @@ public sealed class ContactRevealTests : IDisposable // ---- The one rule for info per signature ---- [Fact] - public void PassiveInfoPerSignatureAveragesTheActiveSensors() + public void PassiveInfoAveragesTheActiveSensors() { var one = Add(float3(0, 0, 0), new[] { "Eye" }); var two = Add(float3(500, 0, 0), new[] { "Eye", "Ear" }); var none = Add(float3(1000, 0, 0)); - // sum(s) / (d n k): 2 / (100 x .5) with one sensor, (2 + 4) / (100 x 2 x .5) with two. - Assert.Equal(.04f, one.PassiveInfoPerSignature(float2(0, 100)), 5); - Assert.Equal(.06f, two.PassiveInfoPerSignature(float2(500, 100)), 5); - Assert.Equal(0f, none.PassiveInfoPerSignature(float2(1000, 100))); + // sum(s) / (d n k): 2 / (100 x .5) with one sensor, (2 + 4) / (100 x 2 x .5) with two; linear in the signature. + Assert.Equal(.04f, one.PassiveInfo(float2(0, 100), 1f), 5); + Assert.Equal(.12f, one.PassiveInfo(float2(0, 100), 3f), 5); + Assert.Equal(.06f, two.PassiveInfo(float2(500, 100), 1f), 5); + Assert.Equal(0f, none.PassiveInfo(float2(1000, 100), 1f)); } // ---- SenseCostIsBounded ----