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","D"} 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-3 and t<=PT+8 then local p2=w(0xffc322); if p2>=0 and p2<12 then local a=ob(p2)+0x62; sp:write_u8(a,sp:read_u8(a)&0xf7) end end if t==PT+1 then local best,bd=-1,1e9; local gx,gy=w(ob(5)),w(ob(5)+0x14) for i=0,4 do local d=(w(ob(i))-gx)^2+(w(ob(i)+0x14)-gy)^2; if d=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~=5 then cur.left=true end; if own==5 and not cur.left and t=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("near=%d st%d %s tgt=%d p2=%d dist=%s v=%d -> %s(%d) after %d\n",NEAR or -1,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)