diff --git a/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs b/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs index 300229d3f..5034b6460 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs @@ -200,6 +200,7 @@ public interface IPowerConsumer Union(50, typeof(ListenerData)), Union(51, typeof(ScannerData)), Union(52, typeof(LockWeaponData)), + Union(53, typeof(FacilityData)), JsonObject(MemberSerialization.OptIn)] public abstract class BehaviorData { diff --git a/Assets/Scripts/ServerShared/Behaviors/Facility.cs b/Assets/Scripts/ServerShared/Behaviors/Facility.cs new file mode 100644 index 000000000..85c9915b8 --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/Facility.cs @@ -0,0 +1,26 @@ +using MessagePack; +using Newtonsoft.Json; + +// The facility gear a station carries to run recipes: a GearData of HardpointType Facility whose Behaviors hold one of these. The type +// says where it mounts (a hull's Facility rows), this says it runs recipes. It holds no field of its own: which recipes a facility +// runs is derived from RecipeData.Facility, so no list is stored here. +[Inspectable, MessagePackObject, JsonObject(MemberSerialization.OptIn)] +public class FacilityData : BehaviorData +{ + public override Behavior CreateInstance(EquippedItem item) + { + return new Facility(this, item); + } + public override Behavior CreateInstance(ConsumableItemEffect item) + { + return new Facility(this, item); + } +} + +// The installed facility that will run its recipes (ruling demand-consumed-inputs-processed-by-facilities). It runs nothing until +// follow-up facility-production: it is seated, priced and carried, and that is all. +public class Facility : Behavior +{ + public Facility(FacilityData data, EquippedItem item) : base(data, item) { } + public Facility(FacilityData data, ConsumableItemEffect item) : base(data, item) { } +} diff --git a/Assets/Scripts/ServerShared/Behaviors/Facility.cs.meta b/Assets/Scripts/ServerShared/Behaviors/Facility.cs.meta new file mode 100644 index 000000000..f3f429e8c --- /dev/null +++ b/Assets/Scripts/ServerShared/Behaviors/Facility.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a65a21b40f2a4daba2b058018ba5e8d7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/ServerShared/Enums.cs b/Assets/Scripts/ServerShared/Enums.cs index 0a6449dc1..e42e956a5 100644 --- a/Assets/Scripts/ServerShared/Enums.cs +++ b/Assets/Scripts/ServerShared/Enums.cs @@ -49,7 +49,10 @@ public enum HardpointType Energy, Ballistic, Launcher, - ControlModule + ControlModule, + // A station facility mount: only a station hull carries one (StationKinds.HullKindFault). Internal: no anchor, nothing on the + // model. Filled only by a kind's Facilities; the random hardpoint fill never touches it. Appended: stored types keep their values. + Facility } public enum WeaponType diff --git a/Assets/Scripts/ServerShared/Recipes.cs b/Assets/Scripts/ServerShared/Recipes.cs index f9aa3ee4a..6b42b45fa 100644 --- a/Assets/Scripts/ServerShared/Recipes.cs +++ b/Assets/Scripts/ServerShared/Recipes.cs @@ -35,7 +35,8 @@ public class RecipeData [JsonProperty("upkeep"), Key(5)] public List Upkeep = new List(); - // The kind of station that runs this recipe. + // The kind of station that runs this recipe: the kind whose Facilities list Facility. A checked mirror of Facility, no longer an + // owner; follow-up recipe-station-kind-derived deletes it once every reader reads the facility. [JsonProperty("stationKind"), Key(6)] public CultRecordRef StationKind; @@ -52,6 +53,11 @@ public class RecipeData [JsonProperty("roleInputs"), Key(9)] public List RoleInputs = new List(); + // The facility gear design that runs this recipe: a GearData of HardpointType Facility holding a FacilityData. Records written + // before key 10 read it unset. + [JsonProperty("facility"), Key(10)] + public CultRecordRef Facility; + public static CultRecordKey KeyOf(CultRecordKey outputKey) => new CultRecordKey("recipe:" + outputKey.Value); } diff --git a/Assets/Scripts/ServerShared/StationKinds.cs b/Assets/Scripts/ServerShared/StationKinds.cs index eb2662eee..ffb6ffd51 100644 --- a/Assets/Scripts/ServerShared/StationKinds.cs +++ b/Assets/Scripts/ServerShared/StationKinds.cs @@ -23,6 +23,11 @@ public class StationKindData [JsonProperty("standIns"), Key(2)] public List StandIns = new List(); + // The facility gear a station of this kind is fitted with (ruling installed-gear-decides-what-a-station-buys). Records written + // before key 3 read it empty. + [JsonProperty("facilities"), Key(3)] + public List> Facilities = new List>(); + public static CultRecordKey KeyOf(string id) => new CultRecordKey("station-kind:" + id); } @@ -98,7 +103,8 @@ public static class StationKinds return standIn != null && standIn.Kind.IsSet() && OfferedKinds(catalog).Contains(standIn.Kind.Key.Value) ? standIn.Kind : default; } - // 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. + // What is wrong with a hull's station facts, or null: a Station hull names a kind the catalog holds, and no other hull names one + // or carries a Facility row. // The one rule, applied to the catalog on Open and to each mod package on Compose. public static string HullKindFault(CultCache catalog, HullData hull) { @@ -109,8 +115,13 @@ public static class StationKinds 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."; + else + { + if (hull.Kind.IsSet()) + return $"Hull '{hull.Name}' is a {hull.HullType} and names a Kind; only a station hull has one."; + if (hull.Hardpoints.Any(hardpoint => hardpoint.Type == HardpointType.Facility)) + return $"Hull '{hull.Name}' is a {hull.HullType} and has a Facility row; only a station hull takes facilities."; + } return null; } diff --git a/tests/Aetheria.Shared.Tests/RecipeSchemaTests.cs b/tests/Aetheria.Shared.Tests/RecipeSchemaTests.cs index 62b311d22..2fb3009b2 100644 --- a/tests/Aetheria.Shared.Tests/RecipeSchemaTests.cs +++ b/tests/Aetheria.Shared.Tests/RecipeSchemaTests.cs @@ -32,6 +32,40 @@ public sealed class RecipeSchemaTests Assert.Equal(4, counted.Count); } + // A record keeps the slots it was written with: one cut short of a later key reads that member as it starts out. + private static byte[] CutShort(byte[] record, int slots) + { + var reader = new MessagePackReader(record); + var count = reader.ReadArrayHeader(); + Assert.True(count > slots); + var start = reader.Consumed; + for (var i = 0; i < slots; i++) reader.Skip(); + var writer = new System.Buffers.ArrayBufferWriter(); + var write = new MessagePackWriter(writer); + write.WriteArrayHeader(slots); + write.Flush(); + return writer.WrittenSpan.ToArray().Concat(record.Skip((int) start).Take((int) (reader.Consumed - start))).ToArray(); + } + + [Fact] + public void RecordsWrittenBeforeFacilityKeysReadUnsetAndEmpty() + { + var options = GameCult.Caching.MessagePack.CultDocumentMessagePackSerialization.OptionsFor(typeof(RecipeData).Assembly); + var item = new CultRecordRef(new CultRecordKey("gear:mill-works")); + var recipe = new RecipeData { Output = item, Facility = item }; + var kind = new StationKindData { Name = "Mill", Facilities = new System.Collections.Generic.List> { item } }; + var current = MessagePackSerializer.Deserialize(MessagePackSerializer.Serialize(recipe, options), options); + Assert.Equal("gear:mill-works", current.Facility.Key.Value); + Assert.Equal("gear:mill-works", MessagePackSerializer.Deserialize(MessagePackSerializer.Serialize(kind, options), options).Facilities.Single().Key.Value); + + var oldRecipe = MessagePackSerializer.Deserialize(CutShort(MessagePackSerializer.Serialize(recipe, options), 10), options); + Assert.False(oldRecipe.Facility.IsSet()); + Assert.Equal("gear:mill-works", oldRecipe.Output.Key.Value); + var oldKind = MessagePackSerializer.Deserialize(CutShort(MessagePackSerializer.Serialize(kind, options), 3), options); + Assert.Empty(oldKind.Facilities); + Assert.Equal("Mill", oldKind.Name); + } + [Fact] public void RoleCountSurvivesTheStore() { diff --git a/tests/Aetheria.Shared.Tests/ShipAuthoringTests.cs b/tests/Aetheria.Shared.Tests/ShipAuthoringTests.cs index 7ed2b84be..af531bd6b 100644 --- a/tests/Aetheria.Shared.Tests/ShipAuthoringTests.cs +++ b/tests/Aetheria.Shared.Tests/ShipAuthoringTests.cs @@ -287,7 +287,7 @@ public sealed class ShipAuthoringTests [Theory] [InlineData(HardpointType.Hull), InlineData(HardpointType.Tool), InlineData(HardpointType.Thermal), InlineData(HardpointType.WarpDrive), InlineData(HardpointType.Reactor), InlineData(HardpointType.Shield), InlineData(HardpointType.Sensors), - InlineData(HardpointType.ControlModule)] + InlineData(HardpointType.ControlModule), InlineData(HardpointType.Facility)] public void AnInternalHardpointCarriesNoAnchor(HardpointType type) { WithOnly(type).Validate(); diff --git a/tests/Aetheria.Shared.Tests/StationKindTests.cs b/tests/Aetheria.Shared.Tests/StationKindTests.cs index 777f4fb92..05ea64da3 100644 --- a/tests/Aetheria.Shared.Tests/StationKindTests.cs +++ b/tests/Aetheria.Shared.Tests/StationKindTests.cs @@ -176,6 +176,27 @@ public sealed class StationKindTests : IDisposable Assert.Contains("only a station hull has one", turretWithKind); } + // station-facilities-on-typed-mounts: only a station hull carries a Facility row. + [Fact] + public void OpenRefusesAFacilityRowOnAnythingButAStationHull() + { + HullData WithFacility(HullType type, CultRecordRef kind = default) + { + var hull = Hull(type, kind); + hull.Hardpoints.Add(new HardpointData { Type = HardpointType.Facility, Position = new int2(0, 0), Shape = new Shape(2, 2), Transform = "Fa.1" }); + return hull; + } + foreach (var type in new[] { HullType.Ship, HullType.Turret }) + Assert.Contains("only a station hull takes facilities", OpenRefusal(cache => cache.Upsert(WithFacility(type)))); + SeedAndReopen(cache => + { + var kind = new CultRecordRef(cache.Upsert(new StationKindData { Name = "Hub" }).Key); + cache.Upsert(WithFacility(HullType.Station, kind)); + }, out var path); + using var opened = AetheriaStores.Open(path); + Assert.Contains(opened.GetAll(), hull => hull.Hardpoints.Any(row => row.Type == HardpointType.Facility)); + } + [Fact] public void OpenRefusesBadStandIn() { diff --git a/tools/blender/aetheria_ships/ship_cc.py b/tools/blender/aetheria_ships/ship_cc.py index b7dca307c..8b010e79b 100644 --- a/tools/blender/aetheria_ships/ship_cc.py +++ b/tools/blender/aetheria_ships/ship_cc.py @@ -50,7 +50,7 @@ HARDPOINT_MEMBERS = ("Type", "Position", "Shape", "Transform", "Rotation", "Armo ANCHOR_MEMBERS = ("Id", "Role", "ModelNodeId", "ParentId", "Order") # Enum member names in value order (HardpointType, ItemRotation, HullType). HARDPOINT_TYPE_NAMES = ("Hull", "Tool", "Thermal", "Thruster", "WarpDrive", "Reactor", "Radiator", - "Shield", "Sensors", "Energy", "Ballistic", "Launcher", "ControlModule") + "Shield", "Sensors", "Energy", "Ballistic", "Launcher", "ControlModule", "Facility") ROTATION_NAMES = ("None", "CounterClockwise", "Reversed", "Clockwise") HULL_TYPE_NAMES = ("Ship", "Station", "Turret") diff --git a/tools/blender/package_hull.md b/tools/blender/package_hull.md index b537da082..ebb9c5b3d 100644 --- a/tools/blender/package_hull.md +++ b/tools/blender/package_hull.md @@ -38,7 +38,8 @@ A row has all of `id`, `kind`, `cell`, `footprint`, `rotation`, `armor`, `arc`: - `id` is the mount id the model's anchor binds to (`aetheria.id`); unique in the manifest. - `kind` is one of `ship_cc.HARDPOINT_TYPE_NAMES` (Hull, Tool, Thermal, Thruster, WarpDrive, Reactor, Radiator, Shield, Sensors, - Energy, Ballistic, Launcher, ControlModule). The packager keeps no list of its own. + Energy, Ballistic, Launcher, ControlModule, Facility). The packager keeps no list of its own. A `Facility` row is internal (no + anchor, nothing on the model) and only a Station hull takes one. - `cell` is `[x, y]`, whole numbers of at least 0: the footprint's lowest-x, lowest-y cell on the raster. `x` runs across, `y` from the stern (0) toward the bow, as the layout panel shows it. Cells are in schematic space, not model space. - `footprint` is rows of `#` (the mount's cells) and `.` separated by `/`, all one width, at least one `#`. The first row is the top