123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- /*
- Copyright (C) 2011 Weber Yann, Schuck Clement
-
- This file is part of Tansive.
-
- Tansive is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- Tansive is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Tansive. If not, see <http://www.gnu.org/licenses/>.
- */
- #include"creature.h"
-
- t_creature* create_random_creature()
- {
- t_creature* res = malloc(sizeof(t_creature));
- t_genome genome = rand_genome();
-
- res->genome_tree = create_new_gnometree_node(genome);
-
- res->energy = res->genome_tree->gnome.energy;
- res->life = res->genome_tree->gnome.life;
- res->timeleft = res->genome_tree->gnome.lifetime;
- res->reprotimer = 0;
- res->mutex = malloc(sizeof(pthread_mutex_t));
- pthread_mutex_init(res->mutex,NULL);
-
- res->mem.mem = malloc(sizeof(t_mem_element)*res->genome_tree->gnome.mem_size);
- bzero(res->mem.mem, sizeof(t_mem_element)*res->genome_tree->gnome.mem_size);
-
- res->x = -1;
- res->y = -1;
-
- return res;
- }
-
- t_creature* create_gnome_creature(t_tree_genome* gnome)
- {
- t_creature* res = malloc(sizeof(t_creature));
-
- res->genome_tree = gnome;
-
- res->mem.mem = malloc(sizeof(t_mem_element)*res->genome_tree->gnome.mem_size);
- int i;
- for(i=0; i<res->genome_tree->gnome.mem_size; i++)
- res->mem.mem[i].type = 0;
-
- res->energy = res->genome_tree->gnome.energy;
- res->life = res->genome_tree->gnome.life;
- res->timeleft = res->genome_tree->gnome.lifetime;
- res->reprotimer = 0;
- res->mutex = malloc(sizeof(pthread_mutex_t));
- pthread_mutex_init(res->mutex,NULL);
-
- res->x = -1;
- res->y = -1;
-
- #ifdef DEBUG
- if(res->genome_tree == 0)
- {
- fprintf(stderr, "Erreur lors de la creation de la creature, gnome == NULL\n");
- exit(-1);
- }
- #endif
-
- return res;
- }
-
- t_chain_creature* create_creature_chain(t_creature* first)
- {
- t_chain_creature* res = malloc(sizeof(t_chain_creature));
- res->creat = first;
- res->next = NULL;
- res->prev = NULL;
-
- return res;
- }
-
- t_chain_creature* add_creature_chain(t_chain_creature* lst_creat, t_creature* new_creat)
- {
- t_chain_creature *cur1 = lst_creat->next, *cur2, *current;
- if(cur1 == NULL)
- {
- lst_creat->next = create_creature_chain(new_creat);
- return lst_creat->next;
- }
- else if(cur1->next == NULL)
- {
- cur1->next = create_creature_chain(new_creat);
-
- }
- else
- {
- cur2 = cur1->next;
- current = create_creature_chain(new_creat);
- cur1->next = current;
- current->prev = cur1;
- current->next = cur2;
- cur2->prev = current;
- }
-
- return cur1->next;
- }
-
- t_chain_creature* del_creature_chain(t_chain_creature** lst_creat, t_creature* dead_creat)
- {
- t_chain_creature *cur1 = (*lst_creat)->next, *cur2, *current;
-
- if((*lst_creat)->creat == dead_creat)
- {
- if((*lst_creat)->next == NULL)
- {
- free(*lst_creat);
- *lst_creat = NULL;
- free(dead_creat);
- return NULL;
- }
- current = (*lst_creat)->next;
- free(*lst_creat);
- current->prev = NULL;
-
- (*lst_creat) = current;
-
- free(dead_creat);
- return current;
-
- }
-
- current = (*lst_creat)->next;
- cur2 = (*lst_creat);
-
- while(current != NULL)
- {
- if(current->creat == dead_creat)
- {
- cur2->next = current->next;
- if(current->next != NULL)
- current->next->prev = cur2;
- free(current);
- free(dead_creat);
- return (*lst_creat);
- }
- cur2 = current;
- current = current->next;
- }
-
- return (*lst_creat);
-
- /*while(current != NULL && ! trouve)
- {
- if(current->creat == dead_creat)
- {
- trouve = 1;
- if(current->next != NULL)
- {
- current->creat = current->next->creat;
- cur1 = current->next;
- current->next = current->next->next;
-
- if(current->next != NULL)
- current->next->prev = current;
- free(cur1);
- free(dead_creat);
- return current;
- }
- else
- {
- cur2->next = NULL;
- free(current);
- free(dead_creat);
- return cur2;
- }
- }
- cur2 = current;
- current = current->next;
- }
- return NULL; */
- }
-
|