From f5eeb2e21481db42c78f6762907e4363e9b5a8ac Mon Sep 17 00:00:00 2001 From: Nibu Date: Thu, 12 Nov 2026 23:00:16 +0000 Subject: [PATCH] Zone.Wear returns the damage the rock absorbed; a break is ChunkExists Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/ServerShared/Zone.cs | 15 ++--- tests/Aetheria.Shared.Tests/DrawnPoseTests.cs | 2 +- .../Aetheria.Shared.Tests/MiningCut2Tests.cs | 62 +++++++++++-------- .../Aetheria.Shared.Tests/MiningCut3Tests.cs | 10 +-- .../Aetheria.Shared.Tests/MiningCut4aTests.cs | 2 +- .../Aetheria.Shared.Tests/MiningCut4bTests.cs | 4 +- .../Aetheria.Shared.Tests/MiningIndexTests.cs | 2 +- 7 files changed, 55 insertions(+), 42 deletions(-) diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index d0c5b5330..7cbde82ef 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -685,16 +685,17 @@ public class Zone return size.Exponent > 0 ? max(abs(size.Minimum), abs(size.Maximum)) : float.PositiveInfinity; } - // Cut 2: the only writer of chunk wear. Returns whether this hit broke the chunk. The break threshold is + // Cut 2: the only writer of chunk wear. Returns the damage the rock absorbed: the damage, or the hitpoints it had + // left when the hit broke it; 0 on a chunk that is already broken. Whether it broke is ChunkExists. The break threshold is // strictly greater-than, matching the deleted per-tick miner's own comparison against accumulated damage: // damage exactly equal to hitpoints does not yet break the chunk, only damage that exceeds them does. - // A chunk that is currently broken (zone time < BrokenUntil) absorbs nothing: this hit did not break it - // (it was already broken), and its wear stays zeroed until it respawns and starts fresh. - public bool Wear(ChunkId chunk, float damage) + // A chunk that is currently broken (zone time < BrokenUntil) absorbs nothing, and its wear stays zeroed until it + // respawns and starts fresh. + public float Wear(ChunkId chunk, float damage) { _wear.TryGetValue(chunk, out var wear); if (wear.BrokenUntil.HasValue && wear.BrokenUntil.Value > _time) - return false; + return 0f; var beltData = (AsteroidBeltData) Planets[chunk.Field]; var size = beltData.Asteroids[chunk.Index].Size; @@ -706,11 +707,11 @@ public class Zone { var respawnTime = Settings.AsteroidRespawnTime.Evaluate(size); _wear[chunk] = new ChunkWear { Damage = 0f, BrokenUntil = _time + respawnTime }; - return true; + return max(hitpoints - wear.Damage, 0f); } _wear[chunk] = new ChunkWear { Damage = newDamage, BrokenUntil = null }; - return false; + return damage; } // Cut 1 (docs/mining-cut.md): a chunk's pose is a pure function of zone time, computed when asked. No stored diff --git a/tests/Aetheria.Shared.Tests/DrawnPoseTests.cs b/tests/Aetheria.Shared.Tests/DrawnPoseTests.cs index 37111b114..e7254fac6 100644 --- a/tests/Aetheria.Shared.Tests/DrawnPoseTests.cs +++ b/tests/Aetheria.Shared.Tests/DrawnPoseTests.cs @@ -542,7 +542,7 @@ public sealed class DrawnPoseTests : IDisposable var chunk = new ChunkId(w.Belt, 5); var hitpoints = w.Zone.Settings.AsteroidHitpoints.Evaluate(w.Asteroids[5].Size); var unworn = w.Zone.ChunkRadius(chunk); - Assert.False(w.Zone.Wear(chunk, hitpoints * .9f)); + Assert.False(w.Zone.Breaks(chunk, hitpoints * .9f)); Assert.True(w.Zone.ChunkRadius(chunk) < unworn * .5f, "the rock has shrunk to less than half its size"); Assert.Equal(before, Meshes()); } diff --git a/tests/Aetheria.Shared.Tests/MiningCut2Tests.cs b/tests/Aetheria.Shared.Tests/MiningCut2Tests.cs index 54fc99972..7716e3c9a 100644 --- a/tests/Aetheria.Shared.Tests/MiningCut2Tests.cs +++ b/tests/Aetheria.Shared.Tests/MiningCut2Tests.cs @@ -134,9 +134,9 @@ public sealed class MiningCut2Tests : IDisposable var hp = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsA[index].Size); // Several partial hits under the threshold: still there, still shrinking. - Assert.False(f.Zone.Wear(chunk, hp * 0.3f)); + Assert.False(f.Zone.Breaks(chunk, hp * 0.3f)); Assert.True(f.Zone.ChunkExists(chunk)); - Assert.False(f.Zone.Wear(chunk, hp * 0.3f)); + Assert.False(f.Zone.Breaks(chunk, hp * 0.3f)); Assert.True(f.Zone.ChunkExists(chunk)); var before = f.Zone.ChunkRadius(chunk); @@ -147,11 +147,11 @@ public sealed class MiningCut2Tests : IDisposable // break condition as strictly greater-than, not greater-or-equal (today's behaviour, kept deliberately // -- see Zone.Wear's own comment). Equal-to-hitpoints must not break the chunk. var remaining = hp - hp * 0.6f; - Assert.False(f.Zone.Wear(chunk, remaining)); + Assert.False(f.Zone.Breaks(chunk, remaining)); Assert.True(f.Zone.ChunkExists(chunk), "damage exactly equal to hitpoints must not break the chunk"); // One more point of damage tips it over. - Assert.True(f.Zone.Wear(chunk, 0.01f)); + Assert.True(f.Zone.Breaks(chunk, 0.01f)); Assert.False(f.Zone.ChunkExists(chunk)); Assert.Equal(0f, f.Zone.ChunkRadius(chunk)); } @@ -165,7 +165,7 @@ public sealed class MiningCut2Tests : IDisposable var hp = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsA[index].Size); var respawn = f.Settings.AsteroidRespawnTime.Evaluate(f.AsteroidsA[index].Size); - Assert.True(f.Zone.Wear(chunk, hp + 1f)); + Assert.True(f.Zone.Breaks(chunk, hp + 1f)); Assert.False(f.Zone.ChunkExists(chunk)); // Uneven steps that sum to just under the authored respawn time: still broken. @@ -188,13 +188,13 @@ public sealed class MiningCut2Tests : IDisposable var hp = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsB[index].Size); var respawn = f.Settings.AsteroidRespawnTime.Evaluate(f.AsteroidsB[index].Size); - Assert.True(f.Zone.Wear(chunk, hp * 1.5f)); + Assert.True(f.Zone.Breaks(chunk, hp * 1.5f)); f.Zone.Update(respawn + 1f); Assert.True(f.Zone.ChunkExists(chunk)); // A hit well under hitpoints, on the freshly respawned chunk, must not break it -- if the old damage // (or the old broken state) leaked through, this hit alone would already be over any stale threshold. - Assert.False(f.Zone.Wear(chunk, hp * 0.4f)); + Assert.False(f.Zone.Breaks(chunk, hp * 0.4f)); Assert.True(f.Zone.ChunkExists(chunk)); var radius = f.Zone.ChunkRadius(chunk); Assert.True(radius > 0f && radius < f.Zone.AsteroidBelts[f.BeltB].UndamagedSize(index, f.Settings)); @@ -211,7 +211,7 @@ public sealed class MiningCut2Tests : IDisposable var chunkA1 = new ChunkId(f.BeltA, 1); var hpA0 = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsA[0].Size); - Assert.True(f.Zone.Wear(chunkA0, hpA0 + 1f)); + Assert.True(f.Zone.Breaks(chunkA0, hpA0 + 1f)); Assert.False(f.Zone.ChunkExists(chunkA0)); Assert.True(f.Zone.ChunkExists(chunkB0), "a different field at the same index must be unaffected"); @@ -230,8 +230,8 @@ public sealed class MiningCut2Tests : IDisposable var hpDamaged = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsA[4].Size); var hpBroken = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsB[3].Size); - Assert.False(f.Zone.Wear(damaged, hpDamaged * 0.45f)); - Assert.True(f.Zone.Wear(broken, hpBroken + 5f)); + Assert.False(f.Zone.Breaks(damaged, hpDamaged * 0.45f)); + Assert.True(f.Zone.Breaks(broken, hpBroken + 5f)); f.Zone.Update(1.3f); var damagedRadiusBefore = f.Zone.ChunkRadius(damaged); @@ -255,7 +255,7 @@ public sealed class MiningCut2Tests : IDisposable var hp = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsA[6].Size); var respawn = f.Settings.AsteroidRespawnTime.Evaluate(f.AsteroidsA[6].Size); - Assert.True(f.Zone.Wear(chunk, hp + 1f)); + Assert.True(f.Zone.Breaks(chunk, hp + 1f)); f.Zone.Update(respawn + 2f); Assert.True(f.Zone.ChunkExists(chunk), "sanity: this chunk must have respawned before packing"); @@ -333,20 +333,20 @@ public sealed class MiningCut2Tests : IDisposable var hp = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsA[index].Size); var respawn = f.Settings.AsteroidRespawnTime.Evaluate(f.AsteroidsA[index].Size); - Assert.True(f.Zone.Wear(chunk, hp + 1f)); + Assert.True(f.Zone.Breaks(chunk, hp + 1f)); Assert.False(f.Zone.ChunkExists(chunk)); // A hit well under hitpoints, inside the respawn window: must not break it (it is already broken) and // must not touch BrokenUntil. f.Zone.Update(respawn * 0.2f); - Assert.False(f.Zone.Wear(chunk, hp * 0.1f), "a hit on an already-broken chunk did not break it"); + Assert.False(f.Zone.Breaks(chunk, hp * 0.1f), "a hit on an already-broken chunk did not break it"); Assert.False(f.Zone.ChunkExists(chunk), "must still be absent -- the old bug un-broke it here"); Assert.Equal(0f, f.Zone.ChunkRadius(chunk)); // A second hit, this one alone well over hitpoints, inside the same window: still must not break it // (it is already broken) and must not push BrokenUntil further out. f.Zone.Update(respawn * 0.3f); - Assert.False(f.Zone.Wear(chunk, hp + 50f), "a hit on an already-broken chunk did not break it, even when large"); + Assert.False(f.Zone.Breaks(chunk, hp + 50f), "a hit on an already-broken chunk did not break it, even when large"); Assert.False(f.Zone.ChunkExists(chunk)); // Just under the original respawn time (measured from the original break, not from either re-hit): @@ -360,9 +360,9 @@ public sealed class MiningCut2Tests : IDisposable Assert.Equal(f.Zone.AsteroidBelts[f.BeltA].UndamagedSize(index, f.Settings), f.Zone.ChunkRadius(chunk), 4); // Worn again from fresh: breaks on the normal threshold, not one warped by the earlier no-op hits. - Assert.False(f.Zone.Wear(chunk, hp * 0.9f)); + Assert.False(f.Zone.Breaks(chunk, hp * 0.9f)); Assert.True(f.Zone.ChunkExists(chunk)); - Assert.True(f.Zone.Wear(chunk, hp * 0.2f)); + Assert.True(f.Zone.Breaks(chunk, hp * 0.2f)); Assert.False(f.Zone.ChunkExists(chunk)); } @@ -377,9 +377,9 @@ public sealed class MiningCut2Tests : IDisposable var hp = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsB[idx].Size); var respawn = f.Settings.AsteroidRespawnTime.Evaluate(f.AsteroidsB[idx].Size); - Assert.True(f.Zone.Wear(chunk, hp + 1f)); + Assert.True(f.Zone.Breaks(chunk, hp + 1f)); f.Zone.Update(respawn * 0.4f); - Assert.False(f.Zone.Wear(chunk, hp * 0.5f), "re-hit inside the window did not break an already-broken chunk"); + Assert.False(f.Zone.Breaks(chunk, hp * 0.5f), "re-hit inside the window did not break an already-broken chunk"); var pack = f.Zone.PackZone(); var reloaded = new Zone(f.Items, f.Settings, pack, new GalaxyZone { Name = "MiningCut2ReHitReload", Owner = null }, null); @@ -424,7 +424,7 @@ public sealed class MiningCut2Tests : IDisposable var hp = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsA[index].Size); var respawn = f.Settings.AsteroidRespawnTime.Evaluate(f.AsteroidsA[index].Size); - Assert.True(f.Zone.Wear(chunk, hp + 1f)); + Assert.True(f.Zone.Breaks(chunk, hp + 1f)); f.Zone.Update(respawn); // zone time now equals BrokenUntil exactly, not "respawn plus a margin". Assert.True(f.Zone.ChunkExists(chunk), "must read as respawned the instant zone time reaches BrokenUntil, not strictly after it"); @@ -458,11 +458,11 @@ public sealed class MiningCut2Tests : IDisposable var hp = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsA[index].Size); var respawn = f.Settings.AsteroidRespawnTime.Evaluate(f.AsteroidsA[index].Size); - Assert.True(f.Zone.Wear(chunk, hp + 1f)); + Assert.True(f.Zone.Breaks(chunk, hp + 1f)); f.Zone.Update(respawn); Assert.True(f.Zone.ChunkExists(chunk)); - Assert.True(f.Zone.Wear(chunk, hp + 1f), "a hit on the instant of respawn breaks the respawned chunk again"); + Assert.True(f.Zone.Breaks(chunk, hp + 1f), "a hit on the instant of respawn breaks the respawned chunk again"); Assert.False(f.Zone.ChunkExists(chunk)); } @@ -477,9 +477,9 @@ public sealed class MiningCut2Tests : IDisposable var hp = f.Settings.AsteroidHitpoints.Evaluate(f.AsteroidsA[index].Size); var respawn = f.Settings.AsteroidRespawnTime.Evaluate(f.AsteroidsA[index].Size); - Assert.True(f.Zone.Wear(chunk, hp + 1f)); + Assert.True(f.Zone.Breaks(chunk, hp + 1f)); f.Zone.Update(respawn + 1f); - Assert.False(f.Zone.Wear(chunk, hp * 0.3f)); + Assert.False(f.Zone.Breaks(chunk, hp * 0.3f)); var packed = f.Zone.PackZone().ChunkWear.Find(w => w.Field.Equals(chunk.Field) && w.Index == index); Assert.NotNull(packed); @@ -501,12 +501,12 @@ public sealed class MiningCut2Tests : IDisposable var undamaged = f.Zone.ChunkPose(f.BeltA, index).w; Assert.Equal(f.Zone.AsteroidBelts[f.BeltA].UndamagedSize(index, f.Settings), undamaged, 4); - Assert.False(f.Zone.Wear(chunk, hp * 0.6f)); + Assert.False(f.Zone.Breaks(chunk, hp * 0.6f)); var worn = f.Zone.ChunkPose(f.BeltA, index).w; Assert.Equal(f.Zone.ChunkRadius(chunk), worn, 4); Assert.True(worn < undamaged, "a worn chunk is smaller than an untouched one"); - Assert.True(f.Zone.Wear(chunk, hp)); + Assert.True(f.Zone.Breaks(chunk, hp)); Assert.Equal(0f, f.Zone.ChunkPose(f.BeltA, index).w); } } @@ -520,3 +520,15 @@ internal static class MiningCut2TestExtensions return list; } } + +internal static class ChunkWearTestExtensions +{ + // Wears the chunk and reports whether that hit broke it: Zone.Wear returns the damage absorbed, and a break is + // ChunkExists going from true to false. + public static bool Breaks(this Zone zone, ChunkId chunk, float damage) + { + var there = zone.ChunkExists(chunk); + zone.Wear(chunk, damage); + return there && !zone.ChunkExists(chunk); + } +} diff --git a/tests/Aetheria.Shared.Tests/MiningCut3Tests.cs b/tests/Aetheria.Shared.Tests/MiningCut3Tests.cs index 76a67472a..d020b068e 100644 --- a/tests/Aetheria.Shared.Tests/MiningCut3Tests.cs +++ b/tests/Aetheria.Shared.Tests/MiningCut3Tests.cs @@ -416,7 +416,7 @@ public sealed partial class MiningCut3Tests : IDisposable var whole = s.Zone.ChunkVisibility(chunk); var hitpoints = PlanetSettings().AsteroidHitpoints.Evaluate(.5f); - Assert.False(s.Zone.Wear(chunk, hitpoints * .5f)); + Assert.False(s.Zone.Breaks(chunk, hitpoints * .5f)); var worn = s.Zone.ChunkVisibility(chunk); Assert.InRange(worn, whole * .05f, whole * .95f); Assert.Equal(ExpectedVisibility(s, chunk, kind), worn, 4); @@ -433,7 +433,7 @@ public sealed partial class MiningCut3Tests : IDisposable Tick((observer, eyeAt)); Assert.True(observer.ChunkVisible(chunk)); - Assert.True(s.Zone.Wear(chunk, PlanetSettings().AsteroidHitpoints.Evaluate(.5f) * 2f)); + Assert.True(s.Zone.Breaks(chunk, PlanetSettings().AsteroidHitpoints.Evaluate(.5f) * 2f)); Assert.Equal(0f, s.Zone.ChunkVisibility(chunk)); Assert.Equal(0f, observer.ChunkInfo(chunk)); Assert.False(observer.SetTarget(chunk)); @@ -487,7 +487,7 @@ public sealed partial class MiningCut3Tests : IDisposable var centre = float2(400, -250); // off the origin, so a query that ignored the belt centre would disagree var s = BuildSceneAt(centre, new[] { kind }, Belt(kind, rocks)); var belt = s.Belts[0]; - Assert.True(s.Zone.Wear(new ChunkId(belt, 5), 1e6f)); // broken + Assert.True(s.Zone.Breaks(new ChunkId(belt, 5), 1e6f)); // broken var found = new List(); var sawSome = false; @@ -555,7 +555,7 @@ public sealed partial class MiningCut3Tests : IDisposable Assert.True(watcher.SetTarget(second)); Tick((watcher, eyeAt)); Assert.False(watcher.Target.Value.IsNone); - Assert.True(s.Zone.Wear(second, 1e6f)); + Assert.True(s.Zone.Breaks(second, 1e6f)); Tick((watcher, eyeAt)); Assert.True(watcher.Target.Value.IsNone); @@ -565,7 +565,7 @@ public sealed partial class MiningCut3Tests : IDisposable var third = new ChunkId(s.Belts[0], 2); Assert.True(docked.SetTarget(third)); docked.Deactivate(); - Assert.True(s.Zone.Wear(third, 1e6f)); + Assert.True(s.Zone.Breaks(third, 1e6f)); docked.Activate(); Assert.True(docked.Target.Value.IsNone); } diff --git a/tests/Aetheria.Shared.Tests/MiningCut4aTests.cs b/tests/Aetheria.Shared.Tests/MiningCut4aTests.cs index 7d43c86d3..49fc75886 100644 --- a/tests/Aetheria.Shared.Tests/MiningCut4aTests.cs +++ b/tests/Aetheria.Shared.Tests/MiningCut4aTests.cs @@ -181,7 +181,7 @@ public sealed partial class MiningCut3Tests { var (s, rock) = OneRock(); var at = At(s, rock); - Assert.True(s.Zone.Wear(rock, 1e6f)); + Assert.True(s.Zone.Breaks(rock, 1e6f)); var subs = new List(); var absorbs = Absorbs(s, subs); FireControl.Detonate(s.Zone, at, 8f, 10f, DamageType.Kinetic); diff --git a/tests/Aetheria.Shared.Tests/MiningCut4bTests.cs b/tests/Aetheria.Shared.Tests/MiningCut4bTests.cs index 351e875a2..6b263652a 100644 --- a/tests/Aetheria.Shared.Tests/MiningCut4bTests.cs +++ b/tests/Aetheria.Shared.Tests/MiningCut4bTests.cs @@ -291,7 +291,7 @@ public sealed class MiningCut4bTests : IDisposable FireControl.Fire(rig.Gun, rig.GunItem, rig.Shooter); Assert.Empty(rig.Resolved); - Assert.True(rig.Zone.Wear(rig.Rock, 1e6f)); + Assert.True(rig.Zone.Breaks(rig.Rock, 1e6f)); Run(rig, 2.5f); var outcome = Assert.Single(rig.Resolved); @@ -383,7 +383,7 @@ public sealed class MiningCut4bTests : IDisposable Assert.True(frozen.Hit, "fixture: the commit rolled a hit"); Assert.Empty(rig.Resolved); - Assert.True(rig.Zone.Wear(rig.Rock, 1e6f)); + Assert.True(rig.Zone.Breaks(rig.Rock, 1e6f)); Run(rig, 2.5f); var outcome = Assert.Single(rig.Resolved); diff --git a/tests/Aetheria.Shared.Tests/MiningIndexTests.cs b/tests/Aetheria.Shared.Tests/MiningIndexTests.cs index e47f9a4a1..a90f8343b 100644 --- a/tests/Aetheria.Shared.Tests/MiningIndexTests.cs +++ b/tests/Aetheria.Shared.Tests/MiningIndexTests.cs @@ -70,7 +70,7 @@ public sealed partial class MiningCut3Tests var centre = float2(-300, 820); var s = BuildSceneAt(centre, 5000f, 1234.5, null, new[] { kind }, Belt(kind, Annulus(rng, 3000, 200f, 900f)), Belt(kind, Annulus(rng, 700, 1000f, 1060f))); - for (var i = 0; i < 40; i++) Assert.True(s.Zone.Wear(new ChunkId(s.Belts[0], i * 73), 1e6f)); + for (var i = 0; i < 40; i++) Assert.True(s.Zone.Breaks(new ChunkId(s.Belts[0], i * 73), 1e6f)); var found = new List(); var nonEmpty = 0;