1
0
Ответвление 0
tansive/src/creature_logic.c
2011-09-22 13:32:21 +02:00

465 строки
11 КиБ
C

/*
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--;
}
}