From c6ab5c765a7be420728c3467c6da1c83bc9983e9 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Tue, 22 Nov 2022 12:01:34 +0100 Subject: [PATCH] fix bad fix causing possible sigseg --- maps/tests/invalid_map/open.cub | 4 +- src/parsing/map.c | 208 +++++++++++++++----------------- 2 files changed, 99 insertions(+), 113 deletions(-) diff --git a/maps/tests/invalid_map/open.cub b/maps/tests/invalid_map/open.cub index 71de77e..1a5b910 100644 --- a/maps/tests/invalid_map/open.cub +++ b/maps/tests/invalid_map/open.cub @@ -9,6 +9,6 @@ C 27, 210,200 111111111111111111111111111111111111111111111111 100000000000000000010000000000000000000000000001 -100000000000N00000010000000100100100000001000000 +100000000000N00000010000000100100100000001000001 100000000000000000000000000001010000000000000001 -111111111111111111111111111111111111111111111111 +111111111111111111111111111111111111111111110111 diff --git a/src/parsing/map.c b/src/parsing/map.c index 3638130..fd165cb 100644 --- a/src/parsing/map.c +++ b/src/parsing/map.c @@ -15,135 +15,121 @@ /* Get the initial position and orientation of the player and * save it into env. */ -int init_player(t_env *env, int x, int y) -{ - char orientation; +int init_player(t_env *env, int x, int y) { + char orientation; - orientation = env->map[y][x]; - env->map[y][x] = '0'; - dvec_set(&env->player_pos, x + 0.5, y + 0.5); - if (orientation == 'N') - dvec_set(&env->player_dir, 0, -1); - else if (orientation == 'S') - dvec_set(&env->player_dir, 0, 1); - else if (orientation == 'E') - dvec_set(&env->player_dir, 1, 0); - else if (orientation == 'W') - dvec_set(&env->player_dir, -1, 0); - env->cam_plan = dvec_rot(env->player_dir, M_PI / 2, 0.66); - return (1); + orientation = env->map[y][x]; + env->map[y][x] = '0'; + dvec_set(&env->player_pos, x + 0.5, y + 0.5); + if (orientation == 'N') + dvec_set(&env->player_dir, 0, -1); + else if (orientation == 'S') + dvec_set(&env->player_dir, 0, 1); + else if (orientation == 'E') + dvec_set(&env->player_dir, 1, 0); + else if (orientation == 'W') + dvec_set(&env->player_dir, -1, 0); + env->cam_plan = dvec_rot(env->player_dir, M_PI / 2, 0.66); + return (1); } /* Find the player into the map */ -int find_player(t_env *env) -{ - char **map; - int x; - int y; - char cell; +int find_player(t_env *env) { + char **map; + int x; + int y; + char cell; - y = 0; - map = env->map; - while (map[y]) - { - x = 0; - while (map[y][x]) - { - cell = map[y][x]; - if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W') - return (init_player(env, x, y)); - x++; - } - y++; - } - return (0); + y = 0; + map = env->map; + while (map[y]) { + x = 0; + while (map[y][x]) { + cell = map[y][x]; + if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W') + return (init_player(env, x, y)); + x++; + } + y++; + } + return (0); } -int verify_map(t_env *env) -{ - int x; - int y; - char cell; - int spawn; +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); + 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. */ -int is_in_open_room(t_env *env, int x, int y) -{ - static char *checked; - static int recurs; +int is_in_open_room(t_env *env, int x, int y) { + static char *checked; + static int recurs; - recurs++; - if (recurs > 1000000 - 2 * env->wide * env->deep) - return (1); - 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)); - if (checked[y * env->wide + x]) - return (0); - checked[y * env->wide + x] = 1; - if (env->map[y][x] == '1') - return (0); - else if (is_in_open_room(env, x + 1, y) - || is_in_open_room(env, x, y + 1) - || is_in_open_room(env, x - 1, y) - || is_in_open_room(env, x, y - 1)) - return (1); - else - return (0); + recurs++; + if (recurs > 1000000 - 2 * env->wide * env->deep) + return (1); + 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)); + if (checked[y * env->wide + x]) + return (0); + checked[y * env->wide + x] = 1; + if (env->map[y][x] == '1') + return (0); + else if (is_in_open_room(env, x + 1, y) || is_in_open_room(env, x, y + 1) || + is_in_open_room(env, x - 1, y) || is_in_open_room(env, x, y - 1)) + return (1); + else + return (0); } /* Convert map chained list into a string array for * render faster. */ -char **create_map_array(t_slist *e_lst, int wide, int deep) -{ - char **ret; - char *end_line; - t_slist *tmp; - int i; +char **create_map_array(t_slist *e_lst, int wide, int deep) { + char **ret; + char *end_line; + t_slist *tmp; + int i; - if (!e_lst) - return (ft_calloc(1, sizeof(char *))); - ret = ft_calloc(deep + 1, sizeof(char *)); - i = 0; - while (i < deep) - { - end_line = ft_calloc(wide - ft_strlen(e_lst->content) + 1, - sizeof(char)); - end_line = ft_memset(end_line, - '0', wide - ft_strlen(e_lst->content) * sizeof(char)); - ret[i] = ft_strjoin(e_lst->content, end_line); - free(end_line); - free(e_lst->content); - tmp = e_lst; - e_lst = e_lst->next; - free(tmp); - i++; - } - return (ret); + if (!e_lst) + return (ft_calloc(1, sizeof(char *))); + ret = ft_calloc(deep + 1, sizeof(char *)); + i = 0; + while (i < deep) { + end_line = ft_calloc(wide - ft_strlen(e_lst->content) + 1, sizeof(char)); + end_line = ft_memset(end_line, '0', + wide - ft_strlen(e_lst->content) * sizeof(char)); + ret[i] = ft_strjoin(e_lst->content, end_line); + free(end_line); + free(e_lst->content); + tmp = e_lst; + e_lst = e_lst->next; + free(tmp); + i++; + } + return (ret); }