so_long/games/enemy.c
Etienne Rey-bethbeder c0a0d33aed avancement
2023-01-04 18:20:55 +01:00

79 lines
2.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* enemy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 14:08:08 by erey-bet #+# #+# */
/* Updated: 2023/01/04 17:04:14 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "../so_long.h"
void distance_player(char **map, int x, int y, int nbr)
{
map[x][y] = nbr + '0';
if (map[x][y + 1] != '1' && map[x][y + 1] == '0')
distance_player(map, x, y + 1, nbr + 1);
if (map[x + 1][y] != '1' && map[x + 1][y] == '0')
distance_player(map, x + 1, y, nbr + 1);
if (map[x - 1][y] != '1' && map[x + 1][y] == '0')
distance_player(map, x - 1, y, nbr + 1);
if (map[x][y - 1] != '1' && map[x][y - 1] == '0')
distance_player(map, x, y - 1, nbr + 1);
}
t_xy path_finding(t_data *data, int x, int y)
{
t_xy new_pos;
int i;
char **map;
new_pos.x = x;
new_pos.y = y;
map = ft_strdups(data->map);
distance_player(map, x, y, 0);
i = -1;
if (map[x][y + 1] != '1')
{
i = map[x][y + 1];
new_pos.y = y + 1;
}
if (map[x + 1][y] != '1')
if (i == -1 || map[x + 1][y] < i)
{
i = map[x + 1][y];
new_pos.x = x + 1;
}
if (map[x - 1][y] != '1' && map[x + 1][y] > '0')
if (i == -1 || map[x - 1][y] < i)
{
i = map[x - 1][y];
new_pos.x = x - 1;
}
if (map[x][y - 1] != '1' && map[x][y - 1] == '0')
if (i == -1 || map[x][y - 1] < i)
{
i = map[x][y - 1];
new_pos.y = y - 1;
}
i = 0;
while (map[i])
free(map[i++]);
free(map);
return (new_pos);
}
void move_enemy(t_data *data)
{
t_xy pos;
set_element(data, data->y_ene, data->x_ene, '0');
pos = path_finding(data, data->x_ene, data->y_ene);
set_element(data, pos.y, pos.x, 'e');
data->x_ene = pos.x;
data->y_ene = pos.y;
}