/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_find_item.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/17 20:06:26 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/17 20:06:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stdint.h>

static uint16_t
	ft_fetch_heals_id(uint64_t pos_y, uint64_t pos_x, t_cub *cl)
{
	uint16_t	id;

	id = 0;
	while (id <= 64)
	{
		if (cl->heals[id].s_pos_y == pos_y && cl->heals[id].s_pos_x == pos_x)
			return (id);
		id++;
	}
	return (0);
}

static void
	ft_weapon_check(const char map_char, t_player *pl, t_cub *cl)
{
	uint8_t	weap_id;

	if (ft_ischarset(FT_CHRST_WEAPONS, map_char))
	{
		weap_id = (map_char == '!') ? (0) : (1);
		cl->mlist.map[(uint64_t)pl->pos_y][(uint64_t)pl->pos_x] = '0';
		cl->weaps[weap_id][0].s_pos_x = 0;
		cl->weaps[weap_id][0].s_pos_y = 0;
		pl->has_weapon[weap_id] = 1;
		pl->handles_weapon = (weap_id == 1) ? (2) : (weap_id);
		/* TODO: ft_sfx_weapon(3) */
	}
}

void
	ft_find_item(t_player *pl, t_map *ml, t_cub *cl)
{
	uint16_t	id;
	const char	map_char = ml->map[(uint64_t)pl->pos_y][(uint64_t)pl->pos_x];

	if (ft_ischarset(FT_CHRST_ITEM, map_char))
	{
		if (map_char == '+' && pl->life < 100)
		{
			pl->life += FT_HEAL_PACK_AMOUNT;
			pl->life = (pl->life > 100) ? (100) : (pl->life);
			ml->map[(uint64_t)pl->pos_y][(uint64_t)pl->pos_x] = '0';
			id = ft_fetch_heals_id((uint64_t)pl->pos_y,
									(uint64_t)pl->pos_x, cl);
			cl->heals[id].s_pos_x = 0;
			cl->heals[id].s_pos_y = 0;
			 /* TODO: ft_sfx_heal(3) */
		}
		ft_weapon_check(map_char, pl, cl);
	}
}