Zone: a death watch exists only for a non-member and is disposed by Remove and Clear; tests for duplicate Add, Clear and the pickup room boundary

WatchDeath returns when the entity already holds a watch, so a duplicate
Entities.Add neither throws nor leaves a second live subscription;
Entities.Clear (ObserveReset) disposes every watch. Adds
AMemberAddedTwiceStillDropsEachItemOnce, AClearedZoneDropsNothingWhenAFormerMemberDies
(finding duplicate-add-leaks-second-watch) and
APickUpOfExactlyTheRoomLeftFitsAndOneMoreChangesNothing (finding
pickup-room-arithmetic-unpinned, from Soul's boundary probe). Unverified
until the Yggdrasil job; no commit here is known not to build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-13 00:43:12 +02:00
commit f2bf0c0410
2 changed files with 65 additions and 2 deletions

View file

@ -105,12 +105,14 @@ public class Zone
// The death ordinal of the loot dice: the nth death in this zone rolls on SimulationDice.For(CombatSeed, LootStream, n).
private uint _deaths;
// One death subscription per member entity, owned here: WatchDeath installs it, UnwatchDeath (the entity leaving
// Entities) disposes it, and nothing else touches it.
// One death subscription per member entity, owned here: a membership record and its subscription are created
// together, and only when the entity is not already a member. Leaving Entities (Remove) or resetting it (Clear)
// disposes it; nothing else touches it.
private readonly Dictionary<Entity, IDisposable> _deathWatches = new Dictionary<Entity, IDisposable>();
private void WatchDeath(Entity entity)
{
if (_deathWatches.ContainsKey(entity)) return;
_deathWatches.Add(entity, entity.Death.Subscribe(_ => { DropLoot(entity); Entities.Remove(entity); entity.Deactivate(); }));
}
@ -119,6 +121,12 @@ public class Zone
if (_deathWatches.Remove(entity, out var watch)) watch.Dispose();
}
private void UnwatchAll()
{
foreach (var watch in _deathWatches.Values) watch.Dispose();
_deathWatches.Clear();
}
public Zone(ItemManager itemManager, PlanetSettings settings, ZonePack pack, GalaxyZone galaxyZone, Galaxy galaxy)
{
_time = pack.Time;
@ -138,6 +146,7 @@ public class Zone
// disposed on removal (death, dock, jump), so an entity admitted again never holds a second one.
Entities.ObserveAdd().Subscribe(add => WatchDeath(add.Value));
Entities.ObserveRemove().Subscribe(remove => UnwatchDeath(remove.Value));
Entities.ObserveReset().Subscribe(_ => UnwatchAll());
// The one record of a removal for observers (ruling reveal-ends-on-perception): read by the next Sense pass.
Entities.ObserveRemove().Subscribe(remove => _departures.Add(new Departure(remove.Value, remove.Value.Position.xz,
remove.Value.Visibility - remove.Value.SignatureOn(SignatureChannel.Emission))));