# -*- coding: utf-8 -*- """圖:遊戲一幀內的執行順序(依 MouseTrailCollector 程式碼的 DefaultExecutionOrder 與 Update/LateUpdate 整理)。 輸出 results_1/figures/fig_frame_flow.png """ import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from matplotlib.patches import FancyBboxPatch, FancyArrowPatch from mtd_common import RESULTS INK, MUTED, GRID = '#1f2328', '#6b7280', '#d9dde3' BAND = {'in': '#eaf2fc', 'upd': '#fff4ea', 'late': '#eafaf3', 'out': '#f3f4f6'} EDGE = {'in': '#2a78d6', 'upd': '#eb6834', 'late': '#1baf7a', 'out': '#9ca3af'} def box(ax, x, y, w, h, title, body, kind): ax.add_patch(FancyBboxPatch((x, y), w, h, boxstyle='round,pad=0.02,rounding_size=0.12', fc='white', ec=EDGE[kind], lw=1.6)) ax.text(x + 0.12, y + h - 0.16, title, fontsize=10.5, fontweight='bold', color=INK, va='top') ax.text(x + 0.12, y + h - 0.52, body, fontsize=8.8, color=INK, va='top', linespacing=1.35) def arrow(ax, p, q, text=None, dashed=False): ax.add_patch(FancyArrowPatch(p, q, arrowstyle='-|>', mutation_scale=12, lw=1.3, color=MUTED, linestyle='--' if dashed else '-', shrinkA=0, shrinkB=0)) if text: ax.text((p[0] + q[0]) / 2 + 0.08, (p[1] + q[1]) / 2, text, fontsize=8, color=MUTED, va='center') def main(): plt.rcParams['font.family'] = ['Microsoft JhengHei', 'DejaVu Sans'] plt.rcParams['axes.unicode_minus'] = False fig, ax = plt.subplots(figsize=(10.2, 11.2)) ax.set_xlim(0, 10.2); ax.set_ylim(0, 11.2); ax.axis('off') def band(y0, y1, label, kind): ax.add_patch(FancyBboxPatch((0.15, y0), 9.9, y1 - y0, boxstyle='round,pad=0.01,rounding_size=0.08', fc=BAND[kind], ec='none')) ax.text(0.3, y1 - 0.08, label, fontsize=9.5, color=EDGE[kind], fontweight='bold', va='top') # 背景執行緒(幀外) band(9.85, 11.1, '幀外:Raw Input 背景執行緒(持續運作)', 'in') box(ax, 0.5, 9.95, 9.2, 0.85, 'RawMouseInput(WndProc,背景執行緒)', '每收到一筆 WM_INPUT 立即以 Stopwatch 蓋時間戳,換算到 Unity 的 realtime 時間軸,推入佇列(t, dx, dy, left)', 'in') # 幀開頭 band(8.3, 9.65, '幀開頭:Update,執行順序 −10000', 'in') box(ax, 0.5, 8.4, 9.2, 0.85, 'RawMouseInputRunner.Update → RawMouseInput.BeginFrame', '把佇列中累積的回報全部取出成 FrameEvents;算出本幀總位移 FrameDelta 與左鍵狀態', 'in') arrow(ax, (5.1, 9.95), (5.1, 9.25)) # Update band(4.35, 8.1, 'Update:預設執行順序(各腳本先後由引擎決定)', 'upd') box(ax, 0.5, 6.2, 4.45, 1.45, 'PlayerLook.Update', '位移 = FrameDelta + 待套用的輔助位移\n→ 更新 yaw / pitch,後座力偏移另行疊加\n→ 記下 LastMouseDelta、LastAssistDelta', 'upd') box(ax, 5.25, 6.2, 4.45, 1.45, 'AimAssist.Update', '讀 EnemyPerceptor.Output(上一幀 LateUpdate 的結果)\n→ 新取樣則重設剩餘誤差 → 算本幀修正量\n→ PlayerLook.AddAssistDelta(度 → 滑鼠單位)', 'upd') box(ax, 0.5, 4.5, 4.45, 1.45, 'HitscanWeapon.Update', '讀 Raw Input 左鍵 → 射線命中判定\n→ Fired / HitLanded 事件 → PlayerLook.AddRecoil', 'upd') box(ax, 5.25, 4.5, 4.45, 1.45, 'BotBrain + PlayerController(MotionCore)', '機器人 AI 餵輸入;玩家與機器人的移動物理\n在 FixedUpdate 由同一套 PlayerController 處理', 'upd') arrow(ax, (5.1, 8.4), (5.1, 8.1)) arrow(ax, (5.25, 6.85), (4.95, 6.85), dashed=True) # LateUpdate band(1.35, 4.15, 'LateUpdate:視角與敵人位置皆已更新', 'late') box(ax, 0.5, 2.85, 9.2, 1.0, 'EnemyPerceptor.LateUpdate(執行順序 0)', 'Truth = 以相機對敵人各部位取樣,算角度誤差與遮擋比例;到刷新率取樣時刻則入佇列並決定該次延遲(含抖動)\n' '擷取時間 + 延遲已到的樣本 → Output(輔助下一幀才讀得到)', 'late') box(ax, 0.5, 1.45, 9.2, 1.3, 'RoundRecorder.LateUpdate(執行順序 100)', '先寫暫停 / 換彈事件 → FrameEvents 每筆寫入 mouse_N.csv(各自到達時間,frame = 本幀列號)\n' '→ frames_N.csv 寫一列:t, dt, 視角, 後座力, mouse_dx, assist_dx, 玩家與機器人狀態, Truth, Output, assist_engaged\n' '→ 機器人已倒下:寫 kill,Commit 三個 CSV 與 config_N.json', 'late') arrow(ax, (5.1, 4.5), (5.1, 4.15)) arrow(ax, (5.1, 2.85), (5.1, 2.75)) # 渲染 band(0.15, 1.2, '幀尾:渲染', 'out') box(ax, 0.5, 0.25, 9.2, 0.7, '畫面輸出', '以本幀的相機旋轉渲染;感知器以螢幕刷新率取樣,對應外掛擷取畫面的頻率', 'out') arrow(ax, (5.1, 1.45), (5.1, 1.2)) out = RESULTS / 'figures' / 'fig_frame_flow.png' out.parent.mkdir(parents=True, exist_ok=True) fig.savefig(out, dpi=200, bbox_inches='tight', facecolor='white') print('wrote', out) if __name__ == '__main__': main()