summaryrefslogtreecommitdiff
path: root/jouer.c
blob: 52a73d6bbdceb3b7ffed1aa063b22a037aa83502 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include <SDL/SDL.h>
#include "structures.h"
#include "constantes.h"

#include "ai.h"
#include "blits.h"

#include "character.h"

#include "priv_entries.h"

static inline void highlight_current_character(struct team_t *team)
{
    struct character_t *chr = &team->chrs[team->chr_cur];

    chr->surf = chr->red_surf;
}

static inline void unhighlight_prev_character(struct team_t *team)
{
    struct character_t *chr = &team->chrs[team->chr_cur];

    chr->surf = chr->def_surf;
}

static struct character_t *find_next_team_member(const struct character_t *current)
{
    for (int i = current->idx + 1; i < current->team->chr_cnt; ++i) {
        struct character_t *chr = &current->team->chrs[i];

        if (chr->alive)
            return chr;
    }

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

        if (chr->alive)
            return chr;
    }

    return NULL;
}

static struct character_t *find_prev_team_member(const struct character_t *current)
{
    for (int i = current->idx - 1; i >= 0; --i) {
        struct character_t *chr = &current->team->chrs[i];

        if (chr->alive)
            return chr;
    }

    for (int i = current->team->chr_cnt - 1; i >= current->idx; ++i) {
        struct character_t *chr = &current->team->chrs[i];

        if (chr->alive)
            return chr;
    }

    return NULL;
}

static struct character_t *get_first_alive_character(const struct team_t *team)
{
    for (int i = 0; i < team->chr_cnt; ++i) {
        if (team->chrs[i].alive)
            return &team->chrs[i];
    }

    return NULL;
}

/* function called after an action has been performed */
static void update_current_character(struct team_t *t1, struct team_t *t2, struct team_t **playing)
{
    struct team_t *current;
    struct character_t *next;

    unhighlight_prev_character(*playing);

    current = *playing;

    next = find_next_team_member(&current->chrs[current->chr_cur]);

    if (next == NULL) {
        /* if our team is dead */
        current = *playing = (current == t1) ? t2 : t1;

        if (!current->chrs[current->chr_cur].alive) {
            next = find_next_team_member(&current->chrs[current->chr_cur]);

            /* if both team are dead */
            if (next == NULL)
                return;

            current->chr_cur = next->idx;
        }
    } else if (next->idx > current->chr_cur) {
        /* we still have some players to use */
        current->chr_cur = next->idx;
    } else {
        current->chr_cur = next->idx;

        current = *playing = (current == t1) ? t2 : t1;

        if (!current->chrs[current->chr_cur].alive) {
            next = find_next_team_member(&current->chrs[current->chr_cur]);

            if (next == NULL) {
                current = *playing = (current == t1) ? t2 : t1;
            } else {
                current->chr_cur = next->idx;
            }
        }
    }

    highlight_current_character(*playing);
}

static
struct target_t select_default_target(const struct action_params_t *params,
        enum target_type_t type)
{
    struct team_t *enemy_team;
    struct target_t target;

    if (type & (TARGET_SELF | TARGET_SINGLE_ALLY)) {
        target.is_chr = true;
        target.chr = params->src;

        return target;
    }

    enemy_team = (params->src->team == params->t1) ? params->t2 : params->t1;

    if (type & TARGET_SINGLE_ENEMY) {
        target.chr = get_first_alive_character(enemy_team);

        if (target.chr != NULL) {
            target.is_chr = true;
            return target;
        }
    }

    if (type & TARGET_TEAM_ALLY) {
        target.is_chr = false;
        target.team = params->src->team;
        return target;
    }

    if (type & TARGET_TEAM_ENEMY) {
        target.is_chr = false;
        target.team = enemy_team;
        return target;
    }

    abort();
}

static
enum action_state_t select_target(struct action_params_t *params, const struct action_t *action)
{
    SURFACES *surfaces = params->surfaces;
    POSITIONS *positions = params->positions;
    /* select our own character because he exists no matter what */
    struct target_t target = select_default_target(params, action->target);
    struct character_t *new_selection;
    SDL_Event event;

    update_selected_target(surfaces, positions, &target);
    SDL_Flip(surfaces->Pecran);

    while (SDL_PollEvent(&event));
    for (;;) {
        SDL_WaitEvent(&event);

        if (event.type != SDL_KEYDOWN)
            continue;

        switch (event.key.keysym.sym) {
            case SDLK_ESCAPE:
            case SDLK_a:
                update_selected_target(surfaces, positions, NULL);
                SDL_Flip(surfaces->Pecran);
                return ACTION_CANCELED;
            case SDLK_UP:
            case SDLK_k:
                if (!target.is_chr)
                    continue;

                new_selection = find_prev_team_member(target.chr);

                if (new_selection->idx == target.chr->idx)
                    continue;

                target.chr = new_selection;
                update_selected_target(surfaces, positions, &target);
                SDL_Flip(surfaces->Pecran);
                break;
            case SDLK_DOWN:
            case SDLK_j:
                if (!target.is_chr)
                    continue;

                new_selection = find_next_team_member(target.chr);

                if (new_selection->idx == target.chr->idx)
                    continue;

                target.chr = new_selection;
                update_selected_target(surfaces, positions, &target);
                SDL_Flip(surfaces->Pecran);
                break;
            case SDLK_LEFT:
            case SDLK_h:
            case SDLK_RIGHT:
            case SDLK_l:
                if (target.is_chr) {
                    new_selection = get_first_alive_character((target.chr->team == params->t1) ? params->t2 : params->t1);
                    if (new_selection == NULL)
                        continue;

                    target.chr = new_selection;
                } else {
                    target.team = (target.team == params->t1) ? params->t2 : params->t1;
                }

                update_selected_target(surfaces, positions, &target);
                SDL_Flip(surfaces->Pecran);
                break;
            case SDLK_RETURN:
            case SDLK_f:
                update_selected_target(surfaces, positions, NULL);

                (*action->f)(surfaces, positions, params->src, &target, action->data);

                return ACTION_PERFORMED;
            default:
                break;
        }
    }
}

static
enum action_state_t dig_entry(const struct entry_t *entries, int cnt_entries, struct action_params_t *params)
{
    SDL_Event event;
    const struct entry_t *target;
    int selection = 0;

    update_list_entries(params->surfaces, params->positions, entries, cnt_entries, selection);

    while (SDL_PollEvent(&event));
    for (;;) {
        SDL_WaitEvent(&event);

        if (event.type != SDL_KEYDOWN)
            continue;

        switch (event.key.keysym.sym) {
            case SDLK_a:
            case SDLK_ESCAPE:
                return ACTION_CANCELED;
            case SDLK_k:
            case SDLK_UP:
                selection = (selection > 0) ? selection - 1 : cnt_entries - 1;
                update_list_entries(params->surfaces, params->positions, entries, cnt_entries, selection);
                break;
            case SDLK_j:
            case SDLK_DOWN:
                selection = (selection < cnt_entries - 1) ? selection + 1 : 0;
                update_list_entries(params->surfaces, params->positions, entries, cnt_entries, selection);
                break;
            case SDLK_f:
            case SDLK_RETURN:
                target = &entries[selection];
                if (!target->children_cnt) {
                    enum action_state_t state;

                    update_list_entries(params->surfaces, params->positions, entries, cnt_entries, -1);

                    state = select_target(params, &target->action);

                    if (state == ACTION_PERFORMED)
                        return ACTION_PERFORMED;
                } else  {
                    if (dig_entry(target->children, target->children_cnt, params) == ACTION_PERFORMED)
                        return ACTION_PERFORMED;
                }

                update_list_entries(params->surfaces, params->positions, entries, cnt_entries, selection);
            default:
                break;
        }
    }
}

static enum action_state_t character_play_turn(struct action_params_t *params)
{
    params->src->defensive = false;

    if (!params->src->team->cpu) {
        return dig_entry(action_entries_g, countof(action_entries_g), params);
    }

    ai_play_turn(params);

    return ACTION_PERFORMED;
}

static void hook_post_action(struct action_params_t *params)
{
    struct character_t *chr = params->src;

    if (chr->alive && chr->poisoned) {
        damage_target_hp(params->surfaces, params->positions, chr, chr->max_hp / 4);

        SDL_Flip(params->surfaces->screen);

        SDL_Delay(1000);
    }
}

int Fjouer (SURFACES *surfaces, POSITIONS *positions, struct team_t *t1, struct team_t *t2)
{
    struct team_t *playing_team;
    unsigned int continuer=1;
    int i;

    unsigned int gagne,perdu;

    SDL_BlitSurface(surfaces->background, NULL, surfaces->Pecran, NULL);

    /* compute whether the allies or the enemies should begin */
    playing_team = (rand() % 2) ? t1 : t2;

    highlight_current_character(playing_team);

    blit_team(surfaces, t1);
    blit_team(surfaces, t2);

    SDL_Flip(surfaces->Pecran);

    while (continuer)
    {
        struct action_params_t params = {
            .surfaces = surfaces,
            .positions = positions,

            .t1 = t1,
            .t2 = t2,

            .src = &playing_team->chrs[playing_team->chr_cur],
        };

        switch (character_play_turn(&params)) {
            case ACTION_CANCELED:
                continuer = 0;
                break;
            case ACTION_PERFORMED:

                SDL_Flip(surfaces->Pecran);

                SDL_Delay(1000);

                SDL_BlitSurface(surfaces->Pfondjeu, &positions->Vpositiondegats, surfaces->Pecran, &positions->Vpositiondegats);

                SDL_Flip(surfaces->Pecran);

                hook_post_action(&params);

                update_current_character(t1, t2, &playing_team);
                blit_team(surfaces, t1);
                blit_team(surfaces, t2);
                break;
            default:
                break;
        }

        // on vérifie à présent si on a gagné ou si on a perdu !
        gagne=1;
        perdu=1;
        for (i = 0; i < t2->chr_cnt; i++) {
            if (t2->chrs[i].alive)
                gagne=0;
        }
        for (i = 0; i < t1->chr_cnt; i++) {
            if (t1->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;
}