/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   enemy.c                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2023/01/04 14:08:08 by erey-bet          #+#    #+#             */
/*   Updated: 2023/01/09 18:46:26 by erey-bet         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../so_long_bonus.h"

void	distance_player(int **map, int x, int y, int nbr)
{
	if (map[x][y] == -1 || nbr >= 10)
		return ;
	if (map[x][y + 1] > -1 && (map[x][y + 1] == 0 || map[x][y + 1] > nbr))
		map[x][y + 1] = nbr;
	if (map[x + 1][y] > -1 && (map[x + 1][y] == 0 || map[x + 1][y] > nbr))
		map[x + 1][y] = nbr;
	if (map[x - 1][y] > -1 && (map[x - 1][y] == 0 || map[x - 1][y] > nbr))
		map[x - 1][y] = nbr;
	if (map[x][y - 1] > -1 && (map[x][y - 1] == 0 || map[x][y - 1] > nbr))
		map[x][y - 1] = nbr;
	distance_player(map, x, y + 1, nbr + 1);
	distance_player(map, x + 1, y, nbr + 1);
	distance_player(map, x - 1, y, nbr + 1);
	distance_player(map, x, y - 1, nbr + 1);
}

int	**ft_strdups_int(char **src)
{
	int	**src_cpy;
	int	x;
	int	y;

	src_cpy = ft_calloc(ft_strslen(src) + 1, (sizeof(int *)));
	if (src_cpy == NULL)
		return (NULL);
	x = -1;
	while (src[++x])
	{
		src_cpy[x] = ft_calloc(ft_strlen(src[x]) + 1, sizeof(int));
		if (src_cpy[x] == NULL)
			return (NULL);
		y = -1;
		while (src[x][++y])
		{
			if (src[x][y] != '0')
				src_cpy[x][y] = -1;
			else
				src_cpy[x][y] = 0;
		}
	}
	src_cpy[x] = NULL;
	return (src_cpy);
}

void	find(int **map, t_xy *new_pos, int x, int y)
{
	int	i;

	i = -1;
	if (map[x][y + 1] != -1)
	{
		i = map[x][y + 1];
		new_pos->y = y + 1;
	}
	if (choice(map, &i, x + 1, y))
	{
		new_pos->x = x + 1;
		new_pos->y = y;
	}
	if (choice(map, &i, x - 1, y))
	{
		new_pos->x = x - 1;
		new_pos->y = y;
	}
	if (choice(map, &i, x, y - 1))
	{
		new_pos->x = x;
		new_pos->y = y - 1;
	}
}

t_xy	path_finding(t_data *data, int x, int y)
{
	t_xy	new_pos;
	int		i;
	int		**map;

	new_pos.x = x;
	new_pos.y = y;
	map = ft_strdups_int(data->map);
	map[data->y_ply][data->x_ply] = -2;
	distance_player(map, data->y_ply, data->x_ply, 1);
	find(map, &new_pos, x, y);
	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;
}
