split map.c

This commit is contained in:
whaffman 2025-04-23 12:30:11 +02:00
parent ac6309ad1f
commit d763c08626
15 changed files with 391 additions and 252 deletions

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* map.h :+: :+: :+: */ /* map.h :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/04/17 19:19:19 by qmennen #+# #+# */ /* Created: 2025/04/17 19:19:19 by qmennen #+# #+# */
/* Updated: 2025/04/22 17:24:47 by qmennen ### ########.fr */ /* Updated: 2025/04/23 12:26:56 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,16 @@
int map_create(t_game **game, const char *mapfile); int map_create(t_game **game, const char *mapfile);
void map_free(t_map *map); void map_free(t_map *map);
void print_map(t_map *map); void print_map(t_map *map);
t_tile get_tile(t_map * map, int x, int y); t_tile get_tile(t_map * map, int x, int y);
int enclosed_map(t_map *map);
void grid_free(t_tile **grid, int height);
t_tile **copy_map(t_tile **grid, int width, int height);
int parse_args(const char *mapfile, t_map *map);
t_tile **create_grid(int width, int height);
int find_player_or_empty(t_map *map, int *x, int *y);
int floodfill(t_map *map, int x, int y);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/19 14:41:55 by whaffman #+# #+# */ /* Created: 2025/04/19 14:41:55 by whaffman #+# #+# */
/* Updated: 2025/04/22 13:18:20 by whaffman ######## odam.nl */ /* Updated: 2025/04/23 12:28:37 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,9 +15,8 @@
# include "cub3d.h" # include "cub3d.h"
# include "libft.h" # include "libft.h"
size_t count_chars(const char *str, char c); size_t count_chars(const char *str, char c);
ssize_t get_file_size(const char *filename);
ssize_t get_file_size(const char *filename);
char *read_map_file(const char *filename); char *read_map_file(const char *filename);
int is_map_line(const char *line); int is_map_line(const char *line);
unsigned int parse_color(const char *color_str); unsigned int parse_color(const char *color_str);
@ -30,5 +29,4 @@ void print_config(t_map *map);
t_tile **copy_map(t_tile **grid, int width, int height); t_tile **copy_map(t_tile **grid, int width, int height);
char **pointer_lines(char *buffer, char c); char **pointer_lines(char *buffer, char c);
#endif #endif

238
src/map.c
View File

@ -1,238 +0,0 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* map.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 13:20:51 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:00:48 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "cub3d.h"
#include "libft.h"
#include "types.h"
#define FAILURE 0
#define SUCCESS 1
void print_map(t_map *map)
{
const int chars[] = {'X', ' ', '.', '#', 'P'};
int i = 0;
while (i < map->height)
{
int j = 0;
while (j < map->width)
{
printf("%c ", chars[map->grid[i][j] + 2]);
j++;
}
printf("\n");
i++;
}
}
int find_player_or_empty(t_map *map, int *x, int *y)
{
int i;
int j;
if (!map || !x || !y)
return 0;
i = 0;
while (i < map->height)
{
j = 0;
while(j < map->width)
{
if (map->grid[i][j] == TILE_PLAYER || map->grid[i][j] == TILE_EMPTY)
{
*x = j;
*y = i;
return 1;
}
j++;
}
i++;
}
return 0;
}
int floodfill(t_map *map, int x, int y)
{
if (!map || x < 0 || y < 0 || x >= map->width || y >= map->height)
return (FAILURE);
if (map->grid[y][x] == TILE_VOID)
return (FAILURE);
if (map->grid[y][x] == TILE_WALL || map->grid[y][x] == TILE_VISITED)
return (SUCCESS);
map->grid[y][x] = TILE_VISITED;
if (!floodfill(map, x + 1, y) ||
!floodfill(map, x, y - 1) ||
!floodfill(map, x, y + 1) ||
!floodfill(map, x - 1, y))
return (FAILURE);
return (SUCCESS);
}
int enclosed_map(t_map *map)
{
int x;
int y;
if (!map)
return FAILURE;
if (!find_player_or_empty(map, &x, &y))
return SUCCESS;
if (!floodfill(map, x, y))
return FAILURE;
if (!enclosed_map(map))
return (FAILURE);
return SUCCESS;
}
static int parse_args(const char *mapfile, t_map *map)
{
char *buffer;
buffer = read_map_file(mapfile);
if (!buffer)
{
perror("Error reading map file");
return (FAILURE);
}
if (!parse_file(buffer, map))
{
ft_putstr_fd("Error parsing map file\n", 2);
free(buffer);
return (FAILURE);
}
free(buffer);
return (SUCCESS);
}
t_tile **create_grid(int width, int height)
{
t_tile **grid;
int i;
grid = malloc(sizeof(t_tile *) * height);
if (!grid)
return (NULL);
i = 0;
while (i < height)
{
grid[i] = malloc(sizeof(t_tile) * width);
if (!grid[i])
{
while (--i >= 0)
free(grid[i]);
free(grid);
return (NULL);
}
i++;
}
return (grid);
}
t_tile **copy_map(t_tile** grid, int width, int height)
{
t_tile **copy;
int i;
int j;
copy = create_grid(width, height);
if (!copy)
return (NULL);
i = 0;
while (i < height)
{
j = 0;
while (j < width)
{
copy[i][j] = grid[i][j];
j++;
}
i++;
}
return (copy);
}
void grid_free(t_tile **grid, int height)
{
int i;
if (!grid)
return;
i = 0;
while (i < height)
{
free(grid[i]);
i++;
}
free(grid);
}
int map_create(t_game **game, const char *mapfile)
{
t_map *map;
t_tile **grid;
if (!game || !*game)
return (FAILURE);
map = malloc(sizeof(t_map));
if (!map)
{
perror("Error allocating memory for map");
return (FAILURE);
}
parse_args(mapfile, map);
print_map(map);
grid = copy_map(map->grid, map->width, map->height);
if (!grid)
{
perror("Error copying map");
free(map);
return (FAILURE);
}
if(!enclosed_map(map))
{
fprintf(stderr, "NOT GOOD MAP FRIEND\n");
grid_free(grid, map->height);
map_free(map);
return (FAILURE);
}
grid_free(map->grid, map->height);
map->grid = grid;
print_config(map);
(*game)->map = map;
return (SUCCESS);
}
void map_free(t_map *map)
{
grid_free(map->grid, map->height);
//free(map->grid);
free(map->NO_texture);
free(map->SO_texture);
free(map->WE_texture);
free(map->EA_texture);
free(map);
}
t_tile get_tile(t_map * map, int x, int y)
{
if (x < 0 || y < 0 || x >= map->width || y >= map->height)
return (TILE_WALL);
return (map->grid[y][x]);
}

36
src/map/copy_map.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* copy_map.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:19:15 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:19:57 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "cub3d.h"
t_tile **copy_map(t_tile **grid, int width, int height)
{
t_tile **copy;
int i;
int j;
copy = create_grid(width, height);
if (!copy)
return (NULL);
i = 0;
while (i < height)
{
j = 0;
while (j < width)
{
copy[i][j] = grid[i][j];
j++;
}
i++;
}
return (copy);
}

38
src/map/create_grid.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* create_grid.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:18:31 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:20:25 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "cub3d.h"
t_tile **create_grid(int width, int height)
{
t_tile **grid;
int i;
grid = malloc(sizeof(t_tile *) * height);
if (!grid)
return (NULL);
i = 0;
while (i < height)
{
grid[i] = malloc(sizeof(t_tile) * width);
if (!grid[i])
{
while (--i >= 0)
free(grid[i]);
free(grid);
return (NULL);
}
i++;
}
return (grid);
}

29
src/map/enclosed_map.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* enclosed_map.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:16:47 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:20:23 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int enclosed_map(t_map *map)
{
int x;
int y;
if (!map)
return (FAILURE);
if (!find_player_or_empty(map, &x, &y))
return (SUCCESS);
if (!floodfill(map, x, y))
return (FAILURE);
if (!enclosed_map(map))
return (FAILURE);
return (SUCCESS);
}

View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* find_player_or_empty.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:13:50 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:20:20 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int find_player_or_empty(t_map *map, int *x, int *y)
{
int i;
int j;
if (!map || !x || !y)
return (FAILURE);
i = 0;
while (i < map->height)
{
j = 0;
while (j < map->width)
{
if (map->grid[i][j] == TILE_PLAYER || map->grid[i][j] == TILE_EMPTY)
{
*x = j;
*y = i;
return (SUCCESS);
}
j++;
}
i++;
}
return (FAILURE);
}

30
src/map/floodfill.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* floodfill.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:14:58 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:20:18 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int floodfill(t_map *map, int x, int y)
{
if (!map || x < 0 || y < 0 || x >= map->width || y >= map->height)
return (FAILURE);
if (map->grid[y][x] == TILE_VOID)
return (FAILURE);
if (map->grid[y][x] == TILE_WALL || map->grid[y][x] == TILE_VISITED)
return (SUCCESS);
map->grid[y][x] = TILE_VISITED;
if (!floodfill(map, x + 1, y)
|| !floodfill(map, x, y - 1)
|| !floodfill(map, x, y + 1)
|| !floodfill(map, x - 1, y))
return (FAILURE);
return (SUCCESS);
}

20
src/map/get_tile.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_tile.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:23:05 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:23:14 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "cub3d.h"
t_tile get_tile(t_map *map, int x, int y)
{
if (x < 0 || y < 0 || x >= map->width || y >= map->height)
return (TILE_WALL);
return (map->grid[y][x]);
}

29
src/map/grid_free.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* grid_free.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:20:38 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:21:00 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "cub3d.h"
void grid_free(t_tile **grid, int height)
{
int i;
if (!grid)
return ;
i = 0;
while (i < height)
{
free(grid[i]);
i++;
}
free(grid);
}

52
src/map/map_create.c Normal file
View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* map_create.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:21:13 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:22:15 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "cub3d.h"
int map_create(t_game **game, const char *mapfile)
{
t_map *map;
t_tile **grid;
if (!game || !*game)
return (FAILURE);
map = malloc(sizeof(t_map));
if (!map)
{
perror("Error allocating memory for map");
return (FAILURE);
}
parse_args(mapfile, map);
print_map(map);
grid = copy_map(map->grid, map->width, map->height);
if (!grid)
{
perror("Error copying map");
free(map);
return (FAILURE);
}
if (!enclosed_map(map))
{
fprintf(stderr, "NOT GOOD MAP FRIEND\n");
grid_free(grid, map->height);
map_free(map);
return (FAILURE);
}
grid_free(map->grid, map->height);
map->grid = grid;
print_config(map);
(*game)->map = map;
return (SUCCESS);
}

25
src/map/map_free.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* map_free.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:22:28 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:22:49 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "cub3d.h"
void map_free(t_map *map)
{
grid_free(map->grid, map->height);
// free(map->grid);
free(map->NO_texture);
free(map->SO_texture);
free(map->WE_texture);
free(map->EA_texture);
free(map);
}

33
src/map/parse_args.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parse_args.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:17:49 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:26:24 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int parse_args(const char *mapfile, t_map *map)
{
char *buffer;
buffer = read_map_file(mapfile);
if (!buffer)
{
perror("Error reading map file");
return (FAILURE);
}
if (!parse_file(buffer, map))
{
ft_putstr_fd("Error parsing map file\n", 2);
free(buffer);
return (FAILURE);
}
free(buffer);
return (SUCCESS);
}

39
src/map/print_map.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* print_map.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/23 12:11:12 by whaffman #+# #+# */
/* Updated: 2025/04/23 12:20:12 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void print_map(t_map *map)
{
const int chars[] = {'X', ' ', '.', '#', 'P'};
int i;
int j;
if (!map)
return ;
if (!map->grid)
return ;
if (map->width <= 0 || map->height <= 0)
return ;
i = 0;
while (i < map->height)
{
j = 0;
while (j < map->width)
{
printf("%c ", chars[map->grid[i][j] + 2]);
j++;
}
printf("\n");
i++;
}
}

View File

@ -18,7 +18,7 @@ C 100,100,10
1111111 111 1111 111111 1111111111 111111 1111111 111 1111 111111 1111111111 111111
1000001110111001 100001 w 1000001001 10001 1000001110111001 100001 1000001001 10001
1000000000000001 100001 100000110111100011 1000000000000001 100001 100000110111100011
1000001110000001110011111111 110000001000000001 1000001110000001110011111111 110000001000000001
1000001 10001000000010000001 100000001000000001 1000001 10001000000010000001 100000001000000001