Browse Source

fix bad fix causing possible sigseg

master
nicolas-arnaud 2 years ago
parent
commit
c6ab5c765a
  1. 4
      maps/tests/invalid_map/open.cub
  2. 48
      src/parsing/map.c

4
maps/tests/invalid_map/open.cub

@ -9,6 +9,6 @@ C 27, 210,200
111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111
100000000000000000010000000000000000000000000001 100000000000000000010000000000000000000000000001
100000000000N00000010000000100100100000001000000 100000000000N00000010000000100100100000001000001
100000000000000000000000000001010000000000000001 100000000000000000000000000001010000000000000001
111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111110111

48
src/parsing/map.c

@ -15,8 +15,7 @@
/* Get the initial position and orientation of the player and /* Get the initial position and orientation of the player and
* save it into env. * save it into env.
*/ */
int init_player(t_env *env, int x, int y) int init_player(t_env *env, int x, int y) {
{
char orientation; char orientation;
orientation = env->map[y][x]; orientation = env->map[y][x];
@ -35,8 +34,7 @@ int init_player(t_env *env, int x, int y)
} }
/* Find the player into the map */ /* Find the player into the map */
int find_player(t_env *env) int find_player(t_env *env) {
{
char **map; char **map;
int x; int x;
int y; int y;
@ -44,11 +42,9 @@ int find_player(t_env *env)
y = 0; y = 0;
map = env->map; map = env->map;
while (map[y]) while (map[y]) {
{
x = 0; x = 0;
while (map[y][x]) while (map[y][x]) {
{
cell = map[y][x]; cell = map[y][x];
if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W') if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W')
return (init_player(env, x, y)); return (init_player(env, x, y));
@ -59,8 +55,7 @@ int find_player(t_env *env)
return (0); return (0);
} }
int verify_map(t_env *env) int verify_map(t_env *env) {
{
int x; int x;
int y; int y;
char cell; char cell;
@ -68,16 +63,13 @@ int verify_map(t_env *env)
spawn = 0; spawn = 0;
y = 0; y = 0;
while (env->map[y]) while (env->map[y]) {
{
x = 0; x = 0;
while (env->map[y][x]) while (env->map[y][x]) {
{
cell = env->map[y][x]; cell = env->map[y][x];
if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W') if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W')
spawn++; spawn++;
else if (cell != '0' && cell != '1' && cell != '2' else if (cell != '0' && cell != '1' && cell != '2' && !ft_isspace(cell))
&& !ft_isspace(cell))
return ((printf("Error\nInvalid character on the map\n"), 0)); return ((printf("Error\nInvalid character on the map\n"), 0));
x++; x++;
} }
@ -91,15 +83,14 @@ int verify_map(t_env *env)
/* Check all around the player if he can acces the void around the map. /* 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. * 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) int is_in_open_room(t_env *env, int x, int y) {
{
static char *checked; static char *checked;
static int recurs; static int recurs;
recurs++; recurs++;
if (recurs > 1000000 - 2 * env->wide * env->deep) if (recurs > 1000000 - 2 * env->wide * env->deep)
return (1); 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); return (1);
if (!checked) if (!checked)
checked = ft_calloc(env->deep * env->wide + 1, sizeof(char)); checked = ft_calloc(env->deep * env->wide + 1, sizeof(char));
@ -108,10 +99,8 @@ int is_in_open_room(t_env *env, int x, int y)
checked[y * env->wide + x] = 1; checked[y * env->wide + x] = 1;
if (env->map[y][x] == '1') if (env->map[y][x] == '1')
return (0); return (0);
else if (is_in_open_room(env, x + 1, y) else if (is_in_open_room(env, x + 1, y) || is_in_open_room(env, x, y + 1) ||
|| is_in_open_room(env, x, y + 1) 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); return (1);
else else
return (0); return (0);
@ -120,8 +109,7 @@ int is_in_open_room(t_env *env, int x, int y)
/* Convert map chained list into a string array for /* Convert map chained list into a string array for
* render faster. * render faster.
*/ */
char **create_map_array(t_slist *e_lst, int wide, int deep) char **create_map_array(t_slist *e_lst, int wide, int deep) {
{
char **ret; char **ret;
char *end_line; char *end_line;
t_slist *tmp; t_slist *tmp;
@ -131,12 +119,10 @@ char **create_map_array(t_slist *e_lst, int wide, int deep)
return (ft_calloc(1, sizeof(char *))); return (ft_calloc(1, sizeof(char *)));
ret = ft_calloc(deep + 1, sizeof(char *)); ret = ft_calloc(deep + 1, sizeof(char *));
i = 0; i = 0;
while (i < deep) while (i < deep) {
{ end_line = ft_calloc(wide - ft_strlen(e_lst->content) + 1, sizeof(char));
end_line = ft_calloc(wide - ft_strlen(e_lst->content) + 1, end_line = ft_memset(end_line, '0',
sizeof(char)); wide - ft_strlen(e_lst->content) * 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); ret[i] = ft_strjoin(e_lst->content, end_line);
free(end_line); free(end_line);
free(e_lst->content); free(e_lst->content);

Loading…
Cancel
Save