/
heal.go
110 lines (88 loc) · 2.17 KB
/
heal.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
package realm
import (
"errors"
"github.com/Gophercraft/core/packet/update"
"github.com/Gophercraft/core/realm/wdb/models"
)
func (m *Map) UnitIsAlive(target Unit) bool {
if target == nil {
return false
}
values := target.Values()
health := values.Get("Health")
return health.Uint32() != 0
}
func (m *Map) UnitIsDead(target Unit) bool {
return !m.UnitIsAlive(target)
}
// Resurrect only
func (m *Map) Resurrect(theDead Unit, health uint32) error {
theDead.Values().Get("Health").SetUint32(health)
m.PropagateObjectChanges(theDead)
return nil
}
// Resurrect + heal
func (m *Map) Revive(target Unit) error {
if !m.UnitIsAlive(target) {
if err := m.Resurrect(target, 1); err != nil {
return err
}
}
return m.HealAll(target)
}
// Heals all wounds
func (m *Map) HealAll(target Unit) error {
if m.UnitIsDead(target) {
return errors.New("realm: cannot heal a dead target!")
}
values := target.Values()
health := values.Get("Health")
maxHealth := values.Get("MaxHealth")
health.SetUint32(maxHealth.Uint32())
powerType := models.Power(values.Get("Power").Byte())
var (
power *update.Value
maxPower *update.Value
)
switch powerType {
case models.PowerMana:
power = values.Get("Mana")
maxPower = values.Get("MaxMana")
case models.PowerRage:
power = values.Get("Rage")
maxPower = values.Get("MaxRage")
case models.PowerFocus:
power = values.Get("Energy")
maxPower = values.Get("MaxEnergy")
case models.PowerEnergy:
power = values.Get("Energy")
maxPower = values.Get("MaxEnergy")
// case models.PowerHappiness:
}
if power != nil {
power.SetUint32(maxPower.Uint32())
}
m.PropagateObjectChanges(target)
return nil
}
// returns the amount of health recharged in a single heartbeat.
func (s *Session) rechargeHealth() uint32 {
return 10
}
func (s *Session) rechargeHeartbeat() {
isRecharging := !s.InCombat()
if isRecharging {
health := s.Get("Health")
prevHealth := health.Uint32()
maxHealth := s.Get("MaxHealth").Uint32()
if prevHealth == maxHealth {
return
}
nextHealth := prevHealth + s.rechargeHealth()
if nextHealth > maxHealth {
nextHealth = maxHealth
}
health.SetUint32(nextHealth)
s.UpdatePlayer()
}
}