Station and belt kinds are seeded: the pick hashes the zone seed and the place in generation order, not a minted key

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-10-23 01:21:02 +02:00
commit 37a8110907
6 changed files with 54 additions and 40 deletions

View file

@ -52,10 +52,11 @@ public static class StationKinds
return Ordered(kinds);
}
// A station's kind: a stable pick among the faction's kinds by the station's site key. It draws nothing from any random
// A station's kind: a stable pick among the faction's kinds by the station's site, which generation names by the zone's
// seed and the station's place in its orbit order (never by a key the cache mints). It draws nothing from any random
// stream, so adding the kind to generation moves no other roll. A faction with no kinds falls back to any kind some
// offered station hull carries, logged, and the hull draw then falls back to any maker. Unset when the catalog has none.
public static CultRecordRef<StationKindData> Choose(CultCache catalog, Faction faction, CultRecordKey siteKey, Action<string> log = null)
public static CultRecordRef<StationKindData> Choose(CultCache catalog, Faction faction, string site, Action<string> log = null)
{
var pool = KindsOf(catalog, faction);
if (pool.Length == 0)
@ -68,7 +69,7 @@ public static class StationKinds
if (pool.Length == 0) return default;
log?.Invoke($"{faction?.Name ?? "This galaxy"} builds no station kind; placing a station of any kind some maker offers.");
}
return pool[(int) (siteKey.Value.StableHash() % (uint) pool.Length)];
return pool[(int) (site.StableHash() % (uint) pool.Length)];
}
// The kind of hull a faction builds for a station kind: the kind itself when the faction offers a model of it (a model
@ -82,22 +83,34 @@ public static class StationKinds
return standIn != null && standIn.Kind.IsSet() ? standIn.Kind : kind;
}
// What is wrong with a hull's kind, or null: a Station hull names a kind the catalog holds, and no other hull names one.
// The one rule, applied to the catalog on Open and to each mod package on Compose.
public static string HullKindFault(CultCache catalog, HullData hull)
{
if (hull.HullType == HullType.Station)
{
if (!hull.Kind.IsSet())
return $"Station hull '{hull.Name}' has no Kind; a station hull names the station kind it is built to be.";
if (catalog.Get(hull.Kind) == null)
return $"Station hull '{hull.Name}' Kind names no station kind this catalog holds.";
}
else if (hull.Kind.IsSet())
return $"Hull '{hull.Name}' is a {hull.HullType} and names a Kind; only a station hull has one.";
return null;
}
// Open refuses a malformed kind catalog, naming the record and field. Every Station hull names a kind the catalog holds
// and no other hull names one; every stand-in names a faction and a kind the catalog holds, is never its own kind, and
// does not stand on another stand-in for the same faction (one hop).
public static void Validate(CultCache catalog)
// does not stand on another stand-in for the same faction (one hop). A caller that gives kindlessStation is the owner
// of the catalog being repaired: a Station hull with no Kind is reported to it by name instead of refused.
public static void Validate(CultCache catalog, Action<string> kindlessStation = null)
{
foreach (var hull in catalog.GetAll<HullData>())
{
if (hull.HullType == HullType.Station)
{
if (!hull.Kind.IsSet())
throw new InvalidOperationException($"Station hull '{hull.Name}' has no Kind; a station hull names the station kind it is built to be.");
if (catalog.Get(hull.Kind) == null)
throw new InvalidOperationException($"Station hull '{hull.Name}' Kind names no station kind this catalog holds.");
}
else if (hull.Kind.IsSet())
throw new InvalidOperationException($"Hull '{hull.Name}' is a {hull.HullType} and names a Kind; only a station hull has one.");
var fault = HullKindFault(catalog, hull);
if (fault == null) continue;
if (kindlessStation != null && hull.HullType == HullType.Station && !hull.Kind.IsSet()) kindlessStation(fault);
else throw new InvalidOperationException(fault);
}
foreach (var kind in catalog.GetAll<StationKindData>())
{

View file

@ -196,7 +196,7 @@ public class Zone
case AsteroidBeltData belt:
// Mining Cut 3 (Q16 B): a belt from before field kinds existed gets its kind here, once, by
// the rule generation uses, and keeps it.
FieldKinds.Ensure(body.Key, belt, cache);
FieldKinds.Ensure(body.Key.Value, belt, cache);
AsteroidBelts[body.Key] = new AsteroidBelt(belt);
break;
case SunData sun:

View file

@ -134,10 +134,11 @@ public class FieldKindData
// Mining Cut 3 (Q16 B): the one owner of which kind a belt is.
public static class FieldKinds
{
// A weighted pick among the catalog's kinds, by a stable hash of the belt's key: the same belt and the same
// catalog always give the same kind. Kinds are ordered by key so the pick does not depend on store order.
// A weighted pick among the catalog's kinds, by a stable hash of the belt's site: the same site and the same
// catalog always give the same kind. Generation names a belt by the zone's seed and the belt's place in generation
// order (never by the key the cache mints, which differs every run); a belt saved without a kind is named by its key. Kinds are ordered by key so the pick does not depend on store order.
// Unset when the catalog has no kind with positive weight.
public static CultRecordRef<FieldKindData> Assign(CultRecordKey field, CultCache catalog)
public static CultRecordRef<FieldKindData> Assign(string site, CultCache catalog)
{
var kinds = catalog.GetAll<FieldKindData>()
.Where(kind => kind.GenerationWeight > 0f)
@ -146,7 +147,7 @@ public static class FieldKinds
.ToArray();
if (kinds.Length == 0) return default;
var pick = field.Value.StableHash() / 4294967296.0 * kinds.Sum(kind => (double) kind.Weight);
var pick = site.StableHash() / 4294967296.0 * kinds.Sum(kind => (double) kind.Weight);
foreach (var kind in kinds)
{
if (pick < kind.Weight) return new CultRecordRef<FieldKindData>(kind.Key);
@ -157,10 +158,10 @@ public static class FieldKinds
// Gives a belt with no kind its kind and saves it; a belt that has one keeps it, whatever the catalog now says.
// Generation (ZoneGenerator) and first load (Zone) both call this, so there is one assignment rule.
public static void Ensure(CultRecordKey field, AsteroidBeltData belt, CultCache cache)
public static void Ensure(string site, AsteroidBeltData belt, CultCache cache)
{
if (belt.Kind.IsSet()) return;
belt.Kind = Assign(field, cache);
belt.Kind = Assign(site, cache);
if (belt.Kind.IsSet()) cache.Upsert(belt);
}
}

View file

@ -119,7 +119,7 @@ public static class ZoneGenerator
pack.Orbits = planets.Select(planet => cache.RefOf(orbitMap[planet])).ToList();
var beltOf = new Dictionary<GeneratorPlanet, AsteroidBeltData>();
pack.Planets = planets.Where(p=>!p.Empty).Select(planet =>
pack.Planets = planets.Where(p=>!p.Empty).Select((planet, index) =>
{
BodyType bodyType = planet.Belt ? BodyType.Asteroid :
planet.Mass > zoneSettings.SunMass ? BodyType.Sun :
@ -213,7 +213,7 @@ public static class ZoneGenerator
var bodyKey = cache.Upsert(planetData).Key;
planetData.Name = bodyKey.Value.Substring(0, 8);
// Mining Cut 3 (Q16 B): a belt's field kind is chosen here, at generation, and saved with it.
if (planetData is AsteroidBeltData generatedBelt) FieldKinds.Ensure(bodyKey, generatedBelt, cache);
if (planetData is AsteroidBeltData generatedBelt) FieldKinds.Ensure($"{seed}:belt:{index}", generatedBelt, cache);
return cache.Upsert(planetData);
}).ToList();
@ -239,7 +239,7 @@ public static class ZoneGenerator
BodyData swarmBody = swarm;
var swarmKey = cache.Upsert(swarmBody).Key;
swarm.Name = swarmKey.Value.Substring(0, 8);
FieldKinds.Ensure(swarmKey, swarm, cache);
FieldKinds.Ensure($"{seed}:swarm:{swarmHosts.Count}", swarm, cache);
pack.Planets.Add(cache.Upsert(swarmBody));
}
@ -268,7 +268,7 @@ public static class ZoneGenerator
BodyData lanedBody = laned;
var lanedKey = cache.Upsert(lanedBody).Key;
laned.Name = lanedKey.Value.Substring(0, 8);
FieldKinds.Ensure(lanedKey, laned, cache);
FieldKinds.Ensure($"{seed}:lane:{shaped.Count}", laned, cache);
pack.Planets.Add(cache.Upsert(lanedBody));
}
@ -420,7 +420,7 @@ public static class ZoneGenerator
var orbit = selectedStationOrbits[i];
var lagrangeOrbit = CreateLagrangeOrbit(orbit);
var station = GetLoadoutGenerator(story.Faction).GenerateStationLoadout(
StationKinds.Choose(cache, story.Faction, cache.RefOf(lagrangeOrbit).Key, itemManager.Log));
StationKinds.Choose(cache, story.Faction, $"{seed}:station:{i}", itemManager.Log));
if (station == null)
{
itemManager.Log($"Story station for {story.Faction?.Name} could not be generated; its story has nowhere to live!");
@ -442,7 +442,7 @@ public static class ZoneGenerator
var lagrangeOrbit = CreateLagrangeOrbit(orbit);
var station = GetLoadoutGenerator(nearestFaction).GenerateStationLoadout(
StationKinds.Choose(cache, nearestFaction, cache.RefOf(lagrangeOrbit).Key, itemManager.Log));
StationKinds.Choose(cache, nearestFaction, $"{seed}:station:{i}", itemManager.Log));
if (station == null)
{
if (isTutorialEntrance)