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.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.h"
  16. t_creature* create_random_creature()
  17. {
  18. t_creature* res = malloc(sizeof(t_creature));
  19. t_genome genome = rand_genome();
  20. res->genome_tree = create_new_gnometree_node(genome);
  21. res->energy = res->genome_tree->gnome.energy;
  22. res->life = res->genome_tree->gnome.life;
  23. res->timeleft = res->genome_tree->gnome.lifetime;
  24. res->reprotimer = 0;
  25. res->mutex = malloc(sizeof(pthread_mutex_t));
  26. pthread_mutex_init(res->mutex,NULL);
  27. res->mem.mem = malloc(sizeof(t_mem_element)*res->genome_tree->gnome.mem_size);
  28. bzero(res->mem.mem, sizeof(t_mem_element)*res->genome_tree->gnome.mem_size);
  29. res->x = -1;
  30. res->y = -1;
  31. return res;
  32. }
  33. t_creature* create_gnome_creature(t_tree_genome* gnome)
  34. {
  35. t_creature* res = malloc(sizeof(t_creature));
  36. res->genome_tree = gnome;
  37. res->mem.mem = malloc(sizeof(t_mem_element)*res->genome_tree->gnome.mem_size);
  38. int i;
  39. for(i=0; i<res->genome_tree->gnome.mem_size; i++)
  40. res->mem.mem[i].type = 0;
  41. res->energy = res->genome_tree->gnome.energy;
  42. res->life = res->genome_tree->gnome.life;
  43. res->timeleft = res->genome_tree->gnome.lifetime;
  44. res->reprotimer = 0;
  45. res->mutex = malloc(sizeof(pthread_mutex_t));
  46. pthread_mutex_init(res->mutex,NULL);
  47. res->x = -1;
  48. res->y = -1;
  49. #ifdef DEBUG
  50. if(res->genome_tree == 0)
  51. {
  52. fprintf(stderr, "Erreur lors de la creation de la creature, gnome == NULL\n");
  53. exit(-1);
  54. }
  55. #endif
  56. return res;
  57. }
  58. t_chain_creature* create_creature_chain(t_creature* first)
  59. {
  60. t_chain_creature* res = malloc(sizeof(t_chain_creature));
  61. res->creat = first;
  62. res->next = NULL;
  63. res->prev = NULL;
  64. return res;
  65. }
  66. t_chain_creature* add_creature_chain(t_chain_creature* lst_creat, t_creature* new_creat)
  67. {
  68. t_chain_creature *cur1 = lst_creat->next, *cur2, *current;
  69. if(cur1 == NULL)
  70. {
  71. lst_creat->next = create_creature_chain(new_creat);
  72. return lst_creat->next;
  73. }
  74. else if(cur1->next == NULL)
  75. {
  76. cur1->next = create_creature_chain(new_creat);
  77. }
  78. else
  79. {
  80. cur2 = cur1->next;
  81. current = create_creature_chain(new_creat);
  82. cur1->next = current;
  83. current->prev = cur1;
  84. current->next = cur2;
  85. cur2->prev = current;
  86. }
  87. return cur1->next;
  88. }
  89. t_chain_creature* del_creature_chain(t_chain_creature** lst_creat, t_creature* dead_creat)
  90. {
  91. t_chain_creature *cur1 = (*lst_creat)->next, *cur2, *current;
  92. if((*lst_creat)->creat == dead_creat)
  93. {
  94. if((*lst_creat)->next == NULL)
  95. {
  96. free(*lst_creat);
  97. *lst_creat = NULL;
  98. free(dead_creat);
  99. return NULL;
  100. }
  101. current = (*lst_creat)->next;
  102. free(*lst_creat);
  103. current->prev = NULL;
  104. (*lst_creat) = current;
  105. free(dead_creat);
  106. return current;
  107. }
  108. current = (*lst_creat)->next;
  109. cur2 = (*lst_creat);
  110. while(current != NULL)
  111. {
  112. if(current->creat == dead_creat)
  113. {
  114. cur2->next = current->next;
  115. if(current->next != NULL)
  116. current->next->prev = cur2;
  117. free(current);
  118. free(dead_creat);
  119. return (*lst_creat);
  120. }
  121. cur2 = current;
  122. current = current->next;
  123. }
  124. return (*lst_creat);
  125. /*while(current != NULL && ! trouve)
  126. {
  127. if(current->creat == dead_creat)
  128. {
  129. trouve = 1;
  130. if(current->next != NULL)
  131. {
  132. current->creat = current->next->creat;
  133. cur1 = current->next;
  134. current->next = current->next->next;
  135. if(current->next != NULL)
  136. current->next->prev = current;
  137. free(cur1);
  138. free(dead_creat);
  139. return current;
  140. }
  141. else
  142. {
  143. cur2->next = NULL;
  144. free(current);
  145. free(dead_creat);
  146. return cur2;
  147. }
  148. }
  149. cur2 = current;
  150. current = current->next;
  151. }
  152. return NULL; */
  153. }