User File #51554590290287988

Upload All User Files

#51554590290287988 -

minimap.lua
795 downloads
Uploaded 11/30/2018 6:11 PM by fmp (see all 10)
local SCALE = client.getwindowsize()

local TILE_ADDR = 0x00A0

-- drawing
local CANVAS_WIDTH = 800
local CANVAS_HEIGHT = 450

local ROOM_SIZE = 12
local ROOM_GAP = 0
local TILE_SIZE = ROOM_SIZE * 2 + ROOM_GAP * 1

local X_OFFSET = 6
local Y_OFFSET = 50

local draw_POS = {
	[1] = { x = 0, y = 0 },
	[2] = { x = 1, y = 0 },
	[3] = { x = 0, y = 1 },
	[4] = { x = 1, y = 1 }
}

-- colors

local HM = 0xC0B8A8 -- houses
local HC = 0xA89868 -- hyrule castle
local AT = HC -- aga's tower
local EX = 0x706890 -- escape
local SY = 0x204020 -- sanctuary
local CV = 0x684830 -- caves

local EP = 0x80A870 -- eastern palace
local DP = 0x807838 -- desert palace
local TH = 0x987870 -- tower of hera

local PD = 0x909090 -- palace of darkness
local SP = 0x7890F8 -- swamp palace
local SW = 0x605030 -- skull woods
local TT = 0x787888 -- thieves town
local IP = 0xA8E0E8 -- ice palace
local MM = 0x408068 -- misery mire
local TR = 0x583028 -- turtle rock
local GT = 0xA09058 -- ganon's tower

local GE = 0x984848 -- empty gt TILEs
local EM = 0x606060 -- empty TILEs

local COMPLETE = 0xFFFF0000
local VISITED = 0xFFFFFF00
local UNVISITED = 0xFFAAAAAA
local CURRENT = 0xFF00FFFF

local BLACK = 0xFF000000
local WHITE = 0xFFFFFFFF

local OPAQUE = 0xFF000000
local TRANS = 0x99000000

local COLL = 0xFF505050
local SEMI = 0xFF000000
local CHEST = 0xFFDEB429

local MINIMAP_X = 266
local MINIMAP_Y = 5
local MINIMAP_SCALE = 3

local function get_color(color, opacity)
	return bit.bor(color, opacity)
end -- end get_color

-- room number from quadrant variables
local R1, R2, R3, R4 = 1, 2, 3, 4

local TILES_TO_VALS = {
	['RX'] = {
		R1 = { A9 = 0x00, AA = 0x00 },
		R2 = { A9 = 0x01, AA = 0x00 },
		R3 = { A9 = 0x00, AA = 0x02 },
		R4 = { A9 = 0x01, AA = 0x02 }
	},

	['A9AA'] = {
			[0x00] = {
				[0x00] = R1,
				[0x02] = R3
			},
			[0x01] = {
				[0x00] = R2,
				[0x02] = R4
		}
	}
}

local function stop_running()
	running = false
end -- stop_running

local function end_thing()
	--the_canvas.clear()
	client.SetGameExtraPadding(0, 0, 0, 0)
	stop_running()
end -- end_thing

local function initialize()
	running = true

	memory.usememorydomain("WRAM")
	client.SetGameExtraPadding(0, 0, 230, 0)
	the_canvas = gui

	event.onexit(end_thing)
end -- end initialize

--[==[
Format:
	[0x##] = {          -- room id
		c = XX          -- color
		p = AABA        -- room pattern
		r = { p }       -- pattern as table
		v = 0100        -- rooms reachable without glitches
		x = { v }       -- reachability as table; no dupes
	}
]==]

local TILE_DATA = {
	[0x00] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x01] = { c = HC, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x02] = { c = SY, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x03] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x04] = { c = TR, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x05] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x06] = { c = SP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x07] = { c = TH, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x08] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x09] = { c = PD, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0A] = { c = PD, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x0B] = { c = PD, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0C] = { c = GT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x0D] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0E] = { c = IP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x0F] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0x10] = { c = GT, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x11] = { c = EX, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x12] = { c = SY, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x13] = { c = TR, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x14] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x15] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x16] = { c = SP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0x17] = { c = TH, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x18] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x19] = { c = PD, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x1A] = { c = PD, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0x1B] = { c = PD, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x1C] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x1D] = { c = GT, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x1E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x1F] = { c = IP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },

	[0x20] = { c = AT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x21] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x22] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x23] = { c = TR, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x24] = { c = TR, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x25] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x26] = { c = SP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0x27] = { c = TH, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x28] = { c = SP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x29] = { c = SW, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x2A] = { c = PD, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x2B] = { c = PD, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0x2C] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x2D] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x2E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x2F] = { c = CV, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },

	[0x30] = { c = AT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x31] = { c = TH, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x32] = { c = EX, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x33] = { c = DP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x34] = { c = SP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x35] = { c = SP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x36] = { c = SP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x37] = { c = SP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x38] = { c = SP, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x39] = { c = SW, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x3A] = { c = PD, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x3B] = { c = PD, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x3C] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x3D] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x3E] = { c = IP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0x3F] = { c = IP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },

	[0x40] = { c = AT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0x41] = { c = HC, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x42] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x43] = { c = DP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x44] = { c = TT, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x45] = { c = TT, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x46] = { c = SP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x47] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x48] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x49] = { c = SW, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x4A] = { c = PD, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x4B] = { c = PD, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0x4C] = { c = GT, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x4D] = { c = GT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x4E] = { c = IP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0x4F] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0x50] = { c = HC, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x51] = { c = HC, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x52] = { c = HC, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x53] = { c = DP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x54] = { c = SP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x55] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x56] = { c = SW, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x57] = { c = SW, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x58] = { c = SW, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x59] = { c = SW, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x5A] = { c = PD, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x5B] = { c = GT, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x5C] = { c = GT, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x5D] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x5E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x5F] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0x60] = { c = HC, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x61] = { c = HC, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x62] = { c = HC, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x63] = { c = DP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x64] = { c = TT, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x65] = { c = TT, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x66] = { c = SP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0x67] = { c = SW, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x68] = { c = SW, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x69] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x6A] = { c = PD, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x6B] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x6C] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x6D] = { c = GT, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x6E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x6F] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0x70] = { c = EX, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x71] = { c = EX, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x72] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x73] = { c = DP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x74] = { c = DP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x75] = { c = DP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x76] = { c = SP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x77] = { c = TH, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x78] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x79] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x7A] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x7B] = { c = GT, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x7C] = { c = GT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0x7D] = { c = GT, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x7E] = { c = IP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x7F] = { c = IP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },

	[0x80] = { c = EX, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x81] = { c = EX, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x82] = { c = EX, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x83] = { c = DP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x84] = { c = DP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x85] = { c = DP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x86] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x87] = { c = TH, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x88] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x89] = { c = EP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x8A] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x8B] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x8C] = { c = GT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x8D] = { c = GT, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x8E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x8F] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0x90] = { c = MM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x91] = { c = MM, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x92] = { c = MM, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x93] = { c = MM, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x94] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x95] = { c = GT, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x96] = { c = GT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0x97] = { c = MM, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x98] = { c = MM, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x99] = { c = EP, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0x9A] = { c = GE, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x9B] = { c = GT, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0x9C] = { c = GT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x9D] = { c = GT, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0x9E] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x9F] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0xA0] = { c = MM, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0xA1] = { c = MM, p = 'AABA', r = { 'A', 'A', 'B', 'A' } },
	[0xA2] = { c = MM, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xA3] = { c = MM, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xA4] = { c = TR, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xA5] = { c = GT, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0xA6] = { c = GT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xA7] = { c = TH, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },
	[0xA8] = { c = EP, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0xA9] = { c = EP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xAA] = { c = EP, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0xAB] = { c = TT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xAC] = { c = TT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xAD] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xAE] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xAF] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0xB0] = { c = AT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xB1] = { c = MM, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0xB2] = { c = MM, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0xB3] = { c = MM, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0xB4] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xB5] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xB6] = { c = TR, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xB7] = { c = TR, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xB8] = { c = EP, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xB9] = { c = EP, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xBA] = { c = EP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xBB] = { c = TT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0xBC] = { c = TT, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0xBD] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xBE] = { c = IP, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0xBF] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0xC0] = { c = AT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0xC1] = { c = MM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xC2] = { c = MM, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xC3] = { c = MM, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xC4] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xC5] = { c = TR, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xC6] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xC7] = { c = TR, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xC8] = { c = EP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xC9] = { c = EP, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0xCA] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xCB] = { c = TT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xCC] = { c = TT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xCD] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xCE] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xCF] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0xD0] = { c = AT, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0xD1] = { c = MM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xD2] = { c = MM, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xD3] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xD4] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xD5] = { c = TR, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xD6] = { c = TR, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xD7] = { c = EM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xD8] = { c = EP, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0xD9] = { c = EP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0xDA] = { c = EP, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0xDB] = { c = TT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xDC] = { c = TT, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xDD] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xDE] = { c = IP, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xDF] = { c = CV, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },

	[0xE0] = { c = AT, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xE1] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xE2] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xE3] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xE4] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xE5] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xE6] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xE7] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xE8] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xE9] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xEA] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xEB] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xEC] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xED] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xEE] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xEF] = { c = CV, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },

	[0xF0] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xF1] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xF2] = { c = HM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xF3] = { c = HM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xF4] = { c = HM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xF5] = { c = HM, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xF6] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xF7] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xF8] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xF9] = { c = CV, p = 'ABBB', r = { 'A', 'B', 'B', 'B' } },
	[0xFA] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0xFB] = { c = CV, p = 'ABBB', r = { 'A', 'B', 'B', 'B' } },
	[0xFC] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0xFD] = { c = CV, p = 'ABAA', r = { 'A', 'B', 'A', 'A' } },
	[0xFE] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0xFF] = { c = CV, p = 'ABCC', r = { 'A', 'B', 'C', 'C' } },

	-- underworld 2
	[0x0100] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0101] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0102] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0103] = { c = CV, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0x0104] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0105] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0106] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0107] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0108] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0109] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x010A] = { c = CV, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0x010B] = { c = CV, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x010C] = { c = CV, p = 'ABCB', r = { 'A', 'B', 'C', 'B' } },
	[0x010D] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x010E] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x010F] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0x0110] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0111] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0112] = { c = CV, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0x0113] = { c = CV, p = 'ABAB', r = { 'A', 'B', 'A', 'B' } },
	[0x0114] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0115] = { c = CV, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0x0116] = { c = CV, p = 'AABC', r = { 'A', 'A', 'B', 'C' } },
	[0x0117] = { c = CV, p = 'AAAA', r = { 'A', 'A', 'A', 'A' } },
	[0x0118] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0119] = { c = CV, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x011A] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x011B] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x011C] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x011D] = { c = CV, p = 'AABB', r = { 'A', 'A', 'B', 'B' } },
	[0x011E] = { c = CV, p = 'ABAC', r = { 'A', 'B', 'A', 'C' } },
	[0x011F] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },

	[0x0120] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0121] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0122] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0123] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0124] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0125] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0126] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
	[0x0127] = { c = CV, p = 'ABCD', r = { 'A', 'B', 'C', 'D' } },
}

-- room tracking
local prev = nil
local prev_quad = nil
local check_map = false

-- drawing
local function draw_tile(tile, quad_id)
	if tile == nil then return end

	local x = (bit.band(tile, 0x0F) * TILE_SIZE + 1) + X_OFFSET
	local y = ((bit.band(tile, 0xF0) / 16) * TILE_SIZE + 1) + Y_OFFSET

	the_canvas.drawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK, BLACK) -- clear only this section

	local d = TILE_DATA[tile]
	local r = d.r

	local room_completion_colors = { 0, 0, 0, 0 }

	for i=1, 4 do
		room_completion_colors[i] =
			quad_id == TILE_DATA[tile].r[i] and CURRENT
				or UNVISITED and OPAQUE or TRANS
		the_canvas.drawRectangle(
				x + (ROOM_SIZE) * draw_POS[i].x,
				y + (ROOM_SIZE) * draw_POS[i].y,
				ROOM_SIZE,
				ROOM_SIZE,
				nil,
				room_completion_colors[i]
			)
		if TILE_DATA[tile].x[r[i]] == 0 then
			--[[the_canvas.drawText(
				x + (ROOM_SIZE) * draw_POS[i].x + 1,
				y + (ROOM_SIZE) * draw_POS[i].y - 1,
				"X",
				0x55FFFFFF,
				0
			)]]
		end
	end

	local C0

	if quad_id then
		C0 = CURRENT
	else
		C0 = UNVISITED
	end

	-- vertical separators
	local C1 = r[1] == r[2] and room_completion_colors[1] or C0
	local C2 = r[3] == r[4] and room_completion_colors[3] or C0

	the_canvas.drawLine(x + ROOM_SIZE + ROOM_GAP, y + ROOM_GAP,
							x + ROOM_SIZE + ROOM_GAP, y + ROOM_SIZE,
							C1)

	the_canvas.drawLine(x + ROOM_GAP + ROOM_SIZE, y + ROOM_GAP + ROOM_GAP + ROOM_SIZE,
							x + ROOM_GAP + ROOM_SIZE, y + ROOM_SIZE + ROOM_SIZE,
							C2)

	-- horizontal separators
	local C3 = r[1] == r[3] and room_completion_colors[1] or C0
	local C4 = r[2] == r[4] and room_completion_colors[2] or C0

	the_canvas.drawLine(x + ROOM_GAP + ROOM_GAP, y + ROOM_GAP + ROOM_SIZE,
							x + ROOM_SIZE, y + ROOM_GAP + ROOM_SIZE,
							C3)

	the_canvas.drawLine(x + ROOM_GAP + ROOM_GAP + ROOM_SIZE, y + ROOM_GAP + ROOM_SIZE,
							x + ROOM_SIZE + ROOM_SIZE, y + ROOM_GAP + ROOM_SIZE,
							C4)

	local C5 = d.p == 'AAAA' and C1 or C0

	the_canvas.drawPixel(x + ROOM_GAP + ROOM_SIZE, y + ROOM_GAP + ROOM_SIZE, BLACK)
	the_canvas.drawPixel(x + ROOM_GAP + ROOM_SIZE, y + ROOM_GAP + ROOM_SIZE, C5)

	the_canvas.drawRectangle(x, y, TILE_SIZE, TILE_SIZE, C0, nil)
end -- end draw_tile

local function draw_all()
	the_canvas.Clear(BLACK)
	for i=0, 0x0127 do
		draw_tile(i)
	end

	--the_canvas.refresh()
end -- end draw_all

local collision_map = {
	[0x01] = COLL,
	[0x02] = 0xFF707070, -- rails
	[0x03] = COLL,
	[0x04] = COLL,
	[0x0B] = COLL,
	[0x26] = COLL,
	[0x27] = COLL,
	[0x43] = COLL,
	[0x44] = 0xFFEEEEEE, -- spike
	[0x46] = COLL,
	[0x50] = COLL,
	[0x51] = COLL,
	[0x52] = COLL,
	[0x53] = COLL,
	[0x54] = COLL,
	[0x55] = COLL,
	[0x56] = COLL,
	[0x57] = COLL,
	[0x6C] = COLL,
	[0x6D] = COLL,
	[0x6E] = COLL,
	[0x6F] = COLL,
	[0x70] = 0xFF7B4A4A,
	[0x71] = 0xFF7B4A4A,
	[0x72] = 0xFF7B4A4A,
	[0x73] = 0xFF7B4A4A,
	[0x74] = 0xFF7B4A4A,
	[0x75] = 0xFF7B4A4A,
	[0x76] = 0xFF7B4A4A,
	[0x77] = 0xFF7B4A4A,
	[0x78] = 0xFF7B4A4A,
	[0x79] = 0xFF7B4A4A,
	[0x7A] = COLL,
	[0x7B] = COLL,
	[0x7C] = COLL,
	[0x7D] = COLL,
	[0x7E] = COLL,
	[0x7F] = COLL,
	[0xC0] = COLL,
	[0xC1] = COLL,
	[0xC2] = COLL,
	[0xC3] = COLL,
	[0xC4] = COLL,
	[0xC5] = COLL,
	[0xC6] = COLL,
	[0xC7] = COLL,
	[0xC8] = COLL,
	[0xC9] = COLL,
	[0xCA] = COLL,
	[0xCB] = COLL,
	[0xCC] = COLL,
	[0xCD] = COLL,
	[0xCE] = COLL,
	[0xCF] = COLL,
	[0xF0] = COLL,
	[0xF1] = COLL,
	[0xF2] = COLL,
	[0xF3] = COLL,
	[0xF4] = COLL,
	[0xF5] = COLL,
	[0xF6] = COLL,
	[0xF7] = COLL,
	[0xF8] = COLL,
	[0xF9] = COLL,
	[0xFA] = COLL,
	[0xFB] = COLL,
	[0xFC] = COLL,
	[0xFD] = COLL,
	[0xFE] = COLL,
	[0xFF] = COLL,
-- other
	[0x09] = 0xFF4A5ABD, -- water
	[0x0D] = 0xFFA4A4A4, -- spike floor
	[0x20] = 0xFF660000, -- pits
	[0x1D] = 0xFF6A4A31, -- stairs
	[0x1E] = 0xFF6A4A31, -- stairs
	[0x1F] = 0xFF6A4A31, -- stairs
	[0x66] = 0xFF414183, -- peg blocks
	[0x67] = 0xFF6A6AC5,
-- semi_collision_map
	[0x0E] = SEMI,
	[0x0F] = SEMI,
	[0x10] = SEMI,
	[0x11] = SEMI,
	[0x12] = SEMI,
	[0x13] = SEMI,
	[0x18] = SEMI,
	[0x19] = SEMI,
	[0x1A] = SEMI,
	[0x1B] = SEMI,
-- chest_map
	[0x58] = CHEST,
	[0x59] = CHEST,
	[0x5A] = CHEST,
	[0x5B] = CHEST,
	[0x5C] = CHEST,
	[0x5D] = CHEST
} --

local curmap = {}

local function in_overworld()
	return memory.readbyte(0x001B) == 0
end

local tile_shit = {}

for x = 0, 63 do
	tile_shit[x] = {}
	for y = 0, 63 do
		tile_shit[x][y] = 0
	end
end

local function get_uw_tile(y, x, tilemap_mask, layer) 
	local tile_idx = y * 64 + x + (layer == 1 and 0x1000 or 0x0000)

	local tile = curmap[tile_idx]
	return tile
end

local function get_ow_tile(y, x)
	-- TODO
	local tmp_00 = 0
	local tmp_02 = x

	local tile_idx = (y * 64 + x) * 2
	local tmp_06 = bit.lshift(memory.read_u16_le(0x2000 + tile_idx), 2)

	local tile_idx2 = bit.lshift(bit.bor(tmp_06, bit.band(tmp_02, 1)), 1)
	tmp_06 = memory.read_u16_le(0x0F8000 + tile_idx2, "System Bus")

	local tile_idx3 = bit.band(tmp_06, 0x1FF)
	local tile = memory.read_u16_le(0x0FFD94 + tile_idx3, "System Bus")

	if tile >= 0x10 and tile < 0x1C then
		tile = bit.bor(bit.rol(bit.lshift(bit.band(tile, 0x40), 1), 2), tile)
	end

	return bit.band(tile, 0xFF)
end

local ow = true
local function get_all_tiles()
	ow = in_overworld()
	curmap = memory.readbyterange(0x12000, 64 * 64 * 2)

	local layer = memory.read_u16_le(0x00EE)
	local tilemap_mask = memory.read_u16_le(0x00EC)

	for y = 0, 63 do
		for x = 0, 63 do
			local tile

			if ow then
				tile = get_ow_tile(y, x, tilemap_mask)
			else
				tile = get_uw_tile(y, x, tilemap_mask, layer)
			end

			tile_shit[x][y] = tile
		end
	end
end

local got_map = false

local function draw_minimap()
	the_canvas.drawRectangle(MINIMAP_X-10, MINIMAP_Y-10, MINIMAP_SCALE * 80, MINIMAP_SCALE * 80, BLACK, BLACK)
	the_canvas.drawRectangle(MINIMAP_X-1, MINIMAP_Y-1, MINIMAP_SCALE*64+2, MINIMAP_SCALE*64+2, CURRENT, BLACK)

	for y = 0, 63 do
		for x = 0, 63 do
			local tile = tile_shit[x][y]
			tile = collision_map[tile]

			if tile then
				the_canvas.drawRectangle(MINIMAP_X + x * MINIMAP_SCALE, MINIMAP_Y + y * MINIMAP_SCALE, MINIMAP_SCALE, MINIMAP_SCALE, tile - 0xAA000000, tile)
			end
		end
	end

	if ow then
		local linky = memory.read_u16_le(0x20) % 0x400
		local linkx = memory.read_u16_le(0x22) % 0x400

		local owm = memory.read_u8(0x8A)
		if bit.band(owm, 1) == 1 then
			linkx = bit.band(linkx + 0x200, 0x3FF)
		end
		if bit.band(math.floor(owm / 8), 1) == 1 then
			linky = bit.band(linky + 0x200, 0x3FF)
		end

		linkx = math.floor(linkx * MINIMAP_SCALE / 16)
		linky = math.floor(linky * MINIMAP_SCALE / 16)
		the_canvas.drawRectangle(MINIMAP_X + linkx, MINIMAP_Y + linky + MINIMAP_SCALE, MINIMAP_SCALE, MINIMAP_SCALE, VISITED)
	else
		local linky = (memory.read_u16_le(0x20) % 0x200) * MINIMAP_SCALE / 8
		local linkx = (memory.read_u16_le(0x22) % 0x200) * MINIMAP_SCALE / 8
		the_canvas.drawRectangle(MINIMAP_X + linkx, MINIMAP_Y + linky + MINIMAP_SCALE, MINIMAP_SCALE * 2, MINIMAP_SCALE * 2, VISITED)
	end
end

local modes_to_ignore = {
	[0x7FBF] = true,
	[0x0000] = true,
	[0x0001] = true,
	[0x0002] = true,
	[0x0003] = true,
	[0x0004] = true,
	[0x0005] = true,
	[0x0006] = true,
	[0x0007] = true,
	[0x0008] = true,
	[0x0008] = true,
	[0x0008] = true,
	[0x0008] = true,
	[0x0009] = true,
	[0x0100] = true,
	[0x0101] = true,
	[0x0102] = true,
	[0x0103] = true,
	[0x0104] = true,
	[0x0105] = true,
	[0x0105] = true,
	[0x0400] = true,
	[0x0401] = true,
	[0x0402] = true,
	[0x0403] = true,
	[0x0500] = true,
	[0x1701] = true,
	[0x1702] = true,
	[0x0500] = true,
	[0x1B02] = true
}

local modes_to_refresh = {
	[0x0702] = true,
	[0x0703] = true,
	[0x0707] = true,
	[0x0708] = true,
	[0x070B] = true,
	[0x070C] = true,
	[0x070D] = true,
	[0x070E] = true,
	[0x070F] = true,
	[0x0710] = true,
	[0x0712] = true,
	[0x0713] = true,
	[0x0715] = true,
	[0x0716] = true,
	[0x071A] = true,
	[0x0901] = true,
	[0x0902] = true,
	[0x0903] = true,
	[0x0904] = true,
	[0x0905] = true,
	[0x0906] = true,
	[0x0907] = true,
	[0x0908] = true,
	[0x090A] = true,
	[0x0923] = true,
	[0x1001] = true
}

local function do_main()
	while running do
		emu.frameadvance()
		--SCALE = client.getwindowsize()

		--[[
		local ow_flag = memory.readbyte(0x001B)

		if not (ow_flag == 0x01) then
			if check_map and prev ~= nil then
				draw_tile(prev)
				prev = nil
			end
			check_map = false
		else
			if memory.readbyte(0x0010) == 0x07 then
				check_map = true
			else
				check_map = false
				draw_tile(prev)
				prev = nil
			end
		end
		
		if check_map then
			local tile = memory.readbyte(TILE_ADDR)
			local A9 = memory.readbyte(0xA9)
			local AA = memory.readbyte(0xAA)

			local quadrant = TILES_TO_VALS.A9AA[A9][AA]
			local quad_id = TILE_DATA[tile].r[quadrant]

			if quadrant ~= prev_quad or tile ~= prev then
				prev_quad = quadrant
			end

			draw_tile(tile, quad_id)

			if tile ~= prev then
				draw_tile(prev)
				prev = tile
			end
		end
--]]
	local gamemode = memory.read_u16_be(0x0010)

		if modes_to_refresh[gamemode] or not got_map then
			get_all_tiles()
			if modes_to_refresh[gamemode] then
				got_map = false
			else
				got_map = true
			end
		else
			got_map = true
		end

		if not modes_to_ignore[gamemode] then
			draw_minimap()
		end
	end
end -- end do_main

initialize()
--draw_all()
get_all_tiles()
do_main()