2025-06-06 18:38:21 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* map_checker.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
|
2025-06-20 12:43:56 +02:00
|
|
|
/* Updated: 2025/06/20 12:43:33 by tchampio ### ########.fr */
|
2025-06-06 18:38:21 +02:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
2025-06-04 19:16:59 +02:00
|
|
|
#include "../../includes/maputils.h"
|
|
|
|
|
#include <fcntl.h>
|
2025-06-18 22:19:25 +02:00
|
|
|
#include <stdlib.h>
|
2025-06-04 19:16:59 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include "../../includes/libft.h"
|
|
|
|
|
|
2025-06-04 21:50:33 +02:00
|
|
|
void print_mapdata(const t_mapdata *data)
|
|
|
|
|
{
|
|
|
|
|
ft_printf(BOLD CYAN "=== Map Data ===\n" RESET);
|
2025-06-06 18:38:21 +02:00
|
|
|
ft_printf(BOLD "Filename: " RESET "%s\n", data->filename);
|
|
|
|
|
ft_printf(BOLD "NO Texture: " RESET "%s\n", data->NO_texture);
|
|
|
|
|
ft_printf(BOLD "SO Texture: " RESET "%s\n", data->SO_texture);
|
|
|
|
|
ft_printf(BOLD "WE Texture: " RESET "%s\n", data->WE_texture);
|
|
|
|
|
ft_printf(BOLD "EA Texture: " RESET "%s\n", data->EA_texture);
|
2025-06-16 17:03:32 +02:00
|
|
|
ft_printf(BOLD "F color: " RESET "%x\n", data->f_color);
|
|
|
|
|
ft_printf(BOLD "C color: " RESET "%x\n", data->c_color);
|
2025-06-04 21:50:33 +02:00
|
|
|
ft_printf(BOLD "Validity: " RESET);
|
|
|
|
|
if (data->isvalid)
|
|
|
|
|
ft_printf(GREEN "VALID\n" RESET);
|
|
|
|
|
else
|
|
|
|
|
ft_printf(RED "INVALID\n" RESET);
|
|
|
|
|
if (!data->isvalid && data->error[0] != '\0')
|
|
|
|
|
{
|
|
|
|
|
ft_printf(BOLD RED "Error: " RESET "%s\n", data->error);
|
|
|
|
|
}
|
|
|
|
|
ft_printf(CYAN "=================\n" RESET);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-16 17:03:32 +02:00
|
|
|
unsigned long set_color(const char *s, t_mapdata *map)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
unsigned long finalcolor;
|
|
|
|
|
char **tab;
|
|
|
|
|
int rgb[100];
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
tab = ft_split(s, ',');
|
|
|
|
|
while (tab[i])
|
|
|
|
|
{
|
|
|
|
|
if (i > 2)
|
|
|
|
|
ft_strlcpy(map->error, "too many colors", 16);
|
|
|
|
|
rgb[i] = ft_atoi(tab[i]);
|
|
|
|
|
free(tab[i]);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
free(tab);
|
|
|
|
|
if (rgb[0] < 0 || rgb[0] > 255
|
|
|
|
|
|| rgb[1] < 0 || rgb[1] > 255
|
|
|
|
|
|| rgb[2] < 0 || rgb[2] > 255)
|
|
|
|
|
return (ft_strlcpy(map->error, "Invalid color", 14), 0);
|
|
|
|
|
finalcolor = ((rgb[0] & 0xff) << 16)
|
|
|
|
|
+ ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff);
|
|
|
|
|
return (finalcolor);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-04 21:50:33 +02:00
|
|
|
bool set_textures(char *line, t_mapdata *map)
|
|
|
|
|
{
|
|
|
|
|
char **tab;
|
2025-06-06 18:38:21 +02:00
|
|
|
int i;
|
2025-06-04 21:50:33 +02:00
|
|
|
|
|
|
|
|
tab = ft_split(line, ' ');
|
|
|
|
|
i = 0;
|
2025-06-06 18:38:21 +02:00
|
|
|
if (tab[0][0] == '1')
|
|
|
|
|
return (false);
|
2025-06-04 21:50:33 +02:00
|
|
|
if (tab[0] && tab[1])
|
|
|
|
|
{
|
|
|
|
|
if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3))
|
|
|
|
|
map->NO_texture = ft_strdup(tab[1]);
|
|
|
|
|
if (!ft_strncmp(tab[0], "SO", 3) || !ft_strncmp(tab[0], "so", 3))
|
|
|
|
|
map->SO_texture = ft_strdup(tab[1]);
|
|
|
|
|
if (!ft_strncmp(tab[0], "WE", 3) || !ft_strncmp(tab[0], "we", 3))
|
|
|
|
|
map->WE_texture = ft_strdup(tab[1]);
|
|
|
|
|
if (!ft_strncmp(tab[0], "EA", 3) || !ft_strncmp(tab[0], "ea", 3))
|
|
|
|
|
map->EA_texture = ft_strdup(tab[1]);
|
2025-06-16 17:03:32 +02:00
|
|
|
if (!ft_strncmp(tab[0], "F", 2) || !ft_strncmp(tab[0], "f", 2))
|
|
|
|
|
map->f_color = set_color(tab[1], map);
|
|
|
|
|
if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2))
|
|
|
|
|
map->c_color = set_color(tab[1], map);
|
2025-06-04 21:50:33 +02:00
|
|
|
}
|
|
|
|
|
while (tab[i])
|
|
|
|
|
free(tab[i++]);
|
2025-06-16 17:03:32 +02:00
|
|
|
return (free(tab), true);
|
2025-06-04 21:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool add_textures(int fd, t_mapdata *map)
|
|
|
|
|
{
|
|
|
|
|
char *line;
|
2025-06-16 17:03:32 +02:00
|
|
|
int set_lines;
|
2025-06-04 21:50:33 +02:00
|
|
|
|
|
|
|
|
line = get_next_line(fd);
|
2025-06-08 20:29:35 +02:00
|
|
|
set_lines = 0;
|
2025-06-16 17:03:32 +02:00
|
|
|
while (line && set_lines < 6)
|
2025-06-04 21:50:33 +02:00
|
|
|
{
|
2025-06-06 18:38:21 +02:00
|
|
|
if (!set_textures(line, map))
|
|
|
|
|
return (free(line), false);
|
2025-06-08 20:29:35 +02:00
|
|
|
else
|
|
|
|
|
set_lines++;
|
2025-06-04 21:50:33 +02:00
|
|
|
free(line);
|
|
|
|
|
line = get_next_line(fd);
|
|
|
|
|
}
|
2025-06-16 17:03:32 +02:00
|
|
|
free(line);
|
2025-06-04 21:50:33 +02:00
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 22:19:25 +02:00
|
|
|
bool check_filename(t_mapdata *map, char *file)
|
2025-06-04 19:16:59 +02:00
|
|
|
{
|
2025-06-06 18:38:21 +02:00
|
|
|
int filename_size;
|
|
|
|
|
int i;
|
|
|
|
|
int j;
|
2025-06-04 19:16:59 +02:00
|
|
|
char end[5];
|
|
|
|
|
|
|
|
|
|
filename_size = ft_strlen(file);
|
|
|
|
|
i = filename_size;
|
|
|
|
|
j = 4;
|
|
|
|
|
ft_bzero(end, 5);
|
|
|
|
|
while (i > (filename_size - 5))
|
|
|
|
|
{
|
|
|
|
|
end[j] = file[i];
|
|
|
|
|
i--;
|
|
|
|
|
j--;
|
|
|
|
|
}
|
|
|
|
|
if (ft_strncmp(end, ".cub", 5))
|
|
|
|
|
return (false);
|
2025-06-18 22:19:25 +02:00
|
|
|
map->filename = ft_strdup(file);
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-20 12:43:56 +02:00
|
|
|
void free_tab_length(char **tab, int length)
|
2025-06-18 22:19:25 +02:00
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
2025-06-20 12:07:03 +02:00
|
|
|
if (!tab)
|
|
|
|
|
return ;
|
2025-06-18 22:19:25 +02:00
|
|
|
i = 0;
|
2025-06-20 12:43:56 +02:00
|
|
|
while (i < length)
|
2025-06-18 22:19:25 +02:00
|
|
|
{
|
|
|
|
|
free(tab[i]);
|
2025-06-20 12:43:56 +02:00
|
|
|
++i;
|
2025-06-18 22:19:25 +02:00
|
|
|
}
|
|
|
|
|
free(tab);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-20 12:43:56 +02:00
|
|
|
int copy_old_map(t_mapdata *map, char **newmap, char **newmapflood, int length)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
if (!map->map)
|
|
|
|
|
return (0);
|
|
|
|
|
while (i < length)
|
|
|
|
|
{
|
|
|
|
|
newmap[i] = ft_strdup(map->map[i]);
|
|
|
|
|
newmapflood[i] = ft_strdup(map->mapflood[i]);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
return (i);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-20 12:07:03 +02:00
|
|
|
void add_map_line(const char *line, t_mapdata *map)
|
2025-06-18 22:19:25 +02:00
|
|
|
{
|
2025-06-20 12:07:03 +02:00
|
|
|
static int length = 0;
|
|
|
|
|
char **newmap;
|
|
|
|
|
char **newmapflood;
|
|
|
|
|
int i;
|
2025-06-18 22:19:25 +02:00
|
|
|
|
2025-06-20 12:43:56 +02:00
|
|
|
newmap = ft_calloc(sizeof(char *), (length + 1));
|
2025-06-20 12:07:03 +02:00
|
|
|
if (!newmap)
|
|
|
|
|
return ;
|
2025-06-20 12:43:56 +02:00
|
|
|
newmapflood = ft_calloc(sizeof(char *), (length + 1));
|
2025-06-20 12:07:03 +02:00
|
|
|
if (!newmapflood)
|
|
|
|
|
return ;
|
2025-06-20 12:43:56 +02:00
|
|
|
i = copy_old_map(map, newmap, newmapflood, length);
|
2025-06-20 12:07:03 +02:00
|
|
|
newmap[i] = ft_strdup(line);
|
|
|
|
|
newmapflood[i] = ft_strdup(line);
|
2025-06-20 12:43:56 +02:00
|
|
|
free_tab_length(map->map, length);
|
|
|
|
|
free_tab_length(map->mapflood, length);
|
|
|
|
|
map->mapheight = ++length;
|
2025-06-20 12:07:03 +02:00
|
|
|
map->map = newmap;
|
|
|
|
|
map->mapflood = newmapflood;
|
2025-06-04 19:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-16 17:03:32 +02:00
|
|
|
void populate_maps(t_mapdata *map, int fd)
|
|
|
|
|
{
|
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
|
|
line = get_next_line(fd);
|
|
|
|
|
while (line)
|
|
|
|
|
{
|
2025-06-20 12:07:03 +02:00
|
|
|
if (line[0] != '\n')
|
|
|
|
|
add_map_line(line, map);
|
2025-06-16 17:03:32 +02:00
|
|
|
free(line);
|
|
|
|
|
line = get_next_line(fd);
|
|
|
|
|
}
|
|
|
|
|
free(line);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-20 12:43:56 +02:00
|
|
|
bool check_walls(t_mapdata *map)
|
2025-06-16 17:03:32 +02:00
|
|
|
{
|
2025-06-20 12:43:56 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
while (i < map->mapheight)
|
|
|
|
|
{
|
|
|
|
|
ft_printf("line: %s", map->map[i]);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
2025-06-16 17:03:32 +02:00
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 22:19:25 +02:00
|
|
|
void print_map(char **map)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
while (map && map[i])
|
|
|
|
|
{
|
2025-06-20 12:07:03 +02:00
|
|
|
ft_printf("line2: %s", map[i]);
|
2025-06-18 22:19:25 +02:00
|
|
|
i++;
|
|
|
|
|
}
|
2025-06-20 12:43:56 +02:00
|
|
|
ft_printf("(%p) %s", map[i], map[i]);
|
2025-06-18 22:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-04 19:16:59 +02:00
|
|
|
bool check_cubfile(char *file, t_mapdata *map)
|
|
|
|
|
{
|
|
|
|
|
int fd;
|
|
|
|
|
|
2025-06-18 22:19:25 +02:00
|
|
|
if (!check_filename(map, file))
|
2025-06-04 19:16:59 +02:00
|
|
|
return (ft_strlcpy(map->error, "File is not a .cub file", 25), false);
|
|
|
|
|
fd = open(file, O_RDONLY);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
return (ft_strlcpy(map->error, "Can't open file", 16), false);
|
2025-06-06 18:38:21 +02:00
|
|
|
if (!add_textures(fd, map))
|
|
|
|
|
return (close(fd), ft_strlcpy(map->error,
|
|
|
|
|
"Map started before all the textures", 37), false);
|
2025-06-16 17:03:32 +02:00
|
|
|
if (map->error[0])
|
|
|
|
|
return (close(fd), false);
|
2025-06-18 22:19:25 +02:00
|
|
|
populate_maps(map, fd);
|
2025-06-20 12:43:56 +02:00
|
|
|
if (!check_walls(map))
|
|
|
|
|
return (close(fd), ft_strlcpy(map->error,
|
|
|
|
|
"Map is malformed (invalid chars or missing walls)", 51), false);
|
|
|
|
|
//if (!flood_fill(map->mapflood))
|
2025-06-18 22:19:25 +02:00
|
|
|
// return (close(fd), ft_strlcpy(map->error,
|
2025-06-20 12:43:56 +02:00
|
|
|
// "Map is not possible (flood fill failed)", 41), false);
|
2025-06-04 19:16:59 +02:00
|
|
|
return (true);
|
|
|
|
|
}
|