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:
parent
17e2da0092
commit
37a8110907
6 changed files with 54 additions and 40 deletions
|
|
@ -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>())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -638,7 +638,7 @@ public sealed partial class MiningCut3Tests : IDisposable
|
|||
var counts = new Dictionary<string, int> { ["Light"] = 0, ["Never"] = 0, ["Middle"] = 0, ["Heavy"] = 0 };
|
||||
for (var i = 0; i < 2000; i++)
|
||||
{
|
||||
var key = new CultRecordKey($"belt-{i}");
|
||||
var key = $"belt-{i}";
|
||||
var picked = FieldKinds.Assign(key, s.Cache);
|
||||
Assert.True(picked.Key.Equals(FieldKinds.Assign(key, s.Cache).Key));
|
||||
counts[s.Cache.Get(picked).Name]++;
|
||||
|
|
@ -649,7 +649,7 @@ public sealed partial class MiningCut3Tests : IDisposable
|
|||
Assert.InRange(counts["Heavy"] / 2000f, 3f / 6 - .04f, 3f / 6 + .04f);
|
||||
|
||||
var empty = BuildScene(new[] { never });
|
||||
Assert.False(FieldKinds.Assign(new CultRecordKey("belt-0"), empty.Cache).IsSet());
|
||||
Assert.False(FieldKinds.Assign("belt-0", empty.Cache).IsSet());
|
||||
}
|
||||
|
||||
// Q16 B, first load: a belt saved before field kinds existed gets its kind when a zone first loads it, by the
|
||||
|
|
@ -664,7 +664,7 @@ public sealed partial class MiningCut3Tests : IDisposable
|
|||
var beltKey = s.Belts[0];
|
||||
var belt = (AsteroidBeltData) s.Zone.Planets[beltKey];
|
||||
Assert.True(belt.Kind.IsSet());
|
||||
Assert.True(belt.Kind.Key.Equals(FieldKinds.Assign(beltKey, s.Cache).Key));
|
||||
Assert.True(belt.Kind.Key.Equals(FieldKinds.Assign(beltKey.Value, s.Cache).Key));
|
||||
var assigned = belt.Kind.Key;
|
||||
|
||||
// Saved: a fresh open of the same stores reads the kind back.
|
||||
|
|
@ -678,7 +678,7 @@ public sealed partial class MiningCut3Tests : IDisposable
|
|||
|
||||
// Kept: a new, overwhelmingly weighted kind now wins the pick for this belt, yet loading it again leaves it.
|
||||
reopened.Upsert(Kind("Overwhelming", 9f, 1e6f));
|
||||
Assert.False(FieldKinds.Assign(beltKey, reopened).Key.Equals(assigned));
|
||||
Assert.False(FieldKinds.Assign(beltKey.Value, reopened).Key.Equals(assigned));
|
||||
var items = new ItemManager(1u, reopened, new ProvenanceLedger(), GameSettings(), _ => { });
|
||||
var reloaded = new Zone(items, PlanetSettings(), s.Pack, new GalaxyZone { Name = "MiningCut3Reload", Owner = null }, null);
|
||||
Assert.True(((AsteroidBeltData) reloaded.Planets[beltKey]).Kind.Key.Equals(assigned));
|
||||
|
|
@ -786,11 +786,11 @@ public sealed partial class MiningCut3Tests : IDisposable
|
|||
return cache;
|
||||
}
|
||||
|
||||
// Q16 B, generation: every belt ZoneGenerator makes carries its kind already, chosen by FieldKinds.Assign on
|
||||
// the belt's own key. Runs against a scratch copy of the shipped catalog with three kinds of different
|
||||
// Q16 B, generation: every belt ZoneGenerator makes carries its kind already, chosen by FieldKinds.Assign. Runs against
|
||||
// a scratch copy of the shipped catalog with three kinds of different
|
||||
// weights, so a pick by any other rule would disagree on some belt.
|
||||
[Fact]
|
||||
public void GenerationChoosesTheKindByTheSameFunction()
|
||||
public void GenerationAssignsEveryBeltItsKind()
|
||||
{
|
||||
var shipped = TestCatalog.Repo;
|
||||
var belts = 0;
|
||||
|
|
@ -826,7 +826,7 @@ public sealed partial class MiningCut3Tests : IDisposable
|
|||
if (!(cache.Get(body) is AsteroidBeltData belt)) continue;
|
||||
belts++;
|
||||
Assert.True(belt.Kind.IsSet());
|
||||
Assert.True(belt.Kind.Key.Equals(FieldKinds.Assign(body.Key, cache).Key));
|
||||
Assert.StartsWith("Gen ", cache.Get(belt.Kind).Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -863,7 +863,7 @@ public sealed partial class MiningCut3Tests : IDisposable
|
|||
var items = new ItemManager(1u, cache, new ProvenanceLedger(), GameSettings(), _ => { });
|
||||
return galaxy.Zones.Take(6).SelectMany(zone =>
|
||||
ZoneGenerator.GenerateZone(items, RestoredHullsTests.TutorialZoneSettings(), RestoredHullsTests.AuthoredPlanetSettings(), galaxy, zone, isTutorial: zone == galaxy.Entrance)
|
||||
.Planets.Select(body => cache.Get(body)).OfType<AsteroidBeltData>().Select(belt => belt.Kind.Key.Value))
|
||||
.Planets.Select(body => cache.Get(body)).OfType<AsteroidBeltData>().Select(belt => cache.Get(belt.Kind).Name))
|
||||
.ToArray();
|
||||
}
|
||||
for (uint seed = 1; seed <= 25; seed++)
|
||||
|
|
|
|||
|
|
@ -224,7 +224,7 @@ public sealed class StationKindTests : IDisposable
|
|||
var seen = new HashSet<string>();
|
||||
for (var site = 0; site < 40; site++)
|
||||
{
|
||||
var kind = StationKinds.Choose(world.Cache, world.Au, new CultRecordKey("site-" + site));
|
||||
var kind = StationKinds.Choose(world.Cache, world.Au, "site-" + site);
|
||||
Assert.Contains(kind.Key.Value, kinds);
|
||||
seen.Add(kind.Key.Value);
|
||||
var pack = world.Generator(world.Au, (uint) site + 1).GenerateStationLoadout(kind);
|
||||
|
|
@ -281,12 +281,12 @@ public sealed class StationKindTests : IDisposable
|
|||
Assert.Empty(StationKinds.KindsOf(world.Cache, world.Lucent));
|
||||
|
||||
var logged = new List<string>();
|
||||
var kind = StationKinds.Choose(world.Cache, world.Lucent, new CultRecordKey("site"), logged.Add);
|
||||
var kind = StationKinds.Choose(world.Cache, world.Lucent, "site", logged.Add);
|
||||
Assert.Equal(Kind("shipyard").Key, kind.Key);
|
||||
Assert.Single(logged);
|
||||
Assert.Contains(world.Lucent.Name, logged[0]);
|
||||
var anonymous = new List<string>();
|
||||
StationKinds.Choose(world.Cache, null, new CultRecordKey("site"), anonymous.Add);
|
||||
StationKinds.Choose(world.Cache, null, "site", anonymous.Add);
|
||||
Assert.Contains("This galaxy", Assert.Single(anonymous));
|
||||
world.StandIn("mining", world.Au, "shipyard");
|
||||
Assert.Equal(Kind("mining").Key, StationKinds.HullKindFor(world.Cache, null, Kind("mining")).Key);
|
||||
|
|
@ -304,7 +304,7 @@ public sealed class StationKindTests : IDisposable
|
|||
world.Sell(world.Au, "mining");
|
||||
world.Sell(world.Au, "shipyard");
|
||||
world.Sell(world.Au, "depot");
|
||||
var keys = Enumerable.Range(0, 30).Select(i => new CultRecordKey("orbit-" + i)).ToArray();
|
||||
var keys = Enumerable.Range(0, 30).Select(i => "orbit-" + i).ToArray();
|
||||
var first = keys.Select(key => StationKinds.Choose(world.Cache, world.Au, key).Key.Value).ToArray();
|
||||
Assert.Equal(first, keys.Reverse().Select(key => StationKinds.Choose(world.Cache, world.Au, key).Key.Value).Reverse().ToArray());
|
||||
Assert.True(first.Distinct().Count() > 1, "the stable hash spreads sites over the faction's kinds");
|
||||
|
|
|
|||
Loading…
Reference in a new issue