-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.lua
More file actions
468 lines (437 loc) · 19.5 KB
/
Core.lua
File metadata and controls
468 lines (437 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
-- Wick's Travel Form
-- Core.lua — form decision logic, macro builder, event dispatch
local ADDON, ns = ...
_G.WICKSTRAVELFORM = ns
ns.version = "0.2.3"
local DEFAULTS = {
point = "CENTER", relativePoint = "CENTER", x = 0, y = -120,
locked = true,
size = 48,
barEnabled = true,
barSegH = 5,
barGap = 2,
barMargin = 2,
barFloat = false,
barFloatW = 120,
barFloatSegH = 5,
barFloatGap = 2,
barFloatX = 0,
barFloatY = 100,
}
ns.MIN_SIZE, ns.MAX_SIZE = 32, 96
-- =====================================================================
-- Druid forms (enUS spell names — used directly in macro text)
-- =====================================================================
ns.FORMS = {
AQUATIC = "Aquatic Form",
TRAVEL = "Travel Form",
CAT = "Cat Form",
FLIGHT = "Flight Form",
SWIFT = "Swift Flight Form",
}
local FORM_SPELL_ID = {
[ns.FORMS.AQUATIC] = 1066,
[ns.FORMS.TRAVEL] = 783,
[ns.FORMS.CAT] = 768,
[ns.FORMS.FLIGHT] = 33943,
[ns.FORMS.SWIFT] = 40120,
}
function ns.formIcon(name)
local _, _, icon = GetSpellInfo(FORM_SPELL_ID[name] or 0)
return icon
end
-- =====================================================================
-- IsFlyableArea() reports incorrectly for Azeroth zones in TBC, so we
-- maintain our own whitelist. enUS-only for now.
-- =====================================================================
local FLYABLE_ZONES = {
["Hellfire Peninsula"] = true,
["Zangarmarsh"] = true,
["Terokkar Forest"] = true,
["Nagrand"] = true,
["Blade's Edge Mountains"] = true,
["Netherstorm"] = true,
["Shadowmoon Valley"] = true,
["Isle of Quel'Danas"] = true,
}
-- Shattrath is flagged indoors by WoW but flying is allowed there.
-- These zones need the flight clause without the [outdoors] conditional.
local FLYABLE_INDOOR_ZONES = {
["Shattrath City"] = true,
}
function ns.isFlyableZone()
local zone = GetRealZoneText()
return zone and (FLYABLE_ZONES[zone] == true or FLYABLE_INDOOR_ZONES[zone] == true)
end
function ns.isFlyableIndoorZone()
local zone = GetRealZoneText()
return zone and FLYABLE_INDOOR_ZONES[zone] == true
end
-- =====================================================================
-- Cached best flight form. Refreshed on SPELLS_CHANGED so we don't scan
-- the spellbook every press.
-- =====================================================================
local cachedFlightForm = nil
local function scanFlightForm()
local best = nil
for tab = 1, GetNumSpellTabs() do
local _, _, offset, num = GetSpellTabInfo(tab)
for i = offset + 1, offset + num do
local n = GetSpellBookItemName(i, BOOKTYPE_SPELL or "spell")
if n == ns.FORMS.SWIFT then return ns.FORMS.SWIFT end
if n == ns.FORMS.FLIGHT then best = ns.FORMS.FLIGHT end
end
end
return best
end
function ns.bestFlightForm()
return cachedFlightForm
end
-- =====================================================================
-- Predict which form a press will resolve to (used for the icon preview
-- and tooltip — the actual cast resolution is done by the macro itself).
-- =====================================================================
function ns.predictForm()
if IsSwimming() then return ns.FORMS.AQUATIC end
local fly = ns.bestFlightForm()
if fly and ns.isFlyableIndoorZone() and not InCombatLockdown() then
return fly
end
if IsOutdoors() then
if fly and ns.isFlyableZone() and not InCombatLockdown() then
return fly
end
return ns.FORMS.TRAVEL
end
return ns.FORMS.CAT
end
local MANAGED_SPELL_IDS = {
[1066] = true, -- Aquatic Form
[783] = true, -- Travel Form
[33943] = true, -- Flight Form
[40120] = true, -- Swift Flight Form
[768] = true, -- Cat Form
}
local FORM_SPELL_TO_NAME = {
[1066] = ns.FORMS.AQUATIC,
[783] = ns.FORMS.TRAVEL,
[33943] = ns.FORMS.FLIGHT,
[40120] = ns.FORMS.SWIFT,
[768] = ns.FORMS.CAT,
}
-- Returns the spell name of the current managed form, or nil if not in one.
function ns.currentManagedForm()
local formIndex = GetShapeshiftForm()
if formIndex == 0 then return nil end
local _, _, _, spellId = GetShapeshiftFormInfo(formIndex)
return FORM_SPELL_TO_NAME[spellId]
end
function ns.isInManagedForm()
return ns.currentManagedForm() ~= nil
end
function ns.isFlying()
local formIndex = GetShapeshiftForm()
if formIndex == 0 then return false end
local _, _, _, spellId = GetShapeshiftFormInfo(formIndex)
return spellId == 33943 or spellId == 40120
end
-- =====================================================================
-- Build the macrotext. The macro itself runs the swim / outdoors /
-- combat conditional checks at click time — those are cheap and always
-- correct. Lua only decides whether to inject the flight clause based
-- on zone + spellbook (rare events), so we don't need any polling.
-- =====================================================================
function ns.buildMacro()
-- Only /cancelform when the predicted cast would be the same form we're already
-- in (powershift prevention). Cross-form transitions (e.g. Cat -> Flight) work
-- with a direct /cast — TBC transitions the form in a single GCD.
local current = ns.currentManagedForm()
if current and current == ns.predictForm() then
return "/cancelform"
end
local clauses = { "[swimming] " .. ns.FORMS.AQUATIC }
local fly = ns.bestFlightForm()
if fly then
if ns.isFlyableIndoorZone() then
-- Shattrath and similar: flyable but IsOutdoors() returns false.
table.insert(clauses, ("[nocombat] %s"):format(fly))
elseif ns.isFlyableZone() then
table.insert(clauses, ("[nocombat,outdoors] %s"):format(fly))
end
end
table.insert(clauses, "[outdoors] " .. ns.FORMS.TRAVEL)
table.insert(clauses, ns.FORMS.CAT)
return "/cast " .. table.concat(clauses, "; ")
end
-- =====================================================================
-- Event dispatcher
-- =====================================================================
local events = {}
function ns:On(event, fn)
events[event] = events[event] or {}
table.insert(events[event], fn)
end
local frame = CreateFrame("Frame", "WicksTravelFormEvents")
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
frame:SetScript("OnEvent", function(_, event, ...)
if events[event] then
for _, fn in ipairs(events[event]) do fn(...) end
end
end)
-- =====================================================================
-- ADDON_LOADED — SavedVariables init (DB isn't readable before this).
-- =====================================================================
ns:On("ADDON_LOADED", function(loaded)
if loaded ~= ADDON then return end
WicksTravelFormDB = WicksTravelFormDB or {}
for k, v in pairs(DEFAULTS) do
if WicksTravelFormDB[k] == nil then WicksTravelFormDB[k] = v end
end
end)
-- =====================================================================
-- One-time setup — runs at the first PLAYER_LOGIN OR PLAYER_ENTERING_WORLD
-- to handle both fresh-login and /reload cases. Class check, event
-- registration, spellbook scan, UI activation.
-- =====================================================================
local inited = false
local function initIfNeeded()
if inited then return end
inited = true
local _, class = UnitClass("player")
if class ~= "DRUID" then
if ns.UI and ns.UI.Deactivate then ns.UI:Deactivate() end
return
end
for _, ev in ipairs({
"ZONE_CHANGED_NEW_AREA",
"ZONE_CHANGED_INDOORS",
"ZONE_CHANGED",
"PLAYER_REGEN_DISABLED",
"PLAYER_REGEN_ENABLED",
"UPDATE_SHAPESHIFT_FORM",
"SPELLS_CHANGED",
"UPDATE_BINDINGS",
"UNIT_POWER_UPDATE",
"UNIT_MAXPOWER",
}) do frame:RegisterEvent(ev) end
cachedFlightForm = scanFlightForm()
if ns.UI and ns.UI.Activate then ns.UI:Activate() end
end
ns:On("PLAYER_LOGIN", initIfNeeded)
ns:On("PLAYER_ENTERING_WORLD", function()
initIfNeeded()
if ns.UI and ns.UI.Refresh then ns.UI:Refresh() end
end)
local function refresh() if ns.UI and ns.UI.Refresh then ns.UI:Refresh() end end
ns:On("SPELLS_CHANGED", function() cachedFlightForm = scanFlightForm(); refresh() end)
ns:On("ZONE_CHANGED_NEW_AREA", refresh)
ns:On("ZONE_CHANGED_INDOORS", refresh)
ns:On("ZONE_CHANGED", refresh)
ns:On("PLAYER_REGEN_DISABLED", refresh)
ns:On("PLAYER_REGEN_ENABLED", refresh)
ns:On("UPDATE_SHAPESHIFT_FORM", function()
refresh()
if ns.ApplyBarLayout then ns.ApplyBarLayout() end
if ns.ApplyFloatBarLayout then ns.ApplyFloatBarLayout() end
end)
ns:On("UPDATE_BINDINGS", function() if ns.UI and ns.UI.UpdateBindLabel then ns.UI:UpdateBindLabel() end end)
ns:On("UNIT_POWER_UPDATE", function(unit) if unit == "player" and ns.UpdateResourceBar then ns.UpdateResourceBar() end end)
ns:On("UNIT_MAXPOWER", function(unit) if unit == "player" and ns.UpdateResourceBar then ns.UpdateResourceBar() end end)
-- =====================================================================
-- Slash command
-- =====================================================================
SLASH_WICKSTRAVELFORM1 = "/wstf"
SLASH_WICKSTRAVELFORM2 = "/wtravel"
SlashCmdList["WICKSTRAVELFORM"] = function(msg)
msg = (msg or ""):lower():gsub("^%s+", ""):gsub("%s+$", "")
local _, class = UnitClass("player")
if class ~= "DRUID" then
print("|cff8a5cf6Wick's Travel Form|r: druids only.")
return
end
if msg == "unlock" or msg == "move" then
if ns.UI then ns.UI:SetLocked(false) end
return
elseif msg == "lock" then
if ns.UI then ns.UI:SetLocked(true) end
return
elseif msg == "reset" then
WicksTravelFormDB.point, WicksTravelFormDB.relativePoint = "CENTER", "CENTER"
WicksTravelFormDB.x, WicksTravelFormDB.y = 0, -120
WicksTravelFormDB.size = 48
if ns.UI then
if ns.UI.ApplyPosition then ns.UI:ApplyPosition() end
if ns.UI.ApplySize then ns.UI:ApplySize() end
end
if ns.ApplyBarLayout then ns.ApplyBarLayout() end
return
elseif msg:match("^size") then
local arg = msg:match("^size%s+(%S+)")
if not arg then
print(("|cff8a5cf6Wick's Travel Form|r: current size %d. Use /wstf size <%d-%d>."):format(
WicksTravelFormDB.size or 48, ns.MIN_SIZE, ns.MAX_SIZE))
return
end
local n = tonumber(arg)
if not n then
print("|cff8a5cf6Wick's Travel Form|r: size must be a number.")
return
end
if n < ns.MIN_SIZE then n = ns.MIN_SIZE end
if n > ns.MAX_SIZE then n = ns.MAX_SIZE end
WicksTravelFormDB.size = n
if ns.UI and ns.UI.ApplySize then ns.UI:ApplySize() end
if ns.ApplyBarLayout then ns.ApplyBarLayout() end
return
elseif msg == "debug" then
local zone = GetRealZoneText() or "?"
print("|cff8a5cf6Wick's Travel Form|r debug:")
print((" zone: %s flyable-zone: %s"):format(zone, tostring(ns.isFlyableZone())))
print((" best flight form: %s"):format(tostring(ns.bestFlightForm())))
print((" swimming: %s outdoors: %s combat: %s"):format(
tostring(IsSwimming()), tostring(IsOutdoors()), tostring(InCombatLockdown())))
print((" predicted form: %s"):format(ns.predictForm()))
print((" macro: %s"):format(ns.buildMacro()))
print((" ns.UI: %s button: %s"):format(
tostring(ns.UI), tostring(_G.WicksTravelFormButton)))
local h = _G.WicksTravelFormHost
local b = _G.WicksTravelFormButton
if h then
local p, _, rp, x, y = h:GetPoint(1)
print((" host shown: %s pos: %s/%s @ %s,%s size: %sx%s db.size: %s"):format(
tostring(h:IsShown()), tostring(p), tostring(rp), tostring(x), tostring(y),
tostring(h:GetWidth()), tostring(h:GetHeight()),
tostring(WicksTravelFormDB and WicksTravelFormDB.size)))
end
if b then
print((" button shown: %s visible: %s macrotext1: %s"):format(
tostring(b:IsShown()), tostring(b:IsVisible()),
tostring(b:GetAttribute("macrotext1"))))
end
local k1, k2 = GetBindingKey("CLICK WicksTravelFormButton:LeftButton")
print((" bind keys: %s | %s"):format(tostring(k1), tostring(k2)))
local bh = _G["WicksTravelFormBarHost"]
if bh then
local bp, _, brp, bx, by = bh:GetPoint(1)
print((" barHost shown: %s size: %sx%s pos: %s/%s @ %s,%s"):format(
tostring(bh:IsShown()), tostring(bh:GetWidth()), tostring(bh:GetHeight()),
tostring(bp), tostring(brp), tostring(bx), tostring(by)))
print((" db.barSegH: %s db.barGap: %s db.barMargin: %s"):format(
tostring(WicksTravelFormDB and WicksTravelFormDB.barSegH),
tostring(WicksTravelFormDB and WicksTravelFormDB.barGap),
tostring(WicksTravelFormDB and WicksTravelFormDB.barMargin)))
local fi = GetShapeshiftForm()
local fname = fi > 0 and (GetShapeshiftFormInfo(fi)) or "none"
print((" mana: %s/%s power type: %s form: %s (#%s)"):format(
tostring(UnitPower("player",0)), tostring(UnitPowerMax("player",0)),
tostring(UnitPowerType("player")), tostring(fname), tostring(fi)))
-- force a relayout and report what the fills look like after
if ns.ApplyBarLayout then ns.ApplyBarLayout() end
local wi = WICKSTRAVELFORM
if wi and wi.priTrack then
print((" priTrack numPoints: %s priTrack h: %s"):format(
tostring(wi.priTrack:GetNumPoints()), tostring(wi.priTrack:GetHeight())))
print((" priFill numPoints: %s priFill w: %s"):format(
tostring(wi.priFill:GetNumPoints()), tostring(wi.priFill:GetWidth())))
else
print(" fill textures not exposed on ns (expected)")
end
else
print(" barHost: MISSING from _G")
end
return
elseif msg == "show" then
-- Force-build and show the button (in case PLAYER_LOGIN didn't fire as expected)
if ns.UI and ns.UI.Build then ns.UI:Build() end
if ns.UI and ns.UI.Refresh then ns.UI:Refresh() end
if _G.WicksTravelFormButton then
_G.WicksTravelFormButton:Show()
print("|cff8a5cf6Wick's Travel Form|r: forced show.")
else
print("|cff8a5cf6Wick's Travel Form|r: button still not built — check chat for Lua errors.")
end
return
elseif msg:match("^bar") then
local sub = msg:match("^bar%s+(.+)") or ""
local key, val = sub:match("^(%S+)%s+(%S+)")
local n = tonumber(val)
if key == "toggle" or key == "t" then
WicksTravelFormDB.barEnabled = not WicksTravelFormDB.barEnabled
if ns.ApplyBarLayout then ns.ApplyBarLayout() end
print("|cff8a5cf6Wick's Travel Form|r: attached bar " .. (WicksTravelFormDB.barEnabled and "enabled" or "disabled"))
return
elseif key == "float" or key == "f" then
WicksTravelFormDB.barFloat = not WicksTravelFormDB.barFloat
if ns.ApplyFloatBarLayout then ns.ApplyFloatBarLayout() end
print("|cff8a5cf6Wick's Travel Form|r: floating bar " .. (WicksTravelFormDB.barFloat and "enabled" or "disabled"))
return
elseif key == "width" or key == "w" then
local fw = tonumber(val)
if not fw or fw < 40 or fw > 600 then
print("|cff8a5cf6Wick's Travel Form|r: float width must be 40-600. Current: " .. (WicksTravelFormDB.barFloatW or 120))
return
end
WicksTravelFormDB.barFloatW = fw
if ns.ApplyFloatBarLayout then ns.ApplyFloatBarLayout() end
return
elseif key == "height" or key == "h" then
if not n or n < 2 or n > 20 then
print("|cff8a5cf6Wick's Travel Form|r: bar height must be 2-20. Current: " .. (WicksTravelFormDB.barSegH or 5))
return
end
WicksTravelFormDB.barSegH = n
if ns.ApplyBarLayout then ns.ApplyBarLayout() end
return
elseif key == "gap" or key == "g" then
if not n or n < 0 or n > 10 then
print("|cff8a5cf6Wick's Travel Form|r: bar gap must be 0-10. Current: " .. (WicksTravelFormDB.barGap or 2))
return
end
WicksTravelFormDB.barGap = n
if ns.ApplyBarLayout then ns.ApplyBarLayout() end
return
elseif key == "margin" or key == "m" then
if not n or n < 0 or n > 20 then
print("|cff8a5cf6Wick's Travel Form|r: bar margin must be 0-20. Current: " .. (WicksTravelFormDB.barMargin or 2))
return
end
WicksTravelFormDB.barMargin = n
if ns.ApplyBarLayout then ns.ApplyBarLayout() end
return
elseif key == "fheight" or key == "fh" then
if not n or n < 2 or n > 20 then
print("|cff8a5cf6Wick's Travel Form|r: float height must be 2-20. Current: " .. (WicksTravelFormDB.barFloatSegH or 5))
return
end
WicksTravelFormDB.barFloatSegH = n
if ns.ApplyFloatBarLayout then ns.ApplyFloatBarLayout() end
return
elseif key == "fgap" or key == "fg" then
if not n or n < 0 or n > 10 then
print("|cff8a5cf6Wick's Travel Form|r: float gap must be 0-10. Current: " .. (WicksTravelFormDB.barFloatGap or 2))
return
end
WicksTravelFormDB.barFloatGap = n
if ns.ApplyFloatBarLayout then ns.ApplyFloatBarLayout() end
return
else
print("|cff8a5cf6Wick's Travel Form|r bar options:")
print((" bar toggle — attached bar on/off (currently: %s)"):format(WicksTravelFormDB.barEnabled ~= false and "on" or "off"))
print((" bar height <2-20> — attached segment height (current: %d)"):format(WicksTravelFormDB.barSegH or 5))
print((" bar gap <0-10> — attached gap between segments (current: %d)"):format(WicksTravelFormDB.barGap or 2))
print((" bar margin <0-20> — attached space below button (current: %d)"):format(WicksTravelFormDB.barMargin or 2))
print((" bar float — floating bar on/off (currently: %s)"):format(WicksTravelFormDB.barFloat and "on" or "off"))
print((" bar width <40-600> — floating bar width (current: %d)"):format(WicksTravelFormDB.barFloatW or 120))
print((" bar fheight <2-20> — floating segment height (current: %d)"):format(WicksTravelFormDB.barFloatSegH or 5))
print((" bar fgap <0-10> — floating gap between segments (current: %d)"):format(WicksTravelFormDB.barFloatGap or 2))
return
end
return
end
print("|cff8a5cf6Wick's Travel Form|r commands: unlock | lock | reset | size <N> | bar | debug | show")
end
-- Friendly binding header / label
BINDING_HEADER_WICKSTRAVELFORM = "Wick's Travel Form"
_G["BINDING_NAME_CLICK WicksTravelFormButton:LeftButton"] = "Smart travel form"