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. 208
      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

208
src/parsing/map.c

@ -15,135 +15,121 @@
/* 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];
env->map[y][x] = '0'; env->map[y][x] = '0';
dvec_set(&env->player_pos, x + 0.5, y + 0.5); dvec_set(&env->player_pos, x + 0.5, y + 0.5);
if (orientation == 'N') if (orientation == 'N')
dvec_set(&env->player_dir, 0, -1); dvec_set(&env->player_dir, 0, -1);
else if (orientation == 'S') else if (orientation == 'S')
dvec_set(&env->player_dir, 0, 1); dvec_set(&env->player_dir, 0, 1);
else if (orientation == 'E') else if (orientation == 'E')
dvec_set(&env->player_dir, 1, 0); dvec_set(&env->player_dir, 1, 0);
else if (orientation == 'W') else if (orientation == 'W')
dvec_set(&env->player_dir, -1, 0); dvec_set(&env->player_dir, -1, 0);
env->cam_plan = dvec_rot(env->player_dir, M_PI / 2, 0.66); env->cam_plan = dvec_rot(env->player_dir, M_PI / 2, 0.66);
return (1); return (1);
} }
/* 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; char cell;
char cell;
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];
{ if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W')
cell = map[y][x]; return (init_player(env, x, y));
if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W') x++;
return (init_player(env, x, y)); }
x++; y++;
} }
y++; 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; int spawn;
int spawn;
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];
{ if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W')
cell = env->map[y][x]; spawn++;
if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W') else if (cell != '0' && cell != '1' && cell != '2' && !ft_isspace(cell))
spawn++; return ((printf("Error\nInvalid character on the map\n"), 0));
else if (cell != '0' && cell != '1' && cell != '2' x++;
&& !ft_isspace(cell)) }
return ((printf("Error\nInvalid character on the map\n"), 0)); y++;
x++; }
} if (spawn > 1)
y++; return ((printf("Error\nToo many spawn on the map\n"), 0));
} return (1);
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. /* 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));
if (checked[y * env->wide + x]) if (checked[y * env->wide + x])
return (0); return (0);
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) return (1);
|| is_in_open_room(env, x, y - 1)) else
return (1); return (0);
else
return (0);
} }
/* 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; int i;
int i;
if (!e_lst) if (!e_lst)
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, ret[i] = ft_strjoin(e_lst->content, end_line);
'0', wide - ft_strlen(e_lst->content) * sizeof(char)); free(end_line);
ret[i] = ft_strjoin(e_lst->content, end_line); free(e_lst->content);
free(end_line); tmp = e_lst;
free(e_lst->content); e_lst = e_lst->next;
tmp = e_lst; free(tmp);
e_lst = e_lst->next; i++;
free(tmp); }
i++; return (ret);
}
return (ret);
} }

Loading…
Cancel
Save