summaryrefslogtreecommitdiff
path: root/map.c
blob: de3bcf1c17efb14ccb6881b36545c0489d6b777c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   map.c                                              :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: kdx    <kdx   @student.42angouleme.fr      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/10/10 07:35:33 by kdx               #+#    #+#             */
/*   Updated: 2022/10/16 00:21:32 by kdx              ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "map.h"
#include "readall.h"
#include <unistd.h>
#include <fcntl.h>

static t_err	check_path(const char *path)
{
	if (ft_strlen(path) <= ft_strlen(".ber"))
		return (1);
	if (ft_strncmp(path + ft_strlen(path) - ft_strlen(".ber"), ".ber", 4))
		return (1);
	return (0);
}

static t_err	find_exit(t_map *map)
{
	size_t	i;

	i = 0;
	while (i < (map->width + 1) * map->height)
	{
		if (map->data[i] == TILE_EXIT)
		{
			map->exit[0] = (i % (map->width + 1)) * TSIZE;
			map->exit[1] = (i / (map->width + 1)) * TSIZE;
			return (0);
		}
		i++;
	}
	return (1);
}

t_err	map_load(t_map *map, const char *path)
{
	int		fd;
	t_err	err;

	if (check_path(path))
		return (ERR_FILENAME);
	fd = open(path, O_RDONLY);
	if (fd < 0)
		return (ERR_OPEN);
	map->data = (unsigned char *)readall(fd);
	close(fd);
	if (map->data == NULL)
		return (ERR_READ);
	err = map_verify(map, (unsigned const char *)map->data);
	if (err)
		return (err);
	if (find_exit(map))
		return (ERR_MISSING);
	return (ERR_NONE);
}

void	map_destroy(t_map *map)
{
	ft_free(map->data);
	map->data = NULL;
}

const char	*map_load_error(t_err err)
{
	if (err == ERR_OPEN)
		return ("Failed to open file.");
	if (err == ERR_READ)
		return ("Failed to read file.");
	if (err == ERR_MALLOC)
		return ("Failed to malloc.");
	if (err == ERR_INVALID_CHAR)
		return ("Found invalid character in loaded map.");
	if (err == ERR_WRONG_DIMENSION)
		return ("Wrong map dimensions.");
	if (err == ERR_FILENAME)
		return ("Invalid filename.");
	if (err == ERR_MISSING)
		return ("Missing required character.");
	if (err == ERR_DUPLICATE)
		return ("Duplicate unique character.");
	if (err == ERR_OPEN_BORDER)
		return ("Borders aren't closed properly.");
	if (err == ERR_NO_PATH)
		return ("Map is impossible.");
	return ("No error.");
}