diff options
author | scratko <m@scratko.xyz> | 2025-07-31 03:55:59 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2025-07-31 04:07:30 +0300 |
commit | 8fe2319e189b4d830a892b8c686d08cff5556962 (patch) | |
tree | 60479d39949e0a13efce32d75e5fec1886eae09f /ghosts.c | |
parent | cba2e3ddd2596f519750aed1179da8a757023850 (diff) | |
download | pacman-8fe2319e189b4d830a892b8c686d08cff5556962.tar.gz pacman-8fe2319e189b4d830a892b8c686d08cff5556962.tar.bz2 pacman-8fe2319e189b4d830a892b8c686d08cff5556962.zip |
Added many inline comments for clarity.
Introduced initial version of README with gameplay details, controls, and AI logic.
Replaced Euclidean distance with Manhattan distance in standard_distance() to better
match grid-based movement without diagonal traversal.
Diffstat (limited to 'ghosts.c')
-rw-r--r-- | ghosts.c | 21 |
1 files changed, 19 insertions, 2 deletions
@@ -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(¤t_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, |