Station generation installs its kind's Facilities on its Facility rows before any interior gear

LoadoutGenerator.EquipFacilities seats each of the kind's Facilities (largest shape first, then by name), branded by the station faction's own product else the first by name, drawing nothing from Random; it throws naming hull, kind and design when no row takes one. The docking bay, heater and interior fill now come after, so no interior gear can sit on a Facility row.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-28 05:23:56 +01:00
commit 20ec6bc6eb
3 changed files with 145 additions and 3 deletions

View file

@ -18,7 +18,8 @@ public class FacilityData : BehaviorData
}
// The installed facility that will run its recipes (ruling demand-consumed-inputs-processed-by-facilities). It runs nothing until
// follow-up facility-production: it is seated, priced and carried, and that is all.
// follow-up facility-production: LoadoutGenerator.EquipFacilities seats it on a station's Facility row, and it is priced and carried,
// and that is all.
public class Facility : Behavior
{
public Facility(FacilityData data, EquippedItem item) : base(data, item) { }

View file

@ -103,8 +103,9 @@ public class LoadoutGenerator
// A stand-in hull keeps its own model but not its own name: the station is what its kind says it is.
if (!hullData.Kind.Key.Equals(kind.Key) && ItemManager.ItemData.Get(kind) is { } kindData) entity.Name = kindData.Name;
// Hardpoint gear goes in first so the docking bay can only take space that gear left free
// Hardpoint gear, then the kind's facilities, go in before any interior gear so the docking bay can only take space they left free
EquipHardpoints(entity);
EquipFacilities(entity, kind);
var emptyShape = entity.UnoccupiedSpace;
@ -365,6 +366,29 @@ public class LoadoutGenerator
throw new InvalidLoadoutException("Failed to equip selected tractor beam!");
}
// Installs the station kind's Facilities, largest shape first then by name (StationKinds.FacilityRoomFaults' order), each on a free
// Facility row that takes it. The brand is derived, never drawn: the station faction's own product of the design, else the first by
// name. A facility is on no market, so the product need not be offered. Draws nothing from Random.
private void EquipFacilities(Entity entity, CultRecordRef<StationKindData> kind)
{
var kindData = ItemManager.ItemData.Get(kind);
if (kindData == null) return;
var designs = kindData.Facilities.Select(facility => ItemManager.ItemData.Get(facility)).OfType<CraftedItemData>()
.OrderByDescending(design => design.Shape.Coordinates.Length).ThenBy(design => design.Name, StringComparer.Ordinal);
foreach (var design in designs)
{
var key = ItemManager.ItemData.RefOf(design).Key;
var products = ItemManager.ItemData.GetAll<FactionProductData>()
.Where(product => product.Design.Key.Equals(key)).OrderBy(product => product.Name, StringComparer.Ordinal).ToArray();
var product = products.FirstOrDefault(p => ItemManager.ItemData.Get(p.Manufacturer) == Faction) ?? products.FirstOrDefault();
var hullName = ItemManager.GetData(entity.Hull).Name;
if (product == null)
throw new InvalidLoadoutException($"Station hull '{hullName}' of kind '{kindData.Name}' has no product of facility {design.Name}.");
if (!entity.TryEquip(ItemManager.CreateInstance(product) as EquippableItem))
throw new InvalidLoadoutException($"Station hull '{hullName}' has no free Facility row that takes {design.Name}, a facility of station kind '{kindData.Name}'.");
}
}
private void EquipHardpoints(Entity entity)
{
var hullData = ItemManager.GetData(entity.Hull) as HullData;
@ -372,7 +396,7 @@ public class LoadoutGenerator
var previousProducts = new List<(FactionProductData product, GearData design)>();
foreach (var hardpoint in hullData.Hardpoints.OrderByDescending(h=>h.Shape.Coordinates.Length))
{
// A Facility row is filled by its kind's Facilities (follow-up station-buys-by-installed-gear); the random fill never does.
// A Facility row is filled only by EquipFacilities, from its kind's Facilities; the random fill never does.
if (hardpoint.Type == HardpointType.Facility) continue;
if (hardpoint.Type == HardpointType.ControlModule)
{

View file

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GameCult.Caching;
using Xunit;
// Facility rows first (aetheria-living-world, cut station-facility-fit): a generated station installs its kind's Facilities on its
// Facility rows, from LoadoutGenerator.EquipFacilities, before the docking bay, the heater or any interior gear. The tests read the
// owners (ruling census-tests-read-the-owners): StationKinds names the served kinds and each kind's Facilities.
public sealed class StationFacilityFitTests
{
private static CultRecordRef<StationKindData> Kind(string id) => new CultRecordRef<StationKindData>(StationKindData.KeyOf(id));
// One station of the kind from this maker's generator, seeded; the entity unpacked from its pack.
private static (ItemManager Items, Entity Station) Generate(CultCache cache, Faction maker, CultRecordRef<StationKindData> kind, uint seed)
{
var items = new ItemManager(1u, cache, new ProvenanceLedger(), RunSaveTests.TestSettings(), _ => { });
var random = new CultMath.Random(seed);
var generator = new LoadoutGenerator(ref random, items, null, null, maker, .5f);
var pack = generator.GenerateStationLoadout(kind);
Assert.NotNull(pack);
return (items, EntitySerializer.Unpack(items, null, pack));
}
// Every (faction, fieldable kind) pair of the catalog with the kind's Facilities, over a few seeds so every hull of the kind is drawn.
private static IEnumerable<(Faction Maker, CultRecordRef<StationKindData> Kind, uint Seed)> Cases(CultCache cache) =>
cache.GetAll<Faction>().SelectMany(faction => StationKinds.FieldableKindsOf(cache, faction)
.Where(kind => cache.Get(kind).Facilities.Count > 0)
.SelectMany(kind => Enumerable.Range(1, 6).Select(seed => (faction, kind, (uint) seed))));
private static string[] FacilityKeys(Entity station) => station.Equipment.Where(item => item.Data.HardpointType == HardpointType.Facility)
.Select(item => item.Data.Name).OrderBy(name => name, StringComparer.Ordinal).ToArray();
[Fact]
public void EveryServedKindGetsItsFacilities()
{
using var cache = RestoredHullsTests.OpenCatalog();
var cases = Cases(cache).ToArray();
Assert.NotEmpty(cases);
foreach (var (maker, kind, seed) in cases)
{
var (items, station) = Generate(cache, maker, kind, seed);
var wanted = cache.Get(kind).Facilities.Select(facility => cache.Get(facility).Name).OrderBy(name => name, StringComparer.Ordinal).ToArray();
Assert.Equal(wanted, FacilityKeys(station));
Assert.All(station.Equipment.Where(item => item.Data.HardpointType == HardpointType.Facility),
item => Assert.Equal(HardpointType.Facility, item.Hardpoint.Type));
}
}
[Fact]
public void NoInteriorGearSitsOnAFacilityRow()
{
using var cache = RestoredHullsTests.OpenCatalog();
foreach (var (maker, kind, seed) in Cases(cache))
{
var (_, station) = Generate(cache, maker, kind, seed);
foreach (var tool in station.Equipment.Where(item => item.Data.HardpointType == HardpointType.Tool))
foreach (var cell in tool.Data.Shape.Coordinates)
{
var at = tool.Position + tool.Data.Shape.Rotate(cell, tool.EquippableItem.Rotation);
Assert.NotEqual(HardpointType.Facility, station.Hardpoints[at.x, at.y]?.Type);
}
}
}
// The hull row of a kind with no second row: an in-memory second facility has nowhere to go and generation names why.
[Fact]
public void AKindWithNoRowFailsByName()
{
using var cache = RestoredHullsTests.OpenCatalog();
var mining = cache.Get(Kind("mining"));
var extra = cache.Get(Kind("processing")).Facilities.Single();
mining.Facilities.Add(extra);
var maker = cache.GetAll<Faction>().First(faction => StationKinds.FieldableKindsOf(cache, faction).Any(kind => kind.Key.Equals(Kind("mining").Key)));
var error = Assert.Throws<InvalidLoadoutException>(() => Generate(cache, maker, Kind("mining"), 1));
Assert.Contains(mining.Name, error.Message);
Assert.Contains(cache.Get(extra).Name, error.Message);
Assert.Contains("station hull", error.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void FacilitiesSurviveThePack()
{
using var cache = RestoredHullsTests.OpenCatalog();
var (maker, kind, seed) = Cases(cache).First();
var items = new ItemManager(1u, cache, new ProvenanceLedger(), RunSaveTests.TestSettings(), _ => { });
var random = new CultMath.Random(seed);
var pack = new LoadoutGenerator(ref random, items, null, null, maker, .5f).GenerateStationLoadout(kind);
var before = EntitySerializer.Unpack(items, null, pack);
var again = EntitySerializer.Unpack(items, null, EntitySerializer.Pack(before) as OrbitalEntityPack);
Assert.NotEmpty(FacilityKeys(before));
Assert.Equal(FacilityKeys(before), FacilityKeys(again));
Assert.All(again.Equipment.Where(item => item.Data.HardpointType == HardpointType.Facility),
item => Assert.Equal(HardpointType.Facility, item.Hardpoint.Type));
}
// The facility step takes nothing from the generator's Random: the same seed with the kind's Facilities emptied draws the same
// station hull, docking bay and heater products.
[Fact]
public void EquipFacilitiesDrawsNothing()
{
using var with = RestoredHullsTests.OpenCatalog();
using var without = RestoredHullsTests.OpenCatalog();
foreach (var (maker, kind, seed) in Cases(with).Where(entry => entry.Seed <= 3))
{
without.Get(kind).Facilities.Clear();
var makerWithout = without.Get(with.RefOf(maker));
var (itemsA, a) = Generate(with, maker, kind, seed);
var (itemsB, b) = Generate(without, makerWithout, kind, seed);
Assert.Equal(a.HullData.Name, b.HullData.Name);
string Brand(ItemManager items, Entity station) => string.Join("|", station.Equipment
.Where(item => item.Data is DockingBayData || item.Data is GearData gear && LoadoutGenerator.IsHeater(gear))
.Select(item => item.Data.Name + ":" + items.ItemData.Get(item.Lot.Product)?.Name).OrderBy(text => text, StringComparer.Ordinal));
Assert.Equal(Brand(itemsA, a), Brand(itemsB, b));
}
}
}