diff --git a/Assets/Scripts/ServerShared/Behaviors/Habitation.cs b/Assets/Scripts/ServerShared/Behaviors/Habitation.cs index 3e2c56e56..d184e2858 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Habitation.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Habitation.cs @@ -73,10 +73,12 @@ public static class Upkeep for (var at = 0; at < owed.Length; at++) { var row = data.Table[at]; + // A row that names no commodity consumes nothing and owes nothing: its Owed stays zero, so Owed is a fraction below one always. + if (!row.Commodity.IsSet()) continue; var was = state?.Owed != null && at < state.Owed.Length ? state.Owed[at] : 0; owed[at] = was + size * row.PerOccupantHour * seconds / 3600; var whole = (int) Math.Floor(owed[at]); - if (whole <= 0 || !row.Commodity.IsSet()) continue; + if (whole <= 0) continue; var key = row.Commodity.Key; var taken = whole; if (!hold.TryTake(new[] { (key, whole) })) diff --git a/Assets/Scripts/ServerShared/LoadoutGenerator.cs b/Assets/Scripts/ServerShared/LoadoutGenerator.cs index eaa15b737..4f225f32c 100644 --- a/Assets/Scripts/ServerShared/LoadoutGenerator.cs +++ b/Assets/Scripts/ServerShared/LoadoutGenerator.cs @@ -137,21 +137,6 @@ 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, @@ -176,6 +161,8 @@ public class LoadoutGenerator if (cargo.TryStore(instance)) stocked += cells; } + EquipCrewQuarters(entity, kind); + entity.CanTow = hullData.CanTow; return EntitySerializer.Pack(entity) as OrbitalEntityPack; @@ -205,10 +192,6 @@ 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) { @@ -396,29 +379,44 @@ 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 kind) + // Installs the station kind's Facilities, largest shape first then by name (StationKinds.InstalledDesigns' order, which + // FacilityRoomFaults counts rows in), 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 kind) => + Install(entity, kind, StationKinds.InstalledDesigns(ItemManager.ItemData, ItemManager.ItemData.Get(kind)).Where(design => !StationKinds.IsCrewQuarters(design))); + + // Installs the Crew Quarters on the station's last Facility row, after every other draw of the loadout: making the unit takes a lot from + // the item stream, and made last it moves no draw the station's own generation makes (the cargo bay it draws, the gear it stocks). + // + // CONJURE (ruling economy-is-never-faked): every station is handed a Crew Quarters here from a not-offered product that no facility + // makes, as it is handed its kind's facilities. This is interim scaffolding, named so it is deleted and not forgotten: follow-up + // conjuring-site-deletions removes it once habitat gear works (a gear-works facility making Crew Quarters) exist to supply the + // station's quarters, and the installation then comes from that production. + private void EquipCrewQuarters(Entity entity, CultRecordRef kind) + { + var quarters = StationKinds.InstalledDesigns(ItemManager.ItemData, ItemManager.ItemData.Get(kind)).Where(StationKinds.IsCrewQuarters).ToArray(); + if (quarters.Length == 0) throw new InvalidLoadoutException("No crew quarters design in the catalog for station!"); + Install(entity, kind, quarters); + } + + private void Install(Entity entity, CultRecordRef kind, IEnumerable designs) { var kindData = ItemManager.ItemData.Get(kind); - if (kindData == null) return; - var designs = kindData.Facilities.Select(facility => ItemManager.ItemData.Get(facility)).OfType() - .OrderByDescending(design => design.Shape.Coordinates.Length).ThenBy(design => design.Name, StringComparer.Ordinal); + var hullName = ItemManager.GetData(entity.Hull).Name; foreach (var design in designs) { - 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}."); + 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}'."); + throw new InvalidLoadoutException($"Station hull '{hullName}' has no free Facility row that takes {design.Name}, a facility of station kind '{kindData?.Name}'."); } } // 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) + private FactionProductData InstalledProduct(EquippableItemData design) { var key = ItemManager.ItemData.RefOf(design).Key; var products = ItemManager.ItemData.GetAll() diff --git a/Assets/Scripts/ServerShared/StationKinds.cs b/Assets/Scripts/ServerShared/StationKinds.cs index 38f68302a..0d8976b34 100644 --- a/Assets/Scripts/ServerShared/StationKinds.cs +++ b/Assets/Scripts/ServerShared/StationKinds.cs @@ -109,6 +109,23 @@ public static class StationKinds public static bool IsRecipeFacility(ItemData item) => item is GearData gear && gear.HardpointType == HardpointType.Facility && gear.Behaviors != null && gear.Behaviors.Any(behavior => behavior is FacilityData); + // Crew Quarters: the station plant that houses a station's crew, Facility gear holding HabitationData. Every station carries one, on a + // Facility row of its own (ruling station-facilities-on-typed-mounts), so the row count of a hull is its kind's facilities plus one. + public static bool IsCrewQuarters(ItemData item) => + item is GearData gear && gear.HardpointType == HardpointType.Facility && gear.Behaviors != null && gear.Behaviors.Any(behavior => behavior is HabitationData); + + // What a station of the kind installs on its Facility rows, in the order rows are counted and seated: the kind's facilities, largest + // shape first then by name, then the one Crew Quarters (the first by name). The one owner of that list, read by FacilityRoomFaults + // and LoadoutGenerator.EquipFacilities. A null kind has only the quarters. + public static EquippableItemData[] InstalledDesigns(CultCache catalog, StationKindData kind) + { + var designs = (kind?.Facilities ?? new List>()).Select(facility => catalog.Get(facility)).OfType() + .OrderByDescending(design => design.Shape.Coordinates.Length).ThenBy(design => design.Name, StringComparer.Ordinal).ToList(); + var quarters = catalog.GetAll().Where(IsCrewQuarters).OrderBy(design => design.Name, StringComparer.Ordinal).FirstOrDefault(); + if (quarters != null) designs.Add(quarters); + return designs.ToArray(); + } + // Every offered station hull takes the facilities of every kind it serves, one station at a time (a station is one kind, so the // designs of one kind each take a distinct Facility row; two kinds may share a row). A hull of kind H serves H itself and every // kind some faction places whose hull kind is H (HullKindFor). One line per design that finds no row, naming the hull, the kind and @@ -133,9 +150,7 @@ public static class StationKinds var kind = catalog.Get(new CultRecordKey(kindKey)); if (kind == null) continue; var rows = hull.Hardpoints.Where(hardpoint => hardpoint.Type == HardpointType.Facility).ToList(); - var designs = kind.Facilities.Select(facility => catalog.Get(facility)).OfType() - .OrderByDescending(design => design.Shape.Coordinates.Length).ThenBy(design => design.Name, StringComparer.Ordinal); - foreach (var design in designs) + foreach (var design in InstalledDesigns(catalog, kind)) { var row = rows.FirstOrDefault(hardpoint => hardpoint.Takes(design)); if (row != null) rows.Remove(row); diff --git a/tools/AetherDb/Economy/CrewQuarters.cs b/tools/AetherDb/Economy/CrewQuarters.cs index 4b7149317..5a75e97e1 100644 --- a/tools/AetherDb/Economy/CrewQuarters.cs +++ b/tools/AetherDb/Economy/CrewQuarters.cs @@ -6,10 +6,13 @@ using GameCult.Caching.MessagePack; using MessagePack; // Economy content: the Crew Quarters every station carries (aetheria-living-world, cut habitation-gear; rulings -// installed-gear-decides-what-a-station-buys, mass-populations-live-in-blooms). A 2x2 interior GearData on a Tool mount holding -// HabitationData: twenty occupants who eat Water, Oxygen and Algae, all basic, sold by a NotOffered product branded as the facilities -// are (a design no product sells is an unsold design; a not-offered product is on no market, and stations install it through -// LoadoutGenerator.InstalledProduct). The commodities are looked up by name, never created. The rates are first drafts, sim-tuned +// installed-gear-decides-what-a-station-buys, mass-populations-live-in-blooms, station-facilities-on-typed-mounts). A 2x2 GearData on a +// Facility mount holding HabitationData: station plant, seated on a Facility row of every station hull beside its kind's facilities. +// Twenty occupants who eat Water, Oxygen and Algae, all basic, sold by a NotOffered product branded as the facilities are (a design no +// product sells is an unsold design; a not-offered product is on no market, and stations install it through +// LoadoutGenerator.EquipFacilities). CONJURE (ruling economy-is-never-faked): no facility makes this design, so every station is handed +// it from nothing, interim scaffolding that follow-up conjuring-site-deletions deletes once habitat gear works (a gear-works facility +// making Crew Quarters) supply a station's quarters. The commodities are looked up by name, never created. The rates are first drafts, sim-tuned // knobs (ruling economy-numbers-are-sim-tuned-knobs), as are the thermal band (shared with Facilities) and the price. Writes whole // values, only where they differ, so a rerun touches nothing. public sealed class CrewQuarters : EconomyBatch @@ -31,7 +34,7 @@ public sealed class CrewQuarters : EconomyBatch var shape = new Shape(2, 2); foreach (var cell in shape.AllCoordinates) shape[cell] = true; // A stored behavior carries a cache-minted element id, so a design is compared by its fields, never by bytes. - if (!(catalog.Get(designKey) is GearData held && held.Name == Name && held.Description == Description && held.Hardpoint == HardpointType.Tool + if (!(catalog.Get(designKey) is GearData held && held.Name == Name && held.Description == Description && held.Hardpoint == HardpointType.Facility && held.Durability == 100 && held.Mass == 250 && held.Price == 1000 && held.Shape.Width == 2 && held.Shape.Height == 2 && held.Shape.Coordinates.Length == 4 && held.Behaviors.Count == 1 && held.Behaviors[0] is HabitationData habitation && habitation.Capacity == Capacity && habitation.Table.Count == rows.Length && habitation.Table.Zip(rows, (have, want) => have.Commodity.Key.Equals(want.Key) && have.PerOccupantHour == want.PerOccupantHour && have.Basic).All(same => same) @@ -39,7 +42,7 @@ public sealed class CrewQuarters : EconomyBatch && held.OptimalTemperature == Facilities.ThermalOptimal && held.PlateauWidth == Facilities.ThermalPlateau)) author.Put(new GearData { - Name = Name, Description = Description, Hardpoint = HardpointType.Tool, Shape = shape, + Name = Name, Description = Description, Hardpoint = HardpointType.Facility, Shape = shape, Durability = 100, Mass = 250, Price = 1000, Behaviors = {