forked from karpathy/scriptsbots
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGLView.cpp
More file actions
1588 lines (1391 loc) · 51.3 KB
/
Copy pathGLView.cpp
File metadata and controls
1588 lines (1391 loc) · 51.3 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 "GLView.h"
#include <ctime>
#include "config.h"
#ifdef LOCAL_GLUT32
#include "glut.h"
#else
#include <GL/glut.h>
#endif
#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
void gl_processNormalKeys(unsigned char key, int x, int y)
{
GLVIEW->processNormalKeys(key, x, y);
}
void gl_processSpecialKeys(int key, int x, int y)
{
GLVIEW->processSpecialKeys(key, x, y);
}
void gl_processReleasedKeys(unsigned char key, int x, int y)
{
GLVIEW->processReleasedKeys(key, x, y);
}
void gl_menu(int key)
{
GLVIEW->menu(key);
}
void gl_changeSize(int w, int h)
{
GLVIEW->changeSize(w,h);
}
void gl_handleIdle()
{
GLVIEW->handleIdle();
}
void gl_processMouse(int button, int state, int x, int y)
{
GLVIEW->processMouse(button, state, x, y);
}
void gl_processMouseActiveMotion(int x, int y)
{
GLVIEW->processMouseActiveMotion(x,y);
}
void gl_processMousePassiveMotion(int x, int y)
{
GLVIEW->processMousePassiveMotion(x,y);
}
void gl_renderScene()
{
GLVIEW->renderScene();
}
void glui_handleRW(int action)
{
GLVIEW->handleRW(action);
}
void glui_handleCloses(int action)
{
GLVIEW->handleCloses(action);
}
void RenderString(float x, float y, void *font, const char* string, float r, float g, float b)
{
glColor3f(r,g,b);
glRasterPos2f(x, y);
int len = (int) strlen(string);
for (int i = 0; i < len; i++)
glutBitmapCharacter(font, string[i]);
}
void drawCircle(float x, float y, float r) {
float n;
for (int k=0;k<17;k++) {
n = k*(M_PI/8);
glVertex3f(x+r*sin(n),y+r*cos(n),0);
}
}
void drawQuadrant(float x, float y, float r, float a, float b) {
glVertex3f(x,y,0);
float n;
for (int k=0;k<(int)((b-a)*8/M_PI+1);k++) {
n = k*(M_PI/8)+a;
glVertex3f(x+r*sin(n),y+r*cos(n),0);
}
glVertex3f(x+r*sin(b),y+r*cos(b),0);
glVertex3f(x,y,0);
}
GLView::GLView(World *s) :
world(world),
modcounter(0),
frames(0),
lastUpdate(0),
mousedrag(false)
{
xtranslate= 0.0;
ytranslate= 0.0;
scalemult= 0.2;
downb[0]=0;downb[1]=0;downb[2]=0;
mousex=0;mousey=0;
}
GLView::~GLView()
{
}
void GLView::changeSize(int w, int h)
{
//Tell GLUT that were changing the projection, not the actual view
glMatrixMode(GL_PROJECTION);
//Reset the coordinate system
glLoadIdentity();
//resize viewport to new size
glViewport(0, 0, w, h);
//reconcile projection (required to keep everything that's visible, visible
glOrtho(0,w,h,0,0,1);
//revert to normal view opperations
glMatrixMode(GL_MODELVIEW);
}
void GLView::processMouse(int button, int state, int x, int y)
{
if(world->isDebug()) printf("MOUSE EVENT: button=%i state=%i x=%i y=%i, scale=%f, mousedrag=%i\n", button, state, x, y, scalemult, mousedrag);
if(!mousedrag && state==1){ //dont let mouse click do anything if drag flag is raised
//have world deal with it. First translate to world coordinates though
int wx= (int) ((x-glutGet(GLUT_WINDOW_WIDTH)/2)/scalemult-xtranslate);
int wy= (int) ((y-glutGet(GLUT_WINDOW_HEIGHT)/2)/scalemult-ytranslate);
if(world->processMouse(button, state, wx, wy, scalemult)) live_selection = Select::MANUAL;
}
mousex=x; mousey=y;
mousedrag= false;
downb[button]=1-state; //state is backwards, ah well
}
void GLView::processMouseActiveMotion(int x, int y)
{
if(world->isDebug()) printf("MOUSE MOTION x=%i y=%i, %i %i %i\n", x, y, downb[0], downb[1], downb[2]);
if (downb[0]==1) {
//left mouse button drag: pan around
xtranslate += (x-mousex)/scalemult;
ytranslate += (y-mousey)/scalemult;
if (abs(x-mousex)>6 || abs(x-mousex)>6) live_follow= 0;
//for releasing follow if the mouse is used to drag screen, but there's a threshold
}
if (downb[1]==1) {
//mouse wheel. Change scale
scalemult -= conf::ZOOM_SPEED*(y-mousey);
if(scalemult<0.01) scalemult=0.01;
}
/* if(downb[2]==1){ //disabled
//right mouse button.
}*/
if(abs(mousex-x)>4 || abs(mousey-y)>4) mousedrag= true; //mouse was clearly dragged, don't select agents after
mousex=x; mousey=y;
}
void GLView::processMousePassiveMotion(int x, int y)
{
//for mouse scrolling. [DISABLED]
/* if(y<=30) ytranslate += 2*(30-y);
if(y>=glutGet(GLUT_WINDOW_HEIGHT)-30) ytranslate -= 2*(y-(glutGet(GLUT_WINDOW_HEIGHT)-30));
if(x<=30) xtranslate += 2*(30-x);
if(x>=glutGet(GLUT_WINDOW_WIDTH)-30) xtranslate -= 2*(x-(glutGet(GLUT_WINDOW_WIDTH)-30));*/
}
void GLView::processNormalKeys(unsigned char key, int x, int y)
{
menu(key);
}
void GLView::processSpecialKeys(int key, int x, int y)
{
menuS(key);
}
void GLView::processReleasedKeys(unsigned char key, int x, int y)
{
if (key==32) {//spacebar input [released]
world->pinput1= 0;
}
}
void GLView::menu(int key)
{
ReadWrite* savehelper= new ReadWrite(); //for loading/saving
if (key == 27) //[esc] quit
exit(0);
else if (key=='p') {
//pause
live_paused= !live_paused;
} else if (key=='m') { //drawing
live_fastmode= !live_fastmode;
} else if (key==43) { //+
live_skipdraw++;
} else if (key==45) { //-
live_skipdraw--;
} else if (key=='l' || key=='k') { //layer switch; l= "next", k= "previous"
if (key=='l') live_layersvis++;
else live_layersvis--;
if (live_layersvis>Layer::LAYERS) live_layersvis= 0;
if (live_layersvis<0) live_layersvis= Layer::LAYERS;
} else if (key=='z' || key=='x') { //change agent visual scheme; x= "next", z= "previous"
if (key=='x') live_agentsvis++;
else live_agentsvis--;
if (live_agentsvis>Visual::VISUALS-1) live_agentsvis= Visual::NONE;
if (live_agentsvis<Visual::NONE) live_agentsvis= Visual::VISUALS-1;
} else if (key==1001) { //add agents
world->addAgents(5);
} else if (key==1002) { //add Herbivore agents
world->addAgents(5, Stomach::PLANT);
} else if (key==1003) { //add Carnivore agents
world->addAgents(5, Stomach::MEAT);
} else if (key==1004) { //add Frugivore agents
world->addAgents(5, Stomach::FRUIT);
} else if (key=='c') {
world->setClosed( !world->isClosed() );
live_worldclosed= (int) world->isClosed();
/* glutGet(GLUT_MENU_NUM_ITEMS);
if (world->isClosed()) glutChangeToMenuEntry(4, "Open World", 'c');
else glutChangeToMenuEntry(4, "Close World", 'c');
glutSetMenu(m_id);*/
} else if (key=='f') {
live_follow= !live_follow; //toggle follow selected agent
} else if(key=='o') {
if(live_selection!=Select::OLDEST) live_selection= Select::OLDEST; //follow oldest agent
else live_selection= Select::NONE;
} else if(key=='q') {
//zoom and translocate to instantly see the whole world
float scaleA= (float)glutGet(GLUT_WINDOW_WIDTH)/(conf::WIDTH+2200);
float scaleB= (float)glutGet(GLUT_WINDOW_HEIGHT)/(conf::HEIGHT+150);
if(scaleA>scaleB) scalemult= scaleB;
else scalemult= scaleA;
xtranslate= -(conf::WIDTH-2020)/2;
ytranslate= -(conf::HEIGHT-80)/2;
live_follow= 0;
} else if(key=='g') {
if(live_selection!=Select::BEST_GEN) live_selection= Select::BEST_GEN; //follow most advanced generation agent
else live_selection= Select::NONE;
} else if(key=='h') {
if(live_selection!=Select::HEALTHY) live_selection= Select::HEALTHY; //follow healthiest
else live_selection= Select::NONE;
}else if (key==127) { //delete
world->selectedKill();
}else if (key==62) { //zoom+ >
scalemult += 10*conf::ZOOM_SPEED;
}else if (key==60) { //zoom- <
scalemult -= 10*conf::ZOOM_SPEED;
if(scalemult<0.01) scalemult=0.01;
}else if (key==32) { //spacebar input [pressed]
world->pinput1= 1;
}else if (key=='/') { // / heal selected
world->selectedHeal();
}else if (key=='|') { // | reproduce selected
world->selectedBabys();
}else if (key==119) { //w (move faster)
world->pcontrol= true;
world->pleft= capm(world->pleft + 0.08, -1, 1);
world->pright= capm(world->pright + 0.08, -1, 1);
}else if (key==97) { //a (turn left)
world->pcontrol= true;
world->pleft= capm(world->pleft - 0.05 + (world->pright-world->pleft)*0.05, -1, 1); //this extra code helps with turning out of tight circles
world->pright= capm(world->pright + 0.05 + (world->pleft-world->pright)*0.05, -1, 1);
}else if (key==115) { //s (move slower)
world->pcontrol= true;
world->pleft= capm(world->pleft - 0.08, -1, 1);
world->pright= capm(world->pright - 0.08, -1, 1);
}else if (key==100) { //d (turn right)
world->pcontrol= true;
world->pleft= capm(world->pleft + 0.05 + (world->pright-world->pleft)*0.05, -1, 1);
world->pright= capm(world->pright - 0.05 + (world->pleft-world->pright)*0.05, -1, 1);
} else if (key==999) { //player control
world->setControl(!world->pcontrol);
glutGet(GLUT_MENU_NUM_ITEMS);
if (world->pcontrol) glutChangeToMenuEntry(1, "Release Agent", 999);
else glutChangeToMenuEntry(1, "Control Selected (w,a,s,d)", 999);
glutSetMenu(m_id);
}else if (key==1005) { //menu only, debug mode
world->setDebug( !world->isDebug() );
live_debug= (int) world->isDebug();
/* glutGet(GLUT_MENU_NUM_ITEMS);
if (world->isDebug()){
glutChangeToMenuEntry(18, "Exit Debug Mode", 1005);
printf("Entered Debug Mode\n");
} else glutChangeToMenuEntry(18, "Enter Debug Mode", 1005);
glutSetMenu(m_id);*/
}else if (key==1006) { //force-reset config
world->writeConfig();
}else if (key==1007) { // reset
world->reset();
world->spawn();
printf("WORLD RESET!\n");
} else if (key==1008) { //save world
handleRW(2);
} else if (key==1009) { //load world
handleRW(1);
} else if (world->isDebug()) {
printf("Unknown key pressed: %i\n", key);
}
//other keys: '1':49, '2':50, ..., '0':48
}
void GLView::menuS(int key) // movement control
{
if (key == GLUT_KEY_UP) {
ytranslate += 20/scalemult;
} else if (key == GLUT_KEY_LEFT) {
xtranslate += 20/scalemult;
} else if (key == GLUT_KEY_DOWN) {
ytranslate -= 20/scalemult;
} else if (key == GLUT_KEY_RIGHT) {
xtranslate -= 20/scalemult;
}
}
void GLView::glCreateMenu()
{
m_id = glutCreateMenu(gl_menu); //right-click context menu
glutAddMenuEntry("Control Selected (w,a,s,d)", 999); //line contains mode-specific text, see menu function above
glutAddMenuEntry("Heal Agent (/)", '/');
glutAddMenuEntry("Delete Agent (del)", 127);
glutAddMenuEntry("-------------------",-1);
glutAddMenuEntry("Spawn Agents", 1001);
glutAddMenuEntry("Spawn Herbivores", 1002);
glutAddMenuEntry("Spawn Carnivores", 1003);
glutAddMenuEntry("Spawn Frugivores", 1004);
glutAddMenuEntry("Toggle Closed", 'c');
glutAddMenuEntry("Save World",1008);
glutAddMenuEntry("-------------------",-1);
glutAddMenuEntry("Load World",1009);
glutAddMenuEntry("Enter Debug Mode", 1005);
glutAddMenuEntry("Reset Agents", 1007);
glutAddMenuEntry("Reset Config", 1006);
glutAddMenuEntry("Exit (esc)", 27);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void GLView::gluiCreateMenu()
{
//GLUI menu. Slimy, yet satisfying.
//must set our live vars to something. Might as well do it here
live_worldclosed= 0;
live_paused= 0;
live_fastmode= 0;
live_skipdraw= 1;
live_agentsvis= Visual::RGB;
live_layersvis= Layer::LAND+1;
live_selection= Select::MANUAL;
live_follow= 0;
live_autosave= 1;
live_debug= 0;
//create GLUI and add the options, be sure to connect them all to their real vals later
Menu = GLUI_Master.create_glui("Menu",0,20,20);
Menu->add_checkbox("Closed world",&live_worldclosed);
Menu->add_checkbox("Pause",&live_paused);
Menu->add_checkbox("Allow Autosaves",&live_autosave);
new GLUI_Button(Menu,"Load World",1, glui_handleRW);
new GLUI_Button(Menu,"Save World",2, glui_handleRW);
new GLUI_Button(Menu,"New World",3, glui_handleRW);
GLUI_Panel *panel_speed= new GLUI_Panel(Menu,"Speed Control");
Menu->add_checkbox_to_panel(panel_speed,"Fast Mode",&live_fastmode);
Menu->add_spinner_to_panel(panel_speed,"Speed",GLUI_SPINNER_INT,&live_skipdraw);
GLUI_Rollout *rollout_vis= new GLUI_Rollout(Menu,"Visuals");
GLUI_RadioGroup *group_layers= new GLUI_RadioGroup(rollout_vis,&live_layersvis);
new GLUI_StaticText(rollout_vis,"Layer");
new GLUI_RadioButton(group_layers,"off");
for(int i=0; i<Layer::LAYERS; i++){
//this code allows the layers to be defined in any order, but displayed as below
char text[16]= "";
if(i==Layer::PLANTS) strcpy(text, "Plant");
else if(i==Layer::MEATS) strcpy(text, "Meat");
else if(i==Layer::HAZARDS) strcpy(text, "Hazard");
else if(i==Layer::FRUITS) strcpy(text, "Fruit");
else if(i==Layer::LAND) strcpy(text, "Map");
else if(i==Layer::LIGHT) strcpy(text, "Light");
new GLUI_RadioButton(group_layers,text);
}
Menu->add_column_to_panel(rollout_vis,true);
GLUI_RadioGroup *group_agents= new GLUI_RadioGroup(rollout_vis,&live_agentsvis);
new GLUI_StaticText(rollout_vis,"Agents");
for(int i=0; i<Visual::VISUALS; i++){
char text[16]= "";
if(i==Visual::NONE) strcpy(text, "off");
else if(i==Visual::RGB) strcpy(text, "RGB");
else if(i==Visual::STOMACH) strcpy(text, "Stomach");
else if(i==Visual::DISCOMFORT) strcpy(text, "Discomfort");
else if(i==Visual::VOLUME) strcpy(text, "Volume");
else if(i==Visual::SPECIES) strcpy(text, "Species ID");
else if(i==Visual::CROSSABLE) strcpy(text, "Crossable");
else if(i==Visual::HEALTH) strcpy(text, "Health");
new GLUI_RadioButton(group_agents,text);
}
GLUI_Rollout *rollout_xyl= new GLUI_Rollout(Menu,"Selection");
GLUI_RadioGroup *group_select= new GLUI_RadioGroup(rollout_xyl,&live_selection);
for(int i=0; i<Select::SELECT_TYPES; i++){
char text[16]= "";
if(i==Select::OLDEST) strcpy(text, "Oldest");
else if(i==Select::BEST_GEN) strcpy(text, "Best Gen.");
else if(i==Select::HEALTHY) strcpy(text, "Healthiest");
else if(i==Select::PRODUCTIVE) strcpy(text, "Productive");
else if(i==Select::AGGRESSIVE) strcpy(text, "Aggressive");
else if(i==Select::NONE) strcpy(text, "off");
else if(i==Select::MANUAL) strcpy(text, "Manual");
else if(i==Select::SOCIAL) strcpy(text, "Social");
new GLUI_RadioButton(group_select,text);
}
Menu->add_checkbox_to_panel(rollout_xyl, "Follow Selected", &live_follow);
Menu->add_button_to_panel(rollout_xyl, "Save Selected", 4, glui_handleRW);
Menu->add_checkbox("DEBUG",&live_debug);
//set to main graphics window
Menu->set_main_gfx_window(win1);
}
void GLView::handleRW(int action) //glui callback for saving/loading worlds
{
live_paused= 1;
//action= 1: loading, action= 2: saving, action= 3: new (reset) world
if (action==1){ //load option selected
Loader = GLUI_Master.create_glui("Load World",0,50,50);
Filename= new GLUI_EditText(Loader,"File Name (e.g, 'WORLD.SCB'):");
Filename->set_w(300);
new GLUI_Button(Loader,"Load",1, glui_handleCloses);
Loader->set_main_gfx_window(win1);
} else if (action==2){ //save option
Saver = GLUI_Master.create_glui("Save World",0,50,50);
Filename= new GLUI_EditText(Saver,"File Name (e.g, 'WORLD.SCB'):");
Filename->set_w(300);
new GLUI_Button(Saver,"Save",2, glui_handleCloses);
Saver->set_main_gfx_window(win1);
} else if (action==3){ //new world (old reset)
Alert = GLUI_Master.create_glui("Alert",0,50,50);
Alert->show();
new GLUI_StaticText(Alert,"Are you sure? This will");
new GLUI_StaticText(Alert,"erase the current world.");
new GLUI_Button(Alert,"Okay",3, glui_handleCloses);
new GLUI_Button(Alert,"Cancel",4, glui_handleCloses);
Alert->set_main_gfx_window(win1);
} else if (action==4){ //save selected agent
Saver = GLUI_Master.create_glui("Save Agent",0,50,50);
Filename= new GLUI_EditText(Saver,"File Name (e.g, 'AGENT.BOT'):");
Filename->set_w(300);
new GLUI_Button(Saver,"Save",6, glui_handleCloses);
}
}
void GLView::handleCloses(int action) //GLUI callback for handling window closing
{
live_paused= 0;
ReadWrite* savehelper= new ReadWrite(); //for loading/saving
if (action==1){ //loading
strcpy(filename,Filename->get_text());
if (debug) printf("File: '\\saves\\%s'\n",filename);
if (!filename || filename=="" || filename==NULL || filename[0]=='\0'){
printf("ERROR: empty filename; returning to program.\n");
Alert = GLUI_Master.create_glui("Alert",0,50,50);
Alert->show();
new GLUI_StaticText(Alert,"No file name given.");
new GLUI_StaticText(Alert,"Returning to main program.");
new GLUI_Button(Alert,"Okay",4, glui_handleCloses);
} else {
savehelper->loadWorld(world, xtranslate, ytranslate, filename);
}
Loader->hide();
} else if (action==2){ //saving
trySaveWorld();
Saver->hide();
} else if (action==3){ //resetting
world->reset();
world->spawn();
printf("WORLD RESET!\n");
Alert->hide();
} else if (action==4){ //Alert cancel/continue
Alert->hide();
} else if (action==5){ //Alert from above saving
savehelper->saveWorld(world, xtranslate, ytranslate, filename);
Alert->hide();
} else if (action==6){ //saving agents
const char *tempname= Filename->get_text();
strcpy(filename, tempname);
if (debug) printf("File: '\\saved_agents\\%s'",filename);
if (!filename || filename==""){
printf("ERROR: empty filename; returning to program.\n");
Alert = GLUI_Master.create_glui("Alert",0,50,50);
Alert->show();
new GLUI_StaticText(Alert, "No file name given." );
new GLUI_StaticText(Alert, "Returning to main program." );
new GLUI_Button(Alert,"Okay");
} else {
//check the filename given to see if it exists yet
char address[32];
strcpy(address,"saved_agents\\");
strcat(address,tempname);
// FILE* ck = fopen(address, "r");
// if(ck){
// if (debug) printf("WARNING: %s already exists!\n", filename);
//
// Alert = GLUI_Master.create_glui("Alert",0,50,50);
// Alert->show();
// new GLUI_StaticText(Alert, "The file name given already exists." );
// new GLUI_StaticText(Alert, "Would you like to overwrite?" );
// new GLUI_Button(Alert,"Okay",7, glui_handleCloses);
// new GLUI_Button(Alert,"Cancel",4, glui_handleCloses);
// live_paused= 1;
// fclose(ck);
// } else {
// fclose(ck);
FILE* sa = fopen(address, "w");
int sidx= world->getSelectedAgent();
if (sidx>=0){
Agent *a= &world->agents[sidx];
savehelper->saveAgent(a, sa);
}
fclose(sa);
// }
}
Saver->hide();
// } else if (action==7){ //overwrite agent save
// FILE* sa = fopen(address, "w");
// int sidx= world->getSelectedAgent();
// if (sidx>=0){
// savehelper->saveAgent(world->agents[sidx], sa);
// }
// fclose(sa);
}
}
void GLView::trySaveWorld(bool autosave)
{
const char *tempname;
if(autosave) tempname= "AUTOSAVE.SCB";
else tempname= Filename->get_text();
strcpy(filename, tempname);
if (debug) printf("File: '\\saves\\%s'\n",filename);
if (!filename || filename=="" || filename==NULL || filename[0]=='\0'){
printf("ERROR: empty filename; returning to program.\n");
Alert = GLUI_Master.create_glui("Alert",0,50,50);
Alert->show();
new GLUI_StaticText(Alert, "No file name given." );
new GLUI_StaticText(Alert, "Returning to main program." );
new GLUI_Button(Alert,"Okay");
} else {
char address[32];
strcpy(address,"saves\\");
strcat(address,tempname);
if(autosave){ //autosave overwrites
ReadWrite* savehelper= new ReadWrite();
savehelper->saveWorld(world, xtranslate, ytranslate, filename);
} else {
//check the filename given to see if it exists yet
FILE* ck = fopen(address, "r");
if(ck){
if (debug) printf("WARNING: %s already exists!\n", filename);
Alert = GLUI_Master.create_glui("Alert",0,50,50);
Alert->show();
new GLUI_StaticText(Alert, "The file name given already exists." );
new GLUI_StaticText(Alert, "Would you like to overwrite?" );
new GLUI_Button(Alert,"Okay",5, glui_handleCloses);
new GLUI_Button(Alert,"Cancel",4, glui_handleCloses);
live_paused= 1;
fclose(ck);
} else {
ReadWrite* savehelper= new ReadWrite();
savehelper->saveWorld(world, xtranslate, ytranslate, filename);
}
}
}
}
void GLView::handleIdle()
{
//set proper window (we don't want to draw on nothing, now do we?!)
if (glutGetWindow() != win1) glutSetWindow(win1);
GLUI_Master.sync_live_all();
//after syncing all the live vars with GLUI_Master, set the vars they represent to their proper values.
world->setClosed(live_worldclosed);
world->setDebug((bool) live_debug);
modcounter++;
if (!live_paused) world->update();
//autosave world periodically, based on world time
if (live_autosave==1 && world->modcounter%(world->FRAMES_PER_EPOCH)==0) trySaveWorld(true);
//show FPS and other stuff
int currentTime = glutGet( GLUT_ELAPSED_TIME );
frames++;
if ((currentTime - lastUpdate) >= 1000) {
sprintf( buf, "FPS: %d speed: %d Total Agents: %d Herbivores: %d Carnivores: %d Frugivores: %d Epoch: %d",
frames, live_skipdraw, world->getAgents(), world->getHerbivores(), world->getCarnivores(), world->getFrugivores(), world->epoch() );
glutSetWindowTitle( buf );
frames = 0;
lastUpdate = currentTime;
}
if (!live_fastmode) {
if (live_skipdraw>0) {
if (modcounter%live_skipdraw==0) renderScene(); //increase fps by skipping drawing
}
else { //we will decrease fps by waiting using clocks
clock_t endwait;
float mult=-0.005*(live_skipdraw-1); //ugly, ah well
endwait = clock () + mult * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
renderScene();
}
}
}
void GLView::renderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT)/2, 0.0f);
glScalef(scalemult, scalemult, 1.0f);
//handle world agent selection interface
world->setSelection(live_selection);
if (world->getSelection()==-1 && live_selection!=Select::MANUAL && modcounter%5==0) live_selection= Select::NONE;
if(live_follow==1){ //if we're following,
float xi=0, yi=0;
world->getFollowLocation(xi, yi); //get the follow location from the world (location of selected agent)
if(xi!=0 && yi!=0){
// if we got to move the screen completly accross the world, jump instead
if(abs(-xi-xtranslate)>0.95*conf::WIDTH || abs(-yi-ytranslate)>0.95*conf::HEIGHT){
xtranslate= -xi; ytranslate= -yi;
} else {
float speed= conf::SNAP_SPEED;
if(scalemult>0.5) speed= cap(speed*pow(scalemult + 0.5,3));
xtranslate+= speed*(-xi-xtranslate); ytranslate+= speed*(-yi-ytranslate);
}
}
}
glTranslatef(xtranslate, ytranslate, 0.0f);
world->draw(this, live_layersvis);
glPopMatrix();
glutSwapBuffers();
}
void GLView::drawAgent(const Agent& agent, float x, float y, bool ghost)
{
float n;
float r= agent.radius;
float rp= agent.radius+2.5;
if (live_agentsvis!=Visual::NONE) {
//color assignment
float red= 0,gre= 0,blu= 0;
float discomfort= 0;
//first, calculate colors which also have indicator boxes
float dd= 2.0*abs(agent.pos.y/conf::HEIGHT - 0.5);
discomfort= cap(sqrt(abs(dd-agent.temperature_preference)));
if (discomfort<0.08) discomfort=0;
float stomach_red= cap(pow(agent.stomach[Stomach::MEAT],2)+pow(agent.stomach[Stomach::FRUIT],2)-pow(agent.stomach[Stomach::PLANT],2));
float stomach_gre= cap(pow(agent.stomach[Stomach::PLANT],2)/2+pow(agent.stomach[Stomach::FRUIT],2)-pow(agent.stomach[Stomach::MEAT],2));
//now colorize agents and other things
if (live_agentsvis==Visual::RGB){ //real rgb values
red= agent.red; gre= agent.gre; blu= agent.blu;
} else if (live_agentsvis==Visual::STOMACH){ //stomach
red= stomach_red;
gre= stomach_gre;
} else if (live_agentsvis==Visual::DISCOMFORT){ //temp discomfort
red= discomfort; gre= (2-discomfort)/2; blu= 1-discomfort;
} else if (live_agentsvis==Visual::VOLUME) { //volume
red= agent.volume;
gre= agent.volume;
blu= agent.volume;
} else if (live_agentsvis==Visual::SPECIES){ //species
red= (cos((float)agent.species/330*M_PI)+1.0)/2.0;
gre= (sin((float)agent.species/100*M_PI)+1.0)/2.0;
blu= (cos((float)agent.species/43*M_PI)+1.0)/2.0;
} else if (live_agentsvis==Visual::CROSSABLE){ //crossover-compatable to selection
//all other agents look grey if unrelated or if none is selected, b/c then we don't have a reference
red= 0.8;
gre= 0.8;
blu= 0.8;
if(world->getSelectedAgent()>=0){
float deviation= abs(agent.species - world->agents[world->getSelectedAgent()].species); //species deviation check
if (deviation==0) { //exact copies
red= 0.2;
} else if (deviation<=world->MAXDEVIATION) {
//reproducable relatives
red= 0;
gre= 0;
} else if (deviation<=3*world->MAXDEVIATION) {
//un-reproducable relatives
red= 0.6;
gre= 0.4;
}
}
} else if (live_agentsvis==Visual::HEALTH) { //volume
gre= powf(agent.health/2,0.5);
red= ((int)(agent.health*1000)%2==0) ? (1.0-agent.health/2)*gre : (1.0-agent.health/2);
}
//handle selected agent
if (agent.id==world->getSelection() && !ghost) {
//draw selection
glBegin(GL_POLYGON);
glColor3f(1,1,1);
drawCircle(x, y, agent.radius+3/scalemult);
glEnd();
if(scalemult > .2){
glPushMatrix();
glTranslatef(x-80,y+20,0);
//draw inputs, outputs
float col;
float yy=15;
float xx=15;
float ss=16;
glBegin(GL_QUADS);
for (int j=0;j<Input::INPUT_SIZE;j++) {
col= agent.in[j];
if(j==Input::TEMP) glColor3f(col,(2-col)/2,(1-col));
glColor3f(col,col,col);
glVertex3f(0+ss*j, 0, 0.0f);
glVertex3f(xx+ss*j, 0, 0.0f);
glVertex3f(xx+ss*j, yy, 0.0f);
glVertex3f(0+ss*j, yy, 0.0f);
if(scalemult > .7){
glEnd();
if(j==Input::CLOCK1 || j==Input::CLOCK2 || j==Input::CLOCK3){
RenderString(xx/3+ss*j, yy*2/3, GLUT_BITMAP_HELVETICA_12, "Q", 0.0f, 0.0f, 0.0f);
} else if(j==Input::TEMP){
RenderString(xx/3+ss*j, yy*2/3, GLUT_BITMAP_HELVETICA_12, "H", 0.8f, 0.5f, 0.0f);
} else if(j==Input::HEARING1 || j==Input::HEARING2){
RenderString(xx/3+ss*j, yy*2/3, GLUT_BITMAP_HELVETICA_12, "E", 1.0f, 1.0f, 1.0f);
}
glBegin(GL_QUADS);
}
}
yy+=5;
for (int j=0;j<Output::OUTPUT_SIZE;j++) {
col= agent.out[j];
if(j==Output::RED) glColor3f(col,0,0);
else if (j==Output::GRE) glColor3f(0,col,0);
else if (j==Output::BLU) glColor3f(0,0,col);
else if (j==Output::JUMP) glColor3f(col,col,0);
// else if (j==Output::GRAB) glColor3f(0,col,col);
else if (j==Output::TONE) glColor3f((1-col)*(1-col),1-fabs(col-0.5)*2,col*col);
else glColor3f(col,col,col);
glVertex3f(0+ss*j, yy, 0.0f);
glVertex3f(xx+ss*j, yy, 0.0f);
glVertex3f(xx+ss*j, yy+ss, 0.0f);
glVertex3f(0+ss*j, yy+ss, 0.0f);
if(scalemult > .7){
glEnd();
if(j==Output::LEFT_WHEEL_B || j==Output::LEFT_WHEEL_F || j==Output::RIGHT_WHEEL_B || j==Output::RIGHT_WHEEL_F){
RenderString(xx/3+ss*j, yy+ss*2/3, GLUT_BITMAP_HELVETICA_12, "!", 0.0f, 1.0f, 0.0f);
} else if(j==Output::VOLUME){
RenderString(xx/3+ss*j, yy+ss*2/3, GLUT_BITMAP_HELVETICA_12, "V", 1.0f, 1.0f, 1.0f);
} else if(j==Output::CLOCKF3){
RenderString(xx/3+ss*j, yy+ss*2/3, GLUT_BITMAP_HELVETICA_12, "Q", 0.0f, 0.0f, 0.0f);
} else if(j==Output::SPIKE){
RenderString(xx/3+ss*j, yy+ss*2/3, GLUT_BITMAP_HELVETICA_12, "S", 1.0f, 0.0f, 0.0f);
} else if(j==Output::PROJECT){
RenderString(xx/3+ss*j, yy+ss*2/3, GLUT_BITMAP_HELVETICA_12, "P", 0.5f, 0.0f, 0.5f);
}
glBegin(GL_QUADS);
}
}
yy+=ss*2;
glEnd();
if(world->isDebug()){
//draw brain in debug mode
glBegin(GL_QUADS);
float offx=0;
ss=8;
xx=ss;
for (int j=0;j<BRAINSIZE;j++) {
col = agent.brain.boxes[j].out;
glColor3f(col,col,col);
glVertex3f(offx+0+ss*j, yy, 0.0f);
glVertex3f(offx+xx+ss*j, yy, 0.0f);
glVertex3f(offx+xx+ss*j, yy+ss, 0.0f);
glVertex3f(offx+ss*j, yy+ss, 0.0f);
if ((j+1)%30==0) {
yy+=ss;
offx-=ss*30;
}
}
glEnd();
glPopMatrix();
//draw DIST range on selected in DEBUG
glBegin(GL_LINES);
glColor3f(1,0,1);
for (int k=0;k<17;k++)
{
n = k*(M_PI/8);
glVertex3f(x+world->DIST*sin(n),y+world->DIST*cos(n),0);
n = (k+1)*(M_PI/8);
glVertex3f(x+world->DIST*sin(n),y+world->DIST*cos(n),0);
}
glEnd();
//now spike, share, and grab effective zones
glBegin(GL_POLYGON);
glColor4f(1,0,0,0.35);
glVertex3f(x,y,0);
glVertex3f(x+(r+agent.spikeLength*world->SPIKELENGTH)*cos(agent.angle+M_PI/8),
y+(r+agent.spikeLength*world->SPIKELENGTH)*sin(agent.angle+M_PI/8),0);
glVertex3f(x+(r+agent.spikeLength*world->SPIKELENGTH)*cos(agent.angle),
y+(r+agent.spikeLength*world->SPIKELENGTH)*sin(agent.angle),0);
glVertex3f(x+(r+agent.spikeLength*world->SPIKELENGTH)*cos(agent.angle-M_PI/8),
y+(r+agent.spikeLength*world->SPIKELENGTH)*sin(agent.angle-M_PI/8),0);
glVertex3f(x,y,0);
glEnd();
//grab is currently a range-only thing, change?
glBegin(GL_POLYGON);
glColor4f(0,1,1,0.15);
drawCircle(x,y,r+world->GRABBING_DISTANCE);
glEnd();
//health-sharing
glBegin(GL_POLYGON);
glColor4f(0,0.5,0,0.25);
drawCircle(x,y,r+world->FOOD_SHARING_DISTANCE);
glEnd();
} else glPopMatrix();
}
/*
//draw lines connecting connected brain boxes, but only in debug mode (NEEDS SIMPLIFICATION)
if(world->isDebug()){
glEnd();
glBegin(GL_LINES);
float offx=0;
ss=30;
xx=ss;
for (int j=0;j<BRAINSIZE;j++) {
for(int k=0;k<CONNS;k++){
int j2= agent.brain.boxes[j].id[k];
//project indices j and j2 into pixel space
float x1= 0;
float y1= 0;
if(j<Input::INPUT_SIZE) { x1= j*ss; y1= yy; }
else {
x1= ((j-Input::INPUT_SIZE)%30)*ss;
y1= yy+ss+2*ss*((int) (j-Input::INPUT_SIZE)/30);
}
float x2= 0;
float y2= 0;
if(j2<Input::INPUT_SIZE) { x2= j2*ss; y2= yy; }
else {
x2= ((j2-Input::INPUT_SIZE)%30)*ss;
y2= yy+ss+2*ss*((int) (j2-Input::INPUT_SIZE)/30);
}
float ww= agent.brain.boxes[j].w[k];
if(ww<0) glColor3f(-ww, 0, 0);
else glColor3f(0,0,ww);
glVertex3f(x1,y1,0);
glVertex3f(x2,y2,0);
}
}
}*/
}
//draw indicator of this agent... used for various events
if (agent.indicator>0) {
glBegin(GL_POLYGON);
glColor4f(agent.ir,agent.ig,agent.ib,0.75);
drawCircle(x, y, r+((int)agent.indicator));
glEnd();
}
//draw giving/receiving
if(agent.dfood!=0){
glBegin(GL_POLYGON);
float mag=cap(abs(agent.dfood)/world->FOODTRANSFER/3);
if(agent.dfood>0) glColor3f(0,mag,0);
else glColor3f(mag,0,0); //draw sharing as a thick green or red outline
for (int k=0;k<17;k++){
n = k*(M_PI/8);
glVertex3f(x+rp*sin(n),y+rp*cos(n),0);
n = (k+1)*(M_PI/8);
glVertex3f(x+rp*sin(n),y+rp*cos(n),0);
}
glEnd();
}
if (scalemult > .1 || ghost) { //dont render eyes, ears, or boost if zoomed too far out, but always render them on ghosts
//draw eyes
for(int q=0;q<NUMEYES;q++) {
glBegin(GL_LINES);
glColor4f(0.5,0.5,0.5,0.75);
glVertex3f(x,y,0);
float aa= agent.angle+agent.eyedir[q];
glVertex3f(x+(r+30)*cos(aa), y+(r+30)*sin(aa), 0);
glEnd();
}
//ears
for(int q=0;q<NUMEARS;q++) {
glBegin(GL_POLYGON);
glColor4f(0.6,0.6,0,0.5);
float aa= agent.angle + agent.eardir[q];
drawCircle(x+r*cos(aa), y+r*sin(aa), 2.0);
glEnd();
}
//boost blur
if (agent.boost){
for(int q=1;q<4;q++){
Vector2f displace(r/4*q*(agent.w1+agent.w2), 0);
displace.rotate(agent.angle+M_PI);
glBegin(GL_POLYGON);
glColor4f(red,gre,blu,0.25);
drawCircle(x+displace.x, y+displace.y, r);
glEnd();
}
}
//vis grabbing
if((scalemult > .3 || ghost) && agent.grabbing>0.5){
glLineWidth(2);
glBegin(GL_LINES);
glColor4f(0.0,0.7,0.7,0.75);
glVertex3f(x,y,0);
float mult= agent.grabID==-1 ? 1 : 0;
float aa= agent.angle+M_PI/8*mult;
float ab= agent.angle-M_PI/8*mult;
glVertex3f(x+(world->GRABBING_DISTANCE+r)*cos(aa), y+(world->GRABBING_DISTANCE+r)*sin(aa), 0);
glVertex3f(x,y,0);
glVertex3f(x+(world->GRABBING_DISTANCE+r)*cos(ab), y+(world->GRABBING_DISTANCE+r)*sin(ab), 0);