42_cube3D/map/parsing_body2.c

146 lines
3.2 KiB
C
Raw Normal View History

2023-05-16 13:12:54 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_body2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 16:30:55 by cchauvet #+# #+# */
/* Updated: 2023/05/16 18:59:38 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing_private.h"
int map_is_in_one_part(const char **body)
{
int y;
y = ft_tablen((const void **) body) - 1;
while (body[y][0] == '\0' || ft_contain_only(body[y], ' '))
y--;
while (y > 0)
{
if (body[y][0] == '\0' || ft_contain_only(body[y], ' '))
return (1);
y--;
}
return (0);
}
static int map_surround(const char **body)
{
int y;
int x;
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++;
}
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);
}
void get_size(const char **body, t_map *map)
{
int y;
int x;
map->size_x = 0;
y = 0;
while (body[y] != NULL)
{
x = ft_strlen(body[y]);
if (x > map->size_x)
map->size_x = x;
y++;
}
map->size_y = y;
}
int body_is_valid2(t_map *map)
{
int error;
error = get_spawn_position((const char **) map->map,
&map->ply_x, &map->ply_y);
if (error == 1)
{
ft_eprintf("map: spawn position: multiple definition");
return (0);
}
else if (error == 2)
{
ft_eprintf("map: spawn position: not defined");
return (0);
}
map->direction = map->map[(int) map->ply_y][(int) map->ply_x];
map->map[(int) map->ply_y][(int) map->ply_x] = '0';
error = map_is_in_one_part((const char **)map->map);
if (error)
{
ft_eprintf("map: splitted");
return (0);
}
if (body_contain_normal_char((const char **)map->map) == 0)
return (0);
return (1);
}
int body_is_valid(t_map *map)
{
int error;
if (map->map == NULL)
return (0);
if (body_is_valid2(map) == 0)
return (0);
error = map_surround((const char **)map->map);
if (error)
{
ft_eprintf("map: not surrounded");
return (0);
}
get_size((const char **)map->map, map);
return (1);
}