From 02651fa67b6a023dbbe51f76d110498dcb14b881 Mon Sep 17 00:00:00 2001 From: soul Date: Sat, 28 Nov 2026 23:22:02 +0100 Subject: [PATCH] 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 --- .../Scripts/ServerShared/LoadoutGenerator.cs | 34 ++++++++++++++-- Assets/Scripts/ServerShared/StationWants.cs | 9 ++++- tests/Aetheria.Shared.Tests/DemoLoopTests.cs | 5 ++- .../Aetheria.Shared.Tests/HabitationTests.cs | 39 +++++++++++++++++++ 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/ServerShared/LoadoutGenerator.cs b/Assets/Scripts/ServerShared/LoadoutGenerator.cs index be77cadf5..eaa15b737 100644 --- a/Assets/Scripts/ServerShared/LoadoutGenerator.cs +++ b/Assets/Scripts/ServerShared/LoadoutGenerator.cs @@ -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().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 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() - .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() + .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; diff --git a/Assets/Scripts/ServerShared/StationWants.cs b/Assets/Scripts/ServerShared/StationWants.cs index e7743b3c1..3d1b1a53a 100644 --- a/Assets/Scripts/ServerShared/StationWants.cs +++ b/Assets/Scripts/ServerShared/StationWants.cs @@ -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()) + foreach (var row in habitation.Table.Where(row => row.Commodity.IsSet())) + wants.Add(row.Commodity.Key); return wants; } diff --git a/tests/Aetheria.Shared.Tests/DemoLoopTests.cs b/tests/Aetheria.Shared.Tests/DemoLoopTests.cs index 4df17442b..f70f6b074 100644 --- a/tests/Aetheria.Shared.Tests/DemoLoopTests.cs +++ b/tests/Aetheria.Shared.Tests/DemoLoopTests.cs @@ -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()).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(good))))); var silo = zenith.CargoBays.Single(bay => bay.Data is SiloData); diff --git a/tests/Aetheria.Shared.Tests/HabitationTests.cs b/tests/Aetheria.Shared.Tests/HabitationTests.cs index ef054fa93..a3da35ea4 100644 --- a/tests/Aetheria.Shared.Tests/HabitationTests.cs +++ b/tests/Aetheria.Shared.Tests/HabitationTests.cs @@ -250,3 +250,42 @@ public sealed class HabitationTests : IDisposable Assert.Null(MessagePackSerializer.Deserialize(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().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()) + .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().Single(entity => entity.Kind.Key.Equals(StationKindData.KeyOf("trade-hub"))); + CultRecordKey Good(string name) => _cache.RefOf(_cache.GetByName(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))); + } +}