/
RCGA.bas
559 lines (517 loc) · 19.2 KB
/
RCGA.bas
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
Attribute VB_Name = "Module1"
Public Type Point2d
X As Single
Y As Single
End Type
Public Type Organism
State As String
Color As Long
Target As Integer
Pos As Point2d
Dir As Single
Flag As Integer
Rad As Single
FearRad As Single
AggresRad As Single
MoveVel As Single
TurnVel As Single
End Type
Public Unit(1 To 20) As Organism
Public Tics%
Public TicCheck%
Public Periods%
Public GenCheck%
Public Bounds As Boolean
Public Status$
Public MsgStatus As Boolean
Public ProxyStatus As Boolean
Function NearestTarget%(Subject%)
Dim A%
Dim CurVal%
Dim BestVal%
For A = LBound(Unit) To UBound(Unit)
If A <> Subject And Unit(A).Target <> Subject Then
CurVal = _
GetDist( _
Unit(Subject).Pos.X - Unit(A).Pos.X, _
Unit(Subject).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If CurVal > BestVal Then BestVal = CurVal
End If
Next A
For A = LBound(Unit) To UBound(Unit)
If A <> Subject And Unit(A).Target <> Subject Then
CurVal = _
GetDist( _
Unit(Subject).Pos.X - Unit(A).Pos.X, _
Unit(Subject).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If CurVal < BestVal Then BestVal = CurVal
End If
Next A
For A = LBound(Unit) To UBound(Unit)
If A <> Subject And Unit(A).Target <> Subject Then
CurVal = _
GetDist( _
Unit(Subject).Pos.X - Unit(A).Pos.X, _
Unit(Subject).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If CurVal = BestVal Then
NearestTarget = A
Exit Function
End If
End If
Next A
End Function
Function BestUnit%(Optional Exclusion%)
Dim A%
Dim CurVal%
Dim BestVal%
For A = LBound(Unit) To UBound(Unit)
If A <> Exclusion Then
CurVal = Unit(A).Flag
If CurVal > BestVal Then BestVal = CurVal
End If
Next A
For A = LBound(Unit) To UBound(Unit)
If A <> Exclusion Then
If Unit(A).Flag = BestVal Then
BestUnit = A
Exit Function
End If
End If
Next A
End Function
Function WorstUnit%(Optional Exclusion%)
Dim A%
Dim CurVal%
Dim BestVal%
For A = LBound(Unit) To UBound(Unit)
If A <> Exclusion Then
CurVal = Unit(A).Flag
If CurVal > BestVal Then BestVal = CurVal
End If
Next A
For A = LBound(Unit) To UBound(Unit)
If A <> Exclusion Then
CurVal = Unit(A).Flag
If CurVal < BestVal Then BestVal = CurVal
End If
Next A
For A = LBound(Unit) To UBound(Unit)
If A <> Exclusion Then
If Unit(A).Flag = BestVal Then
WorstUnit = A
Exit Function
End If
End If
Next A
End Function
Function NearestThreat%(Subject%)
Dim A%
Dim CurVal%
Dim BestVal%
For A = LBound(Unit) To UBound(Unit)
If A <> Subject And Unit(A).Target = Subject Then
CurVal = _
GetDist( _
Unit(Subject).Pos.X - Unit(A).Pos.X, _
Unit(Subject).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If CurVal > BestVal Then BestVal = CurVal
End If
Next A
For A = LBound(Unit) To UBound(Unit)
If A <> Subject And Unit(A).Target = Subject Then
CurVal = _
GetDist( _
Unit(Subject).Pos.X - Unit(A).Pos.X, _
Unit(Subject).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If CurVal < BestVal Then BestVal = CurVal
End If
Next A
For A = LBound(Unit) To UBound(Unit)
If A <> Subject And Unit(A).Target = Subject Then
CurVal = _
GetDist( _
Unit(Subject).Pos.X - Unit(A).Pos.X, _
Unit(Subject).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If CurVal = BestVal Then
NearestThreat = A
Exit Function
End If
End If
Next A
End Function
Function DrawUnits(Scr As PictureBox)
Dim A%
Dim BufferA!
For A = LBound(Unit) To UBound(Unit)
Scr.Circle (Unit(A).Pos.X, Unit(A).Pos.Y), Unit(A).Rad, Unit(A).Color
If A = BestUnit Then
Scr.FillColor = vbGreen
Scr.FillStyle = 0
Scr.Circle (Unit(A).Pos.X, Unit(A).Pos.Y), Unit(A).Rad * 3 / 4, vbGreen
Scr.FillStyle = 1
Scr.FillColor = vbBlack
Scr.DrawStyle = 2
Scr.Circle (Unit(A).Pos.X, Unit(A).Pos.Y), Unit(A).FearRad, vbYellow
Scr.Circle (Unit(A).Pos.X, Unit(A).Pos.Y), Unit(A).AggresRad, vbRed
Scr.DrawStyle = 0
End If
If A = NearestThreat(BestUnit) Then
Scr.FillColor = vbRed
Scr.FillStyle = 0
Scr.Circle (Unit(A).Pos.X, Unit(A).Pos.Y), Unit(A).Rad * 3 / 4, vbRed
Scr.FillStyle = 1
Scr.FillColor = vbBlack
End If
If A = Unit(BestUnit).Target Then
Scr.FillColor = vbYellow
Scr.FillStyle = 0
Scr.Circle (Unit(A).Pos.X, Unit(A).Pos.Y), Unit(A).Rad * 3 / 4, vbYellow
Scr.FillStyle = 1
Scr.FillColor = vbBlack
End If
Scr.Line _
( _
Unit(A).Pos.X, _
Unit(A).Pos.Y _
)- _
( _
GetCoord( _
"Opp", _
Unit(A).Pos.X, _
Unit(A).Pos.Y, _
Unit(A).Dir, _
Unit(A).MoveVel _
), _
GetCoord( _
"Adj", _
Unit(A).Pos.X, _
Unit(A).Pos.Y, _
Unit(A).Dir, _
Unit(A).MoveVel _
) _
), _
vbWhite
If ProxyStatus = True Then
BufferA = _
GetDist( _
Unit(Unit(A).Target).Pos.X - Unit(A).Pos.X, _
Unit(Unit(A).Target).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If BufferA < Unit(A).AggresRad Then
Scr.Circle (Unit(A).Pos.X, Unit(A).Pos.Y), Unit(A).Rad * 3 / 4, vbRed
Scr.DrawStyle = 2
Scr.Line _
( _
Unit(A).Pos.X, _
Unit(A).Pos.Y _
)- _
( _
Unit(Unit(A).Target).Pos.X, _
Unit(Unit(A).Target).Pos.Y _
), _
vbRed
Scr.DrawStyle = 0
Scr.ForeColor = vbRed
Scr.Print Unit(Unit(A).Target).Flag
Scr.ForeColor = vbWhite
End If
If NearestThreat(A) <> 0 Then
BufferA = _
GetDist( _
Unit(NearestThreat(A)).Pos.X - Unit(A).Pos.X, _
Unit(NearestThreat(A)).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If BufferA < Unit(A).FearRad Then
Scr.Circle (Unit(A).Pos.X, Unit(A).Pos.Y), Unit(A).Rad * 3 / 4, vbYellow
Scr.DrawStyle = 2
Scr.Line _
( _
Unit(A).Pos.X, _
Unit(A).Pos.Y _
)- _
( _
Unit(NearestThreat(A)).Pos.X, _
Unit(NearestThreat(A)).Pos.Y _
), _
vbYellow
Scr.DrawStyle = 0
Scr.ForeColor = vbYellow
Scr.Print Unit(NearestThreat(A)).Flag
Scr.ForeColor = vbWhite
End If
End If
End If
Next A
If Bounds = True Then
Scr.DrawWidth = 2
Scr.Line (0, 0)-(Scr.ScaleWidth, Scr.ScaleHeight), vbRed, B
Scr.DrawWidth = 1
End If
End Function
Function ResetDirPos%(Subject%, Scr As PictureBox)
Unit(Subject).Pos.X = Rnd * Scr.ScaleWidth
Unit(Subject).Pos.Y = Rnd * Scr.ScaleHeight
Unit(Subject).Dir = Rnd * 360
End Function
Function ResetEnvCtrls(Scr As PictureBox)
Scr.ForeColor = vbWhite
Tics = 0
TicCheck = 100
Periods = 0
GenCheck = 1
Bounds = False
MsgStatus = False
ProxyStatus = True
End Function
Function ResetTraits(Subject%)
Unit(Subject).Rad = Rnd * 100
Unit(Subject).FearRad = Rnd * 2500
Unit(Subject).AggresRad = Rnd * 2500
Unit(Subject).MoveVel = Rnd * 100
Unit(Subject).TurnVel = Rnd * 180 - 90
End Function
Function ResetUnits(Scr As PictureBox)
Dim A%
Randomize
For A = LBound(Unit) To UBound(Unit)
Unit(A).Flag = 1
Unit(A).State = ""
Unit(A).Color = vbBlack
Unit(A).Target = NearestTarget(A)
Call ResetDirPos(A, Scr)
Call ResetTraits(A)
Next A
End Function
Function TuneUnits()
Dim A%
Dim BufferA!
Dim BufferB!
Dim BufferC!
Dim BufferD!
For A = LBound(Unit) To UBound(Unit)
BufferA = Unit(A).Dir
BufferB = Unit(A).TurnVel
If NearestThreat(A) = 0 Then 'if threat doesn't exist
BufferC = _
GetDist( _
Unit(Unit(A).Target).Pos.X - Unit(A).Pos.X, _
Unit(Unit(A).Target).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If BufferC < Unit(A).AggresRad Then 'if target exists in unit's aggression range
Unit(A).State = "Engaging target"
BufferD = _
GetAng( _
Unit(Unit(A).Target).Pos.X, _
Unit(Unit(A).Target).Pos.Y, _
Unit(A).Pos.X, _
Unit(A).Pos.Y _
)
If Abs(BufferD - BufferA) < 180 Then
If BufferD > BufferA Then BufferB = Abs(BufferB)
If BufferD < BufferA Then BufferB = -Abs(BufferB)
End If
If Abs(BufferD - BufferA) > 180 Then
If BufferD > BufferA Then BufferB = -Abs(BufferB)
If BufferD < BufferA Then BufferB = Abs(BufferB)
End If
Else 'if target exists beyond unit's aggression range
Unit(A).State = "Searching for target"
If Int(Rnd * 2) = 1 Then
BufferB = Abs(BufferB)
Else
BufferB = -Abs(BufferB)
End If
End If
Else 'if threat exists
BufferC = _
GetDist( _
Unit(NearestThreat(A)).Pos.X - Unit(A).Pos.X, _
Unit(NearestThreat(A)).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If BufferC < Unit(A).FearRad Then 'if threat exists in unit's fear range
Unit(A).State = "Evading threat"
BufferD = _
GetAng( _
Unit(NearestThreat(A)).Pos.X, _
Unit(NearestThreat(A)).Pos.Y, _
Unit(A).Pos.X, _
Unit(A).Pos.Y _
)
If Abs(BufferD - BufferA) < 180 Then
If BufferD > BufferA Then BufferB = -Abs(BufferB)
If BufferD < BufferA Then BufferB = Abs(BufferB)
End If
If Abs(BufferD - BufferA) > 180 Then
If BufferD > BufferA Then BufferB = Abs(BufferB)
If BufferD < BufferA Then BufferB = -Abs(BufferB)
End If
Else 'if threat exists beyond unit's fear range
BufferC = _
GetDist( _
Unit(Unit(A).Target).Pos.X - Unit(A).Pos.X, _
Unit(Unit(A).Target).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If BufferC < Unit(A).AggresRad Then 'if target exists in unit's aggression range
Unit(A).State = "Engaging target"
BufferD = _
GetAng( _
Unit(Unit(A).Target).Pos.X, _
Unit(Unit(A).Target).Pos.Y, _
Unit(A).Pos.X, _
Unit(A).Pos.Y _
)
If Abs(BufferD - BufferA) < 180 Then
If BufferD > BufferA Then BufferB = Abs(BufferB)
If BufferD < BufferA Then BufferB = -Abs(BufferB)
End If
If Abs(BufferD - BufferA) > 180 Then
If BufferD > BufferA Then BufferB = -Abs(BufferB)
If BufferD < BufferA Then BufferB = Abs(BufferB)
End If
Else 'if target exists beyond unit's aggression range
Unit(A).State = "Searching for target"
If Int(Rnd * 2) = 1 Then
BufferB = Abs(BufferB)
Else
BufferB = -Abs(BufferB)
End If
End If
End If
End If
Unit(A).TurnVel = BufferB
Next A
End Function
Function UpdateUnits(Scr As PictureBox)
Dim A%
Dim B%
Dim BufferA!
Dim BufferB!
Dim BufferC$
Dim BufferD$
Dim Button%
For A = LBound(Unit) To UBound(Unit)
Unit(A).Dir = Unit(A).Dir + Unit(A).TurnVel
If Unit(A).Dir < 0 Then Unit(A).Dir = 360 + Unit(A).Dir
If Unit(A).Dir >= 360 Then Unit(A).Dir = Unit(A).Dir - 360
Unit(A).Pos.X = _
GetCoord( _
"Opp", _
Unit(A).Pos.X, _
Unit(A).Pos.Y, _
Unit(A).Dir, _
Unit(A).MoveVel _
)
Unit(A).Pos.Y = _
GetCoord( _
"Adj", _
Unit(A).Pos.X, _
Unit(A).Pos.Y, _
Unit(A).Dir, _
Unit(A).MoveVel _
)
If Bounds = True Then
If Unit(A).Pos.X < 0 Then Unit(A).Pos.X = 0
If Unit(A).Pos.X > Scr.ScaleWidth Then Unit(A).Pos.X = Scr.ScaleWidth
If Unit(A).Pos.Y < 0 Then Unit(A).Pos.Y = 0
If Unit(A).Pos.Y > Scr.ScaleHeight Then Unit(A).Pos.Y = Scr.ScaleHeight
Else
If Unit(A).Pos.X < 0 Then Unit(A).Pos.X = Scr.ScaleWidth
If Unit(A).Pos.X > Scr.ScaleWidth Then Unit(A).Pos.X = 0
If Unit(A).Pos.Y < 0 Then Unit(A).Pos.Y = Scr.ScaleHeight
If Unit(A).Pos.Y > Scr.ScaleHeight Then Unit(A).Pos.Y = 0
End If
BufferA = _
GetDist( _
Unit(Unit(A).Target).Pos.X - Unit(A).Pos.X, _
Unit(Unit(A).Target).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If BufferA < (Unit(A).Rad + Unit(Unit(A).Target).Rad) Then
Unit(A).Flag = Unit(A).Flag + 1 'increase predator's evaluation
Unit(Unit(A).Target).Flag = Unit(Unit(A).Target).Flag - 1 'decrease target's evaluation
If Int(Rnd * 2) = 1 Then Unit(Unit(A).Target).Rad = Unit(A).Rad
If Int(Rnd * 2) = 1 Then Unit(Unit(A).Target).MoveVel = Unit(A).MoveVel
If Int(Rnd * 2) = 1 Then Unit(Unit(A).Target).TurnVel = Unit(A).TurnVel
If Int(Rnd * 2) = 1 Then Unit(Unit(A).Target).FearRad = Unit(A).FearRad
If Int(Rnd * 2) = 1 Then Unit(Unit(A).Target).AggresRad = Unit(A).AggresRad
Call ResetDirPos(Unit(A).Target, Scr)
If Unit(A).Target = WorstUnit Or Unit(Unit(A).Target).Flag = 0 Then
Unit(Unit(A).Target).Flag = 1
Call ResetTraits(Unit(A).Target)
End If
Else
BufferA = _
GetDist( _
Unit(NearestTarget(A)).Pos.X - Unit(A).Pos.X, _
Unit(NearestTarget(A)).Pos.Y - Unit(A).Pos.Y, _
0 _
)
If BufferA < Unit(A).AggresRad Then Unit(A).Target = NearestTarget(A)
End If
If Unit(A).Flag >= 0 Then Unit(A).Color = vbBlue
If Unit(A).Flag >= 2 Then Unit(A).Color = vbCyan
If Unit(A).Flag >= 4 Then Unit(A).Color = vbGreen
If Unit(A).Flag >= 6 Then Unit(A).Color = vbYellow
If Unit(A).Flag >= 8 Then Unit(A).Color = vbRed
If Unit(A).Flag >= 10 Then Unit(A).Color = vbMagenta
Next A
If NearestThreat(BestUnit) <> 0 Then
BufferC = NearestThreat(BestUnit)
BufferD = Unit(NearestThreat(BestUnit)).Flag
Else
BufferC = "[None]"
BufferD = "[N/A]"
End If
Status = _
Periods & " period(s) completed" & vbCrLf & _
"with a best record of " & vbCrLf & _
Unit(BestUnit).Flag & " generation(s) by Unit " & BestUnit & vbCrLf & _
vbCrLf & _
"Unit:" & vbTab & BestUnit & vbTab & _
"Generations:" & vbTab & Unit(BestUnit).Flag & vbCrLf & _
"Threat:" & vbTab & BufferC & vbTab & _
"Generations:" & vbTab & BufferD & vbCrLf & _
"Target:" & vbTab & Unit(BestUnit).Target & vbTab & _
"Generations:" & vbTab & Unit(Unit(BestUnit).Target).Flag & vbCrLf & _
vbCrLf & _
"State:" & vbTab & Unit(BestUnit).State & vbCrLf & _
vbCrLf & _
"Fear Radius:" & vbTab & Format(Unit(BestUnit).FearRad, "0.00") & vbTab & " twips" & vbCrLf & _
"Aggres. Radius:" & vbTab & Format(Unit(BestUnit).AggresRad, "0.00") & vbTab & " twips" & vbCrLf & _
"Unit Radius:" & vbTab & Format(Unit(BestUnit).Rad, "0.00") & vbTab & " twips" & vbCrLf & _
"Move Velocity:" & vbTab & Format(Abs(Unit(BestUnit).MoveVel), "0.00") & vbTab & " twips/tic" & vbCrLf & _
"Turn Velocity:" & vbTab & Format(Abs(Unit(BestUnit).TurnVel), "0.00") & vbTab & " degrees/tic"
If MsgStatus = True And Periods <> 0 And Tics = 0 And Periods Mod GenCheck = 0 Then
Button = _
MsgBox( _
Status & vbCrLf & _
vbCrLf & _
"Would you like to reset the population?", _
vbYesNo, _
"Optimization Status" _
)
If Button = vbYes Then Call ResetUnits(Scr)
End If
Tics = Tics + 1
If Tics = TicCheck Then
Tics = 0
Periods = Periods + 1
End If
End Function