Guest User

terranigma_collision_map_mod.lua

a guest
May 20th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.40 KB | None | 0 0
  1. -- Original Script by Gunty found on http://tasvideos.org/forum/viewtopic.php?t=3686&postdays=0&postorder=asc&start=20
  2. -- Modifications by @wzl_
  3.  
  4. -- These tile IDs encode open/cleared tiles where motion is possible
  5. local clear = {}
  6. --for i,v in pairs({0x00, 0x02, 0x04, 0x22, 0x2c, 0x3a}) do clear[v]=true  end
  7. for i,v in pairs({0x00}) do clear[v]=true  end
  8.  
  9. -- These tile IDs encode blocked tiles that cannot be entered
  10. local blocked = {}
  11. --for i,v in pairs({0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x28, 0x2a, 0x32}) do blocked[v]=true end
  12. for i,v in pairs({0x1c, 0x1e, 0x18, 0x32}) do blocked[v]=true end
  13.  
  14. -- 02, 04 Passable
  15. -- 08 ratree plant chests - some pickups > meilin scarf, gumin logs
  16. -- 0a pushable objects
  17.  
  18. -- 18 Blocked, can be pots and chests
  19. -- 1a Sylvain portraits, tower 3/4?
  20. -- 1c Blocked
  21. -- 1e Blocked
  22.  
  23. -- 26 water
  24. -- 28 pits
  25.  
  26. -- 24 rope
  27. -- 32 blocked tiles, usually by pits or water
  28. -- 3a staris no run?
  29. -- 3c diagonal stairs ?
  30. -- 80 npc, magirock?
  31.  
  32. -- a6 floating log in zue
  33. -- destructible rocks id?
  34.  
  35. local ColorBlocked = 0xFF000040
  36. local ColorBlockedOutline = 0xFF000080
  37. local ColorWater = 0x0030FF80
  38. local ColorOther = 0xFFFF0040
  39. local ColorOtherOutline = 0xFFFF0050
  40. local ColorPassable = 0x00FFFF20
  41. local ColorHighground = 0x00FF0020
  42.  
  43. local tiles = {0x1c}
  44. local camx = memory.readword(0x7e080e)
  45. local camy = memory.readword(0x7e0812)
  46. local playerx = memory.readword(0x7e1000)
  47. local playery = memory.readword(0x7e1002)
  48.  
  49. local function getCollisionData()
  50.   camx = memory.readword(0x7e080e)
  51.   camy = memory.readword(0x7e0812)
  52.   playerx = memory.readword(0x7e1000)
  53.   playery = memory.readword(0x7e1002)
  54.  
  55.   local width = memory.readbyte(0x7e0827)*16
  56.   tiles = {}
  57.  
  58.   for y=math.floor(camy/16),math.floor(camy/16)+14 do
  59.     tiles[y] = {}
  60.     for x=math.floor(camx/16),math.floor(camx/16)+16 do
  61.  
  62.       tiles[y][x] = AND(memory.readbyte(0x7ea001+2*(y*width+x)),0xfe)
  63.  
  64.     end
  65.   end
  66. end
  67.  
  68. local function tileExists(x,y)  return tiles[y] and tiles[y][x] end
  69.  
  70. local function tileCleared(x,y) return tileExists(x,y) and clear[tiles[y][x]]   end
  71. local function tileBlocked(x,y) return tileExists(x,y) and blocked[tiles[y][x]] end
  72.  
  73.  
  74. local function renderArrowBody(scrx,scry)
  75.   gui.box(scrx+7,scry+3,scrx+9,scry+12,0xFFFFFFFF,0x000000FF)
  76. end
  77.  
  78. local function renderArrowBodyHorizontal(scrx,scry)
  79.   gui.box(scrx+3,scry+7,scrx+12,scry+9,0xFFFFFFFF,0x000000FF)
  80. end
  81.  
  82. local function renderArrowHeadUp(scrx,scry)
  83.   scry = scry + 2
  84.   gui.line(scrx+5,scry+5,scrx+8,scry+2,0x000000FF)
  85.   gui.line(scrx+11,scry+5,scrx+8,scry+2,0x000000FF)
  86.  
  87.   gui.line(scrx+7,scry+6,scrx+5,scry+6,0x000000FF)
  88.   gui.line(scrx+9,scry+6,scrx+11,scry+6,0x000000FF)
  89.  
  90.   gui.pixel(scrx+8,scry+3,0xFFFFFFFF)
  91.   gui.line(scrx+7,scry+4,scrx+9,scry+4,0xFFFFFFFF)
  92.   gui.line(scrx+6,scry+5,scrx+10,scry+5,0xFFFFFFFF)
  93.  
  94. end
  95.  
  96. local function renderArrowHeadDown(scrx,scry)
  97. scry = scry - 2
  98.   gui.line(scrx+5,scry+10,scrx+8,scry+13,0x000000FF)
  99.   gui.line(scrx+11,scry+10,scrx+8,scry+13,0x000000FF)
  100.  
  101.   gui.line(scrx+7,scry+9,scrx+5,scry+9,0x000000FF)
  102.   gui.line(scrx+9,scry+9,scrx+11,scry+9,0x000000FF)
  103.  
  104.   gui.pixel(scrx+8,scry+12,0xFFFFFFFF)
  105.   gui.line(scrx+7,scry+11,scrx+9,scry+11,0xFFFFFFFF)
  106.   gui.line(scrx+6,scry+10,scrx+10,scry+10,0xFFFFFFFF)
  107. end
  108.  
  109. local function renderArrowHeadRight(scrx,scry)
  110.   scrx = scrx - 2
  111.   gui.line(scrx+10,scry+5,scrx+13,scry+8,0x000000FF)
  112.   gui.line(scrx+10,scry+11,scrx+13,scry+8,0x000000FF)
  113.  
  114.   gui.line(scrx+9,scry+7,scrx+9,scry+5,0x000000FF)
  115.   gui.line(scrx+9,scry+9,scrx+9,scry+11,0x000000FF)
  116.  
  117.   gui.pixel(scrx+12,scry+8,0xFFFFFFFF)
  118.   gui.line(scrx+11,scry+7,scrx+11,scry+9,0xFFFFFFFF)
  119.   gui.line(scrx+10,scry+6,scrx+10,scry+10,0xFFFFFFFF)
  120. end
  121.  
  122. local function renderArrowHeadLeft(scrx,scry)
  123.   scrx = scrx + 2
  124.   gui.line(scrx+5,scry+5,scrx+2,scry+8,0x000000FF)
  125.   gui.line(scrx+5,scry+11,scrx+2,scry+8,0x000000FF)
  126.  
  127.   gui.line(scrx+6,scry+7,scrx+6,scry+5,0x000000FF)
  128.   gui.line(scrx+6,scry+9,scrx+6,scry+11,0x000000FF)
  129.  
  130.   gui.pixel(scrx+3,scry+8,0xFFFFFFFF)
  131.   gui.line(scrx+4,scry+7,scrx+4,scry+9,0xFFFFFFFF)
  132.   gui.line(scrx+5,scry+6,scrx+5,scry+10,0xFFFFFFFF)
  133. end
  134.  
  135. local function renderTile(x,y)
  136.   if not tileExists(x,y) or tileCleared(x,y) then return end
  137.  
  138.   local tile = tiles[y][x]
  139.  
  140.   local scrx = 16*x-camx
  141.   local scry = 16*y-camy-1
  142.  
  143.   if tileBlocked(x,y) then -- blocked tiles
  144.  
  145.     gui.box(scrx-1,scry-1,scrx+16,scry+16,ColorBlocked,0)
  146.     if not tileBlocked(x,y-1) then gui.line(scrx,scry,scrx+15,scry,ColorBlockedOutline) end
  147.     if not tileBlocked(x,y+1) then gui.line(scrx,scry+15,scrx+15,scry+15,ColorBlockedOutline) end
  148.     if not tileBlocked(x-1,y) then gui.line(scrx,scry,scrx,scry+15,ColorBlockedOutline) end
  149.     if not tileBlocked(x+1,y) then gui.line(scrx+15,scry,scrx+15,scry+15,ColorBlockedOutline) end
  150.  
  151.     if not tileBlocked(x-1,y-1) then gui.pixel(scrx,scry,ColorBlockedOutline) end
  152.     if not tileBlocked(x+1,y+1) then gui.pixel(scrx+15,scry+15,ColorBlockedOutline) end
  153.     if not tileBlocked(x-1,y+1) then gui.pixel(scrx,scry+15,ColorBlockedOutline) end
  154.     if not tileBlocked(x+1,y-1) then gui.pixel(scrx+15,scry,ColorBlockedOutline) end
  155.  
  156.     if tile == 0x18 then gui.text(scrx+6,scry+4,"?") end -- 18 is uncertain, see legend up top
  157.  
  158.   elseif tile == 0x28 then
  159.  
  160.   gui.text(scrx+7,scry+4,"X")
  161.   renderArrowHeadDown(scrx, scry + 3)
  162.  
  163.   elseif tile == 0x04 or tile == 0x2c then -- passable terrain
  164.  
  165.   gui.box(scrx-1,scry-1,scrx+16,scry+16,ColorPassable,0)    
  166.  
  167.   elseif tile == 0x02 then -- highground
  168.  
  169.   gui.box(scrx-1,scry-1,scrx+16,scry+16,ColorHighground,0)        
  170.  
  171.   elseif tile == 0x3a then -- stairs
  172.  
  173.   gui.box(scrx-1,scry-1,scrx+16,scry+16,ColorPassable,0)
  174.   renderArrowHeadUp(scrx, scry - 2)
  175.   renderArrowHeadDown(scrx, scry + 2)
  176.  
  177.   elseif tile == 0x26 then -- Water
  178.  
  179.     gui.box(scrx-1,scry-1,scrx+16,scry+16,ColorWater,0)
  180.  
  181.   elseif tile==0x0c then -- leftdown/rightup
  182.    
  183.     gui.line(scrx+15,scry,scrx,scry+15,ColorBlockedOutline)
  184.     if tileCleared(x,y-1) and tileCleared(x-1,y) then
  185.       gui.line(scrx+15,scry,scrx,scry,ColorBlockedOutline)
  186.       gui.line(scrx,scry+1,scrx,scry+15,ColorBlockedOutline)
  187.     else
  188.       for i=14,0,-1 do gui.line(scrx,scry+i,scrx+i,scry,ColorBlocked) end
  189.     end
  190.  
  191.     if tileCleared(x,y+1) and tileCleared(x+1,y) then
  192.       gui.line(scrx+15,scry,scrx+15,scry+15,ColorBlockedOutline)
  193.       gui.line(scrx+14,scry+15,scrx,scry+15,ColorBlockedOutline)
  194.     else
  195.       for i=1,15 do gui.line(scrx+i,scry+15,scrx+15,scry+i,ColorBlocked) end
  196.     end
  197.  
  198.   elseif tile==0x0e then -- leftup/rightdown
  199.    
  200.     gui.line(scrx,scry,scrx+15,scry+15,ColorBlockedOutline)
  201.     if tileCleared(x,y-1) and tileCleared(x+1,y) then
  202.       gui.line(scrx,scry,scrx+15,scry,ColorBlockedOutline)
  203.       gui.line(scrx+15,scry+1,scrx+15,scry+15,ColorBlockedOutline)
  204.     else
  205.       for i=0,14 do gui.line(scrx+i,scry+i,scrx+15,scry+i,ColorBlocked) end
  206.     end
  207.  
  208.     if tileCleared(x,y+1) and tileCleared(x-1,y) then
  209.       gui.line(scrx,scry,scrx,scry+15,ColorBlockedOutline)
  210.       gui.line(scrx+1,scry+15,scrx+15,scry+15,ColorBlockedOutline)
  211.     else
  212.       for i=1,15 do gui.line(scrx+0,scry+i,scrx+i,scry+i,ColorBlocked) end
  213.     end
  214.  
  215.  
  216.   elseif tile==0x10 or tile==0x34 or tile==0x36  then  -- Fall/Vine/Crawl
  217.  
  218.     gui.box(scrx-1,scry-1,scrx+16,scry+16,0x00000080,0)
  219.  
  220.     if tileExists(x,y-1) and tiles[y-1][x]~=tile then gui.line(scrx,scry,scrx+15,scry,0x000000FF) end
  221.     if tileExists(x,y+1) and tiles[y+1][x]~=tile then gui.line(scrx,scry+15,scrx+15,scry+15,0x000000FF) end
  222.     if tileExists(x-1,y) and tiles[y][x-1]~=tile then gui.line(scrx,scry,scrx,scry+15,0x000000FF) end
  223.     if tileExists(x+1,y) and tiles[y][x+1]~=tile then gui.line(scrx+15,scry,scrx+15,scry+15,0x000000FF) end
  224.  
  225.     renderArrowBody(scrx,scry)
  226.     if tile~=0x10 then renderArrowHeadUp(scrx,scry - 2) end
  227.     renderArrowHeadDown(scrx,scry + 2)
  228.  
  229.   elseif tile ==0x3e then -- claw climb
  230.  
  231.   gui.box(scrx-1,scry-1,scrx+16,scry+16,0x00000080,0)
  232.   renderArrowHeadUp(scrx -4,scry- 3)
  233.   renderArrowHeadUp(scrx +3,scry+ 4)
  234.  
  235.  
  236.   elseif tile == 0x0a then -- push object
  237.  
  238.   gui.box(scrx-1,scry-1,scrx+16,scry+16,0x00000080,0)
  239.   renderArrowBodyHorizontal(scrx,scry)
  240.   renderArrowBody(scrx,scry)
  241.   renderArrowHeadLeft(scrx- 3,scry)
  242.   renderArrowHeadRight(scrx + 4,scry)
  243.   renderArrowHeadUp(scrx ,scry- 3)
  244.   renderArrowHeadDown(scrx ,scry+ 4)
  245.   elseif AND(tiles[y][x],0x80)~=0 then
  246.  
  247.     gui.box(scrx,scry,scrx+15,scry+15,0xFF800080)
  248.      gui.text(scrx+2,scry+4,string.format("%02x",tile))
  249.  
  250.  
  251.  
  252.   else
  253.     gui.box(scrx,scry,scrx+15,scry+15,0xFF000080)
  254.     local tile = "??"
  255.     if tiles[y] and tiles[y][x] then tile = tiles[y][x] end
  256.     gui.text(scrx+4,scry+4,string.format("%02x",tile))
  257.   end
  258. end
  259.  
  260.  
  261. local function renderField()
  262.  
  263.   for y=math.floor(camy/16),math.floor(camy/16)+14 do
  264.     for x=math.floor(camx/16),math.floor(camx/16)+16 do
  265.       renderTile(x,y)
  266.     end
  267.   end
  268.  
  269.   local relx = playerx -camx
  270.   local rely = playery -camy
  271.  
  272.   -- Draw player collision
  273.   gui.box(relx-8, rely-17,relx+7, rely-2,0x00FF0080)
  274.  
  275.   gui.line(relx-8, rely-9,relx+7, rely-9,0x0000FFFF)
  276.   gui.line(relx, rely-2,relx, rely-17,0x000000FFFF)
  277.  
  278. end
  279.  
  280. local prevX = 0
  281. local prevY = 0
  282.  
  283. local function getPositionData()
  284.  
  285.   prevX = memory.readword(0x7e1000)
  286.   prevY = memory.readword(0x7e1002)
  287.  
  288. end
  289.  
  290. local dX = 0
  291. local dY = 0
  292.  
  293. local function getDisplacementData()
  294.  
  295.   dX = prevX - memory.readword(0x7e1000)
  296.   dY = prevY - memory.readword(0x7e1002)
  297.  
  298. end
  299.  
  300. local function showDisplacement()
  301.  
  302.   if dX<0 then
  303.     gui.text(playerx-camx+11,playery-camy-12,-dX,0x00FF00FF)
  304.   elseif dX>0 then
  305.     gui.text(playerx-camx-14,playery-camy-12,dX,0x00FF00FF)
  306.   end
  307.  
  308.   if dY<0 then
  309.     gui.text(playerx-camx-1,playery-camy+1,-dY,0x00FF00FF)
  310.   elseif dY>0 then
  311.     gui.text(playerx-camx-1,playery-camy-26,dY,0x00FF00FF)
  312.   end
  313.  
  314.  
  315. end
  316.  
  317. -- Local display function
  318. local function my_display()
  319.   getDisplacementData()
  320.   getCollisionData()
  321.   if not hidden then
  322.     renderField()
  323.     showDisplacement()
  324.   end
  325.   getPositionData()
  326. end
  327.  
  328. -- Hook the display function to the GUI update
  329. local on_gui_update_old = gui.register()
  330. local function on_gui_update_new()
  331.   if on_gui_update_old then
  332.     on_gui_update_old()
  333.   end
  334.   my_display()
  335. end
  336. gui.register(on_gui_update_new)
  337.  
  338. -- Public functions
  339. collision_map = {}
  340.  
  341. function collision_map.show()
  342.   hidden = false
  343. end
  344.  
  345. function collision_map.hide()
  346.   hidden = true
  347. end
Advertisement
Add Comment
Please, Sign In to add comment