From 88d69ebcea42619d4d1b83e482422f6c5ee595ed Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Wed, 3 May 2023 11:11:04 +0000 Subject: [PATCH 1/6] add: extra function --- libftx/extra/Makefile | 4 +-- libftx/extra/extra.h | 43 ++++++++++++++++-------------- libftx/extra/ft_atoul.c | 38 +++++++++++++++++++++++++++ libftx/extra/ft_atoul_check.c | 32 +++++++++++++++++++++++ libftx/extra/ft_contain_only.c | 6 ++--- libftx/extra/ft_is_in.c | 4 +-- libftx/extra/ft_tablen.c | 4 +-- libftx/libftx.h | 48 +++++----------------------------- 8 files changed, 108 insertions(+), 71 deletions(-) create mode 100644 libftx/extra/ft_atoul.c create mode 100644 libftx/extra/ft_atoul_check.c diff --git a/libftx/extra/Makefile b/libftx/extra/Makefile index 6ae9605..e90e814 100644 --- a/libftx/extra/Makefile +++ b/libftx/extra/Makefile @@ -6,13 +6,13 @@ # By: cchauvet # include +#include # include # include # include "../libft/libft.h" -size_t ft_tablen(const char **tab); -char *ft_ultoa_base(unsigned long long n, char *base); -char *get_next_line(int fd); -size_t ft_random_generator(size_t start, size_t stop); -void ft_freer_tab_ultimate(size_t len, ...); -void ft_freer_ultimate(size_t len, ...); -char *ft_strgen(char c, size_t len); -char *ft_strfjoin(char *s1, char *s2); -char *ft_strmerger(size_t arg_len, ...); -int ft_is_in(char *str, char c); -char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size); -char *ft_strndup(const char *src, size_t n); -ssize_t ft_strchri(char *str, char c); -int ft_contain_only_str(char *str, char *to_find); -int ft_contain_only(char *str, char c); -int ft_strcmp(const char *s1, const char *s2); -void ft_swap(void *a, void *b); -void ft_swap_int(int *a, int *b); -void ft_swap_char(char *a, char *b); +unsigned long ft_atoul(const char str[]); +int ft_atoul_check(const char str[]); +size_t ft_tablen(const void **tab); +char *ft_ultoa_base(unsigned long long n, char *base); +char *get_next_line(int fd); +size_t ft_random_generator(size_t start, size_t stop); +void ft_freer_tab_ultimate(size_t len, ...); +void ft_freer_ultimate(size_t len, ...); +char *ft_strgen(char c, size_t len); +char *ft_strfjoin(char *s1, char *s2); +char *ft_strmerger(size_t arg_len, ...); +int ft_is_in(const char *str, char c); +char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size); +char *ft_strndup(const char *src, size_t n); +ssize_t ft_strchri(char *str, char c); +int ft_contain_only_str(const char *str, const char *to_find); +int ft_contain_only(const char *str, char c); +int ft_strcmp(const char *s1, const char *s2); +void ft_swap(void *a, void *b); +void ft_swap_int(int *a, int *b); +void ft_swap_char(char *a, char *b); #endif diff --git a/libftx/extra/ft_atoul.c b/libftx/extra/ft_atoul.c new file mode 100644 index 0000000..e98af1b --- /dev/null +++ b/libftx/extra/ft_atoul.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoul.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet = '0' && str[i] <= '9') + { + out = out * 10 + str[i] - 48; + i++; + } + return (out * sign); +} diff --git a/libftx/extra/ft_atoul_check.c b/libftx/extra/ft_atoul_check.c new file mode 100644 index 0000000..fa834ce --- /dev/null +++ b/libftx/extra/ft_atoul_check.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoul_check.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet '9' || str[i] < '0') + return (0); + if (num > num * 10 + str[i] - '0') + return (0); + num = num * 10 + str[i] - 48; + i++; + } + return (1); +} diff --git a/libftx/extra/ft_contain_only.c b/libftx/extra/ft_contain_only.c index 4844e79..f2b3143 100644 --- a/libftx/extra/ft_contain_only.c +++ b/libftx/extra/ft_contain_only.c @@ -6,13 +6,13 @@ /* By: cchauvet # include # include "./libft/libft.h" +# include "./extra/extra.h" -size_t ft_tablen(const char **tab); +size_t ft_tablen(const void **tab); char *ft_ultoa_base(unsigned long long n, char *base); int ft_printf(const char *format, ...); int ft_eprintf(const char *format, ...); @@ -28,10 +29,10 @@ size_t ft_random_generator(size_t start, size_t stop); void ft_freer_tab_ultimate(size_t len, ...); void ft_freer_ultimate(size_t len, ...); char *ft_strgen(char c, size_t len); -int ft_is_in(char *str, char c); +int ft_is_in(const char *str, char c); char **ft_tabrealloc(char **tab, size_t current_size, size_t new_size); -int ft_contain_only_str(char *str, char *to_find); -int ft_contain_only(char *str, char c); +int ft_contain_only_str(const char *str, const char *to_find); +int ft_contain_only(const char *str, char c); char *ft_strfjoin(char *s1, char *s2); char *ft_strmerger(size_t arg_len, ...); int ft_strcmp(const char *s1, const char *s2); @@ -41,41 +42,4 @@ void ft_swap(void *a, void *b); void ft_swap_int(int *a, int *b); void ft_swap_char(char *a, char *b); -/* void *ft_cancel(void **tab, size_t len); */ -/* int ft_atoi(const char *nptr); */ -/* void ft_bzero(void *s, size_t n); */ -/* void *ft_calloc(size_t nmemb, size_t size); */ -/* int ft_isalnum(int c); */ -/* int ft_isalpha(int c); */ -/* int ft_isascii(int c); */ -/* int ft_isdigit(int c); */ -/* int ft_isprint(int c); */ -/* void *ft_memchr(const void *s, int c, size_t n); */ -/* int ft_memcmp(const void *s1, const void *s2, size_t n); */ -/* void *ft_memcpy(void *dest, const void *src, size_t n); */ -/* void *ft_memmove(void *dest, const void *src, size_t n); */ -/* void *ft_memset(void *s, int c, size_t n); */ -/* char *ft_strchr(const char *s, int c); */ -/* char *ft_strdup(const char *s); */ -/* size_t ft_strlcat(char *dst, const char *src, size_t size); */ -/* size_t ft_strlcpy(char *dst, const char *src, size_t size); */ -/* size_t ft_strlen(const char *s); */ -/* int ft_strncmp(const char *s1, const char *s2, size_t n); */ -/* char *ft_strnstr(const char *big, const char *little, size_t len); */ -/* char *ft_strrchr(const char *s, int c); */ -/* int ft_tolower(int c); */ -/* int ft_toupper(int c); */ - -/* char *ft_substr(char const *s, unsigned int start, size_t len); */ -/* char *ft_strjoin(char const *s1, char const *s2); */ -/* char *ft_strtrim(char const *s1, char const *set); */ -/* char **ft_split(char const *s, char c); */ -/* char *ft_itoa(int n); */ -/* char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); */ -/* void ft_striteri(char *s, void (*f)(unsigned int, char*)); */ -/* void ft_putchar_fd(char c, int fd); */ -/* void ft_putstr_fd(char *s, int fd); */ -/* void ft_putendl_fd(char *s, int fd); */ -/* void ft_putnbr_fd(int n, int fd); */ - #endif From 900547d1cc7636d7fbad6d57eba8df83640e9437 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Wed, 3 May 2023 14:33:57 +0000 Subject: [PATCH 2/6] fix: parsing work --- .gitignore | 3 ++ main.c | 9 ++--- map/parsing.c | 73 +++++++++++++++++++++++++++++++++++++- map/parsing_body.c | 23 ++++++------ map/parsing_header2.c | 2 ++ map/parsing_meta.c | 12 +++---- maps/test1.cub | 22 ++++++++++++ maps/test_not_surround.cub | 22 ++++++++++++ maps/test_splitted.cub | 23 ++++++++++++ 9 files changed, 166 insertions(+), 23 deletions(-) create mode 100644 .gitignore create mode 100644 maps/test1.cub create mode 100644 maps/test_not_surround.cub create mode 100644 maps/test_splitted.cub diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93ad8e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +cub3D +*.swp diff --git a/main.c b/main.c index 9b3a504..a72a1c2 100644 --- a/main.c +++ b/main.c @@ -6,11 +6,12 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/26 12:44:55 by erey-bet #+# #+# */ -/* Updated: 2023/04/27 14:43:10 by erey-bet ### ########.fr */ +/* Updated: 2023/05/03 12:50:25 by cchauvet ### ########.fr */ /* */ /* ************************************************************************** */ #include "cube3D.h" +#include "libftx/libftx.h" int main(int argc, char **argv) { @@ -21,11 +22,11 @@ int main(int argc, char **argv) printf("No argument"); return (1); } - /*map = map_parsing(argv[1]); + map = map_parsing(argv[1]); if (!map) - { return (2); - }*/ + ft_printf("%S", map->map); + return (2); (void)argv; map = malloc(sizeof(t_map)); map->map = malloc(sizeof(char *) * 5); diff --git a/map/parsing.c b/map/parsing.c index 18fdca0..5a4ba6d 100644 --- a/map/parsing.c +++ b/map/parsing.c @@ -1,12 +1,83 @@ #include "./parsing_private.h" +#include "map.h" +#include + +static ssize_t get_nb_line(const char *path) +{ + int fd; + char readed[1]; + size_t i; + + fd = open(path, O_RDONLY); + if (fd == -1) + return (-1); + i = 1; + while (read(fd, readed, 1)) + { + if (readed[0] == '\n') + i++; + } + close(fd); + return (i); +} +static char **read_map(const char *path) +{ + int fd; + size_t i; + char **map; + + map = malloc(sizeof(char *) * (get_nb_line(path) + 1)); + if (map == NULL) + return (NULL); + fd = open(path, O_RDONLY); + if (fd == -1) + { + free(map); + return (NULL); + } + i = 0; + map[i] = get_next_line(fd); + while (map[i] != NULL) + { + map[i][ft_strlen(map[i]) - 1] = '\0'; + i++; + map[i] = get_next_line(fd); + } + return (map); +} t_map *map_parsing(const char *path) { + char **file_content; + char ***header; + char **body; + size_t header_size; t_map *map; + if (parsing_meta(path)) + return (NULL); map = malloc(sizeof(t_map)); if (map == NULL) return (NULL); - (void)path; + file_content = read_map(path); + if (file_content == NULL) + return (NULL); + header = get_header((const char **) file_content, &header_size); + if (header_is_valid(header, map) == 0) + { + ft_freer_tab_ultimate(1, file_content); + ft_freer_ultimate(1, map); + return (NULL); + } + //ici aussi header; + body = get_body((const char **) file_content, header_size); + ft_freer_tab_ultimate(1, file_content); + if (body_is_valid((const char **) body, map) == 0) + { + ft_freer_ultimate(1, map); + // faudra free + return (NULL); + } + map->map = body; return (map); } diff --git a/map/parsing_body.c b/map/parsing_body.c index eb3c9e7..787ab13 100644 --- a/map/parsing_body.c +++ b/map/parsing_body.c @@ -63,7 +63,7 @@ static int map_is_in_one_part(const char **body) { size_t y; - y = ft_tablen((const void **) body); + y = ft_tablen((const void **) body) - 1; while (body[y][0] == '\0' || ft_contain_only(body[y], ' ')) y--; while (y > 0) @@ -77,14 +77,15 @@ static int map_is_in_one_part(const char **body) static int map_surround_check(char **body, size_t x, size_t y) { - if (ft_is_in(";1", body[y][x])) - return (0); - if (ft_is_in(" ", body[y][x])) - return (1); if (y == 0 || body[y] == NULL) return (1); if (x == 0 || body[y][x] == '\0') return (1); + if (ft_is_in("21", body[y][x])) + return (0); + if (ft_is_in(" ", body[y][x])) + return (1); + body[y][x] = '2'; if (map_surround_check(body, x + 1, y)) return (1); if (map_surround_check(body, x - 1, y)) @@ -129,28 +130,30 @@ int body_is_valid(const char **body, t_map *map) { int error; + if (body == NULL) + return (0); error = get_spawn_position(body, &map->spawn_x, &map->spawn_y); if (error == 1) { ft_eprintf("map: spawn position: multiple definition"); - return (1); + return (0); } else if (error == 2) { ft_eprintf("map: spawn position: not define"); - return (1); + return (0); } error = map_surround(body, map); if (error) { ft_eprintf("map: not surrounded"); - return (1); + return (0); } error = map_is_in_one_part(body); if (error) { ft_eprintf("map: splitted"); - return (1); + return (0); } - return (0); + return (1); } diff --git a/map/parsing_header2.c b/map/parsing_header2.c index afcfeb1..12b037b 100644 --- a/map/parsing_header2.c +++ b/map/parsing_header2.c @@ -33,6 +33,8 @@ int header_is_valid(char ***header, t_map *map) int token; + if (header == NULL) + return (1); map->color_bot = -1; map->color_top = -1; i = 0; diff --git a/map/parsing_meta.c b/map/parsing_meta.c index 3acb5e3..7424a94 100644 --- a/map/parsing_meta.c +++ b/map/parsing_meta.c @@ -5,17 +5,13 @@ static int name_check(const char *path) size_t len; len = ft_strlen(path); - if (len < 4) + if (len < 4 + || (ft_strcmp(".cub", path + len - 4) != 0)) { - ft_eprintf("map error: name doesn't finished by .cub"); + ft_eprintf("map: name doesn't finished by .cub"); return (1); } - if (ft_strcmp(".cub", path + len - 3) != 0) - { - ft_eprintf("map error: name doesn't finished by .cub"); - return (1); - } - return (1); + return (0); } int parsing_meta(const char *path) diff --git a/maps/test1.cub b/maps/test1.cub new file mode 100644 index 0000000..288e9ed --- /dev/null +++ b/maps/test1.cub @@ -0,0 +1,22 @@ +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 220,100,0 +C 225,30,0 + + 1111111111111111111111111 + 1000000000110000000000001 + 1011000001110000000000001 + 1001000000000000000000001 +111111111011000001110000000000001 +100000000011000001110111111111111 +11110111111111011100000010001 +11110111111111011101010010001 +11000000110101011100000010001 +10000000000000001100000010001 +10000000000000001101010010001 +11000001110101011111011110N0111 +11110111 1110101 101111010001 +11111111 1111111 111111111111 diff --git a/maps/test_not_surround.cub b/maps/test_not_surround.cub new file mode 100644 index 0000000..68bcbd7 --- /dev/null +++ b/maps/test_not_surround.cub @@ -0,0 +1,22 @@ +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 220,100,0 +C 225,30,0 + + 1111111111111111111111111 + 1000000000110000000000001 + 1011000001110000000000001 + 1001000000000000000000001 +111111111011000001110000000000001 +100000000011000001110111111111111 +11110111111111011100000010001 +11110111111111011101010010001 +11000000110101011100000010001 +10000000000000001100000010001 +10000000000000001101010010001 +11000001110101011111011110N00 +11110111 1110101 101111010001 +11111111 1111111 111111111111 diff --git a/maps/test_splitted.cub b/maps/test_splitted.cub new file mode 100644 index 0000000..3bf9ef3 --- /dev/null +++ b/maps/test_splitted.cub @@ -0,0 +1,23 @@ +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 220,100,0 +C 225,30,0 + + 1111111111111111111111111 + 1000000000110000000000001 + 1011000001110000000000001 + 1001000000000000000000001 +111111111011000001110000000000001 + +100000000011000001110111111111111 +11110111111111011100000010001 +11110111111111011101010010001 +11000000110101011100000010001 +10000000000000001100000010001 +10000000000000001101010010001 +11000001110101011111011110N0111 +11110111 1110101 101111010001 +11111111 1111111 111111111111 From a301809beab3933b80a348de97aebaa4eceb025a Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Thu, 4 May 2023 10:48:31 +0000 Subject: [PATCH 3/6] fix: add error message when read|open failed --- map/parsing.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/map/parsing.c b/map/parsing.c index 5a4ba6d..a6d847a 100644 --- a/map/parsing.c +++ b/map/parsing.c @@ -61,7 +61,10 @@ t_map *map_parsing(const char *path) return (NULL); file_content = read_map(path); if (file_content == NULL) + { + ft_eprintf("map: file error"); return (NULL); + } header = get_header((const char **) file_content, &header_size); if (header_is_valid(header, map) == 0) { From c4d953acb53b6f69af13a45f4e42bc2366acbcab Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Thu, 4 May 2023 11:17:55 +0000 Subject: [PATCH 4/6] fix: add verif when map body doesn't contain only regulare char --- map/parsing_body.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/map/parsing_body.c b/map/parsing_body.c index 787ab13..c2ef3c8 100644 --- a/map/parsing_body.c +++ b/map/parsing_body.c @@ -126,6 +126,29 @@ static int map_surround(const char **body, t_map *map) 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); +} + int body_is_valid(const char **body, t_map *map) { int error; @@ -155,5 +178,7 @@ int body_is_valid(const char **body, t_map *map) ft_eprintf("map: splitted"); return (0); } + if (body_contain_normal_char(body) == 0) + return (0); return (1); } From b323b98dc0e840a3f5c8c3a537b510bf1ebf77d3 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Thu, 4 May 2023 11:47:18 +0000 Subject: [PATCH 5/6] core: check map surround/close --- map/parsing_body.c | 63 +++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/map/parsing_body.c b/map/parsing_body.c index c2ef3c8..9dec019 100644 --- a/map/parsing_body.c +++ b/map/parsing_body.c @@ -75,36 +75,14 @@ static int map_is_in_one_part(const char **body) return (0); } -static int map_surround_check(char **body, size_t x, size_t y) -{ - if (y == 0 || body[y] == NULL) - return (1); - if (x == 0 || body[y][x] == '\0') - return (1); - if (ft_is_in("21", body[y][x])) - return (0); - if (ft_is_in(" ", body[y][x])) - return (1); - body[y][x] = '2'; - if (map_surround_check(body, x + 1, y)) - return (1); - if (map_surround_check(body, x - 1, y)) - return (1); - if (map_surround_check(body, x, y + 1)) - return (1); - if (map_surround_check(body, x, y - 1)) - return (1); - return (0); -} - -static int map_surround(const char **body, t_map *map) +static char **map_cpy(const char **body) { size_t i; char **copy; copy = malloc((ft_tablen((const void **) body) + 1) * sizeof(char *)); if (copy == NULL) - return (1); + return (NULL); i = 0; while (body[i] != NULL) { @@ -112,15 +90,42 @@ static int map_surround(const char **body, t_map *map) if (copy[i] == NULL) { ft_cancel((void **) copy, i); - return (1); + return (NULL); } i++; } copy[i] = NULL; - if (map_surround_check(copy, map->spawn_x, map->spawn_y)) - { - ft_freer_tab_ultimate(1, copy); + return (copy); +} + +static int map_surround(const char **body) +{ + char **copy; + size_t y; + size_t x; + + copy = map_cpy(body); + if (copy == NULL) return (1); + 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++; } ft_freer_tab_ultimate(1, copy); return (0); @@ -166,7 +171,7 @@ int body_is_valid(const char **body, t_map *map) ft_eprintf("map: spawn position: not define"); return (0); } - error = map_surround(body, map); + error = map_surround(body); if (error) { ft_eprintf("map: not surrounded"); From c9d9faa45cfe8394217727638aa41e4a902bf188 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Thu, 4 May 2023 11:58:30 +0000 Subject: [PATCH 6/6] add: replace player spawn char to '0' and store spawn_direction --- map/map.h | 1 + map/parsing.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/map/map.h b/map/map.h index 307435c..bed3d5f 100644 --- a/map/map.h +++ b/map/map.h @@ -12,6 +12,7 @@ typedef struct s_map long long color_top; long long spawn_x; long long spawn_y; + char direction; } t_map; #endif diff --git a/map/parsing.c b/map/parsing.c index a6d847a..fdea169 100644 --- a/map/parsing.c +++ b/map/parsing.c @@ -20,6 +20,7 @@ static ssize_t get_nb_line(const char *path) close(fd); return (i); } + static char **read_map(const char *path) { int fd; @@ -82,5 +83,7 @@ t_map *map_parsing(const char *path) return (NULL); } map->map = body; + map->direction = map->map[map->spawn_y][map->spawn_x]; + map->map[map->spawn_y][map->spawn_x] = '0'; return (map); }