This repository has been archived by the owner on Aug 30, 2018. It is now read-only.
/
combat.go
266 lines (222 loc) · 5.7 KB
/
combat.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
256
257
258
259
260
261
262
263
264
265
266
package world
import (
"fmt"
"math/big"
"math/rand"
"sync"
)
type Combat interface {
Living
Health() *big.Int
SetHealth(*big.Int)
MaxHealth() *big.Int
MaxQuality() *big.Int
StatLike
Hurt(*big.Int, Combat)
Die()
}
type CombatObject struct {
LivingObject
damaged big.Int
combatTicks uint8 // not saved
mtx sync.Mutex
}
func init() {
Register("combatobj", Combat((*CombatObject)(nil)))
}
func (o *CombatObject) Save() (uint, interface{}, []ObjectLike) {
return 1, map[string]interface{}{
"d": &o.damaged,
}, []ObjectLike{&o.LivingObject}
}
func (o *CombatObject) Load(version uint, data interface{}, attached []ObjectLike) {
switch version {
case 0:
o.LivingObject = *attached[0].(*LivingObject)
case 1:
dataMap := data.(map[string]interface{})
o.LivingObject = *attached[0].(*LivingObject)
o.damaged = *dataMap["d"].(*big.Int)
default:
panic(fmt.Sprintf("version %d unknown", version))
}
}
func (o *CombatObject) Think() {
o.LivingObject.Think()
max := o.Outer().(Combat).MaxHealth()
o.mtx.Lock()
if o.combatTicks > 0 {
o.combatTicks--
}
if o.damaged.Sign() > 0 && o.damaged.Cmp(max) < 0 {
var regen big.Int
if o.combatTicks > 0 {
regen.Div(o.Outer().(StatLike).Stat(StatHealthRegen), TuningHealthRegenDivisorCombat)
} else {
regen.Div(o.Outer().(StatLike).Stat(StatHealthRegen), TuningHealthRegenDivisorNonCombat)
}
o.damaged.Sub(&o.damaged, ®en)
if o.damaged.Sign() < 0 {
o.damaged.SetUint64(0)
}
if pos := o.Position(); pos != nil {
o.mtx.Unlock()
pos.Zone().Update(pos, o.Outer())
return
}
}
if o.damaged.Cmp(max) >= 0 {
o.mtx.Unlock()
o.Outer().(Combat).Die()
return
}
o.mtx.Unlock()
}
func (o *CombatObject) Health() *big.Int {
max := o.Outer().(Combat).MaxHealth()
o.mtx.Lock()
defer o.mtx.Unlock()
var health big.Int
if o.damaged.Cmp(max) >= 0 {
return &health
}
return health.Sub(max, &o.damaged)
}
func (o *CombatObject) SetHealth(health *big.Int) {
max := o.Outer().(Combat).MaxHealth()
o.mtx.Lock()
if max.Cmp(health) < 0 {
// TODO: allow negative damage?
o.damaged.SetUint64(0)
} else {
o.damaged.Sub(max, health)
}
o.mtx.Unlock()
}
func (o *CombatObject) MaxHealth() *big.Int {
return o.Outer().(StatLike).Stat(StatHealth)
}
func (o *CombatObject) MaxQuality() *big.Int {
return TuningDefaultStatQuality
}
func (o *CombatObject) Stat(stat Stat) *big.Int {
if s, ok := TuningDefaultStat[stat]; ok {
return s
}
return &big.Int{}
}
var (
DamageMissed = big.NewInt(0)
DamageBlocked = big.NewInt(0)
DamageResisted = big.NewInt(0)
)
func (o *CombatObject) Hurt(amount *big.Int, attacker Combat) {
o.mtx.Lock()
o.combatTicks = 50
o.damaged.Add(&o.damaged, amount)
o.mtx.Unlock()
if pos := o.Position(); pos != nil {
pos.Zone().Damage(attacker, o.Outer().(Combat), amount)
}
}
func (o *CombatObject) Die() {
o.Position().Remove(o.Outer())
}
func (o *CombatObject) Actions(player PlayerLike) []string {
if player == o.Outer() {
return o.LivingObject.Actions(player)
}
return append([]string{"fight"}, o.LivingObject.Actions(player)...)
}
func (o *CombatObject) Interact(player PlayerLike, action string) {
switch action {
default:
o.LivingObject.Interact(player, action)
case "fight":
if player == o.Outer() {
return
}
player.SetSchedule(&CombatSchedule{Target_: o.Outer().(Combat)})
}
}
func (o *CombatObject) Examine() (string, [][][2]string) {
message, info := o.LivingObject.Examine()
info = append(info, [][2]string{
{o.Outer().(Combat).Health().String(), "#4fc"},
{"/", "#ccc"},
{o.Outer().(Combat).MaxHealth().String(), "#4fc"},
{" health", "#ccc"},
})
return message, info
}
type CombatSchedule struct {
Object
Target_ Combat
}
func (s *CombatSchedule) Act(o Living) (uint, bool) {
c, ok := o.(Combat)
if !ok {
return 0, false
}
p1, p2 := c.Position(), s.Target_.Position()
if p1 == nil || p2 == nil || p1.Zone() != p2.Zone() {
return 0, false
}
x1, y1 := p1.Position()
x2, y2 := p2.Position()
if (x1 == x2 && y1 == y2) || (x1 != x2 && y1 != y2) || (x1 == x2 && y1 != y2-1 && y1 != y2+1) || (y1 == y2 && x1 != x2-1 && x1 != x2+1) {
c.SetSchedule(&ScheduleSchedule{Schedules: []Schedule{NewWalkSchedule(x2, y2, true, 0), s}})
return 0, true
}
r := rand.New(rand.NewSource(rand.Int63()))
maxDamage := c.Stat(StatMeleeDamage)
if maxDamage.Sign() <= 0 {
// can't attack
return 0, false
}
damage := (&big.Int{}).Rand(r, maxDamage)
armor := (&big.Int{}).Set(s.Target_.Stat(StatMeleeArmor))
if armor.Sign() <= 0 {
armor.SetUint64(0)
} else {
armor.Rand(r, armor)
}
crit := (&big.Int{}).Div((&big.Int{}).Div(c.Stat(StatCritChance), c.MaxQuality()), TuningCritDivisor)
if crit.Cmp(TuningMinCrit) < 0 {
crit.Set(TuningMinCrit)
} else if crit.Cmp(TuningMaxCrit) > 0 {
crit.Set(TuningMaxCrit)
}
crit_ := crit.Int64()
damage_ := (&big.Int{}).Div((&big.Int{}).Mul(TuningDamageMax, damage), maxDamage).Int64()
switch {
case damage_ < TuningDamageMiss1:
// miss
damage.SetInt64(0)
case damage_ < TuningDamageHit+crit_:
// normal hit
case damage_ < TuningDamageMiss2+crit_:
// miss
damage.SetInt64(0)
default:
damage.Mul(damage, TuningCritMultiplier)
}
if damage.Sign() <= 0 {
// miss
s.Target_.Hurt(DamageMissed, c)
} else if damage.Cmp(armor) <= 0 {
// block
s.Target_.Hurt(DamageBlocked, c)
} else {
resistance := s.Target_.Stat(StatResistance)
if resistance.Sign() > 0 && (&big.Int{}).Rand(r, resistance).Cmp((&big.Int{}).Mul(s.Target_.MaxQuality(), TuningResistDivisor)) > 0 {
s.Target_.Hurt(DamageResisted, c)
} else {
s.Target_.Hurt(damage.Sub(damage, armor), c)
}
}
// TODO: attack speed
return 2, true
}
func (s *CombatSchedule) ShouldSave() bool { return false }
func (s *CombatSchedule) Target() ObjectLike { return s.Target_ }