From 9c60e46ea432c4b8ca83df280ef8adc2140cb632 Mon Sep 17 00:00:00 2001 From: soul Date: Sat, 28 Nov 2026 02:23:29 +0100 Subject: [PATCH] station-kinds facilities: the one writer of a shipped hull's Facility rows AetherDb station-kinds facilities "" ,:... [apply] replaces a station hull's Facility rows with exactly the rows given (Fa.1, Fa.2...), keeps every other row, refuses a hull that is not a station, a malformed row, a cell outside the interior and an overlap, and prints the designs that take each row before and after. Footprint and ParseFootprint are shared with ship-authoring push. Co-Authored-By: Claude Sonnet 5.5 --- .../StationKindsCatalogTests.cs | 75 ++++++++++++++++ tools/AetherDb/Program.cs | 8 +- tools/AetherDb/ShipAuthoringPush.cs | 4 +- tools/AetherDb/StationKindsCatalog.cs | 86 +++++++++++++++++++ 4 files changed, 170 insertions(+), 3 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/StationKindsCatalogTests.cs b/tests/Aetheria.Shared.Tests/StationKindsCatalogTests.cs index 517a6ddc6..cc926afa6 100644 --- a/tests/Aetheria.Shared.Tests/StationKindsCatalogTests.cs +++ b/tests/Aetheria.Shared.Tests/StationKindsCatalogTests.cs @@ -126,6 +126,81 @@ public sealed class StationKindsCatalogTests : IDisposable Assert.False(db.Cache.GetAll().Single(h => h.Name == "Cutter").Kind.IsSet()); } + // A writable catalog holding a solid 6x6 station hull (interior cells 1..4) with one Reactor row, a Ship hull, and a facility design. + private CultCache HubCatalog(out CultRecordKey hubKey) + { + var cache = AetheriaStores.Open(Path.Combine(_root, "hub" + Guid.NewGuid().ToString("N") + ".cc"), catalogWritable: true); + cache.Upsert(new VerseGrammar { Revision = 1 }); + cache.Upsert(new InputLayout { Rows = new InputLayoutRow[] { new InputLayoutRowSpacer { Height = 1 } } }); + var kind = new CultRecordRef(cache.Upsert(new StationKindData { Name = "Hub" }).Key); + var shape = new Shape(6, 6); + foreach (var cell in shape.AllCoordinates) shape[cell] = true; + var reactor = new Shape(2, 2); + foreach (var cell in reactor.AllCoordinates) reactor[cell] = true; + hubKey = cache.Upsert(new HullData + { + Name = "Hub", HullType = HullType.Station, Kind = kind, Shape = shape, Price = 1, + Hardpoints = { new HardpointData { Type = HardpointType.Reactor, Position = new CultMath.int2(1, 1), Shape = reactor, Transform = "R.1", Armor = 2 } } + }).Key; + cache.Upsert(new HullData { Name = "Cutter", HullType = HullType.Ship, Shape = shape, Price = 1 }); + cache.Upsert(new GearData { Name = "Works", Hardpoint = HardpointType.Facility, Shape = reactor, Price = 1, Behaviors = { new FacilityData() } }); + return cache; + } + + private static string Rows(HullData hull) => + string.Join(" ", hull.Hardpoints.Select(row => $"{row.Transform}:{row.Type}@{row.Position.x},{row.Position.y}:{row.Shape.Width}x{row.Shape.Height}:{row.Armor}")); + + [Fact] + public void SetFacilityRowsSetsRowsAndARerunWritesNothing() + { + using var cache = HubCatalog(out var key); + var before = Rows(cache.Get(key)); + var dry = new StringWriter(); + Assert.Equal(0, StationKindsCatalog.SetFacilityRows(cache, "Hub", new[] { "3,3:##/##", "1,3:#/#" }, false, dry)); + Assert.Contains("Dry run", dry.ToString()); + Assert.Contains("Works", dry.ToString()); + Assert.Equal(before, Rows(cache.Get(key))); + Assert.Equal(0, StationKindsCatalog.SetFacilityRows(cache, "Hub", new[] { "3,3:##/##", "1,3:#/#" }, true, new StringWriter())); + var after = Rows(cache.Get(key)); + Assert.Equal(before + " Fa.1:Facility@3,3:2x2:0 Fa.2:Facility@1,3:1x2:0", after); + // The facility design takes the 2x2 row and no other. + var set = cache.Get(key).Hardpoints.Where(row => row.Type == HardpointType.Facility).ToArray(); + Assert.True(set[0].Takes(cache.GetByName("Works"))); + Assert.False(set[1].Takes(cache.GetByName("Works"))); + var stored = cache.GetStored(key).StoredAt; + var again = new StringWriter(); + Assert.Equal(0, StationKindsCatalog.SetFacilityRows(cache, "Hub", new[] { "3,3:##/##", "1,3:#/#" }, true, again)); + Assert.Contains("No change", again.ToString()); + Assert.Equal(stored, cache.GetStored(key).StoredAt); + // A call with no rows clears the Facility rows and keeps every other row as it was. + Assert.Equal(0, StationKindsCatalog.SetFacilityRows(cache, "Hub", new string[0], true, new StringWriter())); + Assert.Equal(before, Rows(cache.Get(key))); + } + + [Fact] + public void SetFacilityRowsRefusesWhatItCannotPlaceAndLeavesTheRowsEqual() + { + using var cache = HubCatalog(out var key); + Assert.Equal(0, StationKindsCatalog.SetFacilityRows(cache, "Hub", new[] { "3,3:##/##" }, true, new StringWriter())); + var before = Rows(cache.Get(key)); + string Refusal(string hull, params string[] rows) + { + var output = new StringWriter(); + Assert.Equal(1, StationKindsCatalog.SetFacilityRows(cache, hull, rows, true, output)); + Assert.Equal(before, Rows(cache.Get(key))); + return output.ToString(); + } + Assert.Contains("only a station hull", Refusal("Cutter", "1,1:#")); + Assert.Contains("exactly one is required", Refusal("Missing", "1,1:#")); + Assert.Contains("outside Hub's interior", Refusal("Hub", "4,4:##/##")); + Assert.Contains("outside Hub's interior", Refusal("Hub", "0,0:#")); + Assert.Contains("overlaps another row", Refusal("Hub", "2,2:#")); + Assert.Contains("overlaps another row", Refusal("Hub", "3,3:#", "3,3:#")); + Assert.Contains("is not a row", Refusal("Hub", "3,3")); + Assert.Contains("is not a row", Refusal("Hub", "-1,3:#")); + Assert.Contains("rows of # and .", Refusal("Hub", "3,3:#x/##")); + } + [Fact] public void DryRunWritesNothingApplyIsIdempotent() { diff --git a/tools/AetherDb/Program.cs b/tools/AetherDb/Program.cs index ff5fdaac6..49c335e88 100644 --- a/tools/AetherDb/Program.cs +++ b/tools/AetherDb/Program.cs @@ -82,6 +82,12 @@ public static class Program if (args.Length != (apply ? 5 : 4)) { Console.WriteLine("station-kinds hull \"\" [apply]"); return 1; } code = StationKindsCatalog.SetHullKind(db.Cache, args[2], args[3], apply, Console.Out); } + else if (args.Length > 1 && args[1] == "facilities") + { + var words = args.Skip(2).Where(a => a != "apply").ToArray(); + if (words.Length < 1) { Console.WriteLine(StationKindsCatalog.FacilitiesUsage); return 1; } + code = StationKindsCatalog.SetFacilityRows(db.Cache, words[0], words.Skip(1).ToArray(), apply, Console.Out); + } else code = StationKindsCatalog.Run(db.Cache, apply, Console.Out); if (apply) db.Cache.FlushAsync().GetAwaiter().GetResult(); return code; @@ -97,7 +103,7 @@ public static class Program case "auto-guns": return AutoGunsCatalog.Run(AetherDb.Open(catalogWritable: args.Contains("apply")).Cache, args.Contains("apply"), Console.Out); case "doctrine-catalog": return DoctrineCatalog.Run(AetherDb.Open(catalogWritable: args.Contains("apply")).Cache, args.Contains("apply"), Console.Out); default: - Console.WriteLine("commands: census, factions, station-fit, hardpoint-fit, hardpoint-rotation \"\" , [apply], loadout [seed], loadout-commit [,,,[@]]... [--group ,...]... [--replace] [--boss-of ], save, settings, settings-dump, dangling [clear ]... [apply], shield-migrate [apply], brownout-migrate [apply], roles-migrate [apply], firing-arc-migrate [apply], range-falloff [apply] [--effective ] [--end ], targeting-catalog [apply], targeting-catalog-6c [apply], targeting-catalog-6d [apply], ship-authoring create|inspect|validate|compose, mine-launcher [apply], smoke-hull [apply], smoke-rounds [apply], roles-backfill [apply], pd-gear [apply], consumables-first-set [apply], launch-suppressor [apply], pirates-faction [apply], verse-grammar [apply], generic-designs [apply], sensor-stat-set [apply], inspection-scanner [apply], minor-power-factions [apply], place-names [apply], product-lines weapons|minor-powers|gear|tiers [apply], doctrine-catalog [apply], auto-guns [apply], assembly-from-maker [apply], shield-catalog [apply], station-kinds [hull \"\" ] [apply], economy report|prices|author ... [apply]|balance"); + Console.WriteLine("commands: census, factions, station-fit, hardpoint-fit, hardpoint-rotation \"\" , [apply], loadout [seed], loadout-commit [,,,[@]]... [--group ,...]... [--replace] [--boss-of ], save, settings, settings-dump, dangling [clear ]... [apply], shield-migrate [apply], brownout-migrate [apply], roles-migrate [apply], firing-arc-migrate [apply], range-falloff [apply] [--effective ] [--end ], targeting-catalog [apply], targeting-catalog-6c [apply], targeting-catalog-6d [apply], ship-authoring create|inspect|validate|compose, mine-launcher [apply], smoke-hull [apply], smoke-rounds [apply], roles-backfill [apply], pd-gear [apply], consumables-first-set [apply], launch-suppressor [apply], pirates-faction [apply], verse-grammar [apply], generic-designs [apply], sensor-stat-set [apply], inspection-scanner [apply], minor-power-factions [apply], place-names [apply], product-lines weapons|minor-powers|gear|tiers [apply], doctrine-catalog [apply], auto-guns [apply], assembly-from-maker [apply], shield-catalog [apply], station-kinds [hull \"\" | facilities \"\" ,:...] [apply], economy report|prices|author ... [apply]|balance"); return 1; } } diff --git a/tools/AetherDb/ShipAuthoringPush.cs b/tools/AetherDb/ShipAuthoringPush.cs index 88ac07179..9f85de9be 100644 --- a/tools/AetherDb/ShipAuthoringPush.cs +++ b/tools/AetherDb/ShipAuthoringPush.cs @@ -493,7 +493,7 @@ public static class ShipAuthoringPush }; // The footprint text of a shape: rows of # and . joined by /, the top row highest y. - private static string Footprint(Shape shape) + internal static string Footprint(Shape shape) { var rows = new string[shape.Height]; for (var y = 0; y < shape.Height; y++) @@ -501,7 +501,7 @@ public static class ShipAuthoringPush return string.Join("/", rows); } - private static Shape ParseFootprint(string text, string label) + internal static Shape ParseFootprint(string text, string label) { var rows = text.Split('/'); var width = rows[0].Length; diff --git a/tools/AetherDb/StationKindsCatalog.cs b/tools/AetherDb/StationKindsCatalog.cs index 781a42abd..8a643444b 100644 --- a/tools/AetherDb/StationKindsCatalog.cs +++ b/tools/AetherDb/StationKindsCatalog.cs @@ -1,7 +1,10 @@ using System; using System.IO; +using System.Collections.Generic; +using System.Globalization; using System.Linq; using GameCult.Caching; +using int2 = CultMath.int2; // Authors the catalog's one list of station kinds (aetheria-living-world, cut station-kind-datum) and gives Zenith, the // one station hull in the repo, its ruled kind (ruling zenith-is-a-trade-hub: provisional, one edit here to change). @@ -102,4 +105,87 @@ public static class StationKindsCatalog output.WriteLine($"\nSet {hull.Name}'s kind in Aetheria.cc"); return 0; } + + public const string FacilitiesUsage = "station-kinds facilities \"\" ,:... [apply]"; + + // Sets a shipped station hull's whole set of Facility rows (ruling station-facilities-on-typed-mounts): the one writer of a + // shipped hull's Facility rows (a package's go through ship-authoring push). A row is ,:, the footprint as + // package_hull.md writes it (rows of # and . joined by /, the top row highest y), the cell its lowest-x, lowest-y cell. The + // rows replace the hull's Facility rows exactly, mount ids Fa.1, Fa.2... in argument order; every other row is kept untouched. + // Refuses a hull that is not a Station, a malformed row, a cell outside the interior and a row over another row. Prints the + // designs that take each Facility row before and after. Rows equal to the hull's write nothing. Dry run unless apply. + public static int SetFacilityRows(CultCache cache, string hullName, string[] rows, bool apply, TextWriter output) + { + var named = cache.GetAll().Where(h => h.Name == hullName).ToArray(); + if (named.Length != 1) + { + output.WriteLine($"The catalog holds {named.Length} hulls named '{hullName}'; exactly one is required."); + return 1; + } + var hull = named[0]; + if (hull.HullType != HullType.Station) + { + output.WriteLine($"Hull '{hull.Name}' is a {hull.HullType}; only a station hull takes facilities."); + return 1; + } + var wanted = new List(); + var claimed = new HashSet<(int, int)>(); + foreach (var other in hull.Hardpoints.Where(row => row.Type != HardpointType.Facility)) + foreach (var cell in other.Shape.Coordinates) claimed.Add((other.Position.x + cell.x, other.Position.y + cell.y)); + foreach (var text in rows) + { + var colon = text.IndexOf(':'); + var at = colon < 0 ? null : text.Substring(0, colon).Split(','); + if (at == null || at.Length != 2 || !int.TryParse(at[0], NumberStyles.None, CultureInfo.InvariantCulture, out var x) || !int.TryParse(at[1], NumberStyles.None, CultureInfo.InvariantCulture, out var y)) + { + output.WriteLine($"'{text}' is not a row: {FacilitiesUsage}"); + return 1; + } + Shape shape; + try { shape = ShipAuthoringPush.ParseFootprint(text.Substring(colon + 1), $"Row '{text}'"); } + catch (InvalidOperationException e) + { + output.WriteLine(e.Message); + return 1; + } + foreach (var cell in shape.Coordinates) + { + var taken = (x + cell.x, y + cell.y); + if (x + cell.x >= hull.Shape.Width || y + cell.y >= hull.Shape.Height || !hull.InteriorCells[new int2(taken.Item1, taken.Item2)]) + { + output.WriteLine($"Row '{text}' puts cell ({taken.Item1},{taken.Item2}) outside {hull.Name}'s interior."); + return 1; + } + if (!claimed.Add(taken)) + { + output.WriteLine($"Row '{text}' overlaps another row at cell ({taken.Item1},{taken.Item2})."); + return 1; + } + } + wanted.Add(new HardpointData { Type = HardpointType.Facility, Position = new int2(x, y), Shape = shape, Transform = "Fa." + (wanted.Count + 1) }); + } + var held = hull.Hardpoints.Where(row => row.Type == HardpointType.Facility).ToArray(); + var designs = cache.GetAll().ToArray(); + string Describe(HardpointData row) => $"{row.Transform} ({row.Position.x},{row.Position.y}) {ShipAuthoringPush.Footprint(row.Shape)} [{string.Join(", ", designs.Where(row.Takes).Select(d => d.Name).OrderBy(n => n, StringComparer.Ordinal))}]"; + foreach (var row in held) output.WriteLine($"{hull.Name} held {Describe(row)}"); + foreach (var row in wanted) output.WriteLine($"{hull.Name} sets {Describe(row)}"); + if (held.Length == 0 && wanted.Count == 0 || held.Length == wanted.Count && held.Zip(wanted, (a, b) => Same(a, b)).All(same => same)) + { + output.WriteLine("No change."); + return 0; + } + if (!apply) + { + output.WriteLine("\nDry run. Pass \"apply\" to set them."); + return 0; + } + hull.Hardpoints = hull.Hardpoints.Where(row => row.Type != HardpointType.Facility).Concat(wanted).ToList(); + cache.Upsert(hull); + output.WriteLine($"\nSet {hull.Name}'s Facility rows in Aetheria.cc"); + return 0; + } + + private static bool Same(HardpointData a, HardpointData b) => + a.Transform == b.Transform && a.Position.x == b.Position.x && a.Position.y == b.Position.y && a.Rotation == b.Rotation && a.Armor == b.Armor + && a.FiringArc == b.FiringArc && ShipAuthoringPush.Footprint(a.Shape) == ShipAuthoringPush.Footprint(b.Shape); }