-- Scripted input + periodic screenshots/WRAM dumps. -- Env: TBG_OUT (output dir), TBG_PLAN = "frame:btn+btn[/hold],..." (default hold 4 frames), -- TBG_SHOTS = "f1,f2,...", TBG_END = last frame, TBG_SAVE_AT (frame to write /state.mss), -- TBG_LOAD (savestate loaded before frame 1; frame numbering restarts at 0) package.path = "/home/siege/Projects/tbg/re/lua/?.lua;" .. package.path local L = require("mech_lib") local out = os.getenv("TBG_OUT") local plan, shots = L.plan(os.getenv("TBG_PLAN")), {} for f in (os.getenv("TBG_SHOTS") or ""):gmatch("%d+") do shots[tonumber(f)] = true end local last = tonumber(os.getenv("TBG_END") or "600") local saveAt = tonumber(os.getenv("TBG_SAVE_AT") or "-1") local frame, held, holdLeft = 0, nil, 0 local running = not os.getenv("TBG_LOAD") if not running then L.load(os.getenv("TBG_LOAD"), function() running = true end) end emu.addEventCallback(function() if held and holdLeft > 0 then local t = {} for b in held:gmatch("[^+]+") do t[b] = true end emu.setInput(t, 0) end end, emu.eventType.inputPolled) emu.addEventCallback(function() if not running then return end frame = frame + 1 holdLeft = holdLeft - 1 if plan[frame] then held, holdLeft = plan[frame][1], plan[frame][2] end if frame == saveAt then L.save(out .. "/state.mss") end if shots[frame] then local h = io.open(string.format("%s/f%05d.png", out, frame), "wb"); h:write(emu.takeScreenshot()); h:close() h = io.open(string.format("%s/f%05d.wram", out, frame), "wb"); h:write(L.wram(0, 0x20000)); h:close() end if frame >= last then emu.stop(0) end end, emu.eventType.endFrame)