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:
parent
82d4881ca2
commit
f2bf0c0410
2 changed files with 65 additions and 2 deletions
|
|
@ -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))));
|
||||
|
|
|
|||
|
|
@ -508,4 +508,58 @@ public sealed class LootTests : IDisposable
|
|||
Kill(Spawn(lab, float3(1, 0, 1)));
|
||||
Assert.True(lab.Zone.FloatingItems.Select(f => Math.Round(Math.Atan2(f.Body.Velocity.y, f.Body.Velocity.x), 3)).Distinct().Count() > 1);
|
||||
}
|
||||
|
||||
// A member added twice holds one death subscription, so it drops each item once, as a single death would.
|
||||
[Fact]
|
||||
public void AMemberAddedTwiceStillDropsEachItemOnce()
|
||||
{
|
||||
var once = Build("Once", probability: 1f);
|
||||
Kill(Spawn(once, float3(3, 0, 4)));
|
||||
var baseline = once.Zone.FloatingItems.Count;
|
||||
Assert.NotEqual(0, baseline);
|
||||
|
||||
var lab = Build("Twice", probability: 1f);
|
||||
var ship = Spawn(lab, float3(3, 0, 4));
|
||||
lab.Zone.Entities.Add(ship);
|
||||
Kill(ship);
|
||||
Assert.Equal(baseline, lab.Zone.FloatingItems.Count);
|
||||
}
|
||||
|
||||
// Clearing Entities removes every member, so a death afterwards drops nothing in this zone.
|
||||
[Fact]
|
||||
public void AClearedZoneDropsNothingWhenAFormerMemberDies()
|
||||
{
|
||||
var lab = Build("Cleared", probability: 1f);
|
||||
var ship = Spawn(lab, float3(3, 0, 4));
|
||||
lab.Zone.Entities.Clear();
|
||||
Kill(ship);
|
||||
Assert.Empty(lab.Zone.FloatingItems);
|
||||
}
|
||||
|
||||
// The pickup is whole or nothing at the arithmetic boundary: two stacks of 90 hold 20 more in all and no free
|
||||
// cell is left, so a stack of 20 is stored (quantity 200 across both) and a stack of 21 changes nothing.
|
||||
[Fact]
|
||||
public void APickUpOfExactlyTheRoomLeftFitsAndOneMoreChangesNothing()
|
||||
{
|
||||
var lab = Build();
|
||||
var taker = Station(lab, float3(0, 0, 0), stocked: false);
|
||||
var bay = taker.CargoBays[0];
|
||||
Assert.True(bay.TryStore(lab.Ore(90), new int2(0, 0)));
|
||||
Assert.True(bay.TryStore(lab.Ore(90), new int2(1, 0)));
|
||||
while (bay.TryStore(lab.Make("Spare"))) { }
|
||||
(int Quantity, int Cells) Held() => (bay.Cargo.Keys.OfType<SimpleCommodity>().Sum(c => c.Quantity), bay.Cargo.Count);
|
||||
var before = Held();
|
||||
|
||||
var over = lab.Ore(21);
|
||||
var overFloating = lab.Zone.Release(over, new float2(1, 1), float2.zero);
|
||||
Assert.False(lab.Zone.TryPickUp(taker, overFloating.Id));
|
||||
Assert.Equal(21, over.Quantity);
|
||||
Assert.Equal(before, Held());
|
||||
Assert.Contains(overFloating, lab.Zone.FloatingItems);
|
||||
|
||||
var exact = lab.Zone.Release(lab.Ore(20), new float2(1, 1), float2.zero);
|
||||
Assert.True(lab.Zone.TryPickUp(taker, exact.Id));
|
||||
Assert.Equal((200, before.Cells), Held());
|
||||
Assert.DoesNotContain(exact, lab.Zone.FloatingItems);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue