From bcb28f684caad47e773e188f7cc0752a9ad5e17c Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Tue, 22 Nov 2022 11:44:54 +0100 Subject: [PATCH] finished i guess --- includes/cub3d.h | 1 + maps/tests/invalid_map/not_ending_map.cub | 2 +- maps/tests/invalid_values/invalid_texture.cub | 2 +- src/parsing/getline.c | 38 ++++++++++--------- src/parsing/map.c | 33 +++++++++++++++- src/parsing/parsing.c | 6 ++- 6 files changed, 59 insertions(+), 23 deletions(-) diff --git a/includes/cub3d.h b/includes/cub3d.h index 0f8d278..43afed3 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -135,6 +135,7 @@ t_dvec dvec_rot(t_dvec vec, double rad, double init_len); int rgb_to_int(char **rgb); char **create_map_array(t_slist *e_lst, int wide, int deep); int is_in_open_room(t_env *env, int x, int y); +int verify_map(t_env *env); int find_player(t_env *env); t_env *load_map(char *filename); char *get_next_line(int fd); diff --git a/maps/tests/invalid_map/not_ending_map.cub b/maps/tests/invalid_map/not_ending_map.cub index 8d79d61..5778867 100644 --- a/maps/tests/invalid_map/not_ending_map.cub +++ b/maps/tests/invalid_map/not_ending_map.cub @@ -8,7 +8,7 @@ F 250, 156,150 111111111111111111111111111111111111111111111111 100000000000000000010000000000000000000000000001 -100000000000000000010000000100100100000001000001 +10000000N000000000010000000100100100000001000001 100000000000000000000000000001010000000000000001 111111111111111111111111111111111111111111111111 diff --git a/maps/tests/invalid_values/invalid_texture.cub b/maps/tests/invalid_values/invalid_texture.cub index 4fa53c6..dad9d91 100644 --- a/maps/tests/invalid_values/invalid_texture.cub +++ b/maps/tests/invalid_values/invalid_texture.cub @@ -1,4 +1,4 @@ -NO ./textures/wood.xpm +NO ./textures/wooood.xpm EA ./textures/wood.xpm SO ./textures/wood.xpm WE ./textures/wood.xpm diff --git a/src/parsing/getline.c b/src/parsing/getline.c index 0560927..5539ef0 100644 --- a/src/parsing/getline.c +++ b/src/parsing/getline.c @@ -19,18 +19,16 @@ char *save_buffer(char *buffer, int *i) char *part2; char *ret; - part1 = malloc(4097 * sizeof(char)); + part1 = ft_calloc(4097, sizeof(char)); j = 0; while (j < 4096) { part1[j++] = buffer[*i]; if (buffer[*i] == '\0' || buffer[(*i)++] == '\n') { - part1[j] = 0; return (part1); } } - part1[4096] = 0; part2 = save_buffer(buffer, i); ret = ft_strjoin(part1, part2); free(part1); @@ -38,31 +36,37 @@ char *save_buffer(char *buffer, int *i) return (ret); } +typedef struct s_gnl +{ + char *line; + char *next; + char *ret; +} t_gnl; + char *get_next_line(const int fd) { static char buffer[BUFFER_SIZE + 1]; static int i; - char *line; - char *ret; - char *next; + t_gnl gnl; - if (fd == -1) - return (NULL); - if (!*buffer) + if (!buffer[i]) + { + i = 0; buffer[read(fd, buffer, BUFFER_SIZE)] = 0; - ret = ft_calloc(1, sizeof(char)); + } + gnl.ret = ft_calloc(1, sizeof(char)); while (buffer[i]) { - next = save_buffer(buffer, &i); - line = ft_strjoin(ret, next); - ret = (free(ret), line); - free(next); + gnl.next = save_buffer(buffer, &i); + gnl.line = ft_strjoin(gnl.ret, gnl.next); + gnl.ret = (free(gnl.ret), gnl.line); + free(gnl.next); if (buffer[i - 1] == '\n') break ; buffer[read(fd, buffer, BUFFER_SIZE)] = 0; i = 0; } - if (*ret) - return (ret); - return (free(ret), NULL); + if (*(gnl.ret)) + return (gnl.ret); + return (free(gnl.ret), NULL); } diff --git a/src/parsing/map.c b/src/parsing/map.c index bfcbead..3638130 100644 --- a/src/parsing/map.c +++ b/src/parsing/map.c @@ -59,6 +59,35 @@ int find_player(t_env *env) return (0); } +int verify_map(t_env *env) +{ + int x; + int y; + char cell; + int spawn; + + spawn = 0; + y = 0; + while (env->map[y]) + { + x = 0; + while (env->map[y][x]) + { + cell = env->map[y][x]; + if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W') + spawn++; + else if (cell != '0' && cell != '1' && cell != '2' + && !ft_isspace(cell)) + return ((printf("Error\nInvalid character on the map\n"), 0)); + x++; + } + y++; + } + if (spawn > 1) + return ((printf("Error\nToo many spawn on the map\n"), 0)); + return (1); +} + /* Check all around the player if he can acces the void around the map. * Also check if the map is not to big to avoid so later stack overflow. */ @@ -68,9 +97,9 @@ int is_in_open_room(t_env *env, int x, int y) static int recurs; recurs++; - if (recurs > 1000000 - 2 * (env->wide * env->deep)) + if (recurs > 1000000 - 2 * env->wide * env->deep) return (1); - if (x < 0 || x >= env->wide || y < 0 || y >= env->deep) + if (x < 0 || x > env->wide || y < 0 || y > env->deep) return (1); if (!checked) checked = ft_calloc(env->deep * env->wide + 1, sizeof(char)); diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index 72f8e31..15957ab 100644 --- a/src/parsing/parsing.c +++ b/src/parsing/parsing.c @@ -97,6 +97,8 @@ t_slist *read_map_line(t_env *env, char *line) int is_valid_map(t_env *env) { + if (!verify_map(env)) + return (0); if (!find_player(env)) printf("Error\nYou probably somewhere but not on the map.\n"); else if (is_in_open_room(env, env->player_pos.x, env->player_pos.y)) @@ -130,7 +132,7 @@ t_env *load_map(char *filename) env = ft_calloc(1, sizeof(t_env)); fd = open(filename, O_RDONLY); line = get_next_line(fd); - while (line) + while (fd > 0 && line) { if ((*line == '1' || *line == '\t' || *line == ' ') && progress > 6 && progress++) @@ -139,7 +141,7 @@ t_env *load_map(char *filename) register_settings(&progress, env, line); line = (free(line), get_next_line(fd)); } - if (progress < 7 && cleanup_datas(env)) + if (!e_map || (progress < 7 && cleanup_datas(env))) return (printf("Error\nYour map isn't valid.\n"), NULL); env->map = create_map_array(e_map, env->wide, env->deep); if (!is_valid_map(env) && cleanup_datas(env))