Zone: every writer of what the belt tasks read settles them first

Soul's F5 and F6. The asteroid-belt tasks read the clock, the orbits and each
belt's damage and respawn state. Update advanced the clock before waiting on
last tick's tasks (Soul measured 242k transforms placed for the next tick in
576 of 3000 ticks), MineAsteroid wrote damage and respawn state under a
running task, CreateOrbit and AddOrbit wrote the orbits, and a task that threw
stayed in the list, so Wait rethrew it on every tick after.

SettleBelts waits for the tasks and clears the list even when one failed; Update
settles before it advances the clock, and MineAsteroid, CreateOrbit and
AddOrbit settle before they write.

Tests: over 3000 ticks every asteroid of a belt is placed for the time of the
tick that started its task; mining every asteroid of a belt, chipping and
breaking each four times a tick for 1000 ticks, never races the task; a task
that fails surfaces once and later ticks run clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 20:46:31 +02:00
commit f879527158
2 changed files with 91 additions and 4 deletions

View file

@ -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];

View file

@ -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);