2025-06-06 18:38:21 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* map_checker.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
2025-07-17 14:11:30 +02:00
|
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
2025-06-06 18:38:21 +02:00
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
|
2025-07-23 13:31:58 +02:00
|
|
|
/* Updated: 2025/07/24 12:20:17 by kcolin ### ########.fr */
|
2025-06-06 18:38:21 +02:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
2025-07-17 14:33:55 +02:00
|
|
|
#include "../../libft/includes/libft.h"
|
|
|
|
|
#include "../utils/colors.h"
|
2025-07-22 12:03:14 +02:00
|
|
|
#include "mapdata.h"
|
2025-07-21 15:22:41 +02:00
|
|
|
#include "populate_map.h"
|
2025-07-17 14:33:55 +02:00
|
|
|
#include "checkers.h"
|
|
|
|
|
#include "setters.h"
|
2025-06-04 19:16:59 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.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);
|
2025-06-24 00:17:33 +02:00
|
|
|
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-24 10:41:50 +02:00
|
|
|
void flood_fill(t_mapdata *map, int x, int y)
|
|
|
|
|
{
|
|
|
|
|
if (map->mapflood[y][x] == '1')
|
|
|
|
|
return ;
|
2025-07-07 14:40:37 +02:00
|
|
|
if (map->mapflood[y][x] == 'z')
|
|
|
|
|
return ;
|
2025-06-24 10:41:50 +02:00
|
|
|
if (map->mapflood[y][x] == ' ' || map->mapflood[y][x] == '\n')
|
|
|
|
|
map->isvalid = false;
|
|
|
|
|
map->mapflood[y][x] = '1';
|
|
|
|
|
flood_fill(map, x + 1, y);
|
|
|
|
|
flood_fill(map, x, y + 1);
|
|
|
|
|
flood_fill(map, x - 1, y);
|
|
|
|
|
flood_fill(map, x, y - 1);
|
|
|
|
|
}
|
|
|
|
|
|
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-07-22 12:03:14 +02:00
|
|
|
return (ft_strlcpy(map->error, "Not a .cub file", ERRLEN), false);
|
2025-06-04 19:16:59 +02:00
|
|
|
fd = open(file, O_RDONLY);
|
|
|
|
|
if (fd < 0)
|
2025-07-22 12:03:14 +02:00
|
|
|
return (ft_strlcpy(map->error, "Can't open file", ERRLEN), false);
|
2025-07-21 13:09:30 +02:00
|
|
|
if (add_textures(fd, map) != 0)
|
|
|
|
|
return (close(fd), false);
|
2025-06-16 17:03:32 +02:00
|
|
|
if (map->error[0])
|
|
|
|
|
return (close(fd), false);
|
2025-07-23 13:31:58 +02:00
|
|
|
if (!populate_maps(map, fd))
|
|
|
|
|
return (close(fd), false);
|
2025-06-20 12:43:56 +02:00
|
|
|
if (!check_walls(map))
|
|
|
|
|
return (close(fd), ft_strlcpy(map->error,
|
2025-07-22 12:03:14 +02:00
|
|
|
"Map is malformed (invalid chars or missing walls)", ERRLEN),
|
2025-06-21 00:17:09 +02:00
|
|
|
false);
|
2025-06-20 19:45:40 +02:00
|
|
|
if (!check_bare_minimum(map))
|
2025-07-22 12:03:14 +02:00
|
|
|
return (close(fd), ft_strlcpy(map->error, "No player", ERRLEN), false);
|
2025-06-24 00:17:33 +02:00
|
|
|
map->isvalid = true;
|
2025-06-24 10:41:50 +02:00
|
|
|
flood_fill(map, map->startx, map->starty);
|
|
|
|
|
if (!map->isvalid)
|
2025-07-22 12:03:14 +02:00
|
|
|
return (close(fd), ft_strlcpy(map->error, "Holes in map", ERRLEN),
|
|
|
|
|
false);
|
2025-06-04 19:16:59 +02:00
|
|
|
return (true);
|
|
|
|
|
}
|