diff --git a/Assets/Scripts/Gameplay/Weapons/GuidedProjectile.cs b/Assets/Scripts/Gameplay/Weapons/GuidedProjectile.cs index c66ede374..4eb75f1ad 100644 --- a/Assets/Scripts/Gameplay/Weapons/GuidedProjectile.cs +++ b/Assets/Scripts/Gameplay/Weapons/GuidedProjectile.cs @@ -35,6 +35,7 @@ public class GuidedProjectile : MonoBehaviour private float _prevDist; private bool _active; private bool _alive; + private float _spawnTime; private Vector3 _targetVelocity; private Vector3 _previousTargetPosition; @@ -46,6 +47,11 @@ public class GuidedProjectile : MonoBehaviour public Vector3 Velocity { get; set; } public Entity SourceEntity { get; set; } + // How long after spawn the round keeps its binding to the simulation's verdict: the shot's own flight time (the + // manager sets it from the committed shot's ArrivalTime), so a round that faded out early still lives to show + // the detonation the simulation resolves at arrival. 0 (the default) kills the round when its fade ends. + public float BindingLifetime { get; set; } + public event Action OnKill; // The simulation's verdict on this round, bound by GuidedProjectileManager to Zone.ShotCommitted and @@ -83,12 +89,15 @@ public class GuidedProjectile : MonoBehaviour // one whose target is gone) just falls away, with no explosion to show. public void Resolve(bool detonated, Vector3? point) { - if (!_alive || !_active) return; + // A round that already faded out (overshot, out of flight) still shows the simulation's detonation: only a + // round that has been killed is done. The fade is stopped so it cannot Kill the round a second time. + if (!_alive) return; if (!detonated) { - StartCoroutine(FadeOut()); + if (_active) StartCoroutine(FadeOut()); return; } + StopAllCoroutines(); if (point.HasValue) transform.position = point.Value; if (HitEffect != null) { @@ -101,6 +110,7 @@ public class GuidedProjectile : MonoBehaviour void OnEnable() { _active = _alive = true; + _spawnTime = Time.time; _phase = Random.value * 100; _prevDist = Single.MaxValue; Particles.startColor = Color.white; @@ -212,6 +222,8 @@ public class GuidedProjectile : MonoBehaviour yield return null; } + // Faded, but the simulation's verdict has not necessarily arrived: wait for it until the shot's flight is over. + while (Time.time - _spawnTime < BindingLifetime) yield return null; StartCoroutine(Kill()); } diff --git a/Assets/Scripts/Gameplay/Weapons/GuidedProjectileManager.cs b/Assets/Scripts/Gameplay/Weapons/GuidedProjectileManager.cs index a611b1106..e5eb74b79 100644 --- a/Assets/Scripts/Gameplay/Weapons/GuidedProjectileManager.cs +++ b/Assets/Scripts/Gameplay/Weapons/GuidedProjectileManager.cs @@ -19,6 +19,9 @@ public class GuidedProjectileManager : InstantWeaponEffectManager zone.ShotResolved.Where(o => o.ShotId == shotId) .Subscribe(o => p.Resolve(o.Result == ShotResult.Hit || o.Result == ShotResult.Burst, o.HasBurstPoint ? At(o) : (Vector3?) null)) .AddTo(bindings); + // The binding outlives a fade-out until the shot's own arrival (the moment the simulation resolves it), plus a + // second of margin; a shot no longer pending has nothing left to resolve. + p.BindingLifetime = zone.TryGetShot(shotId, out var shot) ? max(0f, shot.ArrivalTime - zone.Time) + 1f : 0f; p.Binding = bindings; // replaces, and so disposes, any binding the projectile already had } diff --git a/Assets/Scripts/ServerShared/Agents/States/Combat.cs b/Assets/Scripts/ServerShared/Agents/States/Combat.cs index d27a90ab1..d605ff8a5 100644 --- a/Assets/Scripts/ServerShared/Agents/States/Combat.cs +++ b/Assets/Scripts/ServerShared/Agents/States/Combat.cs @@ -57,10 +57,10 @@ public class CombatState : BaseState var group = _agent.Ship.WeaponGroups[i]; var dps = 0f; foreach (var weapon in group.weapons) - // The range test is FireControl's (InReach, the part of Designated before the lock, which this - // loop is what acquires), and a weapon Solve refuses adds nothing to a group's damage. + // The range test is FireControl's (Designated), and a weapon Solve refuses adds nothing to a group's + // damage. if (weapon.Item.Online.Value && - FireControl.InReach(weapon, _agent.Ship, target, out _) && + FireControl.Designated(weapon, _agent.Ship, target, out _) && !FireControl.Refuses(weapon, _agent.Ship) && (weapon is ConstantWeapon || weapon is InstantWeapon instantWeapon && instantWeapon.CanFire)) diff --git a/Assets/Scripts/ServerShared/Behaviors/ChargedWeapon.cs b/Assets/Scripts/ServerShared/Behaviors/ChargedWeapon.cs index f3363ac23..ea9134fff 100644 --- a/Assets/Scripts/ServerShared/Behaviors/ChargedWeapon.cs +++ b/Assets/Scripts/ServerShared/Behaviors/ChargedWeapon.cs @@ -160,7 +160,8 @@ public class ChargedWeapon : InstantWeapon public override void Activate() { - if(!_charging && !_coolingDown) + // A refused weapon never starts charging: it would pay charge heat and audio for a shot Trigger drops. + if(!_charging && !_coolingDown && !FireControl.Refuses(this, Entity)) { OnStartCharging?.Invoke(); Item.FireAudioEvent(ChargedWeaponAudioEvent.Start); diff --git a/Assets/Scripts/ServerShared/Behaviors/InstantWeapon.cs b/Assets/Scripts/ServerShared/Behaviors/InstantWeapon.cs index 59b7f6344..4d6cb80d0 100644 --- a/Assets/Scripts/ServerShared/Behaviors/InstantWeapon.cs +++ b/Assets/Scripts/ServerShared/Behaviors/InstantWeapon.cs @@ -42,6 +42,7 @@ public class InstantWeapon : Weapon, IProgressBehavior, IEventBehavior, IPowerCo protected int _burstRemaining; private bool _burstPaid; + private bool _burstStarted; private float _burstTimer; private float _burstInterval; protected float _cooldown; // Normalized @@ -53,7 +54,8 @@ public class InstantWeapon : Weapon, IProgressBehavior, IEventBehavior, IPowerCo public float Cooldown { get; protected set; } public virtual bool CanFire { - get => !_coolingDown; + // A triggered burst that has not yet fired a round is not ready either: its cooldown has not started. + get => !_coolingDown && (_burstStarted || _burstRemaining == 0); } public override float DamagePerSecond => Damage / Cooldown; @@ -115,8 +117,9 @@ public class InstantWeapon : Weapon, IProgressBehavior, IEventBehavior, IPowerCo _burstRemaining = (int) BurstCount; _burstInterval = BurstTime / _burstRemaining; _burstTimer = 0; - _cooldown = 1; - _coolingDown = true; + // The cooldown starts with the burst's first round that is not refused (Execute), not here: a burst whose + // every round is refused fires nothing and leaves no cooldown behind. + _burstStarted = false; } protected override void UpdateStats() @@ -242,7 +245,16 @@ public class InstantWeapon : Weapon, IProgressBehavior, IEventBehavior, IPowerCo _burstRemaining = 0; return false; } - if (!refused) _burstPaid = _data.SingleAmmoBurst; + if (!refused) + { + _burstPaid = _data.SingleAmmoBurst; + if (!_burstStarted) + { + _burstStarted = true; + _cooldown = 1; + _coolingDown = true; + } + } _burstRemaining--; _burstTimer -= _burstInterval; diff --git a/Assets/Scripts/ServerShared/FireControl.cs b/Assets/Scripts/ServerShared/FireControl.cs index 8bdada43b..06c9d3ca5 100644 --- a/Assets/Scripts/ServerShared/FireControl.cs +++ b/Assets/Scripts/ServerShared/FireControl.cs @@ -229,18 +229,13 @@ public static class FireControl // visible, out of range) already denies the target. Planar (R7: the simulation is 2D): the range gate, the // burst reach and the range clamp all measure the same distance, so a target's height never moves one of // them without the others. - public static bool Designated(Weapon weapon, Entity source, Entity target, out float range) => - InReach(weapon, source, target, out range) && !(weapon is LockWeapon lockWeapon && !lockWeapon.IsLocked); - - // Designated before the lock: the target is seen and within the weapon's planar range. A LockWeapon acquires - // its lock only while its shooter holds the trigger, so an agent asks this to know when to start locking; - // every other reader wants Designated. The one range test in the file. - public static bool InReach(Weapon weapon, Entity source, Entity target, out float range) + public static bool Designated(Weapon weapon, Entity source, Entity target, out float range) { range = 0f; if (target == null) return false; range = length((target.Position - source.Position).xz); - return source.VisibleEntities.Contains(target) && range >= weapon.MinRange && range <= weapon.Range; + return source.VisibleEntities.Contains(target) && range >= weapon.MinRange && range <= weapon.Range && + !(weapon is LockWeapon lockWeapon && !lockWeapon.IsLocked); } // `designated` is the answer above. `inArc` is the bearing test, asked only once a target is designated. diff --git a/tests/Aetheria.Shared.Tests/FireControlPerWeaponTests.cs b/tests/Aetheria.Shared.Tests/FireControlPerWeaponTests.cs index 4f9ffe282..70d083e27 100644 --- a/tests/Aetheria.Shared.Tests/FireControlPerWeaponTests.cs +++ b/tests/Aetheria.Shared.Tests/FireControlPerWeaponTests.cs @@ -84,7 +84,7 @@ public sealed class FireControlPerWeaponTests : IDisposable private Rig Build( (ItemRotation Rotation, float Arc)[] mounts = null, bool beam = false, WeaponFuse? fuse = null, float blast = 0f, float range = 100f, float targetRange = 60f, float accuracy = 1f, float minHit = .2f, bool lockWeapon = false, - bool turret = false, float energy = 0f, float heat = 0f, float visibility = 0f, int magazine = 0, bool singleAmmoBurst = false, bool decoy = false) + bool turret = false, float energy = 0f, float heat = 0f, float visibility = 0f, int magazine = 0, bool singleAmmoBurst = false, bool decoy = false, bool charged = false, bool auto = false) { mounts ??= new[] { (ItemRotation.None, 0f) }; var hullData = new HullData @@ -120,6 +120,14 @@ public sealed class FireControlPerWeaponTests : IDisposable LockSpeed = Constant(1000), SensorImpact = Constant(1), LockAngle = Constant(0), DirectionImpact = Constant(1), Decay = Constant(1), Count = Constant(1), BurstTime = Constant(0), Cooldown = Constant(1) }; + else if (charged) + behavior = new ChargedWeaponData + { + Count = Constant(1), BurstTime = Constant(0), Cooldown = Constant(1), SingleAmmoBurst = singleAmmoBurst, + ChargeTime = Constant(1), ChargeEnergy = Constant(0), ChargeHeat = Constant(5000), CanFireEarly = true + }; + else if (auto) + behavior = new AutoWeaponData { Count = Constant(1), BurstTime = Constant(0), Cooldown = Constant(1), SingleAmmoBurst = singleAmmoBurst }; else behavior = new InstantWeaponData { Count = Constant(1), BurstTime = Constant(0), Cooldown = Constant(1), SingleAmmoBurst = singleAmmoBurst }; behavior.Damage = Constant(100); @@ -401,13 +409,14 @@ public sealed class FireControlPerWeaponTests : IDisposable public bool Visible; public int Announced; public int Shots; + public bool CanFire; } // One discrete weapon, triggered once at no target, with every cost observable: a magazine, an energy charge, // a sound bank, wear events, heat and visibility. Range 29.5 refuses (arming 30); Range 40 fires. - private Costs Pull(float range, bool single, bool viaTrigger, bool staleRange = false) + private Costs Pull(float range, bool single, bool viaTrigger, bool staleRange = false, bool auto = false) { - var r = Build(fuse: WeaponFuse.Proximity, blast: 30f, range: range, energy: 50f, heat: 5000f, visibility: 100f, magazine: 5, singleAmmoBurst: single); + var r = Build(fuse: WeaponFuse.Proximity, blast: 30f, range: range, energy: 50f, heat: 5000f, visibility: 100f, magazine: 5, singleAmmoBurst: single, auto: auto); r.Shooter.Target.Value = null; Aim(r, float2(0, 1)); var weapon = (InstantWeapon) r.Gun.Weapon; @@ -427,7 +436,13 @@ public sealed class FireControlPerWeaponTests : IDisposable // The Range the previous tick's Execute left, before this tick refreshes it: a power-dependent Range that // has since fallen. Set through the property's own setter, so the trigger sees a Range the round will not. if (staleRange) typeof(Weapon).GetProperty("Range").GetSetMethod(true).Invoke(weapon, new object[] { 100f }); - if (viaTrigger) weapon.Activate(); + if (auto) + { + // AutoWeapon's own trigger: a held weapon whose cooldown has run out retriggers inside Execute. + typeof(Weapon).GetField("_firing", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(weapon, true); + typeof(InstantWeapon).GetField("_cooldown", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(weapon, -1f); + } + else if (viaTrigger) weapon.Activate(); else typeof(InstantWeapon).GetField("_burstRemaining", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(weapon, 1); r.Zone.Update(.01f); @@ -435,6 +450,7 @@ public sealed class FireControlPerWeaponTests : IDisposable costs.ChargeSpent = chargeBefore - capacitor.Charge; costs.Heat = r.Gun.Item.Temperature - heatBefore; costs.Visible = r.Shooter.VisibilitySources.TryGetValue(weapon, out var v) && v > 1f; + costs.CanFire = weapon.CanFire; return costs; } @@ -603,16 +619,63 @@ public sealed class FireControlPerWeaponTests : IDisposable // A round refused on a Range that fell between the trigger and the round is still free: the burst is paid // (SingleAmmoBurst) at its first round that is not refused, judged with the Range that round flies with. - // The trigger saw a stale Range of 100; the round's Range is 29.5, under the arming distance. - // Kills: paying for the burst at the trigger. - [Fact] - public void ARefusedBurstThatPassedTheTriggerOnAStaleRangeCostsNothing() + // The trigger saw a stale Range of 100; the round's Range is 29.5, under the arming distance. The refusal + // starts no cooldown either (operator ruling 2026-09-30): the weapon can fire again at once, while the + // control (Range 40) fired and is cooling down. Covers the player's trigger and AutoWeapon's own. + // Kills: paying for the burst at the trigger; starting the cooldown at the trigger (Trigger or AutoWeapon). + [Theory] + [InlineData(true, false)] + [InlineData(false, false)] + [InlineData(true, true)] + [InlineData(false, true)] + public void ARefusedBurstThatPassedTheTriggerOnAStaleRangeCostsNothing(bool single, bool auto) { - var refused = Pull(29.5f, single: true, viaTrigger: true, staleRange: true); + var fired = Pull(40f, single, viaTrigger: true, staleRange: true, auto: auto); + Assert.Equal(1, fired.Shots); + Assert.False(fired.CanFire, "control: a fired burst starts the cooldown"); + + var refused = Pull(29.5f, single, viaTrigger: true, staleRange: true, auto: auto); Assert.Equal(0, refused.Shots); Assert.Equal(0, refused.Ammo); Assert.Equal(0f, refused.ChargeSpent); Assert.Equal(0, refused.Announced); + Assert.True(refused.CanFire, "a burst that fired nothing leaves no cooldown"); + } + + // A refused charged weapon never starts charging: no charge heat, no audio, no start event, and it can be + // triggered again at once. The control (Range 40) charges, sounds and heats. + // Kills: ChargedWeapon.Activate that does not ask FireControl. + private (int Starts, int Sounds, float Heat, bool Charging) Charge(float range) + { + var r = Build(charged: true, fuse: WeaponFuse.Proximity, blast: 30f, range: range); + r.Shooter.Target.Value = null; + Aim(r, float2(0, 1)); + var weapon = (ChargedWeapon) r.Gun.Weapon; + r.Gun.Item.SoundBank = new WwiseMetaSoundBank { IncludedEvents = new[] { new WwiseMetaObject { Id = 7, Name = "gun_fire" } } }; + var starts = 0; + var sounds = 0; + weapon.OnStartCharging += () => starts++; + r.Gun.Item.AudioEvents.Subscribe(_ => sounds++); + var heatBefore = r.Gun.Item.Temperature; + + weapon.Activate(); + for (var i = 0; i < 3; i++) r.Zone.Update(.1f); + + return (starts, sounds, r.Gun.Item.Temperature - heatBefore, weapon.Progress > 0f); + } + + [Fact] + public void ARefusedChargedWeaponNeverStartsCharging() + { + var fired = Charge(40f); + Assert.Equal(1, fired.Starts); + Assert.True(fired.Sounds > 0, "control: charging sounds"); + Assert.True(fired.Heat > 0f, "control: charging heats"); + + var refused = Charge(29.5f); + Assert.Equal(0, refused.Starts); + Assert.Equal(0, refused.Sounds); + Assert.True(fired.Heat - refused.Heat > 5f, $"a refused weapon adds no charge heat ({refused.Heat} vs {fired.Heat})"); } }