derversion

This commit is contained in:
Ladebeze66
2024-01-30 14:02:57 +01:00
parent 7d67dfd452
commit bb9c6a0f59
8 changed files with 135 additions and 67 deletions

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:49:45 by fgras-ca #+# #+# */
/* Updated: 2024/01/29 19:46:50 by fgras-ca ### ########.fr */
/* Updated: 2024/01/30 13:57:02 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,17 +15,17 @@
static int flood_fill_and_check(t_map_check *map_check,
char *visitedMap, t_point p, char replacement)
{
if (p.x < 0 || p.x >= map_check->maxWidth
if (p.x < 0 || p.x >= map_check->max_width
|| p.y < 0 || p.y >= map_check->height)
return (0);
if (visitedMap[p.y * map_check->maxWidth + p.x] == 1
|| map_check->map[p.y * map_check->maxWidth + p.x] == '1')
if (visitedMap[p.y * map_check->max_width + p.x] == 1
|| map_check->map[p.y * map_check->max_width + p.x] == '1')
return (1);
if (map_check->map[p.y * map_check->maxWidth + p.x] == ' '
if (map_check->map[p.y * map_check->max_width + p.x] == ' '
&& !is_space_surrounded_by_walls(map_check, p))
return (0);
visitedMap[p.y * map_check->maxWidth + p.x] = 1;
map_check->map[p.y * map_check->maxWidth + p.x] = replacement;
visitedMap[p.y * map_check->max_width + p.x] = 1;
map_check->map[p.y * map_check->max_width + p.x] = replacement;
return (flood_fill_and_check(map_check, visitedMap,
(t_point){p.x + 1, p.y}, replacement)
&& flood_fill_and_check(map_check, visitedMap,
@@ -45,10 +45,10 @@ static int find_start_point(t_map_check *map_check, t_point *startPoint)
while (y < map_check->height)
{
x = 0;
while (x < map_check->maxWidth)
while (x < map_check->max_width)
{
if ((map_check->map[y * map_check->maxWidth + x] == '0'
|| map_check->map[y * map_check->maxWidth + x] == '2')
if ((map_check->map[y * map_check->max_width + x] == '0'
|| map_check->map[y * map_check->max_width + x] == '2')
&& !is_space_surrounded_by_walls(map_check, (t_point){x, y}))
{
startPoint->x = x;
@@ -66,7 +66,7 @@ static int initialize_maps(char **copy_map, char **visited_map,
int maxWidth, int height)
{
*copy_map = malloc(maxWidth * height);
*visited_map = calloc(maxWidth * height, sizeof(char));
*visited_map = ft_calloc(maxWidth * height, sizeof(char));
if (!(*copy_map) || !(*visited_map))
{
free(*copy_map);
@@ -92,7 +92,7 @@ int is_map_closed(char *map, int maxWidth, int height)
t_point start_point;
int is_closed;
map_check.maxWidth = maxWidth;
map_check.max_width = maxWidth;
map_check.height = height;
if (!initialize_maps(&copy_map, &visited_map, maxWidth, height))
return (0);

View File

@@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 19:44:27 by fgras-ca #+# #+# */
/* Updated: 2024/01/29 19:48:01 by fgras-ca ### ########.fr */
/* Updated: 2024/01/30 13:57:02 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,7 @@
int check_boundaries(t_point p, t_map_check *map_check)
{
return (p.x > 0 && p.x < map_check->maxWidth - 1
return (p.x > 0 && p.x < map_check->max_width - 1
&& p.y > 0 && p.y < map_check->height - 1);
}
@@ -22,8 +22,8 @@ int is_space_surrounded_by_walls(t_map_check *map_check, t_point p)
{
if (!check_boundaries(p, map_check))
return (0);
return (map_check->map[p.y * map_check->maxWidth + (p.x - 1)] == '1'
&& map_check->map[p.y * map_check->maxWidth + (p.x + 1)] == '1'
&& map_check->map[(p.y - 1) * map_check->maxWidth + p.x] == '1'
&& map_check->map[(p.y + 1) * map_check->maxWidth + p.x] == '1');
return (map_check->map[p.y * map_check->max_width + (p.x - 1)] == '1'
&& map_check->map[p.y * map_check->max_width + (p.x + 1)] == '1'
&& map_check->map[(p.y - 1) * map_check->max_width + p.x] == '1'
&& map_check->map[(p.y + 1) * map_check->max_width + p.x] == '1');
}

View File

@@ -6,52 +6,56 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/16 16:19:13 by fgras-ca #+# #+# */
/* Updated: 2024/01/29 20:17:57 by fgras-ca ### ########.fr */
/* Updated: 2024/01/30 13:55:56 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
bool read_map_line(int fd, char *buffer, ssize_t *bytes_read)
{
*bytes_read = read(fd, buffer, 1023);
if (*bytes_read <= 0)
return (false);
buffer[*bytes_read] = '\0';
return (true);
}
bool process_line(char **map_buffer, int *map_length, char *line)
{
ssize_t bytes_read;
bytes_read = ft_strlen(line);
*map_buffer = ft_realloc(*map_buffer, *map_length + bytes_read + 1);
if (!*map_buffer)
{
perror("Error reallocating memory for map buffer");
return (false);
}
ft_memcpy(*map_buffer + *map_length, line, bytes_read);
*map_length += bytes_read;
(*map_buffer)[*map_length] = '\0';
return (true);
}
bool handle_map(int fd, char **map_buffer, int *map_length)
{
char *line;
size_t len;
ssize_t read;
char buffer[1024];
ssize_t bytes_read;
bool start_copying;
FILE *stream;
stream = fdopen(fd, "r");
line = NULL;
len = 0;
start_copying = false;
*map_length = 0;
*map_buffer = NULL;
if (!stream)
while (read_map_line(fd, buffer, &bytes_read))
{
perror("Error converting file descriptor to FILE *");
return (false);
}
while ((read = getline(&line, &len, stream)) != -1)
{
if (!start_copying && (ft_strchr(line, '1') || ft_strchr(line, '0')))
if (!start_copying && (ft_strchr(buffer, '1')
|| ft_strchr(buffer, '0')))
start_copying = true;
if (start_copying)
{
*map_buffer = realloc(*map_buffer, *map_length + read + 1);
if (!*map_buffer)
{
perror("Error reallocating memory for map buffer");
free(line);
fclose(stream);
if (!process_line(map_buffer, map_length, buffer))
return (false);
}
ft_memcpy(*map_buffer + *map_length, line, read);
*map_length += read;
(*map_buffer)[*map_length] = '\0';
}
}
free(line);
fclose(stream);
return (*map_buffer != NULL);
}