diff --git a/Assets/Scripts/ServerShared/ShipAuthoring.cs b/Assets/Scripts/ServerShared/ShipAuthoring.cs index 1907a1dab..e9bf748cc 100644 --- a/Assets/Scripts/ServerShared/ShipAuthoring.cs +++ b/Assets/Scripts/ServerShared/ShipAuthoring.cs @@ -518,10 +518,12 @@ public static class ShipAuthoringStore throw new InvalidOperationException($"{ship.Id}: joint {onJoint.Key} carries more than one actuator ({lead.Id} and {other.Id}); a joint has one actuator."); } var driven = RigChains.Driven(ship); + // The root joint of the assembly a hardpoint belongs to, or null for a hardpoint outside every assembly. + string RootJointOf(HardpointData hardpoint) => Assemblies.Of(hull, hardpoint) is { } home + ? all.FirstOrDefault(drive => drive.Anchor.Role == "assembly-root" && drive.Anchor.Id == home.Id).Joint : null; foreach (var (joint, anchor) in all) { - var own = anchor.Role == "assembly-root" ? null - : Assemblies.Of(hull, mounts[anchor.Id]) is { } home ? all.FirstOrDefault(drive => drive.Anchor.Role == "assembly-root" && drive.Anchor.Id == home.Id).Joint : null; + var own = anchor.Role == "assembly-root" ? null : RootJointOf(mounts[anchor.Id]); var through = RigChains.Chain(ship, joint).SkipLast(1).FirstOrDefault(step => driven.Contains(step) && step != own); if (through != null) throw new InvalidOperationException($"{ship.Id}: joint {joint} reaches through joint {through}, which another actuator drives; a joint has one actuator."); @@ -529,9 +531,9 @@ public static class ShipAuthoringStore foreach (var (joint, anchor) in all.Where(drive => drive.Anchor.Role == "assembly-root")) { var assembly = hull.Assemblies.Single(candidate => candidate.Id == anchor.Id); - var reach = YawReach(ship, joint); - if (reach < assembly.Max - assembly.Min) - throw new InvalidOperationException($"{ship.Id}: joint {joint} chain reaches {reach} degrees of yaw, short of the {assembly.Max - assembly.Min} degree arc of assembly {assembly.Id}."); + var (low, high) = YawRange(ship, joint, null); + if (low > assembly.Min || high < assembly.Max) + throw new InvalidOperationException($"{ship.Id}: joint {joint} chain sweeps yaw {low} to {high}, short of the {assembly.Min} to {assembly.Max} arc of assembly {assembly.Id}."); } var drives = all.Where(drive => drive.Anchor.Role != "assembly-root").Select(drive => (drive.Joint, Hardpoint: mounts[drive.Anchor.Id])).ToArray(); @@ -545,7 +547,8 @@ public static class ShipAuthoringStore foreach (var (_, hardpoint) in group) if (hardpoint.Rotation != lead.Rotation || hardpoint.FiringArc != lead.FiringArc) throw new InvalidOperationException($"{ship.Id}: weapon hardpoints {lead.Transform} and {hardpoint.Transform} ride joint {group.Key} and must share Rotation and FiringArc."); - var reach = YawReach(ship, group.Key); + // A member's motion composes on top of its assembly's pose, so its reach is its own joints', never the root's. + var reach = YawReach(ship, group.Key, RootJointOf(lead)); if (reach < lead.FiringArc) throw new InvalidOperationException($"{ship.Id}: joint {group.Key} chain reaches {reach} degrees of yaw, short of the {lead.FiringArc} degree firing arc of {lead.Transform}."); } @@ -564,22 +567,32 @@ public static class ShipAuthoringStore foreach (var member in members) if (!tilted.Any(drive => drive.Hardpoint == member && drive.Joint == onJoint.Key)) throw new InvalidOperationException($"{ship.Id}: thruster hardpoint {member.Transform} is in the row of {lead.Transform} on joint {onJoint.Key} and must ride it too; a row rides one joint whole."); - var reach = YawReach(ship, onJoint.Key); + var reach = YawReach(ship, onJoint.Key, RootJointOf(lead)); if (reach < lead.FiringArc) throw new InvalidOperationException($"{ship.Id}: joint {onJoint.Key} chain reaches {reach} degrees of yaw, short of the {lead.FiringArc} degree gimbal arc of {lead.Transform}."); } } // The yaw a driven joint's chain can sweep: the summed range of every degree of freedom whose axis is yaw, capped at a turn. - private static float YawReach(ShipAuthoring ship, string jointId) + // With above set, the chain stops below that joint (an assembly's root): its yaw is the assembly's, not the member's. + private static float YawReach(ShipAuthoring ship, string jointId, string above) + { + var (low, high) = YawRange(ship, jointId, above); + return MathF.Min(high - low, 360f); + } + + // The lowest and highest yaw the chain below `above` (or the whole chain when null) can reach, from the authored pose. + private static (float Low, float High) YawRange(ShipAuthoring ship, string jointId, string above) { var joints = ship.Joints.ToDictionary(joint => joint.Id, StringComparer.Ordinal); - var reach = 0f; - foreach (var joint in RigChains.Chain(ship, jointId).Select(id => joints[id])) + var chain = RigChains.Chain(ship, jointId); + var (low, high) = (0f, 0f); + foreach (var joint in chain.Skip(above == null ? 0 : Array.IndexOf(chain, above) + 1).Select(id => joints[id])) for (var dof = 0; dof < joint.Min.Length; dof++) if (MathF.Abs(joint.Axes[3 * dof + 1]) >= YawAxisCos) - reach += joint.Max[dof] - joint.Min[dof]; - return MathF.Min(reach, 360f); + // An axis pointing down turns the other way, so its limits mirror. + (low, high) = joint.Axes[3 * dof + 1] > 0f ? (low + joint.Min[dof], high + joint.Max[dof]) : (low - joint.Max[dof], high - joint.Min[dof]); + return (low, high); } } diff --git a/tests/Aetheria.Shared.Tests/AssemblyTests.cs b/tests/Aetheria.Shared.Tests/AssemblyTests.cs index db316372c..ebf7e2cce 100644 --- a/tests/Aetheria.Shared.Tests/AssemblyTests.cs +++ b/tests/Aetheria.Shared.Tests/AssemblyTests.cs @@ -190,11 +190,110 @@ public sealed class AssemblyTests var ship = Fixture(); ship.Visual.Joints.Single(joint => joint.Id == "root").Min[0] = -60f; ship.Visual.Joints.Single(joint => joint.Id == "root").Max[0] = 59f; - Assert.Contains("joint root chain reaches 119 degrees of yaw, short of the 120 degree arc of assembly A", Refusal(ship)); + Assert.Contains("joint root chain sweeps yaw -60 to 59, short of the -60 to 60 arc of assembly A", Refusal(ship)); ship.Visual.Joints.Single(joint => joint.Id == "root").Max[0] = 60f; ship.Validate(); } + // The assembly's arc is a range, not a span: 0..90 is no 90 degrees of a joint that sweeps -45..45. + [Fact] + public void TheRootJointMustSweepTheArcRangeNotOnlyItsSpan() + { + var ship = Fixture(); + var root = ship.Visual.Joints.Single(joint => joint.Id == "root"); + (root.Min[0], root.Max[0]) = (-45f, 45f); + (Only(ship).Min, Only(ship).Max) = (-45f, 45f); + ship.Validate(); + (Only(ship).Min, Only(ship).Max) = (0f, 90f); + Assert.Contains("chain sweeps yaw -45 to 45, short of the 0 to 90 arc of assembly A", Refusal(ship)); + (Only(ship).Min, Only(ship).Max) = (-90f, 0f); + Assert.Contains("short of the -90 to 0 arc", Refusal(ship)); + // A joint whose axis points down turns the other way: its 0..90 is yaw -90..0, and its -90..0 is yaw 0..90. + root.Axes = new[] { 0f, -1f, 0f }; + (Only(ship).Min, Only(ship).Max) = (-90f, 0f); + (root.Min[0], root.Max[0]) = (0f, 90f); + ship.Validate(); + (root.Min[0], root.Max[0]) = (-90f, 0f); + Assert.Contains("chain sweeps yaw 0 to 90, short of the -90 to 0 arc", Refusal(ship)); + } + + // A member's motion composes on top of the assembly's pose: its own reach is its own joints', never the root's + // (Soul s1 P1 and P1b). + [Fact] + public void AMembersReachIsItsOwnJointsAndNotTheAssembliesYaw() + { + var gun = Fixture(); + var gj = gun.Visual.Joints.Single(joint => joint.Id == "gj"); + (gj.Min[0], gj.Max[0]) = (0f, 0f); + Assert.Contains("joint gj chain reaches 0 degrees of yaw, short of the 90 degree firing arc of g0", Refusal(gun)); + (gj.Min[0], gj.Max[0]) = (-45f, 44f); + Assert.Contains("reaches 89 degrees", Refusal(gun)); + (gj.Min[0], gj.Max[0]) = (-45f, 45f); + gun.Validate(); + + var row = Fixture(); + var tj = row.Visual.Joints.Single(joint => joint.Id == "tj"); + (tj.Min[0], tj.Max[0]) = (0f, 0f); + Assert.Contains("joint tj chain reaches 0 degrees of yaw, short of the 90 degree gimbal arc of t0", Refusal(row)); + } + + [Fact] + public void AssembliesMembershipNeedsEveryCellInTheRegion() + { + var ship = Fixture(); + var across = new HardpointData { Type = HardpointType.Thruster, Position = new int2(1, 0), Shape = new Shape(2, 1), Transform = "x" }; + across.Shape.Cells[0, 0] = across.Shape.Cells[1, 0] = true; + Assert.Null(Assemblies.Of(ship.Hull, across)); + } + + [Fact] + public void ASpanOverATurnIsRefusedHoweverLittle() + { + var ship = Fixture(); + (Only(ship).Min, Only(ship).Max) = (-180f, 180.25f); + Assert.Contains("at most 360", Refusal(ship)); + } + + [Fact] + public void EachAssemblyNeedsItsOwnRootAnchorAndItsOwnActuator() + { + // B's root is missing: A's root is no root of B. + var ship = Fixture(); + ship.Hull.Shape = new Shape(4, 1); + for (var x = 0; x < 4; x++) ship.Hull.Shape.Cells[x, 0] = true; + ship.Hull.Assemblies.Add(new AssemblyData { Id = "B", Position = new int2(3, 0), Shape = new Shape(), Min = -10f, Max = 10f, Slew = 5f }); + Assert.Contains("assembly B needs an assembly-root anchor of that id", Refusal(ship)); + + // Two roots on one joint are two actuators, and B's root may not hang under A's root joint. + ship.Visual.Anchors.Add(new ShipAnchor { Id = "B", Role = "assembly-root", ModelNodeId = "B-root", Joint = "root" }); + Assert.Contains("joint root carries more than one actuator (A and B)", Refusal(ship)); + ship.Visual.Joints.Add(Joint("bj", "root", -45f, 45f)); + ship.Visual.Anchors.Single(anchor => anchor.Id == "B").Joint = "bj"; + Assert.Contains("reaches through joint root", Refusal(ship)); + ship.Visual.Joints.Single(joint => joint.Id == "bj").Parent = null; + ship.Validate(); + } + + [Fact] + public void OnlyTheAssemblyRootRoleIsExemptFromTheMountRule() + { + foreach (var role in new[] { "weapon-mount", "thruster-emitter", "radiator-mesh" }) + { + var ship = Fixture(); + ship.Visual.Anchors.Add(new ShipAnchor { Id = "loose", Role = role, ModelNodeId = "loose" }); + Assert.Contains("must be the mount of a hardpoint", Refusal(ship)); + } + foreach (var role in new[] { "Assembly-Root", "assembly-root ", "assembly_root" }) + { + var ship = Fixture(); + ship.Visual.Anchors.Add(new ShipAnchor { Id = "loose", Role = role, ModelNodeId = "loose" }); + Assert.Contains("unknown role", Refusal(ship)); + } + var shared = Fixture(); + shared.Hull.Hardpoints.Add(new HardpointData { Type = HardpointType.Hull, Position = new int2(1, 0), Shape = new Shape(), Transform = "A" }); + Refusal(shared); + } + [Fact] public void AMembersChainMayPassItsOwnRootJointAndNoOtherDrivenJoint() { diff --git a/tests/Aetheria.Shared.Tests/ShipRigTests.cs b/tests/Aetheria.Shared.Tests/ShipRigTests.cs index 351fd288b..22a0a5d04 100644 --- a/tests/Aetheria.Shared.Tests/ShipRigTests.cs +++ b/tests/Aetheria.Shared.Tests/ShipRigTests.cs @@ -247,6 +247,31 @@ public sealed class ShipRigTests : IDisposable private static Node[] Reparent(string name, string parent) => RigNodes.Select(node => node.Name == name ? node with { Parent = parent } : node).ToArray(); + // The assembly "A": the gun's cell is its region, its root anchor "aroot" names joint arm, which sweeps the assembly's whole arc. + private static void WithAssembly(ShipParts ship) + { + var arm = ship.Visual.Joints.Single(joint => joint.Id == "arm"); + (arm.Min[0], arm.Max[0]) = (-90f, 90f); + var gun = ship.Hull.Hardpoints.Single(hardpoint => hardpoint.Transform == "gun"); + var region = new Shape(1, 1); + region.Cells[0, 0] = true; + ship.Hull.Assemblies.Add(new AssemblyData { Id = "A", Position = gun.Position, Shape = region, Min = -60f, Max = 60f, Slew = 30f }); + ship.Visual.Anchors.Add(new ShipAnchor { Id = "A", Role = "assembly-root", ModelNodeId = "aroot", Joint = "arm" }); + } + + private static readonly Node[] AssemblyNodes = RigNodes.Append(new Node("aroot", "aroot", "arm")).ToArray(); + + // Bind's nearest-joint rule applies to an assembly's root as it does to a mount (Soul s1 B1-B4). + [Fact] + public void BindHoldsAnAssemblyRootToTheNearestJointAboveItsNode() + { + ShipModCatalog.ReadPackage(Package(WithAssembly, AssemblyNodes)); + const string rule = "assembly root A must name the nearest joint above its model node as its Joint"; + Assert.Contains(rule, BindRefusal(Package(WithAssembly, AssemblyNodes.Select(node => node.Name == "aroot" ? node with { Parent = null } : node).ToArray()))); + Assert.Contains(rule, BindRefusal(Package(WithAssembly, AssemblyNodes.Select(node => node.Name == "aroot" ? node with { Parent = "elbow" } : node).ToArray()))); + Assert.Contains(rule, BindRefusal(Package(ship => { WithAssembly(ship); ship.Visual.Anchors.Single(anchor => anchor.Id == "A").Joint = "elbow"; }, AssemblyNodes))); + } + [Fact] public void BindAcceptsARigTheModelHasEvenWithAnUntaggedNodeBetween() {