diff --git a/map/map.c b/map/map.c index b10e036..788d4c9 100644 --- a/map/map.c +++ b/map/map.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/16 14:50:15 by cchauvet #+# #+# */ +/* Updated: 2023/05/16 14:50:16 by cchauvet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "map.h" #include "./parsing_private.h" diff --git a/map/map.h b/map/map.h index f826c29..9a0a49d 100644 --- a/map/map.h +++ b/map/map.h @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/16 14:50:41 by cchauvet #+# #+# */ +/* Updated: 2023/05/16 14:50:42 by cchauvet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef MAP_H # define MAP_H # include @@ -10,8 +22,8 @@ typedef struct s_map char *img_path[4]; long long color_bot; long long color_top; - int size_x; - int size_y; + int size_x; + int size_y; double ply_x; double ply_y; char direction; diff --git a/map/parsing.c b/map/parsing.c index a5e0af6..b17bb24 100644 --- a/map/parsing.c +++ b/map/parsing.c @@ -1,9 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/16 14:53:00 by cchauvet #+# #+# */ +/* Updated: 2023/05/16 14:54:02 by cchauvet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "./parsing_private.h" #include "map.h" #include #include -static ssize_t get_nb_line(const char *path) +static ssize_t get_nb_line(const char *path) { int fd; char readed[1]; @@ -93,8 +105,6 @@ int map_parsing(const char *path, t_map *map) } header_freer(header); map->map = get_body((const char **) file_content, header_size); - ft_freer_tab_ultimate(1, file_content); - if (body_is_valid(map) == 0) - return (1); - return (0); + ft_freer_tab_ultimate(1, file_content); + return (body_is_valid(map) == 0); } diff --git a/map/parsing.h b/map/parsing.h index 20d7528..7c322ae 100644 --- a/map/parsing.h +++ b/map/parsing.h @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/16 14:54:09 by cchauvet #+# #+# */ +/* Updated: 2023/05/16 14:54:10 by cchauvet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef PARSING_H # define PARSING_H # include "map.h" diff --git a/map/parsing_body.c b/map/parsing_body.c index 4977dc3..75fa174 100644 --- a/map/parsing_body.c +++ b/map/parsing_body.c @@ -1,10 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_body.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/16 14:51:13 by cchauvet #+# #+# */ +/* Updated: 2023/05/16 16:34:23 by cchauvet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "parsing_private.h" char **get_body(const char **file_content, size_t header_size) { char **body; - int i; - int len; + int i; + int len; len = 0; while (file_content[header_size + len] != NULL) @@ -55,124 +67,3 @@ int get_spawn_position(const char **body, double *spawn_x, double *spawn_y) return (2); return (0); } - -static int map_is_in_one_part(const char **body) -{ - int y; - - y = ft_tablen((const void **) body) - 1; - while (body[y][0] == '\0' || ft_contain_only(body[y], ' ')) - y--; - while (y > 0) - { - if (body[y][0] == '\0' || ft_contain_only(body[y], ' ')) - return (1); - y--; - } - return (0); -} - -static int map_surround(const char **body) -{ - int y; - int x; - - y = 0; - while (body[y] != NULL) - { - x = 0; - while (body[y][x] != '\0') - { - if (body[y][x] == '0') - { - if (body[y + 1] == NULL || (y == 0) - || (body[y][x + 1] == '\0') || (x == 0) - || (body[y + 1][x] == ' ' || body[y + 1][x] == '\0') - || (body[y - 1][x] == ' ' || body[y - 1][x] == '\0') - || (body[y][x - 1] == ' ' || body[y][x - 1] == '\0') - || (body[y][x + 1] == ' ' || body[y][x + 1] == '\0')) - return (1); - } - x++; - } - y++; - } - return (0); -} - -static int body_contain_normal_char(const char **body) -{ - unsigned int y; - unsigned int x; - - y = 0; - while (body[y] != NULL) - { - x = 0; - while (body[y][x] != '\0') - { - if (!ft_is_in(" 01NEWS", body[y][x])) - { - ft_eprintf("map: %c: invalid character at rows=%u, colums=%u", body[y][x], y, x); - return (0); - } - x++; - } - y++; - } - return (1); -} - -void get_size(const char **body, t_map *map) -{ - int y; - int x; - - map->size_x = 0; - y = 0; - while (body[y] != NULL) - { - x = ft_strlen(body[y]); - if (x > map->size_x) - map->size_x = x; - y++; - } - map->size_y = y; -} - -int body_is_valid(t_map *map) -{ - int error; - - if (map->map == NULL) - return (0); - error = get_spawn_position((const char **) map->map, &map->ply_x, &map->ply_y); - if (error == 1) - { - ft_eprintf("map: spawn position: multiple definition"); - return (0); - } - else if (error == 2) - { - ft_eprintf("map: spawn position: not defined"); - return (0); - } - map->direction = map->map[(int) map->ply_y][(int) map->ply_x]; - map->map[(int) map->ply_y][(int) map->ply_x] = '0'; - error = map_is_in_one_part((const char **)map->map); - if (error) - { - ft_eprintf("map: splitted"); - return (0); - } - if (body_contain_normal_char((const char **)map->map) == 0) - return (0); - error = map_surround((const char **)map->map); - if (error) - { - ft_eprintf("map: not surrounded"); - return (0); - } - get_size((const char **)map->map, map); - return (1); -} diff --git a/map/parsing_body2.c b/map/parsing_body2.c new file mode 100644 index 0000000..8a8c0d7 --- /dev/null +++ b/map/parsing_body2.c @@ -0,0 +1,145 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_body2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet 0) + { + if (body[y][0] == '\0' || ft_contain_only(body[y], ' ')) + return (1); + y--; + } + return (0); +} + +static int map_surround(const char **body) +{ + int y; + int x; + + y = 0; + while (body[y] != NULL) + { + x = 0; + while (body[y][x] != '\0') + { + if (body[y][x] == '0') + { + if (body[y + 1] == NULL || (y == 0) + || (body[y][x + 1] == '\0') || (x == 0) + || (body[y + 1][x] == ' ' || body[y + 1][x] == '\0') + || (body[y - 1][x] == ' ' || body[y - 1][x] == '\0') + || (body[y][x - 1] == ' ' || body[y][x - 1] == '\0') + || (body[y][x + 1] == ' ' || body[y][x + 1] == '\0')) + return (1); + } + x++; + } + y++; + } + return (0); +} + +static int body_contain_normal_char(const char **body) +{ + unsigned int y; + unsigned int x; + + y = 0; + while (body[y] != NULL) + { + x = 0; + while (body[y][x] != '\0') + { + if (!ft_is_in(" 01NEWS", body[y][x])) + { + ft_eprintf("map: %c: invalid character at rows=%u, colums=%u", + body[y][x], y, x); + return (0); + } + x++; + } + y++; + } + return (1); +} + +void get_size(const char **body, t_map *map) +{ + int y; + int x; + + map->size_x = 0; + y = 0; + while (body[y] != NULL) + { + x = ft_strlen(body[y]); + if (x > map->size_x) + map->size_x = x; + y++; + } + map->size_y = y; +} + +int body_is_valid2(t_map *map) +{ + int error; + + error = get_spawn_position((const char **) map->map, + &map->ply_x, &map->ply_y); + if (error == 1) + { + ft_eprintf("map: spawn position: multiple definition"); + return (0); + } + else if (error == 2) + { + ft_eprintf("map: spawn position: not defined"); + return (0); + } + map->direction = map->map[(int) map->ply_y][(int) map->ply_x]; + map->map[(int) map->ply_y][(int) map->ply_x] = '0'; + error = map_is_in_one_part((const char **)map->map); + if (error) + { + ft_eprintf("map: splitted"); + return (0); + } + if (body_contain_normal_char((const char **)map->map) == 0) + return (0); + return (1); +} + +int body_is_valid(t_map *map) +{ + int error; + + if (map->map == NULL) + return (0); + if (body_is_valid2(map) == 0) + return (0); + error = map_surround((const char **)map->map); + if (error) + { + ft_eprintf("map: not surrounded"); + return (0); + } + get_size((const char **)map->map, map); + return (1); +} diff --git a/map/parsing_header.c b/map/parsing_header.c index 3989c94..c88a06b 100644 --- a/map/parsing_header.c +++ b/map/parsing_header.c @@ -1,4 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_header.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet int get_token(const char *key) { @@ -52,7 +65,7 @@ static size_t get_header_size(const char **file_content, size_t *header_size) while (file_content[++i] != NULL) { if (ft_contain_only(file_content[i], ' ')) - continue; + continue ; if (!is_header(file_content[i])) break ; len++; @@ -66,16 +79,16 @@ char ***get_header(const char **file_content, size_t *header_size) char ***header; size_t len; size_t i; - size_t y; + ssize_t y; len = get_header_size(file_content, header_size); header = malloc(sizeof(char *) * (len + 1)); if (header == NULL) return (NULL); header[len] = NULL; - y = 0; + y = -1; i = 0; - while (y < len) + while (++y < (ssize_t) len) { while (ft_contain_only(file_content[i], ' ')) i++; @@ -85,7 +98,6 @@ char ***get_header(const char **file_content, size_t *header_size) ft_cancel((void **) header, y); return (NULL); } - y++; i++; } return (header); diff --git a/map/parsing_header2.c b/map/parsing_header2.c index 5b657ec..abc4e89 100644 --- a/map/parsing_header2.c +++ b/map/parsing_header2.c @@ -1,7 +1,18 @@ -#include "./parsing_private.h" -#include "map.h" +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_header2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/16 17:28:58 by cchauvet #+# #+# */ +/* Updated: 2023/05/16 19:02:08 by cchauvet ### ########.fr */ +/* */ +/* ************************************************************************** */ -static int set_texture(t_map *map, int token, const char *key, char *value) +#include "./parsing_private.h" + +int set_texture(t_map *map, int token, const char *key, const char *value) { if (map->img_path[token - 1] != NULL) { @@ -14,13 +25,13 @@ static int set_texture(t_map *map, int token, const char *key, char *value) return (0); } -static unsigned int get_rgb(unsigned char red, unsigned char green, +static unsigned int get_rgb(unsigned char red, unsigned char green, unsigned char blue) { return (red << 24 | green << 16 | blue << 8 | 255); } -static long long get_color(const char **tab) +static long long get_color(const char **tab) { long long rgb[3]; size_t i; @@ -41,10 +52,11 @@ static long long get_color(const char **tab) } i++; } - return (get_rgb((unsigned char) rgb[0], (unsigned char) rgb[1], (unsigned char) rgb[2])); + return (get_rgb((unsigned char) rgb[0], + (unsigned char) rgb[1], (unsigned char) rgb[2])); } -static int set_color(long long *color, const char *key, char *value) +int set_color(long long *color, const char *key, const char *value) { char **tab; @@ -71,11 +83,11 @@ int check_header_is_complete(t_map *map) { if (map->img_path[0] == NULL) ft_eprintf("map: incomplete: Nord texture is missing"); - else if (map->img_path[0] == NULL) + else if (map->img_path[1] == NULL) ft_eprintf("map: incomplete: WEST texture is missing"); - else if (map->img_path[0] == NULL) + else if (map->img_path[2] == NULL) ft_eprintf("map: incomplete: SOUTH texture is missing"); - else if (map->img_path[0] == NULL) + else if (map->img_path[3] == NULL) ft_eprintf("map: incomplete: EAST texture is missing"); else if (map->color_bot == -1) ft_eprintf("map: incomplete: floor color is missing"); @@ -85,39 +97,3 @@ int check_header_is_complete(t_map *map) return (0); return (1); } - -int header_is_valid(char ***header, t_map *map) -{ - size_t i; - int token; - - - if (header == NULL) - return (1); - map->color_bot = -1; - map->color_top = -1; - i = 0; - while (i < ft_tablen((const void **) header)) - { - token = get_token(header[i][0]); - if (token > 0 && token < 5) - { - if (set_texture(map, token, header[i][0], header[i][1])) - return (1); - } - else if (token == 5) - { - if (set_color(&map->color_bot, - header[i][0], header[i][1])) - return (1); - } - else if (token == 6) - { - if (set_color(&map->color_top, - header[i][0], header[i][1])) - return (1); - } - i++; - } - return (check_header_is_complete(map)); -} diff --git a/map/parsing_header3.c b/map/parsing_header3.c new file mode 100644 index 0000000..81a62fe --- /dev/null +++ b/map/parsing_header3.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_header3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet 0 && token < 5) + { + if (set_texture(map, token, header[i][0], header[i][1])) + return (1); + } + else if (token == 5) + { + if (set_color(&map->color_bot, + header[i][0], header[i][1])) + return (1); + } + else if (token == 6) + { + if (set_color(&map->color_top, + header[i][0], header[i][1])) + return (1); + } + return (0); +} + +int header_is_valid(char ***header, t_map *map) +{ + size_t i; + + if (header == NULL) + return (1); + map->color_bot = -1; + map->color_top = -1; + i = 0; + while (i < ft_tablen((const void **) header)) + { + if (set_header_data(header, i, map)) + return (1); + i++; + } + return (check_header_is_complete(map)); +} diff --git a/map/parsing_meta.c b/map/parsing_meta.c index b58589d..5139fe1 100644 --- a/map/parsing_meta.c +++ b/map/parsing_meta.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_meta.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet @@ -10,4 +22,9 @@ char ***get_header(const char **file_content, size_t *header_size); int header_is_valid(char ***header, t_map *map); char **get_body(const char **file_content, size_t header_size); int body_is_valid(t_map *map); +int get_spawn_position(const char **body, double *x, double *y); +int map_is_in_one_part(const char **body); +int set_texture(t_map *map, int token, const char *key, const char *value); +int set_color(long long *color, const char *key, const char *value); +int check_header_is_complete(t_map *map); #endif diff --git a/maps/body_not_surrond_min.cub b/maps/body_not_surrond_min.cub new file mode 100644 index 0000000..e26cf6f --- /dev/null +++ b/maps/body_not_surrond_min.cub @@ -0,0 +1,9 @@ +NO ./path_to_the_north_texture +SO ./path_to_the_south_texture +WE ./path_to_the_west_texture +EA ./path_to_the_east_texture + +F 200,100,0 +C 225,30,0 + +N diff --git a/maps/header_missing.cub b/maps/header_missing.cub new file mode 100644 index 0000000..35f2cc5 --- /dev/null +++ b/maps/header_missing.cub @@ -0,0 +1,10 @@ +NO ./path_to_the_north_texture +SO ./path_to_the_south_texture +EA ./path_to_the_east_texture + +F 200,100,0 +C 225,30,0 + + 1 +1N1 + 1