summaryrefslogtreecommitdiff
path: root/jouer.c
blob: 802642aae4c68b3fb8adb32f9bf16b7ab7fed861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include "structures.h"
#include "constantes.h"
#include "prototypes.h"

static inline void highlight_current_character(struct team_t *ally, int character_idx)
{
    struct character_t *chr = &ally->chrs[character_idx];

    chr->surf = chr->red_surf;
}

static inline void unhighlight_prev_character(struct team_t *ally, int character_idx)
{
    struct character_t *chr = &ally->chrs[character_idx];

    chr->surf = chr->def_surf;
}

static void generate_enemy_types(struct team_t *ally_team, ENNEMIS ennemis[], int Vnbennemis)
{
    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 <= Vnbennemis; i++) {
            ennemis[i].classe=GUERRIER_GOBELIN;
        }
    } else {
        /* XXX should not happen for now since we do not change the levels */
        abort();
    }
}

static int find_next_ally(const struct team_t *ally, int current)
{
    for (int i = current + 1; i < ally->chr_cnt; ++i) {
        const struct character_t *chr = &ally->chrs[i];

        if (chr->alive)
            return i;
    }

    for (int i = 0; i <= current; ++i) {
        const struct character_t *chr = &ally->chrs[i];

        if (chr->alive)
            return i;
    }

    return -1;
}

/* function called after an action has been performed */
static void update_current_character(struct team_t *ally, int *ally_idx, bool *ally_turn)
{
    if (*ally_turn) {
        int next;

        unhighlight_prev_character(ally, *ally_idx);

        next = find_next_ally(ally, *ally_idx);

        /* if there is no next ally or they are dead */
        if (next <= *ally_idx) {
            inverse_boolean(*ally_turn);
        }

        *ally_idx = next;

        if (*ally_turn) {
            highlight_current_character(ally, *ally_idx);
        }
    }
}

int Fjouer (SURFACES *surfaces, POSITIONS *positions, struct team_t *ally,ENNEMIS ennemis[])
{
    unsigned int continuer=1;
    int selection=0;
    int i;
    SDL_Event event;
    int Vtourallie=0;
    int Vtourennemi=0;
    int Vtour;
    int Vnbennemis=0;
    int page=0;
    int nbactions=5;

    unsigned int gagne,perdu;
    if(surfaces->Pfondjeu!=NULL)
    {
        SDL_FreeSurface(surfaces->Pfondjeu);
        surfaces->Pfondjeu=NULL;
    }
    Fblitterfond(surfaces); // on blit le fond du jeu
    Fremplirobjets(&ally->objects); // on monte les variables potions ether, etc
    Vnbennemis=Fcalculernbennemis(); // on tire aléatoirement le nombre d'ennemis présents dans le combat
    generate_enemy_types(ally, ennemis,Vnbennemis); // on choisit la classe des ennemis comme guerrier gobelin etc
    Fremplirennemis(surfaces,Vnbennemis,ennemis);

    /* compute whether the allies or the enemies should begin */
    Vtour = rand() % 2;

    if (Vtour == ALLIE) {
        /* the current character will be highlighted in red */
        /* TODO should be generic for an enemy or an ally */
        highlight_current_character(ally, Vtourallie);
    }

    blit_ally_team(surfaces,positions, ally); // on blit les persos sur l'ecran

    Fblitterennemis(surfaces,positions,ennemis,Vnbennemis); // idem pour les ennemis

    while (continuer)
    {
        if(Vtour==ALLIE) //si un player joue
        {
            while (!ally->chrs[Vtourallie].alive) //si le perso selectionné est mort
            {
                if(Vtourallie<2) // si ce n'est pas le dernier
                    Vtourallie++; // on prend le perso suivant
                else // sinon si c'est le dernier
                    Vtourallie=0; // on reprend le 1er
            }
            Fchangeractionselectionnee(surfaces,positions,selection,page,nbactions,ACTIONS,NULL);
            SDL_WaitEvent(&event);
            switch(event.type)
            {
                case SDL_KEYDOWN:
                switch(event.key.keysym.unicode)
                {
                    case SDLK_ESCAPE:
                    case SDLK_a:
                    continuer=0;
                    break;
                }
                switch (event.key.keysym.sym)
                {
                    case SDLK_UP:
                    case SDLK_k:
                    if (selection!=0)
                        selection--;
                    else
                        selection=nbactions-1;
                    page=selection/3;
                    Fchangeractionselectionnee(surfaces,positions,selection,page,nbactions,ACTIONS,NULL);
                    break;
                    case SDLK_DOWN:
                    case SDLK_j:
                    if (selection!=nbactions-1)
                        selection++;
                    else
                        selection=0;
                    page=selection/3;
                    Fchangeractionselectionnee(surfaces,positions,selection,page,nbactions,ACTIONS,NULL);
                    break;
                    case SDLK_RETURN:
                    case SDLK_f:
                    {
                        enum action_state_t (*actionp)(SURFACES *, POSITIONS *, struct team_t *ally, int, ENNEMIS[], int) = NULL;

                        switch (selection) {
                            case ATTAQUE: actionp = Fattaquer; break;
                            case MAGIE_BLANCHE: actionp = Fselectionnermagieblanche; break;
                            case MAGIE_NOIRE: actionp = Fselectionnermagienoire; break;
                            case TECHNIQUES: break;
                            case OBJETS: actionp = Fselectionnerobjet; break;
                            default: abort(); break;
                        }

                        if (actionp && (*actionp)(surfaces,positions, ally, Vtourallie,ennemis,Vnbennemis) == ACTION_PERFORMED) {
                            update_current_character(ally, &Vtourallie, (bool *)&Vtour);
                            blit_ally_team(surfaces, positions, ally);
                        }
                    }
                    break;
                    default:
                    break;
                }
                break;
            }
        }
        else if(Vtour==ENNEMI) // sinon si c'est le cpu qui joue
        {
            while (ennemis[Vtourennemi].etat == MORT) {
                if (Vtourennemi < Vnbennemis) {
                    Vtourennemi++;
                } else if (Vtourennemi == Vnbennemis) {
                    Vtourennemi = 0;
                }
            }
/* TODO reactivate */
#if 0
            Factionennemi(&Vtourennemi,surfaces,positions,ennemis,persos,Vnbennemis,&Vtour,Vtourallie);
#endif
        } // les actions sont faites


        // on vérifie à présent si on a gagné ou si on a perdu !
        gagne=1;
        perdu=1;
        for(i=0;i<=Vnbennemis;i++)
        {
            if(ennemis[i].etat==VIE)
                gagne=0;
        }
        for (i = 0; i < ally->chr_cnt; i++) {
            if (ally->chrs[i].alive)
                perdu=0;
        }

        // la y'a des trucs a mettre pour si on gagne ou on perd parce que sa fait assez pitié :p
        if(perdu)
        {
            SDL_Delay(2000);
            continuer=0;
        }
        else if(gagne)
        {
            SDL_Delay(2000);
            continuer=0;
        }
    }
    return 0;
}

void Finitialiserpositionsennemis(SURFACES *surfaces,POSITIONS *positions,int Vnbennemis)
{
    if (Vnbennemis==0)
    {
        positions->Vpositionennemis[0].x=XWIN-20-surfaces->Tennemi[0]->w;
        positions->Vpositionennemis[0].y=YWIN/2-surfaces->Tennemi[0]->h/2;
    }
    if (Vnbennemis==1)
    {
        positions->Vpositionennemis[0].x = XWIN - 20 - surfaces->Tennemi[0]->w;
        positions->Vpositionennemis[0].y = YWIN/3 - surfaces->Tennemi[0]->h/2;
        positions->Vpositionennemis[1].x = XWIN - 20 - surfaces->Tennemi[2]->w;
        positions->Vpositionennemis[1].y = YWIN/3*2 - surfaces->Tennemi[1]->h/2;
    }
    else if (Vnbennemis==2)
    {
        positions->Vpositionennemis[1].x = XWIN - 20 - surfaces->Tennemi[1]->w;
        positions->Vpositionennemis[1].y = YWIN/2 - surfaces->Tennemi[1]->h/2;
        positions->Vpositionennemis[0].x = XWIN - 20 - surfaces->Tennemi[0]->w;
        positions->Vpositionennemis[0].y = YWIN/4 - surfaces->Tennemi[1]->h/2;
        positions->Vpositionennemis[2].x = XWIN - 20 - surfaces->Tennemi[2]->w;
        positions->Vpositionennemis[2].y = YWIN/4*3 - surfaces->Tennemi[2]->h/2;
    }
    else if (Vnbennemis==3)
    {
        positions->Vpositionennemis[1].x = XWIN - 20 - surfaces->Tennemi[1]->w;
        positions->Vpositionennemis[1].y = YWIN/5*2 - surfaces->Tennemi[1]->h/2;
        positions->Vpositionennemis[2].x = XWIN - 20 - surfaces->Tennemi[2]->w;
        positions->Vpositionennemis[2].y = YWIN/5*3 - surfaces->Tennemi[2]->h/2;
        positions->Vpositionennemis[0].x = XWIN - surfaces->Tennemi[1]->w - surfaces->Tennemi[0]->w/2;
        positions->Vpositionennemis[0].y = YWIN/5 -surfaces->Tennemi[0]->h/2;
        positions->Vpositionennemis[3].x = XWIN - surfaces->Tennemi[2]->w - surfaces->Tennemi[3]->w/2;
        positions->Vpositionennemis[3].y = YWIN/5*4 - surfaces->Tennemi[3]->h/2;
    }
    else if (Vnbennemis==4)
    {
        positions->Vpositionennemis[2].x = XWIN - 20 - surfaces->Tennemi[2]->w;
        positions->Vpositionennemis[2].y = YWIN/2 - surfaces->Tennemi[2]->h/2;
        positions->Vpositionennemis[1].x = XWIN - surfaces->Tennemi[2]->w - surfaces->Tennemi[1]->w/2;
        positions->Vpositionennemis[1].y = YWIN/6*2 - surfaces->Tennemi[1]->h/2;
        positions->Vpositionennemis[3].x = XWIN - surfaces->Tennemi[2]->w - surfaces->Tennemi[3]->w/2;
        positions->Vpositionennemis[3].y = YWIN/6*4 - surfaces->Tennemi[3]->h/2;
        positions->Vpositionennemis[0].x = XWIN - surfaces->Tennemi[2]->w - surfaces->Tennemi[1]->w - surfaces->Tennemi[0]->w/2;
        positions->Vpositionennemis[0].y = YWIN/6 - surfaces->Tennemi[0]->h/2;
        positions->Vpositionennemis[4].x = XWIN - surfaces->Tennemi[2]->w - surfaces->Tennemi[3]->w - surfaces->Tennemi[4]->w/2;
        positions->Vpositionennemis[4].y = YWIN/6*5 - surfaces->Tennemi[4]->h/2;
    }
}

void Fremplirobjets(OBJET *objets)
{
    objets->potions=10;
    objets->ethers=10;
    objets->potionsplus=5;
    objets->ethersplus=5;
}