back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/pac.c
blob: 241609b59594b03b2ed59dfe76711a0d27ee3052 (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
#include "pac.h"
#include "ncurses.h"
#include "queue.h"

void initialize_pac(struct pacman *pac)
{
    pac->lives = max_live;
    pac->coins_eaten = 0;
    pac->position.y = pac_y;
    pac->position.x = pac_x;
    pac->direction = none;
}

static enum movement_direction get_matching_for_directions(game_space field,
                                                           struct pacman *pac,
                                                      enum movement_direction
                                                                    direction)
{
    struct free_directions free_paths =
        find_free_directions(field, pac->position.y, pac->position.x);
    switch(direction) {
    case left:
        return free_paths.left ? left : none;
    case right:
        return free_paths.right ? right : none;
    case up:
        return free_paths.up ? up : none;
    case down:
        return free_paths.down ? down : none;
    default:
        return none;
    }

}

#if 0
void check_remaining_direction(game_space field, struct pacman *pac,
                               enum movement_direction *stored_direction)
{
    enum movement_direction temp_direction;
    struct free_directions free_path =
        find_free_directions(field, pac->position.y, pac->position.x);
    enum movement_direction current_direction =
        stored_direction ? *stored_direction : pac->direction;
    temp_direction = get_correct_path(pac, free_path, current_direction);
    if(temp_direction != none)
        pac->direction = temp_direction;
    else {
        current_direction = pac->direction;
        pac->direction = get_correct_path(pac, free_path, current_direction);
    }
    if(stored_direction)
        *stored_direction = none;
}
#endif

void check_remaining_direction(game_space field, struct pacman *pac,
                            enum movement_direction *stored_direction)
{
    enum movement_direction matching_result;
    if(*stored_direction == none)
        pac->direction =
            get_matching_for_directions(field, pac, pac->direction);
    else {
        matching_result =
            get_matching_for_directions(field, pac, *stored_direction);
        if(matching_result != none)
            pac->direction = matching_result;
        else
            pac->direction =
                get_matching_for_directions(field, pac, pac->direction);
        *stored_direction = none;
   }
}

static void check_remaining_direction_after_pressed_key(game_space field,
                                                        struct pacman *pac)
{
    pac->direction =
        get_matching_for_directions(field, pac, pac->direction);
}

void change_pac_direction(game_space field, struct pacman *pac, int key,
                          enum movement_direction *stored_direction)
{
    enum state { no, yes } is_changed_direction;
    is_changed_direction = no;
    struct free_directions path = find_free_directions(field, pac->position.y,
                                                       pac->position.x);
    switch(key) {
    case KEY_LEFT:
        if(path.left) {
            pac->direction = left;
            is_changed_direction = yes;
        } else
            *stored_direction = left;
        break;
    case KEY_RIGHT:
         if(path.right) {
             pac->direction = right;
             is_changed_direction = yes;
         } else
             *stored_direction = right;
         break;
    case KEY_UP:
         if(path.up) {
             pac->direction = up;
             is_changed_direction = yes;
         } else
             *stored_direction = up;
         break;
    case KEY_DOWN:
         if(path.down) {
             pac->direction = down;
             is_changed_direction = yes;
         } else
             *stored_direction = down;
         break;
    }
    if(is_changed_direction == no)
        check_remaining_direction_after_pressed_key(field, pac);
}

static int is_coin_symbol(int symbol)
{
    return
        symbol == coin || symbol == one_path || symbol == two_paths ||
        symbol == three_paths;
}

static int check_coin(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]) && field_has_coin(x, y) &&
        !queue_consists_point(eaten_coins, position);
}

void make_pac_move(game_space field, struct pacman *pac,
                   struct queue *eaten_coins)
{
    clear_or_revert_symbol(field, pac->position, pac_char, eaten_coins);
    switch(pac->direction) {
    case left:
        --pac->position.x;
        break;
    case right:
        ++pac->position.x;
        break;
    case up:
        --pac->position.y;
        break;
    case down:
        ++pac->position.y;
        break;
    default:
        return;
    }
    if(check_coin(field, pac->position, eaten_coins))
        queue_push(eaten_coins, &pac->position);
}