GitHub快速開始
指南

自動化試玩(Playtest)

用 playtest.yaml 定義自動化試玩,通過確定性輸入錄製無頭執行多組種子,並將測得的指標與遊戲目標對比。

import "github.com/nsgo-dev/game/pkg/playtest"

playtest.yaml

playtest.yaml 是帶版本號的純文本 YAML 檔案,併發布了 JSON Schema(https://game.nsgo.dev/schemas/playtest.v1.json)。未知欄位、缺失或不支援的版本號、重複的指標名以及非法目標都會被拒絕;EncodeConfig 寫出規範的確定性 YAML,載入後再儲存位元組完全一致。

# yaml-language-server: $schema=https://game.nsgo.dev/schemas/playtest.v1.json
version: 1
scope:
  project: dungeon-run
  scene: level_01
  template: platformer2d
runs:
  count: 20
  seedStart: 1
  rate: 60
  speed: 1
  maxDuration: 1m30s
bot:
  profile: platformer-runner
  strategy: Greedy route to the door, jumps at ledges and gaps, 120-220 ms reaction jitter
  reactionMin: 120ms
  reactionMax: 220ms
  params:
    jumpHoldMs: 220
metrics:
  - name: finished
    label: Finished
    measure:
      kind: outcome
      key: finished
      aggregate: count
    goal:
      summary: most first-timers clear floor 1
      source: brief.yaml
      min: 16
  - name: median_time
    label: Median time
    unit: s
    measure:
      kind: timer
      key: clear_time
      aggregate: median
    goal:
      summary: under a minute
      source: brief.yaml
      min: 40
      max: 60
  - name: frame_time
    label: Frame time
    unit: ms
    measure:
      kind: perf
      key: frame_ms
      aggregate: median
    goal:
      summary: 16.7 ms at 60 Hz
      source: game.yaml
      max: 16.7
  - name: coins_taken
    label: Coins taken
    unit: '%'
    measure:
      kind: ratio
      key: coins_taken
      denomKey: coins_total
      aggregate: mean
    goal:
      summary: every coin on a route
      source: playtest.yaml
      min: 100
  - name: deaths
    label: Deaths
    measure:
      kind: event
      key: death
      aggregate: sum
      heatMap: true

欄位說明

  • scope:專案名 project、必填的場景名 scene,以及可選的模板名 template。
  • runs:執行次數 count、可選顯式種子列表 seeds、順序種子起點 seedStart(預設為 1)、固定步頻 rate(預設 60)、倍速後設資料 speed(預設 1),以及單次執行的模擬時長上限 maxDuration(預設 60s)。
  • bot:機器人配置名 profile(例如 platformer-runner、topdown-explorer、shooter、puzzle-solver 或 card-ai)、可選的策略描述 strategy、人類反應抖動區間 reactionMin / reactionMax,以及可選的標量引數 params。
  • metrics:每項指標有唯一的 name、可選的顯示名稱 label 與單位 unit、測量方式 measure 及可選目標 goal:
    • measure.kind 可選 event、counter、timer、outcome、ratio(需提供 denomKey)或 perf(frame_ms、update_ms、draw_ms、p50_frame_ms、p95_frame_ms、p99_frame_ms、max_frame_ms、allocs_per_frame)。
    • measure.aggregate 可選 count、sum、mean、median、min、max、p95、p99 或 rate。
    • 在 event 指標上設定 measure.heatMap: true 會把所有事件座標收集到報告的 heatMaps 欄位中。
    • goal 指定來原始檔 source(如 brief.yaml、game.yaml 或 playtest.yaml)、可選的摘要 summary,以及 min、max(或區間)或 equal。

在 Go 程式碼中上報指標

每次執行時,遊戲會收到一個 *playtest.Collector,用來上報帶座標的事件、計數器、計時器和結果。所有方法都支援 nil 接收者,因此遊戲程式碼無需區分實機執行和自動化試玩:

metrics.StartTimer("clear_time")
metrics.SetCounter("coins_total", float64(len(coins)))

// 拾取金幣時:
metrics.Event("pickup", float64(cx), float64(cy), coin.ID)
metrics.Inc("coins_taken")

// 死亡時:
metrics.Event("death", float64(px), float64(py), "slime_pit")

// 通關或失敗時:
metrics.StopTimer("clear_time")
metrics.Finish() // 或 metrics.Fail("out of lives")

幀耗時來自效能讀數:在 playtest.Sim 上傳入 FrameStats: stats 和 GC: gcMon(或呼叫 metrics.RecordPerf(stats, gcMon)),其均值和分位計算方式與 game/mcp 的 game_perf_readout 一致。

要在 playtest.report.yaml 中輸出柱狀序列圖與對局矩陣,可呼叫 metrics.ConfigureSeries / metrics.AddSeries / metrics.SetSeries / metrics.FlagSeries(寫入 report.Series,如各波受擊次數或每關最短步數)以及 metrics.ConfigureMatrix / metrics.RecordMatchup(寫入 report.Matrices,如卡組對陣勝率矩陣)。

內建機器人配置

所有內建機器人對同一隨機種子均完全確定,並通過 playtest.RegisterBot 註冊:

  • platformer-runner(playtest.ProfilePlatformerRunner,對應 platformer2d 模板):沿水平方向貪心奔向 PlatformerObservation.ExitX,在遇到臺階、坑洞和危險時,以每個種子獨立的 random.Stream 在 [reactionMin, reactionMax] 內取樣的確定性反應抖動起跳。使用 playtest.ObservePlatformerGrid 可以從 physics.TileGrid 和危險碰撞箱構造 PlatformerObservation。
  • topdown-explorer(playtest.ProfileTopDownExplorer,對應 rpg2d 模板):通過 BFS 走向最近的未探索格子、與每個 NPC 對話一次、開啟每個寶箱,並在達成目標或超過 runs.maxDuration 時停止(在 rpg2d 的 playtest.yaml 中配置為 8 倍速)。傳入 ExplorerObservation 並記錄 goal_time、tiles_seen/tiles_total 及開啟 heatMap: true 的 tile_visit 事件即可生成每格停留時間熱力圖。
  • shooter(playtest.ProfileShooter,對應 shmup2d 模板):持續開火、拾取 powerUpRange(預設 80 畫素)內的強化道具,並通過帶固定反應延遲的感知緩衝躲避來襲彈幕(reactionMin == reactionMax == 180ms 時為精確 180ms 反應時間)。使用 RecordShooterWave、RecordShooterHit 和 RecordShooterPowerUp 可上報每波受擊數(hits_per_wave 序列)與道具拾取率。
  • puzzle-solver(playtest.ProfilePuzzleSolver,對應 puzzle2d 模板):結合 SolvePuzzleBFS(帶 stateLimit 狀態數上限、預設 200 萬狀態的廣度優先最短解搜尋)和連續走錯 undoAfterBadMoves(預設 3)步後撤回的擬人玩家。使用 EvaluatePuzzlePack 或 NewPuzzlePackSim 可上報可解關卡數、超出標準步數、難度曲線倒掛次數以及短於預期解法的關卡。
  • card-ai(playtest.ProfileCardAI,對應 cards2d 模板):兩名向前看一回合的貪心 AI(CardAI.ChooseAction)對每對卡組交替先手對戰 matchesPerPair 局。使用 RunCardTournament 或 NewCardTournamentSim 可上報先手勝率、對局回合數中位數、最懸殊卡組對陣勝率、確定性思考耗時以及 deck_win_rates 勝率矩陣。

可以用 playtest.RegisterBot(name, factory) 註冊自定義機器人配置。rpg2d、shmup2d、puzzle2d 和 cards2d 模板均內建了對應機器人的規範 playtest.yaml。

無頭執行與回放

直接呼叫 playtest.Run(cfg, makeSim, opts),或在測試中呼叫 playtest.RunTest 供 nsgo game playtest 驅動:

func TestLevel1Playtest(t *testing.T) {
	playtest.RunTest(t, "playtest.yaml", makeSim)
}

隨後在專案目錄執行:

nsgo game playtest
nsgo game playtest -config playtest.yaml -out playtest.report.yaml -recordings playtest-recordings
nsgo game playtest -check

每個種子的執行都會通過 replay.Recorder 記錄輸入並寫出 <recordings>/run_NNN_seed_<seed>.replay.yaml,因此任意一次執行都可以用 replay.NewPlayer 確定性回放或轉為 game replay 迴歸測試。輸出的 YAML 報告(playtest.report.yaml,version: 1)包含每次執行的結果、各項指標值、目標達成狀態(met、missed、info)、熱力圖座標以及錄製檔案路徑。

編輯器 MCP 工具

編輯器的 MCP 伺服器(game/editor/editormcp,通過 nsgo game mcp 接入)提供兩個試玩工具:

  • game_playtest(run):校驗 playtest.yaml,無頭執行機器人並錄製回放,寫出 YAML 報告,返回結構化報告和彙總文本。
  • playtest_define(edit):校驗提議的新指標,生成規範化的 playtest.yaml 差異並作為待審查提案排隊(Server.PlaytestReviews、Server.ApplyPlaytestReview、Server.DismissPlaytestReview),在使用者確認前不會改寫磁碟上的 playtest.yaml。
編輯此頁