aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_parse_map.c
blob: 81f59ec2a25e2dc99e210702601248824570dfa7 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_parse_map.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: joelecle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:28:56 by joelecle          #+#    #+#             */
/*   Updated: 2020/03/09 17:27:29 by joelecle         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>

static void
	ft_check_map_last_line(t_cub *clist)
{
	t_map	*ml;
	size_t	i;
	size_t	j;

	i = 0;
	ml = &clist->mlist;
	while (ml->map[i])
		i++;
	j = 0;
	i -= 1;
	while (ml->map[i][j])
	{
		if (ml->map[i][j] != '1' && ml->map[i][j] != ' ' &&
			ml->map[i][j] != '\0')
			ft_map_error(FT_ERR_MAP_L_L, clist);
		j++;
	}
}

static int8_t
	ft_error_here(const char *errmsg, char *line, t_cub *clist)
{
	ft_memdel((void*)&line);
	return (ft_map_error(errmsg, clist));
}

static int8_t
	ft_parse_it(int fd, t_cub *clist)
{
	char	*line;
	char	**words;
	int		ret;

	clist->mlist.line_chk += 1;
	if ((ret = get_next_line(fd, &line)) < 0)
		return (ft_map_error(FT_ERR_READ, clist));
	if (ret == 0)
		return (ft_error_here(FT_ERR_UNFINISHED, line, clist));
	if (!line[0])
	{
		ft_memdel((void*)&line);
		return (ft_parse_it(fd, clist));
	}
	if (ft_ischarset("1 ", line[0]))
		return ((ft_get_map_first_line(line, clist) < 0) ? (-1) : (25));
	if (!ft_ischarset(FT_CHRST_VALID_PARSE, line[0]) ||
		!(words = ft_split(line, ' ')))
		return (ft_error_here(FT_ERR_ILL_ENTRY, line, clist));
	if ((ret = ft_select_get(words, clist)) == FT_PARSE_END_RET)
		return ((ft_get_map_first_line(line, clist) < 0) ? (-1) : (25));
	ft_memdel((void*)&line);
	return (ret);
}

static void
	ft_save_name(const char *map_path, t_cub *clist)
{
	ft_memdel((void*)&clist->mlist.filename);
	if (!(clist->mlist.filename =
		(char*)malloc((ft_strlen(map_path) + 1) * sizeof(char))))
		ft_alloc_error(clist);
	ft_sprintf(clist->mlist.filename, "%s", map_path);
}

void
	ft_parse_map(const char *map_path, t_cub *clist)
{
	int		fd;
	int8_t	ret;

	if (ft_check_ext(map_path, ".cub") < 0)
		ft_map_error(FT_ERR_NOT_A_CUB, clist);
	fd = open(map_path, O_RDONLY);
	if (fd < 0)
		ft_error(FT_RET_NO_MAP, FT_ERR_NO_MAP, clist);
	ft_save_name(map_path, clist);
	ret = 1;
	while (ret != FT_PARSE_END_RET && ret >= 0)
		ret = ft_parse_it(fd, clist);
	(ret == -2) ? (ft_map_error(FT_ERR_ALR_SET, clist)) : 0;
	(ret == -1) ? (ft_map_error(clist->errmsg, clist)) : 0;
	if (ft_get_map_core(fd, clist) < 0)
		ft_map_error(clist->errmsg, clist);
	ft_check_map_last_line(clist);
	ft_check_map_surrounds(&clist->mlist, clist);
	ft_get_spawns(clist);
	ft_get_nlvl_pos(&clist->mlist);
	ft_check_missing(clist);
	ft_set_minimap_scale(clist);
	clist->currlvl += (clist->isdead == 0) ? (1) : (0);
	close(fd);
}