Merge branch 'master' of git.sr.ht:~xamora/cube3D

This commit is contained in:
Etienne Rey-bethbeder
2023-05-05 13:29:36 +02:00
19 changed files with 333 additions and 164 deletions

View File

@ -12,6 +12,7 @@ typedef struct s_map
long long color_top;
long long spawn_x;
long long spawn_y;
char direction;
} t_map;
#endif

View File

@ -1,12 +1,89 @@
#include "./parsing_private.h"
#include "map.h"
#include <stddef.h>
static ssize_t get_nb_line(const char *path)
{
int fd;
char readed[1];
size_t i;
fd = open(path, O_RDONLY);
if (fd == -1)
return (-1);
i = 1;
while (read(fd, readed, 1))
{
if (readed[0] == '\n')
i++;
}
close(fd);
return (i);
}
static char **read_map(const char *path)
{
int fd;
size_t i;
char **map;
map = malloc(sizeof(char *) * (get_nb_line(path) + 1));
if (map == NULL)
return (NULL);
fd = open(path, O_RDONLY);
if (fd == -1)
{
free(map);
return (NULL);
}
i = 0;
map[i] = get_next_line(fd);
while (map[i] != NULL)
{
map[i][ft_strlen(map[i]) - 1] = '\0';
i++;
map[i] = get_next_line(fd);
}
return (map);
}
t_map *map_parsing(const char *path)
{
char **file_content;
char ***header;
char **body;
size_t header_size;
t_map *map;
if (parsing_meta(path))
return (NULL);
map = malloc(sizeof(t_map));
if (map == NULL)
return (NULL);
(void)path;
file_content = read_map(path);
if (file_content == NULL)
{
ft_eprintf("map: file error");
return (NULL);
}
header = get_header((const char **) file_content, &header_size);
if (header_is_valid(header, map) == 0)
{
ft_freer_tab_ultimate(1, file_content);
ft_freer_ultimate(1, map);
return (NULL);
}
//ici aussi header;
body = get_body((const char **) file_content, header_size);
ft_freer_tab_ultimate(1, file_content);
if (body_is_valid((const char **) body, map) == 0)
{
ft_freer_ultimate(1, map);
// faudra free
return (NULL);
}
map->map = body;
map->direction = map->map[map->spawn_y][map->spawn_x];
map->map[map->spawn_y][map->spawn_x] = '0';
return (map);
}

View File

@ -63,7 +63,7 @@ static int map_is_in_one_part(const char **body)
{
size_t y;
y = ft_tablen((const void **) body);
y = ft_tablen((const void **) body) - 1;
while (body[y][0] == '\0' || ft_contain_only(body[y], ' '))
y--;
while (y > 0)
@ -75,35 +75,14 @@ static int map_is_in_one_part(const char **body)
return (0);
}
static int map_surround_check(char **body, size_t x, size_t y)
{
if (ft_is_in(";1", body[y][x]))
return (0);
if (ft_is_in(" ", body[y][x]))
return (1);
if (y == 0 || body[y] == NULL)
return (1);
if (x == 0 || body[y][x] == '\0')
return (1);
if (map_surround_check(body, x + 1, y))
return (1);
if (map_surround_check(body, x - 1, y))
return (1);
if (map_surround_check(body, x, y + 1))
return (1);
if (map_surround_check(body, x, y - 1))
return (1);
return (0);
}
static int map_surround(const char **body, t_map *map)
static char **map_cpy(const char **body)
{
size_t i;
char **copy;
copy = malloc((ft_tablen((const void **) body) + 1) * sizeof(char *));
if (copy == NULL)
return (1);
return (NULL);
i = 0;
while (body[i] != NULL)
{
@ -111,46 +90,100 @@ static int map_surround(const char **body, t_map *map)
if (copy[i] == NULL)
{
ft_cancel((void **) copy, i);
return (1);
return (NULL);
}
i++;
}
copy[i] = NULL;
if (map_surround_check(copy, map->spawn_x, map->spawn_y))
{
ft_freer_tab_ultimate(1, copy);
return (copy);
}
static int map_surround(const char **body)
{
char **copy;
size_t y;
size_t x;
copy = map_cpy(body);
if (copy == NULL)
return (1);
y = 0;
while (body[y] != NULL)
{
x = 0;
while (body[y][x] != '\0')
{
if (body[y][x] == '0')
{
if (body[y + 1] == NULL || (y == 0)
|| (body[y][x + 1] == '\0') || (x == 0)
|| (body[y + 1][x] == ' ' || body[y + 1][x] == '\0')
|| (body[y - 1][x] == ' ' || body[y - 1][x] == '\0')
|| (body[y][x - 1] == ' ' || body[y][x - 1] == '\0')
|| (body[y][x + 1] == ' ' || body[y][x + 1] == '\0'))
return (1);
}
x++;
}
y++;
}
ft_freer_tab_ultimate(1, copy);
return (0);
}
static int body_contain_normal_char(const char **body)
{
unsigned int y;
unsigned int x;
y = 0;
while (body[y] != NULL)
{
x = 0;
while (body[y][x] != '\0')
{
if (!ft_is_in(" 01NEWS", body[y][x]))
{
ft_eprintf("map: %c: invalid character at rows=%u, colums=%u", body[y][x], y, x);
return (0);
}
x++;
}
y++;
}
return (1);
}
int body_is_valid(const char **body, t_map *map)
{
int error;
if (body == NULL)
return (0);
error = get_spawn_position(body, &map->spawn_x, &map->spawn_y);
if (error == 1)
{
ft_eprintf("map: spawn position: multiple definition");
return (1);
return (0);
}
else if (error == 2)
{
ft_eprintf("map: spawn position: not define");
return (1);
return (0);
}
error = map_surround(body, map);
error = map_surround(body);
if (error)
{
ft_eprintf("map: not surrounded");
return (1);
return (0);
}
error = map_is_in_one_part(body);
if (error)
{
ft_eprintf("map: splitted");
return (1);
return (0);
}
return (0);
if (body_contain_normal_char(body) == 0)
return (0);
return (1);
}

View File

@ -33,6 +33,8 @@ int header_is_valid(char ***header, t_map *map)
int token;
if (header == NULL)
return (1);
map->color_bot = -1;
map->color_top = -1;
i = 0;

View File

@ -5,17 +5,13 @@ static int name_check(const char *path)
size_t len;
len = ft_strlen(path);
if (len < 4)
if (len < 4
|| (ft_strcmp(".cub", path + len - 4) != 0))
{
ft_eprintf("map error: name doesn't finished by .cub");
ft_eprintf("map: name doesn't finished by .cub");
return (1);
}
if (ft_strcmp(".cub", path + len - 3) != 0)
{
ft_eprintf("map error: name doesn't finished by .cub");
return (1);
}
return (1);
return (0);
}
int parsing_meta(const char *path)