read map files

This commit is contained in:
2025-12-04 10:32:50 +01:00
parent 488aeeaeac
commit 3e837f1144
3 changed files with 191 additions and 1 deletions

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/25 19:47:02 by aortigos #+# #+# */
/* Updated: 2025/12/01 10:03:36 by aortigos ### ########.fr */
/* Updated: 2025/12/04 10:04:49 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -48,4 +48,12 @@ t_turelist *new_texture(char *line);
void lst_back_ture(t_turelist **l_ture, t_turelist *new);
int lst_ture(t_data *m, t_turelist **l_ture);
// read_map.c
int is_surrounded(char *line);
int is_validmap(char *line, int *flag);
char *getmap(t_data *map);
int read_map_(t_data *map, int count);
void process_map(t_data *map, int *count);
#endif

93
src/parsing/read_map.c Normal file
View File

@@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/04 09:28:05 by aortigos #+# #+# */
/* Updated: 2025/12/04 09:59:45 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
int is_surrounded(char *line)
{
while (ft_isspace(*line))
line++;
return (*line == '1' && line [ft_strlen(line) - 1] == '1');
}
int is_validmap(char *line, int *flag)
{
int i;
i = -1;
while (line[++i])
{
if ((line[i] != '1' && line[i] != 32 && line[i] != '0'
&& line[i] != '\n') && !(line[i] != 'W' || line[i] != 'E'
|| line[i] != 'N' || line[i] != 'S'))
return (0);
else if (line[i] != 'W' || line[i] != 'E' || line[i] != 'N'
|| line[i] != 'S')
(*flag)++;
}
return (1);
}
char *getmap(t_data *map)
{
char *tmp;
map->map = ft_strdup("");
while (map->line)
{
if (map->line[0] == '\n')
return (ft_putstr_fd(ERR_MAP_EMPTY, 2), freetl(map->map, map->line,
-1), NULL);
tmp = ft_strjoin(map->map, map->line);
ft_memfree(map->map);
ft_memfree(map->line);
map->map = ft_strdup(tmp);
ft_memfree(tmp);
map->line = get_next_line(map->fd);
}
return (map->map);
}
int read_map_(t_data *map, int count)
{
map->map = getmap(map);
if (!map->map)
return (0);
map->map2d = ft_split(map->map, '\n');
if (!map->map2d)
return (ft_memfree(map->map), 0);
ft_memfree(map->map);
if (!check_tures_space_tab(map->ture2d, count) || !parse_rgb(map->ture2d)
|| !check_dup(map) || !check_first_last_line(map->map2d)
|| !surrounded_by_one(map->map2d))
return (free2d(map->map2d), 0);
return (1);
}
void process_map(t_data *map, int *count)
{
char *tmp;
while (map->line && map->line[0] != '1' && map->line[0] != 32)
{
if (check_color_textures(map->line))
{
tmp = ft_strjoin(map->ture, map->line);
ft_memfree(map->ture);
map->ture = ft_strdup(tmp);
ft_memfree(tmp);
(*count)++;
}
ft_memfree(map->line);
map->line = get_next_line(map->fd);
}
}

View File

@@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_map_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/04 10:07:52 by aortigos #+# #+# */
/* Updated: 2025/12/04 10:32:30 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
int check_tures_space_tab(char **ture2d, int count)
{
int i;
i = -1;
if (count != 6)
return (0);
while (++i < count)
if (!is_valid_texture(ture2d[i]))
return (ft_putstr_fd("texture error\n", 2), 0);
return (1);
}
int parse_rgb(char **ture2d)
{
int i;
char *ptr;
i = 0;
while (ture2d[i])
{
ptr = ture2d[i];
while (ft_isspace(*ptr))
ptr++;
if (ptr[0] == 'F' || ptr[0] == 'C')
if (count_comma(ptr) != 2 || !check_pos_cf(ptr))
return (ft_putstr_fd("color error\n", 2), 0);
i++;
}
return (1);
}
int check_dup(t_data *m)
{
int i;
int j;
i = 0;
while (m->ture2d[i])
{
j = i + 1;
while (m->ture2d[j])
{
if (!ft_strncmp(m->ture2d[i], m->ture2d[j], 2))
return (ft_putstr_fd(ERR_MAP_DUP, 2), 0);
j++;
}
i++;
}
return (1);
}
int check_first_last_line(char **map)
{
if (!map[0])
return (ft_putstr_fd(ERR_MAP_INV, 2), 0);
if (!line_around_one(map[0]) || !line_around_one(map[ft_arraylen(map) - 1]))
return (ft_putstr_fd("one Y\n", 2), 0);
return (1);
}
int surrounded_by_one(char **map)
{
int i;
int flag;
flag = 0;
i = -1;
while (map[++i])
if (!is_surrounded(map[i]) || !is_validmap(map[i], &flag) || flag > 1)
return (ft_putstr_fd("one X\n", 2), 0);
if (flag == 0)
return (ft_putstr_fd("no position map\n", 2), 0);
return (1);
}