From 9a2a9390e12a6b654dabdc6578d6226855876bcc Mon Sep 17 00:00:00 2001 From: soul Date: Thu, 22 Oct 2026 00:32:11 +0200 Subject: [PATCH] Command seam, agents: one PilotCommand per entity, applied once per step by PilotApplier in Zone.Update Agent, Flight, Combat, MoveTo and Follow write only Ship.Command. Entities with no agent apply first, then each agent applies right after it decides. A pilot reads its own pending designation and aim item back from its command; FireControl gains Refuses/AgentFires/HitProbability overloads that take them. Co-Authored-By: Claude Opus 5.5 --- Assets/Scripts/ServerShared/Agents/Agent.cs | 14 +-- Assets/Scripts/ServerShared/Agents/Flight.cs | 4 +- Assets/Scripts/ServerShared/Agents/Minion.cs | 4 +- .../ServerShared/Agents/States/Combat.cs | 23 +++-- .../ServerShared/Agents/States/Follow.cs | 2 +- .../ServerShared/Agents/States/MoveTo.cs | 4 +- Assets/Scripts/ServerShared/Entity.cs | 4 + Assets/Scripts/ServerShared/FireControl.cs | 35 +++++-- Assets/Scripts/ServerShared/Pilot.cs | 98 +++++++++++++++++++ Assets/Scripts/ServerShared/Pilot.cs.meta | 2 + Assets/Scripts/ServerShared/Zone.cs | 13 ++- 11 files changed, 169 insertions(+), 34 deletions(-) create mode 100644 Assets/Scripts/ServerShared/Pilot.cs create mode 100644 Assets/Scripts/ServerShared/Pilot.cs.meta diff --git a/Assets/Scripts/ServerShared/Agents/Agent.cs b/Assets/Scripts/ServerShared/Agents/Agent.cs index 937096a7d..eb74818ba 100644 --- a/Assets/Scripts/ServerShared/Agents/Agent.cs +++ b/Assets/Scripts/ServerShared/Agents/Agent.cs @@ -55,9 +55,9 @@ public class Agent public void Update(float delta) { - // The pilot tick writes Turn every tick, in every state: a state that pilots overwrites this zero with its + // The pilot tick commands Turn every tick, in every state: a state that pilots overwrites this zero with its // demand, and one that does not leaves the hull holding course instead of inheriting the last state's rate. - /*DEL turn0*/ + Ship.Command.Turn = TurnCommand.OfRate(0); VentOnTrack(); _currentState.Update(delta); foreach (var transition in _currentState.Transitions) @@ -84,7 +84,7 @@ public class Agent { if (Ship.HasActiveConsumable(kind)) return; var consumable = Ship.FirstCarriedConsumable(kind); - if (consumable != null) /*DEL use*/; + if (consumable != null) Ship.Command.Use(consumable); } public void Accelerate(float2 targetVelocity, bool noTurn = false) @@ -95,18 +95,18 @@ public class Agent // If Delta V is above the threshold, direct the ship towards the delta and use only main thrusters if (!noTurn && deltaVMag > FORWARD_DELTA_THRESHOLD) { - /*DEL accel-turn*/ - /*DEL accel-move1*/ + Ship.Command.Turn = TurnCommand.Toward(deltaVDirection); + Ship.Command.Movement = float2(0, pow(dot(Ship.Direction, deltaVDirection), 2)); } // If Delta V is low, use all thrusters and leave Turn to the caller (MoveTo already pointed it at the target) else if(deltaVMag > THRUST_DELTA_THRESHOLD) { var right = Ship.Direction.Rotate(ItemRotation.Clockwise); - /*DEL accel-move2*/ + Ship.Command.Movement = float2(dot(right, deltaVDirection), dot(Ship.Direction, deltaVDirection)); } else { - /*DEL accel-move3*/ + Ship.Command.Movement = float2.zero; } } } diff --git a/Assets/Scripts/ServerShared/Agents/Flight.cs b/Assets/Scripts/ServerShared/Agents/Flight.cs index e7fbecfa8..497ac865f 100644 --- a/Assets/Scripts/ServerShared/Agents/Flight.cs +++ b/Assets/Scripts/ServerShared/Agents/Flight.cs @@ -115,7 +115,7 @@ public class Flight { track.Phase = Phase.Hailing; track.Start = _time; - if (!string.IsNullOrEmpty(_doctrine.Hail)) /*DEL hail*/; + if (!string.IsNullOrEmpty(_doctrine.Hail)) Nearest(entity)?.Agent.Ship.Command.Hail(_doctrine.Hail); } else track.Phase = Phase.Engaged; break; @@ -175,7 +175,7 @@ public class Flight private static void Aim(Ship ship, Entity target) { var wanted = target == null ? TargetRef.None : new TargetRef(target); - if (!ship.Target.Value.Equals(wanted)) /*DEL designate*/; + if (!ship.Target.Value.Equals(wanted)) ship.Command.Designate(wanted); } private float Standoff(Ship ship) diff --git a/Assets/Scripts/ServerShared/Agents/Minion.cs b/Assets/Scripts/ServerShared/Agents/Minion.cs index 41c0e3fe1..a1277b264 100644 --- a/Assets/Scripts/ServerShared/Agents/Minion.cs +++ b/Assets/Scripts/ServerShared/Agents/Minion.cs @@ -22,9 +22,9 @@ public class Minion : Agent var combatState = new CombatState(this); _rootState.AddTransition(combatState, - () => Ship.Target.Value.Entity != null, null, true, _rootState); + () => (Ship.Command.Designated ?? Ship.Target.Value).Entity != null, null, true, _rootState); combatState.AddTransition(_rootState, - () => Ship.Target.Value.Entity == null, null, true, _rootState); + () => (Ship.Command.Designated ?? Ship.Target.Value).Entity == null, null, true, _rootState); } } diff --git a/Assets/Scripts/ServerShared/Agents/States/Combat.cs b/Assets/Scripts/ServerShared/Agents/States/Combat.cs index 4ca637212..1a062e7be 100644 --- a/Assets/Scripts/ServerShared/Agents/States/Combat.cs +++ b/Assets/Scripts/ServerShared/Agents/States/Combat.cs @@ -43,7 +43,8 @@ public class CombatState : BaseState _optimumStale = false; } - var target = _agent.Ship.Target.Value.Entity; + // The pilot's own pending designation (its Flight's, this step) when set, else last step's applied target. + var target = (_agent.Ship.Command.Designated ?? _agent.Ship.Target.Value).Entity; if (target == null) return; _availableGroups.Clear(); @@ -70,7 +71,8 @@ public class CombatState : BaseState bestTargetItem = reveal.Item; } } - /*DEL select*/ + _agent.Ship.Command.SelectItem(bestTargetItem); + var aim = bestTargetItem == null ? AimTarget.None : AimTarget.OfItem(bestTargetItem); for (var i = 0; i < _agent.Ship.WeaponGroups.Length; i++) { @@ -81,7 +83,7 @@ public class CombatState : BaseState // damage. if (weapon.Item.Online.Value && FireControl.Designated(weapon, _agent.Ship, target, out _) && - !FireControl.Refuses(weapon, _agent.Ship) && + !FireControl.Refuses(weapon, _agent.Ship, target) && (weapon is ConstantWeapon || weapon is InstantWeapon instantWeapon && instantWeapon.CanFire)) { @@ -94,7 +96,7 @@ public class CombatState : BaseState if (dps > .1) _availableGroups.Add((i, dps)); } - foreach(var w in _availableLockingWeapons) /*DEL lock*/; + foreach(var w in _availableLockingWeapons) _agent.Ship.Command.Pull(w); UseConsumables(toTarget, targetDistance); @@ -139,14 +141,15 @@ public class CombatState : BaseState // fires at, and "the target is designated" for a fused one. foreach (var weapon in _agent.Ship.WeaponGroups[selectedGroup].weapons) { - if (FireControl.AgentFires(weapon, _agent.Ship, target, _agent.Doctrine.MinHitProbability)) - /*DEL pull*/; - else if (weapon.Firing) - /*DEL release*/; + if (FireControl.AgentFires(weapon, _agent.Ship, target, _agent.Doctrine.MinHitProbability, aim)) + _agent.Ship.Command.Pull(weapon); + // A lock pulled above this step counts as firing, as it did when it was activated in place. + else if (weapon.Firing || _agent.Ship.Command.Pulls(weapon)) + _agent.Ship.Command.Release(weapon); } } - /*DEL aim*/ - /*DEL turn*/ + _agent.Ship.Command.Aim = toTarget; + _agent.Ship.Command.Turn = TurnCommand.Toward(toTarget.xz); _agent.Accelerate(movementDirection * _agent.TopSpeed, true);//selectedGroup >= 0); // Fire charged guns! diff --git a/Assets/Scripts/ServerShared/Agents/States/Follow.cs b/Assets/Scripts/ServerShared/Agents/States/Follow.cs index c63b07a06..1bbf3f826 100644 --- a/Assets/Scripts/ServerShared/Agents/States/Follow.cs +++ b/Assets/Scripts/ServerShared/Agents/States/Follow.cs @@ -20,7 +20,7 @@ public class FollowState : BaseState var pull = direction * (float) sign(error) * saturate(abs(error) / max(task.Standoff, 1f)); var desired = task.Anchor.Velocity + pull * _agent.TopSpeed; - /*DEL aim*/ + _agent.Ship.Command.Aim = float3(direction.x, 0, direction.y); _agent.Accelerate(desired); } } diff --git a/Assets/Scripts/ServerShared/Agents/States/MoveTo.cs b/Assets/Scripts/ServerShared/Agents/States/MoveTo.cs index ba203c8cb..aad04db0d 100644 --- a/Assets/Scripts/ServerShared/Agents/States/MoveTo.cs +++ b/Assets/Scripts/ServerShared/Agents/States/MoveTo.cs @@ -23,8 +23,8 @@ public abstract class MoveToState : BaseState // We want to go top speed in the direction of our target var desiredVelocity = dir * _agent.TopSpeed; - /*DEL aim*/ - /*DEL turn*/ + _agent.Ship.Command.Aim = float3(dir.x, 0, dir.y); + _agent.Ship.Command.Turn = TurnCommand.Toward(dir); _agent.Accelerate(desiredVelocity); } } diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index 4a2b25402..780f365bc 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -87,6 +87,10 @@ public abstract class Entity // hull turns only by Ship.Turn. public float3 Aim; + // The entity's one pilot input, written by its one pilot (its agent, the player, a scripted pilot) and applied only by + // PilotApplier in the zone step. Runtime only: never packed. + public PilotCommand Command { get; } = new PilotCommand(); + public string Name; // Names the entity for assertion messages and logs: xunit and the debugger format an object by walking its graph diff --git a/Assets/Scripts/ServerShared/FireControl.cs b/Assets/Scripts/ServerShared/FireControl.cs index 5ea06f455..8f3cc5018 100644 --- a/Assets/Scripts/ServerShared/FireControl.cs +++ b/Assets/Scripts/ServerShared/FireControl.cs @@ -84,8 +84,11 @@ public static class FireControl // Whether the shooter's next round from this weapon is refused: the weapons ask before they spend anything, so // a refused round costs no ammo, energy, sound, heat, wear or visibility (operator ruling 2026-09-30). The // answer is Solve's own -- there is no second copy of the arming test. - public static bool Refuses(Weapon weapon, Entity shooter) => - !IsMineLayer(weapon) && Solve(weapon, weapon.Item, shooter, shooter.Target.Value.Entity).Outcome == FireOutcome.Refused; + public static bool Refuses(Weapon weapon, Entity shooter) => Refuses(weapon, shooter, shooter.Target.Value.Entity); + + // The same answer against a target the shooter's pilot has commanded but not yet applied. + public static bool Refuses(Weapon weapon, Entity shooter, Entity target) => + !IsMineLayer(weapon) && Solve(weapon, weapon.Item, shooter, target).Outcome == FireOutcome.Refused; // A mine layer lays a body, not a round: it is never refused, never arc-gated, and Fire routes it to Lay. public static bool IsMineLayer(Weapon weapon) => weapon.WeaponData is MineLayerData; @@ -96,12 +99,19 @@ public static class FireControl // fires a fused weapon at nothing -- no target, or one it holds no valid data on, is no reason to fish -- and // never one Solve refuses: a refused weapon is not a weapon it can use. // The hit-probability threshold is the shooter's doctrine (RoleDoctrine.MinHitProbability), or the turret's global. - public static bool AgentFires(Weapon weapon, Entity shooter, Entity target, float minHitProbability) + public static bool AgentFires(Weapon weapon, Entity shooter, Entity target, float minHitProbability) => + AgentFires(weapon, shooter, target, minHitProbability, null); + + // The same decision priced on the aim the pilot commands this step (Combat), not the shooter's applied aim slot. + public static bool AgentFires(Weapon weapon, Entity shooter, Entity target, float minHitProbability, AimTarget aim) => + AgentFires(weapon, shooter, target, minHitProbability, (AimTarget?) aim); + + private static bool AgentFires(Weapon weapon, Entity shooter, Entity target, float minHitProbability, AimTarget? aim) { // A mine layer lays only at a designated target in range, never at nothing and never on a hit probability. if (IsMineLayer(weapon)) return Solve(weapon, weapon.Item, shooter, target).Designated; if (FuseOf(weapon.Item, out _) == null) - return HitProbability(weapon, shooter, target) >= minHitProbability; + return HitProbability(weapon, shooter, target, aim) >= minHitProbability; var solution = Solve(weapon, weapon.Item, shooter, target); return solution.Designated && solution.Outcome != FireOutcome.Refused; } @@ -330,7 +340,7 @@ public static class FireControl // common gated-out case (GatedOutHitProbabilityAllocatesNothing pins that at exactly 0). // Evasion fix (evasion-term): the same call also prices how far the target can and does move off the line of // fire, along the same travel direction, so the live price and the HUD read one evasion. - private static (Silhouette Silhouette, float PSpread, float Evasion) Forecast(Weapon weapon, Entity source, Entity target, HullData targetHull, float precision, float range) + private static (Silhouette Silhouette, float PSpread, float Evasion) Forecast(Weapon weapon, Entity source, Entity target, HullData targetHull, float precision, float range, AimTarget aim) { var travel = TravelDirection(weapon, source, target); var evasion = Evasion(target, travel); @@ -340,7 +350,6 @@ public static class FireControl Silhouette sil; try { - var aim = source.ResolvedAim; sil = Silhouette(target, targetHull, aim.Item, bearing, precision, buffer, aim.Mount); var settings = source.ItemManager.GameplaySettings; pSpread = PSpread(weapon.Spread, sil.Span, range, settings.SchematicCellSize); @@ -355,13 +364,21 @@ public static class FireControl return (sil, pSpread, evasion); } - public static float HitProbability(Weapon weapon, Entity source, Entity target) + public static float HitProbability(Weapon weapon, Entity source, Entity target) => + HitProbability(weapon, source, target, (AimTarget?) null); + + // Priced on `aim` rather than the source's aim slot: the aim its pilot commands this step. + public static float HitProbability(Weapon weapon, Entity source, Entity target, AimTarget aim) => + HitProbability(weapon, source, target, (AimTarget?) aim); + + // A null aim reads source.ResolvedAim, after the gate, so a gated-out shot reads nothing more. + private static float HitProbability(Weapon weapon, Entity source, Entity target, AimTarget? aim) { var pFire = PFire(weapon, source, target, out var range); if (pFire <= 0f) return 0f; var targetHull = source.ItemManager.GetData(target.Hull) as HullData; - var (sil, pSpread, evasion) = Forecast(weapon, source, target, targetHull, Precision(source), range); + var (sil, pSpread, evasion) = Forecast(weapon, source, target, targetHull, Precision(source), range, aim ?? source.ResolvedAim); return pFire * PMount(AngularVelocity(source, target), TrackingRate(weapon, source)) * pSpread * sil.POnHull * PDeviation(evasion, Tracking(source)); } @@ -399,7 +416,7 @@ public static class FireControl diagnostic.PSensor = PSensor(settings, diagnostic.Resolution, diagnostic.Info); diagnostic.PFire = diagnostic.Accuracy * diagnostic.PSensor; - var (sil, pSpread, evasion) = Forecast(weapon, source, target, targetHull, diagnostic.Precision, diagnostic.Range); + var (sil, pSpread, evasion) = Forecast(weapon, source, target, targetHull, diagnostic.Precision, diagnostic.Range, source.ResolvedAim); diagnostic.PSpread = pSpread; diagnostic.POnHull = sil.POnHull; diagnostic.Evasion = evasion; diff --git a/Assets/Scripts/ServerShared/Pilot.cs b/Assets/Scripts/ServerShared/Pilot.cs new file mode 100644 index 000000000..95b78348b --- /dev/null +++ b/Assets/Scripts/ServerShared/Pilot.cs @@ -0,0 +1,98 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +using System.Collections.Generic; +using CultMath; + +// A turn demand as a pilot commands it: a rate, or a heading resolved against the hull's facing when it is applied, so a +// held heading keeps steering as the hull turns. +public readonly struct TurnCommand +{ + private readonly bool _toward; + private readonly float _rate; + private readonly float2 _heading; + + private TurnCommand(bool toward, float rate, float2 heading) + { + _toward = toward; + _rate = rate; + _heading = heading; + } + + public static TurnCommand OfRate(float rate) => new TurnCommand(false, rate, default); + public static TurnCommand Toward(float2 heading) => new TurnCommand(true, 0, heading); + + public float Resolve(Ship ship) => _toward ? Steering.Toward(ship, _heading) : _rate; +} + +// The one input an entity's pilot gives it (its agent, the player, a scripted pilot): only own-ship vectors and handles the +// pilot was given (ruling observation-sensors-only). Commands are per entity, never per Flight or side: a side may be one +// ship (ruling brain-one-per-side-r2). PilotApplier.Apply is its only reader. +// Held fields persist until the pilot replaces them, so a pilot deciding below the sim rate keeps flying (ruling +// lower-game-sim-rate); null leaves the field to whoever wrote it last. One-shots apply once, in order, and are cleared. +public sealed class PilotCommand +{ + public TurnCommand? Turn; + public float2? Movement; + public float3? Aim; + + internal readonly List Designations = new List(); + internal readonly List Selections = new List(); + internal readonly List Hails = new List(); + internal readonly List Uses = new List(); + internal readonly List<(IActivatedBehavior Behavior, bool Pull)> Triggers = new List<(IActivatedBehavior, bool)>(); + + public void Designate(TargetRef target) => Designations.Add(target); + public void SelectItem(EquippedItem item) => Selections.Add(item); + public void Hail(string message) => Hails.Add(message); + public void Use(ConsumableItemData consumable) => Uses.Add(consumable); + public void Pull(IActivatedBehavior behavior) => Triggers.Add((behavior, true)); + public void Release(IActivatedBehavior behavior) => Triggers.Add((behavior, false)); + + // The pilot's own pending decisions, which it reads back instead of the ship (that is last step's applied state). + public TargetRef? Designated => Designations.Count > 0 ? Designations[Designations.Count - 1] : (TargetRef?) null; + + // Whether the last trigger this pilot commanded for `behavior` this step is a pull. + public bool Pulls(IActivatedBehavior behavior) + { + for (var i = Triggers.Count - 1; i >= 0; i--) + if (ReferenceEquals(Triggers[i].Behavior, behavior)) return Triggers[i].Pull; + return false; + } + + internal void ClearOneShots() + { + Designations.Clear(); + Selections.Clear(); + Hails.Clear(); + Uses.Clear(); + Triggers.Clear(); + } +} + +// The one writer of an entity's pilot-controlled fields, called only from Zone.Update. The order is a rule: the target +// lands before its item is selected, and the aim before the triggers, so a pilot fires along the aim it commands this +// step, as the player does. +public static class PilotApplier +{ + public static void Apply(Entity entity) + { + var command = entity.Command; + foreach (var target in command.Designations) + if (!entity.Target.Value.Equals(target)) entity.SetTarget(target); + foreach (var item in command.Selections) entity.TrySelectTargetItem(item); + foreach (var hail in command.Hails) entity.SetMessage(hail); + foreach (var consumable in command.Uses) entity.TryActivateConsumable(consumable); + if (command.Aim is float3 aim) entity.Aim = aim; + foreach (var (behavior, pull) in command.Triggers) + if (pull) behavior.Activate(); + else behavior.Deactivate(); + if (entity is Ship ship) + { + if (command.Turn is TurnCommand turn) ship.Turn = turn.Resolve(ship); + if (command.Movement is float2 movement) ship.MovementDirection = movement; + } + command.ClearOneShots(); + } +} diff --git a/Assets/Scripts/ServerShared/Pilot.cs.meta b/Assets/Scripts/ServerShared/Pilot.cs.meta new file mode 100644 index 000000000..e13004643 --- /dev/null +++ b/Assets/Scripts/ServerShared/Pilot.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 0ae6251a7a32402cb7f7db7ff1fc5084 \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index ea46f4c6f..2a7b043f4 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -365,8 +365,19 @@ public class Zone } foreach (var flight in _flights.Values) flight.Update(deltaTime); - foreach(var agent in Agents.ToArray()) + // Every pilot's command is applied by PilotApplier, once per step. Entities with no agent (the player, a scripted + // pilot, a lone ship) first, as the player's input lands before the step; then each agent applies right after it + // decides, so the next agent sees it as it saw the direct writes this replaced. + var agents = Agents.ToArray(); + var piloted = new HashSet(); + foreach (var agent in agents) piloted.Add(agent.Ship); + foreach (var entity in Entities.ToArray()) + if (!piloted.Contains(entity)) PilotApplier.Apply(entity); + foreach (var agent in agents) + { agent.Update(deltaTime); + PilotApplier.Apply(agent.Ship); + } var entities = Entities.ToArray(); foreach (var entity in entities) entity.PublishInbox();