-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboid.py
411 lines (344 loc) · 13.1 KB
/
boid.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
import random
import numpy as np
# Project
from constants import *
import vectorutil as vu
from flora import *
# Constants
BOID_H = 17
BOID_W = BOID_H*2/3
BOID_HH = BOID_H/2
BOID_HW = BOID_W/2
BOID_CLR = ['#128ECF', '#1DAAE7', '#0760F3']
class Boid:
"""A Class representing boid elements.
Capable of moving as flocks, eating flora and reproducing.
They are the food source of predators, which they try to avoid.
"""
# Constants
maxForce = 0.05 * 60/ITER_SEC
maxRoamSpeed = 3.5 * 60/ITER_SEC
maxEscapeSpeed = 4.5 * 60/ITER_SEC
inertiaRatio = 0.33
gestationTime = 9*ITER_SEC
fertileThld = 3*ITER_SEC
matingChance = 0.5
ttlPregnant = 0
reproMax = 35
hungerRate = 80./ITER_SEC
hungerMax = 1500.
hungerThld = hungerMax/3.
floraNrgRatio = 0.75
# Static
all = []
grid = []
predatorGrid = None # Externally set
# Init static
for i in range(GRID_W):
grid.append( [] )
for j in range(GRID_H):
grid[i].append( [] )
#
@staticmethod
def setPredatorGrid(predGrid):
Boid.predatorGrid = predGrid
#
def removeFromGrid(self):
Boid.grid[int(self.pos[0] // VISION_RAD)][int(self.pos[1] // VISION_RAD)].remove(self)
#
def updateGrid(self, remove=True):
if remove:
# Only if cell changed
if ( int(self.pos[0] // VISION_RAD) == int(self.nxt[0] // VISION_RAD)
and int(self.pos[1] // VISION_RAD) == int(self.nxt[1] // VISION_RAD) ):
return
Boid.grid[int(self.pos[0] // VISION_RAD)][int(self.pos[1] // VISION_RAD)].remove(self)
# Add
Boid.grid[int(self.nxt[0] // VISION_RAD)][int(self.nxt[1] // VISION_RAD)].append(self)
#
def getNearCells(self):
return vu.getNearCellsFromPos(self.pos, VISION_RAD, GRID_W-1, GRID_H-1)
#
def distanceSqr(self, other):
return vu.distanceSqrWrapped(self.pos, other.pos)
#
#
def __init__(self, canvas, pos=None, isNewborn=False):
if pos is not None:
self.pos = pos
else:
self.pos = np.array([ random.randint(0, CANVAS_W-1),
random.randint(0, CANVAS_H-1) ])
self.nxt = self.pos
self.vel = vu.randomUnitVector()
self.vel *= random.uniform(2., Boid.maxRoamSpeed)
self.vxt = self.vel
self.maxSpeed = Boid.maxRoamSpeed
self.acc = np.array([0., 0.])
self.inertiaRatio = Boid.inertiaRatio
self.gender = random.choice('MF')
if self.gender == 'M':
if isNewborn: self.fertile = 0
else: self.fertile = random.randint(0, Boid.fertileThld)
else:
self.gestation = 0
self.hunger = random.randint(0, Boid.hungerThld)
self.eating = None
self.caught = False
self.updateGrid(False)
self.points = vu.updateTriPoints(self.nxt, self.vel, BOID_HH, BOID_HW)
self.canvas = canvas
self.id = self.canvas.create_polygon(self.points, fill=random.choice(BOID_CLR))
Boid.all.append(self)
#
def align(self):
steer = np.array([0., 0.])
sum = np.array([0., 0.])
nbr = 0
cells = self.getNearCells()
for cell in cells:
for boid in Boid.grid[cell[0]][cell[1]]:
if boid is not self and boid.eating is None and self.distanceSqr(boid) <= VISION_RADSQR:
sum += boid.vel
nbr += 1
# Apply
if nbr > 0:
steer = sum / nbr
steer = vu.setMag(steer, Boid.maxRoamSpeed)
steer -= self.vel
steer = vu.capMag(steer, Boid.maxForce)
return steer
#
def cohesion(self):
steer = np.array([0., 0.])
sum = np.array([0., 0.])
nbr = 0
cells = self.getNearCells()
for cell in cells:
for boid in Boid.grid[cell[0]][cell[1]]:
if boid is not self and boid.eating is None and self.distanceSqr(boid) <= VISION_RADSQR:
sum += vu.wrapRelativePos(self.pos, boid.pos)
nbr += 1
# Apply
if nbr > 0:
steer = sum / nbr
steer -= self.pos
steer = vu.setMag(steer, Boid.maxRoamSpeed)
steer -= self.vel
steer = vu.capMag(steer, Boid.maxForce)
return steer
#
def separation(self):
steer = np.array([0., 0.])
sum = np.array([0., 0.])
nbr = 0
cells = self.getNearCells()
for cell in cells:
for boid in Boid.grid[cell[0]][cell[1]]:
sqrDist = self.distanceSqr(boid)
if boid is not self and boid.eating is None and sqrDist <= VISION_RADSQR*0.5:
if sqrDist < EPSILON: sqrDist = EPSILON
diff = self.pos - vu.wrapRelativePos(self.pos, boid.pos)
sum += vu.setMag(diff, 1./sqrDist)
nbr += 1
# Apply
if nbr > 0:
steer = sum / nbr
steer = vu.setMag(steer, Boid.maxRoamSpeed)
steer -= self.vel
steer = vu.capMag(steer, Boid.maxForce)
return steer
#
def roam(self):
self.hunger += Boid.hungerRate
# Flocking (with arbitrary weight)
steer = self.align()*1.1
steer += self.cohesion()*0.9
steer += self.separation()*1.
steer = vu.capMag(steer, Boid.maxForce*2.)
return steer
#
def mating(self):
if (self.gender == 'F' or len(Boid.all) + Boid.ttlPregnant >= Boid.reproMax
or self.hunger >= Boid.hungerThld or self.fertile < Boid.fertileThld):
return
cells = self.getNearCells()
for cell in cells:
for boid in Boid.grid[cell[0]][cell[1]]:
if boid is not self and boid.gender == 'F' and boid.gestation <= 0:
success = random.uniform(0., 1.) <= Boid.matingChance
if success:
# if DEBUG_LOG: print('Pregnant boid:', self.id)
boid.gestation = 1
Boid.ttlPregnant += 1
self.fertile = 0 # cool down
return
#
def getBiggestClosestFlora(self):
res = None
maxSize = -1
minSqrDist = -1.
cells = Flora.getNearCells(self.pos)
for cell in cells:
for flora in Flora.grid[cell[0]][cell[1]]:
sqrDist = self.distanceSqr(flora)
if sqrDist <= FLORA_RADSQR:
if (res is None or flora.val > maxSize
or (flora.val == maxSize and sqrDist < minSqrDist)):
res = flora
maxSize = flora.val
minSqrDist = sqrDist
return res
#
def managePredators(self):
escaping = False
sum = np.array([0., 0.])
nbr = 0
cells = self.getNearCells()
for cell in cells:
for pred in Boid.predatorGrid[cell[0]][cell[1]]:
sqrDist = self.distanceSqr(pred)
if sqrDist <= VISION_RADSQR:
diff = self.pos - vu.wrapRelativePos(self.pos, pred.pos)
sum += vu.setMag(diff, 1./sqrDist)
nbr += 1
# Apply
if nbr > 0:
steer = sum / nbr
steer = vu.setMag(steer, Boid.maxEscapeSpeed)
steer -= self.vel
steer = vu.setMag(steer, Boid.maxForce*8.)
self.acc = steer
self.hunger += Boid.hungerRate
escaping = True
return escaping
#
def manageReproduction(self):
if self.gender == 'M':
if self.fertile < Boid.fertileThld:
self.fertile += 1
else:
if self.gestation > 0:
self.gestation += 1
if self.gestation >= Boid.gestationTime:
Boid(self.canvas, vu.getAdjacentPos(self.pos, BOID_H*2), True)
self.gestation = 0
Boid.ttlPregnant -= 1
if DEBUG_LOG:
print('Newborn boid:', self.id)
if len(Boid.all) == Boid.reproMax: print('>> MAX BOIDS!')
#
def killed(self):
self.canvas.delete(self.id)
if self.gender == 'F' and self.gestation > 0:
Boid.ttlPregnant -= 1
self.removeFromGrid()
Boid.all.remove(self)
if DEBUG_LOG:
if len(Boid.all) == 0: print('>> NO MORE BOIDS!')
#
def starved(self):
starved = self.hunger >= Boid.hungerMax
if starved:
if DEBUG_LOG: print('Starved boid:', self.id)
self.killed()
return starved
#
def manageHunger(self):
hungry = (self.hunger >= Boid.hungerThld
or (self.eating is not None and self.hunger > 0)) # Or eating but not full
if hungry:
bite = -1.
if self.eating is not None: bite = self.eating.takeBite()
# Still eating (immobile)
if bite > 0.:
self.hunger -= bite * Boid.floraNrgRatio
self.acc *= 0.
self.vxt *= 0.
# Look for flora
else:
self.eating = None
closestFlora = self.getBiggestClosestFlora()
if closestFlora is None: # Keep looking
self.acc += self.roam()
else:
sqrDist = self.distanceSqr(closestFlora)
nearDist = max(BOID_HH**2, closestFlora.rad**2)
# Close enough to eat
if sqrDist <= nearDist:
bite = closestFlora.takeBite()
if bite > 0.:
self.hunger -= bite * Boid.floraNrgRatio
self.eating = closestFlora
self.acc *= 0.
self.vxt *= 0.
# Go toward flora
else:
self.hunger += Boid.hungerRate
steer = vu.wrapRelativePos(self.pos, closestFlora.pos) - self.pos
steer = vu.setMag(steer, Boid.maxRoamSpeed)
steer -= self.vel
steer = vu.capMag(steer, Boid.maxForce*2)
self.acc = steer
return hungry
#
def updatePos(self):
# Update state
self.acc *= self.inertiaRatio
# Reproduction
self.manageReproduction()
# Show stoppers
if self.caught or self.starved():
return
# Escape predator
escaping = self.managePredators()
if not escaping:
self.maxSpeed = Boid.maxRoamSpeed
# Hungry
hungry = self.manageHunger()
# Roam free
if not hungry:
self.eating = None
self.mating()
self.acc += self.roam()
else:
self.eating = None
self.maxSpeed = Boid.maxEscapeSpeed
# Special case: immobile -> random push
if self.eating is None and vu.norm(self.acc) < EPSILON and vu.norm(self.vxt) < EPSILON:
self.acc = vu.randomUnitVector() * Boid.maxForce
# if DEBUG_LOG: print('Random push:', self.id)
# No deceleration if not at full speed
if vu.norm(self.vxt) < self.maxSpeed:
self.inertiaRatio = 1.
else:
self.inertiaRatio = Boid.inertiaRatio
# Apply acceleration
self.acc = vu.capMag(self.acc, Boid.maxForce*8.) # Saturate acc
self.vxt += self.acc
self.vxt = vu.capMag(self.vxt, self.maxSpeed) # Saturate vel
self.nxt = np.add(self.nxt, self.vxt)
# Update position + rotation
if ((self.acc[0] != 0. or self.acc[1] != 0.)
and (self.vxt[0] != 0. or self.vxt[1] != 0)):
# Warp around
vu.wrapEdges(self)
self.points = vu.updateTriPoints(self.nxt, self.vel, BOID_HH, BOID_HW)
# Update position only
else:
# Move
self.points = [ self.points[0]+self.vxt[0], self.points[1]+self.vxt[1],
self.points[2]+self.vxt[0], self.points[3]+self.vxt[1],
self.points[4]+self.vxt[0], self.points[5]+self.vxt[1] ]
# Warp around
vu.wrapEdges(self, True)
# Apply
if ( self.pos[0] != self.nxt[0] or self.pos[1] != self.nxt[1]
or self.vel[0] != self.vxt[0] or self.vel[1] != self.vxt[1] ):
self.canvas.coords(self.id, *self.points)
self.canvas.update_idletasks()
#
def applyNewPos(self):
self.updateGrid()
self.pos = self.nxt
self.vel = self.vxt