-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
1111 lines (998 loc) · 32.3 KB
/
main.lua
File metadata and controls
1111 lines (998 loc) · 32.3 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
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--=========================================================================================
-- Main module for SwedgeTimer
--=========================================================================================
local addon_name, st = ...
local version = "@project-version@"
-- local version = "2.0"
local ST = LibStub("AceAddon-3.0"):NewAddon(addon_name, "AceConsole-3.0", "AceEvent-3.0")
local LSM = LibStub("LibSharedMedia-3.0")
local STL = LibStub("LibClassicSwingTimerAPI", true)
local LRC = LibStub("LibRangeCheck-2.0")
local LLM = LibStub("LibLatencyMonitor")
local LGC = LibStub("LibGlobalCooldown")
local LWIN = LibStub("LibWindow-1.1")
local print = st.utils.print_msg
ST.DEATHKNIGHT = {}
ST.DRUID = {}
ST.HUNTER = {}
ST.MAGE = {}
ST.PALADIN = {}
ST.PRIEST = {}
ST.ROGUE = {}
ST.SHAMAN = {}
ST.WARLOCK = {}
ST.WARRIOR = {}
--=========================================================================================
-- Funcs/iterables to automate common tasks
--=========================================================================================
local SwingTimerInfo = function(hand)
return STL:SwingTimerInfo(hand)
end
function table.pack(...)
return { n = select("#", ...), ... }
end
ST.interfaces_are_initialised = false
-- keyword-accessible tables to handle case-switching
ST.mainhand = {}
ST.offhand = {}
ST.ranged = {}
ST.class_hands = {
DEATHKNIGHT = {"mainhand", "offhand"},
DRUID = {"mainhand"},
HUNTER = {"mainhand", "offhand", "ranged"},
MAGE = {"mainhand", "ranged"},
PALADIN = {"mainhand"},
PRIEST = {"mainhand", "ranged"},
ROGUE = {"mainhand", "offhand", "ranged"},
SHAMAN = {"mainhand", "offhand"},
WARLOCK = {"mainhand", "ranged"},
WARRIOR = {"mainhand", "offhand", "ranged"},
}
function ST:iter_hands()
-- Iterates over melee/ranged/offhand
local i = 0
local hands = self.class_hands[self.player_class]
local n = #hands
return function ()
i = i + 1
while i <= n do
return hands[i]
end
return nil
end
end
function ST:generic_iter(array)
local i = 0
local n = #array
return function()
i = i + 1
while i <= n do
return array[i]
end
end
end
function ST:is_value_in_array(value, array)
for _, v in pairs(array) do
if v == value then
return true
end
end
return false
end
function ST:convert_color(t, new_alpha)
-- Convert standard 0-255 colors down to WoW 0-1 ranges.
local r,g,b,a = unpack(t)
a = new_alpha or a
r = r/255
g = g/255
b = b/255
return r, g, b, a
end
function ST:convert_color_up(t, new_alpha)
-- Convert WoW 0-1 ranges up to standard 0-255 ranges.
local r,g,b,a = unpack(t)
a = new_alpha or a
r = r * 255
g = g * 255
b = b * 255
return r, g, b, a
end
function ST:get_anchor_frame(hand)
-- Gets the main anchor frame for the hand.
return self[hand].anchor_frame
end
function ST:get_hiding_anchor_frame(hand)
-- Get the hiding anchor frame, which also gets calls to show/hide.
return self:get_anchor_frame(hand).hiding_anchor_frame
end
function ST:get_bar_frame(hand)
-- Gets the bar frame for the hand.
-- This is the frame with the backdrop, but *not* the frame
-- with the bar visuals - that's a child of this one.
return self:get_anchor_frame(hand).bar_frame
end
function ST:get_visuals_frame(hand)
return self:get_bar_frame(hand).visuals_frame
end
function ST:get_profile_table()
return self.db.profile
end
function ST:get_class_table()
return self.db.profile[self.player_class]
end
function ST:get_hand_table(hand)
return self.db.profile[self.player_class][hand]
end
function ST:get_in_range(hand)
if hand == "ranged" then return self.in_ranged_range else return self.in_melee_range end
end
function ST:hide_bar(hand)
self:get_bar_frame(hand):Hide()
self:get_hiding_anchor_frame(hand):Hide()
end
function ST:check_bar_enable_settings(db_class, hand)
-- Function to check if the settings indicate the bar should be shown or hidden.
if not db_class.class_enabled then -- Global enable/disable
return false
end
if not self:bar_is_enabled(hand) then -- Hand enabled
return false
end
if self.current_talent_group == 1 then -- Spec 1 enabled
if not db_class.enable_spec1 then
return false
end
end
if self.current_talent_group == 2 then -- Spec 1 enabled
if not db_class.enable_spec2 then
return false
end
end
return true -- If we get to the end the bar should be shown.
end
function ST:show_bar(hand)
local db_class = self:get_class_table()
if self:check_bar_enable_settings(db_class, hand) then
if self.player_class == "DRUID" then
if self.should_show_bar_this_form then
self:get_bar_frame(hand):Show()
self:get_hiding_anchor_frame(hand):Show()
else
self:hide_bar(hand)
end
elseif self.player_class == "WARRIOR" then
if db_class.hide_in_tank_spec and self.has_imp_def_stance then
self:hide_bar(hand)
elseif db_class.hide_in_arms_spec and self.has_bladestorm then
self:hide_bar(hand)
else
self:get_bar_frame(hand):Show()
self:get_hiding_anchor_frame(hand):Show()
end
else
self:get_bar_frame(hand):Show()
self:get_hiding_anchor_frame(hand):Show()
end
else
self:hide_bar(hand)
end
end
function ST:lock_frames()
for hand in self:iter_hands() do
local f = self:get_anchor_frame(hand)
f:SetMovable(false)
f:EnableMouse(false)
f:SetScript("OnMouseWheel", nil)
end
end
function ST:unlock_frames()
for hand in self:iter_hands() do
local f = self:get_anchor_frame(hand)
f:SetMovable(true)
f:EnableMouse(true)
f:SetScript("OnMouseWheel", function(_, dir)
LWIN.OnMouseWheel(f, dir)
local ACR = LibStub("AceConfigRegistry-3.0")
ACR:NotifyChange(ST.options_table_name)
end
)
end
end
--=========================================================================================
-- Funcs to initialise the addon
--=========================================================================================
function ST:OnInitialize()
if st.is_version_supported == false then
self:Print("WARNING: SwedgeTimer does not support this version of World of Warcraft!"
.." You may experience malfunctions")
end
-- Get player class first
self.player_class_pretty, self.player_class = UnitClass("player")
-- Register our media
LSM:Register(
"border", "Square Full White", [[Interface\BUTTONS\WHITE8X8]]
)
LSM:Register(
"font", "Expressway", [[Interface\Addons\SwedgeTimer\Media\Fonts\Expressway.ttf]]
)
LSM:Register(
"statusbar", "Minimalist", [[Interface\Addons\SwedgeTimer\Media\Textures\Minimalist.tga]]
)
-- Addon database
local SwedgeTimerDB = LibStub("AceDB-3.0"):New(addon_name.."DB", self.defaults, true)
self.db = SwedgeTimerDB
-----------------------------------------------------------
-- Dynamically construct the options tables
-----------------------------------------------------------
local AC = LibStub("AceConfig-3.0")
local ACD = LibStub("AceConfigDialog-3.0")
self:set_opts()
self.options_table_name = addon_name.."_Options"
AC:RegisterOptionsTable(self.options_table_name, self.opts_table)
self.optionsFrame = ACD:AddToBlizOptions(self.options_table_name, addon_name)
-- Profile options
local profiles = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
AC:RegisterOptionsTable(addon_name.."_Profiles", profiles)
ACD:AddToBlizOptions(addon_name.."_Profiles", "Profiles", addon_name)
-----------------------------------------------------------
-- Callbackhandlers for letting the addon know that dependent
-- libraries have their interfaces enabled.
-----------------------------------------------------------
-- Init our lib interfaces only once the range and swing timer
-- libs are both loaded, as they are interdependent
-- init_libs has a check to ensure this only happens once per reload
self.lrc_ready = false
self.stl_ready = false
self.llm_ready = false
LRC.RegisterCallback(self, LRC.CHECKERS_CHANGED, function()
self.lrc_ready = true
self:init_libs()
end
)
STL.RegisterCallback(self, "SWING_TIMER_INFO_INITIALIZED", function()
self.stl_ready = true
self:init_libs()
end
)
-----------------------------------------------------------
-- Callbackhandlers that are less sensitive to load orders.
-----------------------------------------------------------
-- LibLatencyMonitor
LLM.RegisterCallback(self, LLM.LATENCY_CHANGED, self.callback_event_handler)
-- LibGlobalCooldown
LGC.RegisterCallback(self, LGC.GCD_STARTED, self.callback_event_handler)
LGC.RegisterCallback(self, LGC.GCD_OVER, self.callback_event_handler)
LGC.RegisterCallback(self, LGC.GCD_DURATIONS_UPDATED, self.callback_event_handler)
-----------------------------------------------------------
-- The set of containers and frames used in the addon.
-- Even though many are nilled, this collates them so
-- we have a reference to each container visually
-- in the code.
-----------------------------------------------------------
-- Character info containers
self.player_guid = UnitGUID("player")
self.has_oh = false
self.has_ranged = false
self.mh_timer = 0
self.oh_timer = 0
self.ranged_timer = 0
-- Character state containers
self.in_combat = false
self.is_melee_attacking = false
self.has_target = false
self.has_attackable_target = false
self.current_talent_group = GetActiveTalentGroup()
self.form_index = GetShapeshiftForm()
self.is_cat_or_bear = false
if self.player_class == "DRUID" then
if self.form_index == 1 or self.form_index == 3 then
self.is_cat_or_bear = true
end
end
self.should_show_bar_this_form = true
-- MH timer containers
self.mainhand.start = nil
self.mainhand.speed = nil
self.mainhand.ends_at = nil
self.mainhand.inactive_timer = nil
self.mainhand.has_weapon = true -- always true
self.mainhand.is_full = true -- deliberately true
self.mainhand.is_full_timer = nil
self.mainhand.is_paused = false
self.mainhand.current_progress = false
-- OH containers
self.offhand.start = nil
self.offhand.speed = nil
self.offhand.ends_at = nil
self.offhand.inactive_timer = nil
self.offhand.has_weapon = false
self.offhand.is_full = false
self.offhand.is_full_timer = nil
self.offhand.is_paused = false
-- ranged containers
self.ranged.start = nil
self.ranged.speed = nil
self.ranged.ends_at = nil
self.ranged.inactive_timer = nil
self.ranged.has_weapon = false
self.ranged.is_full = false
-- GCD info containers
self.gcd = {}
self.gcd.duration = nil
self.gcd.started = nil
self.gcd.expires = nil
self.gcd.phys_length = nil
self.gcd.spell_length = nil
self.gcd1_phys_time_before_swing = nil
self.gcd1_spell_time_before_swing = nil
self.gcd1_marker_position = nil
-- self.gcd2_phys_time_before_swing = nil
-- self.gcd2_spell_time_before_swing = nil
-- self.gcd2_marker_position = nil
-- Latency info containers
self.latency = {}
self.latency.update_interval_s = 1
self.latency.home_ms = 0
self.latency.calibrated_home_ms = 0
self.latency.world_ms = 0
self.latency.calibrated_world_ms = 0
-----------------------------------------------------------
-- Register WoW API events
-----------------------------------------------------------
self:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
self:RegisterEvent("CHARACTER_POINTS_CHANGED")
self:RegisterEvent("CURRENT_SPELL_CAST_CHANGED")
self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
self:RegisterEvent("PLAYER_ENTERING_WORLD")
self:RegisterEvent("PLAYER_REGEN_ENABLED")
self:RegisterEvent("PLAYER_TARGET_SET_ATTACKING")
self:RegisterEvent("PLAYER_REGEN_ENABLED")
self:RegisterEvent("PLAYER_REGEN_DISABLED")
self:RegisterEvent("PLAYER_ENTER_COMBAT")
self:RegisterEvent("PLAYER_LEAVE_COMBAT")
self:RegisterEvent("UNIT_AURA")
self:RegisterEvent("UNIT_POWER_FREQUENT")
self:RegisterEvent("UNIT_SPELLCAST_FAILED_QUIET")
self:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
self:RegisterEvent("UNIT_TARGET")
self:RegisterEvent("UPDATE_SHAPESHIFT_FORM")
-----------------------------------------------------------
-- Register Slashcommands
-----------------------------------------------------------
self:register_slashcommands()
end
function ST:init_libs()
-- This function inits our interfaces to the libraries we use,
-- but only once those libraries are fully ready and have
-- all of the information necessary.
if self.interfaces_are_initialised then
return
end
if not self.lrc_ready and self.stl_ready then
return
end
self.interfaces_are_initialised = true
self:init_timers()
self:init_range_finders()
self:post_init()
-- If requested, print a welcome message once everything is
-- initialised properly.
local db = self:get_profile_table()
if db.welcome_message then
self:Print("Version " .. version .. " loaded!")
end
end
function ST:init_timers()
self:register_timer_callbacks()
self:check_weapons()
for hand in self:iter_hands() do
local t = {SwingTimerInfo(hand)}
-- If the player has a shield, the below check prevents zero
-- division errors in the offhand timer bar initialisation.
if t[1] == 0 then
t[1] = 0.1
end
self[hand].speed = t[1]
self[hand].ends_at = t[2]
self[hand].start = t[3]
ST:init_visuals_template(hand)
ST:set_bar_texts(hand)
-- hook the onupdate
self:get_bar_frame(hand):SetScript("OnUpdate", self[hand].onupdate)
end
end
function ST:register_timer_callbacks()
STL.RegisterCallback(self, "SWING_TIMER_CLIPPED", self.callback_event_handler)
STL.RegisterCallback(self, "SWING_TIMER_DELTA", self.callback_event_handler)
STL.RegisterCallback(self, "SWING_TIMER_PAUSED", self.callback_event_handler)
STL.RegisterCallback(self, "SWING_TIMER_START", self.callback_event_handler)
STL.RegisterCallback(self, "SWING_TIMER_STOP", self.callback_event_handler)
STL.RegisterCallback(self, "SWING_TIMER_UPDATE", self.callback_event_handler)
end
function ST:check_weapons()
-- Detect what weapon types are equipped.
-- Before we used the LibClassicSwinngTimer function to get the attack speeds.
-- This lib used the same events that this function is triggered on internally,
-- which led to inconsistencies depending on callback order. Using the basic
-- WoW API to make these checks fixes the issue.
local _, speed_offhand = UnitAttackSpeed("player")
speed_offhand = speed_offhand or 0
local speed_ranged = UnitRangedDamage("player") or 0
if speed_offhand == 0 then
self.offhand.has_weapon = false
else
self.offhand.has_weapon = true
end
if speed_ranged == 0 then
self.ranged.has_weapon = false
else
self.ranged.has_weapon = true
end
end
function ST:post_init()
-- Takes care of any miscellaneous widget/state manipulation that needs to run
-- once the libraries and addon are initialised.
self:set_gcd_times_before_swing_seconds()
self:on_latency_update()
-- self:get_druid_talent_info()
self:get_talent_information()
self:determine_form_visibility_flag()
for hand in self:iter_hands() do
self:set_gcd_marker_positions(hand)
-- self:set_bar_full_state(hand)
self:on_bar_inactive(hand)
self[hand].is_full = true
end
if self[self.player_class].post_init then
self[self.player_class].post_init(self)
end
-- Set frames to locked or unlocked based on settings.
local db_class = self:get_class_table()
if db_class.timers_locked then
self:lock_frames()
else
self:unlock_frames()
end
for hand in self:iter_hands() do
self:show_bar(hand)
end
end
--=========================================================================================
-- CallbackHandlers
--=========================================================================================
function ST.callback_event_handler(event, ...)
-- Func to pass all callbacks to their relevant handler
-- if string.find(event, "GCD") then
-- print('<===============')
-- print(event)
-- local args = table.pack(...)
-- for i=1, args.n do
-- print(tostring(args[i]))
-- end
-- print('--------------->')
-- end
ST[event](ST, event, ...)
end
-----------------------------------------------------------
-- GCD lib funcs
-----------------------------------------------------------
function ST:GCD_STARTED(_, duration, expires)
-- print(duration)
-- print(expires)
self.gcd.expires = expires
end
function ST:GCD_OVER()
self.gcd.expires = nil
for hand in self:iter_hands() do
self:get_visuals_frame(hand).gcd_bar:Hide()
end
end
function ST:GCD_DURATIONS_UPDATED(_, phys_length, spell_length)
self.gcd.spell_length = spell_length
self.gcd.phys_length = phys_length
self:set_gcd_times_before_swing_seconds()
if self.interfaces_are_initialised then
self:on_gcd_length_change()
end
end
-----------------------------------------------------------
-- Latency tracking
-----------------------------------------------------------
function ST:LATENCY_CHANGED(_, home, world)
self.latency.home_ms = home
self.latency.world_ms = world
self:set_adjusted_latencies()
if self.interfaces_are_initialised then
self:set_gcd_times_before_swing_seconds()
self:on_latency_update()
end
end
-----------------------------------------------------------
-- Swing Timer Lib
-----------------------------------------------------------
function ST:SWING_TIMER_CLIPPED(_, hand)
end
function ST:SWING_TIMER_DELTA(_, delta)
-- print(string.format("DELTA = %s", delta))
end
function ST:SWING_TIMER_PAUSED(_, hand)
-- print(hand)
self[hand].is_paused = true
end
function ST:SWING_TIMER_START(_, speed, expiration_time, hand)
-- if not ST.interfaces_are_initialised then return end
if self[hand].is_full_timer then
self[hand].is_full_timer:Cancel()
self[hand].is_full = false
self:on_bar_active(hand)
elseif self[hand].is_full then
self[hand].is_full = false
self:on_bar_active(hand)
end
self[hand].is_full = false
self[hand].is_paused = false
self[hand].start = GetTime()
self[hand].speed = speed
self[hand].ends_at = expiration_time
self:set_bar_color(hand)
if self.recent_form_change then
self:on_gcd_length_change()
self.recent_form_change = false
end
end
function ST:SWING_TIMER_STOP(_, hand)
local db_shared = self.db.profile
self[hand].is_full_timer = C_Timer.NewTimer(db_shared.bar_full_delay, function()
-- self:set_bar_full_state(hand)
-- print("C_Timer expired")
self[hand].is_full = true
self:on_bar_inactive(hand)
end)
if self.player_class == "DRUID" then
self:on_attack_speed_change(hand)
end
end
function ST:SWING_TIMER_UPDATE(_, speed, expiration_time, hand)
local t = GetTime()
if expiration_time < t then
expiration_time = t
end
self[hand].is_paused = false
self[hand].speed = speed
self[hand].start = expiration_time - speed
self[hand].ends_at = expiration_time
-- print(string.format("%f : %f", t, expiration_time))
if t >= expiration_time then
self[hand].is_full = true
self:on_bar_inactive(hand)
else
self[hand].is_full = false
end
-- print('New speed = ' .. tostring(speed))
self:on_attack_speed_change(hand)
end
------------------------------------------------------------------------------------
-- Wow API event callbacks
------------------------------------------------------------------------------------
function ST:CURRENT_SPELL_CAST_CHANGED(event, is_cancelled)
self:class_on_current_spell_cast_changed(is_cancelled)
end
function ST:PLAYER_ENTERING_WORLD(event, is_initial_login, is_reloading_ui)
end
function ST:PLAYER_EQUIPMENT_CHANGED(event, slot, has_current)
if slot == 16 or slot == 17 or slot == 18 then
self:check_weapons()
for hand in self:iter_hands() do
self:on_attack_speed_change(hand)
end
end
end
function ST:PLAYER_REGEN_ENABLED()
self.in_combat = false
end
function ST:PLAYER_REGEN_DISABLED()
self.in_combat = true
end
function ST:PLAYER_ENTER_COMBAT()
self.is_melee_attacking = true
end
function ST:PLAYER_LEAVE_COMBAT()
self.is_melee_attacking = false
end
function ST:PLAYER_TARGET_SET_ATTACKING()
-- Required to set the offhand timer delay when startattack or when
-- target is changed (both fire this event).
if not self.has_oh then return end
local t = GetTime()
local old_start = self.offhand.start
if old_start + self.offhand.speed < t then
self:set_bar_color('offhand')
self.offhand.start = GetTime() - self.offhand.speed
end
end
function ST:UNIT_AURA(event, unit_id, is_full_update, updated_auras)
if unit_id ~= "player" or (not self.interfaces_are_initialised) then
return
end
self:class_on_aura_change()
end
function ST:UNIT_POWER_FREQUENT(event, unit_id, powerType)
-- Used to track if a warrior or druid has enough rage to cast
-- any currently queued on-next-cast ability
if unit_id ~= "player" or (not self.interfaces_are_initialised) then
return
end
if powerType == "RAGE" then
if self[self.player_class].on_rage_update then
self[self.player_class].on_rage_update(self)
end
end
end
function ST:UNIT_SPELLCAST_FAILED_QUIET(event, unit_id, cast_guid, spell_id)
if unit_id ~= "player" or (not self.interfaces_are_initialised) then
return
end
-- Trigger any class-specific behaviour
if self[self.player_class].on_spellcast_failed_quiet then
self[self.player_class].on_spellcast_failed_quiet(self, unit_id, cast_guid, spell_id)
end
end
function ST:UNIT_SPELLCAST_SUCCEEDED(event, unit_id, cast_guid, spell_id)
if unit_id ~= "player" then
return
end
-- Trigger any class-specific behaviour
if self[self.player_class].on_spellcast_succeeded then
self[self.player_class].on_spellcast_succeeded(self, unit_id, cast_guid, spell_id)
end
end
function ST:UNIT_TARGET(event, unit_id)
if unit_id ~= "player" then
return
end
if UnitExists("target") then self.has_target = true else self.has_target = false end
if UnitCanAttack("player", "target") == true then
self.has_attackable_target = true
else
self.has_attackable_target = false
end
end
function ST:UPDATE_SHAPESHIFT_FORM()
-- At present, only druids care about shapeshifting, so the code
-- to handle their forms goes here.
if not self.class == "DRUID" then
return
end
local i = GetShapeshiftForm()
-- If form hasn't changed, do nothing.
if i == self.form_index then
return
end
self.form_index = i
self:determine_form_visibility_flag()
if i == 1 or i ==3 then
self.is_cat_or_bear = true
else
self.is_cat_or_bear = false
end
-- Set a flag to ensure a recalculation of bar elements at the next swing
-- in case of any snapshotting
self.recent_form_change = true
if self.interfaces_are_initialised then
self:on_attack_speed_change("mainhand")
self:set_bar_color("mainhand")
end
if self.interfaces_are_initialised then
self:handle_bar_visibility("mainhand")
end
end
function ST:determine_form_visibility_flag()
-- function called on form or settings change to
-- set the form visibility flag
local i = GetShapeshiftForm()
local db = self:get_class_table()
self.should_show_bar_this_form = false
if i == 0 and db.form_vis_normal then
self.should_show_bar_this_form = true
elseif i == 1 and db.form_vis_bear then
self.should_show_bar_this_form = true
elseif i == 4 and db.form_vis_travel then
self.should_show_bar_this_form = true
elseif i == 3 and db.form_vis_cat then
self.should_show_bar_this_form = true
elseif i == 5 and db.form_vis_moonkin and self.has_moonkin then
self.should_show_bar_this_form = true
elseif i == 5 and db.form_vis_tree and self.has_tree_of_life then
self.should_show_bar_this_form = true
elseif i == 5 and (not self.has_moonkin and not self.has_tree_of_life) and db.form_vis_flight then
self.should_show_bar_this_form = true
elseif i == 6 and db.form_vis_flight then
self.should_show_bar_this_form = true
end
end
------------------------------------------------------------------------------------
-- Talent information.
------------------------------------------------------------------------------------
function ST:ACTIVE_TALENT_GROUP_CHANGED(event, group)
self.current_talent_group = group
if self.player_class == "DRUID" then
self:get_druid_talent_info()
self:determine_form_visibility_flag()
elseif self.player_class == "WARRIOR" then
self:get_warrior_talent_info()
end
end
function ST:CHARACTER_POINTS_CHANGED(event)
if self.player_class == "DRUID" then
self:get_druid_talent_info()
self:determine_form_visibility_flag()
elseif self.player_class == "WARRIOR" then
self:get_warrior_talent_info()
end
end
function ST:get_talent_information()
self:get_druid_talent_info()
self:get_warrior_talent_info()
end
function ST:get_druid_talent_info()
-- function to get the druid talent info and determine
-- if the player has moonkin or tree
self.has_moonkin = select(5, GetTalentInfo(1, 11)) == 1
self.has_tree_of_life = select(5, GetTalentInfo(3, 19)) == 1
end
function ST:get_warrior_talent_info()
-- Functions to get talents for specs that the user may want to hide
-- the swing timer in.
self.has_devastate = select(5, GetTalentInfo(3, 20)) == 1
self.has_imp_def_stance = select(5, GetTalentInfo(3, 16)) ~= 0
self.has_bladestorm = select(5, GetTalentInfo(1, 27)) == 1
end
------------------------------------------------------------------------------------
-- Latency and GCD markers
------------------------------------------------------------------------------------
function ST:set_adjusted_latencies()
-- Set the calibrated latencies
local db = self:get_profile_table()
local home = (self.latency.home_ms * db.latency_scale_factor) + db.latency_linear_offset
self.latency.calibrated_home_ms = home
local world = (self.latency.world_ms * db.latency_scale_factor) + db.latency_linear_offset
self.latency.calibrated_world_ms = world
end
function ST:get_gcd_marker_time_offset_seconds()
-- Gets the time offset in seconds according to the settings.
local db = self:get_profile_table()
local offset = 0
if db.gcd_marker_offset_mode == "Dynamic" then
offset = self.latency.world_ms
elseif db.gcd_marker_offset_mode == "Calibrated" then
offset = self.latency.calibrated_world_ms
elseif db.gcd_marker_offset_mode == "Fixed" then
offset = db.latency_linear_offset
end
offset = offset / 1000
return offset
end
function ST:set_gcd_times_before_swing_seconds()
-- Set all of the calculated times before our next swing.
local base_phys = self.gcd.phys_length
local base_spell = self.gcd.spell_length
local offset = self:get_gcd_marker_time_offset_seconds()
self.gcd.gcd1_phys_time_before_swing = base_phys + offset
self.gcd.gcd1_spell_time_before_swing = base_spell + offset
self.gcd.gcd2_phys_time_before_swing = (2 * base_phys) + offset
self.gcd.gcd2_spell_time_before_swing = (2 * base_spell) + offset
end
------------------------------------------------------------------------------------
-- Range finding
------------------------------------------------------------------------------------
function ST:init_range_finders()
self.rangefinder_interval = 0.05
self.melee_range_checker_func = LRC:GetHarmMaxChecker(LRC.MeleeRange)
local r = 30
if self.player_class == "HUNTER" then
r = 35
end
self.ranged_range_checker_func = LRC:GetHarmMaxChecker(r)
self.in_melee_range = nil
self.in_ranged_range = nil
self.target_min_range = nil
self.target_max_range = nil
self:rf_update()
end
function ST:rf_update()
-- On the very first load of the game, CHECKERS_CHANGED can get a callback
-- before the checker funcs are ready. If that happens, loop a C_Timer
-- regardless and re-assign the functions until we get valid checkers,
-- then resume normal function.
if self.melee_range_checker_func == nil then
-- self:Print("Range checker offline")
C_Timer.After(self.rangefinder_interval, function()
self.melee_range_checker_func = LRC:GetHarmMaxChecker(LRC.MeleeRange)
local r = 30
if self.player_class == "HUNTER" then
r = 35
end
self.ranged_range_checker_func = LRC:GetHarmMaxChecker(r)
self:rf_update()
self:set_bar_visibilities()
end
)
return
end
self.in_melee_range = self.melee_range_checker_func("target")
-- print(self.melee_result)
self.in_ranged_range = self.ranged_range_checker_func("target") and not self.in_melee_range
self.target_min_range, self.target_max_range = LRC:GetRange("target")
-- print('minrange = '..tostring(self.target_min_range))
-- print(self.target_max_range)
self:set_bar_visibilities()
self:handle_oor()
C_Timer.After(self.rangefinder_interval, function() self:rf_update() end)
end
------------------------------------------------------------------------------------
-- Bar out-of-range behaviour
------------------------------------------------------------------------------------
function ST:handle_oor()
for hand in self:iter_hands() do
if self:bar_is_enabled(hand) then
self:handle_oor_hand(hand)
end
end
end
function ST:handle_oor_hand(hand)
local db = self:get_hand_table(hand)
local frame = self:get_bar_frame(hand)
-- self:Print(db.dim_oor)
if db.dim_oor then
-- if hand == "offhand" then
-- -- self:Print(self:get_in_range(hand))
-- end
if not self:get_in_range(hand) then
frame:SetAlpha(db.dim_alpha)
else
frame:SetAlpha(1.0)
end
else
frame:SetAlpha(1.0)
end
end
------------------------------------------------------------------------------------
-- Bar visibility
------------------------------------------------------------------------------------
function ST:bar_is_enabled(hand)
local db = self:get_hand_table(hand)
if hand == "mainhand" then -- always has weapon
if db.enabled then
return true
else
return false
end
end
if db.enabled and self[hand].has_weapon then
return true
else
return false
end
end
function ST:handle_bar_visibility(hand)
-- Determines if the bar should be shown or not.
local db = self:get_hand_table(hand)
if db.show_behaviour == "always" then
self:show_bar(hand)
return
end
if db.show_condition == "in_combat" then
if self.in_combat then
self:show_bar(hand)
else
self:hide_bar(hand)
end
elseif db.show_condition == "has_target" then
if not self.has_attackable_target then
self:hide_bar(hand)
return
end
if db.require_in_range then
if not self:get_in_range(hand) then