New folder structure

This commit is contained in:
2025-12-15 10:28:50 +01:00
parent 7dd0523007
commit 09f2b2ccb0
11 changed files with 35 additions and 35 deletions

94
src/map/read_map.c Normal file
View File

@@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/04 09:28:05 by aortigos #+# #+# */
/* Updated: 2025/12/15 10:27:21 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] != '0'
&& line[i] != ' ' && 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->texture2d, count) || !parse_rgb(map->texture2d)
|| !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);
}
}

89
src/map/read_map_utils.c Normal file
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/15 10:27:21 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
int check_tures_space_tab(char **texture2d, int count)
{
int i;
i = -1;
if (count != 6)
return (0);
while (++i < count)
if (!is_valid_texture(texture2d[i]))
return (ft_putstr_fd("texture error\n", 2), 0);
return (1);
}
int parse_rgb(char **texture2d)
{
int i;
char *ptr;
i = 0;
while (texture2d[i])
{
ptr = texture2d[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->texture2d[i])
{
j = i + 1;
while (m->texture2d[j])
{
if (!ft_strncmp(m->texture2d[i], m->texture2d[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);
}

114
src/map/valid_map.c Normal file
View File

@@ -0,0 +1,114 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valid_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/05 16:30:33 by aortigos #+# #+# */
/* Updated: 2025/12/15 10:27:21 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
int h_map(char **map)
{
int i;
int j;
i = 0;
while (map[i])
{
j = 0;
while (map[i][j])
{
if (map[i][j] != '1' && map [i][j] != ' ')
if (map[i][j - 1] == ' ' || map[i][j + 1] == ' ')
return (ft_putstr_fd(ERR_MAP_INV, 2), 0);
j++;
}
i++;
}
return (1);
}
int v_map(char **map)
{
int i;
int j;
i = 0;
while (map[i])
{
j = 0;
while (map[i][j])
{
if (map[i][j] != '1' && map[i][j] != ' ')
if (map[i - 1][j] == ' ' || map[i + 1][j] == ' ')
return (ft_putstr_fd(ERR_MAP_INV, 2), 0);
j++;
}
i++;
}
return (1);
}
char *fixline(char *line, int maxlen)
{
char *new;
int i;
i = 0;
new = ft_calloc(sizeof(char), (maxlen + 1));
if (!new)
return (NULL);
while (line[i])
{
new[i] = line[i];
i++;
}
while (i < maxlen)
{
new[i] = ' ';
i++;
}
return (new);
}
int getsize_line(char **map)
{
int i;
int max;
i = -1;
max = ft_strlen(map[0]);
while (map[++i])
if ((int)ft_strlen(map[i]) > max)
max = ft_strlen(map[i]);
return (max);
}
int valid_map(t_data *m)
{
int i;
int maxlen;
maxlen = getsize_line(m->map2d);
m->sq_map = ft_calloc(sizeof(char *), (ft_arraylen(m->map2d) + 1));
if (!m->sq_map)
return (0);
i = -1;
while (m->map2d[++i])
{
if (maxlen == (int)ft_strlen(m->map2d[i]))
m->sq_map[i] = ft_strdup(m->map2d[i]);
else
m->sq_map[i] = fixline(m->map2d[i], maxlen);
}
m->h_map = ft_arraylen(m->sq_map);
m->w_map = ft_strlen(m->sq_map[0]);
if (!h_map(m->sq_map) || !v_map(m->sq_map))
return (free2d(m->sq_map), free2d(m->map2d), free2d(m->texture2d), 0);
return (1);
}