/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parsing_body.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cchauvet <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2023/05/16 14:51:13 by cchauvet          #+#    #+#             */
/*   Updated: 2023/06/16 14:39:18 by cchauvet         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "parsing_private.h"

char	**get_body(const char **file_content, size_t header_size)
{
	char	**body;
	int		i;
	int		len;

	len = 0;
	while (file_content[header_size + len] != NULL)
		len++;
	body = malloc((len + 1) * sizeof(char *));
	if (body == NULL)
		return (NULL);
	body[len] = NULL;
	i = 0;
	while (i < len)
	{
		body[i] = ft_strdup(file_content[header_size + i]);
		if (body[i] == NULL)
		{
			ft_cancel((void **) body, i);
			return (NULL);
		}
		i++;
	}
	return (body);
}

int	get_spawn_position(const char **body, double *spawn_x, double *spawn_y)
{
	int	x;
	int	y;

	*spawn_x = -1;
	*spawn_y = -1;
	y = 0;
	while (body[y] != NULL)
	{
		x = 0;
		while (body[y][x] != '\0')
		{
			if (ft_is_in("NSEW", body[y][x]))
			{
				if (*spawn_x != -1)
					return (1);
				*spawn_x = x;
				*spawn_y = y;
			}
			x++;
		}
		y++;
	}
	if (*spawn_x == -1)
		return (2);
	return (0);
}

int	map_is_in_one_part(const char **body)
{
	int	y;

	y = ft_tablen((const void **) body) - 1;
	while (body[y][0] == '\0')
			y--;
	while (y > 0)
	{
		if (body[y][0] == '\0')
			return (1);
		y--;
	}
	return (0);
}
