mem=manager:machine().devices[":maincpu"].spaces["program"]
mem2=manager:machine():memory().regions[":maincpu"]
function hex(a,digits) digits = digits or 2 return string.format("%0"..digits.."x",a) end
function setlives(l) l=int(l) mem:write_u8(0xcc,l) end
function setscore(s) s=int(s) mem:write_u16(0xb8,s) end
function setmypos(x,y) mem:write_u16(0x2d,int(x)) mem:write_u16(0x31,int(y)) end
function setmydir(d) d=int(d) mem:write_u8(0x27,d&0xff) mem:write_u8(0x2a,(d&0xff00)>>8) end
function settankposx(x) mem:write_u16(0x2f,int(x)) end
function settankposy(y) mem:write_u16(0x33,int(y)) end
function settankpos(x,y) settankposx(x) settankposy(y) end
function settankdir(d) d=int(d) mem:write_u8(0x29,d&0xff) mem:write_u8(0x2c,(d&0xff00)>>8) end
function delay(delaytime) delaytime = delaytime or 4 emu.wait(delaytime * 1/60) end
function int(i) return math.floor(i) end
function movetank2()
-- move enemy tank in a triangle
movetable = { 10,0, 50,0, 50,10, 10,0, 50,-20, 10,0,}
for pos=1,#movetable-2,2 do
x1=movetable[pos+0] y1=movetable[pos+1]
x2=movetable[pos+2] y2=movetable[pos+3]
dist=math.sqrt((x2-x1)^2 + (y2-y1)^2)
vel=20/60
t=dist/vel
frames=t
--print(pos,x1,y1,x2,y2)
for f=1,frames do
x=(x2-x1)/frames * f + x1
y=(y2-y1)/frames * f + y1
settankpos(x*256,y*256)
setmypos(0,0)
setmydir(0)
print(f,frames,x,y)
delay(1)
end
end
-- spin the enemy tank or missile around 3 times
for i=1,3 do
for d=0,255 do
setmypos(0,0)
setmydir(0)
settankdir(d*256)
settankpos(0x600,0)
setlives(4)
setscore(d)
delay(2)
end
end
-- move me and fix the enemy tank right in front of me
movetable = { 10,0, 50,0, 50,10, 10,0, 50,-20, 10,0,}
for pos=1,#movetable-2,2 do
x1=movetable[pos+0] y1=movetable[pos+1]
x2=movetable[pos+2] y2=movetable[pos+3]
dist=math.sqrt((x2-x1)^2 + (y2-y1)^2)
vel=20/60
t=dist/vel
frames=t
--print(pos,x1,y1,x2,y2)
for f=1,frames do
x=(x2-x1)/frames * f + x1
y=(y2-y1)/frames * f + y1
setmypos(x*256,y*256)
settankpos(x*256+0x401,y*256)
setmydir(0)
setlives(4)
setscore(f)
print(f,frames,x,y)
delay(1)
end
end
end
co1=coroutine.create(movetank2) coroutine.resume(co1)