local x, y, camx, camy, ttype, ttypehigh, tcolor, warpdest
function bitswap (swappy)
nib2 = bit.band(swappy,0xF)
return (nib2*0x10+bit.rshift(swappy,4))
end
function gettile (wx,wy)
hix = bit.rshift(bit.band(0xFF00,wx),8)
hiy = bit.rshift(bit.band(0xFF00,wy),8)
lox = bit.band(0xFF,wx)
loy = bit.band(0xFF,wy)
ccea = bit.band(bitswap(bit.band(hiy,0x0F))+bit.band(bitswap(loy),0x0F)+0xA0,0xFF)
cceb = bitswap(bit.band(hix,0x0F))+bit.band(bitswap(lox),0x0F)
rawloc = ccea*0x100+cceb -- not the final location!!! can vary if above 0xa000
return (rawloc)
-- return (bit.band(0x2000 + 0x100*math.floor(wy/16+1) + math.floor(wx/16),0x7FFF)) works for most space, not glitch rooms
end
function tileid (ntile)
if (ntile >= 0xa000) then
memory.usememorydomain('Cart RAM') -- where normal level data is
realloc = ntile - 0x8000
else
memory.usememorydomain('System Bus')
realloc = ntile
end
tlookup = memory.readbyte(realloc)
return (mainmemory.read_u16_le(0xd00+bit.band(tlookup*2,0xFF)))
end
while true do
x = mainmemory.read_u16_be(0xa63) -- position in level
y = mainmemory.read_u16_be(0xa61)
camx = mainmemory.readbyte(0xa88) -- position relative to upper left camera edge
camy = mainmemory.readbyte(0xa87)
-- warpdest = mainmemory.readbyte(0xa1) -- sector coordinates for a warp (??)
-- 160x144
--gui.drawText(3,130,string.format("%X",bit.rshift(warpdest,4))..' '..string.format("%X",bit.band(warpdest,0xF)))
for i = -1,17,1 do
for j = -1,17,1 do
ttype = tileid(gettile(x-camx+15+16*i,y-camy+15+16*j))
ttypehigh = bit.band(ttype,0xFF00)
if (ttype~=0x4020) and (ttype~=0x4821) and (ttype~=0x484c) and (ttype~=0x4262) then
if (ttype==0x45CB) or (ttype==0x45FA) or (ttype==0x477e) or (ttype==0x46f9) or (ttype==0x47b0) or (ttype==0x47de) then -- door, golf door, exit, warp tile
tcolor = 'BLACK'
elseif (ttype>0x49F0) and (ttype<0x54b4) then -- breakable
tcolor = 'PINK'
elseif (ttype==0x4208) or (ttype==0x49ae) or (ttype==0x423f) then -- platform
tcolor = 'WHITE'
elseif (ttype==0x42c3) or (ttype==0x42e5) or (ttype==0x42fe) or (ttype==0x4934) then -- ladder
tcolor = 'BROWN'
elseif (ttype>0x41a0) and (ttype<0x41ff) then -- water
tcolor = 'BLUE'
else -- solid or unknown
tcolor = 'RED'
end
gui.drawBox((camx-x)%16-8+16*i,(camy-y)%16-16+16*j,(camx-x)%16+7+16*i,(camy-y)%16+16*j-1,tcolor)
end
end
end
gui.drawBox(camx-15,camy-45,camx-1,camy-17,'GREEN')
gui.drawText(3,3,string.format("%X",gettile(x,y-32))..' '..string.format("%X",tileid(gettile(x,y-32))))
gui.drawText(3,12,string.format("%X",gettile(x,y-16))..' '..string.format("%X",tileid(gettile(x,y-16))))
gui.drawText(3,21,string.format("%X",gettile(x,y))..' '..string.format("%X",tileid(gettile(x,y))))
emu.frameadvance()
end