-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TargetSkill.lua
1209 lines (1029 loc) · 43 KB
/
TargetSkill.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
------------------------
-- Validation Options --
------------------------
if not RAIL.Validate.SkillOptions then
RAIL.Validate.SkillOptions = {is_subtable = true}
end
RAIL.Validate.SkillOptions.BuffBasePriority = {"number",40,0}
-- Mental Charge
RAIL.Validate.SkillOptions[8004] = {
MaxFailures = {"number",4},
PriorityOffset = {"number",15},
}
--------------------------
-- Skill Initialization --
--------------------------
RAIL.Event["AI CYCLE"]:Register(-35, -- Priority
"Skill initialization", -- Handler name
1, -- Max runs
function()
-- Map of patterns to event names
local pattern_event_map = {
AreaOfEffect = "SKILL INIT/AREA EFFECT",
Attack = "SKILL INIT/ATTACK", -- also fires SKILL INIT/OFFENSIVE
Buff = "SKILL INIT/BUFF",
Debuff = "SKILL INIT/OFFENSIVE",
--Defense = "SKILL INIT/DEFENSE",
--Emergency = "SKILL INIT/EMERGENCY",
HealOwner = "SKILL INIT/HEAL OWNER",
HealSelf = "SKILL INIT/HEAL SELF",
PartySupport = "SKILL INIT/PARTY BUFF",
--Pushback = "SKILL INIT/PUSHBACK",
--Recover = "SKILL INIT/RECOVER",
Reveal = "SKILL INIT/REVEAL", -- also fires SKILL INIT/OFFENSIVE
}
-- Loop through each skill
for skill_type,skill in RAIL.Self.Skills do
-- Only initialized skills with named script AIs
if type(skill_type) == "string" then
-- Loop through each pattern-event mapping
for pattern,event in pairs(pattern_event_map) do
-- Check if the skill type matches a pattern
if string.find(skill_type,pattern,nil,true) then
-- Initialize the skill
RAIL.Event["SKILL INIT/GENERIC/PRE"]:Fire(skill)
RAIL.Event[event]:Fire(skill)
RAIL.Event["SKILL INIT/GENERIC/POST"]:Fire(skill)
-- Stop looping through pattern-event mappings
break
end
end -- pattern,event in pairs(pattern_event_map)
end -- type(skill_type) == "string"
end -- skill_type,skill in RAIL.Self.Skills
end)
do
local byID = RAIL.Validate.SkillOptions
-- Default options that all skills use
local defaults = {is_subtable = true,
Enabled = {"boolean",true},
Name = {"string",nil}, -- default set by init function
Condition = {"function",nil,unsaved=true}, -- default set by init function
ReservedSP = {"number",0,0},
ReservedSPisPercent = {"boolean",false},
}
RAIL.Event["SKILL INIT/GENERIC/POST"]:Register(0, -- Priority
"Post Init", -- Handler name
-1, -- Max runs (negative means infinite)
function(self,skill)
-- Copy validation options from defaults, but don't overwrite
byID[skill.ID] = Table.DeepCopy(defaults,byID[skill.ID],false)
-- Set the default skill name
byID[skill.ID].Name[2] = AllSkills[skill.ID]:GetName()
-- Rework the skill to now use name from the state file
AllSkills[skill.ID].GetName = function(self)
return RAIL.State.SkillOptions[self.ID].Name
end
-- Get the name, just so it'll show up in the state file
AllSkills[skill.ID]:GetName()
-- Set the default condition function
byID[skill.ID].Condition[2] = AllSkills[skill.ID].Condition
end)
end
--------------------------
-- Skill Selection Base --
--------------------------
do
-- Function to call a new event for each skill of a specified type
local function FireSkillEvent(event_name,skill_name,...)
-- Loop through all skills that match the skill name
for idx,skill in FindPairs(RAIL.Self.Skills,skill_name,nil,true) do
-- Check that the skill is enabled
if
RAIL.State.SkillOptions[skill.ID] and
RAIL.State.SkillOptions[skill.ID].Enabled
then
-- Fire the event
RAIL.Event[event_name]:Fire(skill,unpack(arg))
end
end
end
RAIL.Event["AI CYCLE"]:Register(810, -- Priority
"Casting Terminate", -- Handler name
-1, -- Max runs (negative means infinite)
function() -- Handler function
if RAIL.SkillState == RAIL.SkillState.CASTING then
-- Too spammy
--RAIL.LogT(7,"Casting motion prevents action; cycle terminating after data collection.")
-- Discontinue this AI CYCLE
return false
end
end)
-- Check emergency skills
RAIL.Event["AI CYCLE"]:Register(830, -- Priority
"Emergency Check", -- Handler name
-1, -- Max runs (negative means infinite)
function()
-- Fire an event to check emergency skills
FireSkillEvent("TARGET SELECT/SKILL/EMERGENCY","Emergency")
-- Check if a skill was selected
if RAIL.Target.Skill then
-- Log it
RAIL.LogT(7, "Urgently casting {1}; cycle terminating after data collection.",RAIL.Target.Skill)
-- Skip selection routines
return true,900,1000
end
end)
-- Check non-targeted skills
RAIL.Event["TARGET SELECT/PRE"]:Register(0, -- Priority
"Non-target", -- Handler name
-1, -- Max runs (negative means infinite)
function()
-- Check if skills are ready
if RAIL.SkillState ~= RAIL.SkillState.READY then
-- Skills aren't ready
return false
end
-- Check if a skill was manually requested
if RAIL.Target.Skill and RAIL.Target.Skill.Manual then
-- Don't try a new skill
return false
end
-- Check for party support skills
FireSkillEvent("TARGET SELECT/SKILL/PARTY BUFF", "PartySupport", 0)
-- Check for heal skills
FireSkillEvent("TARGET SELECT/SKILL/HEAL OWNER", "HealOwner", 0)
FireSkillEvent("TARGET SELECT/SKILL/HEAL SELF", "HealSelf", 0)
end)
-- Check buff skills
RAIL.Event["TARGET SELECT/POST"]:Register(10, -- Priority
"Buff skill select", -- Handler name
-1, -- Max runs (negative means infinite)
function()
-- Check if skills are ready
if RAIL.SkillState ~= RAIL.SkillState.READY then
-- Skills aren't ready
return
end
-- Check if a skill was manually requested
if RAIL.Target.Skill and RAIL.Target.Skill.Manual then
-- Don't try a new skill
return
end
-- Check buff skills
-- NOTE: Some buff skill conditions require knowledge of decided movement
FireSkillEvent("TARGET SELECT/SKILL/BUFF","Buff")
end)
-- Check that the AI won't initiate a cast-time skill while chasing
RAIL.Event["TARGET SELECT/POST"]:Register(50, -- Priority
"Walk+cast time", -- Handler name
-1,
function()
-- Check that a skill was selected
if not RAIL.Target.Skill then
return
end
-- Check that a chase location was selected
if not RAIL.Target.Chase then
return
end
-- Check if the skill has a cast time
if RAIL.Target.Skill[1].CastTime > 0 then
-- Remove the skill
RAIL.Target.Skill = nil
end
end)
RAIL.Event["IDLE"]:Register(20, -- Priority
"Idle Skills", -- Handler name
-1, -- Max runs (negative means infinite)
function(self,idletime)
if RAIL.SkillState == RAIL.SkillState.READY then
FireSkillEvent("TARGET SELECT/SKILL/HEAL OWNER", "HealOwner", idletime)
FireSkillEvent("TARGET SELECT/SKILL/HEAL SELF", "HealSelf", idletime)
-- Check if a skill was selected
if RAIL.Target.Skill then
-- TODO: Log it
-- Since a skill target was found, don't continue processing
return false
end
end
end)
RAIL.Event["AI CYCLE"]:Register(990, -- Priority
"Skill SP", -- Handler name
-1, -- Max runs (negative means infinite)
function()
-- Checking SP only applies when a skill is selected
local skill = RAIL.Target.Skill
if not skill then
return
end
-- Check to see if this was manually requested
if skill.Manual then
-- Let the user get a no SP message if there's not enough SP
return
end
-- Check if there is enough SP left
if RAIL.Self:GetUsableSP(skill[1]) <= skill[1].SPCost + 1 then
-- Don't actually use a skill
RAIL.Target.Skill = nil
end
end)
end
---------------------------
-- Offensive Skills Base --
---------------------------
do
-- State validation options
local defaults = {is_subtable = true,
MaxFailures = {"number",10,1},
PriorityOffset = {"number",0},
}
-- List of skills
local offensive_skills = Table.New()
-- Callbacks for the skills
local function SuccessCallback(ticks,skill,target)
-- Reset the failure count
target.BattleOpts[skill.ID .. "failures"] = 0
-- Set the next time that the skill should be cast
if skill.Duration > 0 then
target.BattleOpts[skill.ID .. "next"] = GetTick() - ticks + skill.Duration - skill.CastTime
end
end
local function FailureCallback(ticks,skill,target)
-- Increment the failure count if the skill hasn't been confirmed
local key = skill.ID .. "failures"
target.BattleOpts[key] = (target.BattleOpts[key] or 0) + 1
end
-- Helper function to get priority level of a skill against an actor
local function GetPriority(self,actor)
local actor_prio = actor.BattleOpts.Priority
local skill_prio = RAIL.State.SkillOptions[self.ID].PriorityOffset
return actor_prio + skill_prio
end
-- Initialization
RAIL.Event["SKILL INIT/OFFENSIVE"]:Register(0, -- Priority
"Offensive init", -- Handler name
-1, -- Max runs (infinite)
function(self,skill)
local byID = RAIL.Validate.SkillOptions
-- Copy validation options from defaults, but don't overwrite
byID[skill.ID] = Table.DeepCopy(defaults,byID[skill.ID],false)
-- Add to the offensive skills table
offensive_skills:Append(skill)
-- Add the callbacks
RAIL.SkillState.Callbacks:Add(skill, -- Skill to add callbacks to
SuccessCallback,
FailureCallback,
true) -- Persist past the first call
-- Add the GetPriority support function
if not skill[1] then
-- Skill level not selectable
skill.GetPriority = GetPriority
else
-- Skill level selectable
local i=1
while skill[i] do
skill[i].GetPriority = GetPriority
i = i + 1
end
end
end)
-- Skill selection
RAIL.Event["TARGET SELECT/ENEMY/SKILL"]:Register(0, -- Priority
"Offensive select", -- Handler name
-1, -- Max runs
function(self,actor,can_chase)
-- Ensure there are offensive skills
if offensive_skills:GetN() < 1 then
-- Don't check this anymore
self.RunsLeft = 0
end
-- Loop through each offensive skill
for i=1,offensive_skills:GetN() do
local skill = offensive_skills[i]
-- Check that the skill is enabled
if RAIL.State.SkillOptions[skill.ID].Enabled then
-- Check if skill level is selectable and there's not enough SP for
-- maximum level
local usable_sp = RAIL.Self:GetUsableSP(skill)
if skill[1] and usable_sp <= skill.SPCost + 1 then
-- Find the highest level available with the usable SP
for i=skill.Level,1,-1 do
if skill[i] and usable_sp > skill.SPCost then
skill = skill[i]
break
end
end
end
-- Fire an event for this potential skill-target combo
local r1,r2 = RAIL.Event["TARGET SELECT/ENEMY/SKILL/OFFENSIVE"]:Fire(skill,actor,can_chase)
-- Set the return values of this event based on the child event
if r1 then
self.Event.RetVal[1] = true
end
if r2 then
if self.Event.RetVal[1] == nil then self.Event.RetVal[1] = false end
self.Event.RetVal[2] = true
end
end
end -- i=1,offensive_skills:GetN()
end)
end
RAIL.Event["TARGET SELECT/ENEMY/SKILL/OFFENSIVE"]:Register(0, -- Priority
"Allowed", -- Handler name
-1, -- Max runs (infinite)
function(self,skill,actor)
-- Check that the skill is allowed
-- NOTE: Pretend skill level is 10 for generic offensive skills; attack
-- skills will add another check into this handler chain
if not actor:IsSkillAllowed(10) then
-- Don't continue this event
return false
end
end)
RAIL.Event["TARGET SELECT/ENEMY/SKILL/OFFENSIVE"]:Register(10, -- Priority
"Condition", -- Handler name
-1, -- Max runs (infinite)
function(self,skill,actor)
if not RAIL.State.SkillOptions[skill.ID].Condition(RAIL._G,actor) then
-- Don't continue this event
return false
end
end)
RAIL.Event["TARGET SELECT/ENEMY/SKILL/OFFENSIVE"]:Register(20, -- Priority
"Failures", -- Handler name
-1, -- Max runs (infinite)
function(self,skill,actor)
-- Check that the skill hasn't failed too many times
if (actor.BattleOpts[skill.ID .. "failures"] or 0) >= RAIL.State.SkillOptions[skill.ID].MaxFailures then
-- Don't continue this event
return false
end
end)
RAIL.Event["TARGET SELECT/ENEMY/SKILL/OFFENSIVE"]:Register(25, -- Priority
"Duration", -- Handler name
-1, -- Max runs (infinite)
function(self,skill,actor)
-- Ensure there's a duration
if skill.Duration < 1 then
return
end
-- Check if the duration has expired
if (actor.BattleOpts[skill.ID .. "next"] or 0) > GetTick() then
-- Don't continue this event
return false
end
end)
RAIL.Event["TARGET SELECT/ENEMY/SKILL/OFFENSIVE"]:Register(40, -- Priority
"Range/chase", -- Handler name
-1, -- Max runs (infinite)
function(self,skill,actor,can_chase)
-- Get the skill range
local srange = skill.Range
-- If the actor was allowed it can be chased
if can_chase then
-- Set the second return value of this event to true
self.Event.RetVal[2] = true
-- Ensure that it will unpack properly
if self.Event.RetVal[1] == nil then self.Event.RetVal[1] = false end
-- Fire an event to add the target to the ChaseMap
RAIL.Event["TARGET SELECT/ENEMY/CHASE"]:Fire(actor,
srange,
skill:GetPriority(actor),
true) -- srange uses PythagDistance
end
-- Check the range
if RAIL.Self:DistanceTo(actor) > srange then
-- Don't continue this event
return false
end
end)
RAIL.Event["TARGET SELECT/ENEMY/SKILL/OFFENSIVE"]:Register(50, -- Priority
"Acceptable", -- Handler name
-1, -- Max runs (infinite)
function(self,skill,actor)
-- Set the 1st return value of this event to true
self.Event.RetVal[1] = true
-- If no other skill has been selected yet, choose this
if not RAIL.Target.Skill then
RAIL.Target.Skill = { skill, actor }
return false
end
end)
RAIL.Event["TARGET SELECT/ENEMY/SKILL/OFFENSIVE"]:Register(100, -- Priority
"Priority sieve", -- Handler name
-1, -- Max runs (infinite)
function(self,skill,actor)
-- Get the priority of the skills
local new_prio = skill:GetPriority(actor)
local old_prio = RAIL.Target.Skill[1]:GetPriority(RAIL.Target.Skill[2], -- X or actor
RAIL.Target.Skill[3]) -- Y
-- Check if the new skill is worse
if new_prio < old_prio then
-- Don't continue this event
return false
end
-- Check if the new skill is better
if new_prio > old_prio then
-- Set the skill target
RAIL.Target.Skill = { skill, actor }
return false
end
end)
------------------------------
-- Offensive Skiils: Attack --
------------------------------
do
local function SuccessCallback(ticks,skill,target)
-- Increment skill counter
-- NOTE: This is checked in Actor.lua's IsSkillAllowed()
target.BattleOpts.CastsAgainst = (target.BattleOpts.CastsAgainst or 0) + 1
end
local skill_ids = {}
local have_attacks = false
RAIL.Event["SKILL INIT/ATTACK"]:Register(0, -- Priority
"Attack skill init", -- Handler name
-1, -- Max runs (infinite)
function(self,skill)
-- Count attacks as a offensive skills too
RAIL.Event["SKILL INIT/OFFENSIVE"]:Fire(skill)
-- Save this skill's ID so we can later add success callbacks
skill_ids[skill.ID] = true
-- Register that we do indeed have attacks
have_attacks = true
end)
RAIL.Event["TARGET SELECT/ENEMY/SKILL/OFFENSIVE"]:Register(0, -- Priority
"Attack Skill Allowed", -- Handler name
-1, -- Max runs (infinite)
function(self,skill,actor)
-- Ensure the skill is an attack skill
if not skill_ids[skill.ID] then
-- Don't affect this chain
return
end
-- Check that the skill is allowed
local allowed,level = actor:IsSkillAllowed(skill.Level)
if not allowed then
-- Don't continue this event
return false
end
if level ~= skill.Level then
-- Change the skill level that will be used
self.Event.Args[1] = AllSkills[skill.ID][level]
end
end)
RAIL.Event["TARGET SELECT/POST"]:Register(0, -- Priority
"Attack callback", -- Handler name
-1, -- Max runs (negative means infinite)
function(self)
-- Ensure that there are attack skills to check
if not have_attacks then
-- Don't check this again
self.RunsLeft = 0
return
end
-- Check that a skill was selected
if not RAIL.Target.Skill then
return
end
-- Get the selected skill
local skill = RAIL.Target.Skill[1]
-- Check if the selected skill is an attack skill
if skill_ids[RAIL.Target.Skill[1].ID] then
-- Add the callback for the next cast
RAIL.SkillState.Callbacks:Add(skill,
SuccessCallback,
nil,
false) -- not persistent
end
end)
end
------------------------------
-- Offensive Skills: Reveal --
------------------------------
do
local skill_ids = {}
RAIL.Event["SKILL INIT/REVEAL"]:Register(0, -- Priority
"Reveal init", -- Handler name
-1, -- Max runs (infinite)
function(self,skill)
-- Save this skill as a revealing skill
skill_ids[skill.ID] = true
-- Count reveal as an offensive skill too
return RAIL.Event["SKILL INIT/OFFENSIVE"]:Fire(skill)
end)
RAIL.Event["TARGET SELECT/ENEMY/SKILL/OFFENSIVE"]:Register(5, -- Priority
"Hiding/reveal", -- Handler name
-1, -- Max runs (infinite)
function(self,skill,actor)
-- Check if the actor is hiding and the skill isn't a revealer
if actor.Hide and not skill_ids[skill.ID] then
-- Don't continue this event
return false
end
end)
end
-----------------
-- Buff Skills --
-----------------
do
-- State validation options
local defaults = {is_subtable = true,
MaxFailures = {"number",10,1},
PriorityOffset = {"number",0},
NextCastTime = {"number",0},
}
-- Closure to keep track of skill failures
local failures = {}
-- Function to get the buff priority
local function GetPriority(self)
local base_prio = RAIL.State.SkillOptions.BuffBasePriority
local prio_offset = RAIL.State.SkillOptions[self.ID].PriorityOffset
return base_prio + prio_offset
end
local function SuccessCallback(ticks,skill)
-- Reset the failure count
failures[skill.ID] = 0
-- Set the next time we can use the buff
RAIL.State.SkillOptions[skill.ID].NextCastTime = GetTick() - ticks + skill.Duration - skill.CastTime
end
local function FailureCallback(ticks,skill)
-- Increment the failure count if the skill hasn't been confirmed
failures[skill.ID] = failures[skill.ID] + 1
end
-- Initialization
RAIL.Event["SKILL INIT/BUFF"]:Register(0, -- Priority
"Buff init", -- Handler name
-1, -- Max runs (infinite)
function(self,skill)
local byID = RAIL.Validate.SkillOptions
-- Copy validation options from defaults, but don't overwrite
byID[skill.ID] = Table.DeepCopy(defaults,byID[skill.ID],false)
-- Check if our ID has changed, which indicates that we'll have to recast
-- buffs
if RAIL.Self.ID ~= RAIL.State.Information.SelfID then
RAIL.State.SkillOptions[skill.ID].NextCastTime = 0
end
-- Add callbacks to the skill
failures[skill.ID] = 0
RAIL.SkillState.Callbacks:Add(skill, -- Skill to add callbacks to
SuccessCallback,
FailureCallback,
true) -- Persist past the first call
-- Add the GetPriority support function
if not skill[1] then
-- Skill level not selectable
skill.GetPriority = GetPriority
else
-- Skill level selectable
local i=1
while skill[i] do
skill[i].GetPriority = GetPriority
i = i + 1
end
end
end)
-- Skill selection
RAIL.Event["TARGET SELECT/SKILL/BUFF"]:Register(0, -- Priority
"Failures check", -- Handler name
-1, -- Max runs
function(self,skill,idleticks)
-- Check if the skill has failed too many times
if failures[skill.ID] >= RAIL.State.SkillOptions[skill.ID].MaxFailures then
-- Probably don't have the skill; stop trying
return false
end
end)
end
RAIL.Event["TARGET SELECT/SKILL/BUFF"]:Register(10, -- Priority
"Next cast", -- Handler name
-1, -- Max runs
function(self,skill,idleticks)
-- Get the time that the buff will wear off
local next_cast = RAIL.State.SkillOptions[skill.ID].NextCastTime
-- Don't use the buff if it's still active
if GetTick() < next_cast then
return false
end
end)
RAIL.Event["TARGET SELECT/SKILL/BUFF"]:Register(20, -- Priority
"Condition", -- Handler name
-1, -- Max runs
function(self,skill,idleticks)
-- Check any custom condition
if not RAIL.State.SkillOptions[skill.ID].Condition(RAIL._G,nil) then
return false
end
end)
RAIL.Event["TARGET SELECT/SKILL/BUFF"]:Register(50, -- Priority
"Acceptable", -- Handler name
-1, -- Max runs
function(self,skill,idleticks)
-- If there's not a selected skill, use this one
if not RAIL.Target.Skill then
RAIL.Target.Skill = { skill, RAIL.Self }
return false
end
end)
RAIL.Event["TARGET SELECT/SKILL/BUFF"]:Register(60, -- Priority
"Priority", -- Handler name
-1, -- Max runs
function(self,skill,idleticks)
-- Get the priority levels
local new_prio = skill:GetPriority()
local old_prio = RAIL.Target.Skill[1]:GetPriority(RAIL.Target.Skill[2], -- X or actor
RAIL.Target.Skill[3]) -- Y
-- Check if this skill is lower priority
if new_prio < old_prio then
-- Interrupt this event
return false
end
-- Check if this skill is highest priority
if new_prio > old_prio then
-- Set the skill to this one and then interrupt the event
RAIL.Target.Skill = { skill, RAIL.Self }
return false
end
end)
---------------------
-- Heal Self/Other --
---------------------
do
-- Defaults
local defaults = {
Priority = {"number",50},
MaxFailures = {"number",10,1},
EstimateFutureTicks = {"number",0,0},
OnlyAfterIdleFor = {"number",3000,0},
}
-- Closure to keep track of skill failures
local failures = {}
-- Function to get the buff priority
local function GetPriority(skill)
return RAIL.State.SkillOptions[skill.ID].Priority
end
local function SuccessCallback(ticks,skill)
-- Reset the failure count
failures[skill.ID] = 0
end
local function FailureCallback(ticks,skill)
-- Increment the failure count if the skill hasn't been confirmed
failures[skill.ID] = failures[skill.ID] + 1
end
-- Generate healing skill
local function Generate(event_name,option_name,heal_actor,default_hp,default_percent)
-- Option names
local percent = option_name .. "isPercent"
local init = "SKILL INIT/" .. event_name
RAIL.Event[init]:Register(0,
"heal skill init",
-1,
function(self,skill)
local byID = RAIL.Validate.SkillOptions
-- Copy validation options from defaults, but don't overwrite
byID[skill.ID] = Table.DeepCopy(defaults,byID[skill.ID],false)
-- Setup specific validation
byID[skill.ID][option_name] = {"number",default_hp,0}
byID[skill.ID][percent] = {"boolean",default_percent}
-- Add callbacks to the skill
if failures[skill.ID] == nil then
failures[skill.ID] = 0
RAIL.SkillState.Callbacks:Add(skill, -- Skill to add callbacks to
SuccessCallback,
FailureCallback,
true) -- Persist past the first call
end
-- Add the GetPriority support function
if not skill[1] then
-- Skill level not selectable
skill.GetPriority = GetPriority
else
-- Skill level selectable
local i=1
while skill[i] do
skill[i].GetPriority = GetPriority
i = i + 1
end
end
end)
local select = "TARGET SELECT/SKILL/" .. event_name
RAIL.Event[select]:Register(0, -- Priority
"Failures check", -- Handler name
-1, -- Max runs
function(self,skill,idleticks)
-- Check if the skill has failed too many times
if failures[skill.ID] >= RAIL.State.SkillOptions[skill.ID].MaxFailures then
-- Probably don't have the skill; stop trying
return false
end
end)
RAIL.Event[select]:Register(10,
"Idle time",
-1,
function(self,skill,idleticks)
if (idleticks or 0) < RAIL.State.SkillOptions[skill.ID].OnlyAfterIdleFor then
return false
end
end)
RAIL.Event[select]:Register(20,
"HP check",
-1,
function(self,skill)
local hp = heal_actor.HP[-RAIL.State.SkillOptions[skill.ID].EstimateFutureTicks]
local target = RAIL.State.SkillOptions[skill.ID][option_name]
if RAIL.State.SkillOptions[skill.ID][percent] then
hp = math.floor(hp / heal_actor:GetMaxHP() * 100)
target = math.min(99,target)
end
if hp > target then
return false
end
end)
RAIL.Event[select]:Register(30,
"Condition",
-1,
function(self,skill)
-- Check any custom condition
if not RAIL.State.SkillOptions[skill.ID].Condition(RAIL._G,heal_actor) then
return false
end
end)
if heal_actor ~= RAIL.Self then
RAIL.Event[select]:Register(40,
"Range/chase",
-1,
function(self,skill)
-- Get the skill range
local srange = skill.Range
-- Fire an event to add the target to the ChaseMap
RAIL.Event["TARGET SELECT/ENEMY/CHASE"]:Fire(heal_actor,
srange,
skill:GetPriority(),
true) -- srange uses PythagDistance
-- Check the range
if RAIL.Self:DistanceTo(heal_actor) > srange then
-- Don't continue this event
return false
end
end)
end
RAIL.Event[select]:Register(50, -- Priority
"Acceptable", -- Handler name
-1, -- Max runs
function(self,skill)
-- If there's not a selected skill, use this one
if not RAIL.Target.Skill then
RAIL.Target.Skill = { skill, heal_actor }
return false
end
end)
RAIL.Event[select]:Register(60, -- Priority
"Priority", -- Handler name
-1, -- Max runs
function(self,skill,idleticks)
-- Get the priority levels
local new_prio = skill:GetPriority()
local old_prio = RAIL.Target.Skill[1]:GetPriority(RAIL.Target.Skill[2], -- X or actor
RAIL.Target.Skill[3]) -- Y
-- Check if this skill is lower priority
if new_prio < old_prio then
-- Interrupt this event
return false
end
-- Check if this skill is highest priority
if new_prio > old_prio then
-- Set the skill to this one and then interrupt the event
RAIL.Target.Skill = { skill, heal_actor }
return false
end
end)
end
-- Generate HealOwner and HealSelf
RAIL.Event["AI CYCLE"]:Register(-42,
"Heal skill AI generation",
1,
function()
Generate("HEAL OWNER","OwnerHP",RAIL.Owner, 50, true)
Generate("HEAL SELF", "SelfHP", RAIL.Self, 0, false)
end)
end
-----------------
-- Party Buffs --
-----------------
do
-- State validation options
local defaults = {is_subtable = true,
MaxFailures = {"number",10,1},
PriorityOffset = {"number",0},
AutoIncludeOwner = {"boolean",true},
}
-- Table of targets for each skill
local targets = {}
-- Number of failures by skill ID
local failures = {}
-- Boolean to set when a party support buff is initialized
local have_partybuff = false
-- Function to get the buff priority
local function GetPriority(self,actor)
local base_prio
if actor.BattleOpts.Priority == RAIL.State.ActorOptions.Default.Priority then
base_prio = RAIL.State.SkillOptions.BuffBasePriority
else
base_prio = actor.BattleOpts.Priority
end
local prio_offset = RAIL.State.SkillOptions[self.ID].PriorityOffset
return base_prio + prio_offset
end
local function SuccessCallback(ticks,skill,target)
-- Reset the failure count
failures[skill.ID] = 0
-- Set the next time we can use the buff
target.BattleOpts[skill.ID .. "next"] = GetTick() - ticks + skill.Duration - skill.CastTime
end
local function FailureCallback(ticks,skill,target)
-- Increment the failure count if the skill hasn't been confirmed
failures[skill.ID] = failures[skill.ID] + 1
end
-- Initialization
RAIL.Event["SKILL INIT/PARTY BUFF"]:Register(0, -- Priority
"Init", -- Handler name
-1, -- Max runs (infinite)
function(self,skill)
local byID = RAIL.Validate.SkillOptions
-- Copy validation options from defaults, but don't overwrite
byID[skill.ID] = Table.DeepCopy(defaults,byID[skill.ID],false)
-- Create the targets list
targets[skill.ID] = List.New()
-- Check if we should add the owner right away
if RAIL.State.SkillOptions[skill.ID].AutoIncludeOwner then
targets[skill.ID]:PushRight(RAIL.Owner)
end
-- Set initial failure count
failures[skill.ID] = 0