From 04a6703fd66a7d34b2556a9c203c4dada3baca38 Mon Sep 17 00:00:00 2001 From: scratko Date: Fri, 12 Apr 2024 03:17:46 +0300 Subject: Added behavior modes Reverse direction Random direction --- ghosts.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 16 deletions(-) (limited to 'ghosts.c') diff --git a/ghosts.c b/ghosts.c index fe1a51f..ded74e7 100644 --- a/ghosts.c +++ b/ghosts.c @@ -17,7 +17,7 @@ void initialize_ghost(struct ghost_type *ghost, enum ghost_color color) ghost->home_position.y = red_home_y; ghost->home_position.x = red_home_x; ghost->color = red; - ghost->mode = scatter; + ghost->frightened_status = 0; ghost->direction = none; break; case pink: @@ -26,7 +26,7 @@ void initialize_ghost(struct ghost_type *ghost, enum ghost_color color) ghost->home_position.y = pink_home_y; ghost->home_position.x = pink_home_x; ghost->color = pink; - ghost->mode = scatter; + ghost->frightened_status = 0; ghost->direction = none; break; case blue: @@ -35,7 +35,7 @@ void initialize_ghost(struct ghost_type *ghost, enum ghost_color color) ghost->home_position.y = blue_home_y; ghost->home_position.x = blue_home_x; ghost->color = blue; - ghost->mode = scatter; + ghost->frightened_status = 0; ghost->direction = none; break; case orange: @@ -44,7 +44,7 @@ void initialize_ghost(struct ghost_type *ghost, enum ghost_color color) ghost->home_position.y = orange_home_y; ghost->home_position.x = orange_home_x; ghost->color = orange; - ghost->mode = scatter; + ghost->frightened_status = 0; ghost->direction = none; break; } @@ -56,18 +56,19 @@ void pull_out_ghosts(int *get_out_stage, 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 }; + 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 > final_stage) { + 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]; - orange_ghost->direction = orange_moves[index]; --*get_out_stage; } @@ -188,8 +189,9 @@ static struct coordinates get_near_point(struct coordinates consider_point, return found_point; } -static enum movement_direction find_direction(struct coordinates found_point, - struct coordinates ghost_position) +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; @@ -274,13 +276,12 @@ void breadth_first_search(game_space field, struct ghost_type *ghost, } } ghost->direction = - find_direction(consider_point, start_point); -#if 1 + find_direction_by_near_point(consider_point, + start_point); if(ghost->direction == reverse_direction) { queue_push(&reviewed_points, &consider_point); continue; } -#endif clear_both_queues(&next_points, &reviewed_points); return; } @@ -370,3 +371,49 @@ void compute_distance_between_points(game_space field, } 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.y, ghost->position.x); + random_direction = get_random_direction(); + while(!is_equal_directions(random_direction, paths)) + random_direction = get_random_direction(); + ghost->direction = random_direction; +} + + +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; +} -- cgit v1.2.3