diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2015-01-09 17:40:02 +0100 |
---|---|---|
committer | Olivier Gayot <duskcoder@gmail.com> | 2015-01-09 17:42:32 +0100 |
commit | dfe9604dc9e4292c9f0e420d3a926e8268178308 (patch) | |
tree | e72126bed8102a0852fe8f45fe3b980bdbf41a13 | |
parent | 2c26f3577680ae1ee1989de0f866d4fec912f49d (diff) |
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 <duskcoder@gmail.com>
-rw-r--r-- | ai.h | 8 | ||||
-rw-r--r-- | ia.c | 81 | ||||
-rw-r--r-- | jouer.c | 5 | ||||
-rw-r--r-- | prototypes.h | 3 |
4 files changed, 44 insertions, 53 deletions
@@ -0,0 +1,8 @@ +#ifndef AI_H +#define AI_H + +#include "entry.h" + +void ai_play_turn(struct action_params_t *); + +#endif /* AI_H */ @@ -6,62 +6,49 @@ #include "prototypes.h" #include <time.h> -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(*Vtourennemi<Vnbennemis) - (*Vtourennemi)++; - else if(*Vtourennemi==Vnbennemis) - { - *Vtourennemi=0; - inverse_boolean(*Vtour); + ++alive; } - }while(ennemis[*Vtourennemi].etat==MORT); - if(persos[target].pv<=0) - { - persos[target].pv=0; - persos[target].etat=MORT; - positions->Vpositionmort.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); } @@ -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); |