-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy_bonus.c
116 lines (108 loc) · 2.83 KB
/
enemy_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* enemy_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: het-taja <het-taja@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/14 10:44:30 by het-taja #+# #+# */
/* Updated: 2024/07/15 22:01:41 by het-taja ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long_bonus.h"
static int flood_fill_enemy(t_game *game)
{
char **tmp;
tmp = map_dup(game->enemy_map);
check_path(game->playerx / 50, game->playery / 50, tmp, 'C');
if (check_fill(tmp, 'C') == -1)
{
clear_map(tmp);
tmp = NULL;
return (-1);
}
check_path_exit(game->playerx / 50, game->playery / 50, tmp, 'E');
if (check_fill(tmp, 'E') == -1)
{
clear_map(tmp);
tmp = NULL;
return (-1);
}
clear_map(tmp);
tmp = NULL;
return (0);
}
static int enemy_util(t_game *game, int x, int y)
{
if (game->enemy_map[y][x] == '0')
{
game->enemy_map[y][x] = 'X';
if (flood_fill_enemy(game) != -1)
{
game->map[y][x] = 'X';
game->en_x = x;
game->en_y = y;
return (0);
}
else
game->enemy_map[y][x] = '0';
}
return (1);
}
int put_enemy(t_game *game)
{
int x;
int y;
y = 0;
while (game->enemy_map[y])
{
x = 0;
while (game->enemy_map[y][x])
{
if (enemy_util(game, x, y) == 0)
return (0);
x++;
}
y++;
}
return (1);
}
static int patrol_util(t_game *game)
{
if (game->map[game->en_y][game->en_x + 1] == '0'
|| game->map[game->en_y][game->en_x + 1] == 'P')
{
game->map[game->en_y][game->en_x + 1] = 'X';
game->map[game->en_y][game->en_x] = '0';
game->en_x++;
return (1);
}
else if (game->map[game->en_y - 1][game->en_x] == '0'
|| game->map[game->en_y - 1][game->en_x] == 'P')
{
game->map[game->en_y - 1][game->en_x] = 'X';
game->map[game->en_y][game->en_x] = '0';
game->en_y--;
return (1);
}
return (0);
}
void patrol(t_game *game)
{
if (patrol_util(game))
return ;
else if (game->map[game->en_y + 1][game->en_x] == '0'
|| game->map[game->en_y + 1][game->en_x] == 'P')
{
game->map[game->en_y + 1][game->en_x] = 'X';
game->map[game->en_y][game->en_x] = '0';
game->en_y++;
}
else if (game->map[game->en_y][game->en_x - 1] == '0'
|| game->map[game->en_y][game->en_x - 1] == 'P')
{
game->map[game->en_y][game->en_x - 1] = 'X';
game->map[game->en_y][game->en_x] = '0';
game->en_x--;
}
}