From fc074e311232e90d997aa4062c63225380190bd3 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 29 May 2024 16:34:17 +0200 Subject: [PATCH] Add map checking --- Makefile | 2 +- include/so_long.h | 8 ++-- src/check_for_valid_path.c | 54 +++++++++++++++++++++++ src/check_map.c | 88 ++++++++++++++++++++++++++++++++++++++ src/main.c | 4 +- 5 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 src/check_for_valid_path.c create mode 100644 src/check_map.c diff --git a/Makefile b/Makefile index 51de300..6bfaf36 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ LIBS = -Llibft -lft -lm -LMLX42/build -lmlx42 -ldl -lglfw -pthread VPATH := src SRC = main.c init.c loop.c input.c draw.c tilemap.c player_process.c \ - collision.c map_utils.c + collision.c map_utils.c check_map.c check_for_valid_path.c OBJ_DIR := obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/so_long.h b/include/so_long.h index 6f3800d..1090358 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser grid_size.x * map->grid_size.y); + if (!tiles) + return (1); // TODO: Error + ft_strlcpy(tiles, map->tiles, map->grid_size.x * map->grid_size.y + 1); + floodfill(tiles, map->grid_size, map->player_start_tile); + return (check_tiles(tiles, map->grid_size.x * map->grid_size.y)); +} + +static void floodfill(char *tiles, t_ivector size, t_ivector pos) +{ + if (tiles[pos.y * size.x + pos.x] == WALL || tiles[pos.y * size.x + pos.x] == 'X') + return ; + tiles[pos.y * size.x + pos.x] = 'X'; + floodfill(tiles, size, (t_ivector){pos.x - 1, pos.y}); + floodfill(tiles, size, (t_ivector){pos.x + 1, pos.y}); + floodfill(tiles, size, (t_ivector){pos.x, pos.y - 1}); + floodfill(tiles, size, (t_ivector){pos.x, pos.y + 1}); +} + +static int check_tiles(char *tiles, int size) +{ + int i; + + i = 0; + while (i < size) + { + if (tiles[i] != WALL && tiles[i] != 'X') + return (1); + i++; + } + return (0); +} diff --git a/src/check_map.c b/src/check_map.c new file mode 100644 index 0000000..af7a97d --- /dev/null +++ b/src/check_map.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser grid_size.x) + { + if (get_tile(map, i, 0) != WALL || get_tile(map, i, map->grid_size.y + - 1) != WALL) + return (1); + i++; + } + i = 0; + while (i < map->grid_size.y) + { + if (get_tile(map, 0, i) != WALL || get_tile(map, map->grid_size.x - 1, + i) != WALL) + return (1); + i++; + } + return (0); +} + +static int check_exactly_one(t_tilemap *map, char tile) +{ + int i; + int found; + + found = 0; + i = map->grid_size.x * map->grid_size.y - 1; + while (i >= 0) + { + if (map->tiles[i] == tile) + found++; + i--; + } + if (found != 1) + return (1); + return (0); +} + +static int check_collectibles(t_tilemap *map) +{ + int i; + + i = map->grid_size.x * map->grid_size.y - 1; + while (i >= 0) + { + if (map->tiles[i] == COLLECTIBLE) + return (0); + i--; + } + ft_printf("\nFound no collectibles\n"); + return (1); +} diff --git a/src/main.c b/src/main.c index fca5cf0..75b58d2 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: dkaiser