forked from Hekili/hekili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scripts.lua
1873 lines (1449 loc) · 63.5 KB
/
Scripts.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
-- Scripts.lua
-- December 2014
local addon, ns = ...
local Hekili = _G[ addon ]
local class = Hekili.Class
local scripts = Hekili.Scripts
local state = Hekili.State
local GetResourceInfo, GetResourceID = ns.GetResourceInfo, ns.GetResourceID
local SpaceOut = ns.SpaceOut
local ceil = math.ceil
local orderedPairs = ns.orderedPairs
local roundUp = ns.roundUp
local safeMax = ns.safeMax
local trim = string.trim
local twipe = table.wipe
-- Forgive the name, but this should properly replace ! characters with not, accounting for appropriate bracketing.
-- Why so complex? Because "! 0 > 1" converted to Lua is "not 0 > 1" which evaluates to "false > 1" -- not the goal.
-- This should convert:
-- example 1: ! 0 > 1
-- not ( 0 > 1 )
--
-- example 2: ! ( 0 > 1 & ! ( false | true ) )
-- not ( 0 > 1 & not ( false | true ) )
--
-- example 3: ! cooldown.x.remains > 1 * ( gcd * ( 8 % 3 ) )
-- not ( cooldown.x.remains > 1 * ( gcd * ( 8 % 3 ) ) )
--
-- Hopefully.
local exprBreak = {
["&"] = true,
["|"] = true,
}
local function forgetMeNots( str )
-- First, handle already bracketed "!(X)" -> "not (X)".
local found = 1
while found > 0 do
str, found = str:gsub( "%s*!%s*(%b())%s*", " not %1 " )
end
-- The remaining conditions are not bracketed, but may include brackets.
-- Such as !5>2+(1*3).
-- So we'll start from the !, then go through the string until it's time to stop.
local i = 1
local substring
while( str:find("!") ) do
local start = str:find("!")
--while str:sub( start, start ):match("%s") do
-- start = start + 1
-- end
local parens = 0
local finish = -1
for j = start, str:len() do
local char = str:sub( j, j )
if char == "(" then
parens = parens + 1
elseif char == ")" then
if parens > 0 then parens = parens - 1
else finish = j - 1; break end
elseif parens == 0 then
-- We are not within a bracketed part of the string. We can end here.
if exprBreak[ char ] then
finish = j - 1
break
end
end
end
if finish == -1 then finish = str:len() end
substring = str:sub( start + 1, finish )
substring = substring:trim()
str = format( "%s not ( %s ) %s", str:sub( 1, start - 1 ) or "", substring, str:sub( finish + 1, str:len() ) or "" )
i = i + 1
if i >= 100 then Hekili:Debug( "Was unable to convert '!' to 'not' in string [%s].", str ); break end
end
str = str:gsub( "%s%s", " " )
return str
end
local function atToAbs( str )
-- First, handle already bracketed "!(X)" -> "not (X)".
local found = 1
while found > 0 do
str, found = str:gsub( "%s*@%s*(%b())%s*", " abs(safenum%1) " )
end
-- The remaining conditions are not bracketed, but may include brackets.
-- Such as !5>2+(1*3).
-- So we'll start from the !, then go through the string until it's time to stop.
local i = 1
local substring
while( str:find("@") ) do
local start = str:find("@")
--while str:sub( start, start ):match("%s") do
-- start = start + 1
-- end
local parens = 0
local finish = -1
for j = start, str:len() do
local char = str:sub( j, j )
if char == "(" then
parens = parens + 1
elseif char == ")" then
if parens > 0 then parens = parens - 1
else finish = j - 1; break end
elseif parens == 0 then
-- We are not within a bracketed part of the string. We can end here.
if exprBreak[ char ] then
finish = j - 1
break
end
end
end
if finish == -1 then finish = str:len() end
substring = str:sub( start + 1, finish )
substring = substring:trim()
str = format( "%s abs( %s ) %s", str:sub( 1, start - 1 ) or "", substring, str:sub( finish + 1, str:len() ) or "" )
i = i + 1
if i >= 100 then Hekili:Error( "Was unable to convert '@' to 'abs' in string [%s].", str ); break end
end
str = str:gsub( "%s%s", " " )
return str
end
local mathBreak = {
["<"] = true,
[">"] = true,
["="] = true,
["&"] = true,
["|"] = true,
[","] = true,
}
local function HandleDeprecatedOperators( str, opStr, prefix )
--str = str:gsub("%s", "")
for left, op, right in str:gmatch("(.+)(" .. opStr .. ")(.+)") do
local leftLen, rightLen = left:len(), right:len()
local val1, val2, len1, len2, b1, b2
if left:sub(-1) == ")" then
val1 = left:match("(%b())$")
len1 = val1:len()
val1 = val1:sub( 2, -2 )
else
-- We need to traverse the left side, backwards.
local parens = 0
local eos = -1
for i = 1, leftLen do
local char = left:sub(-i, -i)
if char == ")" then
-- Grab the full bracketed pair and move on.
i = i + left:sub( 1, 1 + leftLen - i ):match( "(%b())$" ):len()
elseif mathBreak[ char ] or char == "(" then
eos = i - 1
break
end
end
if eos == -1 then
val1 = left
len1 = leftLen
else
val1 = left:sub( 1 + leftLen - eos, leftLen):trim()
len1 = eos
end
end
val1 = val1:trim()
if right:sub(1, 1) == "(" then
val2 = right:match("^(%b())")
len2 = val2:len()
val2 = val2:sub( 2, -2 )
else
local parens = 0
local eos = -1
for i = 1, right:len() do
local char = right:sub(i, i)
if char == "(" then
i = i + right:sub( i ):match("^(%b())" ):len()
elseif mathBreak[char] or char == ")" then
eos = i - 1
break
end
end
if eos == -1 then
val2 = right
len2 = rightLen
else
val2 = right:sub(1, eos)
len2 = eos
end
end
val2 = val2:trim()
str = left:sub( 1, leftLen - len1 ) .. " " .. prefix .. "(safenum(" .. val1 .. "),safenum(" .. val2 .. ")) " .. right:sub( 1 + len2 )
end
if str:find(opStr) then return scripts.HandleDeprecatedOperators( str, opStr, prefix ) end
return str
end
scripts.HandleDeprecatedOperators = HandleDeprecatedOperators
local invalid = "([^a-zA-Z0-9_.[])"
local function extendExpression( str, expr, suffix )
if str:find( expr ) then
str = str:gsub( "^" .. expr .. invalid, expr .. "." .. suffix .. "%1" )
str = str:gsub( invalid .. expr .. "$", "%1" .. expr .. "." .. suffix )
str = str:gsub( "^" .. expr .. "$", expr .. "." .. suffix )
str = str:gsub( invalid .. expr .. invalid, "%1" .. expr .. "." .. suffix .. "%2" )
end
return str
end
local function SimcWithResources( str )
for k in pairs( GetResourceInfo() ) do
if str:find( k ) then
str = extendExpression( str, k, "current" )
end
end
if str:find( "health" ) then
str = extendExpression( str, "health", "current" )
end
if str:find( "rune" ) then
str = extendExpression( str, "rune", "current" )
end
if str:find( "spell_targets" ) then
str = extendExpression( str, "spell_targets", "any" )
end
if str:find( "gcd" ) then
str = extendExpression( str, "gcd", "execute" )
end
return str
end
local function space_killer(s)
return s:gsub("%s", "")
end
-- Convert SimC syntax to Lua conditionals.
local function SimToLua( str, modifier )
-- If no conditions were provided, function should return true.
if not str or type( str ) == "number" then return str end
local orig = str
str = str:trim()
if str == "" then return orig end
-- Strip comments.
str = str:gsub("^%-%-.-\n", "")
-- Replace '!' with ' not '.
str = str:gsub( "!=", "~=" )
if str:find("!") then str = forgetMeNots( str ) end
if str:find("@") then str = atToAbs( str ) end
-- Replace '^' (simc XOR) with '~=' (functionally identical for booleans).
if str:find("%^") then str = str:gsub("%^", "~=") end
-- Replace '>?' and '<?' with max/min.
if str:find(">%?") then str = HandleDeprecatedOperators( str, ">%?", "max" ) end
if str:find("<%?") then str = HandleDeprecatedOperators( str, "<%?", "min" ) end
str = SimcWithResources( str )
-- Replace '%' for division with actual division operator '/'.
-- str = str:gsub("([^%%])[ ]+%%[ ]+([^%%])", "%1/%2")
-- Replace '%%' for modulus with '%'.
-- str = str:gsub( "%%%%", "%%" )
-- Replace '&' with ' and '.
str = str:gsub("&", " and ")
-- Replace '|' with ' or '.
str = str:gsub("||", " or "):gsub("|", " or ")
if not modifier then
-- Replace assignment '=' with comparison '=='
str = str:gsub("([^=])=([^=])", "%1==%2" )
-- Fix any conditional '==' that got impacted by previous.
str = str:gsub("==+", "==")
str = str:gsub(">=+", ">=")
str = str:gsub("<=+", "<=")
str = str:gsub("!=+", "~=")
str = str:gsub("~=+", "~=")
end
-- Condense whitespace.
str = str:gsub("%s%s", " ")
-- Condense parenthetical spaces.
str = str:gsub("[(][%s+]", "("):gsub("[%s+][)]", ")")
-- Address equipped.number => equipped[number]
str = str:gsub("%.(%d+)%.", "[%1].")
str = str:gsub("equipped%.(%d+)", "equipped[%1]")
str = str:gsub("main_hand%.(%d[a-z0-9_]+)", "main_hand['%1']")
str = str:gsub("off_hand%.(%d[a-z0-9_]+)", "off_hand['%1']")
str = str:gsub("lowest_vuln_within%.(%d+)", "lowest_vuln_within[%1]")
str = str:gsub("%.in([^a-zA-Z0-9_])", "['in']%1" )
str = str:gsub("%.in$", "['in']" )
str = str:gsub("imps_spawned_during%.([^!=<>&|]+)", "imps_spawned_during['%1'] ")
str = str:gsub("time_to_imps%.(%b()).remains", "time_to_imps[%1].remains")
str = str:gsub("time_to_imps%.(%d+).remains", "time_to_imps[%1].remains")
-- str = str:gsub("incanters_flow_time_to%.(%d+)[.any]?", "incanters_flow_time_to[%1]")
-- Condense bracketed expressions.
str = str:gsub("%b[]", space_killer)
str = str:gsub("prev%.(%d+)", "prev[%1]")
str = str:gsub("prev_gcd%.(%d+)", "prev_gcd[%1]")
str = str:gsub("prev_off_gcd%.(%d+)", "prev_off_gcd[%1]")
str = str:gsub("time_to_sht%.(%d+)", "time_to_sht[%1]")
str = str:gsub("time_to_sht_plus%.(%d+)", "time_to_sht_plus[%1]")
-- str = str:gsub("([a-z0-9_]+)%.(%d+)", "%1[%2]")
--str = SpaceOut( str )
return str
end
scripts.SimToLua = SimToLua
do
-- Okay, this is the auto-recheck parser.
-- Part I: Split into parts.
-- ex. combo_points<5&energy>=action.rake.cost&dot.rake.pmultiplier<2.1&buff.tigers_fury.up&(buff.bloodtalons.up|!talent.bloodtalons.enabled)&(!talent.incarnation.enabled|cooldown.incarnation.remains>18)&!buff.incarnation.up
-- ...and break it into each compartmentalized expression:
-- combo_points<5, energy>=action.rake.cost, dot.rake_multiplier<2.1.
local boundaries = {
["&"] = true,
["|"] = true
}
function scripts:SplitExpr( str )
local output = {}
while( str:len() > 0 ) do
local finish = str:len()
local parens = 0
for i = 1, str:len() do
local char = str:sub( i, i )
if char == "(" then parens = parens + 1
elseif char == ")" then
if parens > 0 then parens = parens - 1 end
elseif boundaries[ char ] and parens == 0 then
finish = i - 1
break
end
end
local expr = str:sub( 1, finish )
local meat = expr:match( "(%b())" )
if meat then
meat = meat:sub( 2, -2 )
if meat:find( "[|&]" ) then
local subExpr = scripts:SplitExpr( meat )
for _, v in ipairs( subExpr ) do
table.insert( output, v )
end
else
table.insert( output, expr )
end
else
table.insert( output, expr )
end
str = str:sub( finish + 2, str:len() )
end
return output
end
local timely = {
{ "^(d?e?buff%.[a-z0-9_]+)%.down$" , "%1.remains" },
{ "^(dot%.[a-z0-9_]+)%.down$" , "%1.remains" },
{ "^!(d?e?buff%.[a-z0-9_]+)%.up$" , "%1.remains" },
{ "^!(dot%.[a-z0-9_]+)%.up$" , "%1.remains" },
{ "^!(d?e?buff%.[a-z0-9_]+)%.react$" , "%1.remains" },
{ "^!(dot%.[a-z0-9_]+)%.react$" , "%1.remains" },
{ "^!(d?e?buff%.[a-z0-9_]+)%.ticking$" , "%1.remains" },
{ "^!(dot%.[a-z0-9_]+)%.ticking$" , "%1.remains" },
{ "^!?(d?e?buff%.[a-z0-9_]+)%.remains$", "%1.remains" },
{ "^!ticking" , "remains" },
{ "^!?remains$" , "remains" },
{ "^refreshable" , "time_to_refresh" },
{ "^gcd.remains$" , "gcd.remains" },
{ "^gcd.remains<?=(.+)$" , "gcd.remains-%1" },
{ "^swing.([a-z_]+).remains$", "swing.%1.remains" },
{ "^(.-)%.deficit<=?(.-)$" , "0.01+%1.timeTo(%1.max-(%2))" },
{ "^(.-)%.deficit>=?(.-)$" , "0.01+%1.timeTo(%1.max-(%2))" },
{ "^cooldown%.([a-z0-9_]+)%.ready$" , "cooldown.%1.remains" },
{ "^cooldown%.([a-z0-9_]+)%.up$" , "cooldown.%1.remains" },
{ "^!?cooldown%.([a-z0-9_]+)%.remains$" , "cooldown.%1.remains" },
{ "^charges_fractional=(.-)$" , "(%1-charges_fractional)*recharge" },
{ "^charges_fractional>=?(.-)$" , "0.01+(%1-charges_fractional)*recharge" },
{ "^charges=(.-)$" , "(%1-charges_fractional)*recharge" },
{ "^charges>=?(.-)$" , "0.01+(%1-charges_fractional)*recharge" },
{ "^(cooldown%.[a-z0-9_]+)%.charges_fractional[>=]+(.-)$", "(%2-%1.charges_fractional)*%1.recharge" },
{ "^(cooldown%.[a-z0-9_]+)%.charges>=?(.-)$" , "(1+%2-%1.charges_fractional)*recharge" },
{ "^(action%.[a-z0-9_]+)%.charges_fractional[>=]+(.-)$" , "(%2-%1.charges_fractional)*%1.recharge" },
{ "^(action%.[a-z0-9_]+)%.charges>=?(.-)$" , "(1+%2-%1.charges_fractional)*%1.recharge" },
{ "^full_recharge_time[<>]=?(.-)$" , "0.01+full_recharge_time-%1" },
{ "^!(action%.[a-z0-9]+)%.executing$" , "%1.execute_remains" },
{ "^!(action%.[a-z0-9]+)%.channeling$" , "%1.channel_remains" },
{ "^(.-time_to_die)<=?(.-)$" , "%1-%2" },
{ "^(.-)%.time_to_(.-)<=?(.-)$", "%1.time_to_%2-%3" },
{ "^debuff%.festering_wound%.stack[>=]=?(.-)$" , "time_to_wounds(%1)" },
{ "^dot%.festering_wound%.stack[>=]=?(.-)$" , "time_to_wounds(%1)" },
{ "^rune<=?(.-)$" , "rune.timeTo(%1)" },
{ "^rune>=?(.-)$" , "rune.timeTo(1+%1)" },
{ "^rune.current<=?(.-)$" , "rune.timeTo(%1)" },
{ "^rune.current>=?(.-)$" , "rune.timeTo(1+%1)" },
{ "^master_assassin_remains[<=]+(.-)$" , "0.01+master_assassin_remains-(%1)" },
{ "^exsanguinated$" , "remains" }, -- Assassination
{ "^!?(debuff%.[a-z0-9_]+)%.exsanguinated$" , "%1.remains" }, -- Assassination
{ "^!?(dot%.[a-z0-9_]+)%.exsanguinated$" , "%1.remains" }, -- Assassination
{ "^ss_buffed$" , "remains" }, -- Assassination
{ "^!?(debuff%.[a-z0-9_]+)%.ss_buffed$" , "%1.remains" }, -- Assassination
{ "^!?(dot%.[a-z0-9_]+)%.ss_buffed$" , "%1.remains" }, -- Assassination
{ "^dot%.([a-z0-9_]+).haste_pct_next_tick$" , "0.01+query_time+(dot.%1.last_tick+dot.%1.tick_time)-query_time" }, -- Assassination
{ "^!?stealthed.all$" , "stealthed.remains" },
{ "^!?stealthed.mantle$" , "stealthed.mantle_remains" },
{ "^!?stealthed.sepsis$" , "stealthed.sepsis_remains" },
{ "^!?stealthed.rogue$" , "stealthed.rogue_remains" },
{ "^!?time_to_hpg$" , "time_to_hpg" }, -- Retribution Paladin
{ "^!?time_to_hpg[<=]=?(.-)$" , "time_to_hpg-%1" }, -- Retribution Paladin
{ "^!?consecration.up" , "consecration.remains" }, -- Prot Paladin
{ "^!?contagion<=?(.-)" , "contagion-%1" }, -- Affliction Warlock
{ "^time_to_imps%.(.+)$" , "time_to_imps[%1]" }, -- Demo Warlock
{ "^active_bt_triggers$" , "time_to_bt_triggers(0)" }, -- Feral Druid w/ Bloodtalons.
{ "^active_bt_triggers<?=0$" , "time_to_bt_triggers(0)" }, -- Feral Druid w/ Bloodtalons.
{ "^active_bt_triggers<(%d+)$" , "time_to_bt_triggers(%1-1)" }, -- Feral Druid w/ Bloodtalons.
{ "^!?action%.([a-z0-9_]+)%.in_flight$" , "action.%1.in_flight_remains" }, -- Fire Mage, but others too, potentially.
{ "^!?action%.([a-z0-9_]+)%.in_flight_remains<=?(.-)$", "action.%1.in_flight_remains-%2" }, -- Fire Mage, but others too, potentially.
{ "^!?variable%.([a-z0-9_]+)$", "safenum(variable.%1)" },
{ "^!?variable%.([a-z0-9_]+)<=?(.-)$", "safenum(variable.%1)-%2" },
{ "^raid_events%.([a-z0-9_]+)%.remains$", "raid_events.%1.remains" },
{ "^raid_events%.([a-z0-9_]+)%.remains$<=?(.-)$", "raid_events.%1.remains-%2" },
{ "^!?raid_events%.([a-z0-9_]+)%.up$", "raid_events.%1.up" },
{ "^!?(pet%.[a-z0-9_]+)%.up$", "%1.remains" },
{ "^!?(pet%.[a-z0-9_]+)%.active$", "%1.remains" },
}
-- Things that tick down.
local decreases = {
["remains$"] = true,
["ticks_remain$"] = true,
["execute_remains$"] = true,
["channel_remains$"] = true,
["^time_to_hpg$"] = true,
["time_to_max$"] = true,
["remains_expected$"] = true,
["expiration_delay_remains$"] = true,
-- ["time_to_%d+$"] = true,
-- ["deficit$"] = true,
}
-- Things that tick up.
local increases = {
["^time$"] = true,
-- ["charges"] = true,
-- ["charges_fractional"] = true,
}
local removals = {
["%.current"] = ""
}
local lessOrEqual = {
-- ["<"] = true,
["<="] = true,
["="] = true,
["=="] = true,
}
local moreOrEqual = {
-- [">"] = true,
[">="] = true,
["="] = true,
["=="] = true,
}
-- Given an expression, can we assess whether it is time-based and progressing in a meaningful way?
-- 1. Cooldowns
local function ConvertTimeComparison( expr, verbose )
while expr:match( "^%b()$" ) do
expr = expr:sub( 2, -2 )
end
local orig = expr
for k, v in pairs( removals ) do
expr = expr:gsub( k, v )
end
local lhs, comp, rhs = expr:match( "^(.-)([<>=~?]+)(.-)$" )
if comp and comp:match( "?" ) then
comp = nil
end
if lhs and comp and rhs then
-- We are looking at a mathematic comparison.
for key in pairs( decreases ) do
if lhs:match( key ) then
if comp == "<" then
return true, "(" .. lhs .. " + 0.01) - (" .. rhs .. ")"
elseif lessOrEqual[ comp ] then
return true, lhs .. " - " .. rhs
end
end
end
for key in pairs( increases ) do
if lhs:match( key ) then
if comp == ">" then
return true, "(" .. rhs .. " + 0.01) - (" .. rhs .. ")"
elseif moreOrEqual[ comp ] then
return true, rhs .. " - " .. lhs
end
end
end
-- resources also tick up (usually, anyway)
for key in pairs( GetResourceInfo() ) do
if lhs == key then
if comp == ">" then
return true, "0.01 + " .. lhs .. ".timeTo( " .. rhs .. " )"
elseif moreOrEqual[ comp ] then
return true, lhs .. ".timeTo( " .. rhs .. " )"
end
end
if rhs == key then
if comp == "<" then
return true, "0.01 + " .. rhs .. ".timeTo( " .. rhs .. " )"
elseif lessOrEqual[ comp ] then
return true, rhs .. ".timeTo( " .. lhs .. " )"
end
end
end
if lhs == "rune" then
if comp == ">" then
return true, "0.01 + rune.timeTo( " .. rhs .. " )"
elseif moreOrEqual[ comp ] then
return true, "rune.timeTo( " .. rhs .. " )"
end
end
if rhs == "rune" then
if comp == "<" then
return true, "0.01 + rune.timeTo( " .. lhs .. " )"
elseif lessOrEqual[ comp ] then
return true, "rune.timeTo( " .. lhs .. " )"
end
end
end
-- If we didn't convert a resource.current to resource.timeTo then let's revert our string.
expr = orig
for i, swap in ipairs( timely ) do
if expr:match( swap[1] ) then
return true, expr:gsub( swap[1], swap[2]), swap[1]
end
end
return false, nil
end
scripts.CTC = ConvertTimeComparison
function scripts:RecheckExpr( expr )
return ConvertTimeComparison( expr )
end
function scripts:BuildRecheck( conditions )
if type( conditions ) ~= "string" then return end
local recheck
conditions = conditions:gsub( " +", "" )
conditions = self:EmulateSyntax( conditions, true )
local exprs = self:SplitExpr( conditions )
if #exprs > 0 then
for i, expr in ipairs( exprs ) do
local converted, calc, why = ConvertTimeComparison( expr )
if converted then
calc = SimToLua( calc )
calc = self:EmulateSyntax( calc, true )
recheck = ( recheck and ( recheck .. ", " ) or "" ) .. calc
end
end
end
return recheck
end
local ops = {
["+"] = true,
["-"] = true,
["*"] = true,
["/"] = true,
["%"] = true,
["|"] = true,
["&"] = true,
["<"] = true,
[">"] = true,
["?"] = true,
["="] = true,
["~"] = true,
["!"] = true,
["!="] = true,
["~="] = true,
["@"] = true
}
local math_ops = {
["+"] = true,
["-"] = true,
["*"] = true,
["/"] = true,
["%"] = true,
["<"] = true,
[">"] = true,
-- ["="] = true,
-- ["!="] = true,
-- ["~="] = true,
["<="] = true,
[">="] = true,
[">?"] = true,
["<?"] = true,
}
local equality = {
["="] = true,
["!="] = true,
["~="] = true,
}
local comp_ops = {
["<"] = true,
[">"] = true,
["?"] = true,
}
local bool_ops = {
["|"] = true,
["&"] = true,
["!"] = true,
}
local funcs = {
["floor"] = true,
["ceil"] = true
}
-- This is hideous.
local esDepth = 0
local esString
function scripts:EmulateSyntax( p, numeric )
if not p or type( p ) ~= "string" then return p end
if esDepth == 0 then
esString = p
end
esDepth = esDepth + 1
local results = {}
local i, maxlen = 1, p:len()
local depth = 0
local bracketed = p:match("(%b())" ) == p
if bracketed then
p = p:sub( 2, p:len() - 1 )
end
local ands = p:find( " and " )
local ors = p:find( " or " )
local nots = p:find( " not " )
local abss = p:find( " abs " )
if ands then p = p:gsub( " and ", "&" ) end
if ors then p = p:gsub( " or ", "|" ) end
if nots then p = p:gsub( " not ", "!" ) end
if abss then p = p:gsub( " abs ", "@" ) end
p = p:gsub( "([!%|&%-%+%*=%%/<>%?%~@]+) +", "%1" )
p = p:gsub( " +([!%|&%-%+%*=%%/<>%?%~@]+)", "%1" )
local orig = p
while ( i <= maxlen ) do
local c = p:sub( i, i )
if c == " " or c == "," then -- do nothing
elseif c == "(" then depth = depth + 1
elseif c == ")" and depth > 0 then
depth = depth - 1
if depth == 0 then
local expr = p:sub( 1, i )
table.insert( results, {
s = expr:trim(),
t = "expr"
} )
if expr:find( "[&%|%-%+/%%%*]" ) ~= nil then results[#results].r = true end
p = p:sub( i + 1 )
i = 0
depth = 0
maxlen = p:len()
end
elseif depth == 0 and ops[c] then
if i > 1 then
local expr = p:sub( 1, i - 1 )
table.insert( results, {
s = expr:trim(),
t = "expr"
} )
if expr:find( "[&$|$-$+/$%%*]" ) ~= nil then results[#results].r = true end
end
c = p:sub( i ):match( "^([&%|%-%+*%%/><!%?=%~@][&%|%-%+*/><%?=%~]?)" )
table.insert( results, {
s = c,
t = "op",
a = c:trim() --sub(1,1)
} )
p = p:sub( i + c:len() )
i = 0
depth = 0
maxlen = p:len()
end
i = i + 1
end
p = p:trim()
if p:len() > 0 then
table.insert( results, {
s = p:trim(),
t = "expr",
l = true
} )
if p:find( "[!&%|%-%+/%%%*@]" ) ~= nil then results[#results].r = true end
end
local output = ""
-- So at this point, we've broken our string into all of its components. Now let's iterate through and fix it up.
i = 1
while( i <= #results ) do
local prev, piece, next = i > 1 and results[i-1] or nil, results[i], i < #results and results[i+1] or nil
local trimmed_prefix
-- If we get a math op (*) followed by a not (!) followed by an expression, we want to safely wrap up the !expr in safenum().
if prev and prev.t == "op" and math_ops[ prev.a ] and piece.t == "op" and piece.a == "!" and next and next.t == "expr" then
table.remove( results, i )
piece = results[ i ]
next = results[ i + 1 ]
piece.s = "(!" .. piece.s .. ")"
elseif piece.t == "expr" and piece.s:match( "^%s*[a-z0-9_]+%s*%(" ) then
trimmed_prefix = piece.s:match( "^%s*([a-z0-9_]+)%s*%(" )
piece.s = piece.s:gsub( "^%s*" .. trimmed_prefix .. "%s*", "" )
end
if piece and piece.t == "expr" then
if piece.r then
if piece.s == orig then
if bracketed then orig = "(" .. orig .. ")" end
if ands then orig = orig:gsub( "&", " and " ) end
if ors then orig = orig:gsub( "|", " or " ) end
if nots then orig = orig:gsub( "!", " not " ) end
if abss then orig = orig:gsub( "@", " abs " ) end
esDepth = esDepth - 1
return orig
end
piece.s = scripts:EmulateSyntax( piece.s, numeric )
end
if ( prev and prev.t == "op" and math_ops[ prev.a ] and not equality[ prev.a ] ) or ( next and next.t == "op" and math_ops[ next.a ] and not equality[ next.a ] ) then
-- This expression is getting mathed.
-- Lets see what it returns and wrap it in btoi if it is a boolean expr.
if piece.s:find("^variable%.") then
-- Let's wrap the variable just to be sure.
if piece.s:sub(1, 1) == "(" then piece.s = "safenum" .. piece.s
else piece.s = "safenum(" .. piece.s .. ")" end
else
local func, warn = Hekili:Loadstring( "return " .. ( SimToLua( piece.s ) or "" ) )
if func then
setfenv( func, state )
state:SetDefaultVariable( 1 )
local pass, val = pcall( func )
state:SetDefaultVariable( 0 )
if not pass and not piece.s:match("variable") then
local safepiece = piece.s:gsub( "%%", "%%%%" )
Hekili:Error( "Unable to compile '" .. safepiece:gsub("%%", "%%%%") .. "' - " .. val .. " (pcall-n)\n\nFrom: " .. esString:gsub( "%%", "%%%%" ) )
else
if trimmed_prefix ~= "safenum" and ( val == nil or type( val ) ~= "number" ) then piece.s = "safenum(" .. piece.s .. ")" end
end
else
Hekili:Error( "Unable to compile '" .. ( piece.s ):gsub("%%","%%%%") .. "' - " .. warn .. " (loadstring-n)\nFrom: " .. esString:gsub( "%%", "%%%%" ) )
end
end
piece.r = nil
elseif not numeric and ( not prev or ( prev.t == "op" and not math_ops[ prev.a ] and not equality[ prev.a ] ) ) and ( not next or ( next.t == "op" and not math_ops[ next.a ] and not equality[ next.a ] ) ) then
-- This expression is not having math operations performed on it.
-- Let's make sure it's a boolean.
if piece.s:find("^variable") then
if piece.s:sub(1, 1) == "(" then piece.s = "safebool" .. piece.s
else piece.s = "safebool(" .. piece.s .. ")" end
else
local func, warn = Hekili:Loadstring( "return " .. ( SimToLua( piece.s ) or "" ) )
if func then
setfenv( func, state )
state:SetDefaultVariable( 1 )
local pass, val = pcall( func )
state:SetDefaultVariable( 0 )
if not pass and not piece.s:match("variable") then
local safepiece = piece.s:gsub( "%%", "%%%%" )
Hekili:Error( "Unable to compile '" .. safepiece:gsub("%%", "%%%%") .. "' - " .. val .. " (pcall-b)\nFrom: " .. esString:gsub( "%%", "%%%%" ) )
else
if trimmed_prefix ~= "safebool" and ( val == nil or type( val ) == "number" ) then
piece.s = "safebool(" .. piece.s .. ")"
end
end
else
Hekili:Error( "Unable to compile '" .. ( piece.s ):gsub("%%","%%%%") .. "' - " .. warn .. " (loadstring-b)." )
end
end
piece.r = nil
end
end
if trimmed_prefix then
piece.s = trimmed_prefix .. piece.s
end
output = output .. piece.s
i = i + 1
end
if bracketed then output = "(" .. output .. ")" end
if ands then output = output:gsub( "&", " and " ) end
if ors then output = output:gsub( "|", " or " ) end
if nots then output = output:gsub( "!", " not " ) end
if abss then output = output:gsub( "@", " abs" ) end
-- output = output:gsub( " ", " " )
-- output = output:gsub( "not (safenum(", "safenum(not (" )
-- output = output:gsub( "not safebool(", "safebool(not " )
output = output:gsub( "!safenum(%b())", "safenum(!%1)" )
output = output:gsub( "@safebool", "@safenum" )
output = output:gsub( "!%((%b())%)", "!%1" )
esDepth = esDepth - 1
return output
end
end
-- Convert SimC syntax to Lua conditionals.
local function SimCToSnapshot( str, modifier )
-- If no conditions were provided, function should return true.
if not str or str == '' then return nil end
if type( str ) == 'number' then return str end
str = str:trim()
-- Strip comments.
str = str:gsub("^%-%-.-\n", "")
-- Replace '!' with ' not '.
-- str = forgetMeNots( str )
str = SimcWithResources( str )
-- Replace '%' for division with actual division operator '/'.
-- str = str:gsub("%%", "/")
-- Replace '&' with ' and '.
-- str = str:gsub("&", " and ")
-- Replace '|' with ' or '.
-- str = str:gsub("||", " or "):gsub("|", " or ")
--[[ if not modifier then
-- Replace assignment '=' with comparison '=='
str = str:gsub("([^=])=([^=])", "%1==%2" )
-- Fix any conditional '==' that got impacted by previous.
str = str:gsub("==+", "==")
str = str:gsub(">=+", ">=")
str = str:gsub("<=+", "<=")
str = str:gsub("!=+", "~=")
str = str:gsub("~=+", "~=")
end