|
|
@ -15,8 +15,7 @@ |
|
|
|
/* Get the initial position and orientation of the player and
|
|
|
|
* 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; |
|
|
|
|
|
|
|
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 */ |
|
|
|
int find_player(t_env *env) |
|
|
|
{ |
|
|
|
int find_player(t_env *env) { |
|
|
|
char **map; |
|
|
|
int x; |
|
|
|
int y; |
|
|
@ -44,11 +42,9 @@ int find_player(t_env *env) |
|
|
|
|
|
|
|
y = 0; |
|
|
|
map = env->map; |
|
|
|
while (map[y]) |
|
|
|
{ |
|
|
|
while (map[y]) { |
|
|
|
x = 0; |
|
|
|
while (map[y][x]) |
|
|
|
{ |
|
|
|
while (map[y][x]) { |
|
|
|
cell = map[y][x]; |
|
|
|
if (cell == 'N' || cell == 'S' || cell == 'E' || cell == 'W') |
|
|
|
return (init_player(env, x, y)); |
|
|
@ -59,8 +55,7 @@ int find_player(t_env *env) |
|
|
|
return (0); |
|
|
|
} |
|
|
|
|
|
|
|
int verify_map(t_env *env) |
|
|
|
{ |
|
|
|
int verify_map(t_env *env) { |
|
|
|
int x; |
|
|
|
int y; |
|
|
|
char cell; |
|
|
@ -68,16 +63,13 @@ int verify_map(t_env *env) |
|
|
|
|
|
|
|
spawn = 0; |
|
|
|
y = 0; |
|
|
|
while (env->map[y]) |
|
|
|
{ |
|
|
|
while (env->map[y]) { |
|
|
|
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') |
|
|
|
spawn++; |
|
|
|
else if (cell != '0' && cell != '1' && cell != '2' |
|
|
|
&& !ft_isspace(cell)) |
|
|
|
else if (cell != '0' && cell != '1' && cell != '2' && !ft_isspace(cell)) |
|
|
|
return ((printf("Error\nInvalid character on the map\n"), 0)); |
|
|
|
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.
|
|
|
|
* 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 int recurs; |
|
|
|
|
|
|
|
recurs++; |
|
|
|
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)); |
|
|
@ -108,10 +99,8 @@ int is_in_open_room(t_env *env, int x, int y) |
|
|
|
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)) |
|
|
|
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); |
|
|
@ -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
|
|
|
|
* 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 *end_line; |
|
|
|
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 *))); |
|
|
|
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)); |
|
|
|
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); |
|
|
|