back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/ghosts.c
blob: 72e4f81f32f63b7d50ae3ed3a19d1002db43ad8a (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include "ghosts.h"
#include "field.h"
#include "pac.h"
#include "queue.h"
#include <ncurses.h>
#include <math.h>
#include <stdlib.h>

enum search_flag { viewed, not_viewed };

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->prison_params.position.x = red_prison_x;
        ghost->prison_params.position.y = red_prison_y;
        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->prison_params.position.x = pink_prison_x;
        ghost->prison_params.position.y = pink_prison_y;
        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->prison_params.position.x = blue_prison_x;
        ghost->prison_params.position.y = blue_prison_y;
        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->prison_params.position.x = orange_prison_x;
        ghost->prison_params.position.y = orange_prison_y;
        break;
    }
        ghost->direction = none;
        ghost->prison_params.prison_counter = 0;
        ghost->prison_params.active = 0;
        ghost->capture_info.status = 0;
        ghost->reached_pacman = 0;
}

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 { prefinal_stage = 2, final_stage = 1 };
    enum movement_direction red_moves[] = { none, none, right, up, up, left };
    enum movement_direction blue_moves[] = { none, none, left, up, up, left };
    enum movement_direction pink_moves[] = { up, right, up, up, up, right };
    enum movement_direction orange_moves[] = { none, left, up, up, up, right };
    int index = *get_out_stage - 1;
    if(*get_out_stage > prefinal_stage) {
        red_ghost->direction = red_moves[index];
        blue_ghost->direction = blue_moves[index];
    }
    if(*get_out_stage > final_stage)
        orange_ghost->direction = orange_moves[index];
    pink_ghost->direction = pink_moves[index];
    --*get_out_stage;
}

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

void clear_ghost_positions(game_space field, const struct queue *eaten_coins,
                           const struct ghost_type *red_ghost,
                           const struct ghost_type *pink_ghost,
                           const struct ghost_type *blue_ghost,
                           const struct ghost_type *orange_ghost)
{
    clear_or_revert_symbol(field, red_ghost->position, ghost_char, eaten_coins);
    clear_or_revert_symbol(field, pink_ghost->position, ghost_char,
                           eaten_coins);
    clear_or_revert_symbol(field, blue_ghost->position, ghost_char,
                           eaten_coins);
    clear_or_revert_symbol(field, orange_ghost->position, ghost_char,
                           eaten_coins);
}

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,
                      struct queue *eaten_coins)
{
    clear_ghost_positions(field, eaten_coins, red_ghost, pink_ghost, blue_ghost,
                          orange_ghost);
    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.x,
                                                       ghost->position.y);
    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;
}

struct coordinates identify_blue_target(struct pacman pac,
                                        const struct ghost_type *red_ghost)
{
    struct coordinates difference, middle_point, result_point;
    middle_point = identify_target_in_front(pac, blue_shift);
    difference.x = middle_point.x - red_ghost->position.x;
    difference.y = middle_point.y - red_ghost->position.y;
    result_point.x = middle_point.x + difference.x;
    result_point.y = middle_point.y + difference.y;
    return result_point;
}

struct coordinates identify_target_in_front(struct pacman pac, int shift)
{
    int x, y;
    struct coordinates target;
    x = pac.position.x;
    y = pac.position.y;
    switch(pac.direction) {
    case left:
        x -= shift;
        break;
    case right:
        x += shift;
        break;
    case up:
        y -= shift;
        break;
    case down:
        y += shift;
        break;
    default:
        ;
    }
    target.x = x;
    target.y = y;
    return target;
}

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

static struct coordinates get_near_point(struct coordinates consider_point,
                                         int dx, int dy)
{
    struct coordinates found_point;
    found_point.x = consider_point.x + dx;
    found_point.y = consider_point.y + dy;
    return found_point;
}

static enum movement_direction
    find_direction_by_near_point(struct coordinates found_point,
                                 struct coordinates ghost_position)
{
    int dx, dy;
    enum movement_direction new_direction;
    dx = found_point.x - ghost_position.x;
    dy = found_point.y - ghost_position.y;
    switch(dx) {
    case 1:
        new_direction = dy == 0 ? right : none;
        break;
    case 0:
        new_direction = dy == -1 ? up : down;
        break;
    case -1:
        new_direction = dy == 0 ? left : none;
        break;
    }
    return new_direction;
}

static void clear_both_queues(struct queue *next_points,
                              struct queue *reviewed_points)
{
    queue_clear(next_points);
    queue_clear(reviewed_points);
}

static int is_bfs_search_for_orange(enum search_flag *current_width,
                                    int *move_count)
{
    if(*current_width == not_viewed) {
        ++*move_count;
        *current_width = viewed;
    }
    return *move_count > orange_search_limit;
}

void breadth_first_search(game_space field, struct ghost_type *ghost,
                          struct coordinates target_point)
{
    int move_count;
    enum search_flag current_width = not_viewed;
    enum movement_direction reverse_direction;
    struct coordinates consider_point, near_point;
    struct coordinates start_point = { ghost->position.y, ghost->position.x };
    struct queue next_points, reviewed_points;
    move_count = 0;
    reverse_direction = ghost->direction;
    reverse_direction ^= 1;
    queue_init(&next_points);
    queue_init(&reviewed_points);
    queue_push(&next_points, &target_point);
    queue_push(&reviewed_points, &target_point);
    while(!empty(&next_points)) {
        if(ghost->position.x == target_point.x &&
                ghost->position.y == target_point.y) {
            ghost->direction=none;
            break;
        }
        consider_point = queue_front(&next_points);
        pop(&next_points);
        int dx;
        for(dx = -1; dx <= 1; ++dx) {
            int dy;
            for(dy = -1; dy <= 1; ++dy) {
                if((dx != 0 && dy != 0) || (dx == 0 && dy == 0))
                    continue;
                near_point = get_near_point(consider_point, dx, dy);
                change_point_if_outside_tunnel(&near_point);
                if(is_obstacle(field, near_point.x, near_point.y))
                    continue;
                if(queue_consists_point(&reviewed_points, near_point))
                    continue;
                if(equal_points(start_point, near_point)) {
                    if(ghost->color == orange) {
                        if(!is_bfs_search_for_orange(&current_width,
                                                     &move_count)) {
                            redirect(field, two_paths, ghost,
                                     ghost->home_position,
                                     compute_distance_between_points);
                            clear_both_queues(&next_points, &reviewed_points);
                            return;
                        }
                    }
                    ghost->direction =
                        find_direction_by_near_point(consider_point,
                                                     start_point);
                    if(ghost->direction == reverse_direction) {
                        queue_push(&reviewed_points, &consider_point);
                        continue;
                    }
                    clear_both_queues(&next_points, &reviewed_points);
                    return;
                }
                if(ghost->color == orange && current_width == not_viewed) {
                    current_width = viewed;
                    ++move_count;
                }
                queue_push(&next_points, &near_point);
                queue_push(&reviewed_points, &near_point);
            }
        }
        current_width = not_viewed;
    }
}

static int standard_distance(int x1, int y1, int x2, int y2)
{
    int dx, dy;
    dx = abs(x1 - x2);
    dx = dx > field_width ? field_width - dx : dx;
    dy = abs(y1 - y2);
    return sqrt(dx*dx + dy*dy);
 }

static int is_minimal_distance(int min_distance, int tmp_distance,
                               enum movement_direction cur_direction)
{
    return min_distance == 0 || tmp_distance < min_distance ||
           (tmp_distance == min_distance && cur_direction != right);
}

static void set_final_direction(int *min_distance,
                                enum movement_direction cur_direction,
                                enum movement_direction *final_direction,
                                struct coordinates initial_point,
                                struct coordinates target_point)
{
    int x1, y1, x2, y2;
    int tmp_distance;
    x1 = initial_point.x;
    y1 = initial_point.y;
    x2 = target_point.x;
    y2 = target_point.y;
    tmp_distance = standard_distance(x1, y1, x2, y2);
    if(is_minimal_distance(*min_distance, tmp_distance, cur_direction)) {
        *min_distance = tmp_distance;
        *final_direction = cur_direction;
    }
}

void compute_distance_between_points(game_space field,
                                     struct ghost_type *ghost,
                                     struct coordinates target_point)
{
    enum movement_direction final_direction, ghost_direction;
    struct coordinates initial_point, changed_initial_point;
    int min_distance;
    final_direction = none;
    ghost_direction = ghost->direction;
    initial_point = ghost->position;
    min_distance = 0;
    struct free_directions consider_paths =
        find_free_directions(field, initial_point.x, initial_point.y);
    if(consider_paths.right && ghost_direction != left) {
        changed_initial_point.x = initial_point.x + 1;
        changed_initial_point.y = initial_point.y;
        set_final_direction(&min_distance, right, &final_direction,
                            changed_initial_point, target_point);
    }
    if(consider_paths.down && ghost_direction != up) {
        changed_initial_point.x = initial_point.x;
        changed_initial_point.y = initial_point.y + 1;
        set_final_direction(&min_distance, down, &final_direction,
                            changed_initial_point, target_point);
    }
    if(consider_paths.left && ghost_direction != right) {
        changed_initial_point.x = initial_point.x - 1;
        changed_initial_point.y = initial_point.y;
        set_final_direction(&min_distance, left, &final_direction,
                            changed_initial_point, target_point);
    }
    if(consider_paths.up && ghost_direction != down) {
        changed_initial_point.x = initial_point.x;
        changed_initial_point.y = initial_point.y - 1;
        set_final_direction(&min_distance, up, &final_direction,
                            changed_initial_point, target_point);
    }
    ghost->direction = final_direction;
}

static enum movement_direction get_random_direction()
{
    return 0 + (int)(4.0*rand()/(RAND_MAX + 1.0));
}

static int is_equal_directions(enum movement_direction random_direction,
                               struct free_directions paths)
{
    switch(random_direction) {
    case left:
        return paths.left;
    case right:
        return paths.right;
    case up:
        return paths.up;
    case down:
        return paths.down;
    default:
        ;
    }
    return 0;
}

void random_redirect(game_space field, struct ghost_type *ghost)
{
    enum movement_direction random_direction;
    struct free_directions paths =
        find_free_directions(field, ghost->position.x, ghost->position.y);
    random_direction = get_random_direction();
    while(!is_equal_directions(random_direction, paths))
        random_direction = get_random_direction();
    ghost->direction = random_direction;
}

static int is_liberation_zone(const struct ghost_type *ghost)
{
    return  ghost->position.y == liberation_y &&
        (ghost->position.x == 13 || ghost->position.x == 14) &&
        ghost->direction == up;
}

void reverse_all_ghosts(struct ghost_type *red_ghost,
                        struct ghost_type *pink_ghost,
                        struct ghost_type *blue_ghost,
                        struct ghost_type *orange_ghost)
{
    if(!red_ghost->prison_params.active)
        red_ghost->direction = is_liberation_zone(red_ghost) ?
            left : red_ghost->direction ^ 1;
    if(!pink_ghost->prison_params.active)
        pink_ghost->direction = is_liberation_zone(pink_ghost) ?
            left : pink_ghost->direction ^ 1;
    if(!blue_ghost->prison_params.active)
        blue_ghost->direction = is_liberation_zone(blue_ghost) ?
            left : blue_ghost->direction ^ 1;
    if(!orange_ghost->prison_params.active)
        orange_ghost->direction = is_liberation_zone(orange_ghost) ?
            left : orange_ghost->direction ^ 1;
}

int is_pac_nearby(struct pacman pac, struct ghost_type ghost)
{
    if(ghost.capture_info.status)
        return 0;
    struct coordinates near_point;
    int dx;
    for(dx = -1; dx <= 1; ++dx) {
        int dy;
        for(dy = -1; dy <= 1; ++dy) {
            if((dx != 0 && dy != 0) || (dx == 0 && dy == 0))
                continue;
            near_point = get_near_point(ghost.position, dx, dy);
            if(equal_points(near_point, pac.position))
                return 1;
        }
    }
    return 0;
}

void set_capture_info(struct ghost_type *ghost, const struct pacman *pac)
{
    ghost->capture_info.previous_pac_position = pac->position;
    ghost->capture_info.previous_ghost_position = ghost->position;
    ghost->capture_info.status = 1;
}

void catch_ghost(const game_space field, struct ghost_type *ghost)
{
    ghost->prison_params.active = 1;
    ghost->direction = none;
    ghost->position.x = ghost->prison_params.position.x;
    ghost->position.y = ghost->prison_params.position.y;
}