forked from wowsims/mop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaura_helpers.go
More file actions
834 lines (700 loc) · 24.5 KB
/
aura_helpers.go
File metadata and controls
834 lines (700 loc) · 24.5 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
// Functions for creating common types of auras.
package core
import (
"math"
"time"
"github.com/wowsims/mop/sim/core/stats"
)
type AuraCallback uint16
func (c AuraCallback) Matches(other AuraCallback) bool {
return (c & other) != 0
}
const (
CallbackEmpty AuraCallback = 0
CallbackOnSpellHitDealt AuraCallback = 1 << iota
CallbackOnSpellHitTaken
CallbackOnPeriodicDamageDealt
CallbackOnHealDealt
CallbackOnPeriodicHealDealt
CallbackOnCastComplete
CallbackOnApplyEffects
CallbackLast
)
type ProcHandler func(sim *Simulation, spell *Spell, result *SpellResult)
type ProcExtraCondition func(sim *Simulation, spell *Spell, result *SpellResult) bool
type ProcTrigger struct {
Name string
ActionID ActionID
MetricsActionID ActionID
Duration time.Duration
Callback AuraCallback
ProcMask ProcMask
ProcMaskExclude ProcMask
SpellFlags SpellFlag
SpellFlagsExclude SpellFlag
Outcome HitOutcome
RequireDamageDealt bool
ProcChance float64
DPM *DynamicProcManager
ICD time.Duration
Handler ProcHandler
TriggerImmediately bool // If false (default), the handler will be called one spell batch window later for improved realism.
ClassSpellMask int64
ExtraCondition ProcExtraCondition
}
func (procAura *Aura) AttachProcTriggerCallback(unit *Unit, config ProcTrigger) {
var icd Cooldown
if config.ICD != 0 {
icd = Cooldown{
Timer: unit.NewTimer(),
Duration: config.ICD,
}
procAura.Icd = &icd
}
var dpm *DynamicProcManager
if config.DPM != nil {
dpm = config.DPM
procAura.Dpm = config.DPM
}
handler := config.Handler
applyHandler := func(sim *Simulation, spell *Spell, result *SpellResult) {
if config.TriggerImmediately {
handler(sim, spell, result)
return
}
pa := sim.GetConsumedPendingActionFromPool()
pa.NextActionAt = sim.CurrentTime + SpellBatchWindow
pa.Priority = ActionPriorityDOT
// Due to the result struct possibly being disposed of after this handler is triggered
// we need to clone the result to make sure the values don't get overwritten
newResult := spell.CloneResult(result)
pa.OnAction = func(sim *Simulation) {
handler(sim, spell, newResult)
spell.DisposeResult(newResult)
}
sim.AddPendingAction(pa)
}
callback := func(aura *Aura, sim *Simulation, spell *Spell, result *SpellResult) {
if config.SpellFlags != SpellFlagNone && !spell.Flags.Matches(config.SpellFlags) {
return
}
if config.SpellFlagsExclude != SpellFlagNone && spell.Flags.Matches(config.SpellFlagsExclude) {
return
}
if config.ClassSpellMask > 0 && config.ClassSpellMask&spell.ClassSpellMask == 0 {
return
}
if config.ProcMaskExclude != ProcMaskUnknown && spell.ProcMask.Matches(config.ProcMaskExclude) {
return
}
if config.ProcMask != ProcMaskUnknown && !spell.ProcMask.Matches(config.ProcMask) {
return
}
if config.Outcome != OutcomeEmpty && !result.Outcome.Matches(config.Outcome) {
return
}
if config.RequireDamageDealt && result.Damage == 0 {
return
}
if icd.Duration != 0 && !icd.IsReady(sim) {
return
}
if config.ExtraCondition != nil && !config.ExtraCondition(sim, spell, result) {
return
}
if config.ProcChance != 1 && sim.RandomFloat(config.Name) > config.ProcChance {
return
} else if dpm != nil && !dpm.Proc(sim, spell.ProcMask, config.Name) {
return
}
if icd.Duration != 0 {
icd.Use(sim)
}
applyHandler(sim, spell, result)
}
if config.ProcChance == 0 {
config.ProcChance = 1
}
if config.Callback.Matches(CallbackOnSpellHitDealt) {
procAura.OnSpellHitDealt = callback
}
if config.Callback.Matches(CallbackOnSpellHitTaken) {
procAura.OnSpellHitTaken = callback
}
if config.Callback.Matches(CallbackOnPeriodicDamageDealt) {
procAura.OnPeriodicDamageDealt = callback
}
if config.Callback.Matches(CallbackOnHealDealt) {
procAura.OnHealDealt = callback
}
if config.Callback.Matches(CallbackOnPeriodicHealDealt) {
procAura.OnPeriodicHealDealt = callback
}
if config.Callback.Matches(CallbackOnCastComplete) {
procAura.OnCastComplete = func(aura *Aura, sim *Simulation, spell *Spell) {
if config.SpellFlags != SpellFlagNone && !spell.Flags.Matches(config.SpellFlags) {
return
}
if config.ClassSpellMask > 0 && config.ClassSpellMask&spell.ClassSpellMask == 0 {
return
}
if config.ProcMask != ProcMaskUnknown && !spell.ProcMask.Matches(config.ProcMask) {
return
}
if config.ProcMaskExclude != ProcMaskUnknown && spell.ProcMask.Matches(config.ProcMaskExclude) {
return
}
if icd.Duration != 0 && !icd.IsReady(sim) {
return
}
if config.ExtraCondition != nil && !config.ExtraCondition(sim, spell, nil) {
return
}
if config.ProcChance != 1 && sim.RandomFloat(config.Name) > config.ProcChance {
return
}
if icd.Duration != 0 {
icd.Use(sim)
}
applyHandler(sim, spell, nil)
}
}
if config.Callback.Matches(CallbackOnApplyEffects) {
procAura.OnApplyEffects = func(aura *Aura, sim *Simulation, target *Unit, spell *Spell) {
if config.SpellFlags != SpellFlagNone && !spell.Flags.Matches(config.SpellFlags) {
return
}
if config.ClassSpellMask > 0 && config.ClassSpellMask&spell.ClassSpellMask == 0 {
return
}
if config.ProcMask != ProcMaskUnknown && !spell.ProcMask.Matches(config.ProcMask) {
return
}
if config.ProcMaskExclude != ProcMaskUnknown && spell.ProcMask.Matches(config.ProcMaskExclude) {
return
}
if icd.Duration != 0 && !icd.IsReady(sim) {
return
}
emptyResult := spell.NewResult(target)
defer spell.DisposeResult(emptyResult)
if config.ExtraCondition != nil && !config.ExtraCondition(sim, spell, emptyResult) {
return
}
if config.ProcChance != 1 && sim.RandomFloat(config.Name) > config.ProcChance {
return
}
if icd.Duration != 0 {
icd.Use(sim)
}
applyHandler(sim, spell, emptyResult)
}
}
}
func (unit *Unit) MakeProcTriggerAura(config ProcTrigger) *Aura {
aura := Aura{
Label: config.Name,
ActionIDForProc: config.ActionID,
Duration: config.Duration,
ActionID: config.MetricsActionID,
}
if config.Duration == 0 {
aura.Duration = NeverExpires
aura.OnReset = func(aura *Aura, sim *Simulation) {
aura.Activate(sim)
}
}
aura.AttachProcTriggerCallback(unit, config)
return unit.GetOrRegisterAura(aura)
}
type CustomStatBuffProcCondition func(sim *Simulation, aura *Aura) bool
// Analog to an Aura "sub-class" that additionally links the Aura to one or more
// Stats. Used within APL snapshotting wrappers.
type StatBuffAura struct {
*Aura
// All Stat types that are buffed (before dependencies) when this Aura
// is activated.
BuffedStatTypes []stats.Stat
// Any special conditions (beyond standard ICD checks etc.) that must be
// satisfied before this Aura can be activated.
CustomProcCondition CustomStatBuffProcCondition
// Whether the aura is currently swapped (in another item set) out or not.
IsSwapped bool
}
func (aura *StatBuffAura) BuffsMatchingStat(statTypesToMatch []stats.Stat) bool {
if aura == nil {
return false
}
return CheckSliceOverlap(aura.BuffedStatTypes, statTypesToMatch)
}
func (aura *StatBuffAura) CanProc(sim *Simulation) bool {
return !aura.IsSwapped && ((aura.CustomProcCondition == nil) || aura.CustomProcCondition(sim, aura.Aura))
}
func (aura *StatBuffAura) InferCDType() CooldownType {
cdType := CooldownTypeUnknown
if aura.BuffsMatchingStat([]stats.Stat{stats.Armor, stats.BlockPercent, stats.DodgeRating, stats.ParryRating, stats.Health}) {
cdType |= CooldownTypeSurvival
} else {
cdType |= CooldownTypeDPS
}
return cdType
}
type StackingStatAura struct {
Aura Aura
BonusPerStack stats.Stats
}
func MakeStackingAura(character *Character, config StackingStatAura) *StatBuffAura {
bonusPerStack := config.BonusPerStack
config.Aura.OnStacksChange = func(aura *Aura, sim *Simulation, oldStacks int32, newStacks int32) {
character.AddStatsDynamic(sim, bonusPerStack.Multiply(float64(newStacks-oldStacks)))
}
return &StatBuffAura{
Aura: character.GetOrRegisterAura(config.Aura),
BuffedStatTypes: bonusPerStack.GetBuffedStatTypes(),
}
}
// Returns the same Aura for chaining.
func MakePermanent(aura *Aura) *Aura {
aura.Duration = NeverExpires
if aura.OnReset == nil {
aura.OnReset = func(aura *Aura, sim *Simulation) {
aura.Activate(sim)
}
} else {
oldOnReset := aura.OnReset
aura.OnReset = func(aura *Aura, sim *Simulation) {
oldOnReset(aura, sim)
aura.Activate(sim)
}
}
return aura
}
func BlockPrepull(aura *Aura) *Aura {
return aura.ApplyOnEncounterStart(func(aura *Aura, sim *Simulation) {
aura.Deactivate(sim)
})
}
type TemporaryStatBuffWithStacksConfig struct {
StackingAuraLabel string
StackingAuraActionID ActionID
AuraLabel string
ActionID ActionID
BonusPerStack stats.Stats
MaxStacks int32
TimePerStack time.Duration
Duration time.Duration
TickImmediately bool
DecrementStacks bool // Set to true if the aura should start at MaxStacks and decrement by 1 each tick
// BuggedAlterTimeRestore mirrors a live-game bug where, if the aura expired during Alter Time,
// it is restored for exactly one tick period then removed entirely instead of resuming normally.
// Only enable this for trinkets where the bug has been confirmed in game (e.g. Wushoolay's Final Choice).
BuggedAlterTimeRestore bool
}
func (character *Character) NewTemporaryStatBuffWithStacks(config TemporaryStatBuffWithStacksConfig) (*StatBuffAura, *Aura) {
stackingAura := MakeStackingAura(character, StackingStatAura{
Aura: Aura{
Label: Ternary(config.StackingAuraLabel != "", config.StackingAuraLabel, config.AuraLabel),
ActionID: Ternary(!config.StackingAuraActionID.IsEmptyAction(), config.StackingAuraActionID, config.ActionID),
Duration: config.Duration,
MaxStacks: config.MaxStacks,
},
BonusPerStack: config.BonusPerStack,
})
if config.TimePerStack > 0 {
var pa *PendingAction
startStackingAction := func(sim *Simulation, tickImmediately bool, numTicks int) {
pa = StartPeriodicAction(sim, PeriodicActionOptions{
Period: config.TimePerStack,
NumTicks: numTicks,
TickImmediately: tickImmediately,
OnAction: func(sim *Simulation) {
if stackingAura.IsActive() {
if config.DecrementStacks {
stackingAura.RemoveStack(sim)
} else {
stackingAura.AddStack(sim)
}
}
},
})
}
aura := character.RegisterAura(Aura{
Label: config.AuraLabel,
ActionID: config.ActionID,
Duration: config.Duration,
OnGain: func(aura *Aura, sim *Simulation) {
stackingAura.Activate(sim)
if config.DecrementStacks {
stackingAura.SetStacks(sim, config.MaxStacks)
}
startStackingAction(sim, config.TickImmediately, int(config.MaxStacks))
},
OnExpire: func(aura *Aura, sim *Simulation) {
if pa != nil {
pa.Cancel(sim)
pa = nil
}
},
OnRestore: func(aura *Aura, sim *Simulation, state AuraState, wasActive bool) {
// BUGGED BEHAVIOR (matches live game for specific trinkets, e.g. Wushoolay's Final Choice):
// If the aura expired during Alter Time (wasActive == false), the restore is broken.
// The stacks are restored for exactly 1 tick duration, then the buff is removed entirely.
if config.BuggedAlterTimeRestore && !wasActive {
// Schedule removal after one tick period
StartPeriodicAction(sim, PeriodicActionOptions{
Period: config.TimePerStack,
NumTicks: 1,
OnAction: func(sim *Simulation) {
stackingAura.Deactivate(sim)
aura.Deactivate(sim)
},
})
return
}
// Normal behavior: restart the periodic action
// When restoring (e.g., via Alter Time), we need to restart the periodic action
// but without TickImmediately to avoid adding an extra stack.
// Note: We don't activate the stacking aura here because it will be restored
// separately by Alter Time's restoration loop with the correct duration.
var remainingTicks int
if config.DecrementStacks {
// For decrementing buffs: we have N stacks, need N more ticks to reach 0
remainingTicks = int(state.Stacks)
} else {
// For incrementing buffs: we have N stacks, need (MaxStacks - N) ticks to reach max
remainingTicks = int(config.MaxStacks - state.Stacks)
}
if remainingTicks > 0 {
startStackingAction(sim, false, remainingTicks)
}
},
})
return stackingAura, aura
}
return stackingAura, nil
}
// Helper for the common case of making an aura that adds stats.
func (character *Character) NewTemporaryStatsAura(auraLabel string, actionID ActionID, tempStats stats.Stats, duration time.Duration) *StatBuffAura {
return character.NewTemporaryStatsAuraWrapped(auraLabel, actionID, tempStats, duration, nil)
}
// Alternative that allows modifying the Aura config.
func (character *Character) NewTemporaryStatsAuraWrapped(auraLabel string, actionID ActionID, buffs stats.Stats, duration time.Duration, modConfig func(*Aura)) *StatBuffAura {
// If one of the stat bonuses is a health bonus, then set up healing metrics for the associated
// heal, since all temporary max health bonuses also instantaneously heal the player.
var healthMetrics *ResourceMetrics
amountHealed := buffs[stats.Health]
includesHealthBuff := amountHealed > 0
buffedStatTypes := buffs.GetBuffedStatTypes()
if includesHealthBuff {
healthMetrics = character.NewHealthMetrics(actionID)
buffs[stats.Health] = 0
}
config := Aura{
Label: auraLabel,
ActionID: actionID,
Duration: duration,
OnGain: func(aura *Aura, sim *Simulation) {
if sim.Log != nil {
character.Log(sim, "Gained %s from %s.", buffs.FlatString(), actionID)
}
character.AddStatsDynamic(sim, buffs)
if includesHealthBuff {
character.UpdateMaxHealth(sim, amountHealed, healthMetrics)
}
for i := range character.OnTemporaryStatsChanges {
character.OnTemporaryStatsChanges[i](sim, aura, buffs)
}
},
OnExpire: func(aura *Aura, sim *Simulation) {
if sim.Log != nil {
character.Log(sim, "Lost %s from fading %s.", buffs.FlatString(), actionID)
}
character.AddStatsDynamic(sim, buffs.Invert())
if includesHealthBuff {
character.UpdateMaxHealth(sim, -amountHealed, healthMetrics)
}
for i := range character.OnTemporaryStatsChanges {
character.OnTemporaryStatsChanges[i](sim, aura, buffs.Invert())
}
},
}
if modConfig != nil {
modConfig(&config)
}
return &StatBuffAura{
Aura: character.GetOrRegisterAura(config),
BuffedStatTypes: buffedStatTypes,
}
}
// Creates a new ProcTriggerAura that is dependent on a parent Aura being active
// This should only be used if the dependent Aura is:
// 1. On the a different Unit than parent Aura is registered to (usually the Character)
// 2. You need to register multiple dependent Aura's for the same Unit
func (parentAura *Aura) MakeDependentProcTriggerAura(unit *Unit, config ProcTrigger) *Aura {
oldExtraCondition := config.ExtraCondition
config.ExtraCondition = func(sim *Simulation, spell *Spell, result *SpellResult) bool {
return parentAura.IsActive() && ((oldExtraCondition == nil) || oldExtraCondition(sim, spell, result))
}
aura := unit.MakeProcTriggerAura(config)
return aura
}
// Attaches a ProcTrigger to a parent Aura
// Preffered use-case.
// For non standard use-cases see: MakeDependentProcTriggerAura
// Returns parent aura for chaining
func (parentAura *Aura) AttachProcTrigger(config ProcTrigger) *Aura {
parentAura.AttachProcTriggerCallback(parentAura.Unit, config)
return parentAura
}
// Attaches a SpellMod to a parent Aura
// Returns parent aura for chaining
func (parentAura *Aura) AttachSpellMod(spellModConfig SpellModConfig) *Aura {
parentAuraDep := parentAura.Unit.AddDynamicMod(spellModConfig)
parentAura.ApplyOnGain(func(_ *Aura, _ *Simulation) {
parentAuraDep.Activate()
})
parentAura.ApplyOnExpire(func(_ *Aura, _ *Simulation) {
parentAuraDep.Deactivate()
})
return parentAura
}
// Attaches a StatDependency to a parent Aura
// Returns parent aura for chaining
func (parentAura *Aura) AttachStatDependency(statDep *stats.StatDependency) *Aura {
parentAura.ApplyOnGain(func(_ *Aura, sim *Simulation) {
parentAura.Unit.EnableBuildPhaseStatDep(sim, statDep)
})
parentAura.ApplyOnExpire(func(_ *Aura, sim *Simulation) {
parentAura.Unit.DisableBuildPhaseStatDep(sim, statDep)
})
if parentAura.IsActive() {
parentAura.Unit.StatDependencyManager.EnableDynamicStatDep(statDep)
}
return parentAura
}
// Adds Stats to a parent Aura
// Returns parent aura for chaining
func (parentAura *Aura) AttachStatsBuff(stats stats.Stats) *Aura {
parentAura.ApplyOnGain(func(aura *Aura, sim *Simulation) {
aura.Unit.AddStatsDynamic(sim, stats)
})
parentAura.ApplyOnExpire(func(aura *Aura, sim *Simulation) {
aura.Unit.AddStatsDynamic(sim, stats.Invert())
})
if parentAura.IsActive() {
parentAura.Unit.AddStats(stats)
}
return parentAura
}
// Adds a Stat to a parent Aura
// Returns parent aura for chaining
func (parentAura *Aura) AttachStatBuff(stat stats.Stat, value float64) *Aura {
statsToAdd := stats.Stats{}
statsToAdd[stat] = value
parentAura.AttachStatsBuff(statsToAdd)
return parentAura
}
// Attaches a multiplicative PseudoStat buff to a parent Aura
// Returns parent aura for chaining
func (parentAura *Aura) AttachMultiplicativePseudoStatBuff(fieldPointer *float64, multiplier float64) *Aura {
parentAura.ApplyOnGain(func(_ *Aura, _ *Simulation) {
*fieldPointer *= multiplier
})
parentAura.ApplyOnExpire(func(_ *Aura, _ *Simulation) {
*fieldPointer /= multiplier
})
if parentAura.IsActive() {
*fieldPointer *= multiplier
}
return parentAura
}
// Attaches an additive PseudoStat buff to a parent Aura
// Returns parent aura for chaining
func (parentAura *Aura) AttachAdditivePseudoStatBuff(fieldPointer *float64, bonus float64) *Aura {
parentAura.ApplyOnGain(func(_ *Aura, _ *Simulation) {
*fieldPointer += bonus
})
parentAura.ApplyOnExpire(func(_ *Aura, _ *Simulation) {
*fieldPointer -= bonus
})
if parentAura.IsActive() {
*fieldPointer += bonus
}
return parentAura
}
func (parentAura *Aura) AttachMultiplyCastSpeed(multiplier float64) *Aura {
parentAura.ApplyOnGain(func(_ *Aura, sim *Simulation) {
parentAura.Unit.MultiplyCastSpeed(sim, multiplier)
})
parentAura.ApplyOnExpire(func(_ *Aura, sim *Simulation) {
parentAura.Unit.MultiplyCastSpeed(sim, 1/multiplier)
})
return parentAura
}
func (parentAura *Aura) AttachMultiplyMeleeSpeed(multiplier float64) *Aura {
parentAura.ApplyOnGain(func(_ *Aura, sim *Simulation) {
parentAura.Unit.MultiplyMeleeSpeed(sim, multiplier)
})
parentAura.ApplyOnExpire(func(_ *Aura, sim *Simulation) {
parentAura.Unit.MultiplyMeleeSpeed(sim, 1/multiplier)
})
return parentAura
}
func (parentAura *Aura) AttachMultiplyAttackSpeed(multiplier float64) *Aura {
parentAura.ApplyOnGain(func(_ *Aura, sim *Simulation) {
parentAura.Unit.MultiplyAttackSpeed(sim, multiplier)
})
parentAura.ApplyOnExpire(func(_ *Aura, sim *Simulation) {
parentAura.Unit.MultiplyAttackSpeed(sim, 1/multiplier)
})
return parentAura
}
// Attaches a Damage Done By Caster buff to a parent Aura
// Returns parent aura for chaining
func (parentAura *Aura) AttachDDBC(index int, maxIndex int, attackTables *[]*AttackTable, handler DynamicDamageDoneByCaster) *Aura {
parentAura.ApplyOnGain(func(_ *Aura, _ *Simulation) {
EnableDamageDoneByCaster(index, maxIndex, (*attackTables)[parentAura.Unit.UnitIndex], handler)
})
parentAura.ApplyOnExpire(func(aura *Aura, _ *Simulation) {
DisableDamageDoneByCaster(index, (*attackTables)[parentAura.Unit.UnitIndex])
})
if parentAura.IsActive() {
EnableDamageDoneByCaster(index, maxIndex, (*attackTables)[parentAura.Unit.UnitIndex], handler)
}
return parentAura
}
type ShieldStrengthCalculator func(unit *Unit) float64
type OnDamageAbsorbedCallback func(sim *Simulation, aura *DamageAbsorptionAura, result *SpellResult, absorbedDamage float64)
type ShieldShouldApplyCondition func(sim *Simulation, spell *Spell, result *SpellResult, isPeriodic bool) bool
type AbsorptionAuraConfig struct {
Aura Aura
DamageMultiplier float64
MaxAbsorbPerHit float64
ShieldStrengthCalculator ShieldStrengthCalculator
OnDamageAbsorbed OnDamageAbsorbedCallback
ShouldApplyToResult ShieldShouldApplyCondition
}
type DamageAbsorptionAura struct {
*Aura
freshShieldStrengthCalculator ShieldStrengthCalculator
ShieldStrength float64
OnDamageAbsorbed []OnDamageAbsorbedCallback
}
func (aura *DamageAbsorptionAura) Activate(sim *Simulation) {
aura.Aura.Activate(sim)
aura.ShieldStrength = aura.freshShieldStrengthCalculator(aura.Unit)
stacks := max(1, int32(aura.ShieldStrength))
aura.Aura.MaxStacks = stacks
aura.Aura.SetStacks(sim, stacks)
}
func (unit *Unit) NewDamageAbsorptionAura(config AbsorptionAuraConfig) *DamageAbsorptionAura {
if config.Aura.Duration == 0 {
config.Aura.Duration = NeverExpires
}
aura := &DamageAbsorptionAura{
Aura: unit.RegisterAura(config.Aura),
freshShieldStrengthCalculator: config.ShieldStrengthCalculator,
}
if config.OnDamageAbsorbed != nil {
aura.OnDamageAbsorbed = append(aura.OnDamageAbsorbed, config.OnDamageAbsorbed)
}
if config.DamageMultiplier == 0 {
config.DamageMultiplier = 1
}
aura.ApplyOnExpire(func(_ *Aura, _ *Simulation) {
aura.ShieldStrength = 0
})
extraSpellCheck := func(sim *Simulation, spell *Spell, result *SpellResult, isPeriodic bool) bool {
return !spell.Flags.Matches(SpellFlagBypassAbsorbs) && ((config.ShouldApplyToResult == nil) || config.ShouldApplyToResult(sim, spell, result, isPeriodic))
}
unit.AddDynamicDamageTakenModifier(func(sim *Simulation, spell *Spell, result *SpellResult, isPeriodic bool) {
if aura.Aura.IsActive() && (result.Damage > 0) && extraSpellCheck(sim, spell, result, isPeriodic) {
absorbedDamage := min(aura.ShieldStrength, result.Damage*config.DamageMultiplier)
if config.MaxAbsorbPerHit > 0 {
absorbedDamage = min(config.MaxAbsorbPerHit, absorbedDamage)
}
result.Damage -= absorbedDamage
aura.ShieldStrength -= absorbedDamage
if sim.Log != nil {
unit.Log(sim, "%s absorbed %.1f damage, new shield strength: %.1f", aura.Label, absorbedDamage, aura.ShieldStrength)
}
for _, callback := range aura.OnDamageAbsorbed {
callback(sim, aura, result, absorbedDamage)
}
if aura.ShieldStrength <= 0 {
aura.Deactivate(sim)
return
}
aura.Aura.SetStacks(sim, int32(aura.ShieldStrength))
}
})
return aura
}
func (aura *DamageAbsorptionAura) AttachOnDamageAbsorbed(callback OnDamageAbsorbedCallback) *DamageAbsorptionAura {
aura.OnDamageAbsorbed = append(aura.OnDamageAbsorbed, callback)
return aura
}
type DamageAbsorptionAuraArray []*DamageAbsorptionAura
func (auras DamageAbsorptionAuraArray) Get(target *Unit) *DamageAbsorptionAura {
if auras == nil {
return nil
}
return auras[target.UnitIndex]
}
func (auras DamageAbsorptionAuraArray) IsEmpty() bool {
for _, aura := range auras {
if aura != nil {
return false
}
}
return true
}
func (auras DamageAbsorptionAuraArray) FindLabel() string {
for _, aura := range auras {
if aura != nil {
return aura.Label
}
}
panic("No valid damage absorption auras in array!")
}
func (caster *Unit) NewAllyDamageAbsorptionAuraArray(makeAura func(*Unit) *DamageAbsorptionAura) DamageAbsorptionAuraArray {
auras := make([]*DamageAbsorptionAura, len(caster.Env.AllUnits))
for _, target := range caster.Env.AllUnits {
if target.Type != EnemyUnit {
auras[target.UnitIndex] = makeAura(target)
}
}
return auras
}
func ApplyFixedUptimeAura(aura *Aura, uptime float64, tickLength time.Duration, startTime time.Duration) {
auraDuration := aura.Duration
ticksPerAura := float64(auraDuration) / float64(tickLength)
chancePerTick := TernaryFloat64(uptime == 1, 1, 1.0-math.Pow(1-uptime, 1/ticksPerAura))
aura.Unit.RegisterResetEffect(func(sim *Simulation) {
StartPeriodicAction(sim, PeriodicActionOptions{
Period: tickLength,
OnAction: func(sim *Simulation) {
if sim.RandomFloat("FixedAura") < chancePerTick {
aura.Activate(sim)
if aura.MaxStacks > 0 {
aura.AddStack(sim)
}
}
},
})
// Also try once at the start.
StartPeriodicAction(sim, PeriodicActionOptions{
Period: startTime,
NumTicks: 1,
OnAction: func(sim *Simulation) {
if sim.RandomFloat("FixedAura") < uptime {
// Use random duration to compensate for increased chance collapsed into single tick.
randomDur := tickLength + time.Duration(float64(auraDuration-tickLength)*sim.RandomFloat("FixedAuraDur"))
aura.Duration = randomDur
aura.Activate(sim)
aura.Duration = auraDuration
}
},
})
})
}