-
Notifications
You must be signed in to change notification settings - Fork 36
/
health.go
255 lines (211 loc) · 7.74 KB
/
health.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
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
package core
import (
"time"
"github.com/wowsims/cata/sim/core/proto"
"github.com/wowsims/cata/sim/core/stats"
)
type healthBar struct {
unit *Unit
currentHealth float64
DamageTakenHealthMetrics *ResourceMetrics
}
func (unit *Unit) EnableHealthBar() {
unit.healthBar = healthBar{
unit: unit,
DamageTakenHealthMetrics: unit.NewHealthMetrics(ActionID{OtherID: proto.OtherAction_OtherActionDamageTaken}),
}
}
func (unit *Unit) HasHealthBar() bool {
return unit.healthBar.unit != nil
}
func (hb *healthBar) reset(_ *Simulation) {
if hb.unit == nil {
return
}
hb.currentHealth = hb.MaxHealth()
}
func (hb *healthBar) MaxHealth() float64 {
return hb.unit.stats[stats.Health]
}
func (hb *healthBar) CurrentHealth() float64 {
return hb.currentHealth
}
func (hb *healthBar) CurrentHealthPercent() float64 {
return hb.currentHealth / hb.unit.stats[stats.Health]
}
func (hb *healthBar) GainHealth(sim *Simulation, amount float64, metrics *ResourceMetrics) {
if amount < 0 {
panic("Trying to gain negative health!")
}
oldHealth := hb.currentHealth
newHealth := min(oldHealth+amount, hb.unit.MaxHealth())
metrics.AddEvent(amount, newHealth-oldHealth)
if sim.Log != nil {
hb.unit.Log(sim, "Gained %0.3f health from %s (%0.3f --> %0.3f) of %0.0f total.", amount, metrics.ActionID, oldHealth, newHealth, hb.MaxHealth())
}
hb.currentHealth = newHealth
}
func (hb *healthBar) RemoveHealth(sim *Simulation, amount float64) {
if amount < 0 {
panic("Trying to remove negative health!")
}
oldHealth := hb.currentHealth
newHealth := max(oldHealth-amount, 0)
metrics := hb.DamageTakenHealthMetrics
metrics.AddEvent(-amount, newHealth-oldHealth)
// TMI calculations need timestamps and Max HP information for each damage taken event
if hb.unit.Metrics.isTanking {
entry := tmiListItem{
Timestamp: sim.CurrentTime,
WeightedDamage: amount / hb.MaxHealth(),
}
hb.unit.Metrics.tmiList = append(hb.unit.Metrics.tmiList, entry)
}
if sim.Log != nil {
hb.unit.Log(sim, "Spent %0.3f health from %s (%0.3f --> %0.3f) of %0.0f total.", amount, metrics.ActionID, oldHealth, newHealth, hb.MaxHealth())
}
hb.currentHealth = newHealth
}
// Used for dynamic updates to maximum health from "Last Stand" effects
func (hb *healthBar) UpdateMaxHealth(sim *Simulation, bonusHealth float64, metrics *ResourceMetrics) {
hb.unit.AddStatsDynamic(sim, stats.Stats{stats.Health: bonusHealth})
if bonusHealth >= 0 {
hb.GainHealth(sim, bonusHealth, metrics)
} else {
hb.RemoveHealth(sim, max(0, min(-bonusHealth, hb.currentHealth-1))) // Last Stand effects always leave the player with at least 1 HP when they expire
}
}
var ChanceOfDeathAuraLabel = "Chance of Death"
func (character *Character) trackChanceOfDeath(healingModel *proto.HealingModel) {
character.Unit.Metrics.isTanking = false
for _, target := range character.Env.Encounter.TargetUnits {
if target.CurrentTarget == &character.Unit {
character.Unit.Metrics.isTanking = true
}
}
character.RegisterAura(Aura{
Label: ChanceOfDeathAuraLabel,
Duration: NeverExpires,
OnReset: func(aura *Aura, sim *Simulation) {
aura.Activate(sim)
},
OnSpellHitTaken: func(aura *Aura, sim *Simulation, spell *Spell, result *SpellResult) {
if result.Damage > 0 {
aura.Unit.RemoveHealth(sim, result.Damage)
if aura.Unit.CurrentHealth() <= 0 && !aura.Unit.Metrics.Died {
// Queue a pending action to let shield effects give health
StartDelayedAction(sim, DelayedActionOptions{
DoAt: sim.CurrentTime,
OnAction: func(s *Simulation) {
if aura.Unit.CurrentHealth() <= 0 && !aura.Unit.Metrics.Died {
aura.Unit.Metrics.Died = true
if sim.Log != nil {
character.Log(sim, "Dead")
}
}
},
})
}
}
},
OnPeriodicDamageTaken: func(aura *Aura, sim *Simulation, spell *Spell, result *SpellResult) {
if result.Damage > 0 {
aura.Unit.RemoveHealth(sim, result.Damage)
if aura.Unit.CurrentHealth() <= 0 && !aura.Unit.Metrics.Died {
// Queue a pending action to let shield effects give health
StartDelayedAction(sim, DelayedActionOptions{
DoAt: sim.CurrentTime,
OnAction: func(s *Simulation) {
if aura.Unit.CurrentHealth() <= 0 && !aura.Unit.Metrics.Died {
aura.Unit.Metrics.Died = true
if sim.Log != nil {
character.Log(sim, "Dead")
}
}
},
})
}
}
},
})
if !character.Unit.Metrics.isTanking {
return
}
if healingModel == nil {
return
}
character.Unit.Metrics.tmiBin = healingModel.BurstWindow
if healingModel.Hps != 0 {
character.applyHealingModel(healingModel)
}
}
func (character *Character) applyHealingModel(healingModel *proto.HealingModel) {
// Store variance parameters for healing cadence. Note that low rolls on
// cadence are special cased here so that the model is still well-behaved
// when CadenceVariation exceeds CadenceSeconds.
medianCadence := healingModel.CadenceSeconds
if medianCadence == 0 {
medianCadence = 2.0
}
minCadence := max(0.0, medianCadence-healingModel.CadenceVariation)
cadenceVariationLow := medianCadence - minCadence
healthMetrics := character.NewHealthMetrics(ActionID{OtherID: proto.OtherAction_OtherActionHealingModel})
character.RegisterResetEffect(func(sim *Simulation) {
// Hack since we don't have OnHealingReceived aura handlers yet.
//ardentDefenderAura := character.GetAura("Ardent Defender")
//willOfTheNecropolisAura := character.GetAura("Will of The Necropolis")
// Initialize randomized cadence model
timeToNextHeal := DurationFromSeconds(0.0)
healPerTick := 0.0
pa := &PendingAction{
NextActionAt: timeToNextHeal,
}
pa.OnAction = func(sim *Simulation) {
// Use modeled HPS to scale heal per tick based on random cadence
healPerTick = healingModel.Hps * (float64(timeToNextHeal) / float64(time.Second))
// Execute the heal
character.GainHealth(sim, healPerTick*character.PseudoStats.HealingTakenMultiplier, healthMetrics)
// Might use this again in the future to track "absorb" metrics but currently disabled
//if ardentDefenderAura != nil && character.CurrentHealthPercent() >= 0.35 {
// ardentDefenderAura.Deactivate(sim)
//}
// if willOfTheNecropolisAura != nil && character.CurrentHealthPercent() > 0.35 {
// willOfTheNecropolisAura.Deactivate(sim)
// }
// Random roll for time to next heal. In the case where CadenceVariation exceeds CadenceSeconds, then
// CadenceSeconds is treated as the median, with two separate uniform distributions to the left and right
// of it.
signRoll := sim.RandomFloat("Healing Cadence Variation Sign")
magnitudeRoll := sim.RandomFloat("Healing Cadence Variation Magnitude")
if signRoll < 0.5 {
timeToNextHeal = DurationFromSeconds(minCadence + magnitudeRoll*cadenceVariationLow)
} else {
timeToNextHeal = DurationFromSeconds(medianCadence + magnitudeRoll*healingModel.CadenceVariation)
}
// Refresh action
pa.NextActionAt = sim.CurrentTime + timeToNextHeal
sim.AddPendingAction(pa)
}
sim.AddPendingAction(pa)
})
}
func (character *Character) GetPresimOptions(playerConfig *proto.Player) *PresimOptions {
healingModel := playerConfig.HealingModel
if healingModel == nil || healingModel.Hps != 0 || healingModel.CadenceSeconds == 0 {
// If Hps is not 0, then we don't need to run the presim.
// Tank sims should always have nonzero Cadence set, even if disabled
return nil
}
return &PresimOptions{
SetPresimPlayerOptions: func(player *proto.Player) {
player.HealingModel = nil
},
OnPresimResult: func(presimResult *proto.UnitMetrics, iterations int32, duration time.Duration) bool {
character.applyHealingModel(&proto.HealingModel{
Hps: presimResult.Dtps.Avg * 1.50,
CadenceSeconds: healingModel.CadenceSeconds,
})
return true
},
}
}