HabitationData: a population is installed gear; Upkeep.Advance consumes by the module's table, Habitants lives on the unit (cut habitation-gear)

Union arm 54, EquippableItem key 15. AdvanceInstalled runs Facility and Habitation behaviours.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-28 23:16:45 +01:00
commit b4d4e94333
7 changed files with 390 additions and 5 deletions

View file

@ -196,6 +196,7 @@ public interface IPowerConsumer
Union(51, typeof(ScannerData)),
Union(52, typeof(LockWeaponData)),
Union(53, typeof(FacilityData)),
Union(54, typeof(HabitationData)),
JsonObject(MemberSerialization.OptIn)]
public abstract class BehaviorData
{

View file

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GameCult.Caching;
using MessagePack;
using Newtonsoft.Json;
// The habitation gear a station carries to house a population: a GearData whose Behaviors hold one of these. The design says how many
// it houses and what each occupant consumes, so a population is installed gear like a facility is (rulings
// installed-gear-decides-what-a-station-buys, mass-populations-live-in-blooms): there is no population record and no needs table
// outside the module design. A station's Crew Quarters and a habitat's Housing Districts are designs of it. Installed gear, never a
// consumable's effect.
[Inspectable, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class HabitationData : BehaviorData
{
// The occupants the unit houses when full: what a unit with no recorded population holds, and, until population dynamics moves
// it, what it holds always.
[Inspectable, JsonProperty("capacity"), Key(1)]
public int Capacity;
// What each occupant consumes: one row per commodity.
[Inspectable, JsonProperty("table"), Key(2)]
public List<HabitationRow> Table = new List<HabitationRow>();
public override Behavior CreateInstance(EquippedItem item)
{
return new Habitation(this, item);
}
public override Behavior CreateInstance(ConsumableItemEffect item)
{
throw new InvalidOperationException($"{item.Data.Name} cannot carry a habitation module: a habitation module is installed gear, never a consumable effect.");
}
}
// One commodity a population eats: units per occupant per sim hour, and whether the occupants cannot do without it (Basic) or it is
// a lifestyle good. Basic is recorded for the follow-up that makes a shortfall matter; this cut reads it nowhere.
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class HabitationRow
{
[JsonProperty("commodity"), Key(0)] public CultRecordRef<ItemData> Commodity;
[JsonProperty("perOccupantHour"), Key(1)] public float PerOccupantHour;
[JsonProperty("basic"), Key(2)] public bool Basic;
// Element identity: minted by the cache on write; "" means unset.
[Key(3)] [CultElementId] public string Id = "";
}
// The population an installed habitation unit houses, on the unit (EquippableItem.Habitants): its size and, per table row, the
// fraction of a unit its occupants have consumed and not yet taken. Written only by Habitation.Execute from Upkeep.Advance's result.
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class Habitants
{
[JsonProperty("size"), Key(0)] public double Size;
// By table row. Always a fraction below one between steps: whole units are taken, or dropped when the hold lacks them.
[JsonProperty("owed"), Key(1)] public double[] Owed = Array.Empty<double>();
}
// The one rule for what a population consumes: the occupants of a unit accrue Size x rate x seconds / 3600 of each row, the hold is
// asked for the whole units, and a unit the hold cannot give is dropped, never carried as a debt (idea planets-blooms-map.rejected-carry-unmet-debt).
// On screen its one caller is Habitation.Execute with one zone step; off screen RegionClock.AdvanceInstalled calls that with the span
// the zone missed. It reads no random, no clock and no crew.
public static class Upkeep
{
// The population after `seconds`, and the rows of the table that went short (a row whose hold gave fewer whole units than the
// occupants owed). A null state is a unit that has not stepped yet: full capacity, nothing owed. Pure over its arguments but for the
// hold it takes from.
public static (Habitants state, IReadOnlyList<CultRecordKey> shortfall) Advance(HabitationData data, Habitants state, double seconds, IProductionHold hold)
{
var size = state?.Size ?? data.Capacity;
if (!(seconds > 0)) seconds = 0;
var owed = new double[data.Table.Count];
var shortfall = new List<CultRecordKey>();
for (var at = 0; at < owed.Length; at++)
{
var row = data.Table[at];
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;
var key = row.Commodity.Key;
var taken = whole;
if (!hold.TryTake(new[] { (key, whole) }))
{
taken = Math.Min(whole, hold.Held(key));
if (taken > 0 && !hold.TryTake(new[] { (key, taken) })) taken = 0;
shortfall.Add(key);
}
owed[at] -= whole;
}
return (new Habitants { Size = size, Owed = owed }, shortfall);
}
}
// The installed habitation unit. It ticks once per zone step, from EquippedItem.Update while the item is active, on screen; off screen
// RegionClock.AdvanceInstalled calls Execute with the span the zone missed. It takes from the same hold a facility does.
public class Habitation : Behavior
{
private readonly HabitationData _data;
private EntityHold _hold;
// The commodities the last step could not fully take: derived by Execute every step, never stored on the unit.
public IReadOnlyList<CultRecordKey> Shortfall { get; private set; } = Array.Empty<CultRecordKey>();
public Habitation(HabitationData data, EquippedItem item) : base(data, item)
{
_data = data;
}
public override bool Execute(float dt)
{
_hold ??= new EntityHold(Entity);
var (state, shortfall) = Upkeep.Advance(_data, Item.EquippableItem.Habitants, dt, _hold);
Item.EquippableItem.Habitants = state;
Shortfall = shortfall;
return true;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4066805b976ae8b493827f0cefb1a1d0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: