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.

creature_logic.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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"creature_logic.h"
  16. unsigned int naturaldead;
  17. unsigned int fightdead;
  18. unsigned int eatdead;
  19. unsigned int starveddead;
  20. unsigned int born;
  21. void creature_decision(t_map* mp,t_chain_creature* lst_creat, t_creature* creat, char* dead, t_chain_creature** ptr_head)
  22. {
  23. int x_acc, y_acc, i=0;
  24. char possible_action = 0;
  25. char repro = 0;
  26. #ifdef DEBUG
  27. printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",creat->genome_tree, creat->x, creat->y);
  28. #endif
  29. possible_action = check_action(mp,creat,&x_acc,&y_acc);
  30. if(possible_action & LOGIC_ACTION_EATVEGET && creat->energy < creat->genome_tree->gnome.energy && creat->genome_tree->gnome.food & GNOME_VEGAN)
  31. {
  32. creat->energy+=10;
  33. mp->map[x_acc][y_acc].type = MAP_ELT_EMPTY;
  34. #ifdef DEBUG
  35. t_chain_creature* current;
  36. current = lst_creat;
  37. printf("Bouffe vegan\n");
  38. while(current != NULL)
  39. {
  40. if(current->creat->genome_tree == 0 || current->creat->x > mp->x || current->creat->y > mp->y )
  41. {
  42. fprintf(stderr, "Erreur de bouffe veget\n");
  43. printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",current->creat->genome_tree, current->creat->x, current->creat->y);
  44. exit(-1);
  45. }
  46. current = current->next;
  47. }
  48. #endif
  49. }
  50. else if(possible_action & LOGIC_ACTION_CREAT && !(rand()%creat->genome_tree->gnome.aggression))
  51. {
  52. t_creature* ennemy = mp->map[x_acc][y_acc].creat;
  53. fight_creat(mp, creat, ennemy, dead, ptr_head);
  54. #ifdef DEBUG
  55. t_chain_creature* current;
  56. current = lst_creat;
  57. printf("Fight ! x= %d, y= %d\n", creat->x, creat->y);
  58. while(current != NULL)
  59. {
  60. if(current->creat->genome_tree == 0 || current->creat->x > mp->x || current->creat->y > mp->y )
  61. {
  62. fprintf(stderr, "Erreur de fight\n");
  63. printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",current->creat->genome_tree, current->creat->x, current->creat->y);
  64. exit(-1);
  65. }
  66. current = current->next;
  67. }
  68. #endif
  69. }
  70. else if(creat->reprotimer == creat->genome_tree->gnome.reprofreq && possible_action & LOGIC_ACTION_CREAT && !repro)
  71. {
  72. repro = 1;
  73. t_tree_genome* repro_gnome = creat->genome_tree;
  74. if(!(rand()%GNOME_MUTATION_CHANCE))
  75. {
  76. printf("MUTATION!\n");
  77. repro_gnome = add_child_treegnome(creat->genome_tree,create_new_gnometree_node(mutation(creat->genome_tree->gnome)));
  78. }
  79. t_chain_creature* new_creat = repro_creat(mp,lst_creat,creat,repro_gnome);
  80. #ifdef DEBUG
  81. t_chain_creature* current;
  82. current = lst_creat;
  83. printf("Reproduction\n");
  84. while(current != NULL)
  85. {
  86. if(current->creat->genome_tree == 0 || current->creat->x > mp->x || current->creat->y > mp->y )
  87. {
  88. fprintf(stderr, "Erreur d'init du repro_gnome\n");
  89. printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",current->creat->genome_tree, current->creat->x, current->creat->y);
  90. exit(-1);
  91. }
  92. current = current->next;
  93. }
  94. #endif
  95. }
  96. else
  97. {
  98. random_move(mp, creat);
  99. #ifdef DEBUG
  100. t_chain_creature* current;
  101. current = lst_creat;
  102. printf("Move\n");
  103. while(current != NULL)
  104. {
  105. if(current->creat->genome_tree == 0 || current->creat->x > mp->x || current->creat->y > mp->y )
  106. {
  107. fprintf(stderr, "Erreur de move\n");
  108. printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",current->creat->genome_tree, current->creat->x, current->creat->y);
  109. exit(-1);
  110. }
  111. current = current->next;
  112. }
  113. #endif
  114. }
  115. i++;
  116. creat->reprotimer = (creat->reprotimer == creat->genome_tree->gnome.reprofreq?creat->genome_tree->gnome.reprofreq:creat->reprotimer+1);
  117. creat->timeleft--;
  118. if(creat->energy == 0)
  119. creat->life--;
  120. else
  121. creat->energy--;
  122. if(creat->timeleft <= 0 || creat->life == 0)
  123. {
  124. if(creat->life == 0 && creat->energy == 0)
  125. {
  126. starveddead++;
  127. creat->genome_tree->gnome.nb_star_dead++;
  128. }
  129. else
  130. {
  131. naturaldead++;
  132. creat->genome_tree->gnome.nb_nat_dead++;
  133. }
  134. *dead = 1;
  135. }
  136. #ifdef DEBUG
  137. t_chain_creature* current;
  138. current = lst_creat;
  139. while(current != NULL)
  140. {
  141. if(current->creat->genome_tree == 0 || current->creat->x > mp->x || current->creat->y > mp->y )
  142. {
  143. fprintf(stderr, "Erreur de je ne sais quoi\n");
  144. printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",current->creat->genome_tree, current->creat->x, current->creat->y);
  145. exit(-1);
  146. }
  147. current = current->next;
  148. }
  149. printf("Fin ! x= %d, y= %d\n", creat->x, creat->y);
  150. #endif
  151. }
  152. char check_action(t_map* mp, t_creature* creat, int* x, int* y)
  153. {
  154. int i = creat->x;
  155. int j = creat->y;
  156. char res = 0;
  157. *x = 0;
  158. *y = 0;
  159. if(i>0 && j>0 && i<mp->x-1 && j<mp->y-1)
  160. {
  161. if(mp->map[i-1][j].type == MAP_ELT_CREAT)
  162. {
  163. res |= LOGIC_ACTION_CREAT;
  164. *x = i-1;
  165. *y = j;
  166. }
  167. else if(mp->map[i-1][j].type == MAP_ELT_VEGET)
  168. {
  169. res |= LOGIC_ACTION_EATVEGET;
  170. *x = i-1;
  171. *y = j;
  172. }
  173. else if(mp->map[i-1][j+1].type == MAP_ELT_CREAT)
  174. {
  175. res |= LOGIC_ACTION_CREAT;
  176. *x = i-1;
  177. *y = j+1;
  178. }
  179. else if(mp->map[i-1][j+1].type == MAP_ELT_VEGET)
  180. {
  181. res |= LOGIC_ACTION_EATVEGET;
  182. *x = i-1;
  183. *y = j+1;
  184. }
  185. else if(mp->map[i-1][j-1].type == MAP_ELT_CREAT)
  186. {
  187. res |= LOGIC_ACTION_CREAT;
  188. *x = i-1;
  189. *y = j-1;
  190. }
  191. else if(mp->map[i-1][j-1].type == MAP_ELT_VEGET)
  192. {
  193. res |= LOGIC_ACTION_EATVEGET;
  194. *x = i-1;
  195. *y = j-1;
  196. }
  197. else if(mp->map[i][j+1].type == MAP_ELT_CREAT)
  198. {
  199. res |= LOGIC_ACTION_CREAT;
  200. *x = i;
  201. *y = j+1;
  202. }
  203. else if(mp->map[i][j+1].type == MAP_ELT_VEGET)
  204. {
  205. res |= LOGIC_ACTION_EATVEGET;
  206. *x = i;
  207. *y = j+1;
  208. }
  209. else if(mp->map[i][j-1].type == MAP_ELT_CREAT)
  210. {
  211. res |= LOGIC_ACTION_CREAT;
  212. *x = i;
  213. *y = j-1;
  214. }
  215. else if(mp->map[i][j-1].type == MAP_ELT_VEGET)
  216. {
  217. res |= LOGIC_ACTION_EATVEGET;
  218. *x = i;
  219. *y = j-1;
  220. }
  221. else if(mp->map[i+1][j].type == MAP_ELT_CREAT)
  222. {
  223. res |= LOGIC_ACTION_CREAT;
  224. *x = i+1;
  225. *y = j;
  226. }
  227. else if(mp->map[i+1][j].type == MAP_ELT_VEGET)
  228. {
  229. res |= LOGIC_ACTION_EATVEGET;
  230. *x = i+1;
  231. *y = j;
  232. }
  233. else if(mp->map[i+1][j+1].type == MAP_ELT_CREAT)
  234. {
  235. res |= LOGIC_ACTION_CREAT;
  236. *x = i+1;
  237. *y = j+1;
  238. }
  239. else if(mp->map[i+1][j+1].type == MAP_ELT_VEGET)
  240. {
  241. res |= LOGIC_ACTION_EATVEGET;
  242. *x = i+1;
  243. *y = j+1;
  244. }
  245. else if(mp->map[i+1][j-1].type == MAP_ELT_CREAT)
  246. {
  247. res |= LOGIC_ACTION_CREAT;
  248. *x = i+1;
  249. *y = j-1;
  250. }
  251. else if(mp->map[i+1][j-1].type == MAP_ELT_VEGET)
  252. {
  253. res |= LOGIC_ACTION_EATVEGET;
  254. *x = i+1;
  255. *y = j-1;
  256. }
  257. else
  258. return 0;
  259. }
  260. else
  261. return 0;
  262. return res;
  263. }
  264. t_chain_creature* repro_creat(t_map* mp, t_chain_creature* lst_creat, t_creature* creat, t_tree_genome* gnome)
  265. {
  266. t_chain_creature *cur1 = lst_creat->next, *cur2, *current;
  267. creat->reprotimer = 0;
  268. int d = 0;
  269. t_creature *new_creat = create_gnome_creature(gnome);
  270. new_creat->x = creat->x;
  271. new_creat->y = creat->y;
  272. do
  273. {
  274. if(new_creat->x>mp->x/2)
  275. new_creat->x-=1;
  276. else
  277. new_creat->x+=1;
  278. if(new_creat->y>mp->y/2)
  279. new_creat->y-=1;
  280. else
  281. new_creat->y+=1;
  282. d++;
  283. }while(new_creat->x>0&&new_creat->y>0&&new_creat->x<mp->x-1 && new_creat->y<mp->y-1 && mp->map[new_creat->x][new_creat->y].type != MAP_ELT_EMPTY && d<LOGIC_MAX_DIST_BORN);
  284. if(d==LOGIC_MAX_DIST_BORN)
  285. {
  286. free(new_creat);
  287. return NULL;
  288. }
  289. else
  290. {
  291. born++;
  292. gnome->gnome.nb_creat++;
  293. mp->map[new_creat->x][new_creat->y].type = MAP_ELT_CREAT;
  294. mp->map[new_creat->x][new_creat->y].creat = new_creat;
  295. return add_creature_chain(lst_creat,new_creat);
  296. }
  297. }
  298. void random_move(t_map* mp, t_creature* creat)
  299. {
  300. char dir = rand()%4;
  301. int speed = rand()%creat->genome_tree->gnome.speed;
  302. int i;
  303. move_creat(mp,creat,dir);
  304. }
  305. void move_creat(t_map* mp, t_creature* creat,char dir)
  306. {
  307. switch(dir)
  308. {
  309. case MOVE_UP:
  310. if(creat->y<mp->y-1 && mp->map[creat->x][creat->y+1].type == MAP_ELT_EMPTY)
  311. {
  312. mp->map[creat->x][creat->y].type = MAP_ELT_EMPTY;
  313. creat->y++;
  314. }
  315. break;
  316. case MOVE_DWN:
  317. if(creat->y>0 && mp->map[creat->x][creat->y-1].type == MAP_ELT_EMPTY)
  318. {
  319. mp->map[creat->x][creat->y].type = MAP_ELT_EMPTY;
  320. creat->y--;
  321. }
  322. break;
  323. case MOVE_RGHT:
  324. if(creat->x<mp->x-1 && mp->map[creat->x+1][creat->y].type == MAP_ELT_EMPTY)
  325. {
  326. mp->map[creat->x][creat->y].type = MAP_ELT_EMPTY;
  327. creat->x++;
  328. }
  329. break;
  330. case MOVE_LFT:
  331. if(creat->x>0 && mp->map[creat->x-1][creat->y].type == MAP_ELT_EMPTY)
  332. {
  333. mp->map[creat->x][creat->y].type = MAP_ELT_EMPTY;
  334. creat->x--;
  335. }
  336. break;
  337. }
  338. mp->map[creat->x][creat->y].type = MAP_ELT_CREAT;
  339. mp->map[creat->x][creat->y].creat = creat;
  340. }
  341. void fight_creat(t_map* mp, t_creature* creat1, t_creature* creat2, char* creat1_dead, t_chain_creature** ptr_head)
  342. {
  343. int strenght1 = creat1->genome_tree->gnome.strenght, strenght2 = creat2->genome_tree->gnome.strenght, weight1 = creat1->genome_tree->gnome.weight, weight2 = creat2->genome_tree->gnome.weight;
  344. char alive1 = 1, alive2 = 1;
  345. t_tree_genome *gnome_tree1 = creat1->genome_tree, *gnome_tree2 = creat2->genome_tree;
  346. creat1->life -= strenght2-(rand()%(strenght2/10+1));
  347. creat2->life -= strenght1-(rand()%(strenght1/10+1));
  348. if(creat1->life <= 0)
  349. {
  350. mp->map[creat1->x][creat1->y].type = MAP_ELT_EMPTY;
  351. //del_creature_chain(ptr_head,creat1);
  352. alive1 = 0;
  353. *creat1_dead = 1;
  354. }
  355. else
  356. *creat1_dead = 0;
  357. if(creat2->life <= 0)
  358. {
  359. mp->map[creat2->x][creat2->y].type = MAP_ELT_EMPTY;
  360. del_creature_chain(ptr_head,creat2);
  361. alive2 = 0;
  362. }
  363. if(!alive1 && alive2)
  364. {
  365. if(creat2->genome_tree->gnome.food & GNOME_CARNI)
  366. {
  367. eatdead++;
  368. gnome_tree1->gnome.nb_eat_dead++;
  369. creat2->life += weight1;
  370. creat2->life = (creat2->life>creat2->genome_tree->gnome.life?creat2->genome_tree->gnome.life:creat2->life);
  371. }
  372. else
  373. {
  374. fightdead++;
  375. gnome_tree1->gnome.nb_figt_dead++;
  376. }
  377. //gnome_tree1->gnome.nb_creat--;
  378. }
  379. else if(alive1 && !alive2)
  380. {
  381. if(creat1->genome_tree->gnome.food & GNOME_CARNI)
  382. {
  383. eatdead++;
  384. gnome_tree2->gnome.nb_eat_dead++;
  385. creat1->life += weight2;
  386. creat1->life = (creat1->life>creat1->genome_tree->gnome.life?creat1->genome_tree->gnome.life:creat1->life);
  387. }
  388. else
  389. {
  390. fightdead++;
  391. gnome_tree2->gnome.nb_figt_dead++;
  392. }
  393. gnome_tree2->gnome.nb_creat--;
  394. }
  395. else if(!alive1 && !alive2)
  396. {
  397. gnome_tree1->gnome.nb_figt_dead++;
  398. gnome_tree2->gnome.nb_figt_dead++;
  399. fightdead+=2;
  400. gnome_tree2->gnome.nb_creat--;
  401. }
  402. }