This commit is contained in:
2024-01-14 23:36:45 +01:00
parent 2fd2c96579
commit b378e60269
44 changed files with 3041 additions and 7203 deletions

110
parsing/ft_map_check.c Normal file
View File

@@ -0,0 +1,110 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:49:45 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 20:02:42 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
int is_map_closed(char *map, int maxWidth, int height)
{
if (!check_borders(map, maxWidth, height))
return (0);
if (!check_interior(map, maxWidth, height))
return (0);
return (1);
}
int check_line_border(char *line, int lineLength, int y)
{
if (lineLength == 0)
return (1);
if (line[0] != '1')
return (0);
if (line[lineLength - 1] != '1')
return (0);
return (1);
}
int check_horizontal_borders(char *map, int maxWidth, int height)
{
int x;
x = 0;
while (x < maxWidth)
{
if (map[x] != '1' && map[x] != ' ')
{
printf("Border issue detected at top edge at (%d,0)\n", x);
return (0);
}
if (map[(height - 1) * maxWidth + x] != '1'
&& map[(height - 1) * maxWidth + x] != ' ')
return (0);
x++;
}
return (1);
}
int check_vertical_borders(char *map, int maxWidth, int height)
{
int x;
int columnheight;
x = 0;
while (x < maxWidth)
{
columnheight = 0;
while (columnheight < height && map[columnheight * maxWidth + x] != ' ')
{
columnheight++;
}
if (columnheight > 0)
{
if (map[x] != '1')
{
printf("Border issue detected at top of column at (%d,0)\n", x);
return (0);
}
if (map[(columnheight - 1) * maxWidth + x] != '1')
return (0);
}
x++;
}
return (1);
}
int check_borders(char *map, int maxWidth, int height)
{
int y;
int linelength;
int x;
y = 0;
while (y < height)
{
linelength = 0;
x = 0;
while (x < maxWidth && map[y * maxWidth + x] != ' ')
{
linelength++;
x++;
}
if (!check_line_border(&map[y * maxWidth], linelength, y))
{
return (0);
}
y++;
}
if (!check_horizontal_borders(map, maxWidth, height))
{
return (0);
}
return (check_vertical_borders(map, maxWidth, height));
}

View File

@@ -0,0 +1,99 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_dimensions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:17:19 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:19:14 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void update_max_dimensions(t_map_params *params)
{
(*(params->height))++;
if (*(params->currentWidth) > *(params->maxWidth))
{
*(params->maxWidth) = *(params->currentWidth);
}
*(params->currentWidth) = 0;
*(params->isNewLine) = 1;
}
void process_character(t_map_params *params, int *i)
{
if (params->buffer[*i] == '\n')
{
update_max_dimensions(params);
}
else if (params->buffer[*i] != '\r')
{
(*(params->currentWidth))++;
*(params->isNewLine) = 0;
}
(*i)++;
}
void get_map_dimensions(t_map_params *params)
{
int i;
int currentwidth;
int isnewline;
i = 0;
currentwidth = 0;
isnewline = 1;
*(params->maxWidth) = 0;
*(params->height) = 0;
params->currentWidth = &currentwidth;
params->isNewLine = &isnewline;
while (i < params->length)
process_character(params, &i);
if (!*(params->isNewLine))
update_max_dimensions(params);
printf("Map dimensions: maxWidth=%d, height=%d\n",
*(params->maxWidth), *(params->height));
}
void fill_map_space(t_structure_map *map_info, int maxWidth, int height)
{
int i;
int totalsize;
totalsize = maxWidth * height;
i = 0;
while (i < totalsize)
{
map_info->map[i] = ' ';
i++;
}
}
void copy_map_data(t_map_params *params)
{
int x;
int y;
int i;
x = 0;
y = 0;
i = 0;
while (i < params->length)
{
if (params->buffer[i] == '\n')
{
y++;
x = 0;
}
else if (params->buffer[i] != '\r' && x < *params->maxWidth)
{
params->map_info->map[y * *params->maxWidth + x]
= params->buffer[i];
x++;
}
i++;
}
}

96
parsing/ft_parsing.c Normal file
View File

@@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:43:46 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:17:53 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
bool init_map_info(t_structure_map *map_info, int maxWidth, int height)
{
map_info->mapX = maxWidth;
map_info->mapY = height;
map_info->map = malloc(maxWidth * height);
if (!map_info->map)
{
perror("Failed to allocate memory for map");
return (false);
}
fill_map_space(map_info, maxWidth, height);
return (true);
}
bool copy_map_data_and_check(t_map_params *params)
{
copy_map_data(params);
if (!is_map_closed(params->map_info->map,
*params->maxWidth, *params->height))
{
printf("Map is not closed!\n");
free(params->map_info->map);
return (false);
}
return (true);
}
bool read_and_verify_map(const char *filename, char **buffer, int *length)
{
*length = 0;
*buffer = read_map(filename, length);
if (!(*buffer))
{
printf("Failed to read the map file.\n");
return (false);
}
return (true);
}
bool process_map(const char *buffer, int length, t_structure_map *map_info)
{
int maxwidth;
int height;
t_map_params map_params;
maxwidth = 0;
height = 0;
map_params.map_info = map_info;
map_params.buffer = buffer;
map_params.length = length;
map_params.maxWidth = &maxwidth;
map_params.height = &height;
map_params.currentWidth = NULL;
map_params.isNewLine = NULL;
get_map_dimensions(&map_params);
if (maxwidth <= 0 || height <= 0)
{
printf("Invalid map dimensions: maxWidth=%d, height=%d\n",
maxwidth, height);
return (false);
}
if (!init_map_info(map_info, maxwidth, height))
{
return (false);
}
return (copy_map_data_and_check(&map_params));
}
bool parse_map(const char *filename, t_structure_map *map_info)
{
char *buffer;
int length;
bool result;
if (!read_and_verify_map(filename, &buffer, &length))
{
return (false);
}
result = process_map(buffer, length, map_info);
free(buffer);
return (result);
}

88
parsing/ft_read_map.c Normal file
View File

@@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_read_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 19:28:43 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 20:02:52 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
char *read_map(const char *filename, int *length)
{
char *buffer;
FILE *file;
file = fopen(filename, "r");
if (!file)
{
perror("Error opening file");
return (NULL);
}
fseek(file, 0, SEEK_END);
*length = ftell(file);
fseek(file, 0, SEEK_SET);
buffer = malloc(*length + 1);
if (!buffer)
{
perror("Error allocating memory for map");
fclose(file);
return (NULL);
}
fread(buffer, 1, *length, file);
buffer[*length] = '\0';
fclose(file);
return (buffer);
}
static int check_line(char *map, int maxWidth, int y, int linelength)
{
int x;
char c;
x = 1;
while (x < linelength - 1)
{
c = map[y * maxWidth + x];
if (c == ' ' && (map[y * maxWidth + (x - 1)] != '1'
|| map[y * maxWidth + (x + 1)] != '1'
|| (y > 0 && map[(y - 1) * maxWidth + x] != '1')
|| (y < maxWidth - 1 && map[(y + 1) * maxWidth + x] != '1')))
return (0);
x++;
}
return (1);
}
static int check_row(char *map, int maxWidth, int y)
{
int x;
int linelength;
x = 0;
linelength = 0;
while (x < maxWidth && map[y * maxWidth + x] != ' ')
{
linelength++;
x++;
}
return (check_line(map, maxWidth, y, linelength));
}
int check_interior(char *map, int maxWidth, int height)
{
int y;
y = 0;
while (y < height)
{
if (!check_row(map, maxWidth, y))
return (0);
y++;
}
return (1);
}