Facility: the rig asks the deposit pick which ore to work, lands where Advance lands, and a mixed list splits

Soul s1 on cut-facility-extraction.
- shares-follow-first-belt, rig-hammers-first-rocks: on screen a rig works an ore at a time as Production.Pick names it from the cycles
  it has worked (the one owner of which ore next, Advance's too), at that recipe's own count and cycle, on a whole rock whose belt holds the
  ore, chosen by a stable hash of the cycle so rocks take turns. FireControl.Extract takes optional `only` (the one ore a rock pays) and
  `landing` (the bays); the player passes neither. Production.OreRate is deleted: nothing reads a mean rate now.
- rig-ignores-silo-share: the units land in StationWants.OutputBays, and nothing is worn unless StationWants.Room and a free cell say the
  ore lands, as Advance's put does.
- mixed-raw-list-picks-on-screen: on screen the ore entries of a design work the rocks and every other entry runs Advance alone.
- run-writer-comment-stale: ItemInstance.Run names its writer.
ExtractionTests: two belts of two ores agree on shares (0.71-1.41), rocks wear together (4 of 20 where a first-rock rig holds one), the
silo's share stops a rig and the station's other bay stays empty, a mixed list splits.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-29 08:56:53 +01:00
commit 1ce71101ff
6 changed files with 157 additions and 103 deletions

View file

@ -26,14 +26,16 @@ public class FacilityData : BehaviorData
// the run list of its design, fixed for a run because the catalog is.
//
// Two paths, by where the zone is (ruling auto-mining-is-off-screen-only). Off screen every facility advances by Production.Advance:
// an ore rig's raw recipes start by the deposit pick. On screen a run list of nothing but raw ore works the zone's rocks instead (Work),
// through Zone.Wear and FireControl.Extract, the player's own mining, at Production.OreRate, the rate Advance's pick averages to; the
// player sees the rocks break, and the unit's FacilityRun is left as the off-screen path last wrote it. Any other list, a skimmer's
// included (ruling skimmers-run-their-recipe-on-screen-for-now), runs Advance in both.
// an ore rig's raw recipes start by Production.Pick. On screen the raw ore entries of the list work the zone's rocks instead (Work),
// through Zone.Wear and FireControl.Extract, the player's own mining, an ore at a time as Production.Pick names it, at the recipe's own
// cycle and count, so the units of each ore come as the pick has them off screen; the player sees the rocks break, and the unit's
// FacilityRun is left as the off-screen path last wrote it. Every other entry, a skimmer's volatiles included (ruling
// skimmers-run-their-recipe-on-screen-for-now), runs Advance in both.
public class Facility : Behavior
{
private IReadOnlyList<RecipeData> _runList;
private bool _mines;
private IReadOnlyList<RecipeData> _ores;
private IReadOnlyList<RecipeData> _others;
private EntityHold _hold;
// Why the facility advanced no further this step: derived by Advance every step, never stored on the unit.
@ -46,66 +48,101 @@ public class Facility : Behavior
// How many steps tried to take a cycle's inputs: a diagnostic of the skip above, derived and not persisted.
public int Attempts { get; private set; }
// The rig's on-screen cycle, derived and not persisted: how many it has worked (the pick's count, begun from the unit's own count
// when first worked), the recipe and rock of the one under way, and the sim seconds into it.
private long _worked = -1;
private RecipeData _working;
private ChunkId _rock;
private bool _rocked;
private double _into;
public Facility(FacilityData data, EquippedItem item) : base(data, item)
{
SetRunList();
}
// The run list as the catalog holds it, and whether it is all raw ore: the lists that work rocks on screen.
// The run list as the catalog holds it, split into the raw ore entries that work rocks on screen and the rest.
private void SetRunList()
{
_runList = Production.RunList(ItemManager.ItemData, Item.EquippableItem.Data.Key);
_mines = _runList.Count > 0 && _runList.All(recipe => Recipes.IsRaw(ItemManager.ItemData, recipe)
&& ItemManager.ItemData.Get(recipe.Output) is SimpleCommodityData output && FieldKinds.IsOre(ItemManager.ItemData, output));
var catalog = ItemManager.ItemData;
_runList = Production.RunList(catalog, Item.EquippableItem.Data.Key);
bool Ore(RecipeData recipe) => Recipes.IsRaw(catalog, recipe) && catalog.Get(recipe.Output) is SimpleCommodityData output && FieldKinds.IsOre(catalog, output);
_ores = _runList.Where(Ore).ToArray();
_others = _runList.Where(recipe => !Ore(recipe)).ToArray();
}
// The on-screen step: one zone step.
public override bool Execute(float dt)
{
if (_mines) Work(dt);
else Advance(dt);
if (_ores.Count > 0) Work(dt);
if (_others.Count > 0 || _ores.Count == 0) Advance(dt, _others);
return true;
}
// The off-screen step: the span the zone missed, always the recipe rule.
public void Span(double seconds) => Advance(seconds);
public void Span(double seconds) => Advance(seconds, _runList);
// The rock an ore rig works, in sim time dt: the first whole rock in belt Site then index order (the order Deposits reads the
// belts in) takes the damage that owes OreRate x dt units (what FireControl.Extract pays for the damage it absorbs), and Extract pays
// the station by dice of its own stream. Barren with no whole rock or no ore the ground holds; Room, with nothing worn, when no bay
// has a cell for any of its ores. A cycle begun off screen resumes when the zone is off screen again.
// Whether the station's output bays have a cell for one unit of the good and its share of the silo has room: the landing Advance's
// put takes, asked before any rock is worn.
private bool Lands(CultRecordKey good) =>
StationWants.Room(Entity, good) >= 1 && StationWants.OutputBays(Entity, good).Any(bay =>
bay.TryFindSpace(new SimpleCommodity { Data = new CultRecordRef<ItemData>(good), Quantity = 1 }));
// The rock an ore rig works, in sim time dt. A cycle begins at Production.Pick on the cycles worked (then the next recipe the ground
// holds and the station has room for, as Advance goes on), picks a whole rock whose belt holds that ore, by a stable hash of the cycle
// (so rocks take turns), and for the recipe's Cycle seconds wears it by what OutputCount units of it owe (what FireControl.Extract
// pays for the damage it absorbs); a rock that breaks gives way to the next. Extract pays that ore only, into the output bays, by dice
// of its own stream. Barren with no ore the ground holds or no whole rock of it; Room, with nothing worn, when the ore has no landing.
private void Work(double dt)
{
_hold ??= new EntityHold(Entity);
var catalog = ItemManager.ItemData;
var zone = Entity.Zone;
var rate = Production.OreRate(catalog, _runList, _hold);
if (zone == null || !(rate > 0)) { Stall = Stall.Barren; return; }
var rock = default(ChunkId);
var found = false;
foreach (var belt in zone.AsteroidBelts.OrderBy(entry => entry.Value.Data.SiteOr(entry.Key), StringComparer.Ordinal))
if (zone == null) { Stall = Stall.Barren; return; }
if (_worked < 0) _worked = Item.EquippableItem.Run?.Cycles ?? 0;
if (_working == null)
{
for (var index = 0; index < belt.Value.Data.Asteroids.Length && !found; index++)
if (zone.ChunkExists(rock = new ChunkId(belt.Key, index))) found = true;
if (found) break;
var pick = Production.Pick(_ores, _worked, _hold);
if (pick < 0) { Stall = Stall.Barren; return; }
for (var step = 0; step < _ores.Count && _working == null; step++)
{
var candidate = _ores[(pick + step) % _ores.Count];
if (_hold.Deposit(candidate.Output.Key) > 0f && Lands(candidate.Output.Key)) _working = candidate;
}
if (_working == null) { Stall = Stall.Room; return; }
_into = 0;
_rocked = false;
}
var ore = _working.Output.Key;
if (!Lands(ore)) { Stall = Stall.Room; return; }
if (!_rocked || !zone.ChunkExists(_rock))
{
var province = zone.GalaxyZone?.Province?.Type ?? default;
var whole = new List<ChunkId>();
foreach (var belt in zone.AsteroidBelts.OrderBy(entry => entry.Value.Data.SiteOr(entry.Key), StringComparer.Ordinal))
{
if (!FieldKinds.Yield(belt.Value.Data.Kind, province, catalog).Any(yield => yield.Commodity.Key.Equals(ore))) continue;
for (var index = 0; index < belt.Value.Data.Asteroids.Length; index++)
if (zone.ChunkExists(new ChunkId(belt.Key, index))) whole.Add(new ChunkId(belt.Key, index));
}
if (whole.Count == 0) { _rocked = false; Stall = Stall.Barren; return; }
_rock = whole[(int) ($"rock:{_worked}:{ore.Value}".StableHash() % (uint) whole.Count)];
_rocked = true;
}
if (!found) { Stall = Stall.Barren; return; }
var room = _runList.Any(recipe => _hold.Deposit(recipe.Output.Key) > 0f && Entity.CargoBays.Any(bay =>
bay.TryFindSpace(new SimpleCommodity { Data = new CultRecordRef<ItemData>(recipe.Output.Key), Quantity = 1 })));
if (!room) { Stall = Stall.Room; return; }
var settings = zone.Settings;
var size = ((AsteroidBeltData) zone.Planets[rock.Field]).Asteroids[rock.Index].Size;
var damage = (float) (rate * dt / settings.AsteroidYield.Evaluate(size) * settings.AsteroidHitpoints.Evaluate(size));
var size = ((AsteroidBeltData) zone.Planets[_rock.Field]).Asteroids[_rock.Index].Size;
var damage = (float) (_working.OutputCount / (double) _working.Cycle * dt / settings.AsteroidYield.Evaluate(size) * settings.AsteroidHitpoints.Evaluate(size));
var place = Entity.Equipment.IndexOf(Item);
var ordinal = SimulationDice.Mix(unchecked((uint) (long) Math.Floor(zone.ExactTime * 1000))) ^ (uint) place;
FireControl.Extract(zone, rock, zone.Wear(rock, damage), Entity, SimulationDice.For(zone.DropSeed, SimulationDice.RigStream, ordinal));
FireControl.Extract(zone, _rock, zone.Wear(_rock, damage), Entity, SimulationDice.For(zone.DropSeed, SimulationDice.RigStream, ordinal),
ore, StationWants.OutputBays(Entity, ore));
Stall = Stall.None;
_into += dt;
if (_into >= _working.Cycle) { _worked++; _working = null; _rocked = false; }
}
private void Advance(double seconds)
private void Advance(double seconds, IReadOnlyList<RecipeData> list)
{
_hold ??= new EntityHold(Entity);
var idle = Item.EquippableItem.Run == null || Item.EquippableItem.Run.Progress <= 0;
@ -115,7 +152,7 @@ public class Facility : Behavior
// stale, so it is rebuilt from the catalog before that step.
if (!idle && Production.Live(ItemManager.ItemData, Item.EquippableItem.Run.Recipe) == null)
SetRunList();
var (run, stall) = Production.Advance(ItemManager.ItemData, _runList, Item.EquippableItem.Run, seconds, _hold);
var (run, stall) = Production.Advance(ItemManager.ItemData, list, Item.EquippableItem.Run, seconds, _hold);
Item.EquippableItem.Run = run;
Stall = stall;
if (idle && stall == Stall.Inputs) _failedAt = _hold.Revision;