This commit is contained in:
2024-01-15 22:48:47 +01:00
parent b1d117f94e
commit 5ad918c85c
872 changed files with 128224 additions and 0 deletions

View File

@@ -0,0 +1,160 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_find_map_start.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 20:04:23 by fgras-ca #+# #+# */
/* Updated: 2024/01/15 22:38:37 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
bool parse_texture_line(const char *line, t_texture *textures)
{
char direction[3];
char path[MAX_LINE_LENGTH];
int i = 0;
int j = 0;
// Extraction de la direction (NO, SO, etc.)
while (line[i] != ' ' && line[i] != '\0' && i < 2)
{
direction[i] = line[i];
i++;
}
direction[i] = '\0';
// Saut des espaces
while (line[i] == ' ' && line[i] != '\0')
i++;
// Extraction du chemin
while (line[i] != ' ' && line[i] != '\0' && j < MAX_LINE_LENGTH - 1)
{
path[j] = line[i];
i++;
j++;
}
path[j] = '\0';
// Assignation à la structure
if (strcmp(direction, "NO") == 0)
textures->north = strdup(path);
else if (strcmp(direction, "SO") == 0)
textures->south = strdup(path);
else if (strcmp(direction, "WE") == 0)
textures->west = strdup(path);
else if (strcmp(direction, "EA") == 0)
textures->east = strdup(path);
return (true);
}
bool parse_color_line(const char *line, unsigned int *color)
{
int r = 0, g = 0, b = 0;
int i = 0;
// Extraction des composantes de couleur
while (line[i] != ',' && line[i] != '\0')
{
r = r * 10 + (line[i] - '0');
i++;
}
i++; // Passer la virgule
while (line[i] != ',' && line[i] != '\0')
{
g = g * 10 + (line[i] - '0');
i++;
}
i++; // Passer la virgule
while (line[i] != ' ' && line[i] != '\0')
{
b = b * 10 + (line[i] - '0');
i++;
}
*color = (r << 16) | (g << 8) | b;
return (true);
}
bool load_cub_file(const char *filename, t_texture *textures, t_structure_map *map_info)
{
int fd;
char line[MAX_LINE_LENGTH];
char *map_buffer = NULL;
int map_length = 0;
int bytes_read;
bool is_map_started = false;
// Ouvrir le fichier
fd = open(filename, O_RDONLY);
if (fd < 0)
{
perror("Error opening file");
return (false);
}
while ((bytes_read = read(fd, line, MAX_LINE_LENGTH - 1)) > 0)
{
line[bytes_read] = '\0'; // Assurer que la ligne est terminée par un caractère nul
printf("Ligne lue : %s\n", line); // Log pour vérifier chaque ligne lue
if (!is_map_started)
{
// Chercher si la ligne actuelle est le début de la carte
if (line[0] == ' ' || line[0] == '1' || line[0] == '0')
{
is_map_started = true;
}
else
{
// Continuer à analyser les textures et couleurs
if (line[0] == 'F' || line[0] == 'C')
parse_color_line(line + 1, line[0] == 'F' ? &textures->floor_color : &textures->ceil_color);
else
parse_texture_line(line, textures);
continue; // Passer au tour suivant de la boucle
}
}
// Si la carte a commencé, ajouter la ligne à map_buffer
if (is_map_started)
{
map_buffer = realloc(map_buffer, map_length + bytes_read + 1);
if (!map_buffer)
{
perror("Error reallocating memory for map buffer");
close(fd);
return (false);
}
memcpy(map_buffer + map_length, line, bytes_read);
map_length += bytes_read;
map_buffer[map_length] = '\0'; // Assurer que la chaîne est terminée
printf("Carte stockée :\n%s\n", map_buffer); // Log pour voir le contenu final de la carte
printf("Longueur de la carte stockée : %d\n", map_length); // Log pour voir la longueur de la carte stockée
}
}
close(fd);
// Traiter la carte stockée en mémoire
if (map_buffer)
{
printf("Calling parse_map with map content...\n");
bool result = parse_map(map_buffer, map_length, map_info);
printf("Result of parse_map: %s\n", result ? "Success" : "Failure");
free(map_buffer); // Libérer la mémoire allouée pour map_buffer
return result;
}
else
{
printf("No map data found in file.\n");
return (false);
}
}

Binary file not shown.

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));
}

Binary file not shown.

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/15 22:37:27 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++;
}
}

Binary file not shown.

View File

@@ -0,0 +1,114 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:43:46 by fgras-ca #+# #+# */
/* Updated: 2024/01/15 22:22:00 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 *map_content, int length, t_structure_map *map_info)
{
t_map_params map_params;
int max_width = 0;
int height = 0;
printf("Received map content (length = %d): \n%s\n", length, map_content);
// Initialisation de la structure de paramètres pour le traitement de la carte
map_params.map_info = map_info;
map_params.buffer = map_content;
map_params.length = length;
map_params.maxWidth = &max_width;
map_params.height = &height;
map_params.currentWidth = NULL;
map_params.isNewLine = NULL;
// Obtenir les dimensions de la carte
get_map_dimensions(&map_params);
if (max_width <= 0 || height <= 0)
{
printf("Invalid map dimensions: maxWidth=%d, height=%d\n", max_width, height);
return (false);
}
// Initialiser les informations de la carte
if (!init_map_info(map_info, max_width, height))
{
return (false);
}
// Copier les données de la carte et vérifier si la carte est fermée
return copy_map_data_and_check(&map_params);
}

Binary file not shown.

View File

@@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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/15 21:48:46 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
char *read_map(const char *filename, int *length)
{
int file;
char buffer[1024]; // Taille de tampon temporaire pour la lecture
int total_length = 0;
int bytes_read;
char *map_content;
// Ouvrir le fichier pour la première lecture
file = open(filename, O_RDONLY);
if (file < 0)
{
perror("Error opening file");
return (NULL);
}
// Lire le fichier pour déterminer sa taille
while ((bytes_read = read(file, buffer, sizeof(buffer))) > 0)
total_length += bytes_read;
// Fermer le fichier après la première lecture
close(file);
// Allouer de la mémoire pour le contenu de la carte
map_content = (char *)malloc(total_length + 1);
if (!map_content)
{
perror("Error allocating memory for map");
return (NULL);
}
// Rouvrir le fichier pour la deuxième lecture
file = open(filename, O_RDONLY);
if (file < 0)
{
perror("Error opening file");
free(map_content);
return (NULL);
}
// Lire le contenu du fichier dans le tampon
*length = 0;
while ((bytes_read = read(file, map_content + *length, total_length - *length)) > 0)
*length += bytes_read;
// Ajouter le caractère de fin de chaîne
map_content[*length] = '\0';
// Fermer le fichier
close(file);
return map_content;
}
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);
}

Binary file not shown.