diff --git a/Assets/Scripts/Editor/ShieldPanelCut3Verify.cs b/Assets/Scripts/Editor/ShieldPanelCut3Verify.cs index 8be2c8aa2..aa6f4b151 100644 --- a/Assets/Scripts/Editor/ShieldPanelCut3Verify.cs +++ b/Assets/Scripts/Editor/ShieldPanelCut3Verify.cs @@ -33,6 +33,13 @@ namespace Aetheria.EditorTools typeof(ShieldPanel).GetMethod("OnEnable", BindingFlags.NonPublic | BindingFlags.Instance); static readonly MethodInfo PanelOnDisable = typeof(ShieldPanel).GetMethod("OnDisable", BindingFlags.NonPublic | BindingFlags.Instance); + static readonly FieldInfo PanelPending = + typeof(ShieldPanel).GetField("pending", BindingFlags.NonPublic | BindingFlags.Instance); + + // pending's element type (PendingHit) is a private nested struct, so this reads it through + // the non-generic ICollection Queue also implements -- Count needs no knowledge of T. + static int PendingCount(ShieldPanel panel) + => ((System.Collections.ICollection)PanelPending.GetValue(panel)).Count; public static void Run() { @@ -53,6 +60,7 @@ namespace Aetheria.EditorTools bool ok = true; ok &= CheckSpawnFrameInjectionAbsorbs(); ok &= CheckReuseStrikesExistingPanel(); + ok &= CheckHitOutsideRadiusDoesNotReachPanel(); ok &= CheckPoolExhaustion(); ok &= CheckNullGuardsDoNotThrow(); ok &= CheckPoolReuseDoesNotRebuild(); @@ -273,6 +281,97 @@ namespace Aetheria.EditorTools finally { DestroyRig(rig); } } + // ============================================================== + // Operator's play report (docs/shield-panel-cut.md), playing FieldShieldTest: "clicking + // outside the hex still makes it tremble and shatter". The rule this pins: a hit outside a + // panel's own radius must not reach that panel at all -- no tremble, no temper loss, no + // break -- whether it arrives as a direct Hit() call or is routed there by the interceptor's + // reuse search; the existing panel is left exactly as it was, and a genuinely-outside click + // opens its own panel instead. + // ============================================================== + static bool CheckHitOutsideRadiusDoesNotReachPanel() + { + var rig = BuildRig(maxLive: 4, panelRadius: 2f); + try + { + rig.interceptor.Absorb(Absorb(new Vector3(0, 0, 20), Vector3.back)); + if (rig.interceptor.LiveCount != 1) + { + Debug.LogError($"[ShieldPanelCut3Verify] out-of-radius: expected 1 live panel after the first strike, got {rig.interceptor.LiveCount}."); + return false; + } + var panel = rig.interceptor.DebugPanelAt(0); + for (int i = 0; i < 40; i++) StepPanel(panel); + + // §2.4/D10's own comment: breakBudgetFraction paces the fracture cascade over many + // frames on purpose, so min temper keeps drifting downward for a while even with zero + // NEW hits -- comparing temper before/after more stepping would be measuring that + // drift, not this rule. What must be verified deterministically instead: no PendingHit + // was ever enqueued for the rejected strike (Hit() returns before Enqueue), and the + // panel's own state buffer does not move at all across a call that does no stepping. + if (PendingCount(panel) != 0) + { + Debug.LogError($"[ShieldPanelCut3Verify] out-of-radius: pending queue not drained before the probe " + + $"({PendingCount(panel)} left) -- test setup is unreliable."); + return false; + } + float temperBefore = MinTemper(panel); + + // Mutation target: ShieldPanel.ContainsWorldPoint / Hit's early-out. A point at local + // (5,0,0) is 5 units from panel-local origin against panelRadius 2 -- well outside. + Vector3 farAsWorld = panel.transform.TransformPoint(new Vector3(5f, 0f, 0f)); + panel.Hit(farAsWorld, Vector3.back, 500f); + if (PendingCount(panel) != 0) + { + Debug.LogError($"[ShieldPanelCut3Verify] out-of-radius: a direct Hit() 5 units outside panelRadius " + + $"(2) still enqueued a PendingHit -- Hit() is not gating on ContainsWorldPoint."); + return false; + } + float temperAfterDirectFarHit = MinTemper(panel); + if (temperAfterDirectFarHit != temperBefore) + { + Debug.LogError($"[ShieldPanelCut3Verify] out-of-radius: a rejected Hit() still changed panel state " + + $"({temperBefore:F6} -> {temperAfterDirectFarHit:F6}) with no Update() in between."); + return false; + } + + // Mutation target: ShieldInterceptor.FindReusable. A clearly-outside click routed + // through Absorb() must open its own panel, not disturb the existing one -- again + // checked with no intervening StepPanel, so any change can only be this call's doing. + rig.interceptor.Absorb(Absorb(new Vector3(20, 0, 0), Vector3.left)); + if (rig.interceptor.LiveCount != 2) + { + Debug.LogError($"[ShieldPanelCut3Verify] out-of-radius: a clearly-outside click should open its own " + + $"panel (live count {rig.interceptor.LiveCount}, expected 2)."); + return false; + } + if (rig.interceptor.DebugPanelAt(0) != panel) + { + Debug.LogError("[ShieldPanelCut3Verify] out-of-radius: the original panel's identity changed from an out-of-radius strike."); + return false; + } + if (PendingCount(panel) != 0) + { + Debug.LogError($"[ShieldPanelCut3Verify] out-of-radius: a far Absorb() enqueued a PendingHit on the " + + "original panel instead of routing to a new one."); + return false; + } + float temperAfterFarAbsorb = MinTemper(panel); + if (temperAfterFarAbsorb != temperBefore) + { + Debug.LogError($"[ShieldPanelCut3Verify] out-of-radius: routing a far Absorb() changed the original " + + $"panel's state ({temperBefore:F6} -> {temperAfterFarAbsorb:F6}) instead of leaving it untouched."); + return false; + } + + Debug.Log($"[ShieldPanelCut3Verify] out-of-radius OK: a direct Hit() and a routed Absorb() outside " + + $"panelRadius both enqueued nothing on the existing panel (temper held at {temperBefore:F6}), " + + "and the far click opened its own panel."); + return true; + } + finally { DestroyRig(rig); } + } + static float MinTemper(ShieldPanel panel) { var raw = new CellStateRaw[panel.DebugCellCount]; diff --git a/Assets/Scripts/Gameplay/ShieldInterceptor.cs b/Assets/Scripts/Gameplay/ShieldInterceptor.cs index 409946d04..29d9b4558 100644 --- a/Assets/Scripts/Gameplay/ShieldInterceptor.cs +++ b/Assets/Scripts/Gameplay/ShieldInterceptor.cs @@ -16,7 +16,11 @@ public class ShieldInterceptor : MonoBehaviour, IAbsorbPresenter public int MaxLivePanels = 12; // §2.3: ~0.12 ms CPU issuance per live panel public float PanelRadius = 2f; // the SHIELD ITEM's size, not the hull's (R2) public float EnergyScale = 1f; - [Range(0f, 2f)] public float ReuseFraction = 1f; + // Cut 3 fix: capped at 1, not 2 -- ShieldPanel.ContainsWorldPoint (the one owner of "is this + // point within my own radius", shared by Hit's own strike gate) clamps the scale to at most 1, + // so a value above 1 was already silently inert. The old [0,2] range let you dial in a number + // that did nothing past 1; this range now tells the truth about what the knob controls. + [Range(0f, 1f)] public float ReuseFraction = 1f; [Tooltip("Margin added on top of growDuration + shardLife before a panel with no further " + "hits is released back to the pool (§3: a fixed lifetime, not a GPU readback).")] public float DeathMargin = 0.25f; @@ -89,7 +93,14 @@ public class ShieldInterceptor : MonoBehaviour, IAbsorbPresenter // spawning a new one -- this is the rule that makes temper erosion (ShieldSim.compute's // multi-hit budget) visible at all. No ResetSim here: that would wipe the very state // this rule exists to preserve. - _live[reuseIndex].Panel.Hit(intercept, dir, energy, pattern); + var lp = _live[reuseIndex]; + lp.Panel.Hit(intercept, dir, energy, pattern); + // Cut 3 fix: DieAt was set once at spawn time and never refreshed on reuse, so a panel + // that keeps getting struck could still expire and get pooled mid-fracture -- the + // operator's "pooling seems very rough". A struck-again panel is exactly as alive as one + // freshly spawned right now; give it the same margin from THIS hit. + lp.DieAt = Time.time + lp.Panel.growDuration + lp.Panel.shardLife + DeathMargin; + _live[reuseIndex] = lp; return; } @@ -136,16 +147,24 @@ public class ShieldInterceptor : MonoBehaviour, IAbsorbPresenter }); } + // Cut 3 fix: this used to compare a raw world-space chord distance from the panel's stale + // spawn-time Position against PanelRadius*ReuseFraction. On the envelope's own documented + // non-uniform ellipsoid (ShieldEnvelope.cs's Longinus/FieldShieldTest numbers), a chord across + // curvature is not the same length as the panel's own in-plane extent -- a click clearly outside + // the rendered tiling could still chord-measure inside the threshold and get routed to Hit(), + // which had no bounds check of its own and dutifully processed it (the operator's "clicking + // outside the hex still makes it tremble and shatter"). ShieldPanel.ContainsWorldPoint is the + // one owner of "is this point within my own radius" now (Hit() gates on it too), so reuse + // selection and the strike gate can never disagree. int FindReusable(Vector3 point) { - float threshold = PanelRadius * ReuseFraction; - float threshold2 = threshold * threshold; int best = -1; float bestDist2 = float.MaxValue; for (int i = 0; i < _live.Count; i++) { + if (!_live[i].Panel.ContainsWorldPoint(point, ReuseFraction)) continue; float d2 = (_live[i].Position - point).sqrMagnitude; - if (d2 < threshold2 && d2 < bestDist2) + if (d2 < bestDist2) { bestDist2 = d2; best = i; diff --git a/Assets/Shaders/Compute/ShieldField/ShieldPanel.cs b/Assets/Shaders/Compute/ShieldField/ShieldPanel.cs index 6d7cace5d..f7147b130 100644 --- a/Assets/Shaders/Compute/ShieldField/ShieldPanel.cs +++ b/Assets/Shaders/Compute/ShieldField/ShieldPanel.cs @@ -338,12 +338,29 @@ namespace ShieldField public Vector2 DebugCellCenter(int i) => data.cells[i].centroid; // ============================================================== + // Cut 3 fix (operator's play report, docs/shield-panel-cut.md): the panel is the one owner + // of "is this point within my own radius" -- both the reuse decision (ShieldInterceptor. + // FindReusable) and the strike gate below (Hit) go through this so a hit outside a panel's + // own tiling can never reach it, however it got routed. `radiusScale` is clamped to at most + // 1 -- ReuseFraction's authored [0,1] range narrows the reuse catchment; it must never widen + // it past the panel's own physical radius, which is what Hit gates on. + public bool ContainsWorldPoint(Vector3 worldPoint, float radiusScale = 1f) + { + Vector3 local = transform.InverseTransformPoint(worldPoint); + float scale = Mathf.Clamp01(radiusScale); + float r = panelRadius * scale; + return local.x * local.x + local.y * local.y <= r * r; + } + /// Strike the panel. worldDir is the projectile's travel direction. public void Hit(Vector3 worldPos, Vector3 worldDir, float energy, FracturePattern pattern = FracturePattern.Radial, float radius = -1f) { if (data == null) return; + // R (operator, playing FieldShieldTest): a hit outside the panel's own radius must not + // reach it at all -- no injection, no temper loss, no tremble. See ContainsWorldPoint. + if (!ContainsWorldPoint(worldPos)) return; Vector3 local = transform.InverseTransformPoint(worldPos); Vector3 ldir = transform.InverseTransformDirection(worldDir).normalized;