This commit is contained in:
Theo Champion 2025-06-18 22:19:25 +02:00
parent 02079dfa87
commit 0627a90bc4
5 changed files with 91 additions and 17 deletions

View file

@ -6,12 +6,13 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
/* Updated: 2025/06/16 17:03:00 by tchampio ### ########.fr */
/* Updated: 2025/06/17 16:33:43 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/maputils.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "../../includes/libft.h"
@ -110,11 +111,10 @@ bool add_textures(int fd, t_mapdata *map)
line = get_next_line(fd);
}
free(line);
print_mapdata(map);
return (true);
}
bool check_filename(char *file)
bool check_filename(t_mapdata *map, char *file)
{
int filename_size;
int i;
@ -133,17 +133,76 @@ bool check_filename(char *file)
}
if (ft_strncmp(end, ".cub", 5))
return (false);
else
return (true);
map->filename = ft_strdup(file);
return (true);
}
void free_tab(char **tab)
{
int i;
i = 0;
while (tab[i])
{
free(tab[i]);
i++;
}
free(tab);
}
void get_map_height(t_mapdata *map)
{
int fd;
char **splitted;
char *line;
int i;
int count;
fd = open(map->filename, O_RDONLY);
line = get_next_line(fd);
splitted = ft_split(line, ' ');
i = 0;
count = 0;
while (splitted[i] && splitted[i][0] != '1')
{
free_tab(splitted);
free(line);
line = get_next_line(fd);
i++;
splitted = ft_split(line, ' ');
}
free_tab(splitted);
while (line)
{
free(line);
count++;
line = get_next_line(fd);
}
map->mapheight = count + 1;
close(fd);
}
void populate_maps(t_mapdata *map, int fd)
{
char *line;
int i;
get_map_height(map);
ft_printf("%d\n", map->mapheight);
map->map = malloc(sizeof(char *) * map->mapheight);
if (!map->map)
return ;
map->mapflood = malloc(sizeof(char *) * map->mapheight);
if (!map->mapflood)
return ;
line = get_next_line(fd);
ft_printf("%s", line);
i = 0;
while (line)
{
map->map[i] = ft_strdup(line);
map->mapflood[i] = ft_strdup(line);
i++;
free(line);
line = get_next_line(fd);
}
@ -152,17 +211,28 @@ void populate_maps(t_mapdata *map, int fd)
bool check_walls(int fd, t_mapdata *map)
{
bool last_first;
populate_maps(map, fd);
(void)fd;
ft_printf("%d\n", map->mapheight);
return (true);
}
void print_map(char **map)
{
int i;
i = 0;
while (map && map[i])
{
ft_printf("%s", map[i]);
i++;
}
}
bool check_cubfile(char *file, t_mapdata *map)
{
int fd;
if (!check_filename(file))
if (!check_filename(map, file))
return (ft_strlcpy(map->error, "File is not a .cub file", 25), false);
fd = open(file, O_RDONLY);
if (fd < 0)
@ -172,9 +242,12 @@ bool check_cubfile(char *file, t_mapdata *map)
"Map started before all the textures", 37), false);
if (map->error[0])
return (close(fd), false);
if (!check_walls(fd, map))
return (close(fd), ft_strlcpy(map->error,
"Map is malformed (invalid chars or missing walls)", 51), false);
close(fd);
fd = open(file, O_RDONLY);
populate_maps(map, fd);
//if (!check_walls(fd, map))
// return (close(fd), ft_strlcpy(map->error,
// "Map is malformed (invalid chars or missing walls)", 51), false);
print_map(map->map);
return (true);
}