From 1e9a89e401e4b6157647d01b8b0425f4a632726c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 16 Oct 2026 19:28:29 +0200 Subject: [PATCH] CombatState: drop the StatsKnown gate; a stale optimum is re-derived at the next agent Update Agents update before the entities, so a weapon that came online this tick has evaluated by the next Update; the Weapon.StatsKnown flag and the gate on it were dead machinery and are removed. The add handler no longer marks stale itself (a new item's Online subscription does). The refit test fits and removes a dominating gun. Not yet verified. Co-Authored-By: Claude Opus 5.5 --- .../ServerShared/Agents/States/Combat.cs | 11 ++-- .../Scripts/ServerShared/Behaviors/Weapon.cs | 4 -- .../ConsumableAiTests.cs | 56 ++++++++++++------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/Assets/Scripts/ServerShared/Agents/States/Combat.cs b/Assets/Scripts/ServerShared/Agents/States/Combat.cs index 3e10a6d2a..42f04e3cd 100644 --- a/Assets/Scripts/ServerShared/Agents/States/Combat.cs +++ b/Assets/Scripts/ServerShared/Agents/States/Combat.cs @@ -24,13 +24,14 @@ public class CombatState : BaseState public CombatState(Agent agent) : base(agent) { foreach (var item in _agent.Ship.Equipment) Watch(item); - _agent.Ship.Equipment.ObserveAdd().Subscribe(add => { Watch(add.Value); _optimumStale = true; }); + _agent.Ship.Equipment.ObserveAdd().Subscribe(add => Watch(add.Value)); _agent.Ship.Equipment.ObserveRemove().Subscribe(_ => _optimumStale = true); } - // The optimum is a function of the loadout's evaluated stats, so it is re-derived whenever a weapon comes or goes - // or changes online state, and only once every weapon that can run has evaluated its stats (a weapon evaluates on - // its first active tick, after it comes online): a ship the zone just spawned samples over live stats, not zeros. + // The optimum is a function of the loadout's evaluated stats, so it is re-derived at the next Update (agents update + // before the entities, so by then the weapons that came online this tick have evaluated) whenever a weapon is + // added, removed or changes online state. Sampling on the event itself would read the stats before they exist: a + // ship the zone just spawned would keep an optimum of 0. private void Watch(EquippedItem item) { if (item.Behaviors.OfType().Any()) item.Online.Subscribe(_ => _optimumStale = true); @@ -38,7 +39,7 @@ public class CombatState : BaseState public override void Update(float delta) { - if (_optimumStale && _agent.Ship.Weapons.All(w => w.StatsKnown || !w.Item.Active.Value)) + if (_optimumStale) { SampleDps(); _optimumStale = false; diff --git a/Assets/Scripts/ServerShared/Behaviors/Weapon.cs b/Assets/Scripts/ServerShared/Behaviors/Weapon.cs index 05edbb887..6a01f81ca 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Weapon.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Weapon.cs @@ -173,13 +173,9 @@ public abstract class Weapon : Behavior, IActivatedBehavior Tracking = Evaluate(_data.Tracking); } - // False until the first active tick has evaluated the stats above: before it they all read zero. - public bool StatsKnown { get; private set; } - public override bool Execute(float dt) { UpdateStats(); - StatsKnown = true; return true; } diff --git a/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs b/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs index adc8903bb..ca1081585 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs @@ -26,6 +26,7 @@ public sealed class ConsumableAiTests : IDisposable private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-consumable-ai-" + Guid.NewGuid().ToString("N")); private readonly List _caches = new List(); private static readonly int2 HardpointCell = new int2(0, 0); + private static readonly int2 SecondHardpointCell = new int2(2, 2); private static readonly int2 HoldCell = new int2(1, 1); private const BindingFlags Private = BindingFlags.Instance | BindingFlags.NonPublic; @@ -46,7 +47,7 @@ public sealed class ConsumableAiTests : IDisposable public Ship Agent, Prey, Locker; public EquippedCargoBay Hold; public ConsumableItemData Vent, Overdrive, Decoy, StackableVent; - public GearData LongGun; + public GearData HeavyGun; public CultRecordRef Maker; public float Optimum; @@ -80,7 +81,7 @@ public sealed class ConsumableAiTests : IDisposable } } - private static GearData GunData(float effective, string name = "Gun") => new GearData + private static GearData GunData(float effective, string name = "Gun", float damage = 10f) => new GearData { Name = name, Hardpoint = HardpointType.Sensors, Shape = new Shape(), Durability = 1, MinimumTemperature = -1000, MaximumTemperature = 1000, OptimalTemperature = 0, PlateauWidth = 2000, @@ -88,7 +89,7 @@ public sealed class ConsumableAiTests : IDisposable { new InstantWeaponData { - Damage = Const(10), Range = Const(1000), MinRange = Const(0), Velocity = Const(0), + Damage = Const(damage), Range = Const(1000), MinRange = Const(0), Velocity = Const(0), Spread = Const(0), DamageSpread = Const(0), Penetration = Const(0), Count = Const(1), BurstTime = Const(0), Cooldown = Const(1), EffectiveRange = Const(effective), DamageAtMaxRange = 0f @@ -107,10 +108,14 @@ public sealed class ConsumableAiTests : IDisposable cache.Upsert(new HullData { Name = "Skiff", HullType = HullType.Ship, Shape = hullShape, Durability = 1000000, Mass = 1000, - Hardpoints = { new HardpointData { Type = HardpointType.Sensors, Position = HardpointCell, Shape = new Shape() } } + Hardpoints = + { + new HardpointData { Type = HardpointType.Sensors, Position = HardpointCell, Shape = new Shape() }, + new HardpointData { Type = HardpointType.Sensors, Position = SecondHardpointCell, Shape = new Shape() } + } }); cache.Upsert(GunData(effective)); - cache.Upsert(GunData(800f, "LongGun")); + cache.Upsert(GunData(800f, "HeavyGun", damage: 1000f)); cache.Upsert(LockWarningTests.Launcher("Launcher", lockSpeed: .6f, sensorImpact: 1, lockAngle: 30, directionImpact: 1)); var interior = new Shape(4, 1); foreach (var cell in interior.AllCoordinates) interior[cell] = true; @@ -161,7 +166,7 @@ public sealed class ConsumableAiTests : IDisposable Overdrive = cache.GetByName("Coolant"), Decoy = cache.GetByName("Flare"), StackableVent = cache.GetByName("StackVent"), - LongGun = cache.GetByName("LongGun"), + HeavyGun = cache.GetByName("HeavyGun"), Agent = NewShip("Gun"), Prey = NewShip(null), Locker = NewShip("Launcher") }; Assert.True(scene.Agent.TryEquip(Make("Hold"), HoldCell)); @@ -316,7 +321,7 @@ public sealed class ConsumableAiTests : IDisposable // The production path for the optimum: a ship the zone admits as piloted derives it from its gun's live stats // before it needs it, and re-derives it when its loadout changes. The gun's effective range is 500 and the - // refitted gun's is 800, so the optimum moves with the weapons it has now. + // heavy gun's is 800, so the optimum moves with the weapons it has now. [Fact] public void ASpawnedShipsOptimumFollowsItsWeaponsAndNeverReadsZero() { @@ -331,18 +336,31 @@ public sealed class ConsumableAiTests : IDisposable Assert.False(s.Active(s.Overdrive)); // 30 m is not "distant" against a derived optimum Assert.Equal(1, s.Carried); - var longGun = (EquippableItem) s.Items.CreateInstance(s.Items.CreateLot(s.LongGun, s.Maker, .5f)); - Assert.True(s.Zone.Remove(s.Agent)); // refitting is a docked ship's act: out of the zone and inactive - s.Agent.Deactivate(); - Assert.NotNull(s.Agent.TryUnequip(s.Agent.Equipment.Single(e => e.Behaviors.OfType().Any()))); - Assert.True(s.Agent.TryEquip(longGun, HardpointCell)); - s.Zone.Admit(s.Agent, piloted: false); // back in, the agent's Minion keeps its CombatState - s.Place(30f, 0f); - s.Zone.Update(.05f); - s.Zone.Update(.05f); - var refit = (float) typeof(CombatState).GetField("_optimumRange", Private).GetValue(s.State); - Assert.NotEqual(s.Optimum, refit); - Assert.InRange(refit, 800f - 1000f / 32f, 800f + 1000f / 32f); + // A refit is a docked ship's act: out of the zone and inactive, then back in. The heavy gun dominates the + // damage, so with it fitted the optimum sits at its effective range, and without it at the first gun's. + void Refit(Action fit) + { + Assert.True(s.Zone.Remove(s.Agent)); + s.Agent.Deactivate(); + fit(s.Agent); + s.Zone.Admit(s.Agent, piloted: false); // back in, the agent's Minion keeps its CombatState + s.Place(30f, 0f); + s.Zone.Update(.05f); + s.Zone.Update(.05f); + } + float Optimum() => (float) typeof(CombatState).GetField("_optimumRange", Private).GetValue(s.State); + + EquippedItem heavy = null; + Refit(ship => + { + var before = ship.Equipment.ToList(); + Assert.True(ship.TryEquip((EquippableItem) s.Items.CreateInstance(s.Items.CreateLot(s.HeavyGun, s.Maker, .5f)), SecondHardpointCell)); + heavy = ship.Equipment.Single(e => !before.Contains(e)); + }); + Assert.InRange(Optimum(), 800f - 1000f / 32f, 800f + 1000f / 32f); + + Refit(ship => Assert.NotNull(ship.TryUnequip(heavy))); + Assert.InRange(Optimum(), 500f - 1000f / 32f, 500f + 1000f / 32f); } // A Search or Scan paint and a Track paint in one tick, in either order: the strongest Track paint decides.