-
Notifications
You must be signed in to change notification settings - Fork 36
/
focus.go
211 lines (170 loc) · 6.02 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
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
package core
import (
"fmt"
"time"
"github.com/wowsims/cata/sim/core/proto"
"github.com/wowsims/cata/sim/core/stats"
)
type OnFocusGain func(*Simulation, float64)
type focusBar struct {
unit *Unit
maxFocus float64
currentFocus float64
baseFocusPerSecond float64
focusTickDuration time.Duration
nextFocusTick time.Duration
isPlayer bool
// These two terms are multiplied together to scale the total Focus regen from ticks.
focusRegenMultiplier float64
hasteRatingMultiplier float64
regenMetrics *ResourceMetrics
focusRefundMetrics *ResourceMetrics
OnFocusGain OnFocusGain
}
func (unit *Unit) EnableFocusBar(maxFocus float64, baseFocusPerSecond float64, isPlayer bool, onFocusGain OnFocusGain) {
unit.SetCurrentPowerBar(FocusBar)
unit.focusBar = focusBar{
unit: unit,
maxFocus: max(100, maxFocus),
focusTickDuration: unit.ReactionTime,
focusRegenMultiplier: 1,
hasteRatingMultiplier: 1,
isPlayer: isPlayer,
baseFocusPerSecond: baseFocusPerSecond,
regenMetrics: unit.NewFocusMetrics(ActionID{OtherID: proto.OtherAction_OtherActionFocusRegen}),
focusRefundMetrics: unit.NewFocusMetrics(ActionID{OtherID: proto.OtherAction_OtherActionRefund}),
OnFocusGain: onFocusGain,
}
}
func (unit *Unit) HasFocusBar() bool {
return unit.focusBar.unit != nil
}
func (fb *focusBar) CurrentFocus() float64 {
return fb.currentFocus
}
func (fb *focusBar) NextFocusTickAt() time.Duration {
return fb.nextFocusTick
}
func (fb *focusBar) MultiplyFocusRegenSpeed(sim *Simulation, multiplier float64) {
fb.ResetFocusTick(sim)
fb.focusRegenMultiplier *= multiplier
}
func (fb *focusBar) FocusRegenPerTick() float64 {
ticksPerSecond := float64(time.Second) / float64(fb.focusTickDuration)
return fb.FocusRegenPerSecond() / ticksPerSecond
}
func (fb *focusBar) FocusRegenPerSecond() float64 {
if fb.isPlayer {
return fb.baseFocusPerSecond * fb.getTotalRegenMultiplier()
} else {
return fb.baseFocusPerSecond
}
}
func (fb *focusBar) getTotalRegenMultiplier() float64 {
return fb.hasteRatingMultiplier * fb.focusRegenMultiplier
}
func (fb *focusBar) AddFocus(sim *Simulation, amount float64, metrics *ResourceMetrics) {
if amount < 0 {
panic("Trying to add negative focus!")
}
newFocus := min(fb.currentFocus+amount, fb.maxFocus)
if fb.isPlayer {
if sim.Log != nil {
fb.unit.Log(sim, "Gained %0.3f focus from %s (%0.3f --> %0.3f) of %0.0f total.", amount, metrics.ActionID, fb.currentFocus, newFocus, fb.maxFocus)
}
metrics.AddEvent(amount, newFocus-fb.currentFocus)
}
if fb.OnFocusGain != nil {
fb.OnFocusGain(sim, newFocus)
}
fb.currentFocus = newFocus
}
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) of %0.0f total.", amount, metrics.ActionID, fb.currentFocus, newFocus, fb.maxFocus)
}
fb.currentFocus = newFocus
}
// Gives an immediate partial Focus tick and restarts the tick timer.
func (fb *focusBar) ResetFocusTick(sim *Simulation) {
timeSinceLastTick := max(sim.CurrentTime-(fb.NextFocusTickAt()-fb.focusTickDuration), 0)
partialTickAmount := fb.FocusRegenPerSecond() * timeSinceLastTick.Seconds()
fb.AddFocus(sim, partialTickAmount, fb.regenMetrics)
fb.nextFocusTick = sim.CurrentTime + fb.focusTickDuration
sim.RescheduleTask(fb.nextFocusTick)
}
func (fb *focusBar) processDynamicHasteRatingChange(sim *Simulation) {
if fb.unit == nil {
return
}
fb.ResetFocusTick(sim)
fb.hasteRatingMultiplier = 1.0 + fb.unit.GetStat(stats.MeleeHaste)/(100*HasteRatingPerHastePercent)
}
func (fb *focusBar) RunTask(sim *Simulation) time.Duration {
if sim.CurrentTime < fb.nextFocusTick {
return fb.nextFocusTick
}
fb.AddFocus(sim, fb.FocusRegenPerTick(), fb.regenMetrics)
fb.nextFocusTick = sim.CurrentTime + fb.focusTickDuration
return fb.nextFocusTick
}
func (fb *focusBar) reset(sim *Simulation) {
if fb.unit == nil {
return
}
fb.currentFocus = fb.maxFocus
fb.hasteRatingMultiplier = 1.0 + fb.unit.GetStat(stats.MeleeHaste)/(100*HasteRatingPerHastePercent)
if fb.unit.Type != PetUnit {
fb.enable(sim, sim.Environment.PrepullStartTime())
}
}
func (fb *focusBar) enable(sim *Simulation, startAt time.Duration) {
sim.AddTask(fb)
fb.nextFocusTick = startAt + time.Duration(sim.RandomFloat("Focus Tick")*float64(fb.focusTickDuration))
sim.RescheduleTask(fb.nextFocusTick)
}
func (fb *focusBar) disable(sim *Simulation) {
fb.nextFocusTick = NeverExpires
sim.RemoveTask(fb)
}
type FocusCostOptions struct {
Cost float64
Refund float64
RefundMetrics *ResourceMetrics // Optional, will default to unit.FocusRefundMetrics if not supplied.
}
type FocusCost struct {
Refund float64
RefundMetrics *ResourceMetrics
ResourceMetrics *ResourceMetrics
}
func newFocusCost(spell *Spell, options FocusCostOptions) *FocusCost {
spell.DefaultCast.Cost = options.Cost
if options.Refund > 0 && options.RefundMetrics == nil {
options.RefundMetrics = spell.Unit.focusRefundMetrics
}
return &FocusCost{
Refund: options.Refund,
RefundMetrics: options.RefundMetrics,
ResourceMetrics: spell.Unit.NewFocusMetrics(spell.ActionID),
}
}
func (ec *FocusCost) MeetsRequirement(_ *Simulation, spell *Spell) bool {
spell.CurCast.Cost = spell.ApplyCostModifiers(spell.CurCast.Cost)
return spell.Unit.CurrentFocus() >= spell.CurCast.Cost
}
func (ec *FocusCost) CostFailureReason(_ *Simulation, spell *Spell) string {
return fmt.Sprintf("not enough focus (Current Focus = %0.03f, Focus Cost = %0.03f)", spell.Unit.CurrentFocus(), spell.CurCast.Cost)
}
func (ec *FocusCost) SpendCost(sim *Simulation, spell *Spell) {
spell.Unit.SpendFocus(sim, spell.CurCast.Cost, ec.ResourceMetrics)
}
func (ec *FocusCost) IssueRefund(sim *Simulation, spell *Spell) {
if ec.Refund > 0 {
spell.Unit.AddFocus(sim, ec.Refund*spell.CurCast.Cost, ec.RefundMetrics)
}
}