forked from karpathy/scriptsbots
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWorld.cpp
More file actions
2033 lines (1779 loc) · 66.7 KB
/
Copy pathWorld.cpp
File metadata and controls
2033 lines (1779 loc) · 66.7 KB
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
#include "World.h"
#include <ctime>
#include "settings.h"
#include "helpers.h"
#include "vmath.h"
#ifdef _WIN32
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <stdio.h>
#include <iostream>
using namespace std;
World::World() :
CLOSED(false),
DEBUG(false),
SELECTION(-1),
pcontrol(false),
pright(0),
pleft(0),
pinput1(0)
{
//inititalize
//ALL .cfg constants must be initially declared in world.h and defined here.
MINFOOD= conf::MINFOOD;
INITFOODDENSITY= conf::INITFOODDENSITY;
INITFRUITDENSITY= conf::INITFRUITDENSITY;
NUMBOTS= conf::NUMBOTS;
ENOUGHBOTS= conf::ENOUGHBOTS;
TOOMANYBOTS= conf::TOOMANYBOTS;
REPORTS_PER_EPOCH= conf::REPORTS_PER_EPOCH;
FRAMES_PER_EPOCH= conf::FRAMES_PER_EPOCH;
FRAMES_PER_DAY= conf::FRAMES_PER_DAY;
CONTINENTS= conf::CONTINENTS;
OCEANPERCENT= conf::OCEANPERCENT;
GRAVITYACCEL= conf::GRAVITYACCEL;
REACTPOWER= conf::REACTPOWER;
SPIKEMULT= conf::SPIKEMULT;
BOTSPEED= conf::BOTSPEED;
BOOSTSIZEMULT= conf::BOOSTSIZEMULT;
SOUNDPITCHRANGE= conf::SOUNDPITCHRANGE;
FOODTRANSFER= conf::FOODTRANSFER;
// MEANRADIUS= conf::MEANRADIUS;
SPIKESPEED= conf::SPIKESPEED;
FRESHKILLTIME= conf::FRESHKILLTIME;
TENDERAGE= conf::TENDERAGE;
MINMOMHEALTH= conf::MINMOMHEALTH;
// REPRATE= conf::REPRATE;
// LEARNRATE= conf::LEARNRATE;
MAXDEVIATION= conf::MAXDEVIATION;
MAXAGE= conf::MAXAGE;
DIST= conf::DIST;
SPIKELENGTH= conf::SPIKELENGTH;
TOOCLOSE= conf::TOOCLOSE;
FOOD_SHARING_DISTANCE= conf::FOOD_SHARING_DISTANCE;
SEXTING_DISTANCE= conf::SEXTING_DISTANCE;
GRABBING_DISTANCE= conf::GRABBING_DISTANCE;
HEALTHLOSS_WHEELS= conf::HEALTHLOSS_WHEELS;
HEALTHLOSS_BOOSTMULT= conf::HEALTHLOSS_BOOSTMULT;
HEALTHLOSS_BADTEMP= conf::HEALTHLOSS_BADTEMP;
HEALTHLOSS_AGING= conf::HEALTHLOSS_AGING;
HEALTHLOSS_BRAINUSE= conf::HEALTHLOSS_BRAINUSE;
HEALTHLOSS_BUMP= conf::HEALTHLOSS_BUMP;
HEALTHLOSS_SPIKE_EXT= conf::HEALTHLOSS_SPIKE_EXT;
HEALTHLOSS_BADAIR= conf::HEALTHLOSS_BADAIR;
HEALTHLOSS_NOOXYGEN= conf::HEALTHLOSS_NOOXYGEN;
HEALTHLOSS_ASSEX= conf::HEALTHLOSS_ASSEX;
HEALTHLOSS_JAWSNAP= conf::HEALTHLOSS_JAWSNAP;
FOODINTAKE= conf::FOODINTAKE;
FOODDECAY= conf::FOODDECAY;
FOODGROWTH= conf::FOODGROWTH;
FOODWASTE= conf::FOODWASTE;
FOODADDFREQ= conf::FOODADDFREQ;
FOODSPREAD= conf::FOODSPREAD;
FOODRANGE= conf::FOODRANGE;
FRUITINTAKE= conf::FRUITINTAKE;
FRUITDECAY= conf::FRUITDECAY;
FRUITWASTE= conf::FRUITWASTE;
FRUITADDFREQ= conf::FRUITADDFREQ;
FRUITREQUIRE= conf::FRUITREQUIRE;
MEATINTAKE= conf::MEATINTAKE;
MEATDECAY= conf::MEATDECAY;
MEATWASTE= conf::MEATWASTE;
MEATVALUE= conf::MEATVALUE;
HAZARDFREQ= conf::HAZARDFREQ;
HAZARDDECAY= conf::HAZARDDECAY;
HAZARDDEPOSIT= conf::HAZARDDEPOSIT;
HAZARDDAMAGE= conf::HAZARDDAMAGE;
reset();
spawn();
printf("WORLD MADE!\n");
}
void World::reset()
{
current_epoch= 0;
modcounter= 0;
idcounter= 0;
//try loading constants config
readConfig();
//tidy up constants
INITFOOD = (int)(INITFOODDENSITY*conf::WIDTH*conf::HEIGHT);
INITFRUIT = (int)(INITFRUITDENSITY*conf::WIDTH*conf::HEIGHT);
CW= conf::WIDTH/conf::CZ;
CH= conf::HEIGHT/conf::CZ; //note: should add custom variables from loaded savegames here possibly
printf("sanitizing agents.\n");
agents.clear();
//handle layers
for(int cx=0; cx<(int)CW; cx++){
for(int cy=0; cy<(int)CH; cy++){
for(int l=0;l<Layer::LAYERS;l++){
cells[l][cx][cy]= 0;
}
// cells[TEMPLAYER][cx][cy]= 2.0*abs((float)cy/CH - 0.5); [old temperature indicating code]
}
}
//open report file; null it up if it exists
FILE* fr = fopen("report.txt", "w");
fclose(fr);
ptr=0;
}
void World::spawn()
{
printf("growing food.\n");
while(getFood()<INITFOOD) {
//pollinate plants
int rx= randi(0,CW);
int ry= randi(0,CH);
if(cells[Layer::PLANTS][rx][ry]!=0) continue;
cells[Layer::PLANTS][rx][ry] = conf::FOODMAX;
if (getFruit()<INITFRUIT) {
//pollinate fruit
cells[Layer::FRUITS][rx][ry] = conf::FRUITMAX;
}
}
//spawn land masses
cellsLandMasses();
//add init agents
printf("programming bots.\n");
addAgents(NUMBOTS, Stomach::PLANT);
}
void World::cellsLandMasses()
{
//creates land masses for the layer given
int leftcount= CW*CH;
printf("clearing land.\n");
for(int i=0;i<CW;i++) {
for(int j=0;j<CH;j++) {
cells[Layer::LAND][i][j]= -1; //"null" all cells
}
}
for (int i=0;i<1.5*(1-pow(OCEANPERCENT,6))*(CONTINENTS+1);i++) {
//spawn init continents (land= 1)
int cx=randi(0,CW);
int cy=randi(0,CH);
cells[Layer::LAND][cx][cy]= 1;
}
for (int i=0;i<1.5*(sqrt((float)CW*CH)/1000*pow((float)2.5,5*OCEANPERCENT)*(CONTINENTS+1))-1;i++) {
//spawn oceans (water= 0)
int cx=randi(0,CW);
int cy=randi(0,CH);
cells[Layer::LAND][cx][cy]= 0;
}
printf("moving tectonic plates.\n");
while(leftcount!=0){
for(int i=0;i<CW;i++) {
for(int j=0;j<CH;j++) {
//land spread
if (cells[Layer::LAND][i][j]==1){
int ox= randi(i-1,i+2);
int oy= randi(j-1,j+2);
if (ox<0) ox+= CW;
if (ox>CW-1) ox-= CW;
if (oy<0) oy+= CH;
if (oy>CH-1) oy-= CH;
if (cells[Layer::LAND][ox][oy]==-1 && randf(0,1)<0.9) cells[Layer::LAND][ox][oy]= 1;
}
//water spread
if (cells[Layer::LAND][i][j]==0){
int ox= randi(i-1,i+2);
int oy= randi(j-1,j+2);
if (ox<0) ox+= CW;
if (ox>CW-1) ox-= CW;
if (oy<0) oy+= CH;
if (oy>CH-1) oy-= CH;
if (cells[Layer::LAND][ox][oy]==-1 && randf(0,1)<0.9) cells[Layer::LAND][ox][oy]= 0;
}
}
}
leftcount= 0;
for(int i=0;i<CW;i++) {
for(int j=0;j<CH;j++) {
if (cells[Layer::LAND][i][j]==-1){
leftcount++;
}
}
}
}
}
void World::readConfig()
{
//get world constants from config file, settings.cfg
char line[64], *pos;
char var[16];
char dataval[16];
int i; //integer buffer
float f; //float buffer
FILE* cf = fopen("settings.cfg", "r");
if(cf){
printf("tweaking constants.\n");
while(!feof(cf)){
fgets(line, sizeof(line), cf);
pos= strtok(line,"\n");
sscanf(line, "%s%s", var, dataval);
if(strcmp(var, "MINFOOD=")==0){ //strcmp= 0 when the arguements equal
sscanf(dataval, "%i", &i);
MINFOOD= f;
}else if(strcmp(var, "INITFOODDENSITY=")==0){
sscanf(dataval, "%f", &f);
INITFOODDENSITY= f;
}else if(strcmp(var, "INITFOOD=")==0){
sscanf(dataval, "%i", &i);
INITFOOD= i;
}else if(strcmp(var, "INITFRUITDENSITY=")==0){
sscanf(dataval, "%f", &f);
INITFRUITDENSITY= f;
}else if(strcmp(var, "INITFRUIT=")==0){
sscanf(dataval, "%i", &i);
INITFRUIT= i;
}else if(strcmp(var, "NUMBOTS=")==0){
sscanf(dataval, "%i", &i);
NUMBOTS= i;
}else if(strcmp(var, "ENOUGHBOTS=")==0){
sscanf(dataval, "%i", &i);
ENOUGHBOTS= i;
}else if(strcmp(var, "TOOMANYBOTS=")==0){
sscanf(dataval, "%i", &i);
TOOMANYBOTS= i;
}else if(strcmp(var, "REPORTS_PER_EPOCH=")==0){
sscanf(dataval, "%i", &i);
REPORTS_PER_EPOCH= i;
}else if(strcmp(var, "FRAMES_PER_EPOCH=")==0){
sscanf(dataval, "%i", &i);
FRAMES_PER_EPOCH= i;
}else if(strcmp(var, "FRAMES_PER_DAY=")==0){
sscanf(dataval, "%i", &i);
FRAMES_PER_DAY= i;
}else if(strcmp(var, "CONTINENTS=")==0){
sscanf(dataval, "%i", &i);
CONTINENTS= i;
}else if(strcmp(var, "OCEANPERCENT=")==0){
sscanf(dataval, "%f", &f);
OCEANPERCENT= f;
}else if(strcmp(var, "GRAVITYACCEL=")==0){
sscanf(dataval, "%f", &f);
GRAVITYACCEL= f;
}else if(strcmp(var, "REACTPOWER=")==0){
sscanf(dataval, "%f", &f);
REACTPOWER= f;
}else if(strcmp(var, "SPIKEMULT=")==0){
sscanf(dataval, "%f", &f);
SPIKEMULT= f;
}else if(strcmp(var, "BOTSPEED=")==0){
sscanf(dataval, "%f", &f);
BOTSPEED= f;
}else if(strcmp(var, "BOOSTSIZEMULT=")==0){
sscanf(dataval, "%f", &f);
BOOSTSIZEMULT= f;
}else if(strcmp(var, "SOUNDPITCHRANGE=")==0){
sscanf(dataval, "%f", &f);
SOUNDPITCHRANGE= f;
}else if(strcmp(var, "FOODTRANSFER=")==0){
sscanf(dataval, "%f", &f);
FOODTRANSFER= f;
// }else if(strcmp(var, "MEANRADIUS=")==0){
// sscanf(dataval, "%f", &f);
// = f;
}else if(strcmp(var, "SPIKESPEED=")==0){
sscanf(dataval, "%f", &f);
SPIKESPEED= f;
}else if(strcmp(var, "FRESHKILLTIME=")==0){
sscanf(dataval, "%i", &i);
FRESHKILLTIME= i;
}else if(strcmp(var, "TENDERAGE=")==0){
sscanf(dataval, "%i", &i);
TENDERAGE= i;
}else if(strcmp(var, "MINMOMHEALTH=")==0){
sscanf(dataval, "%f", &f);
MINMOMHEALTH= f;
// }else if(strcmp(var, "REPRATE=")==0){
// sscanf(dataval, "%f", &f);
// REPRATE= f;
// }else if(strcmp(var, "LEARNRATE=")==0){
// sscanf(dataval, "%f", &f);
// LEARNRATE= f;
}else if(strcmp(var, "MAXDEVIATION=")==0){
sscanf(dataval, "%f", &f);
MAXDEVIATION= f;
}else if(strcmp(var, "MAXAGE=")==0){
sscanf(dataval, "%i", &i);
MAXAGE= i;
}else if(strcmp(var, "DIST=")==0){
sscanf(dataval, "%f", &f);
DIST= f;
}else if(strcmp(var, "SPIKELENGTH=")==0){
sscanf(dataval, "%f", &f);
SPIKELENGTH= f;
}else if(strcmp(var, "TOOCLOSE=")==0){
sscanf(dataval, "%f", &f);
TOOCLOSE= f;
}else if(strcmp(var, "FOOD_SHARING_DISTANCE=")==0){
sscanf(dataval, "%f", &f);
FOOD_SHARING_DISTANCE= f;
}else if(strcmp(var, "SEXTING_DISTANCE=")==0){
sscanf(dataval, "%f", &f);
SEXTING_DISTANCE= f;
}else if(strcmp(var, "GRABBING_DISTANCE=")==0){
sscanf(dataval, "%f", &f);
GRABBING_DISTANCE= f;
}else if(strcmp(var, "HEALTHLOSS_WHEELS=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_WHEELS= f;
}else if(strcmp(var, "HEALTHLOSS_BOOSTMULT=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_BOOSTMULT= f;
}else if(strcmp(var, "HEALTHLOSS_BADTEMP=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_BADTEMP= f;
}else if(strcmp(var, "HEALTHLOSS_AGING=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_AGING= f;
}else if(strcmp(var, "HEALTHLOSS_BRAINUSE=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_BRAINUSE= f;
}else if(strcmp(var, "HEALTHLOSS_BUMP=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_BUMP= f;
}else if(strcmp(var, "HEALTHLOSS_SPIKE_EXT=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_SPIKE_EXT= f;
}else if(strcmp(var, "HEALTHLOSS_BADAIR=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_BADAIR= f;
}else if(strcmp(var, "HEALTHLOSS_NOOXYGEN=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_NOOXYGEN= f;
}else if(strcmp(var, "HEALTHLOSS_ASSEX=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_ASSEX= f;
}else if(strcmp(var, "HEALTHLOSS_JAWSNAP=")==0){
sscanf(dataval, "%f", &f);
HEALTHLOSS_JAWSNAP= f;
}else if(strcmp(var, "FOODINTAKE=")==0){
sscanf(dataval, "%f", &f);
FOODINTAKE= f;
}else if(strcmp(var, "FOODDECAY=")==0){
sscanf(dataval, "%f", &f);
FOODDECAY= f;
}else if(strcmp(var, "FOODGROWTH=")==0){
sscanf(dataval, "%f", &f);
FOODGROWTH= f;
}else if(strcmp(var, "FOODWASTE=")==0){
sscanf(dataval, "%f", &f);
FOODWASTE= f;
}else if(strcmp(var, "FOODADDFREQ=")==0){
sscanf(dataval, "%i", &i);
FOODADDFREQ= i;
}else if(strcmp(var, "FOODSPREAD=")==0){
sscanf(dataval, "%f", &f);
FOODSPREAD= f;
}else if(strcmp(var, "FOODRANGE=")==0){
sscanf(dataval, "%i", &i);
FOODRANGE= i;
}else if(strcmp(var, "FRUITINTAKE=")==0){
sscanf(dataval, "%f", &f);
FRUITINTAKE= f;
}else if(strcmp(var, "FRUITDECAY=")==0){
sscanf(dataval, "%f", &f);
FRUITDECAY= f;
}else if(strcmp(var, "FRUITWASTE=")==0){
sscanf(dataval, "%f", &f);
FRUITWASTE= f;
}else if(strcmp(var, "FRUITADDFREQ=")==0){
sscanf(dataval, "%i", &i);
FRUITADDFREQ= i;
}else if(strcmp(var, "FRUITREQUIRE=")==0){
sscanf(dataval, "%f", &f);
FRUITREQUIRE= f;
}else if(strcmp(var, "MEATINTAKE=")==0){
sscanf(dataval, "%f", &f);
MEATINTAKE= f;
}else if(strcmp(var, "MEATDECAY=")==0){
sscanf(dataval, "%f", &f);
MEATDECAY= f;
}else if(strcmp(var, "MEATWASTE=")==0){
sscanf(dataval, "%f", &f);
MEATWASTE= f;
}else if(strcmp(var, "MEATVALUE=")==0){
sscanf(dataval, "%f", &f);
MEATVALUE= f;
}else if(strcmp(var, "HAZARDFREQ=")==0){
sscanf(dataval, "%i", &i);
HAZARDFREQ= i;
}else if(strcmp(var, "HAZARDDECAY=")==0){
sscanf(dataval, "%f", &f);
HAZARDDECAY= f;
}else if(strcmp(var, "HAZARDDEPOSIT=")==0){
sscanf(dataval, "%f", &f);
HAZARDDEPOSIT= f;
}else if(strcmp(var, "HAZARDDAMAGE=")==0){
sscanf(dataval, "%f", &f);
HAZARDDAMAGE= f;
}
}
fclose(cf);
} else {
printf("settings.cfg did not exist! Its ok, I gave you a new one to tinker with\n");
writeConfig();
}
}
void World::writeConfig()
{
//called if settings.cfg is missing
//happens after initializing and skips loading of config
FILE* cf= fopen("settings.cfg", "w");
fprintf(cf, ";settings.cfg\n//This file will be regenerated with defualt values if missing (regeneration is not necissary for the program to work)\n#modify any value in this file to change constants in Scriptbots. All changes will require a reset or relaunch to take effect\n\n");
fprintf(cf, "\n");
fprintf(cf, "MINFOOD= %i //Minimum number of food cells which must have food during simulation. 0= off\n", conf::MINFOOD);
fprintf(cf, "INITFOODDENSITY= %f //initial density of full food cells. Use 'INITFOOD= #' to set a number\n", conf::INITFOODDENSITY);
fprintf(cf, "//INITFOOD= 0 //remove '//' from the flag to enable\n");
fprintf(cf, "INITFRUITDENSITY= %f //initial density of full fruit cells\n", conf::INITFRUITDENSITY);
fprintf(cf, "//INITFRUIT= 0\n");
fprintf(cf, "\n");
fprintf(cf, "NUMBOTS= %i //initial number of agents, and minimum\n", conf::NUMBOTS);
fprintf(cf, "ENOUGHBOTS= %i //number of agents when above we no longer seed with random spawns\n", conf::ENOUGHBOTS);
fprintf(cf, "TOOMANYBOTS= %i //number of agents at which the full NOAIR healthloss is applied\n", conf::TOOMANYBOTS);
fprintf(cf, "\n");
fprintf(cf, "REPORTS_PER_EPOCH= %i //number of times to record data per epoch. 0 for off. (David Coleman)\n", conf::REPORTS_PER_EPOCH);
fprintf(cf, "FRAMES_PER_EPOCH= %i //number of frames before epoch is incremented by 1.\n", conf::FRAMES_PER_EPOCH);
fprintf(cf, "FRAMES_PER_DAY= %i //number of frames it takes for the daylight cycle to go completely around the map\n", conf::FRAMES_PER_DAY);
fprintf(cf, "\n");
fprintf(cf, "CONTINENTS= %i //number of 'continents' generated on the land layer. Not guarenteed to be accurate\n", conf::CONTINENTS);
fprintf(cf, "OCEANPERCENT= %f //decimal percent of terrain layer which will be ocean. Aproximately\n", conf::OCEANPERCENT);
fprintf(cf, "GRAVITYACCEL= %f //how fast a bot will 'fall' after jumping. 0= weightless (I don't recommend), 0.1 or more= super-gravity]\n", conf::GRAVITYACCEL);
fprintf(cf, "REACTPOWER= %f //the restoring force between two colliding agents. 0= no reaction (be sure to set TOOCLOSE= -1 also)\n", conf::REACTPOWER);
fprintf(cf, "SPIKEMULT= %f //strength of spike injury\n", conf::SPIKEMULT);
fprintf(cf, "BOTSPEED= %f //fastest possible speed of agents\n", conf::BOTSPEED);
fprintf(cf, "BOOSTSIZEMULT= %f //how much boost do agents get when boost is active?\n", conf::BOOSTSIZEMULT);
fprintf(cf, "SOUNDPITCHRANGE= %f //range below hearhigh and above hearlow within any external sounds fade in\n", conf::SOUNDPITCHRANGE);
fprintf(cf, "FOODTRANSFER= %f //how much health is transfered between two agents trading food per tick?\n", conf::FOODTRANSFER);
// fprintf(cf, "MEANRADIUS= %f\n", conf::MEANRADIUS);
fprintf(cf, "SPIKESPEED= %f //how quickly can the spike be extended?\n", conf::SPIKESPEED);
fprintf(cf, "FRESHKILLTIME= %i //number of ticks after a spike, collision, or bite that a bot will still drop full meat\n", conf::FRESHKILLTIME);
fprintf(cf, "TENDERAGE= %i //age of agents where full meat is finally given. Is age/TENDERAGE before then. =0 turns off effect\n", conf::TENDERAGE);
fprintf(cf, "MINMOMHEALTH= %f //minimum amount of health required for an agent to have a child\n", conf::MINMOMHEALTH);
// fprintf(cf, "REPRATE= %f\n", conf::REPRATE);
// fprintf(cf, "LEARNRATE= %f\n", conf::LEARNRATE);
fprintf(cf, "MAXDEVIATION= %f //maximum difference in species ID a crossover reproducing agent will be willing to tolerate\n", conf::MAXDEVIATION);
fprintf(cf, "MAXAGE= %i //Age at which the full HEALTHLOSS_AGING amount is applied to an agent\n", conf::MAXAGE);
fprintf(cf, "\n");
fprintf(cf, "DIST= %f //how far can the senses can detect other agents or cells\n", conf::DIST);
fprintf(cf, "SPIKELENGTH= %f //full spike length. Can not be more than DIST\n", conf::SPIKELENGTH);
fprintf(cf, "TOOCLOSE= %f //how much two agents can be overlapping before they take damage. -1 disables event\n", conf::TOOCLOSE);
fprintf(cf, "FOOD_SHARING_DISTANCE= %f //how far away can food be shared between bots?\n", conf::FOOD_SHARING_DISTANCE);
fprintf(cf, "SEXTING_DISTANCE= %f //how far away can two agents sexually reproduce?\n", conf::SEXTING_DISTANCE);
fprintf(cf, "GRABBING_DISTANCE= %f //how far away can a bot grab another? Can not be more than DIST\n", conf::GRABBING_DISTANCE);
fprintf(cf, "\n");
fprintf(cf, "HEALTHLOSS_WHEELS= %f //How much health is lost for a bot driving at full speed\n", conf::HEALTHLOSS_WHEELS);
fprintf(cf, "HEALTHLOSS_BOOSTMULT= %f //how much boost costs (set to 1 to nullify boost cost; its a multiplier)\n", conf::HEALTHLOSS_BOOSTMULT);
fprintf(cf, "HEALTHLOSS_BADTEMP= %f //how quickly health drains in nonpreferred temperatures\n", conf::HEALTHLOSS_BADTEMP);
fprintf(cf, "HEALTHLOSS_AGING= %f //health lost at MAXAGE\n", conf::HEALTHLOSS_AGING);
fprintf(cf, "HEALTHLOSS_BRAINUSE= %f //how much health is reduced for each box in the brain being active\n", conf::HEALTHLOSS_BRAINUSE);
fprintf(cf, "HEALTHLOSS_BUMP= %f //how much health is lost upon collision\n", conf::HEALTHLOSS_BUMP);
fprintf(cf, "HEALTHLOSS_SPIKE_EXT= %f //how much health a bot looses for extending spike\n", conf::HEALTHLOSS_SPIKE_EXT);
fprintf(cf, "HEALTHLOSS_BADAIR= %f //how much health is lost if in totally opposite environment\n", conf::HEALTHLOSS_BADAIR);
fprintf(cf, "HEALTHLOSS_NOOXYGEN= %f //how much bots are penalized when total agents = TOOMANYBOTS\n", conf::HEALTHLOSS_NOOXYGEN);
fprintf(cf, "HEALTHLOSS_ASSEX= %f //multiplier for radius/MEANRADIUS penalty on mother for asexual reproduction\n", conf::HEALTHLOSS_ASSEX);
fprintf(cf, "HEALTHLOSS_JAWSNAP= %f //when jaws snap fully (0->1), this is the damage applied to bots in front\n", conf::HEALTHLOSS_JAWSNAP);
fprintf(cf, "\n");
fprintf(cf, "FOODINTAKE= %f //how much plant food can feed an agent per tick?\n", conf::FOODINTAKE);
fprintf(cf, "FOODDECAY= %f //how much does food decay(+)/grow(-) on a cell which already has food?\n", conf::FOODDECAY);
fprintf(cf, "FOODGROWTH= %f //how much does food increase by on a cell with both plant and hazard? (fertilizer)\n", conf::FOODGROWTH);
fprintf(cf, "FOODWASTE= %f //how much food disapears if agent eats?\n", conf::FOODWASTE);
// fprintf(cf, "FOODMAX= %f\n", conf::FOODMAX);
fprintf(cf, "FOODADDFREQ= %i //how often does random square get set to full food?\n", conf::FOODADDFREQ);
fprintf(cf, "FOODSPREAD= %f //probability of a fruit cell seeding food to a nearby cell. 0.0002= speedy recovery\n", conf::FOODSPREAD);
fprintf(cf, "FOODRANGE= %i //distance that a single cell of food can seed. in cells.\n", conf::FOODRANGE);
fprintf(cf, "\n");
fprintf(cf, "FRUITINTAKE= %f //how much fruit can feed an agent per tick?\n", conf::FRUITINTAKE);
fprintf(cf, "FRUITDECAY= %f //how much fruit decays on a cell with low plant life?\n", conf::FRUITDECAY);
fprintf(cf, "FRUITWASTE= %f //how much fruit disapears if agent eats?\n", conf::FRUITWASTE);
// fprintf(cf, "FRUITMAX= %f\n", conf::FRUITMAX);
fprintf(cf, "FRUITADDFREQ= %i //how often does a high-plant-food cell get set to full fruit?\n", conf::FRUITADDFREQ);
fprintf(cf, "FRUITREQUIRE= %f //minimum plant health required for fruit to persist on the cell\n", conf::FRUITREQUIRE);
fprintf(cf, "\n");
fprintf(cf, "MEATINTAKE= %f //how much meat can feed an agent per tick?\n", conf::MEATINTAKE);
fprintf(cf, "MEATDECAY= %f //how much meat decays/grows on a cell? through MEATDECAY/[meat_present]\n", conf::MEATDECAY);
fprintf(cf, "MEATWASTE= %f //how much meat disapears if agent eats?\n", conf::MEATWASTE);
// fprintf(cf, "MEATMAX= %f\n", conf::MEATMAX);
fprintf(cf, "MEATVALUE= %f //how much meat a bot's body is worth?\n", conf::MEATVALUE);
fprintf(cf, "\n");
fprintf(cf, "HAZARDFREQ= %i //how often an instant hazard appears?\n", conf::HAZARDFREQ);
fprintf(cf, "HAZARDDECAY= %f //how much non-event hazard decays/grows on a cell per tick?\n", conf::HAZARDDECAY);
fprintf(cf, "HAZARDDEPOSIT= %f //how much hazard is placed by a bot per tick?\n", conf::HAZARDDEPOSIT);
fprintf(cf, "HAZARDDAMAGE= %f //how much health a bot looses while on a filled hazard cell per tick?\n", conf::HAZARDDAMAGE);
// fprintf(cf, "HAZARDMAX= %f\n", conf::HAZARDMAX);
fprintf(cf, "\n\n");
fprintf(cf, "In case you were wondering, no, the program doesn't care about other text lines. Please note the sim only takes the last entry of a flag's value if there are duplicates. Also, can you find the hidden config flag?...\n");
fprintf(cf, "Remember to RESET or RELAUNCH ScriptBots after modification!!!");
fclose(cf);
}
void World::update()
{
modcounter++;
vector<int> dt;
//Process periodic events
if (REPORTS_PER_EPOCH>0 && (modcounter%(10000/REPORTS_PER_EPOCH)==0)) {
//write report and record counts
numHerbivore[ptr]= getHerbivores();
numCarnivore[ptr]= getCarnivores();
numFrugivore[ptr]= getFrugivores();
numHybrid[ptr]= getHybrids();
numTotal[ptr]= getAgents();
ptr++;
if(ptr == conf::RECORD_SIZE) ptr = 0;
writeReport();
deaths.clear();
}
if (modcounter>=FRAMES_PER_EPOCH) {
modcounter=0;
current_epoch++;
}
if ((modcounter%FOODADDFREQ==0 && !CLOSED) || getFood()<MINFOOD) {
int cx=randi(0,CW);
int cy=randi(0,CH);
cells[Layer::PLANTS][cx][cy]= conf::FOODMAX;
}
if (modcounter%HAZARDFREQ==0) {
int cx=randi(0,CW);
int cy=randi(0,CH);
cells[Layer::HAZARDS][cx][cy]= conf::HAZARDMAX;
}
if (modcounter%FRUITADDFREQ==0) {
bool trigger= false;
while (!trigger) {
int cx=randi(0,CW);
int cy=randi(0,CH);
if (cells[Layer::PLANTS][cx][cy]>= conf::FOODMAX*FRUITREQUIRE) {
cells[Layer::FRUITS][cx][cy]= conf::FRUITMAX;
trigger= true;
}
}
}
#pragma omp parallel for schedule(dynamic)
for(int cx=0; cx<(int)CW;cx++){
for(int cy=0; cy<(int)CH;cy++){
//food = cells[Layer::PLANTS]...
if (cells[Layer::PLANTS][cx][cy]>0 && cells[Layer::PLANTS][cx][cy]<conf::FOODMAX) {
cells[Layer::PLANTS][cx][cy]-= FOODDECAY; //food quantity is changed by FOODDECAY
if (cells[Layer::HAZARDS][cx][cy]>0) {
cells[Layer::PLANTS][cx][cy]+= FOODGROWTH*cells[Layer::HAZARDS][cx][cy]/conf::HAZARDMAX; //food grows out of waste/hazard
}
}
if (randf(0,1)<FOODSPREAD && cells[Layer::FRUITS][cx][cy]>0.5*conf::FRUITMAX) {
//food seeding
int ox= randi(cx-1-FOODRANGE,cx+2+FOODRANGE);
int oy= randi(cy-1-FOODRANGE,cy+2+FOODRANGE);
if (ox<0) ox+= CW;
if (ox>CW-1) ox-= CW;
if (oy<0) oy+= CH;
if (oy>CH-1) oy-= CH; //code up to this point ensures world edges are crossed and not skipped
if (cells[Layer::PLANTS][ox][oy]<=conf::FOODMAX*3/4) cells[Layer::PLANTS][ox][oy]+= conf::FOODMAX/4;
}
cells[Layer::PLANTS][cx][cy]= capm(cells[Layer::PLANTS][cx][cy], 0, conf::FOODMAX); //cap food at FOODMAX
//meat = cells[Layer::MEATS]...
if (cells[Layer::MEATS][cx][cy]>0) {
cells[Layer::MEATS][cx][cy] -= MEATDECAY/(cells[Layer::MEATS][cx][cy]/conf::MEATMAX); //meat decays exponentially
}
cells[Layer::MEATS][cx][cy]= capm(cells[Layer::MEATS][cx][cy], 0, conf::MEATMAX); //cap at MEATMAX
//hazard = cells[Layer::HAZARDS]...
if (cells[Layer::HAZARDS][cx][cy]>0){
cells[Layer::HAZARDS][cx][cy]-= HAZARDDECAY; //hazard decays
}
if (cells[Layer::HAZARDS][cx][cy]>conf::HAZARDMAX*9/10 && randf(0,1)<0.125){
cells[Layer::HAZARDS][cx][cy]= 0; //instant hazards will be reset to zero
}
cells[Layer::HAZARDS][cx][cy]= capm(cells[Layer::HAZARDS][cx][cy], 0, conf::HAZARDMAX); //cap at HAZARDMAX
//fruit = cells[Layer::FRUITS]...
if (cells[Layer::PLANTS][cx][cy]<=conf::FOODMAX*FRUITREQUIRE && cells[Layer::FRUITS][cx][cy]>0){
cells[Layer::FRUITS][cx][cy]-= FRUITDECAY; //fruit decays from lack of plant life
}
cells[Layer::FRUITS][cx][cy]= capm(cells[Layer::FRUITS][cx][cy], 0, conf::FRUITMAX); //cap
//light = cells[Layer::LIGHT]...
cells[Layer::LIGHT][cx][cy]= capm(0.5+sin((cx*2*M_PI)/CW-(modcounter+current_epoch*FRAMES_PER_EPOCH)*2*M_PI/FRAMES_PER_DAY), 0, 1);
}
}
//give input to every agent. Sets in[] array
setInputs();
//brains tick. computes in[] -> out[]
brainsTick();
//reset any counter variables per agent and do other stuff before processOutputs
#pragma omp parallel for
for (int i=0; i<(int)agents.size(); i++) {
Agent* a= &agents[i];
//reduce fresh kill flag
if(a->freshkill>0) a->freshkill-= 1;
//process indicator (used in drawing)
if(a->indicator>0) a->indicator-= 1;
//process jaw renderer
if(a->jawrend>0) a->jawrend-=1;
//reset dfood for processOutputs
a->dfood= 0;
//find brain activity on this tick
a->setActivity();
//doLiveMutate
float MR= a->MUTRATE1;
float MR2= a->MUTRATE2;
a->brain.liveMutate(MR, MR2, a->out);
//Age goes up!
if (modcounter%100==0) a->age+= 1;
}
//read output and process consequences of bots on environment. requires out[]
processOutputs();
//process health:
healthTick();
//handle reproduction
//do not omp any of this!
if (modcounter%5==0){
for (int i=0; i<(int)agents.size(); i++) {
if (agents[i].repcounter<0 && agents[i].health>=MINMOMHEALTH) {
//agent is healthy and is ready to reproduce.
if(randf(0,1)<0.75){
for (int j=0; j<(int)agents.size(); j++) {
if(i==j || !(agents[j].sexproject)) continue;
float d= (agents[i].pos-agents[j].pos).length();
float deviation= abs(agents[i].species - agents[j].species); //species deviation check
if (d<=SEXTING_DISTANCE && deviation<=MAXDEVIATION) {
//this adds agent[i].numbabies to world, but with two parents
reproduce(i, j);
agents[i].repcounter= conf::REPRATE;
agents[j].repcounter= conf::REPRATE;
break;
continue;
}
}
} else {
//this adds agents[i].numbabies to world, but with just one parent
reproduce(i, i);
agents[i].repcounter= conf::REPRATE;
continue;
}
}
}
}
for (int i=0; i<(int)agents.size(); i++) {
//remove dead agents. first distribute meat
if (agents[i].health<=0) {
if (agents[i].health<0) agents[i].writeIfKilled("Killed by Something ?");
int cx= (int) agents[i].pos.x/conf::CZ;
int cy= (int) agents[i].pos.y/conf::CZ;
float meat= cells[Layer::MEATS][cx][cy];
float agemult= 1.0;
float freshmult= 0.25; //if this agent wasnt freshly killed, default to 25%
float stomachmult= (4-3*agents[i].stomach[Stomach::MEAT])/4; //carnivores give 25%
if(agents[i].age<TENDERAGE && TENDERAGE>0) agemult= agents[i].age/TENDERAGE; //young killed agents should give very little resources until age 10
if(agents[i].freshkill>0) freshmult= 1; //agents which were spiked recently will give full meat
meat+= MEATVALUE*conf::MEATMAX*agemult*freshmult*stomachmult;
cells[Layer::MEATS][cx][cy]= capm(meat, 0, conf::MEATMAX);
//collect all the death causes from all dead agents
deaths.push_back(agents[i].death);
}
}
vector<Agent>::iterator iter= agents.begin();
while (iter != agents.end()) {
if (iter->health <=0) {
if (SELECTION==iter->id) printf("The Selected Agent was %s!\n", iter->death);
iter= agents.erase(iter);
} else {
++iter;
}
}
//add new agents, if environment isn't closed
if (!CLOSED) {
//make sure environment is always populated with at least NUMBOTS bots
if (agents.size()<NUMBOTS) {
addAgents(NUMBOTS-agents.size());
}
if (agents.size()<ENOUGHBOTS && modcounter%50==0) {
if (randf(0,1)<0.5){
addAgents(1); //every now and then add random bot in if population too low
}
}
}
}
void World::setInputs()
{
// R1 G1 B1 R2 G2 B2 R3 G3 B3 R4 G4 B4 HEALTH CLOCK1 CLOCK2 SOUND HEARING SMELL BLOOD TEMP_DISCOMFORT PLAYER_INPUT1
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15,16 17,18 19 20 21 22
// FOOD_SMELL MEAT_SMELL HAZARD_SMELL WATER_SMELL
// 23 24 25 26
float PI8=M_PI/8/2; //pi/8/2
float PI38= 3*PI8; //3pi/8/2
float PI4= M_PI/4;
#pragma omp parallel for schedule(dynamic)
for (int i=0; i<(int)agents.size(); i++) {
Agent* a= &agents[i];
//our cell position
int scx= (int) a->pos.x/conf::CZ;
int scy= (int) a->pos.y/conf::CZ;
//HEALTH
a->in[Input::HEALTH]= cap(a->health/2); //divide by 2 since health is in [0,2]
//FOOD
// a->in[#]= cells[Layer::PLANTS][cx][cy]/conf::FOODMAX;
// a->in[#]= cells[Layer::MEATS][cx][cy]/conf::MEATMAX;
//SOUND SMELL EYES
float light= cells[Layer::LIGHT][scx][scy]; //grab light level
vector<float> r(NUMEYES,0.5*light);
vector<float> g(NUMEYES,0.5*light);
vector<float> b(NUMEYES,0.5*light);
float smellsum=0;
vector<float> hearsum(NUMEARS,0);
//BLOOD ESTIMATOR
float blood= 0;
//cell sense
int minx, maxx, miny, maxy;
minx= max((scx-DIST/conf::CZ/2),(float)0);
maxx= min((scx+1+DIST/conf::CZ/2),(float)CW);
miny= max((scy-DIST/conf::CZ/2),(float)0);
maxy= min((scy+1+DIST/conf::CZ/2),(float)CH);
float fruit= 0, meat= 0, hazard= 0, water= 0;
//cell "smelling": a faster/better food- & hazard-revealing solution than adding to eyesight
for(scx=minx;scx<maxx;scx++){
for(scy=miny;scy<maxy;scy++){
fruit+= cells[Layer::FRUITS][scx][scy]/conf::FRUITMAX;
meat+= cells[Layer::MEATS][scx][scy]/conf::MEATMAX;
hazard+= cells[Layer::HAZARDS][scx][scy]/conf::HAZARDMAX;
water+= (1-cells[Layer::LAND][scx][scy]);
}
}
fruit*= a->smell_mod/(maxx-minx)/(maxy-miny);
meat*= a->smell_mod/(maxx-minx)/(maxy-miny);
hazard*= a->smell_mod/(maxx-minx)/(maxy-miny);
water*= a->smell_mod/(maxx-minx)/(maxy-miny);
/* EYESIGHT CODE
if (cells[Layer::PLANTS][scx][scy]==0 && cells[Layer::MEATS][scx][scy]==0 && cells[Layer::HAZARDS][scx][scy]==0) continue;
Vector2f cellpos= Vector2f((float)(scx*conf::CZ+conf::CZ/2),(float)(scy*conf::CZ+conf::CZ/2)); //find midpoint of the cell
float d= (a->pos-cellpos).length();
if (d<DIST) {
float angle= (cellpos-a->pos).get_angle();
for(int q=0;q<NUMEYES;q++){
float aa = a->angle + a->eyedir[q];
if (aa<-M_PI) aa += 2*M_PI;
if (aa>M_PI) aa -= 2*M_PI;
float diff1= aa- angle;
if (fabs(diff1)>M_PI) diff1= 2*M_PI- fabs(diff1);
diff1= fabs(diff1);
float fov = a->eyefov[q];
if (diff1<fov) {
//we see this cell with this eye. Accumulate stats
float mul1= a->eye_see_cell_mod*(fabs(fov-diff1)/fov)*((DIST-d)/DIST)*(d/DIST)*2;
r[q] += mul1*0.25*cells[Layer::MEATS][scx][scy]; //meat looks red
g[q] += mul1*0.25*cells[Layer::PLANTS][scx][scy]; //plants are green
r[q] += mul1*0.25*cells[Layer::FRUITS][scx][scy]; //fruit looks yellow
g[q] += mul1*0.25*cells[Layer::FRUITS][scx][scy];
b[q] += mul1*0.25*cells[Layer::HAZARDS][scx][scy]; //hazards are blue???
if(a->selectflag && isDebug()){
linesA.push_back(a->pos);
linesB.push_back(cellpos);
}
}
}
float forwangle= a->angle;
float diff4= forwangle- angle;
if (fabs(forwangle)>M_PI) diff4= 2*M_PI- fabs(forwangle);
diff4= fabs(diff4);
if (diff4<PI38) {
float mul4= ((PI38-diff4)/PI38)*(1-d/DIST);
//meat can also be sensed with blood sensor
blood+= mul4*0.3*cells[Layer::MEATS][scx][scy];
}
}*/
for (int j=0; j<(int)agents.size(); j++) {
if (i==j) continue;
Agent* a2= &agents[j];
if (a->pos.x<a2->pos.x-DIST || a->pos.x>a2->pos.x+DIST
|| a->pos.y>a2->pos.y+DIST || a->pos.y<a2->pos.y-DIST) continue;
float d= (a->pos-a2->pos).length();
if (d<DIST) {
//smell
smellsum+= a->smell_mod*(1-d/DIST);
//sound and hearing
for (int q=0;q<NUMEARS;q++){
Vector2f v(a->radius, 0);
v.rotate(a->angle + a->eardir[q]);
Vector2f earpos= a->pos+ v;
float eardist= (earpos-a2->pos).length();
//hearing. Listening to other agents and their activities
for(int n=0;n<2;n++){
float otone, ovolume;
if(n==0){ //if n=0, do agent vocals.
otone= a2->tone;
ovolume= a2->volume;
}else if(n==1){ //if n=1, do agent wheels
otone= 0.25;
ovolume= (max(fabs(a2->w1),fabs(a2->w2)));
} //future: if n=2, do agent intake sound
if(otone>=a->hearhigh[q]) continue;
if(otone<=a->hearlow[q]) continue;
float tonemult= cap(min((a->hearhigh[q] - otone)/SOUNDPITCHRANGE,(otone - a->hearlow[q])/SOUNDPITCHRANGE));
hearsum[q]+= a->hear_mod*(1-eardist/DIST)*ovolume*tonemult;
}
}
float ang= (a2->pos- a->pos).get_angle(); //current angle between bots
//eyes: bot sight
//we will skip all eyesight if our agent is in the dark (light==0)
if(light!=0){
for(int q=0;q<NUMEYES;q++){
float aa = a->angle + a->eyedir[q];
if (aa<-M_PI) aa += 2*M_PI;
if (aa>M_PI) aa -= 2*M_PI;
float diff1= aa- ang;
if (fabs(diff1)>M_PI) diff1= 2*M_PI- fabs(diff1);
diff1= fabs(diff1);
float fov = a->eyefov[q];
if (diff1<fov) {
//we see a2 with this eye. Accumulate stats
float mul1= light*a->eye_see_agent_mod*(fabs(fov-diff1)/fov)*(1-d/DIST)*(1-d/DIST);
r[q] += mul1*a2->red;
g[q] += mul1*a2->gre;
b[q] += mul1*a2->blu;
if(a->id==SELECTION && isDebug()){ //debug sight lines, get coords
linesA.push_back(a->pos);
linesB.push_back(a2->pos);
}
}
}
}
//blood sensor
float forwangle= a->angle;
float diff4= forwangle- ang;
if (fabs(forwangle)>M_PI) diff4= 2*M_PI- fabs(forwangle);
diff4= fabs(diff4);
if (diff4<PI38) {
float mul4= ((PI38-diff4)/PI38)*(1-d/DIST);
blood+= a->blood_mod*mul4*(1-agents[j].health/2); //remember: health is in [0,2]
//agents with high life dont bleed. low life makes them bleed more
}
}
}
//temperature varies from 0 to 1 across screen.
//it is 0 at equator (in middle), and 1 on edges. Agents can sense this range
float temp= (float)2.0*abs(a->pos.y/conf::HEIGHT - 0.5);
a->in[Input::RED1]= cap(r[0]);
a->in[Input::GRE1]= cap(g[0]);
a->in[Input::BLU1]= cap(b[0]);
a->in[Input::RED2]= cap(r[1]);
a->in[Input::GRE2]= cap(g[1]);
a->in[Input::BLU2]= cap(b[1]);
a->in[Input::RED3]= cap(r[2]);
a->in[Input::GRE3]= cap(g[2]);
a->in[Input::BLU3]= cap(b[2]);
a->in[Input::RED4]= cap(r[3]);
a->in[Input::GRE4]= cap(g[3]);
a->in[Input::BLU4]= cap(b[3]);
a->in[Input::CLOCK1]= abs(sin((modcounter+current_epoch*FRAMES_PER_EPOCH)/a->clockf1));
a->in[Input::CLOCK2]= abs(sin((modcounter+current_epoch*FRAMES_PER_EPOCH)/a->clockf2));
a->in[Input::CLOCK3]= abs(sin((modcounter+current_epoch*FRAMES_PER_EPOCH)/a->clockf3));
a->in[Input::HEARING1]= cap(hearsum[0]);
a->in[Input::HEARING2]= cap(hearsum[1]);
a->in[Input::BOT_SMELL]= cap(smellsum);
a->in[Input::BLOOD]= cap(blood);
a->in[Input::TEMP]= temp;
if (a->id==SELECTION) {
a->in[Input::PLAYER]= pinput1;
} else {
a->in[Input::PLAYER]= 0.0;
}
a->in[Input::FRUIT_SMELL]= cap(fruit);
a->in[Input::MEAT_SMELL]= cap(meat);
a->in[Input::HAZARD_SMELL]= cap(hazard);
a->in[Input::WATER_SMELL]= cap(water);
a->in[Input::RANDOM]= cap(randn(a->in[Input::RANDOM],0.08));
}
}
void World::brainsTick()
{
#pragma omp parallel for schedule(dynamic)