Browse Source

finished i guess

master
nicolas-arnaud 2 years ago
parent
commit
bcb28f684c
  1. 1
      includes/cub3d.h
  2. 2
      maps/tests/invalid_map/not_ending_map.cub
  3. 2
      maps/tests/invalid_values/invalid_texture.cub
  4. 38
      src/parsing/getline.c
  5. 33
      src/parsing/map.c
  6. 6
      src/parsing/parsing.c

1
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); int rgb_to_int(char **rgb);
char **create_map_array(t_slist *e_lst, int wide, int deep); 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 is_in_open_room(t_env *env, int x, int y);
int verify_map(t_env *env);
int find_player(t_env *env); int find_player(t_env *env);
t_env *load_map(char *filename); t_env *load_map(char *filename);
char *get_next_line(int fd); char *get_next_line(int fd);

2
maps/tests/invalid_map/not_ending_map.cub

@ -8,7 +8,7 @@ F 250, 156,150
111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111
100000000000000000010000000000000000000000000001 100000000000000000010000000000000000000000000001
100000000000000000010000000100100100000001000001 10000000N000000000010000000100100100000001000001
100000000000000000000000000001010000000000000001 100000000000000000000000000001010000000000000001
111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111

2
maps/tests/invalid_values/invalid_texture.cub

@ -1,4 +1,4 @@
NO ./textures/wood.xpm NO ./textures/wooood.xpm
EA ./textures/wood.xpm EA ./textures/wood.xpm
SO ./textures/wood.xpm SO ./textures/wood.xpm
WE ./textures/wood.xpm WE ./textures/wood.xpm

38
src/parsing/getline.c

@ -19,18 +19,16 @@ char *save_buffer(char *buffer, int *i)
char *part2; char *part2;
char *ret; char *ret;
part1 = malloc(4097 * sizeof(char)); part1 = ft_calloc(4097, sizeof(char));
j = 0; j = 0;
while (j < 4096) while (j < 4096)
{ {
part1[j++] = buffer[*i]; part1[j++] = buffer[*i];
if (buffer[*i] == '\0' || buffer[(*i)++] == '\n') if (buffer[*i] == '\0' || buffer[(*i)++] == '\n')
{ {
part1[j] = 0;
return (part1); return (part1);
} }
} }
part1[4096] = 0;
part2 = save_buffer(buffer, i); part2 = save_buffer(buffer, i);
ret = ft_strjoin(part1, part2); ret = ft_strjoin(part1, part2);
free(part1); free(part1);
@ -38,31 +36,37 @@ char *save_buffer(char *buffer, int *i)
return (ret); return (ret);
} }
typedef struct s_gnl
{
char *line;
char *next;
char *ret;
} t_gnl;
char *get_next_line(const int fd) char *get_next_line(const int fd)
{ {
static char buffer[BUFFER_SIZE + 1]; static char buffer[BUFFER_SIZE + 1];
static int i; static int i;
char *line; t_gnl gnl;
char *ret;
char *next;
if (fd == -1) if (!buffer[i])
return (NULL); {
if (!*buffer) i = 0;
buffer[read(fd, buffer, BUFFER_SIZE)] = 0; buffer[read(fd, buffer, BUFFER_SIZE)] = 0;
ret = ft_calloc(1, sizeof(char)); }
gnl.ret = ft_calloc(1, sizeof(char));
while (buffer[i]) while (buffer[i])
{ {
next = save_buffer(buffer, &i); gnl.next = save_buffer(buffer, &i);
line = ft_strjoin(ret, next); gnl.line = ft_strjoin(gnl.ret, gnl.next);
ret = (free(ret), line); gnl.ret = (free(gnl.ret), gnl.line);
free(next); free(gnl.next);
if (buffer[i - 1] == '\n') if (buffer[i - 1] == '\n')
break ; break ;
buffer[read(fd, buffer, BUFFER_SIZE)] = 0; buffer[read(fd, buffer, BUFFER_SIZE)] = 0;
i = 0; i = 0;
} }
if (*ret) if (*(gnl.ret))
return (ret); return (gnl.ret);
return (free(ret), NULL); return (free(gnl.ret), NULL);
} }

33
src/parsing/map.c

@ -59,6 +59,35 @@ int find_player(t_env *env)
return (0); 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. /* 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.
*/ */
@ -68,9 +97,9 @@ int is_in_open_room(t_env *env, int x, int y)
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));

6
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) int is_valid_map(t_env *env)
{ {
if (!verify_map(env))
return (0);
if (!find_player(env)) if (!find_player(env))
printf("Error\nYou probably somewhere but not on the map.\n"); 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)) 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)); env = ft_calloc(1, sizeof(t_env));
fd = open(filename, O_RDONLY); fd = open(filename, O_RDONLY);
line = get_next_line(fd); line = get_next_line(fd);
while (line) while (fd > 0 && line)
{ {
if ((*line == '1' || *line == '\t' || *line == ' ') if ((*line == '1' || *line == '\t' || *line == ' ')
&& progress > 6 && progress++) && progress > 6 && progress++)
@ -139,7 +141,7 @@ t_env *load_map(char *filename)
register_settings(&progress, env, line); register_settings(&progress, env, line);
line = (free(line), get_next_line(fd)); 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); return (printf("Error\nYour map isn't valid.\n"), NULL);
env->map = create_map_array(e_map, env->wide, env->deep); env->map = create_map_array(e_map, env->wide, env->deep);
if (!is_valid_map(env) && cleanup_datas(env)) if (!is_valid_map(env) && cleanup_datas(env))

Loading…
Cancel
Save