diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index b1d7239bd..0af842695 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -166,16 +166,29 @@ public class Zone public void AddOrbit(OrbitData orbit) { + SettleBelts(); Orbits.Add(_itemManager.ItemData.RefOf(orbit).Key, new Orbit(Settings, orbit)); } + // The asteroid belts' tasks read the clock, the orbits and each belt's damage and respawn state. Everything that + // writes any of those settles the running tasks first. The list is cleared even when a task failed, so a failure + // surfaces once, here, instead of on every later tick. + private void SettleBelts() + { + try + { + Task.WaitAll(BeltUpdates.ToArray()); + } + finally + { + BeltUpdates.Clear(); + } + } + public void Update(float deltaTime) { + SettleBelts(); _time += deltaTime; - // Last tick's belt tasks read the orbit cache; they finish before it is cleared. - foreach (var t in BeltUpdates) - t.Wait(); - BeltUpdates.Clear(); _updatedOrbits.Clear(); foreach (var orbit in Orbits) { @@ -308,6 +321,7 @@ public class Zone var currentPhase = frac(_time / period); var storedPhase = (float) frac(phase - currentPhase); + SettleBelts(); var orbit = new OrbitData { Distance = distance, @@ -320,6 +334,7 @@ public class Zone public void MineAsteroid(Entity miner, CultRecordKey asteroidBelt, int asteroid, float damage, float efficiency, float penetration) { + SettleBelts(); var beltData = Planets[asteroidBelt] as AsteroidBeltData; var belt = AsteroidBelts[asteroidBelt]; //var asteroidTransform = belt.Transforms[asteroid]; diff --git a/tests/Aetheria.Shared.Tests/RunStartTests.cs b/tests/Aetheria.Shared.Tests/RunStartTests.cs index 31ea78fc1..de0dc9edc 100644 --- a/tests/Aetheria.Shared.Tests/RunStartTests.cs +++ b/tests/Aetheria.Shared.Tests/RunStartTests.cs @@ -534,6 +534,78 @@ public sealed class RunStartTests : IDisposable Assert.True(stalls.Count == 0, $"{stalls.Count} readings of an orbit standing still; first {stalls.FirstOrDefault()}"); } + // A belt's asteroids are laid out for one time: the time of the tick that started the belt's task. Were the + // clock advanced before that task finished, some asteroids would be placed for the next tick and some for this. + [Fact] + public void EveryAsteroidInABeltIsPlacedForTheSameTime() + { + var zone = Arena(new Scenario { Ambient = false }); // no ships: nothing but the zone runs + var (key, belt) = zone.AsteroidBelts.First(); + var asteroids = ((AsteroidBeltData) zone.Planets[key]).Asteroids; + const float dt = .02f; + float2 At(Asteroid asteroid, float time, float2 center) => + OrbitData.Evaluate((float) frac((double) time / zone.Settings.OrbitPeriod.Evaluate(asteroid.Distance) + asteroid.Phase)) * asteroid.Distance + center; + var torn = 0; + zone.Update(dt); + for (var tick = 0; tick < 3000; tick++) + { + var started = zone.Time; // the time the running task was started for + zone.Update(dt); // settles that task and publishes its layout + for (var i = 0; i < asteroids.Length; i++) + { + var placed = belt.Transforms[i].xy; + var here = At(asteroids[i], started, belt.OrbitPosition); + var next = At(asteroids[i], started + dt, belt.OrbitPosition); + if (lengthsq(next - here) < 1e-6f) continue; // too slow to tell the two times apart + if (lengthsq(placed - here) > lengthsq(placed - next)) torn++; + } + } + Assert.True(torn == 0, $"{torn} asteroid placements were for the next tick"); + } + + // Mining writes a belt's damage and respawn state, which the belt's task reads, so it waits for the task first. + // This drives mining hard while the belt updates and fails on any exception the race throws; the race itself is + // rare enough that removing the wait did not fail it in two runs, so it guards, but does not prove, the rule. + [Fact] + public void MiningWhileTheBeltUpdatesNeverRacesIt() + { + var zone = Arena(new Scenario { Ambient = false }); // no ships: nothing but the zone runs + var (key, _) = zone.AsteroidBelts.First(); + var data = (AsteroidBeltData) zone.Planets[key]; + var count = data.Asteroids.Length; + var miner = zone.Entities.First(); + for (var tick = 0; tick < 1000; tick++) + { + zone.Update(.02f); + for (var pass = 0; pass < 4; pass++) + { + // Chip every asteroid (the belt's damage table fills), then break every one (it empties again). + if (data.Resources.Count > 0) + for (var i = 0; i < count; i++) zone.MineAsteroid(miner, key, (i + tick) % count, 1e-3f, 0, 1); + for (var i = 0; i < count; i++) zone.MineAsteroid(miner, key, (i + tick) % count, 1e6f, 0, 1); + } + } + } + + // A belt task that fails surfaces once, on the next tick, and not on every tick after it. + [Fact] + public void AFailedBeltTaskSurfacesOnce() + { + var zone = Arena(new Scenario { Ambient = false }); // no ships: nothing but the zone runs + var (key, _) = zone.AsteroidBelts.First(); + var data = (AsteroidBeltData) zone.Planets[key]; + var asteroids = data.Asteroids; + zone.Update(.02f); + var failures = 0; + data.Asteroids = asteroids.Append(asteroids[0]).ToArray(); // one asteroid more than the belt has room for + zone.Update(.02f); // starts a task that will fail + try { zone.Update(.02f); } catch (AggregateException) { failures++; } // settles it, starts another + data.Asteroids = asteroids; + try { zone.Update(.02f); } catch (AggregateException) { failures++; } // settles that one + Assert.True(failures >= 1, "the failed task surfaced"); + for (var tick = 0; tick < 10; tick++) zone.Update(.02f); + } + private LoadoutGenerator PreludeGenerator() => new LoadoutGenerator(ref _items.Random, _items, _galaxy, _galaxy.Entrance, _protagonist, .5f);