back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/pacman.c
diff options
context:
space:
mode:
Diffstat (limited to 'pacman.c')
-rw-r--r--pacman.c275
1 files changed, 168 insertions, 107 deletions
diff --git a/pacman.c b/pacman.c
index 1fb296c..679fcc2 100644
--- a/pacman.c
+++ b/pacman.c
@@ -28,39 +28,46 @@ struct mode_type {
int reverse_direction;
};
-static void change_mode(struct mode_type *mode_params)
+struct game_params_type {
+ int exit;
+ int pause;
+};
+
+static void initialize_params(struct game_params_type *basic_params)
{
- if(!(mode_params->current_mode == frightened) &&
- mode_params->phase_number == phase_limit)
- return;
- switch(mode_params->current_mode) {
- case chase:
- if(mode_params->chase_count > chase_move_limit) {
- mode_params->current_mode = scatter;
- mode_params->chase_count = 0;
- ++mode_params->phase_number;
- if(mode_params->phase_number == phase_limit)
- mode_params->current_mode = chase;
- if(mode_params->phase_number != phase_limit)
- ++mode_params->reverse_direction;
- }
- break;
- case scatter:
- if(mode_params->scatter_count > scatter_move_limit) {
- mode_params->current_mode = chase;
- mode_params->scatter_count = 0;
- ++mode_params->reverse_direction;
- }
- break;
- case frightened:
- if(mode_params->frightened_count > frightened_move_limit) {
- mode_params->current_mode =
- mode_params->chase_count || mode_params->phase_number ==
- phase_limit ? chase : scatter;
- mode_params->frightened_count = 0;
- ++mode_params->reverse_direction;
- }
- }
+ srand(time(NULL));
+ initscr();
+ start_color();
+ cbreak();
+ noecho();
+ curs_set(0);
+ keypad(stdscr, 1);
+ timeout(timeout_duration);
+ start_color();
+ basic_params->exit = 0;
+ basic_params->pause = 0;
+}
+
+static void initialize_ghosts(struct ghost_type *red_ghost,
+ struct ghost_type *pink_ghost,
+ struct ghost_type *blue_ghost,
+ struct ghost_type *orange_ghost)
+{
+ initialize_ghost(red_ghost, red);
+ initialize_ghost(pink_ghost, pink);
+ initialize_ghost(blue_ghost, blue);
+ initialize_ghost(orange_ghost, orange);
+}
+
+static void initialize_modes(struct mode_type *mode_params, int *get_out_stage)
+{
+ mode_params->current_mode = scatter;
+ mode_params->chase_count = 0;
+ mode_params->scatter_count = 0;
+ mode_params->frightened_count = 0;
+ mode_params->phase_number = 0;
+ mode_params->reverse_direction = 0;
+ *get_out_stage = count_get_out_moves;
}
static int is_up_move_blocked(const struct ghost_type *ghost,
@@ -168,6 +175,84 @@ static void random_pathfinder_stage(game_space field,
}
}
+static void change_mode(struct mode_type *mode_params)
+{
+ if(!(mode_params->current_mode == frightened) &&
+ mode_params->phase_number == phase_limit)
+ return;
+ switch(mode_params->current_mode) {
+ case chase:
+ if(mode_params->chase_count > chase_move_limit) {
+ mode_params->current_mode = scatter;
+ mode_params->chase_count = 0;
+ ++mode_params->phase_number;
+ if(mode_params->phase_number == phase_limit)
+ mode_params->current_mode = chase;
+ if(mode_params->phase_number != phase_limit)
+ ++mode_params->reverse_direction;
+ }
+ break;
+ case scatter:
+ if(mode_params->scatter_count > scatter_move_limit) {
+ mode_params->current_mode = chase;
+ mode_params->scatter_count = 0;
+ ++mode_params->reverse_direction;
+ }
+ break;
+ case frightened:
+ if(mode_params->frightened_count > frightened_move_limit) {
+ mode_params->current_mode =
+ mode_params->chase_count || mode_params->phase_number ==
+ phase_limit ? chase : scatter;
+ mode_params->frightened_count = 0;
+ ++mode_params->reverse_direction;
+ }
+ }
+}
+
+static void chase_mode(game_space field, struct mode_type *mode_params,
+ const struct pacman *pac,
+ struct ghost_type *red_ghost,
+ struct ghost_type *pink_ghost,
+ struct ghost_type *blue_ghost,
+ struct ghost_type *orange_ghost)
+{
+ pathfinder_stage(field, mode_params, pac, red_ghost, pink_ghost, blue_ghost,
+ orange_ghost);
+ if(mode_params->phase_number < phase_limit) {
+ ++mode_params->chase_count;
+ change_mode(mode_params);
+ }
+}
+
+static void scatter_mode(game_space field, struct mode_type *mode_params,
+ struct ghost_type *red_ghost,
+ struct ghost_type *pink_ghost,
+ struct ghost_type *blue_ghost,
+ struct ghost_type *orange_ghost)
+{
+ pathfinder_stage(field, mode_params, NULL, red_ghost, pink_ghost,
+ blue_ghost, orange_ghost);
+ if(mode_params->phase_number < phase_limit) {
+ ++mode_params->scatter_count;
+ change_mode(mode_params);
+ }
+}
+
+static void frightened_mode(game_space field, struct mode_type *mode_params,
+ struct ghost_type *red_ghost,
+ struct ghost_type *pink_ghost,
+ struct ghost_type *blue_ghost,
+ struct ghost_type *orange_ghost)
+{
+ random_pathfinder_stage(field, red_ghost, pink_ghost, blue_ghost,
+ orange_ghost);
+ ++mode_params->frightened_count;
+ change_mode(mode_params);
+ set_frightened_status(mode_params->current_mode == frightened ? 1 : 0,
+ red_ghost, pink_ghost, blue_ghost, orange_ghost);
+}
+
static int is_outside_prison(struct ghost_type ghost)
{
return ghost.prison_params.active && ghost.direction == up;
@@ -224,8 +309,8 @@ static int is_castling(struct pacman pac, struct ghost_type ghost)
ghost.position.y == ghost.capture_info.previous_pac_position.y;
}
-static void caughting_stage(game_space field,
- const struct mode_type *mode_params,
+static void catching_stage(game_space field,
+ struct mode_type *mode_params, int *get_out_stage,
struct pacman *pac, struct ghost_type *red_ghost,
struct ghost_type *pink_ghost,
struct ghost_type *blue_ghost,
@@ -269,89 +354,53 @@ static void caughting_stage(game_space field,
is_castling(*pac, *current_ghost)) {
if(mode_params->current_mode == frightened)
catch_ghost(field, current_ghost);
- else
- --pac->lives;
+ else {
+ catch_pac(pac);
+ if(pac->lives)
+ initialize_ghosts(red_ghost, pink_ghost, blue_ghost,
+ orange_ghost);
+ initialize_modes(mode_params, get_out_stage);
+ usleep(sleep_duration);
+ return;
+ }
}
- current_ghost->capture_info.status = 0;
+ if(current_ghost->capture_info.status)
+ current_ghost->capture_info.status = 0;
}
}
-
-static void chase_mode(game_space field, struct mode_type *mode_params,
- const struct pacman *pac,
- struct ghost_type *red_ghost,
- struct ghost_type *pink_ghost,
- struct ghost_type *blue_ghost,
- struct ghost_type *orange_ghost)
+static void show_defeat_screen()
{
- pathfinder_stage(field, mode_params, pac, red_ghost, pink_ghost, blue_ghost,
- orange_ghost);
- if(mode_params->phase_number < phase_limit) {
- ++mode_params->chase_count;
- change_mode(mode_params);
- }
+ move(17, 9);
+ printw("GAME OVER");
+ refresh();
}
-static void scatter_mode(game_space field, struct mode_type *mode_params,
- struct ghost_type *red_ghost,
+static void restart_game(struct game_params_type *basic_params,
+ game_space field, struct queue *eaten_coins,
+ struct pacman *pac, struct ghost_type *red_ghost,
struct ghost_type *pink_ghost,
struct ghost_type *blue_ghost,
struct ghost_type *orange_ghost)
{
- pathfinder_stage(field, mode_params, NULL, red_ghost, pink_ghost,
- blue_ghost, orange_ghost);
- if(mode_params->phase_number < phase_limit) {
- ++mode_params->scatter_count;
- change_mode(mode_params);
- }
-}
-
-static void frightened_mode(game_space field, struct mode_type *mode_params,
- struct ghost_type *red_ghost,
- struct ghost_type *pink_ghost,
- struct ghost_type *blue_ghost,
- struct ghost_type *orange_ghost)
-{
- random_pathfinder_stage(field, red_ghost, pink_ghost, blue_ghost,
- orange_ghost);
- ++mode_params->frightened_count;
- change_mode(mode_params);
- set_frightened_status(mode_params->current_mode == frightened ? 1 : 0,
- red_ghost, pink_ghost, blue_ghost, orange_ghost);
-}
-
-static void initialize_ghosts(struct ghost_type *red_ghost,
- struct ghost_type *pink_ghost,
- struct ghost_type *blue_ghost,
- struct ghost_type *orange_ghost)
-{
- initialize_ghost(red_ghost, red);
- initialize_ghost(pink_ghost, pink);
- initialize_ghost(blue_ghost, blue);
- initialize_ghost(orange_ghost, orange);
-}
-
-static void initialize_modes(struct mode_type *mode_params, int *get_out_stage)
-{
- mode_params->current_mode = scatter;
- mode_params->chase_count = 0;
- mode_params->scatter_count = 0;
- mode_params->frightened_count = 0;
- mode_params->phase_number = 0;
- mode_params->reverse_direction = 0;
- *get_out_stage = count_get_out_moves;
+ initialize_params(basic_params);
+ initialize_pac(pac);
+ initialize_ghosts(red_ghost, pink_ghost, blue_ghost, orange_ghost);
+ clear_field(field);
+ field = get_new_field();
+ print_field(field);
+ queue_clear(eaten_coins);
+ queue_init(eaten_coins);
}
-static void initialize_params()
+static void defeat_stage(struct game_params_type *basic_params)
{
- srand(time(NULL));
- initscr();
- start_color();
- cbreak();
- noecho();
- curs_set(0);
- keypad(stdscr, 1);
- timeout(timeout_duration);
- start_color();
+ int key;
+ show_defeat_screen();
+ timeout(-1);
+ while((key = getch()) != 'y' && key != 'n')
+ {}
+ if(key == 'y')
+ basic_params->exit = 0;
}
int main()
@@ -361,10 +410,11 @@ int main()
struct pacman pac;
struct queue eaten_coins;
struct mode_type mode_params;
+ struct game_params_type basic_params;
int key, get_out_stage;
enum movement_direction stored_direction;
stored_direction = none;
- initialize_params();
+ initialize_params(&basic_params);
field = get_new_field();
print_field(field);
initialize_ghosts(&red_ghost, &pink_ghost, &blue_ghost, &orange_ghost);
@@ -415,8 +465,8 @@ int main()
/*
* displaying characters
*/
- caughting_stage(field, &mode_params, &pac, &red_ghost, &pink_ghost,
- &blue_ghost, &orange_ghost);
+ catching_stage(field, &mode_params, &get_out_stage, &pac,
+ &red_ghost, &pink_ghost, &blue_ghost, &orange_ghost);
display_ghosts_on_field(&red_ghost, &pink_ghost, &blue_ghost,
&orange_ghost);
display_character(pac.position.y, pac.position.x, pac_char);
@@ -432,8 +482,19 @@ int main()
printw("SCATTER %d ", mode_params.scatter_count);
else if(mode_params.current_mode == frightened)
printw("FRIGHTENED %d ", mode_params.frightened_count);
+ move(1, 50);
+ printw("LIVES: %d", pac.lives);
refresh();
#endif
+ if(!pac.lives) {
+ defeat_stage(&basic_params);
+ if(basic_params.exit)
+ return 0;
+ else
+ restart_game(&basic_params, field, &eaten_coins, &pac,
+ &red_ghost, &pink_ghost, &blue_ghost,
+ &orange_ghost);
+ }
}
queue_clear(&eaten_coins);
endwin();