Tests About Natural Selection In Virtual Environment
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. Copyright (C) 2011 Weber Yann, Schuck Clement
  3. This file is part of Tansive.
  4. Tansive is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Tansive is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Tansive. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include<stdio.h>
  16. #include<string.h>
  17. #include<time.h>
  18. #include<SDL/SDL.h>
  19. #include"map.h"
  20. #include"creature_logic.h"
  21. #include"visu.h"
  22. #include"config.h"
  23. #include"event.h"
  24. #define TEST_DISP_EMPTY_GNOME 1
  25. #define NB_TOURS_TEST 200
  26. #define NB_TOURS_REGEN_VEGET 200
  27. extern unsigned int naturaldead;
  28. extern unsigned int fightdead;
  29. extern unsigned int starveddead;
  30. extern unsigned int eatdead;
  31. extern unsigned int born;
  32. /*
  33. Function to display help and exit.
  34. */
  35. void usage(char* name)
  36. {
  37. fprintf(stderr, "Usage : %s [OPTIONS]\n", name);
  38. fprintf(stderr,"\nRun tansive\n");
  39. fprintf(stderr,"\nDisplay mode:\n");
  40. fprintf(stderr,"\t--display-genome\tDisplay genome tree informations\n");
  41. fprintf(stderr,"\t--disable-info\t\tDisable the display of infos about the number of creature or the number of elapsed round\n");
  42. fprintf(stderr,"\t--no-display\t\tDisable the SDL display\n");
  43. fprintf(stderr,"\nOutput modes:\n");
  44. fprintf(stderr,"\t-odot output.dot\tSpecify the output file for graphviz\n");
  45. fprintf(stderr,"\nWorld options:\n");
  46. fprintf(stderr,"\t--resolution x,y\tSpecify the resolution of the window and the size of the world\n");
  47. fprintf(stderr,"\t-ni number\t\tSpecify the number of creature initialy generated\n");
  48. fprintf(stderr,"\t-nm number\t\tSpecify the maximum number of creature allowed in world\n");
  49. exit(-1);
  50. }
  51. int main(int argc, char** argv)
  52. {
  53. naturaldead=eatdead=fightdead=starveddead=born=0; //Initializing global variable
  54. int choix = 1, xc = -1, yc = -1, zoom = 1, mx = 0, my = 0, i=0, j,k,l;
  55. //variable to store command line options
  56. char type_test = 0, sdl_disp = 1, gnome_disp = 0, info_disp = 1;
  57. int x_resol = 800, y_resol = 800;
  58. int init_number_creat = 800, max_number_creat = 15000;
  59. char* o_dot_name = NULL;
  60. //main loop variable
  61. char again = 1;
  62. int count = 0;
  63. //threads variable
  64. pthread_mutex_t mut_event;
  65. pthread_t event_catcher_thread;
  66. t_param_thread_event param_thread;
  67. //SDL variable
  68. SDL_Surface *screen = NULL, *surface = NULL;
  69. Uint32 rmask, gmask, bmask, amask;
  70. SDL_Rect bckgrndPos;
  71. for(i=1;i<argc;i++)
  72. {
  73. if(strcmp(argv[i], "--help") == 0)
  74. usage(argv[0]);
  75. else if(strcmp(argv[i],"--no-display") == 0)
  76. sdl_disp = 0;
  77. else if(strcmp(argv[i],"--display-genome") == 0)
  78. gnome_disp = 1;
  79. else if(strcmp(argv[i],"--disable-info") == 0)
  80. info_disp = 0;
  81. else if(strcmp(argv[i],"-odot") == 0)
  82. {
  83. i++;
  84. if(argc<i)
  85. usage(argv[0]);
  86. o_dot_name = argv[i];
  87. FILE* file = fopen(o_dot_name, "w+");
  88. if(!file)
  89. usage(argv[0]);
  90. fclose(file);
  91. }
  92. else if(strcmp(argv[i],"--resolution") == 0)
  93. {
  94. i++;
  95. if(argc<=i)
  96. usage(argv[0]);
  97. for(j=0;argv[i][j]!='\0';j++)
  98. {
  99. if(argv[i][j] == ',')
  100. {
  101. argv[i][j] = '\0';
  102. break;
  103. }
  104. }
  105. j++;
  106. if(argv[i][j]=='\0')
  107. usage(argv[0]);
  108. x_resol = atoi(argv[i]);
  109. y_resol = atoi(argv[i]+j);
  110. if(x_resol<=0 || y_resol <=0)
  111. usage(argv[0]);
  112. }
  113. else if(strcmp(argv[i],"-ni") == 0)
  114. {
  115. i++;
  116. if(argc<=i)
  117. usage(argv[0]);
  118. init_number_creat = atoi(argv[i]);
  119. if(init_number_creat<=0)
  120. usage(argv[0]);
  121. }
  122. else if(strcmp(argv[i],"-nm") == 0)
  123. {
  124. i++;
  125. if(argc<=i)
  126. usage(argv[0]);
  127. max_number_creat = atoi(argv[i]);
  128. if(max_number_creat<=0)
  129. usage(argv[0]);
  130. }
  131. else
  132. usage(argv[0]);
  133. }
  134. //Initializing variable
  135. t_chain_creature *lst_creat = NULL, *current = NULL;
  136. t_creature *sav = NULL;
  137. t_tree_genome* head = NULL;
  138. bckgrndPos.x = 0;
  139. bckgrndPos.y = 0;
  140. xc = x_resol/2;
  141. yc = y_resol/2;
  142. srand(time(NULL));
  143. t_map* map = create_map(x_resol, y_resol);
  144. //Generating the first creature, with a random genome
  145. lst_creat = create_creature_chain(create_random_creature());
  146. head = lst_creat->creat->genome_tree;
  147. lst_creat->creat->genome_tree->gnome.nb_creat++;
  148. current = lst_creat;
  149. current->creat->x = rand()%x_resol;
  150. current->creat->y = rand()%y_resol;
  151. map->map[current->creat->x][current->creat->y].type = MAP_ELT_CREAT;
  152. map->map[current->creat->x][current->creat->y].creat = current->creat;
  153. //Generating the other creatures
  154. for(i=1;i<init_number_creat;i++)
  155. {
  156. lst_creat->creat->genome_tree->gnome.nb_creat++;
  157. current = add_creature_chain(lst_creat, create_gnome_creature(current->creat->genome_tree));
  158. current->creat->x = rand()%x_resol;
  159. current->creat->y = rand()%y_resol;
  160. map->map[current->creat->x][current->creat->y].type = MAP_ELT_CREAT;
  161. map->map[current->creat->x][current->creat->y].creat = current->creat;
  162. }
  163. //Initialization of SDL variable
  164. if(sdl_disp)
  165. {
  166. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  167. rmask = 0xff000000;
  168. gmask = 0x00ff0000;
  169. bmask = 0x0000ff00;
  170. amask = 0x000000ff;
  171. #else
  172. rmask = 0x000000ff;
  173. gmask = 0x0000ff00;
  174. bmask = 0x00ff0000;
  175. amask = 0xff000000;
  176. #endif
  177. if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
  178. {
  179. fprintf(stderr, "SDL initialisation error : %s\n", SDL_GetError());
  180. exit(EXIT_FAILURE);
  181. }
  182. screen = SDL_SetVideoMode(x_resol, y_resol, 32, (SDL_HWSURFACE | SDL_DOUBLEBUF));
  183. if(screen == NULL)
  184. {
  185. fprintf(stderr, "Failed to load video mode : %s\n", SDL_GetError());
  186. exit(EXIT_FAILURE);
  187. }
  188. surface = SDL_CreateRGBSurface(SDL_HWSURFACE, x_resol, y_resol, 32, rmask, gmask, bmask, amask);
  189. SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0, 0, 0));
  190. SDL_EnableKeyRepeat(10, 10);
  191. SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0, 0, 0));
  192. }
  193. k=j=0;
  194. //Pre-generation of vegetals
  195. for(i=0;i<NB_TOURS_REGEN_VEGET;i++)
  196. {
  197. map_regen_veget(map);
  198. if(sdl_disp)
  199. {
  200. for(l=0; l<(x_resol*y_resol)/NB_TOURS_REGEN_VEGET; l++)
  201. {
  202. setPixelRGB(surface, j,k,0,255,0);
  203. j=(j==x_resol-1?0:j+1);
  204. k=(j==0?k+1:k);
  205. }
  206. SDL_BlitSurface(surface, NULL, screen, &bckgrndPos);
  207. SDL_Flip(screen);
  208. }
  209. }
  210. char dead = 0;
  211. int counter_creat = 0, prev_nb_creat;
  212. if(sdl_disp)
  213. {
  214. //Launching the event catcher thread
  215. param_thread.x = &xc;
  216. param_thread.y = &yc;
  217. param_thread.mx = &mx;
  218. param_thread.my = &my;
  219. param_thread.x_resol = &x_resol;
  220. param_thread.y_resol = &y_resol;
  221. param_thread.again = &again;
  222. param_thread.zoom = &zoom;
  223. param_thread.mutex = &mut_event;
  224. pthread_mutex_init(&mut_event,NULL);
  225. pthread_create(&event_catcher_thread,NULL,event_catcher_loop,&param_thread);
  226. pthread_mutex_lock(&mut_event);
  227. }
  228. //Main loop
  229. while(again)
  230. {
  231. if(sdl_disp)
  232. {
  233. pthread_mutex_unlock(&mut_event);
  234. }
  235. map_regen_veget(map);
  236. //Allowing 1 decision per creature
  237. current = lst_creat;
  238. prev_nb_creat = counter_creat;
  239. counter_creat = 0;
  240. if(current != NULL)
  241. {
  242. do
  243. {
  244. dead = 0;
  245. sav = current->creat;
  246. //The decision
  247. creature_decision(map,lst_creat, current->creat, &dead, &lst_creat);
  248. current = current->next;
  249. if(dead)
  250. {
  251. sav->genome_tree->gnome.nb_creat--;
  252. map->map[sav->x][sav->y].type = MAP_ELT_EMPTY;
  253. lst_creat = del_creature_chain(&lst_creat,sav);
  254. }
  255. else
  256. {
  257. counter_creat++;
  258. }
  259. }while(current != NULL);
  260. }
  261. if(info_disp)
  262. printf("%d | nb creatures = %d | born = %d | deads : natural = %d, starved = %d, fight = %d, eat = %d\n",count, counter_creat,born, naturaldead, starveddead, fightdead,eatdead);
  263. clean_treegnome(head);
  264. if(gnome_disp)
  265. disp_genome_tree_info(head,1);
  266. if(o_dot_name && count%1000 == 0)
  267. {
  268. //Writing the genome tree for graphviz
  269. FILE* file = fopen(o_dot_name, "w+");
  270. write_graphviz_genome_tree(file, head);
  271. fclose(file);
  272. }
  273. count++;
  274. if(counter_creat > max_number_creat || counter_creat == 0)
  275. again = 0;
  276. if(sdl_disp)
  277. {
  278. pthread_mutex_lock(&mut_event);
  279. //Drawing the map
  280. SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0, 0, 0));
  281. draw_map_zoom(screen, surface,map, xc, yc, zoom);
  282. draw_zoom_area(screen,surface, mx, my, map);
  283. SDL_BlitSurface(surface, NULL, screen, &bckgrndPos);
  284. SDL_Flip(screen);
  285. }
  286. }//Main loop's end
  287. if(sdl_disp)
  288. {
  289. pthread_mutex_unlock(&mut_event);
  290. pthread_join(event_catcher_thread, NULL);
  291. SDL_Quit();
  292. }
  293. if(o_dot_name)
  294. {
  295. //Writing the genome tree for graphviz
  296. FILE* file = fopen(o_dot_name, "w+");
  297. write_graphviz_genome_tree(file, head);
  298. fclose(file);
  299. }
  300. return 0;
  301. }