back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/ghosts.c
blob: 2559740ad89fc89f13824d157668ae22513abc67 (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
#include "ghosts.h"
#include "field.h"

void initialize_ghost(struct ghost_type *ghost, enum ghost_color color)
{
    switch(color) {
        case red:
            ghost->position.y = red_y;
            ghost->position.x = red_x;
            ghost->home_position.y = red_home_y;
            ghost->home_position.x = red_home_x;
            ghost->color = red;
            ghost->mode = scatter;
            ghost->direction = none;
            break;
        case pink:
            ghost->position.y = pink_y;
            ghost->position.x = pink_x;
            ghost->home_position.y = pink_home_y;
            ghost->home_position.x = pink_home_x;
            ghost->color = pink;
            ghost->mode = scatter;
            ghost->direction = none;
            break;
        case blue:
            ghost->position.y = blue_y;
            ghost->position.x = blue_x;
            ghost->home_position.y = blue_home_y;
            ghost->home_position.x = blue_home_x;
            ghost->color = blue;
            ghost->mode = scatter;
            ghost->direction = none;
            break;
        case orange:
            ghost->position.y = orange_y;
            ghost->position.x = orange_x;
            ghost->home_position.y = orange_home_y;
            ghost->home_position.x = orange_home_x;
            ghost->color = orange;
            ghost->mode = scatter;
            ghost->direction = none;
            break;
    }
}

void pull_out_ghosts(int *get_out_stage,
                     struct ghost_type *red_ghost, 
                     struct ghost_type *pink_ghost,
                     struct ghost_type *blue_ghost, 
                     struct ghost_type *orange_ghost)
{
    enum { final_stage = 1 };
    enum movement_direction red_moves[] = { none, right, up, up, left };
    enum movement_direction blue_moves[] = { none, left, up, up, left };
    enum movement_direction pink_moves[] = { right, up, up, up, right };
    enum movement_direction orange_moves[] = { left, up, up, up, right };
    int index = *get_out_stage - 1;
    if(*get_out_stage > final_stage) {
        red_ghost->direction = red_moves[index];
        blue_ghost->direction = blue_moves[index];
    }
    pink_ghost->direction = pink_moves[index];
    orange_ghost->direction = orange_moves[index];
    --*get_out_stage;
}

static void change_position(struct ghost_type *ghost, 
                            enum movement_direction direction)
{
    switch(direction) {
        case left:
            --ghost->position.x;
            return;
        case right:
            ++ghost->position.x;
            return;
        case up:
            --ghost->position.y;
            return;
        case down:
            ++ghost->position.y;
            return;
        default:
            return;
    }
}

void make_ghost_moves(game_space field,
                      struct ghost_type *red_ghost,
                      struct ghost_type *pink_ghost,
                      struct ghost_type *blue_ghost,
                      struct ghost_type *orange_ghost)
{
    clear_symbol(field, red_ghost->position.y, red_ghost->position.x, 
                 ghost_char);
    clear_symbol(field, pink_ghost->position.y, pink_ghost->position.x, 
                 ghost_char);
    clear_symbol(field, blue_ghost->position.y, blue_ghost->position.x, 
                 ghost_char);
    clear_symbol(field, orange_ghost->position.y, orange_ghost->position.x, 
                 ghost_char);
    change_position(red_ghost, red_ghost->direction);
    change_position(pink_ghost, pink_ghost->direction);
    change_position(blue_ghost, blue_ghost->direction);
    change_position(orange_ghost, orange_ghost->direction);
}

static void pave_only_way(game_space field, struct ghost_type *ghost)
{
   struct free_directions path = find_free_directions(field, ghost->position.y,
                                                      ghost->position.x);
   enum movement_direction reverse_direction = ghost->direction;
   reverse_direction ^= 1;
   if(path.left && left != reverse_direction)
       ghost->direction = left;
   else if(path.right && right != reverse_direction)
       ghost->direction = right;
   else if(path.up && up != reverse_direction)
       ghost->direction = up;
   else if(path.down && down != reverse_direction)
       ghost->direction = down;
}

void redirect(game_space field, enum intersection_type paths, 
              struct ghost_type *ghost, 
              void (*pathfinder)(game_space field, enum intersection_type paths,
                                 struct ghost_type *ghost))
{
    if(paths == one_path)
        pave_only_way(field, ghost);
    else if(paths == two_paths || paths == three_paths)
        pathfinder(field, paths, ghost);
}


void breadth_first_search(game_space field, enum intersection_type paths,
                          struct ghost_type *ghost)
{

}