Advertisement
Guest User

terranigma_collision_map.lua

a guest
Jun 27th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.03 KB | None | 0 0
  1. -- These tile IDs encode open/cleared tiles where motion is possible
  2. local clear = {}
  3. for i,v in pairs({0x00, 0x02, 0x04, 0x22, 0x2c, 0x3a}) do clear[v]=true  end
  4.  
  5. -- These tile IDs encode blocked tiles that cannot be entered
  6. local blocked = {}
  7. for i,v in pairs({0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x28, 0x2a, 0x32}) do blocked[v]=true end
  8.  
  9. local tiles = {}
  10. local camx = memory.readword(0x7e080e)
  11. local camy = memory.readword(0x7e0812)
  12. local playerx = memory.readword(0x7e1000)
  13. local playery = memory.readword(0x7e1002)
  14.  
  15. local function getCollisionData()
  16.   camx = memory.readword(0x7e080e)
  17.   camy = memory.readword(0x7e0812)
  18.   playerx = memory.readword(0x7e1000)
  19.   playery = memory.readword(0x7e1002)
  20.  
  21.   local width = memory.readbyte(0x7e0827)*16
  22.   tiles = {}
  23.  
  24.   for y=math.floor(camy/16),math.floor(camy/16)+14 do
  25.     tiles[y] = {}
  26.     for x=math.floor(camx/16),math.floor(camx/16)+16 do
  27.  
  28.       tiles[y][x] = AND(memory.readbyte(0x7ea001+2*(y*width+x)),0xfe)
  29.  
  30.     end
  31.   end
  32. end
  33.  
  34. local function tileExists(x,y)  return tiles[y] and tiles[y][x] end
  35.  
  36. local function tileCleared(x,y) return tileExists(x,y) and clear[tiles[y][x]]   end
  37. local function tileBlocked(x,y) return tileExists(x,y) and blocked[tiles[y][x]] end
  38.  
  39.  
  40. local function renderArrowBody(scrx,scry)
  41.   gui.box(scrx+7,scry+3,scrx+9,scry+12,0xFFFFFFFF,0x000000FF)
  42. end
  43.  
  44. local function renderArrowHeadUp(scrx,scry)
  45.  
  46.   gui.line(scrx+5,scry+5,scrx+8,scry+2,0x000000FF)
  47.   gui.line(scrx+11,scry+5,scrx+8,scry+2,0x000000FF)
  48.  
  49.   gui.line(scrx+7,scry+6,scrx+5,scry+6,0x000000FF)
  50.   gui.line(scrx+9,scry+6,scrx+11,scry+6,0x000000FF)
  51.  
  52.   gui.pixel(scrx+8,scry+3,0xFFFFFFFF)
  53.   gui.line(scrx+7,scry+4,scrx+9,scry+4,0xFFFFFFFF)
  54.   gui.line(scrx+6,scry+5,scrx+10,scry+5,0xFFFFFFFF)
  55.  
  56. end
  57.  
  58. local function renderArrowHeadDown(scrx,scry)
  59.   gui.line(scrx+5,scry+10,scrx+8,scry+13,0x000000FF)
  60.   gui.line(scrx+11,scry+10,scrx+8,scry+13,0x000000FF)
  61.  
  62.   gui.line(scrx+7,scry+9,scrx+5,scry+9,0x000000FF)
  63.   gui.line(scrx+9,scry+9,scrx+11,scry+9,0x000000FF)
  64.  
  65.   gui.pixel(scrx+8,scry+12,0xFFFFFFFF)
  66.   gui.line(scrx+7,scry+11,scrx+9,scry+11,0xFFFFFFFF)
  67.   gui.line(scrx+6,scry+10,scrx+10,scry+10,0xFFFFFFFF)
  68. end
  69.  
  70.  
  71. local function renderTile(x,y)
  72.   if not tileExists(x,y) or tileCleared(x,y) then return end
  73.  
  74.   local tile = tiles[y][x]
  75.  
  76.   local scrx = 16*x-camx
  77.   local scry = 16*y-camy-1
  78.  
  79.   if tileBlocked(x,y) then
  80.  
  81.     gui.box(scrx-1,scry-1,scrx+16,scry+16,0xFF000080,0)
  82.     if not tileBlocked(x,y-1) then gui.line(scrx,scry,scrx+15,scry,0xFF0000FF) end
  83.     if not tileBlocked(x,y+1) then gui.line(scrx,scry+15,scrx+15,scry+15,0xFF0000FF) end
  84.     if not tileBlocked(x-1,y) then gui.line(scrx,scry,scrx,scry+15,0xFF0000FF) end
  85.     if not tileBlocked(x+1,y) then gui.line(scrx+15,scry,scrx+15,scry+15,0xFF0000FF) end
  86.  
  87.     if not tileBlocked(x-1,y-1) then gui.pixel(scrx,scry,0xFF0000FF) end
  88.     if not tileBlocked(x+1,y+1) then gui.pixel(scrx+15,scry+15,0xFF0000FF) end
  89.     if not tileBlocked(x-1,y+1) then gui.pixel(scrx,scry+15,0xFF0000FF) end
  90.     if not tileBlocked(x+1,y-1) then gui.pixel(scrx+15,scry,0xFF0000FF) end
  91.  
  92.  
  93.   elseif tile==0x0c then -- leftdown/rightup
  94.    
  95.     gui.line(scrx+15,scry,scrx,scry+15,0xFF0000FF)
  96.     if tileCleared(x,y-1) and tileCleared(x-1,y) then
  97.       gui.line(scrx+15,scry,scrx,scry,0xFF000080)
  98.       gui.line(scrx,scry+1,scrx,scry+15,0xFF000080)
  99.     else
  100.       for i=14,0,-1 do gui.line(scrx,scry+i,scrx+i,scry,0xFF000080) end
  101.     end
  102.  
  103.     if tileCleared(x,y+1) and tileCleared(x+1,y) then
  104.       gui.line(scrx+15,scry,scrx+15,scry+15,0xFF000080)
  105.       gui.line(scrx+14,scry+15,scrx,scry+15,0xFF000080)
  106.     else
  107.       for i=1,15 do gui.line(scrx+i,scry+15,scrx+15,scry+i,0xFF000080) end
  108.     end
  109.  
  110.   elseif tile==0x0e then -- leftup/rightdown
  111.    
  112.     gui.line(scrx,scry,scrx+15,scry+15,0xFF0000FF)
  113.     if tileCleared(x,y-1) and tileCleared(x+1,y) then
  114.       gui.line(scrx,scry,scrx+15,scry,0xFF000080)
  115.       gui.line(scrx+15,scry+1,scrx+15,scry+15,0xFF000080)
  116.     else
  117.       for i=0,14 do gui.line(scrx+i,scry+i,scrx+15,scry+i,0xFF000080) end
  118.     end
  119.  
  120.     if tileCleared(x,y+1) and tileCleared(x-1,y) then
  121.       gui.line(scrx,scry,scrx,scry+15,0xFF000080)
  122.       gui.line(scrx+1,scry+15,scrx+15,scry+15,0xFF000080)
  123.     else
  124.       for i=1,15 do gui.line(scrx+0,scry+i,scrx+i,scry+i,0xFF000080) end
  125.     end
  126.  
  127.  
  128.   elseif tile==0x10 or tile==0x34 or tile==0x36 or tile==0x3e then  -- Fall/Vine/Crawl/Claw climb
  129.  
  130.     gui.box(scrx-1,scry-1,scrx+16,scry+16,0x00000080,0)
  131.  
  132.     if tileExists(x,y-1) and tiles[y-1][x]~=tile then gui.line(scrx,scry,scrx+15,scry,0x000000FF) end
  133.     if tileExists(x,y+1) and tiles[y+1][x]~=tile then gui.line(scrx,scry+15,scrx+15,scry+15,0x000000FF) end
  134.     if tileExists(x-1,y) and tiles[y][x-1]~=tile then gui.line(scrx,scry,scrx,scry+15,0x000000FF) end
  135.     if tileExists(x+1,y) and tiles[y][x+1]~=tile then gui.line(scrx+15,scry,scrx+15,scry+15,0x000000FF) end
  136.  
  137.     renderArrowBody(scrx,scry)
  138.     if tile~=0x10 then renderArrowHeadUp(scrx,scry) end
  139.     renderArrowHeadDown(scrx,scry)
  140.  
  141.   elseif AND(tiles[y][x],0x80)~=0 then
  142.  
  143.     gui.box(scrx,scry,scrx+15,scry+15,0xFF800080)
  144.      gui.text(scrx+2,scry+4,"NPC")
  145.  
  146.  
  147.  
  148.   else
  149.     gui.box(scrx,scry,scrx+15,scry+15,0xFF000080)
  150.     local tile = "??"
  151.     if tiles[y] and tiles[y][x] then tile = tiles[y][x] end
  152.     gui.text(scrx+4,scry+4,string.format("%02x",tile))
  153.   end
  154. end
  155.  
  156.  
  157. local function renderField()
  158.  
  159.   for y=math.floor(camy/16),math.floor(camy/16)+14 do
  160.     for x=math.floor(camx/16),math.floor(camx/16)+16 do
  161.       renderTile(x,y)
  162.     end
  163.   end
  164.  
  165.   local relx = playerx -camx
  166.   local rely = playery -camy
  167.  
  168.   -- Draw player collision
  169.   gui.box(relx-8, rely-17,relx+7, rely-2,0x00FF0080)
  170.  
  171.   gui.line(relx-8, rely-9,relx+7, rely-9,0x0000FFFF)
  172.   gui.line(relx, rely-2,relx, rely-17,0x000000FFFF)
  173.  
  174. end
  175.  
  176. local prevX = 0
  177. local prevY = 0
  178.  
  179. local function getPositionData()
  180.  
  181.   prevX = memory.readword(0x7e1000)
  182.   prevY = memory.readword(0x7e1002)
  183.  
  184. end
  185.  
  186. local dX = 0
  187. local dY = 0
  188.  
  189. local function getDisplacementData()
  190.  
  191.   dX = prevX - memory.readword(0x7e1000)
  192.   dY = prevY - memory.readword(0x7e1002)
  193.  
  194. end
  195.  
  196. local function showDisplacement()
  197.  
  198.   if dX<0 then
  199.     gui.text(playerx-camx+11,playery-camy-12,-dX,0x00FF00FF)
  200.   elseif dX>0 then
  201.     gui.text(playerx-camx-14,playery-camy-12,dX,0x00FF00FF)
  202.   end
  203.  
  204.   if dY<0 then
  205.     gui.text(playerx-camx-1,playery-camy+1,-dY,0x00FF00FF)
  206.   elseif dY>0 then
  207.     gui.text(playerx-camx-1,playery-camy-26,dY,0x00FF00FF)
  208.   end
  209.  
  210.  
  211. end
  212.  
  213. -- Local display function
  214. local function my_display()
  215.   getDisplacementData()
  216.   getCollisionData()
  217.   if not hidden then
  218.     renderField()
  219.     showDisplacement()
  220.   end
  221.   getPositionData()
  222. end
  223.  
  224. -- Hook the display function to the GUI update
  225. local on_gui_update_old = gui.register()
  226. local function on_gui_update_new()
  227.   if on_gui_update_old then
  228.     on_gui_update_old()
  229.   end
  230.   my_display()
  231. end
  232. gui.register(on_gui_update_new)
  233.  
  234. -- Public functions
  235. collision_map = {}
  236.  
  237. function collision_map.show()
  238.   hidden = false
  239. end
  240.  
  241. function collision_map.hide()
  242.   hidden = true
  243. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement