123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- /*
- 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_logic.h"
-
- unsigned int naturaldead;
- unsigned int fightdead;
- unsigned int eatdead;
- unsigned int starveddead;
- unsigned int born;
-
- void creature_decision(t_map* mp,t_chain_creature* lst_creat, t_creature* creat, char* dead, t_chain_creature** ptr_head)
- {
- int x_acc, y_acc, i=0;
- char possible_action = 0;
- char repro = 0;
-
- #ifdef DEBUG
- printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",creat->genome_tree, creat->x, creat->y);
- #endif
-
- possible_action = check_action(mp,creat,&x_acc,&y_acc);
- if(possible_action & LOGIC_ACTION_EATVEGET && creat->energy < creat->genome_tree->gnome.energy && creat->genome_tree->gnome.food & GNOME_VEGAN)
- {
- creat->energy+=10;
- mp->map[x_acc][y_acc].type = MAP_ELT_EMPTY;
- #ifdef DEBUG
- t_chain_creature* current;
- current = lst_creat;
- printf("Bouffe vegan\n");
- while(current != NULL)
- {
- if(current->creat->genome_tree == 0 || current->creat->x > mp->x || current->creat->y > mp->y )
- {
- fprintf(stderr, "Erreur de bouffe veget\n");
- printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",current->creat->genome_tree, current->creat->x, current->creat->y);
-
- exit(-1);
- }
- current = current->next;
- }
- #endif
-
- }
- else if(possible_action & LOGIC_ACTION_CREAT && !(rand()%creat->genome_tree->gnome.aggression))
- {
-
- t_creature* ennemy = mp->map[x_acc][y_acc].creat;
- fight_creat(mp, creat, ennemy, dead, ptr_head);
- #ifdef DEBUG
- t_chain_creature* current;
- current = lst_creat;
-
- printf("Fight ! x= %d, y= %d\n", creat->x, creat->y);
- while(current != NULL)
- {
- if(current->creat->genome_tree == 0 || current->creat->x > mp->x || current->creat->y > mp->y )
- {
- fprintf(stderr, "Erreur de fight\n");
- printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",current->creat->genome_tree, current->creat->x, current->creat->y);
-
- exit(-1);
- }
- current = current->next;
- }
- #endif
-
- }
- else if(creat->reprotimer == creat->genome_tree->gnome.reprofreq && possible_action & LOGIC_ACTION_CREAT && !repro)
- {
-
- repro = 1;
- t_tree_genome* repro_gnome = creat->genome_tree;
-
- if(!(rand()%GNOME_MUTATION_CHANCE))
- {
- printf("MUTATION!\n");
- repro_gnome = add_child_treegnome(creat->genome_tree,create_new_gnometree_node(mutation(creat->genome_tree->gnome)));
- }
-
- t_chain_creature* new_creat = repro_creat(mp,lst_creat,creat,repro_gnome);
-
- #ifdef DEBUG
- t_chain_creature* current;
- current = lst_creat;
- printf("Reproduction\n");
- while(current != NULL)
- {
- if(current->creat->genome_tree == 0 || current->creat->x > mp->x || current->creat->y > mp->y )
- {
- fprintf(stderr, "Erreur d'init du repro_gnome\n");
- printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",current->creat->genome_tree, current->creat->x, current->creat->y);
-
- exit(-1);
- }
- current = current->next;
- }
- #endif
-
- }
- else
- {
- random_move(mp, creat);
- #ifdef DEBUG
- t_chain_creature* current;
- current = lst_creat;
-
- printf("Move\n");
- while(current != NULL)
- {
- if(current->creat->genome_tree == 0 || current->creat->x > mp->x || current->creat->y > mp->y )
- {
- fprintf(stderr, "Erreur de move\n");
- printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",current->creat->genome_tree, current->creat->x, current->creat->y);
-
- exit(-1);
- }
- current = current->next;
- }
- #endif
- }
-
- i++;
-
- creat->reprotimer = (creat->reprotimer == creat->genome_tree->gnome.reprofreq?creat->genome_tree->gnome.reprofreq:creat->reprotimer+1);
- creat->timeleft--;
-
- if(creat->energy == 0)
- creat->life--;
- else
- creat->energy--;
-
-
- if(creat->timeleft <= 0 || creat->life == 0)
- {
-
- if(creat->life == 0 && creat->energy == 0)
- {
- starveddead++;
- creat->genome_tree->gnome.nb_star_dead++;
- }
- else
- {
- naturaldead++;
- creat->genome_tree->gnome.nb_nat_dead++;
- }
-
- *dead = 1;
-
- }
- #ifdef DEBUG
- t_chain_creature* current;
- current = lst_creat;
-
- while(current != NULL)
- {
- if(current->creat->genome_tree == 0 || current->creat->x > mp->x || current->creat->y > mp->y )
- {
- fprintf(stderr, "Erreur de je ne sais quoi\n");
- printf("\tcreat->genome_tree = %d, x= %d, y= %d\n",current->creat->genome_tree, current->creat->x, current->creat->y);
-
- exit(-1);
- }
- current = current->next;
- }
-
- printf("Fin ! x= %d, y= %d\n", creat->x, creat->y);
- #endif
-
- }
-
- char check_action(t_map* mp, t_creature* creat, int* x, int* y)
- {
- int i = creat->x;
- int j = creat->y;
- char res = 0;
-
- *x = 0;
- *y = 0;
-
- if(i>0 && j>0 && i<mp->x-1 && j<mp->y-1)
- {
- if(mp->map[i-1][j].type == MAP_ELT_CREAT)
- {
- res |= LOGIC_ACTION_CREAT;
- *x = i-1;
- *y = j;
- }
- else if(mp->map[i-1][j].type == MAP_ELT_VEGET)
- {
- res |= LOGIC_ACTION_EATVEGET;
- *x = i-1;
- *y = j;
- }
-
- else if(mp->map[i-1][j+1].type == MAP_ELT_CREAT)
- {
- res |= LOGIC_ACTION_CREAT;
- *x = i-1;
- *y = j+1;
- }
- else if(mp->map[i-1][j+1].type == MAP_ELT_VEGET)
- {
- res |= LOGIC_ACTION_EATVEGET;
- *x = i-1;
- *y = j+1;
- }
-
- else if(mp->map[i-1][j-1].type == MAP_ELT_CREAT)
- {
- res |= LOGIC_ACTION_CREAT;
- *x = i-1;
- *y = j-1;
- }
- else if(mp->map[i-1][j-1].type == MAP_ELT_VEGET)
- {
- res |= LOGIC_ACTION_EATVEGET;
- *x = i-1;
- *y = j-1;
- }
-
- else if(mp->map[i][j+1].type == MAP_ELT_CREAT)
- {
- res |= LOGIC_ACTION_CREAT;
- *x = i;
- *y = j+1;
- }
- else if(mp->map[i][j+1].type == MAP_ELT_VEGET)
- {
- res |= LOGIC_ACTION_EATVEGET;
- *x = i;
- *y = j+1;
- }
-
- else if(mp->map[i][j-1].type == MAP_ELT_CREAT)
- {
- res |= LOGIC_ACTION_CREAT;
- *x = i;
- *y = j-1;
- }
- else if(mp->map[i][j-1].type == MAP_ELT_VEGET)
- {
- res |= LOGIC_ACTION_EATVEGET;
- *x = i;
- *y = j-1;
- }
-
- else if(mp->map[i+1][j].type == MAP_ELT_CREAT)
- {
- res |= LOGIC_ACTION_CREAT;
- *x = i+1;
- *y = j;
- }
- else if(mp->map[i+1][j].type == MAP_ELT_VEGET)
- {
- res |= LOGIC_ACTION_EATVEGET;
- *x = i+1;
- *y = j;
- }
-
- else if(mp->map[i+1][j+1].type == MAP_ELT_CREAT)
- {
- res |= LOGIC_ACTION_CREAT;
- *x = i+1;
- *y = j+1;
- }
- else if(mp->map[i+1][j+1].type == MAP_ELT_VEGET)
- {
- res |= LOGIC_ACTION_EATVEGET;
- *x = i+1;
- *y = j+1;
- }
-
- else if(mp->map[i+1][j-1].type == MAP_ELT_CREAT)
- {
- res |= LOGIC_ACTION_CREAT;
- *x = i+1;
- *y = j-1;
- }
- else if(mp->map[i+1][j-1].type == MAP_ELT_VEGET)
- {
- res |= LOGIC_ACTION_EATVEGET;
- *x = i+1;
- *y = j-1;
- }
-
- else
- return 0;
- }
- else
- return 0;
- return res;
- }
-
- t_chain_creature* repro_creat(t_map* mp, t_chain_creature* lst_creat, t_creature* creat, t_tree_genome* gnome)
- {
- t_chain_creature *cur1 = lst_creat->next, *cur2, *current;
- creat->reprotimer = 0;
- int d = 0;
-
- t_creature *new_creat = create_gnome_creature(gnome);
-
- new_creat->x = creat->x;
- new_creat->y = creat->y;
- do
- {
- if(new_creat->x>mp->x/2)
- new_creat->x-=1;
- else
- new_creat->x+=1;
-
- if(new_creat->y>mp->y/2)
- new_creat->y-=1;
- else
- new_creat->y+=1;
- d++;
-
- }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);
- if(d==LOGIC_MAX_DIST_BORN)
- {
- free(new_creat);
- return NULL;
- }
- else
- {
- born++;
- gnome->gnome.nb_creat++;
- mp->map[new_creat->x][new_creat->y].type = MAP_ELT_CREAT;
- mp->map[new_creat->x][new_creat->y].creat = new_creat;
- return add_creature_chain(lst_creat,new_creat);
- }
- }
-
- void random_move(t_map* mp, t_creature* creat)
- {
- char dir = rand()%4;
- int speed = rand()%creat->genome_tree->gnome.speed;
- int i;
- move_creat(mp,creat,dir);
- }
-
- void move_creat(t_map* mp, t_creature* creat,char dir)
- {
- switch(dir)
- {
- case MOVE_UP:
- if(creat->y<mp->y-1 && mp->map[creat->x][creat->y+1].type == MAP_ELT_EMPTY)
- {
- mp->map[creat->x][creat->y].type = MAP_ELT_EMPTY;
- creat->y++;
- }
- break;
- case MOVE_DWN:
- if(creat->y>0 && mp->map[creat->x][creat->y-1].type == MAP_ELT_EMPTY)
- {
- mp->map[creat->x][creat->y].type = MAP_ELT_EMPTY;
- creat->y--;
- }
- break;
- case MOVE_RGHT:
- if(creat->x<mp->x-1 && mp->map[creat->x+1][creat->y].type == MAP_ELT_EMPTY)
- {
- mp->map[creat->x][creat->y].type = MAP_ELT_EMPTY;
- creat->x++;
- }
- break;
- case MOVE_LFT:
- if(creat->x>0 && mp->map[creat->x-1][creat->y].type == MAP_ELT_EMPTY)
- {
- mp->map[creat->x][creat->y].type = MAP_ELT_EMPTY;
- creat->x--;
- }
- break;
-
- }
-
-
- mp->map[creat->x][creat->y].type = MAP_ELT_CREAT;
- mp->map[creat->x][creat->y].creat = creat;
- }
-
- void fight_creat(t_map* mp, t_creature* creat1, t_creature* creat2, char* creat1_dead, t_chain_creature** ptr_head)
- {
- 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;
- char alive1 = 1, alive2 = 1;
- t_tree_genome *gnome_tree1 = creat1->genome_tree, *gnome_tree2 = creat2->genome_tree;
-
-
- creat1->life -= strenght2-(rand()%(strenght2/10+1));
- creat2->life -= strenght1-(rand()%(strenght1/10+1));
-
- if(creat1->life <= 0)
- {
- mp->map[creat1->x][creat1->y].type = MAP_ELT_EMPTY;
- //del_creature_chain(ptr_head,creat1);
- alive1 = 0;
- *creat1_dead = 1;
- }
- else
- *creat1_dead = 0;
-
- if(creat2->life <= 0)
- {
-
- mp->map[creat2->x][creat2->y].type = MAP_ELT_EMPTY;
- del_creature_chain(ptr_head,creat2);
- alive2 = 0;
- }
-
- if(!alive1 && alive2)
- {
- if(creat2->genome_tree->gnome.food & GNOME_CARNI)
- {
- eatdead++;
- gnome_tree1->gnome.nb_eat_dead++;
- creat2->life += weight1;
- creat2->life = (creat2->life>creat2->genome_tree->gnome.life?creat2->genome_tree->gnome.life:creat2->life);
- }
- else
- {
- fightdead++;
- gnome_tree1->gnome.nb_figt_dead++;
- }
- //gnome_tree1->gnome.nb_creat--;
- }
- else if(alive1 && !alive2)
- {
- if(creat1->genome_tree->gnome.food & GNOME_CARNI)
- {
- eatdead++;
- gnome_tree2->gnome.nb_eat_dead++;
- creat1->life += weight2;
- creat1->life = (creat1->life>creat1->genome_tree->gnome.life?creat1->genome_tree->gnome.life:creat1->life);
- }
- else
- {
- fightdead++;
- gnome_tree2->gnome.nb_figt_dead++;
- }
- gnome_tree2->gnome.nb_creat--;
- }
- else if(!alive1 && !alive2)
- {
- gnome_tree1->gnome.nb_figt_dead++;
- gnome_tree2->gnome.nb_figt_dead++;
- fightdead+=2;
- gnome_tree2->gnome.nb_creat--;
- }
- }
|