-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.py
executable file
·428 lines (242 loc) · 7.87 KB
/
world.py
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
'''A 2d world that supports agents with steering behaviour
Created for HIT3046 AI for Games by Clinton Woodward [email protected]
'''
from vector2d import Vector2D, Rect
from matrix33 import Matrix33
from graphics import *
from pyglet import clock
from fish import Fish
from util import DictWrap
from path import Path
from hunter import Hunter
from rock import Rock
from guppy import Guppy
from util import Util
from tank import Tank
from food import Food
from random import uniform
class World(object):
'''
Inits
=================================='''
def __init__(self, width, height):
self.width = width
self.height = height
self.center = Vector2D(width/2, height/2)
self.obstacles = []
self._clock = 0
self.scale = 10
self.gravity = Vector2D(0, -900)
self.makeTank()
self.makeFish()
self.makeDebug()
self.makeFood()
self.makeObstacles()
self.makeHunters()
'''
Setup methods
=================================='''
def makeFish(self):
self.fishes = []
def makeFood(self):
self.food = []
self.foodDistance = 30
self.autoFeed = True
self.autoFeedAboveInterval = 2 # seconds
self.autoFeedBelowInterval = 1 # seconds
self.sicknessEnabled = True
self.sicknessInterval = 0.1
clock.schedule_interval(self.autoAddFoodAbove, self.autoFeedAboveInterval)
clock.schedule_interval(self.autoAddFoodBelow, self.autoFeedBelowInterval)
clock.schedule_interval(self.makeFishSicker, self.sicknessInterval)
def makeTank(self):
self.tank = Tank(world=self)
def makeObstacles(self):
self.obstacles = []
self.addRocks(10)
def makeHunters(self):
self.hunters = []
self.addHunters()
'''
'Add' methods
=================================='''
def addFood(self, x, y=None, num = 1):
# If no y was given then use the standard food distance above the tank
if(y is None):
y = self.height - self.foodDistance
else:
y = Util.clamp(self.tank.box.bottom, y, self.height)
# Make sure the food will go in the tank
x = Util.clamp(self.tank.box.left, x, self.tank.box.right)
# Add the foods
for _ in range(num):
newFood = Food(world=self)
newFood.pos = Vector2D(x, y)
self.food.append(newFood)
def obstacleOverlapsOtherObstacles(self, obstacle):
breathingSpace = 20
for o in self.obstacles:
if(o.pos.distance(obstacle.pos) - obstacle.boundingRadius - o.boundingRadius < breathingSpace):
return True
return False
def addRocks(self, num=10):
for i in range(num):
newRock = Rock(world=self)
newRock.vel = Vector2D.random(newRock.maxSpeed)
newRock.vel.y = 0
newRock.pos = self.tank.randomPosition()
while(self.obstacleOverlapsOtherObstacles(newRock)):
newRock.pos = self.tank.randomPosition()
self.obstacles.append(newRock)
def autoAddFoodAbove(self, dt=0):
self.autoAddFood(above=True)
def autoAddFoodBelow(self, dt=0):
self.autoAddFood(above=False)
self.autoAddFood(above=False)
def autoAddFood(self, above = True):
# Make sure auto food is enabled
if(not self.autoFeed): return
position = self.tank.randomPosition()
# Add the food
if(above):
self.addFood(x = position.x)
else:
self.addFood(x = position.x, y = position.y)
def addHunters(self, num=1):
# Add the foods
for _ in range(num):
newHunter = Hunter(world=self)
newHunter.pos = self.tank.randomPosition()
self.hunters.append(newHunter)
'''
Updating
=================================='''
def update(self, delta, forced=False):
self.lastDelta = delta
if not self.paused or forced:
self._clock += delta
# Debug fish
if(len(self.fishes)): self.fishes[0].chosenOne = True
if(len(self.hunters)): self.hunters[0].chosenOne = True
# Keep list of living fish for food calculatinons
self.livingFishes = [f for f in self.fishes if not f.dead]
# TODO: Calculate fish times/distances from foods for everything so it isn't
# recalculated by every fish * food
# self.calculateFoodData()
# Updates
[f.update(delta) for f in self.fishes]
[f.update(delta) for f in self.food if not f.eaten]
[o.update(delta) for o in self.obstacles]
[h.update(delta) for h in self.hunters]
# Kill dead fishes
self.fishes = [f for f in self.fishes if not (f.dead and f.pos.y < self.tank.box.bottom - 50)]
# Remove food that's off the screen
self.food = [f for f in self.food if not f.eaten]
def makeFishSicker(self, dt=0):
# Make sure sickness is enabled
if(not self.sicknessEnabled): return
[f.sicker() for f in self.fishes]
'''
Rendering
=================================='''
def render(self):
# Draw tank first
self.tank.render()
# Then fish
[f.render() for f in self.fishes]
# Food
[f.render() for f in self.food if not f.eaten]
# Rocks
[o.render() for o in self.obstacles]
# Hunters
[h.render() for h in self.hunters]
'''
World logic
=================================='''
def resize(self, width, height):
self.width = width
self.height = height
self.center = Vector2D(width/2, height/2)
self.tank.resize()
def addFish(self, num=1):
if(num < 1): return
newFishes = []
for _ in xrange(num):
newFish = Guppy(world=self, scale=10)
self.fishes.append(newFish)
newFishes.append(newFish)
# if(num == 1)
# return newFishes[0]
return newFishes
def randomizePath(self):
self.path.create_random_path(8, self.width/6, self.height/6, self.width*2/3 + self.width/6, self.height*2/3 + self.height/6)
for agent in self.fishes:
agent.path = self.path
def getNeighbours(self, agent, distance=100):
distanceSq = distance**2
return [a for a in self.fishes if a != agent and a.pos.distanceSq(agent.pos) < distanceSq]
def getFood(self, agent, distance=10000):
return [f for f in self.food if not f.eaten and self.tank.contains(f.pos)]
# Enables/disables the circle of life properties
@property
def lionKing(self):
return self.autoFeed # same value for both, so just pick this one
@lionKing.setter
def lionKing(self, value):
self.autoFeed = value
self.sicknessEnabled = value
'''
Util
=================================='''
def wrap_around(self, pos):
''' Treat world as a toroidal space. Updates parameter object pos '''
max_x, max_y = self.width, self.height
if pos.x > max_x:
pos.x = pos.x - max_x
elif pos.x < 0:
pos.x = max_x - pos.x
if pos.y > max_y:
pos.y = pos.y - max_y
elif pos.y < 0:
pos.y = max_y - pos.y
def transform_points(self, points, pos, forward, side, scale=Vector2D(1, 1)):
''' Transform the given list of points, using the provided position,
direction and scale, to object world space. '''
# make a copy of original points (so we don't trash them)
wld_pts = Util.copyPoints(points)
# create a transformation matrix to perform the operations
mat = Matrix33()
# scale,
mat.scale_update(scale.x, scale.y)
# rotate
mat.rotate_by_vectors_update(forward, side)
# and translate
mat.translate_update(pos.x, pos.y)
# now transform all the points (vertices)
mat.transform_vector2d_list(wld_pts)
# done
return wld_pts
def transform_point(self, point, pos, forward, side):
''' Transform the given single point, using the provided position,
and direction (forward and side unit vectors), to object world space. '''
# make a copy of the original point (so we don't trash it)
wld_pt = point.copy()
# create a transformation matrix to perform the operations
mat = Matrix33()
# rotate
mat.rotate_by_vectors_update(forward, side)
# and translate
mat.translate_update(pos.x, pos.y)
# now transform the point (in place)
mat.transform_vector2d(wld_pt)
# done
return wld_pt
'''
Debug variables
=================================='''
def makeDebug(self):
self.paused = False
self.drawDebug = False,
self.drawComponentForces = False
self.drawHidingSpots = False
self.awokenHunter = True