hate underachiever forever
[QuestHelper.git] / radar.lua
blob47de78ef251b4224afcbbdd1fdfb6bb950603b98
1 QuestHelper_File["radar.lua"] = "Development Version"
2 QuestHelper_Loadtime["radar.lua"] = GetTime()
4 local tick = GetTime()
6 local anchor = nil
8 local rc = nil
10 local map = {}
12 local widgets = {}
13 local lwidth = 1.5
14 local lheight = 1.5
15 local quieted = true
17 local texparent = CreateFrame("Frame")
19 local grid = 3
21 QH_OnUpdate(function()
22 if not QuestHelper_Pref.radar then
23 if not quieted then
24 for _, v in ipairs(widgets) do
25 v[3] = nil
26 v[1]:Hide()
27 end
28 quieted = true
29 end
30 return
31 end
32 quieted = false
34 if not rc then rc = LibStub("LibRangeCheck-2.0") end
36 if tick <= GetTime() then
37 tick = tick + 1
39 local targ = UnitGUID("target")
40 if targ ~= anchor then
41 for _, v in pairs(map) do QuestHelper:ReleaseTable(v) end
42 wipe(map)
43 for _, v in ipairs(widgets) do
44 v[3] = nil
45 v[1]:Hide()
46 end
47 anchor = targ
48 end
50 local minRange, maxRange = rc:GetRange('target')
52 if minRange then
53 if not maxRange then maxRange = 100 end
55 minRange = math.max(0, minRange - 1)
56 maxRange = maxRange + 1
58 local mnr, mxr = minRange * minRange, maxRange * maxRange
59 local px, py = QuestHelper.routing_ax, QuestHelper.routing_ay
61 -- first we blur
62 local newmap = QuestHelper:CreateTable("radar")
63 for x, d in pairs(map) do
64 if not newmap[x - grid] then newmap[x - grid] = QuestHelper:CreateTable("radar") end
65 if not newmap[x] then newmap[x] = QuestHelper:CreateTable("radar") end
66 if not newmap[x + grid] then newmap[x + grid] = QuestHelper:CreateTable("radar") end
68 for y, v in pairs(d) do
69 newmap[x - grid][y - grid] = (newmap[x - grid][y - grid] or 0) + v
70 newmap[x - grid][y] = (newmap[x - grid][y] or 0) + v * 2
71 newmap[x - grid][y + grid] = (newmap[x - grid][y + grid] or 0) + v
72 newmap[x][y - grid] = (newmap[x][y - grid] or 0) + v * 2
73 newmap[x][y] = (newmap[x][y] or 0) + v * 4
74 newmap[x][y + grid] = (newmap[x][y + grid] or 0) + v * 2
75 newmap[x + grid][y - grid] = (newmap[x + grid][y - grid] or 0) + v
76 newmap[x + grid][y] = (newmap[x + grid][y] or 0) + v * 2
77 newmap[x + grid][y + grid] = (newmap[x + grid][y + grid] or 0) + v
78 end
80 QuestHelper:ReleaseTable(d)
81 end
82 QuestHelper:ReleaseTable(map)
83 map = newmap
86 -- then we crop
87 local highest = 0
88 local newmap = QuestHelper:CreateTable("radar")
89 for x, d in pairs(map) do
90 local newk = QuestHelper:CreateTable("radar")
91 local something = false
92 local dx = px - x
93 dx = dx * dx
94 for y, v in pairs(d) do
95 local dy = py - y
96 dy = dy * dy
97 --print(dx + dy, mnr, mxr)
98 if dx + dy >= mnr and dx + dy <= mxr then
99 newk[y] = v
100 highest = max(highest, v)
101 something = true
105 if something then
106 newmap[x] = newk
108 QuestHelper:ReleaseTable(d)
110 QuestHelper:ReleaseTable(map)
111 map = newmap
113 -- then we normalize
114 if highest > 0 then
115 highest = 1 / highest -- I don't know if mult is faster in lua or not, but it often is
116 for x, d in pairs(map) do
117 for y, v in pairs(d) do
118 d[y] = v * highest
123 -- then we add
124 -- probably a more efficient way to do this
125 px, py = math.floor(px / grid + 0.5) * grid, math.floor(py / grid + 0.5) * grid
126 for dx = 0, maxRange, grid do
127 for dy = 0, maxRange, grid do
128 local ofs = dx * dx + dy * dy
129 if ofs >= mnr and ofs <= mxr then
130 if not map[px + dx] then map[px + dx] = QuestHelper:CreateTable("radar") end
131 if not map[px - dx] then map[px - dx] = QuestHelper:CreateTable("radar") end
132 map[px + dx][py + dy] = (map[px + dx][py + dy] or 0) + 0.01
133 if dx > 0 then
134 map[px - dx][py + dy] = (map[px - dx][py + dy] or 0) + 0.01
135 if dy > 0 then
136 map[px - dx][py - dy] = (map[px - dx][py - dy] or 0) + 0.01
139 if dy > 0 then
140 map[px + dx][py - dy] = (map[px + dx][py - dy] or 0) + 0.01
146 -- then we post
147 local widgetofs = 1
148 for x, d in pairs(map) do
149 for y, v in pairs(d) do
150 if v > 0.1 then
151 local widg = widgets[widgetofs]
152 if not widg then
153 widg = {texparent:CreateTexture()}
154 table.insert(widgets, widg)
155 widg[1]:SetWidth(lwidth)
156 widg[1]:SetHeight(lheight)
158 widgetofs = widgetofs + 1
160 widg[1]:SetTexture(0, 1, 0, math.min(1, v * 1.2))
161 widg[1]:Show()
162 widg[2] = x
163 widg[3] = y
168 for rem = widgetofs, #widgets do
169 widgets[rem][1]:Hide()
170 widgets[rem][3] = nil
175 -- here we replace the widgets
177 local rotatey = (GetCVar("rotateMinimap") == "0") and 0 or GetPlayerFacing()
178 rotatey = rotatey * 180 / 3.14159
180 local mapdiam = QuestHelper.Astrolabe:GetMapDiameter()
181 local minimap = QuestHelper.Astrolabe:GetMinimapObject()
182 local mapwid, maphei = minimap:GetWidth(), minimap:GetHeight()
184 local xds, yds = mapwid / mapdiam, maphei / mapdiam
186 local nwidth, nheight = xds * grid, yds * grid
187 if nwidth ~= lwidth or nheight ~= lheight then
188 for _, widg in pairs(widgets) do
189 widg[1]:SetWidth(nwidth)
190 widg[1]:SetHeight(nheight)
193 lwidth, lheight = nwidth, nheight
195 local xdx, xdy, ydx, ydy = cos(rotatey), -sin(rotatey), -sin(rotatey), -cos(rotatey) -- this will be wrong, but I'll figure it out
196 xdx, ydx = xdx * xds, ydx * xds
197 xdy, ydy = xdy * yds, ydy * yds
199 local px, py = QuestHelper.routing_ax, QuestHelper.routing_ay
201 for _, v in pairs(widgets) do
202 if not v[3] then break end
204 local dx, dy = v[2] - px, v[3] - py
206 v[1]:SetPoint("CENTER", Minimap, "CENTER", dx * xdx + dy * ydx, dx * xdy + dy * ydy)
207 --print("placing", dx * xdx + dy * ydx, dx * xdy + dy * ydy)
209 end, "radar")
213 if ( minimapRotationEnabled ) then
214 minimapRotationOffset = -Astrolabe.GetFacing()