forked from Hekili/hekili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.lua
7600 lines (5907 loc) · 246 KB
/
State.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
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
-- State.lua
-- June 2014
local addon, ns = ...
local Hekili = _G[ addon ]
local auras = ns.auras
local formatKey = ns.formatKey
local ResourceRegenerates = ns.ResourceRegenerates
local Error = ns.Error
local orderedPairs = ns.orderedPairs
local round, roundUp, roundDown = ns.round, ns.roundUp, ns.roundDown
local safeMin, safeMax = ns.safeMin, ns.safeMax
local GetPlayerAuraBySpellID = C_UnitAuras.GetPlayerAuraBySpellID
local FindPlayerAuraByID, IsAbilityDisabled, IsDisabledCovenantSpell = ns.FindPlayerAuraByID, ns.IsAbilityDisabled, ns.IsDisabledCovenantSpell
-- Clean up table_x later.
local insert, remove, sort, tcopy, unpack, wipe = table.insert, table.remove, table.sort, ns.tableCopy, table.unpack, table.wipe
local format = string.format
local Mark, SuperMark, ClearMarks = ns.Mark, ns.SuperMark, ns.ClearMarks
local RC = LibStub( "LibRangeCheck-3.0" )
local LSR = LibStub( "SpellRange-1.0" )
local class = Hekili.Class
local scripts = Hekili.Scripts
local unknown_buff, unknown_debuff
-- This will be our environment table for local functions.
local state = Hekili.State
local PTR = ns.PTR
state.PTR = PTR
state.ptr = PTR and 1 or 0
state.now = 0
state.offset = 0
state.modified = false
state.resetType = "heavy"
state.encounterID = 0
state.encounterName = "None"
state.encounterDifficulty = 0
state.aggro = false
state.tanking = false
state.delay = 0
state.delayMin = 0
state.delayMax = 15
state.false_start = 0
state.latency = 0
state.filter = "none"
state.cast_target = "nobody"
state.arena = false
state.bg = false
state.mainhand_speed = 0
state.offhand_speed = 0
state.min_targets = 0
state.max_targets = 0
state.action = {}
state.active_dot = {}
state.args = {}
state.azerite = {}
state.essence = {}
state.aura = {}
state.auras = auras
state.buff = {}
state.consumable = {}
state.cooldown = {}
state.empowerment = {
id = 0,
spell = "none",
start = 0,
finish = 0,
hold = 0,
stages = {}
}
state.max_empower = 3
state.empowering = {}
state.health = {
max = 1,
initialized = false
}
state.legendary = {}
state.runeforge = state.legendary -- Different APLs use runeforge.X.equipped vs. legendary.X.enabled.
state.debuff = {}
state.dot = {}
state.equipped = {}
state.main_hand = {
size = 0
}
state.off_hand = {
size = 0
}
state.gcd = {}
state.history = {
casts = {},
units = {}
}
state.holds = {}
state.items = {}
state.pet = {
fake_pet = {
name = "Mary-Kate Olsen",
expires = 0,
permanent = false,
}
}
state.player = {
lastcast = "none",
lastgcd = "none",
lastoffgcd = "none",
casttime = 0,
updated = true,
channeling = false,
channel_start = 0,
channel_end = 0,
channel_spell = nil
}
state.prev = {
meta = "castsAll",
history = { "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action" }
}
state.prev_gcd = {
meta = "castsOn",
history = { "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action" }
}
state.prev_off_gcd = {
meta = "castsOff",
history = { "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action", "no_action" }
}
state.predictions = {}
state.predictionsOff = {}
state.predictionsOn = {}
state.purge = {}
state.pvptalent = {}
state.race = {}
state.script = {}
state.set_bonus = {}
state.settings = {}
state.sim = {}
state.spec = {}
state.stance = {}
state.stat = {}
state.swings = {
mh_actual = 0,
mh_speed = UnitAttackSpeed( "player" ) > 0 and UnitAttackSpeed( "player" ) or 2.6,
mh_projected = 2.6,
oh_actual = 0,
oh_speed = select( 2, UnitAttackSpeed( "player" ) ) or 2.6,
oh_projected = 3.9
}
state.system = {}
state.table = table
state.talent = {}
state.target = {
debuff = state.debuff,
dot = state.dot,
health = {},
updated = true
}
state.movement = {}
setmetatable( state.movement, {
__index = function( t, k )
if k == "distance" then
if state.buff.movement.up then return state.target.maxR end
return 0
end
return state.target[ k ]
end
} )
state.sim.target = state.target
state.toggle = {}
state.totem = {}
state.trinket = {
t1 = {
slot = "t1",
--[[ has_cooldown = {
slot = "t1"
}, ]]
stacking_stat = {
slot = "t1"
},
has_stacking_stat = {
slot = "t1"
},
stat = {
slot = "t1"
},
has_stat = {
slot = "t1"
},
is = {
slot = "t1"
},
},
t2 = {
slot = "t2",
--[[ has_cooldown = {
slot = "t2",
}, ]]
stacking_stat = {
slot = "t2"
},
has_stacking_stat = {
slot = "t2"
},
stat = {
slot = "t2"
},
has_stat = {
slot = "t2",
},
is = {
slot = "t2",
},
},
main_hand = {
slot = "main_hand",
--[[ has_cooldown = {
slot = "main_hand",
}, ]]
stacking_stat = {
slot = "main_hand"
},
has_stacking_stat = {
slot = "main_hand"
},
stat = {
slot = "main_hand"
},
has_stat = {
slot = "main_hand",
},
is = {
slot = "main_hand",
},
},
any = {},
cooldown = {
},
has_cooldown = {
},
stacking_stat = {
},
has_stacking_stat = {
},
stacking_proc = {
},
has_stacking_proc = {
},
stat = {
},
has_stat = {
},
}
state.trinket.proc = state.trinket.stat
state.trinket[1] = state.trinket.t1
state.trinket[2] = state.trinket.t2
state.using_apl = setmetatable( {}, {
__index = function( t, k )
return false
end
} )
state.role = setmetatable( {}, {
__index = function( t, k )
return false
end
} )
local mt_no_trinket_cooldown = {
}
local mt_no_trinket_stacking_stat = {
}
local mt_no_trinket_stat = {
}
local mt_no_trinket = {
__index = function( t, k )
if k:sub(1,4) == "has_" then
return false
elseif k == "down" then
return true
end
return false
end
}
local no_trinket = setmetatable( {
slot = "none",
cooldown = setmetatable( {}, mt_no_trinket_cooldown ),
stacking_stat = setmetatable( {}, mt_no_trinket_stacking_stat ),
stat = setmetatable( {}, mt_no_trinket_stat ),
is = setmetatable( {}, {
__index = function( t, k )
return false
end
} )
}, mt_no_trinket )
setmetatable( state.trinket, {
__index = function( t, k )
if t.t1.is[ k ] then
return t.t1
elseif t.t2.is[ k ] then
return t.t2
else
return no_trinket
end
end
} )
state.trinket.stat.any = state.trinket.any
local mt_trinket_any = {
__index = function( t, k )
return state.trinket.t1[ k ] or state.trinket.t2[ k ]
end
}
setmetatable( state.trinket.any, mt_trinket_any )
local mt_trinket_any_stacking_stat = {
__index = function( t, k )
if state.trinket.t1.has_stacking_stat[k] then return state.trinket.t1
elseif state.trinket.t2.has_stacking_stat[k] then return state.trinket.t2 end
return no_trinket
end
}
setmetatable( state.trinket.stacking_stat, mt_trinket_any_stacking_stat )
setmetatable( state.trinket.stacking_proc, mt_trinket_any_stacking_stat )
local mt_trinket_any_stat = {
__index = function( t, k )
--[[ if k == "any" then
return ( state.trinket.has_stat[
end ]]
if state.trinket.t1.has_stat[k] then return state.trinket.t1
elseif state.trinket.t2.has_stat[k] then return state.trinket.t2 end
return no_trinket
end
}
setmetatable( state.trinket.stat, mt_trinket_any_stat )
local mt_trinket = {
__index = function( t, k )
local isEnabled = ( not rawget( t, "__usable" ) ) or ( rawget( t, "__ability" ) and not state:IsDisabled( t.__ability ) or false )
if k == "id" then
return isEnabled and t.__id or 0
elseif k == "ability" then
return rawget( t, "__ability" ) or "null_cooldown"
elseif k == "usable" then
return rawget( t, "__usable" ) or false
elseif k == "has_use_buff" or k == "use_buff" then
return isEnabled and t.__has_use_buff or false
elseif k == "use_buff_duration" or k == "buff_duration" then
return isEnabled and t.__has_use_buff and t.__use_buff_duration or 0.01
elseif k == "has_proc" or k == "proc" then
return isEnabled and t.__proc or false
end
if k == "up" or k == "ticking" or k == "active" then
return isEnabled and class.trinkets[ t.id ].buff and state.buff[ class.trinkets[ t.id ].buff ].up or false
elseif k == "react" or k == "stack" or k == "stacks" then
return isEnabled and class.trinkets[ t.id ].buff and state.buff[ class.trinkets[ t.id ].buff ][ k ] or 0
elseif k == "remains" then
return isEnabled and class.trinkets[ t.id ].buff and state.buff[ class.trinkets[ t.id ].buff ].remains or 0
elseif k == "has_cooldown" then
return isEnabled and ( GetItemSpell( t.id ) ~= nil ) or false
elseif k == "ready_cooldown" then
if isEnabled and t.usable and t.ability then
return t.cooldown.ready
end
return true
elseif k == "cooldown" then
if t.usable and t.ability and state.cooldown[ t.ability ] then
return state.cooldown[ t.ability ]
end
return state.cooldown.null_cooldown
elseif k == "cast_time" or k == "cast_time" then
return t.usable and t.ability and class.abilities[ t.ability ] and class.abilities[ t.ability ].cast or 0
end
return k
end
}
setmetatable( state.trinket.t1, mt_trinket )
setmetatable( state.trinket.t2, mt_trinket )
setmetatable( state.trinket.main_hand, mt_trinket )
local mt_trinket_is = {
__index = function( t, k )
local item = state.trinket[ t.slot ]
if item.usable and item.ability == k then return true end
return false
end,
}
setmetatable( state.trinket.t1.is, mt_trinket_is )
setmetatable( state.trinket.t2.is, mt_trinket_is )
setmetatable( state.trinket.main_hand.is, mt_trinket_is )
--[[ local mt_trinket_cooldown = {
__index = function(t, k)
if k == "duration" or k == "expires" then
-- Refresh the ID in case we changed specs and ability is spec dependent.
local start, duration = GetItemCooldown( state.trinket[ t.slot ].id )
t.duration = duration or 0
t.expires = start and ( start + duration ) or 0
return t[k]
elseif k == "remains" then
return max( 0, t.expires - ( state.query_time ) )
elseif k == "up" then
return t.remains == 0
elseif k == "down" then
return t.remains > 0
end
-- return Error( "UNK: " .. k )
end
}
setmetatable( state.trinket.t1.cooldown, mt_trinket_cooldown )
setmetatable( state.trinket.t2.cooldown, mt_trinket_cooldown ) ]]
local mt_trinket_has_stacking_stat = {
__index = function( t, k )
local trinket = state.trinket[ t.slot ].id
if trinket == 0 then return false end
if k == "any" or k == "any_dps" then return class.trinkets[ trinket ].stacking_stat ~= nil end
if k == "ms" then k = "multistrike" end
return class.trinkets[ trinket ].stacking_stat == k
end
}
setmetatable( state.trinket.t1.has_stacking_stat, mt_trinket_has_stacking_stat )
setmetatable( state.trinket.t2.has_stacking_stat, mt_trinket_has_stacking_stat )
setmetatable( state.trinket.main_hand.has_stacking_stat, mt_trinket_has_stacking_stat )
local mt_trinket_has_stat = {
__index = function( t, k )
local trinket = state.trinket[ t.slot ].id
if trinket == 0 then return false end
if k == "any" or k == "any_dps" then return class.trinkets[ trinket ].stat ~= nil end
if k == "ms" then k = "multistrike" end
return class.trinkets[ trinket ].stat == k
end
}
setmetatable( state.trinket.t1.has_stat, mt_trinket_has_stat )
setmetatable( state.trinket.t2.has_stat, mt_trinket_has_stat )
setmetatable( state.trinket.main_hand.has_stat, mt_trinket_has_stat )
local mt_trinkets_has_stat = {
__index = function( t, k )
if k == "ms" then k = "multistrike" end
if k == "any" then
return class.trinkets[ state.trinket.t1.id ].stat ~= nil or class.trinkets[ state.trinket.t2.id ].stat ~= nil
end
return class.trinkets[ state.trinket.t1.id ].stat == k or class.trinkets[ state.trinket.t2.id ].stat == k
end
}
setmetatable( state.trinket.has_stat, mt_trinkets_has_stat )
local mt_trinkets_has_stacking_stat = {
__index = function( t, k )
if k == "ms" then k = "multistrike" end
if k == "any" then
return class.trinkets[ state.trinket.t1.id ].stacking_stat ~= nil or class.trinkets[ state.trinket.t2.id ].stacking_stat ~= nil
end
return class.trinkets[ state.trinket.t1.id ].stacking_stat == k or class.trinkets[ state.trinket.t2.id ].stacking_stat == k
end
}
setmetatable( state.trinket.has_stacking_stat, mt_trinkets_has_stacking_stat )
state.max = safeMax
state.min = safeMin
state.abs = safeAbs
if Hekili.Version:match( "^Dev" ) then
state.print = print
else
state.print = function() end
end
state.Enum = Enum
state.FindPlayerAuraByID = ns.FindPlayerAuraByID
state.FindUnitBuffByID = ns.FindUnitBuffByID
state.FindUnitDebuffByID = ns.FindUnitDebuffByID
state.FindRaidBuffByID = ns.FindRaidBuffByID
state.FindRaidBuffLowestRemainsByID = ns.FindRaidBuffLowestRemainsByID
state.FindLowHpPlayerWithoutBuffByID = ns.FindLowHpPlayerWithoutBuffByID
state.GetActiveLossOfControlData = C_LossOfControl.GetActiveLossOfControlData
state.GetActiveLossOfControlDataCount = C_LossOfControl.GetActiveLossOfControlDataCount
state.GetNumGroupMembers = GetNumGroupMembers
-- state.GetItemCooldown = GetItemCooldown
state.GetItemCount = GetItemCount
state.GetItemGem = GetItemGem
state.GetItemInfo = GetItemInfo
state.GetPlayerAuraBySpellID = GetPlayerAuraBySpellID
state.GetShapeshiftForm = GetShapeshiftForm
state.GetShapeshiftFormInfo = GetShapeshiftFormInfo
state.GetSpellCount = GetSpellCount
state.GetSpellInfo = GetSpellInfo
state.GetSpellLink = GetSpellLink
state.GetSpellTexture = GetSpellTexture
state.GetStablePetInfo = GetStablePetInfo
state.GetTime = GetTime
state.GetTotemInfo = GetTotemInfo
state.InCombatLockdown = InCombatLockdown
state.IsActiveSpell = ns.IsActiveSpell
state.IsPlayerSpell = IsPlayerSpell
state.IsSpellKnown = IsSpellKnown
state.IsSpellKnownOrOverridesKnown = IsSpellKnownOrOverridesKnown
state.IsUsableItem = IsUsableItem
state.IsUsableSpell = IsUsableSpell
state.UnitAura = UnitAura
state.UnitAuraSlots = UnitAuraSlots
state.UnitBuff = UnitBuff
state.UnitCanAttack = UnitCanAttack
state.UnitCastingInfo = UnitCastingInfo
state.UnitChannelInfo = UnitChannelInfo
state.UnitClassification = UnitClassification
state.UnitDebuff = UnitDebuff
state.UnitExists = UnitExists
state.UnitGetTotalAbsorbs = UnitGetTotalAbsorbs
state.UnitGUID = UnitGUID
state.UnitHealth = UnitHealth
state.UnitHealthMax = UnitHealthMax
state.UnitName = UnitName
state.UnitIsFriend = UnitIsFriend
local UnitIsUnit = _G.UnitIsUnit
state.UnitIsUnit = function( a, b )
return a == b or UnitIsUnit( a, b )
end
state.UnitIsPlayer = UnitIsPlayer
state.UnitLevel = UnitLevel
state.UnitPower = UnitPower
state.UnitPartialPower = UnitPartialPower
state.UnitPowerMax = UnitPowerMax
state.abs = math.abs
state.ceil = math.ceil
state.floor = math.floor
state.format = string.format
state.ipairs = ipairs
state.pairs = pairs
state.rawget = rawget
state.rawset = rawset
state.select = select
state.tinsert = table.insert
state.insert = table.insert
state.remove = table.remove
state.tonumber = tonumber
state.tostring = tostring
state.type = type
state.safenum = function( val )
if type( val ) == "number" then return val end
return val == true and 1 or 0
end
state.safebool = function( val )
if type( val ) == "boolean" then return val end
if val == nil then return false end
if val == 0 then return false end
return true
end
state.combat = 0
state.faction = UnitFactionGroup( "player" )
state.race[ formatKey( UnitRace("player") ) ] = true
state.class = Hekili.Class
state.targets = ns.targets
state._G = 0
-- Place an ability on cooldown in the simulated game state.
local function setCooldown( action, duration )
local cd = state.cooldown[ action ] or {}
cd.duration = duration > 0 and duration or cd.duration
cd.expires = state.query_time + duration
cd.charge = 0
cd.recharge_began = state.query_time
cd.next_charge = cd.expires
cd.recharge = duration > 0 and duration or cd.recharge
state.cooldown[ action ] = cd
end
state.setCooldown = setCooldown
local function spendCharges( action, charges )
local ability = class.abilities[ action ]
if not ability.charges or ability.charges == 1 then
setCooldown( action, ability.cooldown )
return
end
if not state.cooldown[ action ] then state.cooldown[ action ] = {} end
local cd = state.cooldown[ action ]
if cd.next_charge <= state.query_time then
cd.recharge_began = state.query_time
cd.next_charge = state.query_time + ( ability.recharge or ability.cooldown )
cd.recharge = ability.recharge > 0 and ability.recharge or cd.recharge
end
cd.charge = max( 0, cd.charge - charges )
local dur = ability.recharge or ability.cooldown
cd.duration = dur > 0 and dur or cd.duration
cd.expires = cd.charge == 0 and cd.next_charge or 0
end
state.spendCharges = spendCharges
local function gainCharges( action, charges )
if class.abilities[ action ].charges then
state.cooldown[ action ].charge = min( class.abilities[ action ].charges, state.cooldown[ action ].charge + charges )
-- resolve cooldown state.
if state.cooldown[ action ].charge > 0 then
-- state.cooldown[ action ].duration = 0
state.cooldown[ action ].expires = 0
end
if state.cooldown[ action ].charge == class.abilities[ action ].charges then
state.cooldown[ action ].next_charge = 0
-- state.cooldown[ action ].recharge = 0
state.cooldown[ action ].recharge_began = 0
end
else
-- Error-proof gaining charges for abilities without charges.
if charges >= 1 then
setCooldown( action, 0 )
end
end
end
state.gainCharges = gainCharges
function state.gainChargeTime( action, time, debug )
local ability = class.abilities[ action ]
if not ability then return end
local cooldown = state.cooldown[ action ]
if not ability.charges then
-- Error-proof gaining charge time on chargeless abilities.
cooldown.expires = cooldown.expires - time
return
end
if cooldown.charge == ability.charges then return end
cooldown.next_charge = cooldown.next_charge - time
cooldown.recharge_began = cooldown.recharge_began - time
if cooldown.expires > 0 then cooldown.expires = max( 0, cooldown.expires - time ) end
if cooldown.next_charge <= state.query_time then
cooldown.charge = min( ability.charges, cooldown.charge + 1 )
-- We have a charge, reset cooldown.
-- cooldown.duration = 0
cooldown.expires = 0
if cooldown.charge == ability.charges then
cooldown.next_charge = 0
-- cooldown.recharge = 0
cooldown.recharge_began = 0
else
cooldown.recharge_began = cooldown.next_charge
cooldown.next_charge = cooldown.next_charge + ability.recharge
-- cooldown.recharge = ability.recharge
end
end
end
function state.reduceCooldown( action, time )
local ability = class.abilities[ action ]
if not ability then return end
if ability.charges then
state.gainChargeTime( action, time )
return
end
state.cooldown[ action ].expires = max( 0, state.cooldown[ action ].expires - time )
end
-- Cycling System...
do
local cycle = {}
local debug = function( ... ) if Hekili.ActiveDebug then Hekili:Debug( ... ) end end
function state.SetupCycle( ability, quiet )
wipe( cycle )
if not ability and not quiet then
debug( " - no ability provided to SetupCycle." )
return
end
local aura = ability.cycle
if not aura then
-- Fallback check, is there an aura with the same name as the ability?
aura = class.auras[ ability.key ] and ability.key
end
if not aura and not quiet then
debug( " - no aura identified for target-cycling and no aura matching " .. ability.key .. " found in ability / spec module; target cycling disabled." )
return
end
local cDebuff = class.auras[ aura ] and state.debuff[ aura ]
if not cDebuff then
debug( " - the debuff '%s' was not found in our database.", aura )
return
end
if cDebuff.up and not ability.cycle_to then
-- We want to target enemies with this debuff.
cycle.expires = cDebuff.expires
cycle.minTTD = max( state.settings.cycle_min, ability.min_ttd or 0, cDebuff.duration / 2 )
cycle.maxTTD = ability.max_ttd
cycle.aura = aura
if state.active_dot[ aura ] >= state.cycle_enemies then
if not quiet then debug( " - we do not have another valid target for " .. aura .. ": " .. state.active_dot[ aura ] .. " vs " .. state.cycle_enemies .. "." ) end
state.ClearCycle()
return
end
if not quiet then
debug( " - we will use the ability on a different target, if available, until %s expires at %.2f [+%.2f].", cycle.aura, cycle.expires, cycle.expires - state.query_time )
end
elseif cDebuff.down and ability.cycle_to and state.active_dot[ aura ] > 0 and state.query_time < state.now + ( 2 * state.gcd.max ) then
cycle.expires = state.query_time + ( 2 * state.gcd.max ) -- Assume the aura is available for 2 GCDs (don't forecast a slow target swap).
cycle.minTTD = max( state.settings.cycle_min, ability.min_ttd or 0, cDebuff.duration / 2 )
cycle.maxTTD = ability.max_ttd
cycle.aura = aura
if not quiet then
debug( " - we will use the ability on a target with %s, if available, .", cycle.aura )
end
else
if not quiet then debug( " - cycle aura appears to be down, so we're sticking with our current target." ) end
end
--[[ Possible future version:
local debuffIsUp = cDebuff.up
cycle.expires = cDebuff.duration + state.query_time
cycle.minTTD = max( state.settings.cycle_min, ability.min_ttd or 0, cDebuff.duration / 2 )
cycle.maxTTD = ability.max_ttd
cycle.aura = aura
if debuffIsUp and not ability.cycle_to and state.active_dot[ aura ] >= state.cycle_enemies then
debug( " - we will not use the ability on a different target, as we have enough targets with %s.", cycle.aura )
state.ClearCycle()
return
end
if not debuffIsUp and not ability.cycle_to then
debug( " - we will not use the ability on a different target, as current target does not have %s.", cycle.aura )
state.ClearCycle()
return
end
if not debuffIsUp and ability.cycle_to and active_dot[ aura ] == 0 then
debug( " - we will not use the ability on a different target, as no other target has %s applied.", cycle.aura)
state.ClearCycle()
return
end
]]
end
function state.GetCycleInfo()
return cycle.expires, cycle.minTTD, cycle.maxTTD, cycle.aura
end
function state.SetCycleInfo( expires, minTTD, maxTTD, aura )
cycle.expires = expires
cycle.minTTD = minTTD
cycle.maxTTD = maxTTD
cycle.aura = aura
end
function state.HasCyclingDebuff( aura )
if not cycle.aura then return false end
if aura and aura ~= cycle.aura then return false end
return true
end
function state.IsCycling( aura, quiet )
if not cycle.aura then
return false, "cycle.aura is nil"
end
if aura and cycle.aura ~= aura then
if not quiet then debug( "cycle.aura ~= '%s'", aura ) end
return false, format( "cycle aura (%s) is not '%s'", cycle.aura or "none", aura )
end
if state.cycle_enemies == 1 then
return false, "cycle_enemies == 1"
end
if cycle.expires < state.query_time then
return false, format( "cycle aura (%s) expires before current time", cycle.aura )
end
if state.active_dot[ cycle.aura ] >= state.cycle_enemies then
return false, format( "active_dot[%d] >= cycle_enemies[%d]", state.active_dot[ cycle.aura ], state.cycle_enemies )
end
return true
end
function state.ClearCycle()
if cycle.aura then wipe( cycle ) end
state.cycle = nil
end
state.cycleInfo = cycle
end
-- Apply a buff to the current game state.
local function applyBuff( aura, duration, stacks, value, v2, v3, applied )
if not aura then
Error( "Attempted to apply/remove a nameless aura '%s'.\n\n%s", aura or "nil", debugstack() )
return
end
local auraInfo = class.auras[ aura ]
if not auraInfo then
local spec = class.specs[ state.spec.id ]
if spec then
spec:RegisterAura( aura, { ["duration"] = duration } )
class.auras[ aura ] = spec.auras[ aura ]
end
auraInfo = class.auras[ aura ]
if not auraInfo then return end
end
if auraInfo.alias then
aura = auraInfo.alias[1]
end
if state.cycle then
if duration == 0 then state.active_dot[ aura ] = max( 0, state.active_dot[ aura ] - 1 )
else state.active_dot[ aura ] = max( state.active_enemies, state.active_dot[ aura ] + 1 ) end
return
end
local b = state.buff[ aura ]
if not b then return end
duration = duration or auraInfo.duration or 15
if duration == 0 then
b.last_expiry = b.expires or 0
b.expires = 0
b.lastCount = b.count
b.count = 0
b.lastApplied = b.applied
b.last_application = b.applied or 0
b.v1 = value or 0
b.v2 = 0
b.v3 = 0
b.applied = 0
b.caster = "unknown"
state.active_dot[ aura ] = max( 0, state.active_dot[ aura ] - 1 )
if auraInfo.funcs.onRemove then auraInfo.funcs.onRemove() end
else
if not b.up then state.active_dot[ aura ] = state.active_dot[ aura ] + 1 end
b.lastCount = b.count
b.lastApplied = b.applied
b.applied = applied or state.query_time
b.last_application = b.applied or 0
-- b.duration = duration
b.expires = b.applied + duration
b.last_expiry = b.expires
b.count = max( 0, min( class.auras[ aura ].max_stack or 1, stacks or 1 ) )
b.v1 = value or 0