diff options
author | scratko <m@scratko.xyz> | 2024-04-14 21:18:36 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2024-04-14 21:18:36 +0300 |
commit | 0cf5dfed3e492608d044a5fc90c1815fab506fd7 (patch) | |
tree | d1efa942e89ab4f064324fcee4c623173abc5298 /ghosts.c | |
parent | 91583d5699503e981105beecc51d37b59dc1842e (diff) | |
download | pacman-0cf5dfed3e492608d044a5fc90c1815fab506fd7.tar.gz pacman-0cf5dfed3e492608d044a5fc90c1815fab506fd7.tar.bz2 pacman-0cf5dfed3e492608d044a5fc90c1815fab506fd7.zip |
Capture and liberation of ghosts
Fixed coordinate in field_has_coin.
Added marks on the field for the liberation zone (1 in front of #).
Prison parameters for the ghost are set.
Fixed condition in BFS search (additionally exclude the current point in the
loop, dx == 0 && dy == 0).
Diffstat (limited to 'ghosts.c')
-rw-r--r-- | ghosts.c | 89 |
1 files changed, 84 insertions, 5 deletions
@@ -19,6 +19,11 @@ void initialize_ghost(struct ghost_type *ghost, enum ghost_color color) ghost->color = red; ghost->frightened_status = 0; ghost->direction = none; + ghost->prison_params.position.x = red_home_x; + ghost->prison_params.position.y = red_home_y; + ghost->prison_params.prison_counter = 0; + ghost->prison_params.active = 0; + ghost->capture_info.status = 0; break; case pink: ghost->position.y = pink_y; @@ -28,6 +33,11 @@ void initialize_ghost(struct ghost_type *ghost, enum ghost_color color) ghost->color = pink; ghost->frightened_status = 0; ghost->direction = none; + ghost->prison_params.position.x = pink_home_x; + ghost->prison_params.position.y = pink_home_y; + ghost->prison_params.prison_counter = 0; + ghost->prison_params.active = 0; + ghost->capture_info.status = 0; break; case blue: ghost->position.y = blue_y; @@ -37,6 +47,11 @@ void initialize_ghost(struct ghost_type *ghost, enum ghost_color color) ghost->color = blue; ghost->frightened_status = 0; ghost->direction = none; + ghost->prison_params.position.x = blue_home_x; + ghost->prison_params.position.y = blue_home_y; + ghost->prison_params.prison_counter = 0; + ghost->prison_params.active = 0; + ghost->capture_info.status = 0; break; case orange: ghost->position.y = orange_y; @@ -46,6 +61,11 @@ void initialize_ghost(struct ghost_type *ghost, enum ghost_color color) ghost->color = orange; ghost->frightened_status = 0; ghost->direction = none; + ghost->prison_params.position.x = orange_home_x; + ghost->prison_params.position.y = orange_home_y; + ghost->prison_params.prison_counter = 0; + ghost->prison_params.active = 0; + ghost->capture_info.status = 0; break; } } @@ -256,7 +276,7 @@ void breadth_first_search(game_space field, struct ghost_type *ghost, for(dx = -1; dx <= 1; ++dx) { int dy; for(dy = -1; dy <= 1; ++dy) { - if(dx != 0 && dy != 0) + 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); @@ -406,14 +426,73 @@ void random_redirect(game_space field, struct ghost_type *ghost) ghost->direction = random_direction; } +static int is_liberation_zone(const struct ghost_type *ghost) +{ + return ghost->position.y == liberation_y && + (ghost->position.x == 14 || ghost->position.x == 15) && + 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) { - red_ghost->direction ^= 1; - pink_ghost->direction ^= 1; - blue_ghost->direction ^= 1; - orange_ghost->direction ^= 1; + 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; +} + +void set_frightened_status(int status, struct ghost_type *red_ghost, + struct ghost_type *pink_ghost, + struct ghost_type *blue_ghost, + struct ghost_type *orange_ghost) +{ + red_ghost->frightened_status = status; + pink_ghost->frightened_status = status; + blue_ghost->frightened_status = status; + orange_ghost->frightened_status = status; +} + +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; } |