/
health.go
215 lines (185 loc) · 3.67 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
package main
import (
"math"
"strconv"
)
// bodyPart is an enum of general types of body parts.
type bodyPart int
const (
head = iota
chest
rArm
lArm
rHand
lHand
rLeg
lLeg
rFoot
lFoot
numBodyParts
)
func (b bodyPart) String() string {
switch b {
case head:
return "head"
case chest:
return "chest"
case rArm:
return "right-arm"
case lArm:
return "left-arm"
case rHand:
return "right-hand"
case lHand:
return "left-hand"
case rLeg:
return "right-leg"
case lLeg:
return "left-leg"
case rFoot:
return "right-foot"
case lFoot:
return "left-foot"
default:
return ""
}
}
func (b bodyPart) armorType() armor {
switch b {
case head:
return helmet
case chest:
return chestplate
case rArm:
return chestplate
case lArm:
return chestplate
case rHand:
return gauntlets
case lHand:
return gauntlets
case rLeg:
return greaves
case lLeg:
return greaves
case rFoot:
return boots
case lFoot:
return boots
default:
return shield
}
}
// bodyPartInstance is an instance of a body part with status.
type bodyPartInstance struct {
bodyPart bodyPart
armor *armorEquip
// out of 100
health int
broken bool
detached bool
infected bool
severed bool
}
// getBodyPart returns a corresponding bodyPartInstance
// given a bodyPart
func (h *health) getBodyPart(bp bodyPart) *bodyPartInstance {
for index := 0; index < len(h.bodyParts); index++ {
if h.bodyParts[index].bodyPart == bp {
return &h.bodyParts[index]
}
}
return nil
}
// health represents a characters health status.
type health struct {
bodyParts []bodyPartInstance
// out of 100
blood int
stamina int
unconscious bool
}
func (h *health) checkHealth() {
hd := h.getBodyPart(head)
if hd.broken || hd.detached || hd.severed || hd.health == 0 {
h.unconscious = true
}
hd = h.getBodyPart(chest)
if hd.detached || hd.health == 0 {
h.unconscious = true
}
if h.totalHealth() <= 50 {
h.unconscious = true
}
if h.blood <= 50 {
h.unconscious = true
}
}
func (h health) String() string {
var ans string
for _, x := range h.bodyParts {
ans += x.bodyPart.String() + ": " + strconv.Itoa(x.health) + "\n"
}
return ans
}
func (h *health) totalHealth() int {
var general, disabled int
for _, x := range h.bodyParts {
general += x.health
// extra weight to head and chest
if x.bodyPart == head {
general += 3 * x.health
} else if x.bodyPart == chest {
general += 2 * x.health
}
if x.broken || x.infected || x.severed || x.detached {
disabled++
}
}
capacity := float32(general) / (100*float32(len(h.bodyParts)) + 500)
capacity /= float32(math.Exp2(float64(disabled)))
return int(capacity*100 + .5)
}
// newPersonHealth initializes health to 100.
func newPersonHealth() health {
parts := make([]bodyPartInstance, numBodyParts)
for x := 0; x < numBodyParts; x++ {
parts[x] = bodyPartInstance{
bodyPart: bodyPart(x),
health: 100,
}
}
return health{
bodyParts: parts,
blood: 100,
stamina: 100,
}
}
var (
movementCrucial = []bodyPart{
head,
rLeg,
lLeg,
lFoot,
rFoot,
chest,
}
)
// movementCapacity calculates a characters movement penalty
// from taking damage. It first calculates overall health, and
// then applies further redutions for broken bones.
// It returns movement ability in decimal form. Ex: .5 = 50%
func (h *health) movementCapacity() float32 {
general := 0
disabled := 0
for _, x := range h.bodyParts {
general += x.health
if x.broken || x.infected || x.severed || x.detached {
disabled++
}
}
capacity := float32(general) / (100 * float32(len(h.bodyParts)))
capacity /= float32(math.Exp2(float64(disabled)))
//TODO factor in carried item weight
return capacity
}