-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpars_map_flood.c
75 lines (68 loc) · 1.97 KB
/
pars_map_flood.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pars_map_flood.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbrandy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/16 16:51:13 by lbrandy #+# #+# */
/* Updated: 2021/04/02 20:13:51 by lbrandy ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int max_width_map(char **map)
{
int i;
int j;
int max;
i = 0;
max = 0;
while (map[i])
{
j = 0;
while (map[i][j])
j++;
if (j > max)
max = j;
i++;
}
return (max);
}
void flood_fill(char **map, t_pos *p, int x, int y)
{
char new_color;
new_color = '#';
if (x > 0 && x < p->map_width && y > 0 && y < (p->map_height + 2)
&& (map[y][x] == ' ' || map[y][x] == '*'))
error_handler("bad map\n");
if (x >= 0 && x < p->map_width && y >= 0 && y < (p->map_height + 2)
&& (map[y][x] == '0' || map[y][x] == '2') && map[y][x] != new_color)
{
map[y][x] = new_color;
flood_fill(map, p, x + 1, y);
flood_fill(map, p, x - 1, y);
flood_fill(map, p, x, y + 1);
flood_fill(map, p, x, y - 1);
}
}
void flood(t_pos *pos, t_all *all)
{
int i;
int j;
char **new_map;
i = 1;
pos->map_width = max_width_map(all->map);
new_map = copy_map(pos, all);
while (new_map[i] && i < (pos->map_height + 2))
{
j = 0;
while (new_map[i][j])
{
if (new_map[i][j] == '0' || new_map[i][j] == '2')
flood_fill(new_map, pos, j, i);
j++;
}
i++;
}
free_new_map(pos, new_map);
}