From 1b0d085dae9bdc8b738af0b633746be7c2e13654 Mon Sep 17 00:00:00 2001 From: soul Date: Tue, 1 Dec 2026 00:28:56 +0100 Subject: [PATCH] Item facing has one owner: EquippedItem.Orient Behavior.Direction, FireControl.MountDirection and Thruster's column and torque derive an item's facing through Orient instead of each rotating by EquippableItem.Rotation. Thruster.Torque is a live read over a cached lever direction. Behaviour-neutral; ItemFacingOwnerTests compares bit for bit with the base expressions and passes unchanged on the base commit. Co-Authored-By: Claude Sonnet 5.5 --- .../ServerShared/Behaviors/Behaviors.cs | 2 +- .../ServerShared/Behaviors/Thruster.cs | 11 +- Assets/Scripts/ServerShared/Entity.cs | 7 + Assets/Scripts/ServerShared/FireControl.cs | 2 +- .../ItemFacingOwnerTests.cs | 139 ++++++++++++++++++ 5 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 tests/Aetheria.Shared.Tests/ItemFacingOwnerTests.cs diff --git a/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs b/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs index ad8f6dbef..61c5a2dcd 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Behaviors.cs @@ -28,7 +28,7 @@ public abstract class Behavior { if(Item != null) { - var itemDirection = Entity.Direction.Rotate(Item.EquippableItem.Rotation); + var itemDirection = Item.Orient(Entity.Direction); return float3(itemDirection.x, 0, itemDirection.y); } diff --git a/Assets/Scripts/ServerShared/Behaviors/Thruster.cs b/Assets/Scripts/ServerShared/Behaviors/Thruster.cs index 98befbde2..594414a2c 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Thruster.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Thruster.cs @@ -49,7 +49,11 @@ public class Thruster : Behavior, IPowerConsumer // Thrust at a full grant, the same stat read without the power-supply term: what a column promises. Live, like Thrust. public float NominalThrust => EvaluateNominalPower(_data.Thrust); // GameplaySettings.ThrustScale scales the push alone: it tunes how hard ships fight gravity, and turning does not. - public float Torque { get; } + public float Torque => Item == null ? 0f : -dot(_lever, Item.Orient(float2(1, 0))); + + // Unit vector from the thruster to the hull's centre of mass: placement, fixed at construction. Which way the + // thruster faces is not cached with it; Torque reads that live from EquippedItem.Orient. + private readonly float2 _lever; // This thruster's effect per unit throttle at the given thrust, in the body frame: xy is the push (x starboard, y // forward), z the clockwise yaw rate in rad/s. Zero when the item is absent or offline. The one geometry owner for @@ -57,7 +61,7 @@ public class Thruster : Behavior, IPowerConsumer public float3 Column(float thrust) { if (Item == null || !Item.Active.Value) return default; - var push = -float2(0, 1).Rotate(Item.EquippableItem.Rotation) * thrust * ItemManager.GameplaySettings.ThrustScale / Entity.Mass; + var push = -Item.Orient(float2(0, 1)) * thrust * ItemManager.GameplaySettings.ThrustScale / Entity.Mass; return float3(push.x, push.y, Torque * thrust * ItemManager.GameplaySettings.TorqueMultiplier / Entity.Mass); } @@ -85,13 +89,12 @@ public class Thruster : Behavior, IPowerConsumer var itemData = ItemManager.GetData(item.EquippableItem); var itemCenter = hullData.Shape.Inset(itemData.Shape, item.Position, item.EquippableItem.Rotation).CenterOfMass; var toCenter = hullCenter - itemCenter; - Torque = -dot(normalize(toCenter), float2(1, 0).Rotate(item.EquippableItem.Rotation)); + _lever = normalize(toCenter); } public Thruster(ThrusterData data, ConsumableItemEffect item) : base(data, item) { _data = data; - Torque = 0; } // Cut 3 (docs/stats-and-power-cut.md): the resolved stat times the behaviour-supplied throttle scalar (ยง1.2), diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index e51f7aae8..e88d1c4ad 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -2178,6 +2178,13 @@ public class EquippedItem : IStatContext // null for Tool gear and the hull. The item's origin cell need not lie in it: Entity.Hardpoints[,] is a // per-cell map for armour and display, never an item lookup. public HardpointData Hardpoint { get; } + + // The one owner of an item's facing in the sim: a vector in the body frame, expressed in the item's frame. + // Today that is the item's authored rotation; an articulated pod's pose composes here and nowhere else + // (ruling aetheria-release:ruling:articulated-pods-share-one-frame). Behavior.Direction, FireControl.MountDirection + // and Thruster's column and torque all derive facing through this. + public float2 Orient(float2 bodyVector) => bodyVector.Rotate(EquippableItem.Rotation); + public EquippableItemData Data { get; } public Lot Lot { get; } diff --git a/Assets/Scripts/ServerShared/FireControl.cs b/Assets/Scripts/ServerShared/FireControl.cs index 80ca9df2b..c02e7391a 100644 --- a/Assets/Scripts/ServerShared/FireControl.cs +++ b/Assets/Scripts/ServerShared/FireControl.cs @@ -28,7 +28,7 @@ public static class FireControl // HardpointTransforms readback branch is deleted. public static float3 MountDirection(EquippedItem item) { - var itemDirection = item.Entity.Direction.Rotate(item.EquippableItem.Rotation); + var itemDirection = item.Orient(item.Entity.Direction); return normalize(float3(itemDirection.x, 0, itemDirection.y)); } diff --git a/tests/Aetheria.Shared.Tests/ItemFacingOwnerTests.cs b/tests/Aetheria.Shared.Tests/ItemFacingOwnerTests.cs new file mode 100644 index 000000000..10139a15d --- /dev/null +++ b/tests/Aetheria.Shared.Tests/ItemFacingOwnerTests.cs @@ -0,0 +1,139 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using CultMath; +using GameCult.Caching; +using Xunit; +using static CultMath.math; + +// Cut item-facing-owner: EquippedItem.Orient is the one derivation of an item's facing. Behaviour-neutral, so every +// expectation below is the pre-cut expression written inline (rotate the body vector by the item's authored +// rotation; torque from the lever to the hull's centre of mass) and compared bit for bit. The file compiles and +// passes unchanged against the base commit, which is how the before/after equality was taken. +public sealed class ItemFacingOwnerTests : IDisposable +{ + private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-itemfacing-" + Guid.NewGuid().ToString("N")); + private string Catalog => Path.Combine(_root, "Aetheria.cc"); + + public ItemFacingOwnerTests() => Directory.CreateDirectory(_root); + public void Dispose() => Directory.Delete(_root, true); + + private static PerformanceStat Constant(float v) => new PerformanceStat { Min = v, Max = v }; + + private static EquippableItem Mint(CultCache cache, ItemManager items, ItemData design, ItemRotation rotation = ItemRotation.None) => new EquippableItem + { + Data = cache.RefOf(design), + Rotation = rotation, + Durability = design is EquippableItemData equippable ? equippable.Durability : 0, + Lot = items.Lots.Add(new Lot { Design = cache.RefOf(design), Origin = new Attributed(), Quality = .5f, Roles = new List() }) + }; + + private static Shape Solid(int w, int h) + { + var shape = new Shape(w, h); + foreach (var cell in shape.AllCoordinates) shape[cell] = true; + return shape; + } + + private static readonly ItemRotation[] Rotations = + { ItemRotation.None, ItemRotation.CounterClockwise, ItemRotation.Reversed, ItemRotation.Clockwise }; + + // A 7x7 hull, a reactor, and one off-centre 1x1 thruster per rotation, so every column has a torque. + private Ship Build(CultCache cache, out ItemManager items, out HullData hullData) + { + items = new ItemManager(1u, cache, new ProvenanceLedger(), + new GameplaySettings + { + DefaultEntitySettings = new EntitySettings(), + Tiers = new[] { new RarityTier { Name = "Common", Quality = .5f, Color = new float3(1, 1, 1) } }, + QualityPriceModifier = new ExponentialLerp(), + ThrustScale = 1.3f, TorqueMultiplier = 1.7f, FiringArc = 110 + }, _ => { }); + var zone = new Zone(items, new PlanetSettings(), new ZonePack(), new GalaxyZone { Name = "Test Zone", Owner = null }, null); + hullData = cache.GetByName("Skiff"); + var ship = new Ship(items, zone, Mint(cache, items, hullData), new EntitySettings()); + Assert.True(ship.TryEquip(Mint(cache, items, cache.GetByName("Reactor")))); + var cells = new[] { int2(3, 1), int2(5, 2), int2(2, 5), int2(4, 4) }; + for (var i = 0; i < Rotations.Length; i++) + Assert.True(ship.TryEquip(Mint(cache, items, cache.GetByName("Drive"), Rotations[i]), cells[i]), $"drive {i}"); + zone.Admit(ship, piloted: false); + foreach (var item in ship.Equipment) item.UpdatePerformance(); + ship.Update(.1f); + return ship; + } + + private CultCache OpenCatalog() + { + var cache = AetheriaStores.Open(Catalog, catalogWritable: true); + cache.Upsert(new VerseGrammar { Revision = 1 }); + cache.Upsert(new HullData { Name = "Skiff", HullType = HullType.Ship, Shape = Solid(7, 7), Durability = 10, Mass = 1000 }); + cache.Upsert(new GearData + { + Name = "Reactor", Hardpoint = HardpointType.Tool, Shape = new Shape(), Durability = 10, + MinimumTemperature = 0, MaximumTemperature = 1000, OptimalTemperature = 280, PlateauWidth = 400, + Behaviors = { new ReactorData { Charge = Constant(1000), Efficiency = Constant(1), OverloadEfficiency = Constant(1), ThrottlingFactor = Constant(2) } } + }); + cache.Upsert(new GearData + { + Name = "Drive", Hardpoint = HardpointType.Tool, Shape = Solid(1, 1), Durability = 10, + MinimumTemperature = 0, MaximumTemperature = 1000, OptimalTemperature = 280, PlateauWidth = 400, + Behaviors = { new ThrusterData { Thrust = Constant(100), Visibility = Constant(0), Heat = Constant(0), EnergyUsage = Constant(100) } } + }); + cache.FlushAsync().Wait(); + return cache; + } + + private static float2 Pre(float2 v, ItemRotation r) => r switch + { + ItemRotation.None => v, + ItemRotation.CounterClockwise => float2(-v.y, v.x), + ItemRotation.Reversed => float2(-v.x, -v.y), + _ => float2(v.y, -v.x) + }; + + [Fact] + public void FacingReadsIdenticallyForEveryRotation() + { + using var cache = OpenCatalog(); + var ship = Build(cache, out var items, out var hullData); + var thrusters = ship.GetBehaviors().ToList(); + Assert.Equal(Rotations.Length, thrusters.Count); + foreach (var heading in new[] { float2(0, 1), float2(1, 0), normalize(float2(.6f, -.8f)), normalize(float2(-.28f, .96f)) }) + { + ship.Direction = heading; + foreach (var thruster in thrusters) + { + var item = thruster.Item; + var rotation = item.EquippableItem.Rotation; + var rotated = Pre(heading, rotation); + + Assert.Equal(float3(rotated.x, 0, rotated.y), thruster.Direction); + Assert.Equal(normalize(float3(rotated.x, 0, rotated.y)), FireControl.MountDirection(item)); + + var toCenter = hullData.Shape.CenterOfMass + - hullData.Shape.Inset(items.GetData(item.EquippableItem).Shape, item.Position, rotation).CenterOfMass; + var torque = -dot(normalize(toCenter), Pre(float2(1, 0), rotation)); + Assert.Equal(torque, thruster.Torque); + + // Weapon arcs (InArc) and the aim's mount direction are decided by this one facing: a sweep of bearings, + // at a 110-degree arc, so both sides of the edge are crossed. + var arc = FireControl.ArcFor(item); + for (var k = 0; k < 24; k++) + { + var angle = k * MathF.PI / 12f + .01f; + var toTarget = float3(sin(angle), 0, cos(angle)); + var expectedBears = dot(normalize(float3(rotated.x, 0, rotated.y)), normalize(toTarget)) >= cos(radians(arc / 2f)); + Assert.Equal(arc >= 360f || expectedBears, FireControl.InArc(item, toTarget)); + } + + Assert.True(item.Active.Value); + var thrust = 37f; + var settings = items.GameplaySettings; + var push = -Pre(float2(0, 1), rotation) * thrust * settings.ThrustScale / ship.Mass; + Assert.Equal(float3(push.x, push.y, torque * thrust * settings.TorqueMultiplier / ship.Mass), thruster.Column(thrust)); + } + Assert.True(thrusters.Count(t => MathF.Abs(t.Torque) > .05f) >= 3, "the fixture must give most columns a real torque"); + } + } +}