自动化试玩(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。