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

@ -22,6 +22,7 @@ typedef struct s_mapdata
char *EA_texture; char *EA_texture;
int f_color; int f_color;
int c_color; int c_color;
int mapheight;
char **map; char **map;
char **mapflood; char **mapflood;
bool isvalid; bool isvalid;

View file

@ -6,12 +6,12 @@ EA ./path_to_the_east_texture
F 220,100,0 F 220,100,0
C 225,30,0 C 225,30,0
1111111111111111111111111 1111111111111111111111111
1000000000110000000000001 1000000000110000000000001
1011000001110000000000001 1011000001110000000000001
1001000000000000000000001 1001000000000000000000001
111111111011000001110000000000001 111111111011000001110000000000001
100000000011000001110111111111111 10000000001100000111011111D111111
11110111111111011100000010001 11110111111111011100000010001
11110111111111011101010010001 11110111111111011101010010001
11000000110101011100000010001 11000000110101011100000010001

View file

@ -6,12 +6,13 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */ /* 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 "../../includes/maputils.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "../../includes/libft.h" #include "../../includes/libft.h"
@ -110,11 +111,10 @@ bool add_textures(int fd, t_mapdata *map)
line = get_next_line(fd); line = get_next_line(fd);
} }
free(line); free(line);
print_mapdata(map);
return (true); return (true);
} }
bool check_filename(char *file) bool check_filename(t_mapdata *map, char *file)
{ {
int filename_size; int filename_size;
int i; int i;
@ -133,17 +133,76 @@ bool check_filename(char *file)
} }
if (ft_strncmp(end, ".cub", 5)) if (ft_strncmp(end, ".cub", 5))
return (false); return (false);
else map->filename = ft_strdup(file);
return (true); 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) void populate_maps(t_mapdata *map, int fd)
{ {
char *line; 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); line = get_next_line(fd);
ft_printf("%s", line);
i = 0;
while (line) while (line)
{ {
map->map[i] = ft_strdup(line);
map->mapflood[i] = ft_strdup(line);
i++;
free(line); free(line);
line = get_next_line(fd); 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 check_walls(int fd, t_mapdata *map)
{ {
bool last_first; (void)fd;
ft_printf("%d\n", map->mapheight);
populate_maps(map, fd);
return (true); 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) bool check_cubfile(char *file, t_mapdata *map)
{ {
int fd; int fd;
if (!check_filename(file)) if (!check_filename(map, file))
return (ft_strlcpy(map->error, "File is not a .cub file", 25), false); return (ft_strlcpy(map->error, "File is not a .cub file", 25), false);
fd = open(file, O_RDONLY); fd = open(file, O_RDONLY);
if (fd < 0) if (fd < 0)
@ -172,9 +242,12 @@ bool check_cubfile(char *file, t_mapdata *map)
"Map started before all the textures", 37), false); "Map started before all the textures", 37), false);
if (map->error[0]) if (map->error[0])
return (close(fd), false); 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); 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); return (true);
} }

BIN
vgcore.11143 Normal file

Binary file not shown.

BIN
vgcore.83143 Normal file

Binary file not shown.