tools/playthrough.js
167 lines · 7362 bytes
1#!/usr/bin/env node
2// Headless playthrough for the 1MB roguelike.
3// Usage: node tools/playthrough.js [seedCount] [mode] [gameDir]
4// node tools/playthrough.js --seeds <base36,...> [mode] [gameDir]
5//
6// Plays full runs through the real engine: floors 1-5 with a camp choice
7// between each, the finale (floor 6), then the postgame floor 7. Each floor
8// is split into goals (torch, bow, crown + key into the beast den, leave)
9// and each goal is a best-first search over real game states (most torch
10// first), keeping the best end state per flag combination and backtracking
11// when a later goal fails. Every winning
12// input sequence is then replayed from a fresh run through key(), the real
13// input handler, and must end in a win on floor 7.
14"use strict"
15const path = require("path")
16const vm = require("vm")
17
18const only = process.argv[2] == "--seeds" ? process.argv.splice(2, 2)[1].split(",").map(v => parseInt(v, 36)) : null
19const seedCount = only ? only.length : Math.max(1, parseInt(process.argv[2], 10) || 50)
20const mode = process.argv[3] || "standard"
21const gameDir = process.argv[4] || path.join(__dirname, "..", "1mb")
22const ctx = require("./engine").load(gameDir)
23
24vm.runInContext(`
25const BOT_ACTS={U:()=>step(0,-1),D:()=>step(0,1),L:()=>step(-1,0),R:()=>step(1,0),".":()=>wait(),t:()=>dropTorch()}
26const BOT_KEYS={U:"ArrowUp",D:"ArrowDown",L:"ArrowLeft",R:"ArrowRight",".":".",t:"t"}
27const LEG_LIMIT=2000000
28let botBudget=0,botDeep=[0,0]
29const GOAL_NAMES=["torch","bow","crown and key into the den","leave"]
30function botBits(){let b="";for(const k in O)for(const o of O[k])b+=o.on?1:0;return b}
31// Position, flags, overlays, and the turn parity and beast only where they act; torch, ward, dread and chase are compared by dominance.
32function botKey(){const f=S.flags,d=S.drop;return [S.r,S.x,S.y,dark()&&!f.torch?S.turn&3:"",S.gameState,S.level,!!f.torch,f.ash,f.idol,f.gold,f.crown,f.key,f.mask,f.relic,f.relicPlus,S.r=="e"?S.beast.x+","+S.beast.y:"",d?[d.r,d.x,d.y,d.fuel].join():"",botBits()].join("|")}
33function botRes(){return [S.flags.torch,S.flags.ward,-S.dread,-S.chase]}
34// Records the state; false when a recorded state with the same key is at least as good in every resource.
35function botNew(seen){
36 const k=botKey(),v=botRes(),list=seen.get(k)
37 if(!list){seen.set(k,[v]);return true}
38 if(list.some(o=>o.every((x,i)=>x>=v[i])))return false
39 list.push(v);return true
40}
41function botSnap(){return JSON.stringify(S)+"\\u0000"+botBits()}
42function botRestore(p,world){
43 R=world.R;O=world.O;V=world.V
44 const i=p.lastIndexOf("\\u0000"),b=p.slice(i+1)
45 S=JSON.parse(p.slice(0,i))
46 let j=0;for(const k in O)for(const o of O[k])o.on=b[j++]=="1"
47}
48function botFull(){return JSON.stringify({S,R,O,V})}
49function botLoad(f){const x=JSON.parse(f);S=x.S;R=x.R;O=x.O;V=x.V}
50// Distinct end states by route-relevant flags, keeping the most torch for each.
51function botSig(){const f=S.flags;return [S.gameState,S.level,S.r,f.gold,f.mask,f.ward>0,f.relic].join()}
52function botLeg(goal,last){
53 if(goal())return [[botFull(),""]]
54 const world={R,O,V},start=botSnap(),seen=new Map(),buckets=[],ends=new Map()
55 const push=(p,path)=>{const t=Math.min(S.flags.torch,63);(buckets[t]=buckets[t]||{h:0,a:[]}).a.push([p,path])}
56 botNew(seen);push(start,"")
57 let n=0
58 for(;;){
59 let b=buckets.length-1;while(b>=0&&!(buckets[b]&&buckets[b].h<buckets[b].a.length))b--
60 if(b<0||n>=LEG_LIMIT)break
61 const bk=buckets[b],[p,path]=bk.a[bk.h];bk.a[bk.h++]=null
62 for(const a in BOT_ACTS){
63 botRestore(p,world)
64 if((a=="."||a=="t")&&S.r!="e")continue
65 BOT_ACTS[a]();n++
66 if(!S.alive||S.gameState=="dead")continue
67 if(!botNew(seen))continue
68 // Only the last goal (the escape) may wander the beast den.
69 const hit=goal()
70 if(!last&&!hit&&S.r=="e")continue
71 if(hit){
72 const g=botSig(),e=ends.get(g)
73 if(!e||S.flags.torch>e[2])ends.set(g,[botFull(),path+a,S.flags.torch])
74 continue
75 }
76 push(botSnap(),path+a)
77 }
78 }
79 botBudget-=n
80 botRestore(start,world)
81 return [...ends.values()].sort((a,b)=>b[2]-a[2])
82}
83function botGoals(L){
84 const done=()=>S.gameState!="play"||S.level!=L
85 return L==6?[done]:[()=>S.flags.torch>0,()=>S.flags.idol,()=>S.flags.crown&&S.flags.key&&(S.r=="e"||!R.e),done]
86}
87function botSolve(goals,i,cont){
88 if(botBudget<0)return null
89 if(i==goals.length)return cont()
90 const at=[S.level,goals.length==1?3:i];if(at[0]>botDeep[0]||at[0]==botDeep[0]&&at[1]>botDeep[1])botDeep=at
91 for(const [f,path] of botLeg(goals[i],i==goals.length-1)){
92 botLoad(f)
93 const rest=botSolve(goals,i+1,cont)
94 if(rest!=null)return path+rest
95 }
96 return null
97}
98function botRun(){
99 const L=S.level
100 return botSolve(botGoals(L),0,()=>{
101 if(S.gameState=="win"){
102 if(S.level>=7)return ""
103 startPostgame()
104 const r=botRun()
105 return r==null?null:"|p"+r
106 }
107 if(S.gameState=="camp"){
108 const f=botFull()
109 for(const c of "1423"){
110 botLoad(f);applyCampChoice(c)
111 const r=botRun()
112 if(r!=null)return "|"+c+r
113 }
114 return null
115 }
116 if(S.level==6&&S.gameState=="play")return botRun()
117 return null
118 })
119}
120function botPlay(s,mode,budget){
121 seed=s;startRun(mode,0);botBudget=budget;botDeep=[0,0]
122 const moves=botRun()
123 return {moves,left:botBudget,stuck:"floor "+botDeep[0]+" "+GOAL_NAMES[botDeep[1]]}
124}
125function botReplay(s,mode,moves){
126 muted=1
127 seed=s;startRun(mode,0)
128 for(let i=0;i<moves.length;i++){
129 const c=moves[i]
130 if(c=="|"){key({key:moves[++i]});continue}
131 key({key:BOT_KEYS[c]})
132 }
133 return {state:S.gameState,level:S.level,turns:moves.replace(/\\|./g,"").length}
134}
135`, ctx, { filename: "playthrough-bot" })
136
137const seeds = only || JSON.parse(vm.runInContext(
138 `JSON.stringify((() => { const r = rng(1), out = []; for (let i = 0; i < ${seedCount}; i++) out.push(Math.floor(r() * 1e9)); return out })())`,
139 ctx, { filename: "playthrough-seeds" }))
140
141const t0 = Date.now()
142const fails = []
143let totalMoves = 0
144for (const s of seeds) {
145 const res = JSON.parse(vm.runInContext(`JSON.stringify(botPlay(${s}, ${JSON.stringify(mode)}, 30000000))`, ctx))
146 if (res.moves == null) {
147 fails.push({ seed: s, why: `${res.left < 0 ? "search budget exhausted" : "no winning line found"}, deepest: ${res.stuck}` })
148 console.log(` seed ${s.toString(36)}: NOT WON, ${fails[fails.length - 1].why}`)
149 continue
150 }
151 const rep = JSON.parse(vm.runInContext(`JSON.stringify(botReplay(${s}, ${JSON.stringify(mode)}, ${JSON.stringify(res.moves)}))`, ctx))
152 if (rep.state != "win" || rep.level != 7) {
153 fails.push({ seed: s, why: `replay ended ${rep.state} on floor ${rep.level}` })
154 console.log(` seed ${s.toString(36)}: NOT WON, ${fails[fails.length - 1].why}`)
155 continue
156 }
157 totalMoves += rep.turns
158 console.log(` seed ${s.toString(36)}: won in ${rep.turns} moves`)
159}
160const elapsed = ((Date.now() - t0) / 1000).toFixed(1)
161const won = seeds.length - fails.length
162if (fails.length) {
163 console.error(`playthrough: ${fails.length}/${seeds.length} ${mode} runs NOT WON (${elapsed}s)`)
164 for (const f of fails.slice(0, 20)) console.error(` seed ${f.seed.toString(36)}: ${f.why}`)
165 process.exit(1)
166}
167console.log(`playthrough: OK — ${won}/${seeds.length} ${mode} runs won floors 1-7 and replayed through key() (avg ${Math.round(totalMoves / won)} moves, ${elapsed}s)`)