back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/ghosts.c
diff options
context:
space:
mode:
Diffstat (limited to 'ghosts.c')
-rw-r--r--ghosts.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/ghosts.c b/ghosts.c
index 72e4f81..87e2350 100644
--- a/ghosts.c
+++ b/ghosts.c
@@ -157,6 +157,11 @@ struct coordinates identify_blue_target(struct pacman pac,
return result_point;
}
+/*
+ * determining the target point for the pink ghost (4 cells in front of
+ * Pac-Man).
+ * Also involved in determining the midpoint for the blue ghost
+ */
struct coordinates identify_target_in_front(struct pacman pac, int shift)
{
int x, y;
@@ -243,6 +248,10 @@ static int is_bfs_search_for_orange(enum search_flag *current_width,
return *move_count > orange_search_limit;
}
+/*
+ * The search is performed from Pac-Man to the ghost so that at the end
+ * of the algorithm we immediately know the closest point to the ghost.
+ */
void breadth_first_search(game_space field, struct ghost_type *ghost,
struct coordinates target_point)
{
@@ -283,6 +292,10 @@ void breadth_first_search(game_space field, struct ghost_type *ghost,
if(ghost->color == orange) {
if(!is_bfs_search_for_orange(&current_width,
&move_count)) {
+ /*
+ * Instead of two_paths, it could be anything except
+ * direct_path
+ */
redirect(field, two_paths, ghost,
ghost->home_position,
compute_distance_between_points);
@@ -315,10 +328,14 @@ void breadth_first_search(game_space field, struct ghost_type *ghost,
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;
+ if (dx > field_width / 2)
+ dx = field_width - dx;
+
dy = abs(y1 - y2);
- return sqrt(dx*dx + dy*dy);
+ /* Manhattan distance */
+ return dx + dy;
}
static int is_minimal_distance(int min_distance, int tmp_distance,