Every station carries Crew Quarters and buys what its crew eats (cut habitation-gear)

LoadoutGenerator seats the design after the heater, through the one choice of product for installed gear; StationWants.Drawn adds the table of every running habitation unit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-28 23:22:02 +01:00
commit 02651fa67b
4 changed files with 79 additions and 8 deletions

View file

@ -137,6 +137,21 @@ public class LoadoutGenerator
if (!entity.TryEquip(heater, heaterPosition))
throw new InvalidLoadoutException("Failed to equip selected heater!");
// Every station carries Crew Quarters (ruling installed-gear-decides-what-a-station-buys): its crew is what buys it food, water
// and air. Placed as the heater is, before the interior fills. Installed plant has a not-offered product, which a random draw
// never reaches, so the design is chosen as EquipFacilities chooses a facility: the largest that fits, by name.
emptyShape = entity.UnoccupiedSpace;
var crewQuarters = ItemManager.ItemData.GetAll<GearData>().Where(item => IsCrewQuarters(item) && item.Shape.FitsWithin(emptyShape, out _, out _))
.OrderByDescending(design => design.Shape.Coordinates.Length).ThenBy(design => design.Name, StringComparer.Ordinal).FirstOrDefault();
var crewProduct = crewQuarters == null ? null : InstalledProduct(crewQuarters);
if (crewProduct == null) throw new InvalidLoadoutException("No compatible crew quarters found for station!");
crewQuarters.Shape.FitsWithin(emptyShape, out var crewRotation, out var crewPosition);
var crew = ItemManager.CreateInstance(crewProduct) as EquippableItem;
crew.Rotation = crewRotation;
if (!entity.TryEquip(crew, crewPosition))
throw new InvalidLoadoutException("Failed to equip selected crew quarters!");
FillInterior(entity);
// The consumable goes in before the gear stock: sixteen gear picks drawn with replacement can fill a real hold,
@ -190,6 +205,10 @@ public class LoadoutGenerator
item.Behaviors.Any(b => b is ThermotoggleData { HighPass: false }) &&
item.Behaviors.Any(b => b is HeatData);
// Interior gear that houses a population: the station's Crew Quarters.
internal static bool IsCrewQuarters(GearData item) =>
item.HardpointType == HardpointType.Tool && item.Behaviors.Any(b => b is HabitationData);
// Every entity needs a hull, so hulls are always required
public (FactionProductData product, HullData design) RandomHull(HullType type, Predicate<HullData> hullFilter = null)
{
@ -388,11 +407,8 @@ public class LoadoutGenerator
.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;
var product = InstalledProduct(design);
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))
@ -400,6 +416,16 @@ public class LoadoutGenerator
}
}
// The product an installed design is made through, which may be not offered: this faction's own when it has one, else the first by
// name. Null when no product makes it. The one choice of product for gear that is installed rather than drawn.
private FactionProductData InstalledProduct(CraftedItemData design)
{
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();
return products.FirstOrDefault(p => ItemManager.ItemData.Get(p.Manufacturer) == Faction) ?? products.FirstOrDefault();
}
private void EquipHardpoints(Entity entity)
{
var hullData = ItemManager.GetData(entity.Hull) as HullData;

View file

@ -6,8 +6,8 @@ using MessagePack;
using Newtonsoft.Json;
// What a station buys (aetheria-living-world:ruling:installed-gear-decides-what-a-station-buys): the goods its installed
// recipe facilities draw, and, when it carries a silo, the goods of the haul orders that start at its orbit
// (ruling transit-depots-buy-for-onward-haul). Derived on every call from the station's gear and the catalog, stored nowhere.
// recipe facilities draw, the goods its installed habitation modules' tables name (the crew eats), and, when it carries a silo,
// the goods of the haul orders that start at its orbit (ruling transit-depots-buy-for-onward-haul). Derived on every call from the station's gear and the catalog, stored nowhere.
// StationServices.TrySellPart is gated by it and limited by the bays' room.
//
// Where goods land and how much of a product a station holds (cut facility-output-room). A station with a silo keeps what its
@ -87,6 +87,11 @@ public static class StationWants
foreach (var recipe in FacilityRecipes(station).Where(recipe => CanStart(station, recipe)))
foreach (var draw in Production.Draws(catalog, recipe))
wants.Add(draw);
// What its crew eats: the table of every habitation unit that runs (the module defines what and how much it consumes).
foreach (var unit in station.Equipment.Where(unit => unit.Enabled.Value && !unit.EquippableItem.IsDestroyed))
foreach (var habitation in unit.Behaviors.Select(behavior => behavior.Data).OfType<HabitationData>())
foreach (var row in habitation.Table.Where(row => row.Commodity.IsSet()))
wants.Add(row.Commodity.Key);
return wants;
}

View file

@ -73,8 +73,9 @@ public sealed partial class RunStartTests
if (left == 0) Assert.Equal(value, credits);
else Assert.True(credits > 0 && credits < value);
// Zenith's silo wants the ores of the haul order to the refinery: it buys what the hold still carries, and a fresh stack too.
var zenithWants = StationWants.Of(zenith);
// Zenith's silo wants the ores of the haul order to the refinery (and its crew quarters the goods of their table): it buys what the hold still carries, and a fresh stack too.
var crewEats = zenith.Equipment.SelectMany(unit => unit.Behaviors.Select(behavior => behavior.Data).OfType<HabitationData>()).SelectMany(data => data.Table.Select(row => row.Commodity.Key)).ToArray();
var zenithWants = StationWants.Of(zenith).Except(crewEats).ToArray();
Assert.NotEmpty(zenithWants);
Assert.All(zenithWants, good => Assert.True(FieldKinds.IsOre(_cache, _cache.Get(new CultRecordRef<ItemData>(good)))));
var silo = zenith.CargoBays.Single(bay => bay.Data is SiloData);

View file

@ -250,3 +250,42 @@ public sealed class HabitationTests : IDisposable
Assert.Null(MessagePackSerializer.Deserialize<EquippableItem>(precut, options).Habitants);
}
}
// Every station is a population (cut habitation-gear): a Crew Quarters in each station's interior, and what it eats is what the
// station buys. These read the generated region and the demo's arena, so they belong to the run-start fixture.
public sealed partial class RunStartTests
{
private static bool IsCrewQuartersUnit(EquippedItem unit) => unit.Behaviors.OfType<Habitation>().Any();
[Fact]
public void EveryStationCarriesCrewQuarters()
{
var stations = 0;
foreach (var seed in RegionSeeds)
{
var (galaxy, _, _) = RegionRun(seed);
foreach (var station in galaxy.Zones.SelectMany(zone => zone.Contents.Entities.OfType<OrbitalEntity>())
.Where(entity => (_items.GetData(entity.Hull) as HullData)?.HullType == HullType.Station))
{
stations++;
Assert.True(station.Equipment.Count(IsCrewQuartersUnit) == 1, $"seed {seed}: {station.Name} carries {station.Equipment.Count(IsCrewQuartersUnit)} crew quarters");
}
}
Assert.True(stations > 0, "a region with no station proves nothing");
}
[Fact]
public void StationBuysWhatItsCrewEats()
{
var (_, arena, _, failures) = Launch(new DemoTerminus(), Inputs(() => 1));
Assert.True(failures.Count == 0, string.Join("; ", failures));
var zenith = arena.Entities.OfType<OrbitalEntity>().Single(entity => entity.Kind.Key.Equals(StationKindData.KeyOf("trade-hub")));
CultRecordKey Good(string name) => _cache.RefOf<ItemData>(_cache.GetByName<SimpleCommodityData>(name)).Key;
var eaten = new[] { Good("Water"), Good("Oxygen"), Good("Algae") };
Assert.All(eaten, good => Assert.Contains(good, StationWants.Of(zenith)));
var quarters = zenith.Equipment.Single(IsCrewQuartersUnit);
Assert.NotNull(zenith.TryUnequip(quarters));
Assert.All(eaten, good => Assert.DoesNotContain(good, StationWants.Of(zenith)));
}
}