#include #include #include #include "structures.h" #include "constantes.h" #include "prototypes.h" #include #include #include "players.h" static void init_team_players(struct team_t *team, bool left, const enum character_class_t *classes, SURFACES *surfaces) { for (int i = 0; i < team->chr_cnt; i++) { struct character_t *chr = &team->chrs[i]; chr->name = "Player"; chr->team = team; chr->cpu = team->cpu; chr->class_ = classes[i]; chr->alive = true; switch (chr->class_) { case CLASS_PALADIN: chr->def_surf = surfaces->Ppaladin; chr->red_surf = surfaces->red_paladin; break; case CLASS_PRIEST: chr->def_surf = surfaces->Ppretre; chr->red_surf = surfaces->red_priest; break; case CLASS_THIEF: chr->def_surf = surfaces->Pvoleur; chr->red_surf = surfaces->red_thief; break; case CLASS_WARRIOR_GOBELIN: chr->def_surf = surfaces->Pgobelin; chr->red_surf = surfaces->red_warrior_gobelin; break; default: abort(); } chr->surf = chr->def_surf; chr->hp = base_stats_g[chr->class_].hp; chr->max_hp = base_stats_g[chr->class_].hp; chr->mp = base_stats_g[chr->class_].mp; chr->max_mp = base_stats_g[chr->class_].mp; chr->magic = base_stats_g[chr->class_].magic; chr->strength = base_stats_g[chr->class_].strength; chr->defense = base_stats_g[chr->class_].defense; chr->spirit = base_stats_g[chr->class_].spirit; for (int j = 0; j < countof(chr->affinities); ++j) { chr->affinities[j] = rand() % AFFINITY_COUNT; } /* set its position */ chr->pos.x = (left) ? 20 : XWIN - chr->surf->w - 20; chr->pos.y = (YWIN / ((team->chr_cnt + 1) * 2)) * (2 * (i + 1)) - chr->surf->h / 2; } } static struct team_t *new_ally_team(SURFACES *surfaces, const enum character_class_t *classes, int cnt) { struct team_t *team = malloc(sizeof(struct team_t)); team->name = "Team Player"; team->cpu = false; team->chr_cnt = cnt; team->chr_cur = rand() % team->chr_cnt; team->chrs = malloc(sizeof(struct character_t) * team->chr_cnt); init_team_players(team, true, classes, surfaces); /* TODO remove when the level is set */ for (int i = 0; i < team->chr_cnt; ++i) { team->chrs[i].ally.nv = 0; } return team; } static void generate_enemy_types(const struct team_t *ally_team, enum character_class_t *classes, int count) { int avg = 0; for (int i = 0; i < ally_team->chr_cnt; i++) { avg += ally_team->chrs[i].ally.nv; } avg /= ally_team->chr_cnt; if (avg < 5) { /* easy */ for (int i = 0; i <= count; i++) { classes[i] = CLASS_WARRIOR_GOBELIN; } } else { /* XXX should not happen for now since we do not change the levels */ abort(); } } static struct team_t *new_enemy_team(SURFACES *surfaces, const struct team_t *ally_team) { struct team_t *team = malloc(sizeof(struct team_t)); enum character_class_t *classes; team->name = "Team CPU"; team->cpu = true; team->chr_cnt = rand() % 5 + 1; team->chr_cur = rand() % team->chr_cnt; team->chrs = malloc(sizeof(struct character_t) * team->chr_cnt); classes = malloc(sizeof(enum character_class_t) * team->chr_cnt); generate_enemy_types(ally_team, classes, team->chr_cnt); init_team_players(team, false, classes, surfaces); free(classes); return team; } void Fmenuchoixpersos (SURFACES *surfaces, POSITIONS *positions) { TTF_Font *police = NULL; int Vchoix = PALADIN; SDL_Color vert; bool continuer = true; struct team_t *ally_team; struct team_t *enemy_team; enum character_class_t chr_classes[3]; SDL_Event event; int Vnbperso = 0; vert.r = 39; vert.g = 189; vert.b = 31; police = TTF_OpenFont ("TIMESI.TTF",36); /* loop until we have actually chosen our three characters */ while (Vnbperso < 3) { static const char *strings[] = { "Please choose the class of your first character", "Please choose the class of your second character", "Please choose the class of your third character", }; /* display the background with the selected text */ if (Vchoix == PALADIN) { SDL_BlitSurface (surfaces->Pchoixpaladin,NULL,surfaces->Pecran,&positions->Vpositionmenu); } else if (Vchoix == PRETRE) { SDL_BlitSurface (surfaces->Pchoixpretre,NULL,surfaces->Pecran,&positions->Vpositionmenu); } else if (Vchoix == VOLEUR) { SDL_BlitSurface (surfaces->Pchoixvoleur,NULL,surfaces->Pecran,&positions->Vpositionmenu); } else { abort(); } /* display the string associated with the current character number (aka Please choose ...) */ surfaces->Ptextechoixmenu = TTF_RenderText_Blended (police, strings[Vnbperso], vert); positions->Vpositiontextemenu.x=XWIN/2-surfaces->Ptextechoixmenu->w/2; SDL_BlitSurface (surfaces->Ptextechoixmenu,NULL,surfaces->Pecran,&positions->Vpositiontextemenu); SDL_BlitSurface (surfaces->Ppretre,NULL,surfaces->Pecran,&positions->Vpositionmenupretre); SDL_BlitSurface (surfaces->Ppaladin,NULL,surfaces->Pecran,&positions->Vpositionmenupaladin); SDL_BlitSurface (surfaces->Pvoleur,NULL,surfaces->Pecran,&positions->Vpositionmenuvoleur); SDL_Flip (surfaces->Pecran); continuer = true; while (continuer) { SDL_WaitEvent (&event); switch (event.type) { case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_LEFT: case SDLK_h: if (Vchoix > PALADIN) { Vchoix--; } else { Vchoix=VOLEUR; } Fchangersurlignage2 (Vchoix,surfaces,positions); break; case SDLK_RIGHT: case SDLK_l: if (Vchoix < VOLEUR) { Vchoix++; } else { Vchoix = PALADIN; } Fchangersurlignage2 (Vchoix,surfaces,positions); break; case SDLK_RETURN: case SDLK_f: chr_classes[Vnbperso++] = Vchoix; continuer = false; break; default: break; } break; } } } TTF_CloseFont (police); SDL_Flip(surfaces->Pecran); ally_team = new_ally_team(surfaces, chr_classes, 3); enemy_team = new_enemy_team(surfaces, ally_team); Fmap(surfaces,positions, ally_team, enemy_team); } void Fchangersurlignage2 (int Vchoix, SURFACES *surfaces, POSITIONS *positions) { if (Vchoix==PALADIN) SDL_BlitSurface (surfaces->Pchoixpaladin,NULL,surfaces->Pecran,&positions->Vpositionmenu); else if (Vchoix==PRETRE) SDL_BlitSurface (surfaces->Pchoixpretre,NULL,surfaces->Pecran,&positions->Vpositionmenu); else if (Vchoix==VOLEUR) SDL_BlitSurface (surfaces->Pchoixvoleur,NULL,surfaces->Pecran,&positions->Vpositionmenu); SDL_BlitSurface (surfaces->Ppretre,NULL,surfaces->Pecran,&positions->Vpositionmenupretre); SDL_BlitSurface (surfaces->Ppaladin,NULL,surfaces->Pecran,&positions->Vpositionmenupaladin); SDL_BlitSurface (surfaces->Pvoleur,NULL,surfaces->Pecran,&positions->Vpositionmenuvoleur); SDL_BlitSurface (surfaces->Ptextechoixmenu,NULL,surfaces->Pecran,&positions->Vpositiontextemenu); SDL_Flip (surfaces->Pecran); }