local sp=manager.machine.devices[":maincpu"].spaces["program"] local ports=manager.machine.ioport.ports local function F(b) return ports[":ctrl1:mdpad:PAD"].fields["P1 "..b] end local K={U=F("Up"),D=F("Down"),L=F("Left"),R=F("Right"),B=F("B")} local function w(a) local v=sp:read_u16(a); if v>=0x8000 then v=v-0x10000 end return v end local function ob(i) return 0xffb04a+i*0x80 end local lf=io.open(os.getenv("GKOUT") or "gkev.txt","w") local dirs={"none","U","UR","UL","R","L"} local trials={} for s=1,8 do for _,d in ipairs(dirs) do trials[#trials+1]={s=s,d=d} end end local PT=tonumber(os.getenv("PT") or "120") local di=0; local t=-1; local fr=0; local cur local S={n=0,P2=0,CPU=0,OPP=0,loose=0,skip=0,tP2=0} local function clr() for _,k in pairs(K) do k:clear_value() end end emu.register_frame_done(function() fr=fr+1 if t<0 then if fr>5 then di=di+1 if di>#trials then lf:write(string.format("SUMMARY n=%d P2=%d CPUmate=%d OPP=%d loose=%d skip=%d targetP2=%d\n",S.n,S.P2,S.CPU,S.OPP,S.loose,S.skip,S.tP2)); lf:close(); manager.machine:exit() return end manager.machine:load("gkp"..trials[di].s); t=0; clr() end return end t=t+1; local tr=trials[di] if t==PT-6 and tr.d~="none" then for c in tr.d:gmatch(".") do K[c]:set_value(1) end end if t==PT then if w(0xffc320)~=5 or w(0xffb7aa)~=5 then S.skip=S.skip+1; lf:write(string.format("st%d %s SKIP c1=%d own=%d\n",tr.s,tr.d,w(0xffc320),w(0xffb7aa))); clr(); t=-1; fr=0; return end K.B:set_value(1); cur={p2=w(0xffc322)} end if t==PT+5 then clr() end if t==PT+4 then cur.tgt=w(0xffbee0); cur.v=math.floor(math.sqrt(w(0xffb772)^2+w(0xffb774)^2)) if cur.tgt>=0 and cur.tgt<12 then cur.d=math.floor(math.sqrt((w(ob(cur.tgt))-w(0xffb74a))^2+(w(ob(cur.tgt)+0x14)-w(0xffb75e))^2)) end end if t>PT+4 then local own=w(0xffb7aa) if (own>=0 and own<12) or t>PT+150 then local who if own==cur.p2 then who="P2" elseif own>=0 and own<=5 then who="CPU" elseif own>=6 and own<=11 then who="OPP" else who="loose" end S.n=S.n+1; S[who]=S[who]+1; if cur.tgt==cur.p2 then S.tP2=S.tP2+1 end lf:write(string.format("st%d %s tgt=%d p2=%d dist=%s v=%d -> %s(%d) after %d\n",tr.s,tr.d,cur.tgt,cur.p2,tostring(cur.d),cur.v,who,own,t-PT)); lf:flush() t=-1; fr=0 end end end)