/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   map_create.c                                       :+:    :+:            */
/*                                                     +:+                    */
/*   By: qmennen <qmennen@student.codam.nl>           +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/04/23 12:21:13 by whaffman      #+#    #+#                 */
/*   Updated: 2025/05/07 11:52:14 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "cub3d.h"

int	map_create(t_game **game, const char *mapfile)
{
	t_tile		**grid;

	if (!(*game))
		return (FAILURE);
	(*game)->map = malloc(sizeof(t_map));
	if (!(*game)->map)
	{
		perror("Error allocating memory for (*game)->map");
		return (FAILURE);
	}
	ft_memset((*game)->map->textures, 0, sizeof((*game)->map->textures));
	if (!parse_args(mapfile, (*game)))
		return (FAILURE);
	grid = copy_map((*game)->map->grid, (*game)->map->width, (*game)->map->height);
	if (!grid)
	{
		perror("Error copying (*game)->map");
		free((*game)->map);
		return (FAILURE);
	}
	if (!enclosed_map((*game)->map))
	{
		ft_putendl_fd("Map is not enclosed", STDERR_FILENO);
		grid_free(grid, (*game)->map->height);
		map_free((*game)->map);
		return (FAILURE);
	}
	grid_free((*game)->map->grid, (*game)->map->height);
	(*game)->map->grid = grid;
	print_config((*game)->map);
	return (SUCCESS);
}
