-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevolution3.pde
1748 lines (1714 loc) · 50.9 KB
/
evolution3.pde
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/205807*@* */
/* !do not delete the line above, required for linking your tweak if you upload again */
// Last edited January 17, 2013, UNTIL I brought it back on June 19, 2015!
// Okay, that's all the easy to edit stuff.
Configuration config = new Configuration();
final boolean haveGround = true; // true if the ground exists, false if no ground.
final float WINDOW_SIZE = 1.0; // window size multiplier. If it's 1, the size is 1280x720.
float histMinValue = -1; //histogram information
float histMaxValue = 16;
int histBarsPerMeter = 10;
final int FRAMES_PER_SECOND = 60;
final int SIMULATION_FRAMES = config.SIMULATION_SECONDS * FRAMES_PER_SECOND;
PFont font;
ArrayList<Float[]> percentile = new ArrayList<Float[]>(0);
ArrayList<Integer[]> barCounts = new ArrayList<Integer[]>(0);
ArrayList<Integer[]> speciesCounts = new ArrayList<Integer[]>(0);
ArrayList<Integer> topSpeciesCounts = new ArrayList<Integer>(0);
ArrayList<Creature> creatureDatabase = new ArrayList<Creature>(0);
ArrayList<Rectangle> rects = new ArrayList<Rectangle>(0);
// Contains the contents of RECTANGLES as an ArrayList.
ArrayList<Rectangle> baseRects = new ArrayList<Rectangle>(0);
// Historical rectangles in case of rectangles randomization. The index is the
// generation number.
ArrayList<ArrayList<Rectangle>> rectsHistory = new ArrayList<ArrayList<Rectangle>>(0);
PGraphics graphImage;
PGraphics screenImage;
PGraphics popUpImage;
PGraphics segBarImage;
// 0 = 100th percentile
// 1 = 90th percentile
// ...
// 8 = 20th percentile
// 9 = 10th percentile
// 10 = 9th percentile
// 11 = 8th percentile
// ...
// 19 = 0th percentile
final float FRICTION = 4;
int minBar = int(histMinValue*histBarsPerMeter);
int maxBar = int(histMaxValue*histBarsPerMeter);
int barLen = maxBar-minBar;
int gensToDo = 0;
float cTimer = 60;
int windowWidth = 1280;
int windowHeight = 720;
int timer = 0;
float cam = 0;
int frames = 60;
int menu = 0;
// The current generation
int gen = -1;
float sliderX = 1170;
int genSelected = 0;
boolean drag = false;
boolean justGotBack = false;
int creatures = 0;
int creaturesTested = 0;
int fontSize = 0;
int[] fontSizes = {50,36,25,20,16,14,11,9};
int statusWindow = -4;
int overallTimer = 0;
boolean miniSimulation = false;
int creatureWatching = 0;
int simulationTimer = 0;
int[] creaturesInPosition = new int[1000];
float camzoom = 0.015;
float target;
float force;
float average;
int speed;
int id;
boolean stepbystep;
boolean stepbystepslow;
boolean slowDies;
int timeShow;
int[] p = {0,10,20,30,40,50,60,70,80,90,
100,200,300,400,500,600,700,800,900,910,920,930,940,950,960,970,980,990,999};
float inter(int a, int b, float offset){
return float(a)+(float(b)-float(a))*offset;
}
float r(){
return pow(random(-1,1),19);
}
int rInt(){
return int(random(-0.01,1.01));
}
class Rectangle{
float x1, y1, x2, y2;
Rectangle(float tx1, float ty1, float tx2, float ty2){
x1 = tx1;
y1 = ty1;
x2 = tx2;
y2 = ty2;
}
}
class Node{
float x, y, vx, vy, m, f;
Node(float tx, float ty, float tvx, float tvy, float tm, float tf){
x = tx;
y = ty;
vx = tvx;
vy = tvy;
m = tm;
f = tf;
}
void applyForces(int i){
Node ni = n.get(i);
ni.vx *= config.AIR_FRICTION;
ni.vy *= config.AIR_FRICTION;
ni.y += ni.vy;
ni.x += ni.vx;
}
void applyGravity(int i){
Node ni = n.get(i);
ni.vy += config.GRAVITY;
}
void hitWalls(int index){
Node ni = n.get(index);
float dif = ni.y+ni.m/2;
if(dif >= 0 && haveGround){
ni.y = -ni.m/2;
ni.vy = 0;
ni.x -= ni.vx*ni.f;
if(ni.vx > 0){
ni.vx -= ni.f*dif*FRICTION;
if(ni.vx < 0){
ni.vx = 0;
}
}else{
ni.vx += ni.f*dif*FRICTION;
if(ni.vx > 0){
ni.vx = 0;
}
}
}
for(int i = 0; i < rects.size(); i++){
Rectangle r = rects.get(i);
boolean flip = false;
float px, py;
int section = 0;
if(abs(ni.x-(r.x1+r.x2)/2) <= (r.x2-r.x1+ni.m)/2 && abs(ni.y-(r.y1+r.y2)/2) <= (r.y2-r.y1+ni.m)/2){
if(ni.x >= r.x1 && ni.x < r.x2 && ni.y >= r.y1 && ni.y < r.y2){
float d1 = ni.x-r.x1;
float d2 = r.x2-ni.x;
float d3 = ni.y-r.y1;
float d4 = r.y2-ni.y;
if(d1 < d2 && d1 < d3 && d1 < d4){
px = r.x1;
py = ni.y;
section = 3;
}else if(d2 < d3 && d2 < d4){
px = r.x2;
py = ni.y;
section = 5;
}else if(d3 < d4){
px = ni.x;
py = r.y1;
section = 1;
}else{
px = ni.x;
py = r.y2;
section = 7;
}
flip = true;
}else{
if(ni.x < r.x1){
px = r.x1;
section = 0;
}else if(ni.x < r.x2){
px = ni.x;
section = 1;
}else{
px = r.x2;
section = 2;
}
if(ni.y < r.y1){
py = r.y1;
section += 0;
}else if(ni.y < r.y2){
py = ni.y;
section += 3;
}else{
py = r.y2;
section += 6;
}
}
float distance = dist(ni.x,ni.y,px,py);
float rad = ni.m/2;
float wallAngle = 0;
if(distance <= 0.00000001){ // distance is zero, can't use atan2
if(section <= 2){
wallAngle = PI/4.0 + section*PI/4.0;
}else if(section >= 6){
wallAngle = 5*PI/4.0 + (8-section)*PI/4.0;
}else if(section == 3){
wallAngle = PI;
}else if(section == 5 || section == 4){
wallAngle = 0;
}
flip = false;
}else{
wallAngle = atan2(py-ni.y,px-ni.x);
}
if(flip){
wallAngle += PI;
}
if(distance < rad || flip){
dif = rad-distance;
float multi = rad/distance;
if(flip){
multi = -multi;
}
ni.x = (ni.x-px)*multi+px;
ni.y = (ni.y-py)*multi+py;
float veloAngle = atan2(ni.vy,ni.vx);
float veloMag = dist(0,0,ni.vx,ni.vy);
float relAngle = veloAngle-wallAngle;
float relY = sin(relAngle)*veloMag*dif*FRICTION;
ni.vx = -sin(relAngle)*relY;
ni.vy = cos(relAngle)*relY;
}
}
}
}
Node copyNode(){
return (new Node(x,y,0,0,m,f));
}
Node modifyNode(float mutability){
float newX = x+r()*0.5*mutability*config.MUTABILITY_FACTOR;
float newY = y+r()*0.5*mutability*config.MUTABILITY_FACTOR;
float newM = m+r()*0.1*mutability*config.MUTABILITY_FACTOR;
newM = min(max(newM,config.MINIMUM_NODE_SIZE),config.MAXIMUM_NODE_SIZE);
float newF = f+r()*0.1*mutability*config.MUTABILITY_FACTOR;
newF = min(max(newF,config.MINIMUM_NODE_FRICTION),config.MAXIMUM_NODE_FRICTION);
return (new Node(newX,newY,0,0,newM,newF));//max(m+r()*0.1,0.2),min(max(f+r()*0.1,0),1)
}
}
class Muscle{
int period, c1, c2;
float contractTime,contractLength, extendTime, extendLength;
float thruPeriod;
boolean contracted;
float rigidity;
Muscle(int tperiod, int tc1, int tc2, float tcontractTime,
float textendTime, float tcontractLength, float textendLength, boolean tcontracted, float trigidity){
period = tperiod;
c1 = tc1;
c2 = tc2;
contractTime = tcontractTime;
extendTime = textendTime;
contractLength = tcontractLength;
extendLength = textendLength;
contracted = tcontracted;
rigidity = trigidity;
}
void applyForce(int i, float target){
Node ni1 = n.get(c1);
Node ni2 = n.get(c2);
float distance = dist(ni1.x,ni1.y,ni2.x,ni2.y);
float angle = atan2(ni1.y-ni2.y,ni1.x-ni2.x);
force = min(max(1-(distance/target),-0.4),0.4);
ni1.vx += cos(angle)*force*rigidity/ni1.m;
ni1.vy += sin(angle)*force*rigidity/ni1.m;
ni2.vx -= cos(angle)*force*rigidity/ni2.m;
ni2.vy -= sin(angle)*force*rigidity/ni2.m;
}
Muscle copyMuscle(){
return new Muscle(period,c1,c2,contractTime,extendTime,
contractLength,extendLength,contracted,rigidity);
}
Muscle modifyMuscle(int nodeNum,float mutability){
int newc1 = c1;
int newc2 = c2;
if(random(0,1)<0.02*mutability*config.MUTABILITY_FACTOR){
newc1 = int(random(0,nodeNum));
}
if(random(0,1)<0.02*mutability*config.MUTABILITY_FACTOR){
newc2 = int(random(0,nodeNum));
}
float newR = min(max(rigidity*(1+r()*0.9*mutability*config.MUTABILITY_FACTOR),0.01),0.08);
float maxMuscleChange = 1+0.025/newR;
float newCL = min(max(contractLength+r()*mutability*config.MUTABILITY_FACTOR,0.4),2);
float newEL = min(max(extendLength+r()*mutability*config.MUTABILITY_FACTOR,0.4),2);
float newCL2 = min(newCL,newEL);
float newEL2 = min(max(newCL,newEL),newCL2*maxMuscleChange);
float newCT = contractTime;
float newET = extendTime;
if(random(0,1) < 0.5){ //contractTime is changed
newCT = ((contractTime-extendTime)*r()*mutability*config.MUTABILITY_FACTOR+newCT+1)%1;
}else{ //extendTime is changed
newET = ((extendTime-contractTime)*r()*mutability*config.MUTABILITY_FACTOR+newET+1)%1;
}
return new Muscle(max(period+rInt(),0),
newc1,newc2,newCT,newET,newCL2,newEL2,isItContracted(newCT,newET),newR);
}
}
class Creature{
ArrayList<Node> n;
ArrayList<Muscle> m;
float d;
int id;
boolean alive;
float creatureTimer;
float mutability;
Creature(int tid, ArrayList<Node> tn, ArrayList<Muscle> tm, float td, boolean talive, float tct, float tmut){
id = tid;
m = tm;
n = tn;
d = td;
alive = talive;
creatureTimer = tct;
mutability = tmut;
}
Creature modified(int id){
Creature modifiedCreature = new Creature(id,
new ArrayList<Node>(0),new ArrayList<Muscle>(0),0,true,
creatureTimer+r()*16*mutability*config.MUTABILITY_FACTOR,min(mutability*config.MUTABILITY_FACTOR*random(0.8,1.25),2));
for(int i = 0; i < n.size(); i++){
modifiedCreature.n.add(n.get(i).modifyNode(mutability));
}
for(int i = 0; i < m.size(); i++){
modifiedCreature.m.add(m.get(i).modifyMuscle(n.size(),mutability));
}
if(random(0,1) < 0.04*mutability*config.MUTABILITY_FACTOR || n.size() <= config.CREATURE_MIN_NODES - 1){ //Add a node
modifiedCreature.addRandomNode();
}
if(random(0,1) < 0.04*mutability*config.MUTABILITY_FACTOR){ //Add a muscle
modifiedCreature.addRandomMuscle(-1,-1);
}
if(random(0,1) < 0.04*mutability*config.MUTABILITY_FACTOR && modifiedCreature.n.size() >= config.CREATURE_MIN_NODES + 1){ //Remove a node
modifiedCreature.removeRandomNode();
}
if(random(0,1) < 0.04*mutability*config.MUTABILITY_FACTOR && modifiedCreature.m.size() >= config.CREATURE_MIN_MUSCLES + 1){ //Remove a muscle
modifiedCreature.removeRandomMuscle();
}
modifiedCreature.checkForOverlap();
modifiedCreature.checkForLoneNodes();
return modifiedCreature;
}
void checkForOverlap(){
ArrayList<Integer> bads = new ArrayList<Integer>();
for(int i = 0; i < m.size(); i++){
for(int j = i+1; j < m.size(); j++){
if(m.get(i).c1 == m.get(j).c1 && m.get(i).c2 == m.get(j).c2){
bads.add(i);
}else if(m.get(i).c1 == m.get(j).c2 && m.get(i).c2 == m.get(j).c1){
bads.add(i);
}else if(m.get(i).c1 == m.get(i).c2){
bads.add(i);
}
}
}
for(int i = bads.size()-1; i >= 0; i--){
int b = bads.get(i)+0;
if(b < m.size()){
m.remove(b);
}
}
}
void checkForLoneNodes(){
if(n.size() >= 3){
for(int i = 0; i < n.size(); i++){
int connections = 0;
int connectedTo = -1;
for(int j = 0; j < m.size(); j++){
if(m.get(j).c1 == i){
connections++;
connectedTo = m.get(j).c2;
}else if(m.get(j).c2 == i){
connections++;
connectedTo = m.get(j).c1;
}
}
if(connections <= 1){
int newConnectionNode = floor(random(0,n.size()));
while(newConnectionNode == i || newConnectionNode == connectedTo){
newConnectionNode = floor(random(0,n.size()));
}
addRandomMuscle(i,newConnectionNode);
}
}
}
}
void addRandomNode(){
int parentNode = floor(random(0,n.size()));
float ang1 = random(0,2*PI);
float distance = sqrt(random(0,1));
float x = n.get(parentNode).x+cos(ang1)*0.5*distance;
float y = n.get(parentNode).y+sin(ang1)*0.5*distance;
n.add(new Node(x,y,0,0,random(config.MINIMUM_NODE_SIZE,config.MAXIMUM_NODE_SIZE),
random(config.MINIMUM_NODE_FRICTION,config.MAXIMUM_NODE_FRICTION))); //random(0.1,1),random(0,1)
int nextClosestNode = 0;
float record = 100000;
for(int i = 0; i < n.size()-1; i++){
if(i != parentNode){
float dx = n.get(i).x-x;
float dy = n.get(i).y-y;
if(sqrt(dx*dx+dy*dy) < record){
record = sqrt(dx*dx+dy*dy);
nextClosestNode = i;
}
}
}
addRandomMuscle(parentNode,n.size()-1);
addRandomMuscle(nextClosestNode,n.size()-1);
}
void addRandomMuscle(int tc1, int tc2){
if(tc1 == -1){
tc1 = int(random(0,n.size()));
tc2 = tc1;
while(tc2 == tc1 && n.size() >= 2){
tc2 = int(random(0,n.size()));
}
}
float rlength1 = random(0.5,1.5);
float rlength2 = random(0.5,1.5);
float rtime1 = random(0,1);
float rtime2 = random(0,1);
if(tc1 != -1){
float distance = dist(n.get(tc1).x,n.get(tc1).y,n.get(tc2).x,n.get(tc2).y);
float ratio = random(0.01,0.2);
rlength1 = distance*(1-ratio);
rlength2 = distance*(1+ratio);
}
m.add(new Muscle(int(random(1,3)),tc1,tc2,rtime1,rtime2,
min(rlength1,rlength2),max(rlength1,rlength2),isItContracted(rtime1,rtime2),random(0.02,0.08)));
}
void removeRandomNode(){
int choice = floor(random(0,n.size()));
n.remove(choice);
int i = 0;
while(i < m.size()){
if(m.get(i).c1 == choice || m.get(i).c2 == choice){
m.remove(i);
}else{
i++;
}
}
for(int j = 0; j < m.size(); j++){
if(m.get(j).c1 >= choice){
m.get(j).c1--;
}
if(m.get(j).c2 >= choice){
m.get(j).c2--;
}
}
}
void removeRandomMuscle(){
int choice = floor(random(0,m.size()));
m.remove(choice);
}
Creature copyCreature(int newID){
ArrayList<Node> n2 = new ArrayList<Node>(0);
ArrayList<Muscle> m2 = new ArrayList<Muscle>(0);
for(int i = 0; i < n.size(); i++){
n2.add(n.get(i).copyNode());
}
for(int i = 0; i < m.size(); i++){
m2.add(m.get(i).copyMuscle());
}
if(newID == -1){
newID = id;
}
return new Creature(newID,n2,m2,d,alive,creatureTimer,mutability);
}
}
void drawGround(int toImage){
if(toImage == 0){
noStroke();
fill(0,130,0);
if(haveGround) rect(0,windowHeight*0.8,windowWidth,windowHeight*0.2);
for(int i = 0; i < rects.size(); i++){
Rectangle r = rects.get(i);
rect(r.x1/camzoom-cam/camzoom+windowWidth/2,r.y1/camzoom+windowHeight*0.8,(r.x2-r.x1)/camzoom,(r.y2-r.y1)/camzoom);
}
}else if(toImage == 2){
popUpImage.noStroke();
popUpImage.fill(0,130,0);
if(haveGround) popUpImage.rect(0,360,450,90);
float ww = 450;
float wh = 450;
for(int i = 0; i < rects.size(); i++){
Rectangle r = rects.get(i);
popUpImage.rect(r.x1/camzoom-cam/camzoom+ww/2,r.y1/camzoom+wh*0.8,(r.x2-r.x1)/camzoom,(r.y2-r.y1)/camzoom);
}
}
}
void drawNode(Node ni, float x, float y,int toImage){
color c = color(512-int(ni.f*512),0,0);
if(ni.f <= 0.5){
c = color(255,255-int(ni.f*512),255-int(ni.f*512));
}
if(toImage == 0){
fill(c);
noStroke();
ellipse(ni.x/camzoom+x,ni.y/camzoom+y,ni.m/camzoom,ni.m/camzoom);
}else if(toImage == 1){
screenImage.fill(c);
screenImage.noStroke();
screenImage.ellipse(ni.x/camzoom+x,ni.y/camzoom+y,ni.m/camzoom,ni.m/camzoom);
}else if(toImage == 2){
popUpImage.fill(c);
popUpImage.noStroke();
popUpImage.ellipse(ni.x/camzoom+x,ni.y/camzoom+y,ni.m/camzoom,ni.m/camzoom);
}
}
void drawMuscle(Muscle mi, Node ni1, Node ni2, float x,float y,int toImage){
boolean c = mi.contracted;
float w = 0.1/camzoom;
if(c){
w = 0.2/camzoom;
}
if(toImage == 0){
strokeWeight(w);
stroke(70,35,0,mi.rigidity*3000);
line(ni1.x/camzoom+x, ni1.y/camzoom+y, ni2.x/camzoom+x, ni2.y/camzoom+y);
}else if(toImage == 1){
screenImage.strokeWeight(w);
screenImage.stroke(70,35,0,mi.rigidity*3000);
screenImage.line(ni1.x/camzoom+x, ni1.y/camzoom+y, ni2.x/camzoom+x, ni2.y/camzoom+y);
}else if(toImage == 2){
popUpImage.strokeWeight(w);
popUpImage.stroke(70,35,0,mi.rigidity*3000);
popUpImage.line(ni1.x/camzoom+x, ni1.y/camzoom+y, ni2.x/camzoom+x, ni2.y/camzoom+y);
}
}
void drawPosts(int toImage){
if(toImage == 0){
noStroke();
for(int i = int((-cam*camzoom-windowWidth/2)/5)-1;
i <= int((-cam*camzoom+windowWidth/2)/5)+1; i++){
fill(255);
rect(windowWidth/2+(i*5-cam-0.1)/camzoom,windowHeight*0.8-3/camzoom,0.2/camzoom,3/camzoom);
rect(windowWidth/2+(i*5-cam-1)/camzoom,windowHeight*0.8-3/camzoom,2/camzoom,1/camzoom);
fill(120);
text(i+" m",windowWidth/2+(i*5-cam)/camzoom,windowHeight*0.8-2.17/camzoom);
}
}else if(toImage == 2){
popUpImage.textAlign(CENTER);
popUpImage.textFont(font, 0.96/camzoom);
popUpImage.noStroke();
float w = 450;
float h = 450;
for(int i = int((-cam*camzoom-w/2)/5)-1;
i <= int((-cam*camzoom+w/2)/5)+1; i++){
popUpImage.fill(255);
popUpImage.rect(w/2+(i*5-cam-0.1)/camzoom,h*0.8-3/camzoom,0.2/camzoom,3/camzoom);
popUpImage.rect(w/2+(i*5-cam-1)/camzoom,h*0.8-3/camzoom,2/camzoom,1/camzoom);
popUpImage.fill(120);
popUpImage.text(i+" m",w/2+(i*5-cam)/camzoom,h*0.8-2.17/camzoom);
}
}
}
void drawArrow(float x){
noStroke();
fill(120,0,255);
rect(windowWidth/2+(x-cam-1.7)/camzoom,windowHeight*0.8-4.8/camzoom,3.4/camzoom,1.1/camzoom);
beginShape();
vertex(windowWidth/2+(x-cam)/camzoom,windowHeight*0.8-3.2/camzoom);
vertex(windowWidth/2+(x-cam-0.5)/camzoom,windowHeight*0.8-3.7/camzoom);
vertex(windowWidth/2+(x-cam+0.5)/camzoom,windowHeight*0.8-3.7/camzoom);
endShape(CLOSE);
fill(255);
text((float(round(x*2))/10)+" m",windowWidth/2+(x-cam)/camzoom,windowHeight*0.8-3.91/camzoom);
}
void drawGraphImage(){
image(graphImage,50,180,650,380);
image(segBarImage,50,580,650,100);
if(gen >= 1){
stroke(0,160,0,255);
strokeWeight(3);
float genWidth = float(610)/gen;
float lineX = 90+genSelected*genWidth;
line(lineX,180,lineX,500+180);
Integer[] s = speciesCounts.get(genSelected);
textAlign(LEFT);
textFont(font,12);
noStroke();
for(int i = 1; i < 101; i++){
int c = s[i]-s[i-1];
if(c >= 25){
float y = ((s[i]+s[i-1])/2)/1000.0*100+573;
if(i-1 == topSpeciesCounts.get(genSelected)){
stroke(0);
strokeWeight(2);
}else{
noStroke();
}
fill(255,255,255);
rect(lineX+10,y,50,14);
colorMode(HSB,1.0);
fill(getColor(i-1,true));
text("S"+floor((i-1)/10)+""+((i-1)%10)+": "+c,lineX+11,y+12);
colorMode(RGB,255);
}
}
noStroke();
}
}
color getColor(int i, boolean adjust){
colorMode(HSB,1.0);
float col = (i*1.618034)%1;
if(i == 46){
col = 0.083333;
}else if(i == 44){
col = 0.1666666;
}else if(i == 57){
col = 0.5;
}
float light = 1.0;
if(abs(col-0.333) <= 0.18 && adjust){
light = 0.7;
}
return color(col,1.0,light);
}
void drawGraph(int graphWidth, int graphHeight){
drawLines(60,int(graphHeight*0.05),graphWidth-60,int(graphHeight*0.9));
if(gen >= 1){
drawSegBars(60,0,graphWidth-60,150);
}
}
void drawLines(int x, int y, int graphWidth, int graphHeight){
graphImage.beginDraw();
graphImage.smooth();
graphImage.background(220);
if(gen >= 1){
float gh = float(graphHeight);
float genWidth = float(graphWidth)/gen;
float best = extreme(1);
float worst = extreme(-1);
float meterHeight = float(graphHeight)/(best-worst);
float zero = (best/(best-worst))*gh;
float unit = setUnit(best, worst);
graphImage.stroke(150);
graphImage.strokeWeight(2);
graphImage.fill(150);
graphImage.textFont(font, 18);
graphImage.textAlign(RIGHT);
for(float i = ceil((worst-(best-worst)/18.0)/unit)*unit; i < best+(best-worst)/18.0;i+=unit){
float lineY = y-i*meterHeight+zero;
graphImage.line(x,lineY,graphWidth+x,lineY);
graphImage.text(showUnit(i,unit)+" m",x-5,lineY+4);
}
graphImage.stroke(0);
for(int i = 0; i < 29; i++){
int k;
if(i == 28){
k = 14;
}else if(i < 14){
k = i;
}else{
k = i+1;
}
if(k == 14){
graphImage.stroke(255,0,0,255);
graphImage.strokeWeight(5);
}else{
stroke(0);
if(k == 0 || k == 28 || (k >= 10 && k <= 18)){
graphImage.strokeWeight(3);
}else{
graphImage.strokeWeight(1);
}
}
for(int j = 0; j < gen; j++){
graphImage.line(x+j*genWidth,(-percentile.get(j)[k])*meterHeight+zero+y,
x+(j+1)*genWidth,(-percentile.get(j+1)[k])*meterHeight+zero+y);
}
}
}
graphImage.endDraw();
}
void drawSegBars(int x, int y, int graphWidth, int graphHeight){
segBarImage.beginDraw();
segBarImage.smooth();
segBarImage.noStroke();
segBarImage.colorMode(HSB, 1);
segBarImage.background(0,0,0.5);
float genWidth = float(graphWidth)/gen;
int gensPerBar = floor(gen/500)+1;
for(int i = 0; i < gen; i+=gensPerBar){
int i2 = min(i+gensPerBar,gen);
float barX1 = x+i*genWidth;
float barX2 = x+i2*genWidth;
int cum = 0;
for(int j = 0; j < 100; j++){
segBarImage.fill(getColor(j,false));
segBarImage.beginShape();
segBarImage.vertex(barX1,y+speciesCounts.get(i)[j]/1000.0*graphHeight);
segBarImage.vertex(barX1,y+speciesCounts.get(i)[j+1]/1000.0*graphHeight);
segBarImage.vertex(barX2,y+speciesCounts.get(i2)[j+1]/1000.0*graphHeight);
segBarImage.vertex(barX2,y+speciesCounts.get(i2)[j]/1000.0*graphHeight);
segBarImage.endShape();
}
}
segBarImage.endDraw();
colorMode(RGB, 255);
}
float extreme(float sign){
float record = -sign;
for(int i = 0; i < gen; i++){
float toTest = percentile.get(i+1)[int(14-sign*14)];
if(toTest*sign > record*sign){
record = toTest;
}
}
return record;
}
float setUnit(float best, float worst){
float unit2 = 3*log(best-worst)/log(10)-3.3;
if((unit2+100)%3 < 1){
return pow(10,int(unit2/3));
}else if((unit2+100)%3 < 2){
return pow(10,int((unit2-1)/3))*2;
}else{
return pow(10,int((unit2-2)/3))*5;
}
}
String showUnit(float i, float unit){
if(unit < 1){
return nf(i,0,2)+"";
}else{
return int(i)+"";
}
}
ArrayList<Creature> quickSort(ArrayList<Creature> c){
if(c.size() <= 1){
return c;
}else{
ArrayList<Creature> less = new ArrayList<Creature>();
ArrayList<Creature> more = new ArrayList<Creature>();
ArrayList<Creature> equal = new ArrayList<Creature>();
Creature c0 = c.get(0);
equal.add(c0);
for(int i = 1; i < c.size(); i++){
Creature ci = c.get(i);
if(ci.d == c0.d){
equal.add(ci);
}else if(ci.d > c0.d){
more.add(ci);
}else{
less.add(ci);
}
}
ArrayList<Creature> total = new ArrayList<Creature>();
total.addAll(quickSort(more));
total.addAll(equal);
total.addAll(quickSort(less));
return total;
}
}
boolean isItContracted(float rtime1, float rtime2){
boolean contracted;
if(rtime1 <= rtime2){
return true;
}else{
return false;
}
}
void toStableConfiguration(int nodeNum, int muscleNum){
for(int j = 0; j < 200; j++){
for(int i = 0; i < muscleNum; i++){
Muscle mi = m.get(i);
if(mi.contracted){
target = mi.contractLength;
}else{
target = mi.extendLength;
}
mi.applyForce(i,target);
}
for(int i = 0; i < nodeNum; i++){
Node ni = n.get(i);
ni.applyForces(i);
}
}
for(int i = 0; i < nodeNum; i++){
Node ni = n.get(i);
ni.vx = 0;
ni.vy = 0;
}
}
void adjustToCenter(int nodeNum){
float avx = 0;
float lowY = -1000;
for(int i = 0; i < nodeNum; i++){
Node ni = n.get(i);
avx += ni.x;
if(ni.y+ni.m/2 > lowY){
lowY = ni.y+ni.m/2;
}
}
avx /= nodeNum;
for(int i = 0; i < nodeNum; i++){
Node ni = n.get(i);
ni.x -= avx;
ni.y -= lowY;
}
}
void setGlobalVariables(Creature thisCreature){
n.clear();
m.clear();
for(int i = 0; i < thisCreature.n.size(); i++){
n.add(thisCreature.n.get(i).copyNode());
}
for(int i = 0; i < thisCreature.m.size(); i++){
m.add(thisCreature.m.get(i).copyMuscle());
}
id = thisCreature.id;
timer = 0;
camzoom = 0.01;
cam = 0;
cTimer = thisCreature.creatureTimer;
simulationTimer = 0;
}
void simulate(){
for(int i = 0; i < m.size(); i++){
Muscle mi = m.get(i);
mi.thruPeriod = ((float(timer)/cTimer)/float(mi.period))%float(1);
if((mi.thruPeriod <= mi.extendTime && mi.extendTime <= mi.contractTime) ||
(mi.contractTime <= mi.thruPeriod && mi.thruPeriod <= mi.extendTime) ||
(mi.extendTime <= mi.contractTime && mi.contractTime <= mi.thruPeriod)){
target = mi.contractLength;
mi.contracted = true;
}else{
target = mi.extendLength;
mi.contracted = false;
}
mi.applyForce(i,target);
}
for(int i = 0; i < n.size(); i++){
Node ni = n.get(i);
ni.applyForces(i);
ni.applyGravity(i);
ni.hitWalls(i);
}
simulationTimer++;
}
// Calculate the distance for the current creature, by taking the average of the x coordinates
// of all its nodes.
void setAverage(){
average = 0;
for(int i = 0; i < n.size(); i++){
Node ni = n.get(i);
average += ni.x;
}
average = average/n.size();
// Workaround for bug where creatures fall through blocks and get an enormous distance (in the millions)
if(abs(average) > 10000) {
writeLog ("Discarding erroneous result of " + average);
average = 0;
}
}
ArrayList<Node> n = new ArrayList<Node>();
ArrayList<Muscle> m = new ArrayList<Muscle>();
Creature[] c = new Creature[1000];
ArrayList<Creature> c2 = new ArrayList<Creature>();
void mouseWheel(MouseEvent event) {
int delta = -1;//event.getCount();
if(menu == 5){
if(delta == -1){
camzoom *= 0.9090909;
if(camzoom < 0.006){
camzoom = 0.006;
}
textFont(font, 0.96/camzoom);
}else if(delta == 1){
camzoom *= 1.1;
if(camzoom > 0.1){
camzoom = 0.1;
}
textFont(font, 0.96/camzoom);
}
}
}
void mousePressed(){
if(gensToDo >= 1){
gensToDo = 0;
}
float mX = mouseX/WINDOW_SIZE;
float mY = mouseY/WINDOW_SIZE;
if(menu == 1 && gen >= 1 && abs(mY-365) <= 25 && abs(mX-sliderX-25) <= 25){
drag = true;
}
}
void openMiniSimulation(){
simulationTimer = 0;
speed = config.MINI_SIMULATION_SPEED;
// In the main window, the animation performs slower. Increase speed to let it show at normal speed.
if(statusWindow < 0)
speed = speed * 2;
if(gensToDo == 0){
miniSimulation = true;
int id;
Creature cj;
if(statusWindow <= -1){
// Load rectangles for selected generation
int simGen = genSelected - 1;
rects = rectsHistory.get(simGen);
cj = creatureDatabase.get(simGen*3+statusWindow+3);
id = cj.id;
}else{
// Showing a creature in the creature overview, after sorting.
id = statusWindow;
cj = c2.get(id);
}
setGlobalVariables(cj);
creatureWatching = id;
}
}
void setMenu(int m){
menu = m;
if(m == 1){
drawGraph(975,570);
}
}
void startASAP(){
setMenu(4);
creaturesTested = 0;
stepbystep = false;
stepbystepslow = false;
}
void mouseReleased(){
drag = false;
miniSimulation = false;
float mX = mouseX/WINDOW_SIZE;
float mY = mouseY/WINDOW_SIZE;
if(menu == 0 && abs(mX-windowWidth/2) <= 200 && abs(mY-400) <= 100){
setMenu(1);
}else if(menu == 1 && gen == -1 && abs(mX-120) <= 100 && abs(mY-300) <= 50){
setMenu(2);
}else if(menu == 1 && gen >= 0 && abs(mX-990) <= 230){
if(abs(mY-40) <= 20){
// Full animation of a generation
setMenu(4);
creaturesTested = 0;
stepbystep = true;
stepbystepslow = true;
}
if(abs(mY-90) <= 20){
// 1 quick generation
setMenu(4);
creaturesTested = 0;
stepbystep = true;
stepbystepslow = false;
}
if(abs(mY-140) <= 20){
// ASAP generation, 1 or unlimited (ALAP)
if(mX < 990){
gensToDo = 1;
}else{
gensToDo = 1000000000;
}
startASAP();
}
}else if(menu == 3 && abs(mX-1030) <= 130 && abs(mY-684) <= 20){
gen = 0;
setMenu(1);
}else if(menu == 7 && abs(mX-1030) <= 130 && abs(mY-684) <= 20){
setMenu(8);
}else if(menu == 9 && abs(mX-1030) <= 130 && abs(mY-690) <= 20){
setMenu(10);
}else if(menu == 11 && abs(mX-1130) <= 80 && abs(mY-690) <= 20){
setMenu(12);
}else if(menu == 13 && abs(mX-1130) <= 80 && abs(mY-690) <= 20){
setMenu(1);
}
}
void drawScreenImage(int stage){
screenImage.beginDraw();
screenImage.smooth();
screenImage.background(220,253,102);
screenImage.noStroke();
camzoom = 0.12;
for(int j = 0; j < 1000; j++){
Creature cj = c2.get(j);
if(stage == 3) cj = c[cj.id-(gen*1000)-1001];
int j2 = j;
if(stage == 0){
j2 = cj.id-(gen*1000)-1;
creaturesInPosition[j2] = j;
}
int x = j2%40;
int y = floor(j2/40);