Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 35.10 KB | None | 0 0
  1. local TILE_ADDR = 0x00A0
  2.  
  3. -- drawing
  4. local CANVAS_WIDTH = 800
  5. local CANVAS_HEIGHT = 450
  6.  
  7. local ROOM_SIZE = 12
  8. local ROOM_GAP = 0
  9. local TILE_SIZE = ROOM_SIZE * 2 + ROOM_GAP * 1
  10.  
  11. local X_OFFSET = 6
  12. local Y_OFFSET = 50
  13.  
  14. local draw_POS = {
  15.     [1] = { x = 0, y = 0 },
  16.     [2] = { x = 1, y = 0 },
  17.     [3] = { x = 0, y = 1 },
  18.     [4] = { x = 1, y = 1 }
  19. }
  20.  
  21. -- colors
  22.  
  23. local HM = 0xC0B8A8 -- houses
  24. local HC = 0xA89868 -- hyrule castle
  25. local AT = HC -- aga's tower
  26. local EX = 0x706890 -- escape
  27. local SY = 0x204020 -- sanctuary
  28. local CV = 0x684830 -- caves
  29.  
  30. local EP = 0x80A870 -- eastern palace
  31. local DP = 0x807838 -- desert palace
  32. local TH = 0x987870 -- tower of hera
  33.  
  34. local PD = 0x909090 -- palace of darkness
  35. local SP = 0x7890F8 -- swamp palace
  36. local SW = 0x605030 -- skull woods
  37. local TT = 0x787888 -- thieves town
  38. local IP = 0xA8E0E8 -- ice palace
  39. local MM = 0x408068 -- misery mire
  40. local TR = 0x583028 -- turtle rock
  41. local GT = 0xA09058 -- ganon's tower
  42.  
  43. local GE = 0x984848 -- empty gt TILEs
  44. local EM = 0x606060 -- empty TILEs
  45.  
  46. local COMPLETE = 0xFFFF0000
  47. local VISITED = 0xFFFFFF00
  48. local UNVISITED = 0xFFAAAAAA
  49. local CURRENT = 0xFF00FFFF
  50.  
  51. local BLACK = 0xFF000000
  52. local WHITE = 0xFFFFFFFF
  53.  
  54. local OPAQUE = 0xFF000000
  55. local TRANS = 0x99000000
  56.  
  57. local COLL = 0xFF505050
  58. local SEMI = 0xF0909090
  59. local CHEST = 0xFFDEB429
  60.  
  61. local MINIMAP_X = 266
  62. local MINIMAP_Y = 5
  63. local MINIMAP_SCALE = 3
  64.  
  65. local function get_color(color, opacity)
  66.     return bit.bor(color, opacity)
  67. end -- end get_color
  68.  
  69. -- room number from quadrant variables
  70. local R1, R2, R3, R4 = 1, 2, 3, 4
  71.  
  72. local TILES_TO_VALS = {
  73.     ['RX'] = {
  74.         R1 = { A9 = 0x00, AA = 0x00 },
  75.         R2 = { A9 = 0x01, AA = 0x00 },
  76.         R3 = { A9 = 0x00, AA = 0x02 },
  77.         R4 = { A9 = 0x01, AA = 0x02 }
  78.     },
  79.  
  80.     ['A9AA'] = {
  81.             [0x00] = {
  82.                 [0x00] = R1,
  83.                 [0x02] = R3
  84.             },
  85.             [0x01] = {
  86.                 [0x00] = R2,
  87.                 [0x02] = R4
  88.         }
  89.     }
  90. }
  91.  
  92. local function stop_running()
  93.     running = false
  94. end
  95.  
  96. local function end_thing()
  97.     client.SetGameExtraPadding(0, 0, 0, 0)
  98.     stop_running()
  99. end
  100.  
  101. local function initialize()
  102.     running = true
  103.  
  104.     memory.usememorydomain("WRAM")
  105.     client.SetGameExtraPadding(0, 0, 230, 0)
  106.     the_canvas = gui
  107.  
  108.     event.onexit(end_thing)
  109. end
  110.  
  111. --[==[
  112. Format:
  113.     [0x##] = {        -- room id
  114.         c = XX        -- color
  115.         p = AABA        -- room pattern
  116.         r = { p }      -- pattern as table
  117.         v = 0100        -- rooms reachable without glitches
  118.         x = { v }      -- reachability as table; no dupes
  119.     }
  120. ]==]
  121.  
  122. local TILE_DATA = {
  123.     [0x00] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  124.     [0x01] = { c = HC, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  125.     [0x02] = { c = SY, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  126.     [0x03] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  127.     [0x04] = { c = TR, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  128.     [0x05] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  129.     [0x06] = { c = SP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  130.     [0x07] = { c = TH, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  131.     [0x08] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  132.     [0x09] = { c = PD, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  133.     [0x0A] = { c = PD, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  134.     [0x0B] = { c = PD, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  135.     [0x0C] = { c = GT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  136.     [0x0D] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  137.     [0x0E] = { c = IP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  138.     [0x0F] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  139.  
  140.     [0x10] = { c = GT, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  141.     [0x11] = { c = EX, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  142.     [0x12] = { c = SY, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  143.     [0x13] = { c = TR, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  144.     [0x14] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  145.     [0x15] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  146.     [0x16] = { c = SP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  147.     [0x17] = { c = TH, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  148.     [0x18] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  149.     [0x19] = { c = PD, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  150.     [0x1A] = { c = PD, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  151.     [0x1B] = { c = PD, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  152.     [0x1C] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  153.     [0x1D] = { c = GT, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  154.     [0x1E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  155.     [0x1F] = { c = IP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  156.  
  157.     [0x20] = { c = AT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  158.     [0x21] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  159.     [0x22] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  160.     [0x23] = { c = TR, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  161.     [0x24] = { c = TR, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  162.     [0x25] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  163.     [0x26] = { c = SP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  164.     [0x27] = { c = TH, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  165.     [0x28] = { c = SP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  166.     [0x29] = { c = SW, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  167.     [0x2A] = { c = PD, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  168.     [0x2B] = { c = PD, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  169.     [0x2C] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  170.     [0x2D] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  171.     [0x2E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  172.     [0x2F] = { c = CV, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  173.  
  174.     [0x30] = { c = AT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  175.     [0x31] = { c = TH, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  176.     [0x32] = { c = EX, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  177.     [0x33] = { c = DP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  178.     [0x34] = { c = SP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  179.     [0x35] = { c = SP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  180.     [0x36] = { c = SP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  181.     [0x37] = { c = SP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  182.     [0x38] = { c = SP, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  183.     [0x39] = { c = SW, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  184.     [0x3A] = { c = PD, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  185.     [0x3B] = { c = PD, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  186.     [0x3C] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  187.     [0x3D] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  188.     [0x3E] = { c = IP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  189.     [0x3F] = { c = IP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  190.  
  191.     [0x40] = { c = AT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  192.     [0x41] = { c = HC, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  193.     [0x42] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  194.     [0x43] = { c = DP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  195.     [0x44] = { c = TT, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  196.     [0x45] = { c = TT, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  197.     [0x46] = { c = SP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  198.     [0x47] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  199.     [0x48] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  200.     [0x49] = { c = SW, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  201.     [0x4A] = { c = PD, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  202.     [0x4B] = { c = PD, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  203.     [0x4C] = { c = GT, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  204.     [0x4D] = { c = GT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  205.     [0x4E] = { c = IP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  206.     [0x4F] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  207.  
  208.     [0x50] = { c = HC, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  209.     [0x51] = { c = HC, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  210.     [0x52] = { c = HC, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  211.     [0x53] = { c = DP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  212.     [0x54] = { c = SP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  213.     [0x55] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  214.     [0x56] = { c = SW, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  215.     [0x57] = { c = SW, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  216.     [0x58] = { c = SW, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  217.     [0x59] = { c = SW, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  218.     [0x5A] = { c = PD, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  219.     [0x5B] = { c = GT, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  220.     [0x5C] = { c = GT, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  221.     [0x5D] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  222.     [0x5E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  223.     [0x5F] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  224.  
  225.     [0x60] = { c = HC, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  226.     [0x61] = { c = HC, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  227.     [0x62] = { c = HC, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  228.     [0x63] = { c = DP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  229.     [0x64] = { c = TT, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  230.     [0x65] = { c = TT, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  231.     [0x66] = { c = SP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  232.     [0x67] = { c = SW, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  233.     [0x68] = { c = SW, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  234.     [0x69] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  235.     [0x6A] = { c = PD, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  236.     [0x6B] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  237.     [0x6C] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  238.     [0x6D] = { c = GT, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  239.     [0x6E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  240.     [0x6F] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  241.  
  242.     [0x70] = { c = EX, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  243.     [0x71] = { c = EX, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  244.     [0x72] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  245.     [0x73] = { c = DP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  246.     [0x74] = { c = DP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  247.     [0x75] = { c = DP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  248.     [0x76] = { c = SP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  249.     [0x77] = { c = TH, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  250.     [0x78] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  251.     [0x79] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  252.     [0x7A] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  253.     [0x7B] = { c = GT, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  254.     [0x7C] = { c = GT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  255.     [0x7D] = { c = GT, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  256.     [0x7E] = { c = IP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  257.     [0x7F] = { c = IP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  258.  
  259.     [0x80] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  260.     [0x81] = { c = EX, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  261.     [0x82] = { c = EX, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  262.     [0x83] = { c = DP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  263.     [0x84] = { c = DP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  264.     [0x85] = { c = DP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  265.     [0x86] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  266.     [0x87] = { c = TH, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  267.     [0x88] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  268.     [0x89] = { c = EP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  269.     [0x8A] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  270.     [0x8B] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  271.     [0x8C] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  272.     [0x8D] = { c = GT, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  273.     [0x8E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  274.     [0x8F] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  275.  
  276.     [0x90] = { c = MM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  277.     [0x91] = { c = MM, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  278.     [0x92] = { c = MM, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  279.     [0x93] = { c = MM, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  280.     [0x94] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  281.     [0x95] = { c = GT, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  282.     [0x96] = { c = GT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  283.     [0x97] = { c = MM, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  284.     [0x98] = { c = MM, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  285.     [0x99] = { c = EP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  286.     [0x9A] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  287.     [0x9B] = { c = GT, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  288.     [0x9C] = { c = GT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  289.     [0x9D] = { c = GT, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  290.     [0x9E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  291.     [0x9F] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  292.  
  293.     [0xA0] = { c = MM, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  294.     [0xA1] = { c = MM, p = 'AABA', r = { 'A', 'A', 'B', 'A' } },
  295.     [0xA2] = { c = MM, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  296.     [0xA3] = { c = MM, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  297.     [0xA4] = { c = TR, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  298.     [0xA5] = { c = GT, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  299.     [0xA6] = { c = GT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  300.     [0xA7] = { c = TH, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  301.     [0xA8] = { c = EP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  302.     [0xA9] = { c = EP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  303.     [0xAA] = { c = EP, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  304.     [0xAB] = { c = TT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  305.     [0xAC] = { c = TT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  306.     [0xAD] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  307.     [0xAE] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  308.     [0xAF] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  309.  
  310.     [0xB0] = { c = AT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  311.     [0xB1] = { c = MM, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  312.     [0xB2] = { c = MM, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  313.     [0xB3] = { c = MM, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  314.     [0xB4] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  315.     [0xB5] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  316.     [0xB6] = { c = TR, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  317.     [0xB7] = { c = TR, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  318.     [0xB8] = { c = EP, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  319.     [0xB9] = { c = EP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  320.     [0xBA] = { c = EP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  321.     [0xBB] = { c = TT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  322.     [0xBC] = { c = TT, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  323.     [0xBD] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  324.     [0xBE] = { c = IP, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  325.     [0xBF] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  326.  
  327.     [0xC0] = { c = AT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  328.     [0xC1] = { c = MM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  329.     [0xC2] = { c = MM, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  330.     [0xC3] = { c = MM, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  331.     [0xC4] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  332.     [0xC5] = { c = TR, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  333.     [0xC6] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  334.     [0xC7] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  335.     [0xC8] = { c = EP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  336.     [0xC9] = { c = EP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  337.     [0xCA] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  338.     [0xCB] = { c = TT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  339.     [0xCC] = { c = TT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  340.     [0xCD] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  341.     [0xCE] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  342.     [0xCF] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  343.  
  344.     [0xD0] = { c = AT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  345.     [0xD1] = { c = MM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  346.     [0xD2] = { c = MM, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  347.     [0xD3] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  348.     [0xD4] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  349.     [0xD5] = { c = TR, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  350.     [0xD6] = { c = TR, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  351.     [0xD7] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  352.     [0xD8] = { c = EP, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  353.     [0xD9] = { c = EP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  354.     [0xDA] = { c = EP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  355.     [0xDB] = { c = TT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  356.     [0xDC] = { c = TT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  357.     [0xDD] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  358.     [0xDE] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  359.     [0xDF] = { c = CV, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  360.  
  361.     [0xE0] = { c = AT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  362.     [0xE1] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  363.     [0xE2] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  364.     [0xE3] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  365.     [0xE4] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  366.     [0xE5] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  367.     [0xE6] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  368.     [0xE7] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  369.     [0xE8] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  370.     [0xE9] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  371.     [0xEA] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  372.     [0xEB] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  373.     [0xEC] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  374.     [0xED] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  375.     [0xEE] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  376.     [0xEF] = { c = CV, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  377.  
  378.     [0xF0] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  379.     [0xF1] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  380.     [0xF2] = { c = HM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  381.     [0xF3] = { c = HM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  382.     [0xF4] = { c = HM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  383.     [0xF5] = { c = HM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  384.     [0xF6] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  385.     [0xF7] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  386.     [0xF8] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  387.     [0xF9] = { c = CV, p = 'ABBB', r = { 'A', 'B', 'B', 'B' } },
  388.     [0xFA] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  389.     [0xFB] = { c = CV, p = 'ABBB', r = { 'A', 'B', 'B', 'B' } },
  390.     [0xFC] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  391.     [0xFD] = { c = CV, p = 'ABAA', r = { 'A', 'B', 'A', 'A' } },
  392.     [0xFE] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  393.     [0xFF] = { c = CV, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
  394.  
  395.     -- underworld 2
  396.     [0x0100] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  397.     [0x0101] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  398.     [0x0102] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  399.     [0x0103] = { c = CV, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  400.     [0x0104] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  401.     [0x0105] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  402.     [0x0106] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  403.     [0x0107] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  404.     [0x0108] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  405.     [0x0109] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  406.     [0x010A] = { c = CV, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  407.     [0x010B] = { c = CV, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  408.     [0x010C] = { c = CV, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
  409.     [0x010D] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  410.     [0x010E] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  411.     [0x010F] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  412.  
  413.     [0x0110] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  414.     [0x0111] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  415.     [0x0112] = { c = CV, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  416.     [0x0113] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
  417.     [0x0114] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  418.     [0x0115] = { c = CV, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  419.     [0x0116] = { c = CV, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
  420.     [0x0117] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
  421.     [0x0118] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  422.     [0x0119] = { c = CV, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  423.     [0x011A] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  424.     [0x011B] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  425.     [0x011C] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  426.     [0x011D] = { c = CV, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
  427.     [0x011E] = { c = CV, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
  428.     [0x011F] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  429.  
  430.     [0x0120] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  431.     [0x0121] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  432.     [0x0122] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  433.     [0x0123] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  434.     [0x0124] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  435.     [0x0125] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  436.     [0x0126] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  437.     [0x0127] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
  438. }
  439.  
  440. -- room tracking
  441. local prev = nil
  442. local prev_quad = nil
  443. local check_map = false
  444.  
  445. -- drawing
  446. local function draw_tile(tile, quad_id)
  447.     if tile == nil then return end
  448.  
  449.     local x = (bit.band(tile, 0x0F) * TILE_SIZE + 1) + X_OFFSET
  450.     local y = ((bit.band(tile, 0xF0) / 16) * TILE_SIZE + 1) + Y_OFFSET
  451.  
  452.     the_canvas.drawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK, BLACK) -- clear only this section
  453.  
  454.     local d = TILE_DATA[tile]
  455.     local r = d.r
  456.  
  457.     local room_completion_colors = { 0, 0, 0, 0 }
  458.  
  459.     for i=1, 4 do
  460.         room_completion_colors[i] =
  461.             quad_id == TILE_DATA[tile].r[i] and CURRENT
  462.                 or UNVISITED and OPAQUE or TRANS
  463.         the_canvas.drawRectangle(
  464.                 x + (ROOM_SIZE) * draw_POS[i].x,
  465.                 y + (ROOM_SIZE) * draw_POS[i].y,
  466.                 ROOM_SIZE,
  467.                 ROOM_SIZE,
  468.                 nil,
  469.                 room_completion_colors[i]
  470.             )
  471.         if TILE_DATA[tile].x[r[i]] == 0 then
  472.             --[[the_canvas.drawText(
  473.                 x + (ROOM_SIZE) * draw_POS[i].x + 1,
  474.                 y + (ROOM_SIZE) * draw_POS[i].y - 1,
  475.                 "X",
  476.                 0x55FFFFFF,
  477.                 0
  478.             )]]
  479.         end
  480.     end
  481.  
  482.     local C0
  483.  
  484.     if quad_id then
  485.         C0 = CURRENT
  486.     else
  487.         C0 = UNVISITED
  488.     end
  489.  
  490.     -- vertical separators
  491.     local C1 = r[1] == r[2] and room_completion_colors[1] or C0
  492.     local C2 = r[3] == r[4] and room_completion_colors[3] or C0
  493.  
  494.     the_canvas.drawLine(x + ROOM_SIZE + ROOM_GAP, y + ROOM_GAP,
  495.                             x + ROOM_SIZE + ROOM_GAP, y + ROOM_SIZE,
  496.                             C1)
  497.  
  498.     the_canvas.drawLine(x + ROOM_GAP + ROOM_SIZE, y + ROOM_GAP + ROOM_GAP + ROOM_SIZE,
  499.                             x + ROOM_GAP + ROOM_SIZE, y + ROOM_SIZE + ROOM_SIZE,
  500.                             C2)
  501.  
  502.     -- horizontal separators
  503.     local C3 = r[1] == r[3] and room_completion_colors[1] or C0
  504.     local C4 = r[2] == r[4] and room_completion_colors[2] or C0
  505.  
  506.     the_canvas.drawLine(x + ROOM_GAP + ROOM_GAP, y + ROOM_GAP + ROOM_SIZE,
  507.                             x + ROOM_SIZE, y + ROOM_GAP + ROOM_SIZE,
  508.                             C3)
  509.  
  510.     the_canvas.drawLine(x + ROOM_GAP + ROOM_GAP + ROOM_SIZE, y + ROOM_GAP + ROOM_SIZE,
  511.                             x + ROOM_SIZE + ROOM_SIZE, y + ROOM_GAP + ROOM_SIZE,
  512.                             C4)
  513.  
  514.     local C5 = d.p == 'AAAA' and C1 or C0
  515.  
  516.     the_canvas.drawPixel(x + ROOM_GAP + ROOM_SIZE, y + ROOM_GAP + ROOM_SIZE, BLACK)
  517.     the_canvas.drawPixel(x + ROOM_GAP + ROOM_SIZE, y + ROOM_GAP + ROOM_SIZE, C5)
  518.  
  519.     the_canvas.drawRectangle(x, y, TILE_SIZE, TILE_SIZE, C0, nil)
  520. end -- end draw_tile
  521.  
  522. local function draw_all()
  523.     the_canvas.Clear(BLACK)
  524.     for i=0, 0x0127 do
  525.         draw_tile(i)
  526.     end
  527.  
  528.     --the_canvas.refresh()
  529. end -- end draw_all
  530.  
  531. local NONE = nil
  532.  
  533. local NOTHING = 0xFF777777
  534. local TURF = 0xFF4A9C4A
  535. local COLL = 0xFF282828
  536. local SEMI = 0xFF303030
  537. local CHEST = 0xFFDEB429
  538. local RAIL = 0xFF707070
  539. local SPIKE = 0xFFE0E0E0
  540. local POT = 0xFF7B4A4A
  541. local BUSH = 0xFF317331
  542. local WATER = 0xFF3962AC
  543. local SHALLOW_WATER = 0xFF5A83C5
  544. local THICK_GRASS = 0xFF115511
  545. local SPIKE_FLOOR = 0xFFA4A4A4
  546. local PIT = 0xFF550000
  547. local CRACKED = 0xFF330000
  548. local AUTO_STAIRS = 0xFF6A4A31
  549. local MANUAL_STAIRS = 0xFF8A6A51
  550. local ROOM_STAIRS = 0xFF5A737B
  551. local PEGS_1 = 0xFF414183
  552. local PEGS_2 = 0xFF6A6AC5
  553. local LIGHT_ROCK = 0xFF7BBD94
  554. local DARK_ROCK = 0xFF528373
  555. local RUPEES = 0xFF626283
  556. local SOM_LINE = 0xFFEEEEEE
  557. local SOM_LINE_Q = 0xFF7B4A4A
  558. local TORCH = 0xFFA4A4A4
  559. local CONVEYOR = 0xFF627362
  560. local SIGN = 0xFF8B7341
  561. local STAR = 0xFFAC7B52
  562. local KEY_DOOR = 0xFFAC9C6A
  563.  
  564. local collision_map = {
  565.     [0x00] = NOTHING,
  566.     [0x01] = COLL,
  567.     [0x02] = COLL,
  568.     [0x03] = COLL,
  569.     [0x04] = COLL,
  570.     [0x05] = nil,
  571.     [0x06] = nil,
  572.     [0x07] = nil,
  573.     [0x08] = WATER,
  574.     [0x09] = SHALLOW_WATER,
  575.     [0x0A] = nil,
  576.     [0x0B] = COLL,
  577.     [0x0C] = nil,
  578.     [0x0D] = SPIKE_FLOOR,
  579.     [0x0E] = SEMI,
  580.     [0x0F] = SEMI,
  581.     [0x10] = SEMI,
  582.     [0x11] = SEMI,
  583.     [0x12] = SEMI,
  584.     [0x13] = SEMI,
  585.     [0x14] = SEMI,
  586.     [0x15] = SEMI,
  587.     [0x16] = SEMI,
  588.     [0x17] = SEMI,
  589.     [0x18] = SEMI,
  590.     [0x19] = SEMI,
  591.     [0x1A] = SEMI,
  592.     [0x1B] = SEMI,
  593.     [0x1C] = AUTO_STAIRS, -- stairs?
  594.     [0x1D] = AUTO_STAIRS,
  595.     [0x1E] = AUTO_STAIRS,
  596.     [0x1F] = AUTO_STAIRS,
  597.     [0x20] = PIT,
  598.     [0x21] = nil,
  599.     [0x22] = MANUAL_STAIRS,
  600.     [0x23] = nil,
  601.     [0x24] = nil,
  602.     [0x25] = nil,
  603.     [0x26] = COLL,
  604.     [0x27] = COLL,
  605.     [0x28] = SEMI,
  606.     [0x29] = SEMI,
  607.     [0x2A] = SEMI,
  608.     [0x2B] = SEMI,
  609.     [0x2C] = SEMI,
  610.     [0x2D] = SEMI,
  611.     [0x2E] = SEMI,
  612.     [0x2F] = SEMI,
  613.     [0x30] = ROOM_STAIRS,
  614.     [0x31] = ROOM_STAIRS,
  615.     [0x32] = ROOM_STAIRS,
  616.     [0x33] = ROOM_STAIRS,
  617.     [0x34] = ROOM_STAIRS,
  618.     [0x35] = ROOM_STAIRS,
  619.     [0x36] = ROOM_STAIRS,
  620.     [0x37] = ROOM_STAIRS,
  621.     [0x38] = ROOM_STAIRS,
  622.     [0x39] = ROOM_STAIRS,
  623.     [0x3A] = STAR,
  624.     [0x3B] = STAR,
  625.     [0x3C] = nil,
  626.     [0x3D] = AUTO_STAIRS,
  627.     [0x3E] = AUTO_STAIRS,
  628.     [0x3F] = AUTO_STAIRS,
  629.     [0x40] = THICK_GRASS,
  630.     [0x41] = nil,
  631.     [0x42] = COLL,
  632.     [0x43] = COLL,
  633.     [0x44] = SPIKE,
  634.     [0x45] = nil,
  635.     [0x46] = COLL,
  636.     [0x47] = nil,
  637.     [0x48] = TURF,
  638.     [0x49] = nil,
  639.     [0x4A] = nil,
  640.     [0x4B] = nil,
  641.     [0x4C] = nil,
  642.     [0x4D] = nil,
  643.     [0x4E] = COLL,
  644.     [0x4F] = COLL,
  645.     [0x50] = BUSH,
  646.     [0x51] = BUSH,
  647.     [0x52] = LIGHT_ROCK,
  648.     [0x53] = DARK_ROCK,
  649.     [0x54] = SIGN,
  650.     [0x55] = LIGHT_ROCK,
  651.     [0x56] = DARK_ROCK,
  652.     [0x57] = COLL,
  653.     [0x58] = CHEST,
  654.     [0x59] = CHEST,
  655.     [0x5A] = CHEST,
  656.     [0x5B] = CHEST,
  657.     [0x5C] = CHEST,
  658.     [0x5D] = CHEST,
  659.     [0x5E] = ROOM_STAIRS,
  660.     [0x5F] = nil,
  661.     [0x60] = RUPEES,
  662.     [0x61] = nil,
  663.     [0x62] = CRACKED,
  664.     [0x63] = CHEST,
  665.     [0x64] = nil,
  666.     [0x65] = nil,
  667.     [0x66] = PEGS_1,
  668.     [0x67] = PEGS_2,
  669.     [0x68] = CONVEYOR,
  670.     [0x69] = CONVEYOR,
  671.     [0x6A] = CONVEYOR,
  672.     [0x6B] = CONVEYOR,
  673.     [0x6C] = COLL,
  674.     [0x6D] = COLL,
  675.     [0x6E] = COLL,
  676.     [0x6F] = COLL,
  677.     [0x70] = POT,
  678.     [0x71] = POT,
  679.     [0x72] = POT,
  680.     [0x73] = POT,
  681.     [0x74] = POT,
  682.     [0x75] = POT,
  683.     [0x76] = POT,
  684.     [0x77] = POT,
  685.     [0x78] = POT,
  686.     [0x79] = POT,
  687.     [0x7A] = POT,
  688.     [0x7B] = POT,
  689.     [0x7C] = POT,
  690.     [0x7D] = POT,
  691.     [0x7E] = POT,
  692.     [0x7F] = POT,
  693.     [0x80] = ROOM_STAIRS,
  694.     [0x81] = ROOM_STAIRS,
  695.     [0x82] = ROOM_STAIRS,
  696.     [0x83] = ROOM_STAIRS,
  697.     [0x84] = ROOM_STAIRS,
  698.     [0x85] = ROOM_STAIRS,
  699.     [0x86] = ROOM_STAIRS,
  700.     [0x87] = ROOM_STAIRS,
  701.     [0x88] = ROOM_STAIRS,
  702.     [0x89] = ROOM_STAIRS,
  703.     [0x8A] = ROOM_STAIRS,
  704.     [0x8B] = ROOM_STAIRS,
  705.     [0x8C] = ROOM_STAIRS,
  706.     [0x8D] = ROOM_STAIRS,
  707.     [0x8E] = ROOM_STAIRS,
  708.     [0x8F] = ROOM_STAIRS,
  709.     [0x90] = nil,
  710.     [0x91] = nil,
  711.     [0x92] = nil,
  712.     [0x93] = nil,
  713.     [0x94] = nil,
  714.     [0x95] = nil,
  715.     [0x96] = nil,
  716.     [0x97] = nil,
  717.     [0x98] = nil,
  718.     [0x99] = nil,
  719.     [0x9A] = nil,
  720.     [0x9B] = nil,
  721.     [0x9C] = nil,
  722.     [0x9D] = nil,
  723.     [0x9E] = nil,
  724.     [0x9F] = nil,
  725.     [0xA0] = nil,
  726.     [0xA1] = nil,
  727.     [0xA2] = nil,
  728.     [0xA3] = nil,
  729.     [0xA4] = nil,
  730.     [0xA5] = nil,
  731.     [0xA6] = nil,
  732.     [0xA7] = nil,
  733.     [0xA8] = nil,
  734.     [0xA9] = nil,
  735.     [0xAA] = nil,
  736.     [0xAB] = nil,
  737.     [0xAC] = nil,
  738.     [0xAD] = nil,
  739.     [0xAE] = nil,
  740.     [0xAF] = nil,
  741.     [0xB0] = SOM_LINE,
  742.     [0xB1] = SOM_LINE,
  743.     [0xB2] = SOM_LINE,
  744.     [0xB3] = SOM_LINE,
  745.     [0xB4] = SOM_LINE,
  746.     [0xB5] = SOM_LINE,
  747.     [0xB6] = SOM_LINE_Q,
  748.     [0xB7] = nil,
  749.     [0xB8] = nil,
  750.     [0xB9] = nil,
  751.     [0xBA] = nil,
  752.     [0xBB] = nil,
  753.     [0xBC] = nil,
  754.     [0xBD] = SOM_LINE,
  755.     [0xBE] = AUTO_STAIRS,
  756.     [0xBF] = nil,
  757.     [0xC0] = TORCH,
  758.     [0xC1] = TORCH,
  759.     [0xC2] = TORCH,
  760.     [0xC3] = TORCH,
  761.     [0xC4] = TORCH,
  762.     [0xC5] = TORCH,
  763.     [0xC6] = TORCH,
  764.     [0xC7] = TORCH,
  765.     [0xC8] = TORCH,
  766.     [0xC9] = TORCH,
  767.     [0xCA] = TORCH,
  768.     [0xCB] = TORCH,
  769.     [0xCC] = TORCH,
  770.     [0xCD] = TORCH,
  771.     [0xCE] = TORCH,
  772.     [0xCF] = TORCH,
  773.     [0xD0] = nil,
  774.     [0xD1] = nil,
  775.     [0xD2] = nil,
  776.     [0xD3] = nil,
  777.     [0xD4] = nil,
  778.     [0xD5] = nil,
  779.     [0xD6] = nil,
  780.     [0xD7] = nil,
  781.     [0xD8] = nil,
  782.     [0xD9] = nil,
  783.     [0xDA] = nil,
  784.     [0xDB] = nil,
  785.     [0xDC] = nil,
  786.     [0xDD] = nil,
  787.     [0xDE] = nil,
  788.     [0xDF] = nil,
  789.     [0xE0] = nil,
  790.     [0xE1] = nil,
  791.     [0xE2] = nil,
  792.     [0xE3] = nil,
  793.     [0xE4] = nil,
  794.     [0xE5] = nil,
  795.     [0xE6] = nil,
  796.     [0xE7] = nil,
  797.     [0xE8] = AUTO_STAIRS,
  798.     [0xE9] = nil,
  799.     [0xEA] = nil,
  800.     [0xEB] = nil,
  801.     [0xEC] = nil,
  802.     [0xED] = nil,
  803.     [0xEE] = nil,
  804.     [0xEF] = nil,
  805.     [0xF0] = KEY_DOOR,
  806.     [0xF1] = KEY_DOOR,
  807.     [0xF2] = KEY_DOOR,
  808.     [0xF3] = KEY_DOOR,
  809.     [0xF4] = COLL,
  810.     [0xF5] = COLL,
  811.     [0xF6] = COLL,
  812.     [0xF7] = COLL,
  813.     [0xF8] = COLL,
  814.     [0xF9] = COLL,
  815.     [0xFA] = COLL,
  816.     [0xFB] = COLL,
  817.     [0xFC] = COLL,
  818.     [0xFD] = COLL,
  819.     [0xFE] = COLL,
  820.     [0xFF] = COLL
  821. }
  822.  
  823. local function in_overworld()
  824.     return memory.readbyte(0x001B) == 0
  825. end
  826.  
  827. local tile_shit = {}
  828. local ow = true
  829. local owm = -1
  830. local owmy, owmx
  831. local uwm = -1
  832.  
  833. local mod_factor = 0x200
  834. local scale_factor = 2
  835. local scale_factor_tile = 8
  836. local MAP_OFFSET_BASE_X = 128
  837. local MAP_OFFSET_BASE_Y = 108
  838. local map_offset_x = MAP_OFFSET_BASE_X
  839. local map_offset_y = MAP_OFFSET_BASE_Y
  840.  
  841. for x = 0, 63 do
  842.     tile_shit[x] = {}
  843.     for y = 0, 63 do
  844.         tile_shit[x][y] = 0
  845.     end
  846. end
  847.  
  848. local function get_uw_tile(y, x, tilemap_mask, layer)
  849.     local tile_idx = y * 64 + x + (layer == 1 and 0x1000 or 0x0000)
  850.     return memory.read_u8(0x12000 + tile_idx)
  851. end
  852.  
  853. local function get_ow_tile(y, x)
  854.     local tile1_idx = (y * 64 + x) * 2
  855.     local tile1 = bit.lshift(memory.read_u16_le(0x2000 + tile1_idx), 2)
  856.  
  857.     local tile2_idx = bit.lshift(bit.bor(tile1, bit.band(x, 1)), 1)
  858.     local tile2 = memory.read_u16_le(0x0F8000 + tile2_idx, "System Bus")
  859.  
  860.     local tile3_idx = bit.band(tile2, 0x1FF)
  861.     local tile3 = memory.read_u16_le(0x0FFD94 + tile3_idx, "System Bus")
  862.  
  863.     if tile3 >= 0x10 and tile3 < 0x1C then
  864.         tile3 = bit.bor(bit.rol(bit.lshift(bit.band(tile3, 0x40), 1), 2), tile3)
  865.     end
  866.  
  867.     return bit.band(tile3, 0xFF)
  868. end
  869.  
  870. local big_ow_screens = {
  871.     [0x00] = true,
  872.     [0x03] = true,
  873.     [0x05] = true,
  874.     [0x18] = true,
  875.     [0x1B] = true,
  876.     [0x1E] = true,
  877.     [0x2A] = true,
  878.     [0x30] = true,
  879.     [0x35] = true,
  880.     [0x40] = true,
  881.     [0x43] = true,
  882.     [0x45] = true,
  883.     [0x58] = true,
  884.     [0x5B] = true,
  885.     [0x5E] = true,
  886.     [0x6A] = true,
  887.     [0x70] = true,
  888.     [0x75] = true
  889. }
  890.  
  891. local function get_all_tiles()
  892.     --print('get_all_tiles')
  893.     ow = in_overworld()
  894.  
  895.     if ow then
  896.         owm = memory.read_u8(0x8A)
  897.         owmx = bit.band(owm, 1) == 1
  898.         owmy = bit.band(math.floor(owm / 8), 1) == 1
  899.     else
  900.         uwm = memory.read_u8(0xA0)
  901.     end
  902.  
  903.     local layer = memory.read_u16_le(0x00EE)
  904.     local tilemap_mask = memory.read_u16_le(0x00EC)
  905.  
  906.     mod_factor = (ow) and 0x400 or 0x200
  907.     scale_factor = (ow) and 1 or 2
  908.     scale_factor_tile = (ow) and 16 or 8
  909.     map_offset_x = math.floor(MAP_OFFSET_BASE_X * MINIMAP_SCALE / scale_factor_tile)
  910.     map_offset_y = math.floor(MAP_OFFSET_BASE_Y * MINIMAP_SCALE / scale_factor_tile)
  911.  
  912.     for y = 0, 63 do
  913.         for x = 0, 63 do
  914.             local tile
  915.  
  916.             if ow then
  917.                 tile = get_ow_tile(y, x, tilemap_mask)
  918.             else
  919.                 tile = get_uw_tile(y, x, tilemap_mask, layer)
  920.             end
  921.  
  922.             tile_shit[x][y] = tile
  923.         end
  924.     end
  925. end
  926.  
  927. local got_map = false
  928.  
  929. local function draw_minimap()
  930.     --the_canvas.drawRectangle(MINIMAP_X-10, MINIMAP_Y-10, MINIMAP_SCALE * 80, MINIMAP_SCALE * 80, BLACK, BLACK)
  931.     the_canvas.drawRectangle(MINIMAP_X-1, MINIMAP_Y-1, MINIMAP_SCALE*64+2, MINIMAP_SCALE*64+2, COMPLETE, BLACK)
  932.  
  933.     for y = 0, 63 do
  934.         for x = 0, 63 do
  935.             local tile = tile_shit[x][y]
  936.             tile = collision_map[tile]
  937.  
  938.             if tile then
  939.                 the_canvas.drawRectangle(MINIMAP_X + x * MINIMAP_SCALE,
  940.                                          MINIMAP_Y + y * MINIMAP_SCALE,
  941.                                          MINIMAP_SCALE,
  942.                                          MINIMAP_SCALE,
  943.                                          tile - 0xAA000000,
  944.                                          tile)
  945.             end
  946.         end
  947.     end
  948.  
  949.     local linky = memory.read_u16_le(0x20)
  950.     local linkx = memory.read_u16_le(0x22)
  951.     local cameray = memory.read_u16_le(0x618)
  952.     local camerax = memory.read_u16_le(0x61C)
  953.  
  954.     linky = linky % mod_factor
  955.     linkx = linkx % mod_factor
  956.     cameray = cameray % mod_factor
  957.     camerax = camerax % mod_factor
  958.  
  959.     if ow then
  960.         if owmx then
  961.             linkx = bit.band(linkx + 0x200, 0x3FF)
  962.             camerax = bit.band(camerax + 0x200, 0x3FF)
  963.         end
  964.         if owmy then
  965.             linky = bit.band(linky + 0x200, 0x3FF)
  966.             cameray = bit.band(cameray + 0x200, 0x3FF)
  967.         end
  968.     end
  969.  
  970.     linky = math.floor(linky * MINIMAP_SCALE / scale_factor_tile)
  971.     linkx = math.floor(linkx * MINIMAP_SCALE / scale_factor_tile)
  972.     cameray = math.floor(cameray * MINIMAP_SCALE / scale_factor_tile)
  973.     camerax = math.floor(camerax * MINIMAP_SCALE / scale_factor_tile)
  974.  
  975.     the_canvas.drawRectangle(MINIMAP_X + camerax - map_offset_x, MINIMAP_Y + cameray - map_offset_y, 256 / scale_factor_tile * MINIMAP_SCALE, 236 / scale_factor_tile * MINIMAP_SCALE, CURRENT)
  976.     the_canvas.drawRectangle(MINIMAP_X + linkx, MINIMAP_Y + linky + MINIMAP_SCALE, MINIMAP_SCALE * scale_factor, MINIMAP_SCALE * scale_factor, VISITED)
  977. end
  978.  
  979. local modes_to_ignore = {
  980.     [0x7FBF] = true,
  981.     [0x0000] = true,
  982.     [0x0001] = true,
  983.     [0x0002] = true,
  984.     [0x0003] = true,
  985.     [0x0004] = true,
  986.     [0x0005] = true,
  987.     [0x0006] = true,
  988.     [0x0007] = true,
  989.     [0x0008] = true,
  990.     [0x0008] = true,
  991.     [0x0008] = true,
  992.     [0x0008] = true,
  993.     [0x0009] = true,
  994.     [0x0100] = true,
  995.     [0x0101] = true,
  996.     [0x0102] = true,
  997.     [0x0103] = true,
  998.     [0x0104] = true,
  999.     [0x0105] = true,
  1000.     [0x0105] = true,
  1001.     [0x0400] = true,
  1002.     [0x0401] = true,
  1003.     [0x0402] = true,
  1004.     [0x0403] = true,
  1005.     [0x0500] = true,
  1006.     [0x1701] = true,
  1007.     [0x1702] = true,
  1008.     [0x0500] = true,
  1009.     [0x1B02] = true
  1010. }
  1011.  
  1012. local active_link_modes = {
  1013.     [0x0700] = true,
  1014.     [0x0900] = true,
  1015.     [0x0B00] = true,
  1016. }
  1017.  
  1018. local refresh_map_modes = {
  1019.     [0x0703] = true,
  1020.     [0x0704] = true,
  1021.     [0x0705] = true,
  1022.     [0x0708] = true,
  1023.     [0x0709] = true,
  1024.     [0x070B] = true,
  1025.     [0x070C] = true,
  1026.     [0x070D] = true,
  1027.     [0x0710] = true,
  1028.     [0x0716] = true,
  1029.     [0x0717] = true,
  1030.     [0x071A] = true,
  1031. }
  1032.  
  1033. local function do_main()
  1034.     local last = nil
  1035.     while running do
  1036.         emu.frameadvance()
  1037.         local gamemode = memory.read_u16_be(0x0010)
  1038.  
  1039.         local current = tostring(in_overworld()) .. '-' .. tostring(memory.read_u8(in_overworld() and 0x8A or 0xA0))
  1040.         if current ~= last or refresh_map_modes[gamemode] then
  1041.             -- get new map when changing screen
  1042.             got_map = false
  1043.             last = current
  1044.         end
  1045.  
  1046.         if not got_map and active_link_modes[gamemode] then
  1047.             -- redraw map once we know
  1048.             get_all_tiles()
  1049.             got_map = true
  1050.         end
  1051.  
  1052.         if not modes_to_ignore[gamemode] then
  1053.             draw_minimap()
  1054.         end
  1055.     end
  1056.  
  1057.         --[[
  1058.         local ow_flag = memory.readbyte(0x001B)
  1059.  
  1060.         if not (ow_flag == 0x01) then
  1061.             if check_map and prev ~= nil then
  1062.                 draw_tile(prev)
  1063.                 prev = nil
  1064.             end
  1065.             check_map = false
  1066.         else
  1067.             if memory.readbyte(0x0010) == 0x07 then
  1068.                 check_map = true
  1069.             else
  1070.                 check_map = false
  1071.                 draw_tile(prev)
  1072.                 prev = nil
  1073.             end
  1074.         end
  1075.        
  1076.         if check_map then
  1077.             local tile = memory.readbyte(TILE_ADDR)
  1078.             local A9 = memory.readbyte(0xA9)
  1079.             local AA = memory.readbyte(0xAA)
  1080.  
  1081.             local quadrant = TILES_TO_VALS.A9AA[A9][AA]
  1082.             local quad_id = TILE_DATA[tile].r[quadrant]
  1083.  
  1084.             if quadrant ~= prev_quad or tile ~= prev then
  1085.                 prev_quad = quadrant
  1086.             end
  1087.  
  1088.             draw_tile(tile, quad_id)
  1089.  
  1090.             if tile ~= prev then
  1091.                 draw_tile(prev)
  1092.                 prev = tile
  1093.             end
  1094.         end
  1095. --]]
  1096. end
  1097.  
  1098. initialize()
  1099. --draw_all()
  1100. --get_all_tiles()
  1101. event.onloadstate(get_all_tiles)
  1102. do_main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement