/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ using System; using System.Collections.Generic; using System.Linq; using CultMath; using Xunit; // The demo scenario (aetheria-release, cut demo-galaxy): a galaxy of a fixed cast whose antagonist's home is the // boss zone and the exit. The cast, the Pirates included, is in the shipped catalog. public sealed partial class RunStartTests { private static readonly uint[] DemoSeeds = { 1, 2, 3, 4, 5 }; private Galaxy DemoGalaxy(uint seed) => RunStart.Generate(new DemoTerminus(), Inputs(() => seed)); [Fact] public void DemoTerminus_fixed_cast() { foreach (var seed in DemoSeeds) Assert.Equal(new[] { "Pirates", "Zhestokost", "Lucent Media", "Aeronautics Unlimited" }, DemoGalaxy(seed).Factions.Select(faction => faction.Name)); } // The entrance declares its stations as orders (ruling demo-zenith-ore-depot, demo-processing-station-buys-ore): on every seed, // Aeronautics Unlimited's trade hub on Zenith and the Pirates' processing station with its Refinery, apart by at least their reaches. [Fact] public void DemoEntranceHoldsZenithAndTheRefinery() { var cellSize = _items.GameplaySettings.SchematicCellSize; foreach (var seed in Enumerable.Range(1, 40).Select(s => (uint) s).Concat(new uint[] { 69, 97 })) { var (_, arena, _, failures) = Launch(new DemoTerminus(), Inputs(() => seed)); Assert.True(failures.Count == 0, $"seed {seed}: " + string.Join("; ", failures)); arena.Update(.02f); // the stations take their places on their orbits var stations = arena.Entities.OfType().Where(entity => entity.HullData.HullType == HullType.Station).ToArray(); Assert.True(stations.Length == 2, $"seed {seed}: {stations.Length} stations"); var hub = stations.Single(station => station.Kind.Key.Equals(StationKindData.KeyOf("trade-hub"))); var refinery = stations.Single(station => station.Kind.Key.Equals(StationKindData.KeyOf("processing"))); Assert.Equal("Aeronautics Unlimited", hub.Faction.Name); Assert.Equal("Pirates", refinery.Faction.Name); Assert.Contains(refinery.Equipment, equipped => equipped.Behaviors.OfType().Any() && _items.GetData(equipped.EquippableItem).Name == "Refinery"); var apart = math.length(hub.Position.xz - refinery.Position.xz); var reaches = hub.HullData.Reach(cellSize) + refinery.HullData.Reach(cellSize); Assert.True(apart >= reaches, $"seed {seed}: stations {apart} apart against a reach sum of {reaches}"); } } [Fact] public void DemoTerminus_gate_at_antagonist_home() { foreach (var seed in DemoSeeds) { var galaxy = DemoGalaxy(seed); // Zones are compared by index: xunit formats a failing zone into its message, and a zone drags the galaxy // graph in with it until the test host dies. int At(GalaxyZone zone) => Array.IndexOf(galaxy.Zones, zone); var zhestokost = galaxy.Factions.Single(faction => faction.Name == "Zhestokost"); Assert.Equal(new[] { "Zhestokost" }, galaxy.BossZones.Keys.Select(faction => faction.Name)); Assert.Equal(At(galaxy.HomeZones[zhestokost]), At(galaxy.BossZones[zhestokost])); Assert.Equal(At(galaxy.HomeZones[zhestokost]), At(galaxy.Exit)); Assert.NotEqual(At(galaxy.Entrance), At(galaxy.Exit)); } } // The player's standing is part of the run: Continue restores it, and the gate, through the saved game. [Fact] public void DemoTerminus_standing_survives_continue() { var (galaxy, arena, staged, failures) = Launch(new DemoTerminus(), Inputs(() => 1)); Assert.True(failures.Count == 0, string.Join("; ", failures)); galaxy.Entrance.Contents = arena; LiveRegion.Fill(_items, galaxy); var (game, zones) = RunSave.Capture(_cache, galaxy, arena, staged.Player, new SavedActionBarBinding[0], 0); RunSave.Commit(_cache, game, zones, _items.Lots); var restored = new Galaxy(_cache, _cache.GetGlobal(), _ => { }); Assert.NotEqual(0u, galaxy.Seed); Assert.Equal(galaxy.Seed, restored.Seed); Dictionary Standing(Galaxy g) => g.Factions.ToDictionary(faction => faction.Name, faction => g.FactionRelationships[faction]); Assert.Equal(new Dictionary { ["Pirates"] = FactionRelationship.Friendly, ["Zhestokost"] = FactionRelationship.Hated, ["Lucent Media"] = FactionRelationship.Neutral, ["Aeronautics Unlimited"] = FactionRelationship.Neutral }, Standing(restored)); Assert.Equal(Standing(galaxy), Standing(restored)); Assert.Equal(Array.IndexOf(galaxy.Zones, galaxy.Exit), Array.IndexOf(restored.Zones, restored.Exit)); Assert.Equal("Zhestokost", restored.Antagonist.Name); Assert.Equal(_cache.RefOf(galaxy.Antagonist), _cache.RefOf(restored.Antagonist)); Assert.Equal( galaxy.BossZones.Select(boss => (boss.Key.Name, Array.IndexOf(galaxy.Zones, boss.Value))), restored.BossZones.Select(boss => (boss.Key.Name, Array.IndexOf(restored.Zones, boss.Value)))); } // A catalog without the Pirates (the shipped record and its products removed from this scratch copy) does not start the // run: the galaxy refuses, naming the cast field, and the scenario never reaches staging. [Fact] public void DemoTerminus_missing_cast_refused() { var pirates = _cache.GetAll().Single(faction => faction.Name == "Pirates"); var piratesRef = _cache.RefOf(pirates); foreach (var product in _cache.GetAll().Where(product => product.Manufacturer.Equals(piratesRef)).ToArray()) Assert.True(_cache.Remove(_cache.RefOf(product).Key)); Assert.True(_cache.Remove(piratesRef.Key)); Assert.DoesNotContain(_cache.GetAll(), faction => faction.Name.StartsWith("Pirates", StringComparison.Ordinal)); var refusal = Assert.Throws(() => DemoGalaxy(1)); Assert.Contains("Pirates", refusal.Message); Assert.Contains(nameof(GalaxyCast.ProtagonistFaction), refusal.Message); } // The operator's ruling is that the first release is a demo: the run the simulation's exit launches says so by name. [Fact] public void DemoTerminus_is_named_a_demo() { Assert.IsType(Scenarios.Terminus); Assert.Equal("Terminus (Demo)", Scenarios.Terminus.Name); } // The run start as New Game runs it: the player is not a Pirate, so the hull is the base Longinus, made by Alakrita, // not by the Pirates or any cast faction, with no faction on the ship, at the entrance zone's origin. [Fact] public void DemoTerminus_player_flies_a_non_pirates_brand_hull_the_longinus() { foreach (var seed in DemoSeeds) { var (galaxy, _, staged, failures) = Launch(new DemoTerminus(), Inputs(() => seed)); Assert.True(failures.Count == 0, string.Join("; ", failures)); Assert.Equal("Longinus", staged.Player.HullData.Name); var design = _cache.RefOf(staged.Player.HullData).Key; var product = Assert.Single(_cache.GetAll(), p => p.Design.Key.Equals(design)); var maker = _cache.Get(product.Manufacturer); Assert.Equal("Alakrita", maker.Name); Assert.DoesNotContain(galaxy.Factions, faction => faction.Name == maker.Name); Assert.Null(staged.Player.Faction); Assert.Equal(float3.zero, staged.Player.Position); } } // The ruling's default fit is the authored one (economy-is-never-faked): the Longinus with Light Lasers on its Energy mounts, the same // products at the same cells on every seed, every lot at StarterQuality. The Cell Blaster is ammunition-fed on an Energy mount and spends its // crates in seconds, so no Energy-mount weapon may draw ammunition. Kills: a Generated player (the brands and the Energy pair roll per seed), // Spotlight swapped for Strobe (Cell Blaster), a different quality. [Fact] public void DemoTerminus_player_carries_the_authored_starter() { var expected = new[] { "Wheelhouse@2,6/None", "True North@1,0/Reversed", "True North@3,0/Reversed", "Hestia@2,4/None", "Homer@0,5/None", "Homer@5,5/None", "Stagecoach@2,14/CounterClockwise", "Stagecoach@3,14/Clockwise", "Spotlight@1,8/None", "Spotlight@4,8/None", "Iapyx@2,2/CounterClockwise", "Iapyx@3,2/Clockwise", "not if i see you first@3,10/None", "PotaT+-@1,5/None", "👀🎯@1,6/None", "Space Tractor@1,7/None", "Pithos@2,8/None" }.OrderBy(entry => entry, StringComparer.Ordinal).ToArray(); foreach (var seed in DemoSeeds) { var (_, _, staged, failures) = Launch(new DemoTerminus(), Inputs(() => seed)); Assert.True(failures.Count == 0, string.Join("; ", failures)); var player = staged.Player; var slots = player.Equipment.Concat(player.CargoBays).Where(item => item != player.EquippedHull).Distinct().ToArray(); var got = string.Join(" | ", slots.Select(item => $"{_cache.Get(_items.GetLot(item.EquippableItem).Product).Name}@{item.Position.x},{item.Position.y}/{item.EquippableItem.Rotation}") .OrderBy(entry => entry, StringComparer.Ordinal).ToArray()); Assert.True(got == string.Join(" | ", expected), "seed " + seed + ": " + got); Assert.All(slots, item => Assert.Equal(DemoTerminus.StarterQuality, _items.GetLot(item.EquippableItem).Quality, 3)); var energy = player.Weapons.Where(weapon => player.Hardpoints[weapon.Item.Position.x, weapon.Item.Position.y]?.Type == HardpointType.Energy).ToArray(); Assert.Equal(2, energy.Length); Assert.All(energy, weapon => Assert.False(weapon.WeaponData.AmmoType.IsSet(), $"seed {seed}: {weapon.Item.Data.Name} on an Energy mount draws ammunition")); } } // The demo region's size and connectivity are authored: 64 zones, links thinned to half. The reference galaxy is the // same cast with the thinning written out here, generated at the same seed, so any other density is a different graph. [Fact] public void DemoTerminus_region_is_sixty_four_zones_linked_at_half_density() { string Links(Galaxy g) => string.Join(";", g.Zones.Select(zone => string.Join(",", zone.AdjacentZones.Select(adjacent => Array.IndexOf(g.Zones, adjacent)).OrderBy(index => index)))); Galaxy Reference(uint seed, float density) { var cast = new GalaxyCast { ProtagonistFaction = "Pirates", AntagonistFaction = "Zhe", BufferFaction = "Luc", NeutralFactions = new[] { "Aero" }, QuestFaction = null, ZoneCount = 64, LinkDensity = density, InfluenceScale = .5f }; return RunStart.Generate(new Scripted(false, _ => { }, seed, stage => stage.Cast(cast)), Inputs()); } foreach (var seed in DemoSeeds) { var galaxy = DemoGalaxy(seed); Assert.Equal(64, galaxy.Zones.Length); Assert.Equal(Links(Reference(seed, .5f)), Links(galaxy)); Assert.NotEqual(Links(Reference(seed, 1f)), Links(galaxy)); } } // A cast that names no quest faction places no quest zone: one home for each of its four factions. [Fact] public void ACastNamingNoQuestFactionPlacesNoQuestZone() { var galaxy = DemoGalaxy(1); Assert.Equal(4, galaxy.HomeZones.Count); Assert.Equal(4, galaxy.Factions.Length); } // A name that resolves to nothing refuses the galaxy whichever cast field names it, and the refusal names the field. [Theory] [InlineData(nameof(GalaxyCast.ProtagonistFaction))] [InlineData(nameof(GalaxyCast.AntagonistFaction))] [InlineData(nameof(GalaxyCast.BufferFaction))] [InlineData(nameof(GalaxyCast.QuestFaction))] [InlineData(nameof(GalaxyCast.NeutralFactions))] public void AnUnresolvedCastNameRefusesTheGalaxyNamingItsField(string field) { var cast = TestCasts.Wide; typeof(GalaxyCast).GetField(field).SetValue(cast, field == nameof(cast.NeutralFactions) ? new[] { "Nonesuch" } : "Nonesuch"); var scenario = new Scripted(false, _ => { }, generate: stage => stage.Cast(cast)); var refusal = Assert.Throws(() => RunStart.Generate(scenario, Inputs())); Assert.Contains(field, refusal.Message); } // What a release build lists under New Game is the simulation alone (ruling ifl-wormhole-exit-standin): the demo run // is reached through its exit, and the main galaxy is listed only in editor and development builds // (MainMenu.ShowScenarios), with the demo and the test arenas. [Fact] public void ReleaseBuildsListOnlyTheSimulation() { Assert.Equal(new[] { typeof(InfiniteFunLand) }, Scenarios.Modes.Select(scenario => scenario.GetType())); Assert.Equal(new[] { typeof(MainGalaxy), typeof(DemoTerminus) }, Scenarios.Development.Select(scenario => scenario.GetType())); } // The wide test cast at half influence is the galaxy it was before the prelude was retired: its cast, homes, entrance // and ownership at a fixed seed, with no exit and no boss zone. Homes, entrance and owners depend on InfluenceOf, so // an InfluenceScale of 1 or a lost halving fails it. [Fact] public void CastGalaxyAtHalfInfluenceIsUnchanged() { var galaxy = RunStart.Generate(new Scripted(false, _ => { }, generate: stage => stage.Cast(TestCasts.Wide)), Inputs(() => GalaxySeed)); Assert.Equal(WideCastSeed1, CastShape(galaxy)); Assert.True(galaxy.Exit == null, "a cast galaxy has no exit until its scenario places the gate"); Assert.True(galaxy.BossZones.Count == 0, $"the cast galaxy has {galaxy.BossZones.Count} boss zones"); } private static string CastShape(Galaxy g) => string.Join(",", g.Factions.Select(f => f.Name)) + " | homes " + string.Join(",", g.Factions.Select(f => g.HomeZones.TryGetValue(f, out var z) ? Array.IndexOf(g.Zones, z) : -2)) + " | entrance " + Array.IndexOf(g.Zones, g.Entrance) + " | zones " + g.Zones.Length + " | owners " + string.Join(",", g.Zones.Select(z => z.Owner == null ? "-" : z.Owner.ShortName)); // Measured at seed 1 under CultMath 0.3.0 (cultmath-unity-v0.3.0); the noise kernel changed there, which re-rolled the galaxy. private const string WideCastSeed1 = "Miss Terri’s Sugariffic Snack Company,Zhestokost,Lucent Media,Adrasteia,Aeronautics Unlimited,Finch Cybernetics | homes 3,44,40,19,0,30 | entrance 12 | zones 64 | owners AU,AU,Finch,Miss Terri's,Lucent,Zhestokost,-,Lucent,AU,AU,Lucent,Adrasteia,-,Finch,Zhestokost,-,Adrasteia,-,AU,Adrasteia,AU,Zhestokost,Miss Terri's,-,AU,Finch,Miss Terri's,-,Zhestokost,-,Finch,-,-,AU,-,-,-,-,Miss Terri's,AU,Lucent,-,Finch,-,Zhestokost,AU,-,Zhestokost,Adrasteia,Adrasteia,AU,Miss Terri's,Finch,AU,Finch,-,Lucent,Zhestokost,-,AU,Miss Terri's,Zhestokost,-,Lucent"; }