自動化試玩(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。