using System; using System.Collections.Generic; using System.IO; using System.Linq; using CultMath; using GameCult.Caching; using GameCult.Caching.MessagePack; using MessagePack; using Xunit; using Random = CultMath.Random; // Test-side handle on the shipped catalog's trade-hub kind, the one Zenith carries (ruling zenith-is-a-trade-hub). internal static class TestStationKinds { public static readonly CultRecordRef TradeHub = new CultRecordRef(StationKindData.KeyOf("trade-hub")); } // Station kinds (aetheria-living-world, cut station-kind-datum): the one list of kinds in the catalog, a hull's kind, a // placed station's kind, and the stand-in rule for a faction with no model of a kind. Worlds here are the composed shipped // catalog copied to a temp directory, with Zenith taken off the market so the only station hulls are the ones a test adds. public sealed class StationKindTests : IDisposable { private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-stationkinds-" + Guid.NewGuid().ToString("N")); private readonly List _caches = new List(); private int _worlds; public StationKindTests() => Directory.CreateDirectory(_root); public void Dispose() { foreach (var cache in _caches) cache.Dispose(); Directory.Delete(_root, true); } private static CultRecordRef Kind(string id) => new CultRecordRef(StationKindData.KeyOf(id)); private sealed class World { public CultCache Cache; public ItemManager Items; public List Log = new List(); public Faction Zh, Au, Lucent; public HullData Zenith; private FactionProductData _zenithProduct; private int _added; public static World Make(StationKindTests host, bool zenithOffered) { var dir = Path.Combine(host._root, "world" + host._worlds++); Directory.CreateDirectory(dir); File.Copy(TestCatalog.Repo, Path.Combine(dir, "Aetheria.cc")); var cache = new CultCache(TestCatalog.Registry()); host._caches.Add(cache); cache.AddBackingStore(new SingleFileMessagePackBackingStore(Path.Combine(dir, "Aetheria.cc")), AetheriaStores.CatalogTypes); cache.AddBackingStore(new SingleFileMessagePackBackingStore(Path.Combine(dir, "run.cc")), AetheriaStores.RunTypes); var world = new World { Cache = cache }; world.Items = new ItemManager(1u, cache, new ProvenanceLedger(), RunSaveTests.TestSettings(), world.Log.Add); world.Zh = cache.GetAll().Single(f => f.Name == "Zhestokost"); world.Au = cache.GetAll().Single(f => f.ShortName == "AU"); world.Lucent = cache.GetAll().Single(f => f.ShortName == "Lucent"); world.Zenith = cache.GetAll().Single(h => h.Name == "Zenith"); world._zenithProduct = cache.GetAll().Single(p => cache.Get(p.Design) == world.Zenith); if (!zenithOffered) { world._zenithProduct.NotOffered = true; cache.Upsert(world._zenithProduct); } return world; } // A station hull of the given kind that this faction sells: a copy of Zenith's body, so it takes the same docking bay // and heater, with its own name and kind. public HullData Sell(Faction maker, string kindId, string name = null) { var options = CultDocumentMessagePackSerialization.OptionsFor(typeof(HullData).Assembly); var hull = MessagePackSerializer.Deserialize(MessagePackSerializer.Serialize(Zenith, options), options); hull.Name = name ?? $"{maker.ShortName} {kindId} {_added++}"; hull.Kind = Kind(kindId); var hullRef = Cache.Upsert(hull); var product = MessagePackSerializer.Deserialize(MessagePackSerializer.Serialize(_zenithProduct, options), options); product.Name = hull.Name; product.Design = new CultRecordRef(hullRef.Key); product.Manufacturer = Cache.RefOf(maker); product.NotOffered = false; Cache.Upsert(product); return hull; } public void StandIn(string kindId, Faction faction, string asKindId) { var kind = Cache.Get(Kind(kindId)); kind.StandIns.Add(new StationKindStandIn { Faction = Cache.RefOf(faction), Kind = Kind(asKindId) }); Cache.Upsert(kind); } public LoadoutGenerator Generator(Faction faction, uint seed = 1) { var random = new Random(seed); Items.Random = new Random(seed); return new LoadoutGenerator(ref random, Items, null, null, faction, .5f); } public string HullName(OrbitalEntityPack pack) => Items.GetData(pack.Hull).Name; public HullData HullOf(OrbitalEntityPack pack) => (HullData) Items.GetData(pack.Hull); } // ---- Open ---- private CultCache SeedAndReopen(Action seed, out string path) { path = Path.Combine(_root, "open" + _worlds++ + ".cc"); using (var cache = AetheriaStores.Open(path, catalogWritable: true)) { cache.Upsert(new VerseGrammar { Revision = 1 }); cache.Upsert(new InputLayout { Rows = new InputLayoutRow[] { new InputLayoutRowSpacer { Height = 1 } } }); seed(cache); cache.FlushAsync().Wait(); } return null; } private string OpenRefusal(Action seed) { SeedAndReopen(seed, out var path); var failure = Assert.Throws(() => AetheriaStores.Open(path)); return failure.Message; } private static HullData Hull(HullType type, CultRecordRef kind = default) => new HullData { Name = "Probe " + type, HullType = type, Shape = new Shape(2, 2), Price = 1, Kind = kind }; [Fact] public void OpenRefusesKindlessStationHull() { Assert.Contains("has no Kind", OpenRefusal(cache => cache.Upsert(Hull(HullType.Station)))); Assert.Contains("names no station kind", OpenRefusal(cache => cache.Upsert(Hull(HullType.Station, Kind("nowhere"))))); var shipWithKind = OpenRefusal(cache => { var kind = new CultRecordRef(cache.Upsert(new StationKindData { Name = "Hub" }).Key); cache.Upsert(Hull(HullType.Ship, kind)); }); Assert.Contains("only a station hull has one", shipWithKind); var turretWithKind = OpenRefusal(cache => { var kind = new CultRecordRef(cache.Upsert(new StationKindData { Name = "Hub" }).Key); cache.Upsert(Hull(HullType.Turret, kind)); }); Assert.Contains("only a station hull has one", turretWithKind); } [Fact] public void OpenRefusesBadStandIn() { CultRecordRef Mint(CultCache cache, string name, Action shape = null) { var kind = new StationKindData { Name = name }; shape?.Invoke(kind); return new CultRecordRef(cache.Upsert(kind).Key); } Assert.Contains("kind itself", OpenRefusal(cache => { CultRecordRef faction = cache.Upsert(new Faction { Name = "F", ShortName = "F" }); var kind = new StationKindData { Name = "Self" }; var handle = cache.Upsert(kind); kind.StandIns.Add(new StationKindStandIn { Faction = faction, Kind = new CultRecordRef(handle.Key) }); cache.Upsert(kind); })); Assert.Contains("StandIns[0].Faction names no faction", OpenRefusal(cache => { var other = Mint(cache, "Other"); Mint(cache, "Lonely", kind => kind.StandIns.Add(new StationKindStandIn { Faction = new CultRecordRef(new CultRecordKey("nobody")), Kind = other })); })); Assert.Contains("StandIns[0].Kind names no station kind", OpenRefusal(cache => { CultRecordRef faction = cache.Upsert(new Faction { Name = "F", ShortName = "F" }); Mint(cache, "Lonely", kind => kind.StandIns.Add(new StationKindStandIn { Faction = faction, Kind = Kind("nowhere") })); })); // A stand-in whose kind stands in for another faction only is one hop for this faction: Open accepts it. SeedAndReopen(cache => { CultRecordRef mine = cache.Upsert(new Faction { Name = "F", ShortName = "F" }); CultRecordRef other = cache.Upsert(new Faction { Name = "G", ShortName = "G" }); var third = Mint(cache, "Third"); var second = Mint(cache, "Second", kind => kind.StandIns.Add(new StationKindStandIn { Faction = other, Kind = third })); Mint(cache, "First", kind => kind.StandIns.Add(new StationKindStandIn { Faction = mine, Kind = second })); }, out var accepted); AetheriaStores.Open(accepted).Dispose(); // Any one of the stand-in kind's entries for the same faction makes a second hop. Assert.Contains("one hop", OpenRefusal(cache => { CultRecordRef mine = cache.Upsert(new Faction { Name = "F", ShortName = "F" }); CultRecordRef other = cache.Upsert(new Faction { Name = "G", ShortName = "G" }); var third = Mint(cache, "Third"); var second = Mint(cache, "Second", kind => { kind.StandIns.Add(new StationKindStandIn { Faction = other, Kind = third }); kind.StandIns.Add(new StationKindStandIn { Faction = mine, Kind = third }); }); Mint(cache, "First", kind => kind.StandIns.Add(new StationKindStandIn { Faction = mine, Kind = second })); })); Assert.Contains("one hop", OpenRefusal(cache => { CultRecordRef faction = cache.Upsert(new Faction { Name = "F", ShortName = "F" }); var third = Mint(cache, "Third"); var second = Mint(cache, "Second", kind => kind.StandIns.Add(new StationKindStandIn { Faction = faction, Kind = third })); Mint(cache, "First", kind => kind.StandIns.Add(new StationKindStandIn { Faction = faction, Kind = second })); })); } // ---- generation ---- [Fact] public void StationsArePlacedByKind() { var world = World.Make(this, zenithOffered: false); world.Sell(world.Au, "mining"); world.Sell(world.Au, "shipyard"); world.Sell(world.Au, "mining"); var kinds = StationKinds.KindsOf(world.Cache, world.Au).Select(kind => kind.Key.Value).ToArray(); Assert.Equal(new[] { StationKindData.KeyOf("mining").Value, StationKindData.KeyOf("shipyard").Value }, kinds); var seen = new HashSet(); for (var site = 0; site < 40; 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); Assert.NotNull(pack); Assert.Equal(kind.Key, pack.Kind.Key); Assert.Equal(kind.Key, world.HullOf(pack).Kind.Key); } Assert.Equal(kinds.Length, seen.Count); } [Fact] public void StandInUsesFactionModelAndKeepsName() { var world = World.Make(this, zenithOffered: false); var hub = world.Sell(world.Zh, "trade-hub", "Zh Hub"); world.StandIn("mining", world.Zh, "trade-hub"); var mining = world.Cache.Get(Kind("mining")); Assert.Contains(Kind("mining").Key.Value, StationKinds.KindsOf(world.Cache, world.Zh).Select(kind => kind.Key.Value)); Assert.Equal(Kind("trade-hub").Key, StationKinds.HullKindFor(world.Cache, world.Zh, Kind("mining")).Key); for (uint seed = 1; seed <= 10; seed++) { var pack = world.Generator(world.Zh, seed).GenerateStationLoadout(Kind("mining")); Assert.Equal("Zh Hub", world.HullName(pack)); Assert.Equal(Kind("mining").Key, pack.Kind.Key); Assert.Equal(mining.Name, pack.Name); } var own = world.Generator(world.Zh).GenerateStationLoadout(Kind("trade-hub")); Assert.Equal(hub.Name, own.Name); } [Fact] public void ModelWinsOverStaleStandIn() { var world = World.Make(this, zenithOffered: false); world.Sell(world.Zh, "trade-hub", "Zh Hub"); world.Sell(world.Zh, "mining", "Zh Mine"); world.StandIn("mining", world.Zh, "trade-hub"); Assert.Equal(Kind("mining").Key, StationKinds.HullKindFor(world.Cache, world.Zh, Kind("mining")).Key); for (uint seed = 1; seed <= 10; seed++) { var pack = world.Generator(world.Zh, seed).GenerateStationLoadout(Kind("mining")); Assert.Equal("Zh Mine", world.HullName(pack)); Assert.Equal("Zh Mine", pack.Name); } } [Fact] public void FactionWithoutModelsFallsBack() { var world = World.Make(this, zenithOffered: false); world.Sell(world.Au, "shipyard"); Assert.Empty(StationKinds.KindsOf(world.Cache, world.Lucent)); var logged = new List(); 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(); 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); Assert.False(StationKinds.HullKindFor(world.Cache, world.Au, default).IsSet()); var pack = world.Generator(world.Lucent).GenerateStationLoadout(kind); Assert.NotNull(pack); Assert.Equal(kind.Key, pack.Kind.Key); } [Fact] public void KindChoiceLeavesRandomStream() { var world = World.Make(this, zenithOffered: false); world.Sell(world.Au, "mining"); world.Sell(world.Au, "shipyard"); world.Sell(world.Au, "depot"); 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"); // Generating a zone with one station kind in the catalog and with several draws the same orbits and turrets. (float, float)[] Orbits(Action stock) { var w = World.Make(this, zenithOffered: true); stock?.Invoke(w); var authored = AuthoredSettings.Load(RunStartTests.FindRepoRoot()); var dir = Path.Combine(_root, "galaxy" + _worlds++); var galaxy = new Galaxy(authored.Read("TutorialGenerationSettings"), authored.Read("TutorialBackgroundSettings"), w.Cache, new PlayerSettings(), Directory.CreateDirectory(Path.Combine(dir, "Narrative")), _ => { }, null, 3); var pack = ZoneGenerator.GenerateZone(w.Items, authored.Read("ZoneSettings"), authored.Read("PlanetSettings"), galaxy, galaxy.Entrance, galaxy.IsPrelude, true); Assert.NotEmpty(pack.Entities.OfType()); return pack.Orbits.Select(orbit => w.Cache.Get(orbit)).Select(orbit => (orbit.Distance, orbit.Phase)).ToArray(); } var one = Orbits(null); var several = Orbits(w => { w.Sell(w.Au, "mining"); w.Sell(w.Zh, "shipyard"); w.Sell(w.Lucent, "depot"); }); Assert.Equal(one, several); } // A station's kind is a function of the zone's seed and the orbit's place in generation, not of a key the cache mints: // the same zone generated again, through the real key minting, gives the same stations the same kinds in the same order. [Fact] public void SameSeedGivesTheSameStationKinds() { string[] Kinds() { var w = World.Make(this, zenithOffered: true); foreach (var faction in new[] { w.Au, w.Zh, w.Lucent }) foreach (var kind in new[] { "mining", "shipyard", "depot", "power" }) w.Sell(faction, kind); var authored = AuthoredSettings.Load(RunStartTests.FindRepoRoot()); var galaxy = new Galaxy(authored.Read("TutorialGenerationSettings"), authored.Read("TutorialBackgroundSettings"), w.Cache, new PlayerSettings(), Directory.CreateDirectory(Path.Combine(_root, "galaxy" + _worlds++, "Narrative")), _ => { }, null, 3); return galaxy.Zones.Take(8).SelectMany(zone => ZoneGenerator.GenerateZone(w.Items, authored.Read("ZoneSettings"), authored.Read("PlanetSettings"), galaxy, zone, zone == galaxy.Entrance && galaxy.IsPrelude, true) .Entities.OfType().Where(e => e.Kind.IsSet()).Select(e => e.Kind.Key.Value)).ToArray(); } var first = Kinds(); Assert.True(first.Length >= 3, "the sample placed stations"); for (var run = 1; run < 6; run++) Assert.Equal(first, Kinds()); } [Fact] public void GeneratedZonesPlaceStationsByKind() { var w = World.Make(this, zenithOffered: true); w.Sell(w.Au, "mining"); w.Sell(w.Au, "shipyard"); var authored = AuthoredSettings.Load(RunStartTests.FindRepoRoot()); var galaxy = new Galaxy(authored.Read("TutorialGenerationSettings"), authored.Read("TutorialBackgroundSettings"), w.Cache, new PlayerSettings(), Directory.CreateDirectory(Path.Combine(_root, "zones", "Narrative")), _ => { }, null, 5); var stations = 0; foreach (var galaxyZone in galaxy.Zones.Take(8)) { var pack = ZoneGenerator.GenerateZone(w.Items, authored.Read("ZoneSettings"), authored.Read("PlanetSettings"), galaxy, galaxyZone, galaxyZone == galaxy.Entrance && galaxy.IsPrelude, true); foreach (var station in pack.Entities.OfType().Where(e => w.HullOf(e).HullType == HullType.Station)) { stations++; Assert.True(station.Kind.IsSet(), "a placed station has a kind"); Assert.Equal(station.Kind.Key, w.HullOf(station).Kind.Key); var faction = w.Cache.Get(station.Faction); var kinds = StationKinds.KindsOf(w.Cache, faction); if (kinds.Length > 0) Assert.Contains(station.Kind.Key.Value, kinds.Select(kind => kind.Key.Value)); } } Assert.True(stations > 0, "the sample generated stations"); } [Fact] public void KindSurvivesSave() { var world = World.Make(this, zenithOffered: false); world.Sell(world.Au, "research"); var pack = world.Generator(world.Au).GenerateStationLoadout(Kind("research")); var options = CultDocumentMessagePackSerialization.OptionsFor(typeof(HullData).Assembly); var bytes = MessagePackSerializer.Serialize(pack, options); var restored = Assert.IsType(MessagePackSerializer.Deserialize(bytes, options)); Assert.Equal(Kind("research").Key, restored.Kind.Key); var entity = Assert.IsType(EntitySerializer.Unpack(world.Items, null, restored)); Assert.Equal(Kind("research").Key, entity.Kind.Key); Assert.Equal(Kind("research").Key, ((OrbitalEntityPack) EntitySerializer.Pack(entity)).Kind.Key); } // ---- the shipped catalog ---- [Fact] public void ShippedCatalogHoldsOneKindList() { using var cache = AetheriaStores.Open(Path.Combine(RunStartTests.FindRepoRoot(), "GameData", "Aetheria.cc")); var ids = new[] { "mining", "volatiles", "agriculture", "processing", "fabrication", "shipyard", "depot", "trade-hub", "residential", "habitat-support", "urban", "administrative", "power", "research", "salvage", "black-market", "waystation" }; Assert.Equal(ids.Select(id => StationKindData.KeyOf(id).Value).OrderBy(key => key, StringComparer.Ordinal), cache.GetAll().Select(kind => cache.RefOf(kind).Key.Value).OrderBy(key => key, StringComparer.Ordinal)); Assert.Equal(StationKindData.KeyOf("trade-hub").Value, cache.GetAll().Single(h => h.Name == "Zenith").Kind.Key.Value); } private static void AssertStandInsLive(CultCache cache) { foreach (var kind in cache.GetAll()) foreach (var standIn in kind.StandIns) { var faction = cache.Get(standIn.Faction); var models = StationKinds.KindsOf(cache, faction); var own = cache.GetAll().Where(p => cache.Get(p.Manufacturer) == faction && LoadoutGenerator.IsOffered(cache, p)) .Select(p => cache.Get(p.Design) as HullData).Where(h => h != null && h.HullType == HullType.Station).Select(h => h.Kind.Key).ToArray(); Assert.DoesNotContain(cache.RefOf(kind).Key, own); Assert.Contains(standIn.Kind.Key, own); Assert.NotEmpty(models); } } [Fact] public void ShippedStandInsAreLive() { using var cache = AetheriaStores.Open(TestCatalog.Repo); AssertStandInsLive(cache); } // The retirement check is not vacuous: given a stand-in it passes when the faction offers the kind it stands in with and // not the owning kind, and fails when the faction has gained its own model of the owning kind or has no model to use. [Fact] public void StandInRetirementCheckCanFail() { var live = World.Make(this, zenithOffered: false); live.Sell(live.Au, "mining"); live.StandIn("shipyard", live.Au, "mining"); Assert.Single(live.Cache.GetAll().SelectMany(kind => kind.StandIns)); AssertStandInsLive(live.Cache); var retired = World.Make(this, zenithOffered: false); retired.Sell(retired.Au, "mining"); retired.Sell(retired.Au, "shipyard"); retired.StandIn("shipyard", retired.Au, "mining"); Assert.ThrowsAny(() => AssertStandInsLive(retired.Cache)); var hollow = World.Make(this, zenithOffered: false); hollow.StandIn("shipyard", hollow.Au, "mining"); Assert.ThrowsAny(() => AssertStandInsLive(hollow.Cache)); } }