-
Notifications
You must be signed in to change notification settings - Fork 0
/
heat_map.c
116 lines (106 loc) · 2.66 KB
/
heat_map.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heat_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dstepane <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/10 16:14:04 by dstepane #+# #+# */
/* Updated: 2019/03/10 16:14:06 by dstepane ### ########.fr */
/* */
/* ************************************************************************** */
#include "filler.h"
static int assign_heat(t_flr *f, intmax_t i, int x, int y)
{
f->map_h--;
f->map_w--;
if ((x + 1 < f->map_w && f->hm[y][x + 1] == i) ||
(y + 1 < f->map_h && f->hm[y + 1][x] == i) ||
(y + 1 < f->map_h && x + 1 < f->map_w && f->hm[y + 1][x + 1] == i) ||
(y - 1 >= 0 && x + 1 < f->map_w && f->hm[y - 1][x + 1] == i) ||
(y - 1 >= 0 && f->hm[y - 1][x] == i) ||
(y - 1 >= 0 && x - 1 >= 0 && f->hm[y - 1][x - 1] == i) ||
(x - 1 >= 0 && f->hm[y][x - 1] == i) ||
(y < f->map_h && x - 1 >= 0 && f->hm[y + 1][x - 1] == i))
{
f->hm[y][x] = i + 1;
f->map_h++;
f->map_w++;
return (1);
}
f->map_h++;
f->map_w++;
return (0);
}
static void fill_heatmap2(t_flr *f, intmax_t i, intmax_t *done)
{
int x;
int y;
y = 0;
while (y < f->map_h)
{
while (x < f->map_w)
{
if (f->hm[y][x] == 99)
{
if (assign_heat(f, i, x, y))
(*done)++;
}
x++;
}
x = 0;
y++;
}
}
void fill_heatmap(t_flr *f)
{
intmax_t i;
intmax_t done;
intmax_t save;
i = 0;
done = 0;
save = -1;
while (done > save)
{
save = done;
fill_heatmap2(f, i, &done);
i++;
}
}
static void create_heatmap2(t_flr *f, int *y)
{
int x;
x = 0;
while (x < f->map_w)
{
if (f->map[*y][x] == '.')
f->hm[*y][x] = 99;
else if (f->map[*y][x] == f->foe || f->map[*y][x] == (f->foe - 32))
f->hm[*y][x] = 0;
else if (f->map[*y][x] == f->me || f->map[*y][x] == (f->me - 32))
f->hm[*y][x] = -1;
x++;
}
(*y)++;
}
int create_heatmap(t_flr *f)
{
int y;
y = 0;
if (!(f->hm = (int **)malloc(sizeof(int *) * f->map_h + 1)))
return (-1);
while (y < f->map_h)
{
f->hm[y] = (int *)malloc(sizeof(int *) * f->map_w);
if (!f->hm[y])
{
while (y > 0)
free(f->hm[--y]);
free(f->hm);
return (-1);
}
create_heatmap2(f, &y);
}
f->hm[y] = NULL;
return (0);
}