-
Notifications
You must be signed in to change notification settings - Fork 25
/
Core.lua
554 lines (468 loc) · 15.8 KB
/
Core.lua
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
--[[
Author: Dennis Werner Garske (DWG)
License: MIT License
]]
-- Setup to wrap our stuff in a table so we don't pollute the global environment
local _G = _G or getfenv(0);
local Roids = _G.Roids or {};
_G.Roids = Roids;
Roids.Hooks = Roids.Hooks or {};
Roids.mouseoverUnit = Roids.mouseoverUnit or nil;
Roids.Extensions = Roids.Extensions or {};
-- Executes the given Macro's body
-- body: The Macro's body
function Roids.ExecuteMacroBody(body)
local lines = Roids.splitString(body, "\n");
for k,v in pairs(lines) do
ChatFrameEditBox:SetText(v);
ChatEdit_SendText(ChatFrameEditBox);
end
end
-- Gets the body of the Macro with the given name
-- name: The name of the Macro
-- returns: The body of the macro
function Roids.GetMacroBody(name)
local macroId = GetMacroIndexByName(name);
if not macroId then
return false;
end
local _,_, body = GetMacroInfo(macroId);
if not body and SuperMacroFrame ~= nil then
body = GetSuperMacroInfo(name, "body");
end
return body;
end
-- Attempts to execute a macro by the given name
-- name: The name of the macro
-- returns: Whether the macro was executed or not
function Roids.ExecuteMacroByName(name)
local body = Roids.GetMacroBody(name);
if not body then
return false;
end
Roids.ExecuteMacroBody(body);
return true;
end
-- Searches for a ':', '>' or '<' in the given word and returns its position
-- word: The word to search in
-- returns: The position of the delimeter or nil and 1 for '>' or 2 for '<'
function Roids.FindDelimeter(word)
local delimeter = string.find(word, ":");
local which = nil;
if not delimeter then
delimeter = string.find(word, ">");
which = 1;
if not delimeter then
delimeter = string.find(word, "<");
which = 0;
end
end
if not delimeter then
which = nil;
end
return delimeter, which;
end
-- Parses the given message and looks for any conditionals
-- msg: The message to parse
-- returns: A set of conditionals found inside the given string
function Roids.parseMsg(msg)
local modifier = "";
local modifierEnd = string.find(msg, "]");
local help = nil;
-- If we find conditionals trim down the message to everything except the conditionals
if string.sub(msg, 1, 1) == "[" and modifierEnd then
modifier = string.sub(msg, 2, modifierEnd - 1);
msg = string.sub(msg, modifierEnd + 1);
-- No conditionals found. Just return the message as is
elseif string.sub(msg, 1, 1) ~= "!" then
return msg;
end
local target;
local conditionals = {};
msg = Roids.Trim(msg)
if string.sub(msg, 1, 1) == "!" then
msg = string.sub(msg, 2);
conditionals.checkchanneled = msg;
end
local pattern = "(@?%w+:?>?<?%w*[_?%-?%w*]*[/?%w*]*)";
for w in string.gfind(modifier, pattern) do
local delimeter, which = Roids.FindDelimeter(w);
-- x:y
if delimeter then
local conditional = string.sub(w, 1, delimeter - 1);
if which then
conditionals[conditional] = { bigger = which, amount = string.sub(w, delimeter + 1) };
else
conditionals[conditional] = string.sub(w, delimeter + 1);
end
-- @target
elseif string.sub(w, 1, 1) == "@" then
conditionals["target"] = string.sub(w, 2);
-- Any other keyword like harm or help
elseif Roids.Keywords[w] ~= nil then
conditionals[w] = 1;
end
end
return msg, conditionals;
end
function Roids.SetHelp(conditionals)
if conditionals.help then
conditionals.help = 1;
elseif conditionals.harm then
conditionals.help = 0;
end
end
-- Fixes the conditionals' target by using the player's current target if it exists or falling back to the player itself if it doesn'target
-- conditionals: The conditionals containing the current target
-- returns: Whether or not we've changed the player's current target
function Roids.FixEmptyTarget(conditionals)
if not conditionals.target then
if UnitExists("target") then
conditionals.target = "target";
elseif GetCVar("autoSelfCast") == "1" then
conditionals.target = "player";
end
end
return false;
end
-- Fixes the conditionals' target by targeting the target with the given name
-- conditionals: The conditionals containing the current target
-- name: The name of the player to target
-- hook: The target hook
-- returns: Whether or not we've changed the player's current target
function Roids.FixEmptyTargetSetTarget(conditionals, name, hook)
if not conditionals.target then
hook(name);
conditionals.target = "target";
return true;
end
return false;
end
-- Returns the name of the focus target or nil
function Roids.GetFocusName()
if ClassicFocus_CurrentFocus then
return ClassicFocus_CurrentFocus;
elseif CURR_FOCUS_TARGET then
return CURR_FOCUS_TARGET;
end
return nil;
end
-- Attempts to target the focus target.
-- returns: Whether or not it succeeded
function Roids.TryTargetFocus()
local name = Roids.GetFocusName();
if not name then
return false;
end
Roids.Hooks.TARGET_SlashCmd(name);
return true;
end
-- Does the given action with a set of conditionals provided by the given msg
-- msg: The conditions followed by the action's parameters
-- hook: The hook of the function we've intercepted
-- fixEmptyTargetFunc: A function setting the player's target if the player has none. Required to return true if we need to re-target later or false if not
-- targetBeforeAction: A boolean value that determines whether or not we need to target the target given in the conditionals before performing the given action
-- action: A function that is being called when everything checks out
function Roids.DoWithConditionals(msg, hook, fixEmptyTargetFunc, targetBeforeAction, action)
local msg, conditionals = Roids.parseMsg(msg);
msg = Roids.Trim(msg);
-- No conditionals. Just exit.
if not conditionals then
if not msg then
return false;
else
if string.sub(msg, 1, 1) == "{" and string.sub(msg, -1) == "}" then
return Roids.ExecuteMacroByName(string.sub(msg, 2, -2));
end
if hook then
hook(msg);
end
return true;
end
end
if conditionals.target == "mouseover" then
if not UnitExists("mouseover") then
conditionals.target = Roids.mouseoverUnit;
end
if not conditionals.target or (conditionals.target ~= "focus" and not UnitExists(conditionals.target)) then
return false;
end
end
local needRetarget = false;
if fixEmptyTargetFunc then
needRetarget = fixEmptyTargetFunc(conditionals, msg, hook)
end
Roids.SetHelp(conditionals);
if conditionals.target == "focus" then
if UnitExists("target") and UnitName("target") == Roids.GetFocusName() then
conditionals.target = "target";
needRetarget = false;
else
if not Roids.TryTargetFocus() then
return false;
end
conditionals.target = "target";
needRetarget = true;
end
end
for k, v in pairs(conditionals) do
if not Roids.Keywords[k] or not Roids.Keywords[k](conditionals) then
if needRetarget then
TargetLastTarget();
needRetarget = false;
end
return false;
end
end
if conditionals.target ~= nil and targetBeforeAction then
if not UnitIsUnit("target", conditionals.target) then
needRetarget = true;
end
if SpellIsTargeting() then
SpellStopCasting()
end
TargetUnit(conditionals.target);
else
if needRetarget then
TargetLastTarget();
needRetarget = false;
end
end
local result = true;
if string.sub(msg, 1, 1) == "{" and string.sub(msg, -1) == "}" then
result = Roids.ExecuteMacroByName(string.sub(msg, 2, -2));
else
action(msg);
end
if needRetarget then
TargetLastTarget();
end
return result;
end
-- Attempts to cast a single spell from the given set of conditional spells
-- msg: The player's macro text
function Roids.DoCast(msg)
local handled = false;
msg = Roids.Trim(msg);
for k, v in pairs(Roids.splitString(msg, ";%s*")) do
if Roids.DoWithConditionals(v, Roids.Hooks.CAST_SlashCmd, Roids.FixEmptyTarget, true, CastSpellByName) then
handled = true; -- we parsed at least one command
break;
end
end
return handled;
end
-- Attempts to target a unit by its name using a set of conditionals
-- msg: The raw message intercepted from a /target command
function Roids.DoTarget(msg)
local handled = false;
local action = function(msg)
if string.sub(msg, 1, 1) == "@" then
msg = UnitName(string.sub(msg, 2));
end
Roids.Hooks.TARGET_SlashCmd(msg);
end
for k, v in pairs(Roids.splitString(msg, ";%s*")) do
if Roids.DoWithConditionals(v, Roids.Hooks.TARGET_SlashCmd, Roids.FixEmptyTargetSetTarget, false, action) then
handled = true;
break;
end
end
return handled;
end
-- Attempts to attack a unit by a set of conditionals
-- msg: The raw message intercepted from a /petattack command
function Roids.DoPetAttack(msg)
local handled = false;
for k, v in pairs(Roids.splitString(msg, ";%s*")) do
if Roids.DoWithConditionals(v, nil, Roids.FixEmptyTarget, true, PetAttack) then
handled = true;
break;
end
end
return handled;
end
-- Searches for the given itemName in the player's iventory
-- itemName: The name of the item to look for
-- returns: The bag number and the slot number if the item has been found. nil otherwhise
function Roids.FindItem(itemName)
RoidsTooltip:SetOwner(WorldFrame, "ANCHOR_NONE");
for i = 0, 4 do
for j = 1, GetContainerNumSlots(i) do
RoidsTooltip:ClearLines();
RoidsTooltip:SetBagItem(i, j);
if RoidsTooltipTextLeft1:GetText() == itemName then
return i, j;
end
end
end
for i = 0, 19 do
RoidsTooltip:ClearLines();
hasItem = RoidsTooltip:SetInventoryItem("player", i);
if hasItem and RoidsTooltipTextLeft1:GetText() == itemName then
return -i;
end
end
end
-- Attempts to use or equip an item from the player's inventory by a set of conditionals
-- msg: The raw message intercepted from a /use or /equip command
function Roids.DoUse(msg)
local handled = false;
local action = function(msg)
local bag, slot = Roids.FindItem(msg);
if bag and bag < 0 then
return UseInventoryItem(-bag);
end
if not bag or not slot then
return;
end
UseContainerItem(bag, slot);
end
for k, v in pairs(Roids.splitString(msg, ";%s*")) do
if Roids.DoWithConditionals(v, action, Roids.FixEmptyTarget, true, action) then
handled = true;
break;
end
end
return handled;
end
function Roids.DoEquipOffhand(msg)
local handled = false;
local action = function(msg)
local bag, slot = Roids.FindItem(msg);
if not bag or not slot then
return;
end
PickupContainerItem(bag, slot);
PickupInventoryItem(17);
end
for k, v in pairs(Roids.splitString(msg, ";%s*")) do
if Roids.DoWithConditionals(v, action, Roids.FixEmptyTarget, true, action) then
handled = true;
break;
end
end
return handled;
end
function Roids.DoUnshift(msg)
local handled;
local action = function(msg)
local currentShapeshiftIndex = Roids.GetCurrentShapeshiftIndex();
if currentShapeshiftIndex ~= 0 then
CastShapeshiftForm(currentShapeshiftIndex);
end
end
for k, v in pairs(Roids.splitString(msg, ";%s*")) do
handled = false;
if Roids.DoWithConditionals(v, action, Roids.FixEmptyTarget, true, action) then
handled = true;
break;
end
end
if handled == nil then
action();
end
return handled;
end
-- Holds information about the currently cast spell
Roids.CurrentSpell = {
-- "channeled" or "cast"
type = "",
-- the name of the spell
spellName = "",
-- is the Attack ability enabled
autoAttack = false,
-- is the Auto Shot ability enabled
autoShot = false,
-- is the Shoot ability (wands) enabled
wand = false,
};
-- Dummy Frame to hook ADDON_LOADED event in order to preserve compatiblity with other AddOns like SuperMacro
Roids.Frame = CreateFrame("FRAME");
Roids.Frame:RegisterEvent("ADDON_LOADED");
Roids.Frame:RegisterEvent("SPELLCAST_CHANNEL_START");
Roids.Frame:RegisterEvent("SPELLCAST_CHANNEL_STOP");
Roids.Frame:RegisterEvent("SPELLCAST_INTERRUPTED");
Roids.Frame:RegisterEvent("SPELLCAST_FAILED");
Roids.Frame:RegisterEvent("PLAYER_ENTER_COMBAT");
Roids.Frame:RegisterEvent("PLAYER_LEAVE_COMBAT");
Roids.Frame:RegisterEvent("START_AUTOREPEAT_SPELL");
Roids.Frame:RegisterEvent("STOP_AUTOREPEAT_SPELL");
Roids.Frame:SetScript("OnEvent", function()
Roids.Frame[event]();
end);
function Roids.Frame.ADDON_LOADED()
if event ~= "ADDON_LOADED" then
return;
end
if arg1 == "Roid-Macros" then
Roids.InitializeExtensions();
return;
end
if arg1 ~= "SuperMacro" then
return;
end
local hooks = {
cast = { action = Roids.DoCast, },
target = { action = Roids.DoTarget, },
use = { action = Roids.DoUse, },
};
-- Hook SuperMacro's RunLine to stay compatible
Roids.Hooks.RunLine = RunLine;
Roids.RunLine = function(...)
for i = 1, arg.n do
local intercepted = false;
local text = arg[i];
for k,v in pairs(hooks) do
local begin, _end = string.find(text, "^/"..k.."%s+[!%[]");
if begin then
local msg = string.sub(text, _end);
v.action(msg);
intercepted = true;
end
end
if not intercepted then
Roids.Hooks.RunLine(text);
end
end
end
RunLine = Roids.RunLine;
end
function Roids.Frame.SPELLCAST_CHANNEL_START()
Roids.CurrentSpell.type = "channeled";
end
function Roids.Frame.SPELLCAST_CHANNEL_STOP()
Roids.CurrentSpell.type = "";
Roids.CurrentSpell.spellName = "";
end
Roids.Frame.SPELLCAST_INTERRUPTED = Roids.Frame.SPELLCAST_CHANNEL_STOP;
Roids.Frame.SPELLCAST_FAILED = Roids.Frame.SPELLCAST_CHANNEL_STOP;
function Roids.Frame.PLAYER_ENTER_COMBAT()
Roids.CurrentSpell.autoAttack = true;
end
function Roids.Frame.PLAYER_LEAVE_COMBAT()
Roids.CurrentSpell.autoAttack = false;
end
function Roids.Frame.START_AUTOREPEAT_SPELL(...)
local _, className = UnitClass("player");
if className == "HUNTER" then
Roids.CurrentSpell.autoShot = true;
else
Roids.CurrentSpell.wand = true;
end
end
function Roids.Frame.STOP_AUTOREPEAT_SPELL(...)
local _, className = UnitClass("player");
if className == "HUNTER" then
Roids.CurrentSpell.autoShot = false;
else
Roids.CurrentSpell.wand = false;
end
end
Roids.Hooks.SendChatMessage = SendChatMessage;
function SendChatMessage(msg, ...)
if msg and string.find(msg, "^#showtooltip ") then
return;
end
Roids.Hooks.SendChatMessage(msg, unpack(arg));
end