-- P1 bot: chase puck, carry to offensive blue line, save state blN there, then shoot to continue. local sp=manager.machine.devices[":maincpu"].spaces["program"] local ports=manager.machine.ioport.ports local function F(p,b) return ports[":ctrl"..p..":mdpad:PAD"].fields["P"..p.." "..b] end local K={U=F(1,"Up"),D=F(1,"Down"),L=F(1,"Left"),R=F(1,"Right"),B=F(1,"B"),C=F(1,"C")} 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 function pos(i) return w(ob(i)),w(ob(i)+0x14) end local lf=io.open("blsave.txt","w") local fr=0; local ph="chase"; local t=0; local saved=0; local spot local BL=tonumber(os.getenv("BLY") or "80") math.randomseed(7) local function steer(dx,dy,dead) for _,k in ipairs({"U","D","L","R"}) do K[k]:clear_value() end if dx*dx+dy*dy<=(dead or 0)^2 then return end local ax,ay=math.abs(dx),math.abs(dy) if ax>ay*0.41 then if dx>0 then K.R:set_value(1) else K.L:set_value(1) end end if ay>ax*0.41 then if dy>0 then K.U:set_value(1) else K.D:set_value(1) end end end emu.register_frame_done(function() fr=fr+1; if fr<450 then return end local c1=w(0xffc320); local own=w(0xffb7aa) if c1<0 or c1>11 then return end local x1,y1=pos(c1); local px,py=w(0xffb74a),w(0xffb75e) local gy=select(2,pos(5)); local dirY=(gy<0) and 1 or -1 if ph=="chase" then K.C:clear_value() if own==c1 and c1~=5 then ph="carry"; t=0; spot={math.random(-70,70),dirY*BL} else steer(px-x1,py-y1) end elseif ph=="carry" then t=t+1 if own~=c1 then ph="chase" return end if math.abs(y1-spot[2])<10 then saved=saved+1; manager.machine:save("bl"..saved) lf:write(string.format("bl%d fr=%d c1=%d pos=(%d,%d) dirY=%d\n",saved,fr,c1,x1,y1,dirY)); lf:flush() if saved>=12 then lf:close(); manager.machine:exit() return end ph="shoot"; t=0; K.C:set_value(1) else steer(spot[1]-x1,spot[2]-y1) end if t>200 then ph="chase" end elseif ph=="shoot" then t=t+1 if t==3 then K.C:clear_value() end if t>60 then ph="chase" end end end)