diff options
Diffstat (limited to 'map_verify.c')
-rw-r--r-- | map_verify.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/map_verify.c b/map_verify.c new file mode 100644 index 0000000..a3ccc5a --- /dev/null +++ b/map_verify.c @@ -0,0 +1,130 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_verify.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/10 08:14:30 by kdx #+# #+# */ +/* Updated: 2022/10/16 00:14:35 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "map.h" + +static t_err only_chars(const unsigned char *data) +{ + int accepted[256]; + size_t i; + + i = 0; + while (i < 256) + accepted[i++] = 0; + accepted[TILE_NONE] = 1; + accepted[TILE_WALL] = 1; + accepted[TILE_COLLECTIBLE] = 1; + accepted[TILE_EXIT] = 1; + accepted[TILE_PLAYER] = 1; + accepted['\n'] = 1; + i = 0; + while (data[i] != '\0') + if (!accepted[data[i++]]) + return (1); + return (0); +} + +static t_err find_uniques(const unsigned char *data) +{ + int uniques[256]; + size_t i; + size_t to_be_found; + + i = 0; + while (i < 256) + uniques[i++] = 0; + uniques['P'] = 1; + uniques['E'] = 1; + uniques['C'] = 5; + to_be_found = 3; + i = 0; + while (data[i] != '\0') + { + if (uniques[data[i]] && uniques[data[i]] != 7) + { + if (uniques[data[i]] == 3) + return (ERR_DUPLICATE); + if ((uniques[data[i]] & 2) == 0) + to_be_found--; + uniques[data[i]] |= 2; + } + i++; + } + return (ERR_MISSING + (to_be_found == 0) * (ERR_NONE - ERR_MISSING)); +} + +static t_err sensible_dimensions(t_map *map, const unsigned char *data) +{ + size_t prev_newline; + + if (ft_strchr((const char *)data, '\n') == NULL) + return (1); + prev_newline = 0; + map->width = 0; + map->height = 0; + while (*data != '\0') + { + if (*data++ == '\n') + { + if (map->width == 0) + map->width = prev_newline; + if (prev_newline == 0 || map->width != prev_newline) + return (1); + prev_newline = 0; + map->height++; + } + else + prev_newline++; + } + if (prev_newline != 0 || map->width == 0 || map->height == 0) + return (1); + return (0); +} + +static t_err valid_borders(t_map *map) +{ + size_t x; + size_t y; + + x = 0; + while (x < map->width) + { + if (map->data[x] != TILE_WALL + || map_get(map, x * TSIZE, (map->height - 1) * TSIZE) != TILE_WALL) + return (ERR_OPEN_BORDER); + x += 1; + } + y = 0; + while (y < map->height) + { + if (map_get(map, 0, y * TSIZE) != TILE_WALL + || map_get(map, (map->width - 1) * TSIZE, y * TSIZE) != TILE_WALL) + return (ERR_OPEN_BORDER); + y += 1; + } + return (ERR_NONE); +} + +t_err map_verify(t_map *map, const unsigned char *data) +{ + if (only_chars(data)) + return (ERR_INVALID_CHAR); + if (find_uniques(data)) + return (find_uniques(data)); + if (sensible_dimensions(map, data)) + return (ERR_WRONG_DIMENSION); + if (valid_borders(map)) + return (ERR_OPEN_BORDER); + if (!map_pathfind(map)) + return (ERR_NO_PATH); + return (ERR_NONE); +} |