forked from wowsims/wotlk
/
focus.go
138 lines (108 loc) · 3.38 KB
/
focus.go
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
package core
import (
"time"
"github.com/Tereneckla/wotlk/sim/core/proto"
)
// Time between focus ticks.
const MaxFocus = 100.0
const tickDuration = time.Second * 1
const BaseFocusPerTick = 5.0
// OnFocusGain is called any time focus is increased.
type OnFocusGain func(sim *Simulation)
type focusBar struct {
unit *Unit
focusPerTick float64
currentFocus float64
onFocusGain OnFocusGain
tickAction *PendingAction
}
func (unit *Unit) EnableFocusBar(regenMultiplier float64, onFocusGain OnFocusGain) {
unit.focusBar = focusBar{
unit: unit,
focusPerTick: BaseFocusPerTick * regenMultiplier,
onFocusGain: onFocusGain,
}
}
func (unit *Unit) HasFocusBar() bool {
return unit.focusBar.unit != nil
}
func (fb *focusBar) CurrentFocus() float64 {
return fb.currentFocus
}
func (fb *focusBar) AddFocus(sim *Simulation, amount float64, actionID ActionID) {
if amount < 0 {
panic("Trying to add negative focus!")
}
newFocus := MinFloat(fb.currentFocus+amount, MaxFocus)
if sim.Log != nil {
fb.unit.Log(sim, "Gained %0.3f focus from %s (%0.3f --> %0.3f).", amount, actionID, fb.currentFocus, newFocus)
}
fb.currentFocus = newFocus
if fb.onFocusGain != nil {
fb.onFocusGain(sim)
}
}
func (fb *focusBar) SpendFocus(sim *Simulation, amount float64, metrics *ResourceMetrics) {
if amount < 0 {
panic("Trying to spend negative focus!")
}
newFocus := fb.currentFocus - amount
metrics.AddEvent(-amount, -amount)
if sim.Log != nil {
fb.unit.Log(sim, "Spent %0.3f focus from %s (%0.3f --> %0.3f).", amount, metrics.ActionID, fb.currentFocus, newFocus)
}
fb.currentFocus = newFocus
}
func (fb *focusBar) reset(sim *Simulation) {
if fb.unit == nil {
return
}
fb.currentFocus = MaxFocus
pa := &PendingAction{
Priority: ActionPriorityRegen,
NextActionAt: tickDuration,
}
pa.OnAction = func(sim *Simulation) {
fb.AddFocus(sim, fb.focusPerTick, ActionID{OtherID: proto.OtherAction_OtherActionFocusRegen})
pa.NextActionAt = sim.CurrentTime + tickDuration
sim.AddPendingAction(pa)
}
fb.tickAction = pa
sim.AddPendingAction(pa)
}
func (fb *focusBar) Cancel(sim *Simulation) {
if fb.tickAction != nil {
fb.tickAction.Cancel(sim)
fb.tickAction = nil
}
}
type FocusCostOptions struct {
Cost float64
}
type FocusCost struct {
Refund float64
ResourceMetrics *ResourceMetrics
}
func newFocusCost(spell *Spell, options FocusCostOptions) *FocusCost {
spell.DefaultCast.Cost = options.Cost
return &FocusCost{
ResourceMetrics: spell.Unit.NewFocusMetrics(spell.ActionID),
}
}
func (fc *FocusCost) MeetsRequirement(spell *Spell) bool {
spell.CurCast.Cost = MaxFloat(0, spell.CurCast.Cost*spell.Unit.PseudoStats.CostMultiplier)
return spell.Unit.CurrentFocus() >= spell.CurCast.Cost
}
func (fc *FocusCost) LogCostFailure(sim *Simulation, spell *Spell) {
spell.Unit.Log(sim,
"Failed casting %s, not enough focus. (Current Focus = %0.03f, Focus Cost = %0.03f)",
spell.ActionID, spell.Unit.CurrentFocus(), spell.CurCast.Cost)
}
func (fc *FocusCost) SpendCost(sim *Simulation, spell *Spell) {
spell.Unit.SpendFocus(sim, spell.CurCast.Cost, fc.ResourceMetrics)
}
func (fc *FocusCost) IssueRefund(sim *Simulation, spell *Spell) {
}
func (spell *Spell) FocusMetrics() *ResourceMetrics {
return spell.Cost.(*FocusCost).ResourceMetrics
}