From dfe9604dc9e4292c9f0e420d3a926e8268178308 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Fri, 9 Jan 2015 17:40:02 +0100 Subject: replace the code of the artificial "intellligence" the new function of the AI chooses a random target in the adversary team and use "attack" on it. Signed-off-by: Olivier Gayot --- ai.h | 8 ++++++ ia.c | 81 +++++++++++++++++++++++++----------------------------------- jouer.c | 5 ++-- prototypes.h | 3 --- 4 files changed, 44 insertions(+), 53 deletions(-) create mode 100644 ai.h diff --git a/ai.h b/ai.h new file mode 100644 index 0000000..070527d --- /dev/null +++ b/ai.h @@ -0,0 +1,8 @@ +#ifndef AI_H +#define AI_H + +#include "entry.h" + +void ai_play_turn(struct action_params_t *); + +#endif /* AI_H */ diff --git a/ia.c b/ia.c index f122df2..0844339 100644 --- a/ia.c +++ b/ia.c @@ -6,62 +6,49 @@ #include "prototypes.h" #include -void Factionennemi(int* Vtourennemi,SURFACES* surfaces,POSITIONS* positions, ENNEMIS ennemis[], PERSONNAGES persos[],int Vnbennemis,int*Vtour) +#include "actions.h" + +static struct character_t *ai_find_random_target(const struct team_t *team) { - int target,degats; + int alive = 0; + int target_idx; + + for (int i = 0; i < team->chr_cnt; ++i) { + if (team->chrs[i].alive) + ++alive; + } + + target_idx = rand() % alive; + + alive = 0; + for (int i = 0; i < team->chr_cnt; ++i) { + if (team->chrs[i].alive) { + if (alive == target_idx) + return &team->chrs[i]; - target=Fchoosetargetallie(persos); - degats=Fgeneratedegats(persos, ennemis,*Vtourennemi,target); - persos[target].pv=persos[target].pv-degats; - do - { - if(*VtourennemiVpositionmort.x=positions->Vpositionpersos[target].x+surfaces->Tperso[target]->w/2-surfaces->Pmort->w/2; - positions->Vpositionmort.y=positions->Vpositionpersos[target].y+surfaces->Tperso[target]->h/2-surfaces->Pmort->h/2; - SDL_BlitSurface(surfaces->Pmort,NULL,surfaces->Pecran,&positions->Vpositionmort); } - /* TODO re-enable */ -#if 0 - Fafficherdegats(surfaces,positions,degats,ALLIE,target,persos); -#endif - SDL_Delay(1000); - SDL_BlitSurface(surfaces->Pfondjeu,&positions->Vpositiondegats,surfaces->Pecran,&positions->Vpositiondegats); - SDL_Flip(surfaces->Pecran); + + /* no one is alive */ + return NULL; } -int Fchoosetargetallie(PERSONNAGES persos[]) +void ai_play_turn(struct action_params_t *params) { - int min=0,max=2,target; + /* XXX complete brainless articial intelligence */ - do - { - target=(rand()%(max-min+1)+min); - }while(persos[target].etat==MORT); - return target; -} + struct team_t *target_team; + struct character_t *target; -int Fgeneratedegats(PERSONNAGES persos[],ENNEMIS ennemis[],int Vtourennemi,int target) -{ - int degats,taux,min,max; + target_team = (params->src->team == params->t1) ? params->t2 : params->t1; - degats=ennemis[Vtourennemi].force*60-persos[target].defense*50; - taux=degats/4; - min=degats-taux; - max=degats+taux; - degats=(rand()%(max-min+1)+min); - if(degats<0) - degats=0; + target = ai_find_random_target(target_team); + + if (target == NULL) { + /* do nothing, everyone is dead */ + return; + } - return degats; + attack(params->surfaces, params->positions, params->src, target, NULL); } diff --git a/jouer.c b/jouer.c index 48be23b..5050a29 100644 --- a/jouer.c +++ b/jouer.c @@ -2,6 +2,7 @@ #include "structures.h" #include "constantes.h" +#include "ai.h" #include "blits.h" #include "priv_entries.h" @@ -237,9 +238,7 @@ static enum action_state_t character_play_turn(struct action_params_t *params) return dig_entry(action_entries_g, countof(action_entries_g), params); } -#if 0 - Factionennemi(&Vtourennemi,surfaces,positions,ennemis,persos,Vnbennemis,&Vtour,Vtourallie); -#endif + ai_play_turn(params); return ACTION_PERFORMED; } diff --git a/prototypes.h b/prototypes.h index 79bb5dd..698bc46 100644 --- a/prototypes.h +++ b/prototypes.h @@ -19,9 +19,6 @@ void Finitialiserpositions (POSITIONS *positions, SURFACES *surfaces); Uint32 obtenirPixel(SDL_Surface *surface, int x, int y); void definirPixel(SDL_Surface *surface, int x, int y, Uint32 pixel); -int Fgeneratedegats(PERSONNAGES persos[],ENNEMIS ennemis[],int Vtourennemi,int target); -int Fchoosetargetallie(PERSONNAGES persos[]); -void Factionennemi(int* Vtourennemi,SURFACES* surfaces,POSITIONS* positions, ENNEMIS ennemis[], PERSONNAGES persos[],int Vnbennemis,int*Vtour); void Fgetmap (int map[][11]); void Fmap (SURFACES*surfaces, POSITIONS* positions, struct team_t *ally, struct team_t *enemy); void Fchargersurfaces_map (SURFACES *surfaces,POSITIONS*positions); -- cgit v1.2.3