back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/field.c
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-04-02 17:43:35 +0300
committerscratko <m@scratko.xyz>2024-04-02 17:43:35 +0300
commitbdee2852c13f6b02ec5207ded584839a3118233e (patch)
tree39f1c14be91fb848ce0f94a64532e48a7667e5de /field.c
downloadpacman-bdee2852c13f6b02ec5207ded584839a3118233e.tar.gz
pacman-bdee2852c13f6b02ec5207ded584839a3118233e.tar.bz2
pacman-bdee2852c13f6b02ec5207ded584839a3118233e.zip
Initial commit
Diffstat (limited to 'field.c')
-rw-r--r--field.c163
1 files changed, 163 insertions, 0 deletions
diff --git a/field.c b/field.c
new file mode 100644
index 0000000..df61097
--- /dev/null
+++ b/field.c
@@ -0,0 +1,163 @@
+#include "field.h"
+#include "ghosts.h"
+#include <ncurses.h>
+#include <stdlib.h>
+
+static const char field_sample[field_height][field_width] = {
+ {"////////////////////////////"},
+ {"/1....2.....1//1.....2....1/"},
+ {"/.////./////.//./////.////./"},
+ {"/./ /./ /.//./ /./ /./"},
+ {"/./ /./ /.//./ /./ /./"},
+ {"/.////./////.//./////.////./"},
+ {"/2....3..2..2..2..2..3....2/"},
+ {"/.////.//.////////.//.////./"},
+ {"/.////.//.////////.//.////./"},
+ {"/1....2//1..1//1..1//2....1/"},
+ {"//////.///// // /////.//////"},
+ {" /.///// // /////./ "},
+ {" /.//1 1//./ "},
+ {" /.// ///##/// //./ "},
+ {"//////.// / /2 2//////"},
+ {" 3 2/ / //2 "},
+ {"//////.// //////// //.//////"},
+ {" /.//2 2//./ "},
+ {"//////.// //////// //.//////"},
+ {"/1....3..2..1//1..2..3....1/"},
+ {"/.////./////.//./////.////./"},
+ {"/.////./////.//./////.////./"},
+ {"/1.1//2..2..2..2..2..2//1.1/"},
+ {"///.//.//.////////.//.//.///"},
+ {"///.//.//.////////.//.//.///"},
+ {"/1.2..1//1..1//1..1//1..2.1/"},
+ {"/.//////////.//.//////////./"},
+ {"/1..........2..2..........1/"},
+ {"////////////////////////////"},
+};
+
+static void copy_field(game_space field)
+{
+ int i;
+ for(i = 0; i < field_height; ++i) {
+ int j;
+ for(j = 0; j < field_width; ++j)
+ field[i][j] = field_sample[i][j];
+ }
+}
+
+game_space get_new_field()
+{
+ game_space field = malloc(field_width * field_height);
+ copy_field(field);
+ return field;
+}
+
+static int is_has_point(int i, int j)
+{
+ return !((i == 12 && j == 9) || (i == 12 && j == 18) ||
+ (i == 14 && j == 18) || (i == 15 && j == 9) ||
+ (i == 17 && j == 9) || (i == 17 && j == 18));
+}
+
+void print_field(game_space field)
+{
+ int i;
+ char symbol;
+ for(i = 0; i < field_height; ++i) {
+ int j;
+ for(j = 0; j < field_width; ++j) {
+ symbol = field[i][j];
+ move(i, j);
+ switch(symbol) {
+ case '1':
+ case '2':
+ case '3':
+ if(is_has_point(i, j))
+ addch('.');
+ else
+ addch(' ');
+ break;
+ case '/':
+ addch('/');
+ break;
+ case '.':
+ addch('.');
+ break;
+ case '#':
+ addch('#');
+ break;
+ }
+ refresh();
+ }
+ }
+}
+
+void display_character(int y, int x, int symbol)
+{
+ move(y, x);
+ addch(symbol);
+ refresh();
+}
+
+void display_ghosts_on_field(struct ghost_type *red_ghost,
+ struct ghost_type *pink_ghost,
+ struct ghost_type *blue_ghost,
+ struct ghost_type *orange_ghost)
+{
+ display_character(red_ghost->position.y, red_ghost->position.x, '&');
+ display_character(pink_ghost->position.y, pink_ghost->position.x, '&');
+ display_character(blue_ghost->position.y, blue_ghost->position.x, '&');
+ display_character(orange_ghost->position.y, orange_ghost->position.x, '&');
+}
+
+void clear_symbol(game_space field, int y, int x,
+ enum select_character character)
+{
+ int symbol = field[y][x];
+ move(y, x);
+ if(character == ghost_char) {
+ switch(symbol) {
+ case '#':
+ addch('#');
+ break;
+ case '.':
+ addch('.');
+ break;
+ case ' ':
+ addch(' ');
+ break;
+ }
+ }
+ else if(character == pac_char)
+ addch(' ');
+ refresh();
+}
+
+enum intersection_type get_intersection(const game_space field,
+ struct ghost_type *ghost)
+{
+ int y, x, symbol;
+ y = ghost->position.y;
+ x = ghost->position.x;
+ symbol = field[y][x];
+ switch(symbol) {
+ case one_path:
+ return one_path;
+ case two_paths:
+ return two_paths;
+ case three_paths:
+ return three_paths;
+ default:
+ return direct_path;
+ }
+}
+
+struct free_directions find_free_directions(game_space field, int y, int x)
+{
+ struct free_directions found_paths;
+ found_paths.left = field[y][x-1] != '/' && field[y][x-1] != '#' ? 1 : 0;
+ found_paths.right = field[y][x+1] != '/' && field[y][x+1] != '#' ? 1 : 0;
+ found_paths.up = field[y-1][x] != '/' && field[y-1][x] != '#' ? 1 : 0;
+ found_paths.down = field[y+1][x] != '/' && field[y+1][x] != '#' ? 1 : 0;
+ return found_paths;
+}