125 lines
2.9 KiB
C
125 lines
2.9 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* pars_map_3.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/11/12 20:07:42 by lfirmin #+# #+# */
|
|
/* Updated: 2024/11/12 20:07:42 by lfirmin ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../include/so_long.h"
|
|
|
|
int have_require_item(t_data *data)
|
|
{
|
|
int i;
|
|
int j;
|
|
|
|
i = 1;
|
|
while (data->map[i])
|
|
{
|
|
j = 1;
|
|
while (data->map[i][j])
|
|
{
|
|
if (data->map[i][j] == 'E')
|
|
data->e++;
|
|
if (data->map[i][j] == 'P')
|
|
data->p++;
|
|
if (data->map[i][j] == 'C')
|
|
data->colect++;
|
|
j++;
|
|
}
|
|
i++;
|
|
}
|
|
if (data->e != 1 || data->p != 1)
|
|
return (ft_printf_fd(2, "Error : The number of E/P is invalid\n"), 1);
|
|
if (data->colect < 1)
|
|
return (ft_printf_fd(2, \
|
|
"Error : There are no collectables\n"), 1);
|
|
return (0);
|
|
}
|
|
|
|
int pars_map(t_data *data, char *file)
|
|
{
|
|
if (is_ber(file) == 1)
|
|
return (clean_data(data), 1);
|
|
if (get_map(file, data) == 1)
|
|
return (clean_data(data), 1);
|
|
if (check_char(data) == 1)
|
|
return (clean_data(data), 1);
|
|
if (check_rectangle(data) == 1)
|
|
return (clean_data(data), 1);
|
|
if (check_close(data) == 1)
|
|
return (clean_data(data), 1);
|
|
if (have_require_item(data) == 1)
|
|
return (clean_data(data), 1);
|
|
if (check_playable(data) == 1)
|
|
return (ft_printf_fd(2, \
|
|
"Error : Map not playable\n"), clean_data(data), 1);
|
|
return (0);
|
|
}
|
|
|
|
int check_rectangle(t_data *data)
|
|
{
|
|
size_t len;
|
|
int i;
|
|
|
|
len = ft_strlen(data->map[0]);
|
|
i = 1;
|
|
while (data->map[i])
|
|
{
|
|
if (ft_strlen(data->map[i]) != len)
|
|
return (ft_printf_fd(2, "Error : Invalid map format\n"), 1);
|
|
i++;
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
int is_ber(char *file)
|
|
{
|
|
size_t len;
|
|
char *ext;
|
|
int i;
|
|
|
|
ext = ".ber";
|
|
if (!file)
|
|
return (ft_printf_fd(2, "Error: No file provided\n"), 1);
|
|
len = ft_strlen(file);
|
|
if (len < 4)
|
|
return (ft_printf_fd(2, "Error: Invalid file name\n"), 1);
|
|
i = 0;
|
|
while (i < 4)
|
|
{
|
|
if (file[len - 4 + i] != ext[i])
|
|
return (ft_printf_fd(2, "Error: File type isn't .ber\n"), 1);
|
|
i++;
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
int check_char(t_data *data)
|
|
{
|
|
int i;
|
|
int j;
|
|
char c;
|
|
|
|
i = 0;
|
|
c = 0;
|
|
while (data->map[i])
|
|
{
|
|
j = 0;
|
|
while (data->map[i][j] && data->map[i][j] != '\n')
|
|
{
|
|
c = data->map[i][j];
|
|
if (c != 'E' && c != 'P' && c != 'C' && c != '0' && c != '1')
|
|
return (ft_printf_fd(2, \
|
|
"Error : invalid char -> '%c'\n", c), 1);
|
|
j++;
|
|
}
|
|
i++;
|
|
}
|
|
return (0);
|
|
}
|