using OSM.Client.Contracts; using OSM.Client.Screens.Battle; using OSM.Client.Units; using OSM.Client.World; namespace OSM.Client.Diagnostics; /// /// 阶段 E 的静态自检:快照差分、表现队列行为、HUD 版面。 /// 不连服务器,纯离线校验表现层规则——真实对局里的表现由 --solo-vertical-smoke-test /// 与 --solo-reconnect-smoke-test 覆盖。 /// public partial class PhaseESmokeTest : Node { private PackedScene _unitViewScene = null!; private int _skips; public override void _Ready() { _unitViewScene = GD.Load("res://src/Units/UnitView.tscn"); _ = RunAsync(); } private async Task RunAsync() { try { VerifySnapshotDiff(); VerifyAnimatorQueue(); VerifyGreyboxGround(); // Control 的版面在首帧之后才结算,等两帧再量。 await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame); await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame); var hudChecked = VerifyHudLayout(); GD.Print( "PHASE_E_OK diff=true animator=true cancellation=true ground=true " + $"hudLayout={hudChecked.ToString().ToLowerInvariant()}"); GetTree().Quit(0); } catch (Exception exception) { GD.PushError($"PHASE_E_FAILED {exception.Message}"); GetTree().Quit(1); } } // ==================== 快照差分 ==================== private static IReadOnlyList Build( SoloGameState? previous, SoloGameState? current) => BattleSnapshotDiff.Build(previous, current); private static void VerifySnapshotDiff() { if (Build(null, State(("a", 1, 100, false))).Count != 0) { throw new InvalidOperationException("首帧不应产生补间任务。"); } RequireSingle(Build(State(("a", 1, 100, false)), State(("a", 2, 100, false))), BattleAnimationKind.Move); // 同格子 + 掉血 = 纯受击,量值就是权威血量差 var hit = RequireSingle(Build(State(("a", 1, 100, false)), State(("a", 1, 73, false))), BattleAnimationKind.Hit); if (Math.Abs(hit.Magnitude - 27) > 0.001) { throw new InvalidOperationException($"受击量值应为 27,实际 {hit.Magnitude}。"); } var heal = RequireSingle(Build(State(("a", 1, 40, false)), State(("a", 1, 55, false))), BattleAnimationKind.Heal); if (Math.Abs(heal.Magnitude - 15) > 0.001) { throw new InvalidOperationException($"治疗量值应为 15,实际 {heal.Magnitude}。"); } // 致命一击:先受击再淘汰,顺序不能反 var kill = Build(State(("a", 5, 30, false)), State(("a", 5, 0, true))); if (kill.Count != 2 || kill[0].Kind != BattleAnimationKind.Hit || kill[1].Kind != BattleAnimationKind.Eliminate) { throw new InvalidOperationException( $"致命一击应产生 [Hit, Eliminate],实际 [{string.Join(",", kill.Select(item => item.Kind))}]。"); } var respawn = Build(State(("a", 5, 0, true)), State(("a", 9, 100, false))); if (!respawn.Any(item => item.Kind == BattleAnimationKind.Respawn)) { throw new InvalidOperationException("复活应产生 Respawn 任务。"); } // 中途出现的单位(召唤/新入场)直接以权威状态现身,不补间 if (Build(State(("a", 1, 100, false)), State(("a", 1, 100, false), ("b", 3, 50, false))).Count != 0) { throw new InvalidOperationException("新出现的单位不应补间。"); } // 移动 + 受击同时发生:两条都要有 var mixed = Build(State(("a", 1, 100, false)), State(("a", 4, 90, false))); if (mixed.Count != 2 || mixed[0].Kind != BattleAnimationKind.Move || mixed[1].Kind != BattleAnimationKind.Hit) { throw new InvalidOperationException( $"移动+受击应产生 [Move, Hit],实际 [{string.Join(",", mixed.Select(item => item.Kind))}]。"); } } private static BattleAnimationRequest RequireSingle( IReadOnlyList requests, BattleAnimationKind expected) { if (requests.Count != 1 || requests[0].Kind != expected) { throw new InvalidOperationException( $"期望只有一条 {expected},实际 [{string.Join(",", requests.Select(item => item.Kind))}]。"); } return requests[0]; } private static SoloGameState State( params (string Guid, long GridId, double Hp, bool Eliminated)[] characters) { return new SoloGameState { GameId = "phase-e", Round = 1, Characters = characters .Select(entry => new SoloCharacterDto { Guid = entry.Guid, GridId = entry.GridId, Hp = entry.Hp, MaxHp = 100, IsEliminated = entry.Eliminated }) .ToArray() }; } // ==================== 表现队列 ==================== private void VerifyAnimatorQueue() { var animator = new BattleAnimator(); // 故意不进场景树:手动推进 _Process,静态自检才具备确定性。 var unit = _unitViewScene.Instantiate(); AddChild(unit); unit.ApplySnapshot( new SoloCharacterDto { Guid = "a", Hp = 60, MaxHp = 100, GridId = 1 }, isFriendly: true, isCurrentActor: false, isSelectable: false, worldPosition: new Vector2(100, 100)); animator.Initialize(_ => unit); animator.SkipCalled += _ => _skips++; animator.Submit( [new BattleAnimationRequest("a", BattleAnimationKind.Move, 0)], _ => new Vector2(200, 100)); if (animator.PendingCount != 1) { throw new InvalidOperationException($"入队后应只有 1 条待播任务,实际 {animator.PendingCount}。"); } if (!animator.TakesOverPosition("a")) { throw new InvalidOperationException("有待播 Move 时表现层必须接管位置。"); } if (animator.TakesOverHp("a")) { throw new InvalidOperationException("只有 Move 时不应接管血条。"); } // 同棋子连续移动要合并(否则快速连招会把队列堆满过期补间) animator.Submit( [new BattleAnimationRequest("a", BattleAnimationKind.Move, 0)], _ => new Vector2(300, 100)); if (animator.PendingCount != 1 || animator.MergedTasks != 1) { throw new InvalidOperationException( $"连续移动应合并成 1 条,实际 {animator.PendingCount} 条 / 合并 {animator.MergedTasks} 次。"); } // 血量任务与移动任务互不干扰 animator.Submit( [new BattleAnimationRequest("a", BattleAnimationKind.Hit, 40)], _ => new Vector2(300, 100)); if (animator.PendingCount != 2 || !animator.TakesOverPosition("a") || !animator.TakesOverHp("a")) { throw new InvalidOperationException("受击任务应独立入队,并同时接管血条。"); } // 推进到队列排空:表现层必须能收敛,不能有卡住的任务 for (var step = 0; step < 400 && animator.IsBusy; step++) { animator._Process(0.05); } if (animator.IsBusy) { throw new InvalidOperationException("表现队列没有在合理步数内排空。"); } if (unit.Position.DistanceTo(new Vector2(300, 100)) > 0.5) { throw new InvalidOperationException( $"移动结束后棋子应停在权威位置 (300,100),实际 {unit.Position}。"); } // 积压:超过上限必须自动丢弃并要求对齐,而不是无限堆积 var skipsBefore = _skips; var backlog = Enumerable.Range(0, 80) .Select(index => new BattleAnimationRequest($"unit-{index}", BattleAnimationKind.Move, 0)) .ToArray(); animator.Submit(backlog, _ => new Vector2(10, 10)); if (animator.IsBusy) { throw new InvalidOperationException("积压时应全部丢弃并请求对齐。"); } if (_skips <= skipsBefore) { throw new InvalidOperationException("积压丢弃必须发出 SkipCalled,外部才会重新对齐权威快照。"); } // 手动跳过 skipsBefore = _skips; animator.Submit( [new BattleAnimationRequest("a", BattleAnimationKind.Move, 0)], _ => new Vector2(200, 200)); animator.SkipAll("手动跳过"); if (animator.IsBusy || _skips <= skipsBefore) { throw new InvalidOperationException("手动跳过必须清空队列并发出 SkipCalled。"); } // 这个 animator 故意没进场景树,得自己释放,否则 Godot 退出时会报 ObjectDB 泄漏。 animator.Free(); } // ==================== 灰盒地面(只画服务器真实存在的格子) ==================== /// /// 地面必须严格由权威地图数据驱动:稀疏地图只画那几个格,不按 Length×Width 铺满, /// 也不画协议里并不存在的掩体(旧实现硬编码了 4 个掩体)。 /// private void VerifyGreyboxGround() { var ground = new GreyboxMap2D(); AddChild(ground); var sparseMap = new SoloMapDto { Length = 12, Width = 12, Grids = [ new SoloGridDto { Id = 7, X = 3, Y = 1, Z = 0, Color = "Gray" }, new SoloGridDto { Id = 8, X = 3, Y = 2, Z = 1, Color = "Red" } ] }; ground.SetMap(sparseMap); if (ground.DrawnGrids.Count != 2) { throw new InvalidOperationException( $"地面应只画服务器给的 2 个格,实际 {ground.DrawnGrids.Count} 个。"); } // 再设一次尺寸不能凭空造出格子(旧实现按 MapSize 铺满矩形) ground.SetMapSize(new Vector2I(12, 12)); if (ground.DrawnGrids.Count != 2) { throw new InvalidOperationException("设置地图尺寸不应产生协议外的格子。"); } ground.SetMap(null); if (ground.DrawnGrids.Count != 0) { throw new InvalidOperationException("传 null 应清空地面。"); } ground.QueueFree(); } // ==================== HUD 版面(16:9 缩放) ==================== private bool VerifyHudLayout() { var viewportSize = GetViewport().GetVisibleRect().Size; if (viewportSize.X < 320 || viewportSize.Y < 240) { GD.PushWarning($"PHASE_E_HUD_SKIPPED 视口过小({viewportSize}),跳过版面检查。"); return false; } var battle = GetNode("/root/Main/BattleScreen"); var layout = battle.GetNode("HudLayer/SafeArea/Layout"); var dock = layout.GetNode("BottomRightActionDock"); var context = layout.GetNode("BottomContextBar"); var characterPanel = layout.GetNode("BottomLeftCharacterPanel"); var viewportRect = new Rect2(Vector2.Zero, viewportSize); // 操作按钮必须完整落在视口内(窗口缩到 16:9 的最小尺寸时也不能被裁掉) string[] buttons = [ "MoveButton", "AttackButton", "SkillButton", "SuperSkillButton", "ItemButton", "HideGridButton", "EndTurnButton", "LogButton" ]; foreach (var name in buttons) { var rect = dock.GetNode($"ActionGrid/{name}").GetGlobalRect(); if (!viewportRect.Encloses(rect)) { throw new InvalidOperationException( $"操作按钮 {name} 超出视口:{rect} 不在 {viewportRect} 内。"); } } var dockRect = dock.GetGlobalRect(); var contextRect = context.GetGlobalRect(); var characterRect = characterPanel.GetGlobalRect(); if (dockRect.Intersects(contextRect)) { throw new InvalidOperationException($"操作坞与上下文栏重叠:{dockRect} ∩ {contextRect}。"); } if (dockRect.Intersects(characterRect)) { throw new InvalidOperationException($"操作坞与角色面板重叠:{dockRect} ∩ {characterRect}。"); } if (contextRect.Intersects(characterRect)) { throw new InvalidOperationException($"上下文栏与角色面板重叠:{contextRect} ∩ {characterRect}。"); } return true; } }