My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
-- Tottler

-- See README.txt for an explanation of this Addon, how it relates to
-- the original TOTTler, and why these changes exist at all.



TOTTLER_MODE_AUTO = "auto"
TOTTLER_MODE_MAINTANK = "mt"
TOTTLER_MODE_MANUAL = "manual"
TOTTLER_MODE_ALL = "all"


TOTTLER_HP_RATIO_MIN = 1.1
TOTTLER_HP_RATIO_MAX = 3.0


-- when Tricks is cast, but before an attack is made, this buffs the
-- caster and lasts 30 seconds.
TOTTLER_ID_TRICKS_READY = 57934

-- after an attack is made, this buffs the caster, and is the buff we
-- cancel
TOTTLER_ID_TRICKS_THREAT = 59628

-- after an attack is made, this buffs the target and provides them
-- with a 15% damage bonus
TOTTLER_ID_TRICKS_BONUS = 57933


-- used to save and restore settings, but not used at runtime.
tottler_settings = nil


-- these are our actual settings. The default values are here, the
-- actual values are loaded in ADDON_LOADED if there are any
local tott_monitor = true
local tott_quiet = false
local tott_enabled = true
local tott_mode = TOTTLER_MODE_AUTO
local tott_exclusions = {}
local tott_hp_ratio = 1.3


-- filled with function references later
local mode_table = nil
local cli_functions = nil


-- this is storage for the target of the most previously cast Tricks
-- of the Trade
local tott_target = nil


-- this is the predicate function used to determine if tricks threat
-- should be canceled or not (returns true to cancel, false to not
-- cancel). This is set in the cli_mode and ADDON_LOADED functions.
local tott_mode_predicate = nil



-- Some output functions


local function message(...)
print("|cfffff000[tottler]|r " .. table.concat({...}, ""))
end


local function verbose(...)
if not tott_quiet then
print("|cfffff000[tottler]|r " .. table.concat({...}, ""))
end
end



-- Odds and ends


local function auto_mode_hp_threshold()

-- returns the HP threshold for auto mode.

return math.ceil(UnitHealthMax("player") * tott_hp_ratio)
end



local function number_convert(val)
-- this is just a simple helper function, intended to be called via
-- pcall in order to ensure that a value can be treated as a number

return val * 1
end



-- The predicate modes for determining whether to cancel tricks or not


local function mode_auto(toon)
-- if the target has less Max HP than the threshold, they don't get
-- threat. If they are above the threshold they're likely a tank

return UnitHealthMax(toon) < auto_mode_hp_threshold()
end



local function mode_manual(toon)
-- toons NOT in the exclusion list get their threat cancelled

return not tott_exclusions[toon]
end



local function mode_mt(toon)
-- main tanks (specified in the raid or from LFG) get their threat
-- cancelled

local n,instanceType = IsInInstance()

if instanceType == "raid" then
for i=1, GetNumRaidMembers() do
local noob,_ = UnitName("raid"..i)
if (noob == toon) then
-- if they aren't the MT, exclude them
return not GetPartyAssignment("MAINTANK", "raid"..i)
end
end

-- they aren't the raid's MT, so exclude them
return true

elseif instanceType == "party" then

-- we only do this when using LFG, because that's the only time
-- a party has a detectable MT role

local m, sm = GetLFGMode()
if m == "lfgparty" or m == "queued" then
for i=1, GetNumPartyMembers() do
local noob,_ = UnitName("party"..i)
if (noob == toon) then
-- if they aren't the MT, exclude them
return not UnitGroupRolesAssigned("party"..i)
end
end
end

-- they aren't the party's MT, so exclude them
return true
end
end



local function mode_all(toon)
-- everybody gets their threat cancelled!
return true
end



mode_table = {
[TOTTLER_MODE_AUTO] = mode_auto,
[TOTTLER_MODE_MANUAL] = mode_manual,
[TOTTLER_MODE_MAINTANK] = mode_mt,
[TOTTLER_MODE_ALL] = mode_all,
}



local function mode_names(sep)

-- utility function that returns a string of the known predicate
-- mode names. sep is a comma by default

sep = sep or ","

local modes = {}
for k in pairs(mode_table) do
table.insert(modes, k)
end

table.sort(modes)

return table.concat(modes, sep)
end



--- CLI functions



local function cli_add(toonName)

-- handles /tott add

if not toonName or toonName == "%t" then
toonName,_ = UnitName("target")
elseif toonName == "%f" then
toonName,_ = UnitName("focus")
end

tott_exclusions[toonName] = 1
message("Tricks threat transfer will not be cancelled for ", toonName)
end



local function cli_temporary(toonName)

-- handles /tott temporary

if not toonName or toonName == "%t" then
toonName,_ = UnitName("target")
elseif toonName == "%f" then
toonName,_ = UnitName("focus")
end

tott_exclusions[toonName] = 2
message("Tricks threat transfer will not be cancelled for ", toonName)
end



local function cli_remove(toonName)

-- handles /tott remove

if not toonName or toonName == "%t" then
toonName,_ = UnitName("target")
elseif toonName == "%f" then
toonName,_ = UnitName("focus")
end

tott_exclusions[toonName] = nil
message("Tricks threat transfer will be cancelled for ", toonName)
end



local function cli_check(toonName)

-- handles /tott check

if not toonName or toonName == "%t" then
toonName,_ = UnitName("target")
elseif toonName == "%f" then
toonName,_ = UnitName("focus")
end

if tott_mode_predicate(toonName) then
message(toonName, " will not receive threat from your Tricks")
else
message(toonName, " will receive threat from your Tricks")
end
end



local function cli_clear()

-- handles /tott clear

tott_exclusions = {}
message("Tricks threat list cleared")
end



local function cli_list()

-- handles /tott list

if next(tott_exclusions) then
local tmp = {}
for n,_ in pairs(tott_exclusions) do
table.insert(tmp, n)
end
table.sort(tmp)

tmp = table.concat(tmp, "\n")
message("The following toons will receive threat from Tricks\n", tmp)

else
message("All toons will have Tricks threat transfer cancelled.")
end
end



local function cli_status()

-- handles /tott status

local msg = {
"Addon currently " .. ((tott_enabled and "enabled") or "disabled"),
"Threat cancel mode is " .. tott_mode,
"Monitor mode is " .. ((tott_monitor and "enabled") or "disabled"),
"Quiet mode is " .. ((tott_quiet and "enabled") or "disabled"),
}

message(table.concat(msg, "\n"))
end



local function cli_enable()

-- handles /tott enable

tott_enabled = true
message("Addon enabled")
end



local function cli_disable()

-- handles /tott disable

tott_enabled = false
message("Addon disabled")
end



local function cli_toggle()

-- handles /tott toggle

if tott_enabled then
cli_disable()
else
cli_enable()
end
end



local function cli_mode(mode)

-- handles /tott mode

if not mode then
message("Current mode is ", tott_mode)

else
local pred = mode_table[mode]
if pred then
tott_mode = mode
tott_mode_predicate = pred
message("Mode changed to ", mode)

else
message("Invalide mode. Select one of ", mode_names(),
". Current mode is ", tott_mode)
end
end
end



local function cli_hp_ratio(value)

-- handles /tott ratio

if not value then
message("HP ratio for Auto mode is ", tott_hp_ratio,
" times max HP (ie: ", auto_mode_hp_threshold(), ")")

else
local ok,val = pcall(number_convert, value)

if ok and (val >= TOTTLER_HP_RATIO_MIN and
val <= TOTTLER_HP_RATIO_MAX) then

tott_hp_ratio = val
message("HP ratio for Auto mode set to ", val,
" times max HP (ie: ", auto_mode_hp_threshold(), ")")
else
message("HP ratio must be a number between ",
TOTTLER_HP_RATIO_MIN, " and ", TOTTLER_HP_RATIO_MAX)
end
end
end



local function cli_quiet(orly)

-- handles /tott quiet

if not orly or (orly == "toggle") then
tott_quiet = not tott_quiet
elseif orly == "on" then
tott_quiet = true
elseif orle == "off" then
tott_quiet = false
else
message("Need to specify one of on||off||toggle." ..
" Current quiet setting is ",
((tott_quiet and "on") or "off"))
return
end

message("Quiet mode is ", ((tott_quiet and "enabled") or "disabled"))
end



local function cli_monitor(orly)

-- handles /tott monitor

if not orly or (orly == "toggle") then
tott_monitor = not tott_monitor
elseif orly == "on" then
tott_monitor = true
elseif orly == "off" then
tott_monitor = false
else
message("Need to specify one of on||off||toggle." ..
" Current monitor setting is ",
((tott_monitor and "on") or "off"))
return
end

message("Incoming Tricks monitoring is ",
((tott_monitor and "enabled") or "disabled"))
end



local function cli_help(cmd)

-- handles /tott help

if cmd then
local cli = cli_functions[cmd]
if cli then
message("Usage is |cFF00FFFF/tott ", cmd, " ",
(cli.args or ""), "|r\n", (cli.help or ""))
else
message("Unknown command: ", cmd)
end

else

local cmds = {}
for k in pairs(cli_functions) do
table.insert(cmds, k)
end
table.sort(cmds)

message("Tottler is a simple tool for canceling the threat transfer",
" buff from Tricks of the Trade. The addon supports the",
" following commands.\n", table.concat(cmds, ", "), "\n",
"Use |cFF00FFFF/tott help COMMAND|r for help on a specific",
" command.")
end
end



cli_functions = {
add = {
fun = cli_add,
args = "[TOON]",
help = "Add toon to the exclusion list. Current target by default." },

temporary = {
fun = cli_temporary,
args = "[TOON]",
help = ("Temporarily add toon to the exclusion list. Current" ..
" target by default. Removed at logout.") },

remove = {
fun = cli_remove,
args = "[TOON]",
help = ("Remove toon from the exclusions list. Current target by" ..
" default.") },

check = {
fun = cli_check,
args = "[TOON]",
help = ("Tests whether a toon will receive Tricks threat or not." ..
" Current target by default.") },

clear = {
fun = cli_clear,
help = "Clears the exclusions list" },

list = {
fun = cli_list,
help = "Prints the exclusions list" },

status = {
fun = cli_status,
help = ("Prints the current buff canceling mode, status, and" ..
" verbosity. This is the default command if not" ..
" specified.") },

enable = {
fun = cli_enable,
help = "Enables buff canceling" },

disable = {
fun = cli_disable,
help = "Disables buff canceling" },

toggle = {
fun = cli_toggle,
help = "Toggles buff canceling" },

mode = {
fun = cli_mode,
args = "["..mode_names("||").."]",
help = ("Sets the threat cancelation mode, or prints the current" ..
" mode.") },

ratio = {
fun = cli_hp_ratio,
args = "[RATIO]",
help = "Shows or sets the HP ratio for auto mode" },

monitor = {
fun = cli_monitor,
args = "[on||off||toggle]",
help = "Sets or toggles Tricks damage buff monitoring" },

quiet = {
fun = cli_quiet,
args = "[on||off||toggle]",
help = "Sets or toggles event messages" },

help = {
fun = cli_help,
help = "Prints help messages" },
}



SLASH_TOTTLER1 = '/tottler';

SLASH_TOTTLER2 = '/tott';


function SlashCmdList.TOTTLER(msg)

local cmd, args = msg.split(' ', msg, 2);
if cmd == "" then cmd = "status" end

local cli = cli_functions[cmd]

if cli and cli.fun then
cli.fun(args and string.split(' ', args))
else
message("Unknown command: ", cmd)
end
end



local function buff_tricks_bonus(i,n,c,s)
if tott_monitor then
c,_ = UnitName(c)
message("Gained Tricks damange bonus from ", c)
end
end



local function buff_tricks_threat(i,n,c,s)
local noob = tott_target
if tott_mode_predicate(noob) then
verbose(noob, " will not receive threat.")
CancelUnitBuff("player", i)
else
verbose(noob, " will receive threat.")
end
end



-- table of functions to call when we gain a particular buff (by its
-- spell ID)

local buff_watch = {
[TOTTLER_ID_TRICKS_BONUS] = buff_tricks_bonus,
[TOTTLER_ID_TRICKS_THREAT] = buff_tricks_threat,
}



-- needed to eliminate duplicate buff triggers.
local buff_cache = {}



local function UNIT_AURA(unit_id)

-- This will watch for buffs on ourself with a spell id matching
-- one of the keys in buff_watch. If the buff is new (that is to
-- say, it's not in buff_cache already), it will trigger the
-- function in buff_watch, passing the buff index, name, caster,
-- and spell id.

if not tott_enabled then return end
-- I should just make it unregister these events

if unit_id ~= "player" then return end

local watch = buff_watch
local prev = buff_cache
local found = {}

local i = 1
while true do
local n, _, _, _, _, _, _, c, _, _, s = UnitBuff(unit_id, i)

if not s then
break

elseif watch[s] then
local cs = found[s]
if not cs then
found[s] = { [c] = 1 }
else
cs[c] = 1
end

if not prev[s] or not prev[s][c] then
watch[s](i,n,c,s)
end
end

i = i + 1
end

buff_cache = found
end



local function UNIT_SPELLCAST_SEND(caster, spell, rank, target)
if spell == "Tricks of the Trade" then
tott_target = target
end
end



local function ADDON_LOADED(name)
if name ~= "tottler" then return end

local t = tottler_settings

if t then
tott_monitor = t.tott_monitor
tott_quiet = t.tott_quiet
tott_enabled = t.tott_enabled
tott_mode = t.tott_mode
tott_exclusions = t.tott_exclusions
tott_hp_ratio = t.tott_hp_ratio
end

tott_mode_predicate = mode_table[tott_mode]
end



local function filter_exclusions(te)
local t = {}
for k,v in pairs(te) do
if v > 1 then
t[k] = v
end
end
return t
end



local function PLAYER_LOGOUT()
tottler_settings = {
tott_quiet = tott_quiet,
tott_monitor = tott_monitor,
tott_enabled = tott_enabled,
tott_mode = tott_mode,
tott_exclusions = filter_exclusions(tott_exclusions),
tott_hp_ratio = tott_hp_ratio }
end



-- map the event name to its callback. I just like to name the
-- function after the event.
local eventhandlers = {
UNIT_AURA = UNIT_AURA,
UNIT_SPELLCAST_SENT = UNIT_SPELLCAST_SEND,
ADDON_LOADED = ADDON_LOADED,
PLAYER_LOGOUT = PLAYER_LOGOUT,
}



-- create a frame and have it watch for events and call our handlers
local frame = CreateFrame("FRAME", "TOTTlerEventFrame")
frame:SetScript("OnEvent", function(f,e,...) eventhandlers[e](...) end)
for event in pairs(eventhandlers) do
frame:RegisterEvent(event)
end



-- The end.

Change log

r172 by obriencj on Aug 25, 2010   Diff
sigh
Go to: 
Project members, sign in to write a code review

Older revisions

r171 by obriencj on Aug 25, 2010   Diff
clean this mess up
r166 by obriencj on Aug 25, 2010   Diff
let's only do this once, eh?
r163 by obriencj on Aug 19, 2010   Diff
some explanations for how and what and
why
All revisions of this file

File info

Size: 15216 bytes, 722 lines

File properties

svn:executable
*
Powered by Google Project Hosting