back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/field.c
blob: fd3160669c0e5e35eae9bbb937b50dab5b856703 (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
407
408
409
410
#include "field.h"
#include "ghosts.h"
#include "queue.h"
#include <ncurses.h>
#include <stdlib.h>
#include "color_palette.h"

enum { frightened_limit = 50 };

static const char field_sample[field_height][field_width] = {
    {"////////////////////////////"},
    {"/1....2.....1//1.....2....1/"},
    {"/.////./////.//./////.////./"},
    {"/*/  /./   /.//./   /./  /*/"},
    {"/./  /./   /.//./   /./  /./"},
    {"/.////./////.//./////.////./"},
    {"/2....3..2..2..2..2..3....2/"},
    {"/.////.//.////////.//.////./"},
    {"/.////.//.////////.//.////./"},
    {"/1....2//1..1//1..1//2....1/"},
    {"//////.///// // /////.//////"},
    {"     /.///// // /////./     "},
    {"     /.//1  y11y  1//./     "},
    {"     /.// ///##/// //./     "},
    {"//////.// /      /2  2//////"},
    {"      3  2/      / //2      "},
    {"//////.// //////// //.//////"},
    {"     /.//2        2//./     "},
    {"//////.// //////// //.//////"},
    {"/1....3..2..1//1..2..3....1/"},
    {"/.////./////.//./////.////./"},
    {"/*////./////.//./////.////*/"},
    {"/1.1//2..2..y. y..2..2//1.1/"},
    {"///.//.//.////////.//.//.///"},
    {"///.//.//.////////.//.//.///"},
    {"/1.2..1//1..1//1..1//1..2.1/"},
    {"/.//////////.//.//////////./"},
    {"/1..........2..2..........1/"},
    {"////////////////////////////"},
    {" C C          Score:    /243"}
};

static void copy_field(game_space field)
{
    int i;
    for(i = 0; i < field_height; ++i) {
        int j;
        for(j = 0; j < field_width; ++j)
            field[i][j] = field_sample[i][j];
    }
}

game_space get_new_field()
{
    game_space field = malloc(field_width * field_height);
    copy_field(field);
    return field;
}

void clear_field(game_space *field)
{
    free(*field);
    *field = NULL;
}

static int field_has_coin(int x, int y)
{
    return !((x == 9 && y == 12)|| (x == 18 && y == 12) ||
             (x == 18 && y == 14) || (x == 9 && y == 15) ||
             (x == 9 && y == 17) || (x == 18 && y == 17) ||
             (x == 13 && y == 12) || (x == 14 && y == 12));
}

static int yellow_block_contains_coin(int x, int y)
{
    return (x == 12 && y == 22) || (x == 15 && y == 22);
}

static int is_coin_symbol(int symbol, int x, int y)
{
    return
        symbol == coin || symbol == energizer || symbol == one_path ||
        symbol == two_paths || symbol == three_paths ||
        (symbol == yellow_block && yellow_block_contains_coin(x, y));
}

int check_coin_for_pac(game_space field, struct coordinates position,
               struct queue *eaten_coins)
{
    int x, y;
    x = position.x;
    y = position.y;
    return
        is_coin_symbol(field[y][x], x, y) && field_has_coin(x, y) &&
        !queue_consists_point(eaten_coins, position);
}

int field_has_energizer(const game_space field, int x, int y)
{
    return field[y][x] == energizer;
}

void print_field(game_space field)
{
    int i;
    char symbol;
    for(i = 0; i < field_height; ++i) {
        int j;
        for(j = 0; j < field_width; ++j) {
            symbol = field[i][j];
            move(i, j);
            if(i == field_height-1) {
                symbol != ' ' ?  paint_stats() : reset_attr();
                addch(symbol);
                continue;
            }
            if(is_coin_symbol(symbol, j, i) && field_has_coin(j, i)) {
                paint_field_element(coin);
                symbol == energizer ? addch('*') : addch('.');
                continue;
            }
            switch(symbol) {
            case one_path:
            case two_paths:
            case yellow_block:
                reset_attr();
                addch(' ');
                break;
            case block:
                paint_field_element(block);
                addch(' ');
                break;
            case door:
                paint_field_element(door);
                addch(' ');
                break;
            case ' ':
                reset_attr();
                addch(' ');
                break;
            }
            if(i == field_height-1) {
                symbol != ' ' ?  paint_stats() : reset_attr();
                addch(symbol);
            }
            refresh();
        }
    }
}

static int is_countdown(int timer, int prison_status)
{
    return (timer <= frightened_limit && timer > frightened_limit-10) &&
        !prison_status;
}

void display_character(int y, int x, int symbol, int prison_status)
{
    move(y, x);
    if((enum select_character) symbol == pac_char) {
        paint_pac();
        addch(symbol);
        refresh();
        return;
    }
    if((enum select_character) symbol != ghost_char)
        is_countdown(symbol, prison_status) ?
            printw("%d", frightened_limit -  symbol) : addch(ghost_char);
    else
        addch(ghost_char);
    refresh();
}

void display_ghosts_on_field(struct ghost_type *red_ghost,
                             struct ghost_type *pink_ghost,
                             struct ghost_type *blue_ghost,
                             struct ghost_type *orange_ghost,
                             int frightened_counter)
{
    paint_ghost(red_ghost->color, frightened_counter,
                red_ghost->prison_params.active);
    display_character(red_ghost->position.y, red_ghost->position.x,
                      frightened_counter ? frightened_counter : ghost_char,
                      red_ghost->prison_params.active);

    paint_ghost(pink_ghost->color, frightened_counter,
                pink_ghost->prison_params.active);
    display_character(pink_ghost->position.y, pink_ghost->position.x,
                      frightened_counter ? frightened_counter : ghost_char,
                      pink_ghost->prison_params.active);

    paint_ghost(blue_ghost->color, frightened_counter,
                blue_ghost->prison_params.active);
    display_character(blue_ghost->position.y, blue_ghost->position.x,
                      frightened_counter ? frightened_counter : ghost_char,
                      blue_ghost->prison_params.active);

    paint_ghost(orange_ghost->color, frightened_counter,
                orange_ghost->prison_params.active);
    display_character(orange_ghost->position.y, orange_ghost->position.x,
                      frightened_counter ? frightened_counter : ghost_char,
                      orange_ghost->prison_params.active);
}

void display_score(int value)
{
    move(field_height-1, 21);
    paint_stats();
    printw("%d", value);
    refresh();
}

void display_ready()
{
    move(17, 11);
    /*
     * the same color as pacman
     */
    paint_pac();
    printw("READY!");
    refresh();
}

void clear_or_revert_symbol(const game_space field, struct coordinates position,
                            enum select_character character,
                            const struct queue *eaten_coins)
{
    int x, y;
    x = position.x;
    y = position.y;
    int symbol = field[y][x];
    move(y, x);
    if(character == ghost_char) {
        if(is_coin_symbol(symbol, x, y) && field_has_coin(x, y) &&
                !queue_consists_point(eaten_coins, position)) {
            if(symbol == energizer) {
                paint_field_element(coin);
                addch('*');
            } else {
                paint_field_element(coin);
                addch('.');
            }
            refresh();
            return;
        }
        switch(symbol) {
        case one_path:
        case two_paths:
        case three_paths:
        case yellow_block:
            reset_attr();
            addch(' ');
            break;
        case coin:
            reset_attr();
            addch(' ');
            break;
        case door:
            paint_field_element(door);
            addch('#');
            break;
        case ' ':
            reset_attr();
            addch(' ');
            break;
        }
    }
    else if(character == pac_char) {
        reset_attr();
        addch(' ');
    }
    refresh();
}

enum intersection_type get_intersection(const game_space field,
                                          struct ghost_type *ghost)
{
    int y, x, symbol;
    y = ghost->position.y;
    x = ghost->position.x;
    symbol = field[y][x];
    switch(symbol) {
    case one_path:
        return one_path;
    case two_paths:
        return two_paths;
    case three_paths:
        return three_paths;
    case yellow_block:
        return yellow_block;
    default:
        return direct_path;
    }
}

/*
 * last conditions x-1 == left_outside_tunnel_x and
 * x+1 == right_outside_tunnel_x are used by only pacman (while
 * cheking remaining direction)
 */
struct free_directions find_free_directions(const game_space field, int x,
                                            int y)
{
    struct free_directions found_paths;
    found_paths.left = (field[y][x-1] != '/' && field[y][x-1] != '#') ||
        x-1 == left_outside_tunnel_x ? 1 : 0;
    found_paths.right = (field[y][x+1] != '/' && field[y][x+1] != '#') ||
        x+1 == right_outside_tunnel_x ? 1 : 0;
    found_paths.up = field[y-1][x] != '/' && field[y-1][x] != '#' ? 1 : 0;
    found_paths.down = field[y+1][x] != '/' && field[y+1][x] != '#' ? 1 : 0;
    return found_paths;
}

int is_obstacle(const game_space field, int x, int y)
{
    int symbol = field[y][x];
    return symbol == door || symbol == block;
}

void change_point_if_outside_tunnel(struct coordinates *point)
{
    if(point->x == left_outside_tunnel_x) {
        point->x = right_outside_tunnel_x - 1;
        return;
    }
    if(point->x == right_outside_tunnel_x)
        point->x = left_outside_tunnel_x + 1;
}

void clear_energizer(game_space field, struct coordinates point)
{
    int x, y;
    x = point.x;
    y = point.y;
    field[y][x] = ' ';
}

void clear_ready()
{
    move(17, 11);
    reset_attr();
    printw("      ");
    refresh();
}

int is_equal_points(struct coordinates first_point, struct coordinates
                    second_point)
{
    int x1, y1, x2, y2;
    x1 = first_point.x;
    y1 = first_point.y;
    x2 = second_point.x;
    y2 = second_point.y;
    return x1 == x2 && y1 == y2;
}

void erase_life(int value)
{
    enum {
        one_life_left  = 1,
        no_lives_left  = 0,
        gap            = 2,
        two_lives_x    = 3
    };
    int x, y;
    y = field_height - 1;
    switch(value) {
    case one_life_left:
        x = two_lives_x;
        break;
    case no_lives_left:
        x = two_lives_x - gap;
    }
    move(y, x);
    reset_attr();
    addch(' ');
    refresh();
}

void display_hit(struct coordinates pac_position,
                 const struct ghost_type *red_ghost,
                 const struct ghost_type *pink_ghost,
                 const struct ghost_type *blue_ghost,
                 const struct ghost_type *orange_ghost)
{
    struct coordinates ghost_position;
    if(red_ghost->reached_pacman)
        ghost_position = red_ghost->position;
    else if(pink_ghost->reached_pacman)
        ghost_position = pink_ghost->position;
    else if(blue_ghost->reached_pacman)
        ghost_position = blue_ghost->position;
    else
        ghost_position = orange_ghost->position;
    reset_attr();
    move(ghost_position.y, ghost_position.x);
    addch(' ');
    paint_hit();
    move(pac_position.y, pac_position.x);
    addch('X');
    refresh();
}

void erase_hit(struct coordinates point)
{
    move(point.y, point.x);
    reset_attr();
    addch(' ');
    refresh();
}