Cub3D
Loading...
Searching...
No Matches
check_enclosed_by_walls.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* check_enclosed_by_walls.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/10/07 13:51:35 by kamitsui #+# #+# */
9/* Updated: 2024/12/29 20:19:14 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "cub3d.h"
14
15static bool is_enclosed_on_remaining_area(
16 t_map *map, bool **visited)
17{
18 int x;
19 int y;
20
21 y = 0;
22 while (y < map->height)
23 {
24 x = 0;
25 while (x < map->width)
26 {
27 if (map->data[y][x] == '0' && visited[y][x] == false)
28 if (flood_fill(map, x, y, (bool **)visited) != true)
29 return (false);
30 x++;
31 }
32 y++;
33 }
34 return (ENUM_TRUE);
35}
36
37static bool **init_visited(t_map *map)
38{
39 bool **visited;
40 size_t i;
41
42 visited = (bool **)malloc((size_t)map->height * sizeof(bool *));
43 i = 0;
44 while (i < (size_t)map->height)
45 {
46 visited[i] = (bool *)ft_calloc((size_t)map->width, sizeof(bool));
47 i++;
48 }
49 return ((bool **)visited);
50}
51
52void free_visited(bool **visited, size_t size)
53{
54 size_t i;
55
56 i = 0;
57 while (i < (size_t)size)
58 {
59 free(visited[i]);
60 i++;
61 }
62 free(visited);
63}
64
65static t_bool process_false_or_error(t_bool is_surrounded,
66 bool **visited, size_t size)
67{
68 if (is_surrounded == ENUM_FALSE)
69 {
70 free_visited(visited, size);
71 ft_eprintf("%s%s\n", ERR_PROMPT, EMSG_MAP_NOT_ENCLOSED);
72 }
73 return (is_surrounded);
74}
75
84int check_enclosed_by_walls(const char *line, t_parse *parse)
85{
86 bool **visited;
87 int start_x;
88 int start_y;
89 t_map *map;
90 t_bool is_surrounded;
91
92 visited = init_visited(&parse->game->map);
93 if (visited == NULL)
94 return (EXIT_FAILURE);
95 start_x = parse->player_grid.x;
96 start_y = parse->player_grid.y;
97 map = &parse->game->map;
98 is_surrounded = ENUM_TRUE;
99 is_surrounded = flood_fill(map, start_x, start_y, visited);
100 if (process_false_or_error(is_surrounded, visited, (size_t)map->height)
101 != ENUM_TRUE)
102 return (EXIT_FAILURE);
103 is_surrounded = is_enclosed_on_remaining_area(map, visited);
104 if (process_false_or_error(is_surrounded, visited, (size_t)map->height)
105 != ENUM_TRUE)
106 return (EXIT_FAILURE);
107 free_visited(visited, (size_t)map->height);
108 (void)line;
109 return (EXIT_SUCCESS);
110}
111// debug code
112//printf("-- End flood_fill()\n");//debug
113//int fd = parse->game->debug.fd;// for debug
114//put_visited(fd, visited, map);
game map
Definition type_cub3d.h:237