/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   render_minimap.c                                   :+:    :+:            */
/*                                                     +:+                    */
/*   By: whaffman <whaffman@student.codam.nl>         +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/05/12 11:31:34 by whaffman      #+#    #+#                 */
/*   Updated: 2025/05/22 19:40:39 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "cub3d.h"

static int	inside_circle(t_vec2_int point)
{
	const int	radius_squared = 0.25 * MINIMAP_SIZE * MINIMAP_SIZE;
	int			dx;
	int			dy;

	dx = point.x - 0.5 * MINIMAP_SIZE;
	dy = point.y - 0.5 * MINIMAP_SIZE;
	return (dx * dx + dy * dy <= radius_squared);
}

static void	draw_minimap_pixel(t_game *game, t_vec2_int disp, t_vec2 map)
{
	mlx_image_t		*img;

	img = game->screen->minimap;
	if (map.x < 0 || map.x >= game->map->width
		|| map.y < 0 || map.y >= game->map->height)
		mlx_put_pixel(img, disp.x, disp.y, 0x00ff0044);
	else if (game->map->grid[(int)map.y][(int)map.x] == TILE_WALL)
		mlx_put_pixel(img, disp.x, disp.y, 0x00ff00ff);
	else if (game->map->grid[(int)map.y][(int)map.x] == TILE_EMPTY
		|| game->map->grid[(int)map.y][(int)map.x] == TILE_PLAYER)
		mlx_put_pixel(img, disp.x, disp.y, 0x00ff0077);
	else if (game->map->grid[(int)map.y][(int)map.x] == TILE_VISITED)
		mlx_put_pixel(img, disp.x, disp.y, 0xaaff00aa);
	else
		mlx_put_pixel(img, disp.x, disp.y, 0x00ff0044);
}

void	render_map(t_game *game)
{
	int			i;
	t_vec2_int	disp;
	t_vec2		map;
	double		map_center;

	i = 0;
	map_center = 0.5 * MINIMAP_SIZE / TILE_SIZE;
	while (i < MINIMAP_SIZE * MINIMAP_SIZE)
	{
		disp.x = i % MINIMAP_SIZE;
		disp.y = i / MINIMAP_SIZE;
		if (inside_circle(disp))
		{
			map = mul(vec2_from_int(disp), 1.0 / TILE_SIZE);
			map = add(map, game->player->pos);
			map = sub(map, (t_vec2){map_center, map_center});
			map = rot_by_dir(map, game->player->dir, game->player->pos);
			draw_minimap_pixel(game, disp, map);
		}
		i++;
	}
}
