-
Notifications
You must be signed in to change notification settings - Fork 2
/
boids.bmx
630 lines (514 loc) · 10.2 KB
/
boids.bmx
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
Import warpy.gfx
Function dist#(x1# , y1# , x2# , y2#)
dx# = x2 - x1
dy# = y2 - y1
Return Sqr(dx * dx + dy * dy)
End Function
Global things:TList
Type thing
Field x# , y# , vx# , vy#
Field radius#
Function init()
things = New TList
End Function
Method New()
things.addlast Self
End Method
Method die()
things.remove Self
End Method
Method update() Abstract
Method draw() Abstract
End Type
Global boids:TList
Type boid Extends thing
Field nvx# , nvy#
Field tspeed#
Field red,green,blue
Field neighbours
Field dlim#
Function init()
boids = New TList
End Function
Method New()
boids.addlast Self
dlim=60
End Method
Method die()
v# = Sqr(vx * vx + vy * vy)
an# = ATan2(vy , vx)
For c = 1 To Rand(2 , 5)
ean# = an + Rnd( - 50 , 50)
ev#=v*Rnd(0,1)
evx# = Cos(ean) * ev
evy#=Sin(ean)*ev
explosion.Create x , y , evx , evy
Next
boids.remove Self
Super.die()
End Method
Method draw()
Global bsize#=3
angle# = ATan2(vy , vx)
x1# = x + Cos(angle) * bsize
y1# = y + Sin(angle) * bsize
x2# = x + Cos(angle + 120) * bsize
y2# = y + Sin(angle + 120) * bsize
x3# = x + Cos(angle - 120) * bsize
y3# = y + Sin(angle - 120) * bsize
SetColor red,green,blue
drawzoomLine x1 , y1 , x2 , y2
drawzoomLine x2 , y2 , x3 , y3
drawzoomLine x3 , y3 , x1 , y1
'DrawzoomText dlim,x,y
End Method
Method update()
x:+ vx
y:+ vy
'vx:* .95
'vy:* .95
nvx = vx
nvy = vy
tspeed#=3
avoid
dojob
steer
End Method
Method dojob() Abstract
Method steer()
oan# = ATan2(vy , vx)
nan# = ATan2(nvy , nvx)
dp# = vy * nvy + vx * nvx
ov# = Sqr(vx * vx + vy * vy)
nv# = Sqr(nvx * nvx + nvy * nvy)
dp:/ (nv * ov)
If ov = 0 Then dp = 1
'dp:*dp
tspeed# :* (1 + dp)/2
If ov > tspeed
ov:+ (tspeed-ov)*.7
Else
ov:+ (tspeed - ov) * .01
If Rand(50/(tspeed*tspeed)) = 1
exhaust.Create x , y , - vx*.1 , - vy*.1
endif
EndIf
'ov:* (1 + dp) * .5
dan# = (nan - oan) Mod 360
If dan < - 180 dan:+ 360
If dan > 180 dan:- 360
dan:*.1
If Abs(dan) > 15 Then dan = Sgn(dan) * 15
dan:+.1
an# = oan + dan
vx = Cos(an) * ov
vy = Sin(an) * ov
End Method
Method avoid()
Local ax# , ay#
neighbours=0
For t:thing = EachIn things
If t <> Self And boid(t)
dx# = t.x - x
dy# = t.y - y
d# = Sqr(dx * dx + dy * dy)-radius-t.radius
If d < dlim
neighbours:+1
If d < 4 And boid(t)
'Print("crash")
die
If boid(t) boid(t).die
EndIf
'tspeed:*.8
ad# = dlim-d
ax:- dx*ad/d
ay:-dy*ad/d
EndIf
EndIf
Next
v#=Sqr(vx*vx+vy*vy)
nvx:+ ax*.15*v
nvy:+ ay*.15*v
End Method
Method clump()
Local ax# , ay#, hx#,hy#
If boids.count() < 2 Return
tspeed:*.7
Local clumpers
For b:boid = EachIn boids
dx# = b.x - x
dy# = b.y - y
d# = dx * dx + dy * dy
If b <> Self And d<30000
ax:+ b.x
ay:+ b.y
hx:+ b.vx
hy:+b.vy
clumpers:+1
EndIf
Next
If clumpers
ax:/ clumpers
ay:/ clumpers
ax:- x
ay:- y
a#=Sqr(ax*ax+ay*ay)*3
hx:/ clumpers
hy:/ clumpers
h# = Sqr(hx * hx + hy * hy) * 3
nvx:+ ax /a
nvy:+ ay / a
If h > 0
nvx:+ hx / h
nvy:+ hy / h
endif
Else
tspeed=0
EndIf
If neighbours = 1
v#=Sqr(vx*vx+vy*vy)
tspeed = v * .9
If tspeed<.5 tspeed=.5
EndIf
End Method
End Type
Type fetcher Extends boid
Field targs:TList
Field gotcargo
Field jobfor:place
Method New()
targs = New TList
radius = 3
red = 255
green = 255
blue=255
End Method
Function Create:fetcher(x# , y#)
f:fetcher=New fetcher
f.x = x
f.y = y
Return f
End Function
Method die()
If jobfor
jobfor.needs:+ 1
EndIf
Super.die
End Method
Method dojob()
If Not jobfor And Rand(500)=1 picktarget
If targs.count()
gotarget
dlim=60
Else
dlim=100
v# = vx * vx + vy * vy
'tspeed=Sqr(v)*.8
clump
If Rand(10)=1 picktarget
EndIf
End Method
Method gotarget()
targ:place=place(targs.last())
dx# = targ.x - x
dy# = targ.y - y
angle# = ATan2(dy , dx)
aggro#=6
nvx:+ Cos(angle) *aggro
nvy:+ Sin(angle)*aggro
d# = dx * dx + dy * dy
If d < 2500
tspeed:* Sqr(d / 2500)
If d < 100
reachtarget
EndIf
EndIf
End Method
Method picktarget()
l:TList = New TList
For p:place = EachIn needers
If dist(x,y,p.x,p.y)<=p.needr l.addlast p
Next
If Not l.count() Return
n = Rand(0 , l.count() - 1)
targ:place = place(l.valueatindex(n) )
targs.addlast targ
End Method
Method addtarget(ptargs:TList)
mindist# = - 1
closest:place = Null
For p:place = EachIn ptargs
dx# = p.x - x
dy# = p.y - y
d# = dx * dx + dy * Dy
If d < mindist Or mindist = - 1
closest = p
mindist=d
EndIf
Next
If closest
targs.addlast closest
EndIf
End Method
Method reachtarget()
targ:place = place(targs.removelast() )
If quarry(targ)
'got resource
gotcargo=1
ElseIf fort(targ)
If gotcargo
fort(targ).build
gotcargo = 0
jobfor=Null
ElseIf takejob(targ)
targs.addlast targ
addtarget quarries
EndIf
EndIf
End Method
Method takejob(targ:place)
If targ.jobtaken()
jobfor = targ
Return 1
EndIf
End Method
End Type
Global places:TList
Global needers:TList
Type place Extends thing
Field x# , y#
Field needs,needr#
Function init()
places = New TList
needers = New TList
quarry.init
fort.init
End Function
Method New()
places.addlast Self
radius=7
End Method
Function doneeders()
needers = New TList
For p:place = EachIn places
If p.needs > 0
needers.addlast p
p.needr:+.8
EndIf
Next
End Function
Method jobtaken()
If needs
needs:- 1
Return 1
EndIf
End Method
End Type
Global quarries:TList
Type quarry Extends place
Field an#,speed#
Function init()
quarries = New TList
End Function
Method New()
quarries.addlast Self
an = Rand(360)
speed=Rnd(.1,1)
End Method
Function Create:quarry(x# , y#)
q:quarry = New quarry
q.x = x
q.y = y
Return q
End Function
Method update()
an:+ Rnd( - 1 , 2)
x:+ Cos(an) * speed
y:+ Sin(an) * speed
End Method
Method draw()
SetColor 100,50,50
drawzoomRect x - 5 , y - 5 , 10 , 10
End Method
End Type
Global forts:TList=New TList
Type fort Extends place
Field built#
Function init()
forts = New TList
End Function
Method New()
forts.addlast Self
needs=5
End Method
Function Create:fort(x# , y#)
f:fort=New fort
f.x = x
f.y = y
Return f
End Function
Method update()
End Method
Method build()
built:+ .2
End Method
Method draw()
SetColor 0 , 0 , 100
SetAlpha built
drawzoomRect x - 5 , y - 5 , 10 , 10
SetAlpha 1 - built
SetColor 100 , 0 , 0
drawzoomrect x-5,y-5,10,10,1,0
'drawzoomLine x - 5 , y - 5 , x - 5 , y + 5
'drawzoomLine x - 5 , y - 5 , x + 5 , y - 5
'drawzoomLine x - 5 , y + 5 , x + 5 , y + 5
'drawzoomLine x + 5 , y - 5 , x + 5 , y + 5
SetAlpha 1
If needs
SetAlpha .3
drawzoomshell x , y , needr, needr/10
SetAlpha 1
EndIf
End Method
End Type
Global puffs:TList
Type puff Extends thing
Field life#
Field radius#
Function init()
puffs = New TList
End Function
Method New()
puffs.addlast Self
End Method
Function Create:puff(x# , y# , vx# , vy#)
e:puff = New puff
e.x = x
e.y = y
e.vx = vx
e.vy = vy
e.radius = Rnd(1 , 5)
Return e
End Function
Method update()
x:+ vx
y:+ vy
life:+ .03
If life > 1
die
EndIf
End Method
Method die()
puffs.remove Self
Super.die
End Method
Method draw()
SetColor 255 , 0 , 0
SetAlpha 1-life
r# = Sin(180 * life) * radius
drawzoomcircle x,y,r
SetAlpha 1
End Method
End Type
Type explosion Extends puff
Function Create:explosion(x# , y# , vx# , vy#)
e:explosion = New explosion
e.x = x
e.y = y
e.vx = vx
e.vy = vy
e.radius = Rnd(1 , 5)
Return e
End Function
Method draw()
SetColor 255 , 0 , 0
SetAlpha 1-life
r# = Sin(180 * life) * radius
drawzoomcircle x,y,r
SetAlpha 1
End Method
End Type
Type exhaust Extends puff
Function Create:exhaust(x# , y# , vx# , vy#)
e:exhaust = New exhaust
e.x = x
e.y = y
e.vx = vx
e.vy = vy
e.radius = Rnd(.5 , 3)
Return e
End Function
Method draw()
SetColor 245 , 245 , 245
SetAlpha (1-life)*.5
r# = Sin(180 * life) * radius
drawzoomcircle x,y,r
SetAlpha 1
End Method
End Type
thing.init
place.init
boid.init
puff.init
AppTitle = "Twoot"
initgfx 600,600
SetClsColor 132,112,255
For c = 1 To 0
fetcher.Create(Rand(600) , Rand(600) )
Next
fort.Create 300 , 300
quarry.Create 50 , 50
quarry.Create 550 , 550
quarry.Create - 200 , 800
quarry.Create 800 , - 200
Global holdx# , holdy#
Global mx,my,zmx,zmy,mz=MouseZ()
While Not (KeyHit(KEY_ESCAPE) Or AppTerminate() )
omz = mz
mz = MouseZ()
dmz = mz - omz
DrawText mz,0,15
zoom:*(1+dmz*.1)
mx = MouseX()
my = MouseY()
zmx = unzoomx(mx)
zmy=unzoomy(my)
If boids.count()<200 And Rand(50) = 1
an = Rand(360)
r=Rand(600,1000)
fetcher.Create 300+Cos(an)*r,300+Sin(an)*r
EndIf
If MouseHit(1)
fort.Create(zmx , zmy )
EndIf
If MouseDown(2)
panx = holdx-mx
pany = holdy- my
Else
holdx = panx+mx
holdy = pany+my
EndIf
place.doneeders
For p:place = EachIn places
p.update
Next
For b:boid = EachIn boids
b.update
Next
For e:puff = EachIn puffs
e.update
Next
For p:place = EachIn places
p.draw
Next
For b:boid = EachIn boids
b.draw
Next
For e:puff = EachIn puffs
e.draw
Next
DrawText boids.count() + " boids" , 0 , 0
DrawText "left click to construct new fort",0,585
Flip
Cls
Wend