diff --git a/Assets/Scripts/ServerShared/Galaxy.cs b/Assets/Scripts/ServerShared/Galaxy.cs index 0b2f71387..6e28cf49e 100644 --- a/Assets/Scripts/ServerShared/Galaxy.cs +++ b/Assets/Scripts/ServerShared/Galaxy.cs @@ -288,9 +288,9 @@ public class Galaxy progressCallback?.Invoke("Placing Factions"); if (progressCallback != null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao - // Choose some megas to have bosses placed based on whether a boss hull is assigned + // Choose some megas to have bosses placed: the factions that make a Boss hull var bossMegas = Factions - .Where(m => m.BossHull.IsSet()) + .Where(m => LoadoutGenerator.BossHullsOf(_cache, m).Length > 0) .Take(bossCount) .ToArray(); diff --git a/Assets/Scripts/ServerShared/ItemData.cs b/Assets/Scripts/ServerShared/ItemData.cs index ca1484e36..431acc7ce 100644 --- a/Assets/Scripts/ServerShared/ItemData.cs +++ b/Assets/Scripts/ServerShared/ItemData.cs @@ -555,6 +555,11 @@ public class HullData : EquippableItemData [Inspectable, JsonProperty("visual"), Key(32)] public CultRecordRef Visual; + // Only bosses fly this hull. Which factions it is a boss for comes from its products' makers + // (LoadoutGenerator.BossHullsOf), so the flag names no faction and reads no model. + [Inspectable, JsonProperty("boss"), Key(33)] + public bool Boss; + // The farthest corner of any occupied hull cell from the centre of mass the schematic frame uses, in world // units: a true bound on how far the hull reaches from its position (the half-diagonal of the bounding box is not, // as FireControl.Detonate's comment records). A cell at (x, y) spans x +/- .5 and y +/- .5. diff --git a/Assets/Scripts/ServerShared/LoadoutGenerator.cs b/Assets/Scripts/ServerShared/LoadoutGenerator.cs index 67cc7f6b4..34e1cd1c0 100644 --- a/Assets/Scripts/ServerShared/LoadoutGenerator.cs +++ b/Assets/Scripts/ServerShared/LoadoutGenerator.cs @@ -48,7 +48,13 @@ public class LoadoutGenerator ItemManager.Log("Unable to generate ship loadout: no compatible hull found!"); return null; } - var hull = ItemManager.CreateInstance(hullProduct) as EquippableItem; + return ShipLoadout(hullProduct); + } + + // The ship a hull product becomes: outfit, pickup, consumables, packed. Callers choose the product; this is the one build. + public EntityPack ShipLoadout(FactionProductData product) + { + var hull = ItemManager.CreateInstance(product) as EquippableItem; if(hull==null) ItemManager.Log("WHAT???"); var entity = new Ship(ItemManager, null, hull, ItemManager.GameplaySettings.DefaultEntitySettings); @@ -184,7 +190,9 @@ public class LoadoutGenerator } // The products this faction can be offered for a design kind (only gear needs a home), and whether manufacturer preference applies (it does - // not once a required item has fallen back to any manufacturer). The one availability rule for every selection. + // not once a required item has fallen back to any manufacturer). The one availability rule for every selection: a Boss + // hull is never on offer, so it is never station stock, ordinary traffic, a generated scenario fit or another faction's + // fallback boss, and only BossHullsOf reaches it. private ((FactionProductData product, T design)[] available, bool preferManufacturers) AvailableProducts(Predicate filter, bool required) where T : CraftedItemData { var hulls = SoldHulls(ItemManager.ItemData); @@ -193,6 +201,7 @@ public class LoadoutGenerator .Where(entry => entry.design != null && entry.design.Price > 0 && + !(entry.design is HullData { Boss: true }) && entry.product.Manufacturer.IsSet() && (!(entry.design is EquippableItemData equippable) || HasHome(equippable, hulls)) && (filter?.Invoke(entry.design) ?? true)) @@ -209,10 +218,12 @@ public class LoadoutGenerator return (available, preferManufacturers); } - // A faction's boss: the most expensive ship hull the faction can be offered, with the gap logged. Null only when no - // ship hull is on offer at all. + // A faction's boss: the Boss hull it makes (BossHullsOf, dearest first), else the most expensive ordinary ship hull the + // faction can be offered, with the gap logged. Null only when no ship hull is on offer at all. public EntityPack GenerateBossLoadout() { + var made = BossHullsOf(ItemManager.ItemData, Faction); + if (made.Length > 0) return ShipLoadout(made[0].product); ItemManager.Log($"{Faction?.Name ?? "This faction"} makes no boss hull; its boss flies the most expensive ship hull instead."); var (available, _) = AvailableProducts(hull => hull.HullType == HullType.Ship, required: true); @@ -221,6 +232,19 @@ public class LoadoutGenerator return GenerateShipLoadout(hull => hull == priciest); } + // The one owner of which hull is a faction's boss: the Ship hulls flagged Boss among the designs of the products the + // faction manufactures, dearest first then by name. The flag lives on the hull and the maker on the product, so a Boss + // hull several makers sell is a boss of each; nothing here reads a model, a prefab or a visual. + public static (FactionProductData product, HullData hull)[] BossHullsOf(CultCache items, Faction faction) => + faction == null + ? Array.Empty<(FactionProductData, HullData)>() + : items.GetAll() + .Where(product => items.Get(product.Manufacturer) == faction) + .Select(product => (product, hull: items.Get(product.Design) as HullData)) + .Where(entry => entry.hull is { Boss: true, HullType: HullType.Ship }) + .OrderByDescending(entry => entry.hull.Price).ThenBy(entry => entry.hull.Name, StringComparer.Ordinal).ThenBy(entry => entry.product.Name, StringComparer.Ordinal) + .ToArray(); + // Hardpoint gear that no hull in the catalog can mount has no home (operator, 2026-09-30: "we'll need to author a // bunch more hulls before all the gear variety in the game has a home"): generation never offers it, so no station // stocks it. It gains one the moment a hull with a hardpoint that takes it exists. Tool gear goes in any interior. diff --git a/tests/Aetheria.Shared.Tests/RunGoalTests.cs b/tests/Aetheria.Shared.Tests/RunGoalTests.cs index 5d133893f..cbb7ac13b 100644 --- a/tests/Aetheria.Shared.Tests/RunGoalTests.cs +++ b/tests/Aetheria.Shared.Tests/RunGoalTests.cs @@ -8,6 +8,8 @@ using System.IO; using System.Linq; using System.Text.RegularExpressions; using CultMath; +using GameCult.Caching; +using GameCult.Caching.MessagePack; using UniRx; using Xunit; @@ -38,6 +40,191 @@ public sealed partial class RunStartTests .Where(hull => hull != null && hull.HullType == HullType.Ship && hull.Price > 0) .Distinct().ToArray(); + // A Boss hull a faction makes, by the production path: a copy of a sold ship hull (so it builds into a real ship) flagged + // Boss under its own name and price, and one copy of that hull's product per maker. A copy rather than the shipped + // hull itself, because flagging a shipped hull would make it a Boss hull of every maker the catalog gives it. + private HullData MakeBoss(string name, int price, params Faction[] makers) => MakeBoss(HullType.Ship, name, price, makers); + + private HullData MakeBoss(HullType type, string name, int price, params Faction[] makers) + { + var options = CultDocumentMessagePackSerialization.OptionsFor(typeof(HullData).Assembly); + T Copy(T document) => MessagePack.MessagePackSerializer.Deserialize(MessagePack.MessagePackSerializer.Serialize(document, options), options); + + var like = SoldShipHulls().OrderBy(hull => hull.Name, StringComparer.Ordinal).First(); + var likeProduct = _cache.GetAll().OrderBy(product => product.Name, StringComparer.Ordinal) + .First(product => _cache.Get(product.Design) == like); + var boss = Copy(like); + boss.Name = name; + boss.Price = price; + boss.HullType = type; + boss.Boss = true; + boss.Prefab = null; + boss.Visual = default; + var bossRef = _cache.Upsert(boss); + foreach (var maker in makers) + { + var product = Copy(likeProduct); + product.Name = $"{name} by {maker.Name}"; + product.Design = bossRef; + product.Manufacturer = _cache.RefOf(maker); + _cache.Upsert(product); + } + return _cache.Get(bossRef); + } + + private static Faction FactionNamed(Galaxy galaxy, string name) => galaxy.Factions.Single(faction => faction.Name == name); + + private HullData PriciestSold() => + SoldShipHulls().OrderByDescending(hull => hull.Price).ThenBy(hull => hull.Name, StringComparer.Ordinal).First(); + + private LoadoutGenerator GeneratorFor(Galaxy galaxy, ItemManager items, Faction faction, uint seed) + { + var random = new CultMath.Random(seed); + return new LoadoutGenerator(ref random, items, galaxy, galaxy.Exit, faction, .5f); + } + + [Fact] + public void Boss_flies_the_boss_hull_its_faction_makes() + { + var galaxy = DemoGalaxy(1); + var antagonist = Zhestokost(galaxy); + var cheapest = SoldShipHulls().Min(hull => hull.Price); + var priciest = PriciestSold(); + Assert.True(cheapest < priciest.Price, "the catalog needs two ship hulls of different price for this test to tell them apart"); + + // The faction also makes the dearest ordinary hull, and three Boss hulls: B and A tie on price, C is cheaper. The boss + // is the dearest Boss hull, ties by name: not the dearest hull it makes, not the first product, not the cheapest. + _cache.Upsert(new FactionProductData { Name = "Decoy", Design = _cache.RefOf(priciest), Manufacturer = _cache.RefOf(antagonist) }); + MakeBoss("Boss B", cheapest + 10, antagonist); + MakeBoss("Boss C", cheapest, antagonist); + MakeBoss("Boss A", cheapest + 10, antagonist); + + var log = new List(); + var items = LoggingItems(log); + var boss = Assert.Single(BossZonePack(galaxy, items, galaxy.Exit).Entities, entity => entity.Boss); + Assert.Equal("Boss A", HullName(items, boss)); + Assert.DoesNotContain(log, line => line.Contains("makes no boss hull")); + Assert.Equal(new[] { "Boss A", "Boss B", "Boss C" }, LoadoutGenerator.BossHullsOf(_cache, antagonist).Select(entry => entry.hull.Name)); + } + + [Fact] + public void Boss_without_a_boss_hull_falls_back() + { + var galaxy = DemoGalaxy(1); + var priciest = PriciestSold(); + + var log = new List(); + var items = LoggingItems(log); + var boss = Assert.Single(BossZonePack(galaxy, items, galaxy.Exit).Entities, entity => entity.Boss); + Assert.Equal(priciest.Name, HullName(items, boss)); + Assert.Contains(log, line => line.Contains("Zhestokost") && line.Contains("makes no boss hull")); + Assert.Empty(LoadoutGenerator.BossHullsOf(_cache, Zhestokost(galaxy))); + Assert.Empty(LoadoutGenerator.BossHullsOf(_cache, null)); + } + + [Fact] + public void Another_factions_boss_hull_is_never_used() + { + var galaxy = DemoGalaxy(1); + var antagonist = Zhestokost(galaxy); + var lucent = FactionNamed(galaxy, "Lucent"); + var expected = PriciestSold().Name; + // Dearer than every ordinary hull, and Lucent's alone: Zhestokost neither derives it nor falls back to it. + var luxury = MakeBoss("Lucent Flagship", PriciestSold().Price + 1000, lucent); + + var log = new List(); + var items = LoggingItems(log); + var boss = Assert.Single(BossZonePack(galaxy, items, galaxy.Exit).Entities, entity => entity.Boss); + Assert.Equal(expected, HullName(items, boss)); + Assert.Contains(log, line => line.Contains("Zhestokost") && line.Contains("makes no boss hull")); + Assert.Empty(LoadoutGenerator.BossHullsOf(_cache, antagonist)); + Assert.Same(luxury, Assert.Single(LoadoutGenerator.BossHullsOf(_cache, lucent)).hull); + } + + [Fact] + public void A_boss_hull_two_makers_sell_is_a_boss_of_each() + { + var galaxy = DemoGalaxy(1); + var antagonist = Zhestokost(galaxy); + var lucent = FactionNamed(galaxy, "Lucent"); + var shared = MakeBoss("Shared Boss", PriciestSold().Price - 1, antagonist, lucent); + // A Boss station hull the antagonist also makes is no boss: a boss is a ship. + MakeBoss(HullType.Station, "Boss Station", PriciestSold().Price + 1, antagonist); + + Assert.Same(shared, Assert.Single(LoadoutGenerator.BossHullsOf(_cache, antagonist)).hull); + Assert.Same(shared, Assert.Single(LoadoutGenerator.BossHullsOf(_cache, lucent)).hull); + var items = LoggingItems(new List()); + var boss = Assert.Single(BossZonePack(galaxy, items, galaxy.Exit).Entities, entity => entity.Boss); + Assert.Equal("Shared Boss", HullName(items, boss)); + } + + // Ruling hull-programmer-art-stopgap: a boss hull may be an agent's stopgap with no authored model. + [Fact] + public void A_boss_hull_needs_no_authored_model() + { + var galaxy = DemoGalaxy(1); + var antagonist = Zhestokost(galaxy); + var bare = MakeBoss("Bare Boss", 1, antagonist); + Assert.Null(bare.Prefab); + Assert.False(bare.Visual.IsSet()); + + Assert.Same(bare, Assert.Single(LoadoutGenerator.BossHullsOf(_cache, antagonist)).hull); + var items = LoggingItems(new List()); + var boss = Assert.Single(BossZonePack(galaxy, items, galaxy.Exit).Entities, entity => entity.Boss); + Assert.Equal("Bare Boss", HullName(items, boss)); + } + + // A Boss hull is flown only by bosses: with the cheapest price (the heaviest draw weight) and every relevant maker, it still + // never comes out of a generation draw, including the any-manufacturer fallback and a faction no maker is allied to. + [Fact] + public void Boss_hulls_are_never_stock_or_traffic() + { + var galaxy = DemoGalaxy(1); + var antagonist = Zhestokost(galaxy); + var lucent = FactionNamed(galaxy, "Lucent"); + var bossShip = MakeBoss("Traffic Boss", 1, antagonist, lucent); + var bossStation = MakeBoss(HullType.Station, "Stock Boss", 1, antagonist, lucent); + var stranger = new Faction { Name = "Stranger" }; + _cache.Upsert(stranger); + stranger = _cache.GetAll().Single(faction => faction.Name == "Stranger"); + MakeBoss("Strangers Foe Boss", 1, lucent); + var log = new List(); + var items = LoggingItems(log); + + var drawn = 0; + for (uint seed = 1; seed <= 20; seed++) + foreach (var faction in new[] { antagonist, lucent, stranger, null }) + { + var generator = GeneratorFor(galaxy, items, faction, seed); + for (var i = 0; i < 3; i++) + { + var pack = generator.GenerateShipLoadout(); + Assert.NotNull(pack); + drawn++; + Assert.DoesNotContain(HullName(items, pack), new[] { "Traffic Boss", "Strangers Foe Boss" }); + Assert.NotEqual("Stock Boss", generator.RandomHull(HullType.Station).design?.Name); + } + // Asked for by name, with the boss the only candidate: nothing is on offer, however the fallback is reached. + Assert.Null(generator.GenerateShipLoadout(hull => hull == bossShip)); + Assert.Null(generator.GenerateShipLoadout(hull => hull.Name == "Strangers Foe Boss")); + Assert.Null(generator.RandomHull(HullType.Station, hull => hull == bossStation).design); + } + Assert.Equal(20 * 4 * 3, drawn); + } + + [Fact] + public void Main_galaxy_bosses_are_factions_that_make_boss_hulls() + { + Assert.Empty(RunStart.Generate(new MainGalaxy(), Inputs(() => 1)).BossZones); + + var maker = _cache.GetAll().OrderBy(faction => faction.Name, StringComparer.Ordinal).First(); + MakeBoss("Main Boss", 100, maker); + var galaxy = RunStart.Generate(new MainGalaxy(), Inputs(() => 1)); + var bossFaction = Assert.Single(galaxy.BossZones.Keys); + Assert.Equal(maker.Name, bossFaction.Name); + Assert.NotEmpty(LoadoutGenerator.BossHullsOf(_cache, bossFaction)); + } + [Fact] public void Boss_spawns_once_in_boss_zone() { @@ -152,10 +339,10 @@ public sealed partial class RunStartTests public void Exit_open_only_in_the_exit_zone() { var galaxy = RunStart.Generate(new MainGalaxy(), Inputs(() => 1)); - // The shipped catalog sets no BossHull outside the demo's antagonist, so the main galaxy places no boss: give a - // faction one and a boss zone that is not the exit, as the generator would for a faction with a boss hull. + // The shipped catalog has no Boss hull, so the main galaxy places no boss: give a faction one and a boss zone that is + // not the exit, as the generator would for a faction that makes a Boss hull. var faction = galaxy.Factions.First(); - faction.BossHull = _cache.RefOf(SoldShipHulls().First()); + MakeBoss("Gate Boss", 100, faction); var bossZone = galaxy.Zones.First(zone => zone != galaxy.Exit); galaxy.BossZones[faction] = bossZone; var zone = ZoneOf(galaxy, bossZone); diff --git a/tools/AetherDb/Program.cs b/tools/AetherDb/Program.cs index 500d85a8e..583e811ad 100644 --- a/tools/AetherDb/Program.cs +++ b/tools/AetherDb/Program.cs @@ -695,7 +695,8 @@ public static class Program } // Each faction's generation-critical links and the size of its curated place list. An empty list is not broken: - // Galaxy.NamePlaces falls back to catalogue designations for a faction without one. + // Galaxy.NamePlaces falls back to catalogue designations for a faction without one. The boss column is derived: + // the Boss hulls the faction makes (LoadoutGenerator.BossHullsOf), never a field on the faction. private static int Factions() { var db = AetherDb.Open(); @@ -704,7 +705,8 @@ public static class Program foreach (var faction in db.Cache.GetAll().OrderBy(f => f.Name)) { var key = db.Cache.RefOf(faction).Key; - var boss = "none"; + var bosses = LoadoutGenerator.BossHullsOf(db.Cache, faction); + var boss = bosses.Length == 0 ? "none" : string.Join(',', bosses.Select(b => b.hull.Name)); Console.WriteLine($"{faction.Name,-26} {faction.ShortName,-13} {faction.Places.Length,-22} {boss,-14} {faction.InfluenceDistance,-9} {(faction.Doctrine == null ? "none" : faction.Doctrine.EngageOn.ToString()),-10} " + products.Count(p => p.Manufacturer.Key.Equals(key))); } @@ -804,13 +806,18 @@ public static class Program string outcome; try { + // Generation never offers a Boss hull, so one is built through its first product. + var bossProduct = hull.Boss && hull.HullType == HullType.Ship + ? db.Cache.GetAll().Where(p => db.Cache.Get(p.Design) == hull).OrderBy(p => p.Name, StringComparer.Ordinal).FirstOrDefault() + : null; var pack = hull.HullType switch { + HullType.Ship when hull.Boss => bossProduct == null ? null : generator.ShipLoadout(bossProduct), HullType.Ship => generator.GenerateShipLoadout(candidate => candidate == hull), HullType.Turret => generator.GenerateTurretLoadout(), _ => generator.GenerateStationLoadout() }; - outcome = pack == null ? "NO LOADOUT (nothing suitable found)" : "ok"; + outcome = pack != null ? "ok" : hull.Boss && hull.HullType == HullType.Ship ? "NO LOADOUT (boss hull with no product)" : "NO LOADOUT (nothing suitable found)"; if (pack == null) failures++; if (pack != null && log.Any(l => l.Contains("targeting system available", StringComparison.OrdinalIgnoreCase))) targetingGaps++; }