forked from iand/amberfell
/
wolf.go
292 lines (248 loc) · 8.7 KB
/
wolf.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
To the extent possible under law, Ian Davis has waived all copyright
and related or neighboring rights to this Amberfell Source Code file.
This work is published from the United Kingdom.
*/
package main
import (
"github.com/banthar/gl"
"math"
)
type Wolf struct {
MobData
}
func NewWolf(heading float64, x, y, z uint16) *Wolf {
wolf := &Wolf{}
wolf.heading = heading
wolf.position[XAXIS] = float64(x)
wolf.position[YAXIS] = float64(y)
wolf.position[ZAXIS] = float64(z)
wolf.walkingSpeed = 12
wolf.sprintSpeed = 25
wolf.fullEnergy = 30
wolf.energy = wolf.fullEnergy
wolf.fullHealth = 100
wolf.health = wolf.fullHealth
wolf.healingRate = 1
wolf.attackStrength = 5
wolf.mass = 5
wolf.behaviours = []MobBehaviour{
MobBehaviour{
behaviour: BEHAVIOUR_EVADE,
targetType: TARGET_CAMPFIRE,
targetRange: 14,
targetAngle: 180,
sunlight: SUNLIGHT_LEVELS_ANY,
weight: 2,
last: false,
},
MobBehaviour{
behaviour: BEHAVIOUR_ATTACK,
targetType: TARGET_PLAYER,
targetRange: 1,
targetAngle: 60,
sunlight: SUNLIGHT_LEVELS_ANY,
weight: 2,
last: false,
},
MobBehaviour{
behaviour: BEHAVIOUR_PURSUE,
targetType: TARGET_PLAYER,
targetRange: 12,
targetAngle: 180,
sunlight: SUNLIGHT_LEVELS_ANY,
weight: 2,
last: false,
},
MobBehaviour{
behaviour: BEHAVIOUR_PURSUE,
targetType: TARGET_PLAYER,
targetRange: 22,
targetAngle: 120,
sunlight: SUNLIGHT_LEVELS_NIGHT,
weight: 3,
last: true,
},
MobBehaviour{
behaviour: BEHAVIOUR_SEPARATE,
targetType: TARGET_WOLF,
targetRange: 10,
targetAngle: 120,
sunlight: SUNLIGHT_LEVELS_ANY,
weight: 2,
last: false,
},
MobBehaviour{
behaviour: BEHAVIOUR_GATHER,
targetType: TARGET_WOLF,
targetRange: 30,
targetAngle: 120,
sunlight: SUNLIGHT_LEVELS_ANY,
weight: 1,
last: false,
},
MobBehaviour{
behaviour: BEHAVIOUR_ALIGN,
targetType: TARGET_WOLF,
targetRange: 30,
targetAngle: 120,
sunlight: SUNLIGHT_LEVELS_ANY,
weight: 1,
last: false,
},
MobBehaviour{
behaviour: BEHAVIOUR_WANDER,
targetType: TARGET_ANY,
targetRange: 0,
targetAngle: 0,
sunlight: SUNLIGHT_LEVELS_ANY,
weight: 1,
last: false,
},
}
return wolf
}
func (self *Wolf) W() float64 { return 2 }
func (self *Wolf) H() float64 { return 2 }
func (self *Wolf) D() float64 { return 1 }
func (self *Wolf) Act2(dt float64) {
// Behaviour
// Determine intention: hunt, flee or wander
// Look for a threat (campfires)
// If threat nearby then evade it
// Look for a quarry (e.g. player, possibly other animals)
// If quarry nearby then pursue it (arrival)
// Otherwise wander
// Look for other wolves in vicinity (angle of vision, distance or neighbourhood)
// Maintain a minimum separation from others
// Maintain cohesion with others
// Maintain alignment with others
// Need angle, distance and weight for each of above 3 behaviours
// FIVE COMPONENTS
// Behaviour 4 bits pursue, evade, separate, cohese, align
// Target block type 8 bits +
// Range 6 bits
// Angle of range 7 bits
// Look ahead prediction time 4 bits
// Environment (day/night)
// Terminate sequence?
// TOTAL: 29bits
}
func (self *Wolf) Draw(center Vectorf, selectedBlockFace *BlockFace) {
pos := Vectorf{self.position[XAXIS], self.position[YAXIS], self.position[ZAXIS]}
gl.PushMatrix()
gl.Translated(self.position[XAXIS], self.position[YAXIS], self.position[ZAXIS])
gl.Rotated(self.Heading(), 0.0, 1.0, 0.0)
// Translate to top of ground block
gl.Translatef(0.0, -0.5, 0.0)
pos[YAXIS] += -0.5
headHeight := 0.25
headWidth := headHeight
headDepth := headHeight * 2.0
neckHeight := 0.0
torsoWidth := 1.5 * headHeight
torsoHeight := 1.5 * headHeight
torsoDepth := 5 * headHeight
legHeight := 5*headHeight - torsoHeight - neckHeight - headHeight
legWidth := (torsoWidth - 0.25*headHeight) / 2
legDepth := legWidth
var legAngle, step float64
horzSpeed := self.velocity[XAXIS]*self.velocity[XAXIS] + self.velocity[ZAXIS]*self.velocity[ZAXIS]
legAngle = math.Sin(self.walkSequence) * (15 + 55*horzSpeed/(self.sprintSpeed*self.sprintSpeed))
headAngle := 30.0
// torsoAngle = -math.Abs(legAngle / 6)
step = headHeight * 0.3 * math.Pow(math.Sin(self.walkSequence), 2)
gl.Translated(0.0, step, 0)
pos[YAXIS] += step
// Translate to top of leg
// Translate to centre of front left leg
gl.Translated(0.0, legHeight, 0)
pos[YAXIS] += legHeight
legDepthOffset := torsoDepth/2 - legWidth/2
legHeightOffset := -legHeight / 2
legWidthOffset := (legWidth + 0.25*headHeight) / 2
// Translate to centre of front left leg
gl.Translated(legDepthOffset, 0, legWidthOffset)
gl.Rotated(legAngle, 0.0, 0.0, 1.0)
gl.Translated(0, legHeightOffset, 0)
pos[XAXIS] += legDepthOffset
pos[YAXIS] += legHeightOffset
pos[ZAXIS] += legWidthOffset
Cuboid(pos, legWidth, legHeight, legDepth, textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], FACE_NONE)
pos[XAXIS] -= legDepthOffset
pos[YAXIS] -= legHeightOffset
pos[ZAXIS] -= legWidthOffset
gl.Translated(0, -legHeightOffset, 0)
gl.Rotated(-legAngle, 0.0, 0.0, 1.0)
gl.Translated(-legDepthOffset, 0, -legWidthOffset)
legWidthOffset = -legWidthOffset
if horzSpeed <= self.walkingSpeed*self.walkingSpeed {
legAngle = -legAngle
}
gl.Translated(legDepthOffset, 0, legWidthOffset)
gl.Rotated(legAngle, 0.0, 0.0, 1.0)
gl.Translated(0, legHeightOffset, 0)
pos[XAXIS] += legDepthOffset
pos[YAXIS] += legHeightOffset
pos[ZAXIS] += legWidthOffset
Cuboid(pos, legWidth, legHeight, legDepth, textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], FACE_NONE)
pos[XAXIS] -= legDepthOffset
pos[YAXIS] -= legHeightOffset
pos[ZAXIS] -= legWidthOffset
gl.Translated(0, -legHeightOffset, 0)
gl.Rotated(-legAngle, 0.0, 0.0, 1.0)
gl.Translated(-legDepthOffset, 0, -legWidthOffset)
legDepthOffset = -legDepthOffset
legWidthOffset = -legWidthOffset
if horzSpeed > self.walkingSpeed*self.walkingSpeed {
legAngle = -legAngle
}
gl.Translated(legDepthOffset, 0, legWidthOffset)
gl.Rotated(legAngle, 0.0, 0.0, 1.0)
gl.Translated(0, legHeightOffset, 0)
pos[XAXIS] += legDepthOffset
pos[YAXIS] += legHeightOffset
pos[ZAXIS] += legWidthOffset
Cuboid(pos, legWidth, legHeight, legDepth, textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], FACE_NONE)
pos[XAXIS] -= legDepthOffset
pos[YAXIS] -= legHeightOffset
pos[ZAXIS] -= legWidthOffset
gl.Translated(0, -legHeightOffset, 0)
gl.Rotated(-legAngle, 0.0, 0.0, 1.0)
gl.Translated(-legDepthOffset, 0, -legWidthOffset)
legWidthOffset = -legWidthOffset
if horzSpeed <= self.walkingSpeed*self.walkingSpeed {
legAngle = -legAngle
}
gl.Translated(legDepthOffset, 0, legWidthOffset)
gl.Rotated(legAngle, 0.0, 0.0, 1.0)
gl.Translated(0, legHeightOffset, 0)
pos[XAXIS] += legDepthOffset
pos[YAXIS] += legHeightOffset
pos[ZAXIS] += legWidthOffset
Cuboid(pos, legWidth, legHeight, legDepth, textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], textures[TEXTURE_WOLF_LEG], FACE_NONE)
pos[XAXIS] -= legDepthOffset
pos[YAXIS] -= legHeightOffset
pos[ZAXIS] -= legWidthOffset
gl.Translated(0, -legHeightOffset, 0)
gl.Rotated(-legAngle, 0.0, 0.0, 1.0)
gl.Translated(-legDepthOffset, 0, -legWidthOffset)
//gl.Rotated(torsoAngle, 0.0, 0.0, 1.0)
// Translate to centre of torso
gl.Translated(0.0, torsoHeight/2, 0.0)
pos[YAXIS] += torsoHeight / 2
Cuboid(pos, torsoWidth, torsoHeight, torsoDepth, textures[TEXTURE_WOLF_TORSO_FRONT], textures[TEXTURE_WOLF_TORSO_BACK], textures[TEXTURE_WOLF_TORSO_SIDE], textures[TEXTURE_WOLF_TORSO_SIDE], textures[TEXTURE_WOLF_TORSO_TOP], textures[TEXTURE_WOLF_TORSO_TOP], FACE_NONE)
// Translate to shoulders
gl.Translated(0.0, torsoHeight/2, 0.0)
pos[YAXIS] += torsoHeight / 2
// Translate to centre of head
gl.Translated(torsoDepth/2+headDepth*0.5, 0.0, 0.0)
pos[XAXIS] += torsoDepth/2 + headDepth*0.5
pos[YAXIS] += 0.0
gl.Rotated(-headAngle, 0.0, 0.0, 1.0)
Cuboid(pos, headWidth, headHeight, headDepth, textures[TEXTURE_WOLF_HEAD_FRONT], textures[TEXTURE_WOLF_HEAD_BACK], textures[TEXTURE_WOLF_HEAD_SIDE], textures[TEXTURE_WOLF_HEAD_SIDE], textures[TEXTURE_WOLF_HEAD_TOP], textures[TEXTURE_WOLF_HEAD_BOTTOM], FACE_NONE)
gl.PopMatrix()
}
func (self *Wolf) TargetType() uint8 {
return TARGET_WOLF
}