aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_check_map_line.c
blob: 44580db09c235b80d7e8bdcda6b4405a5ae35373 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_check_map_line.c                                :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:28:34 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:28:37 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stddef.h>
#include <stdint.h>

static int8_t
	ft_first_checks(char *line, size_t i, t_cub *cl)
{
	if (!ft_ischarset(FT_CHRST_MAP_ENTRY, line[i]))
	{
		ft_sprintf(cl->errmsg, FT_ERR_ILL_MAP);
		return (-1);
	}
	if (ft_ischarset(FT_CHRST_SPAWN, line[i]))
		cl->mlist.isspawn += 1;
	if (cl->mlist.isspawn > 1)
	{
		ft_sprintf(cl->errmsg, FT_ERR_MULT_SPAWN);
		return (-1);
	}
	if (line[i] == 'L')
		cl->mlist.isnlvl += 1;
	if (cl->mlist.isnlvl > 1)
	{
		ft_sprintf(cl->errmsg, FT_ERR_MULT_NLVL);
		return (-1);
	}
	cl->mlist.istraps = (line[i] == 'T') ? (1) : (cl->mlist.istraps);
	cl->mlist.isheals = (line[i] == '+') ? (1) : (cl->mlist.isheals);
	return (0);
}

static int8_t
	ft_second_checks(char *line, size_t i, t_cub *clist)
{
	if (!ft_ischarset("1 ", line[i]))
	{
		ft_sprintf(clist->errmsg, FT_ERR_MAP_WALLS);
		return (-1);
	}
	return (0);
}

size_t
	ft_get_line_len(char *line)
{
	size_t	i;
	size_t	j;

	i = 0;
	j = 0;
	while (line[i])
	{
		if (line[i] == ' ')
			j++;
		i++;
	}
	return (i - j);
}

static int8_t
	ft_check_side_walls(char *line, size_t i, t_cub *clist)
{
	if ((line[0] != ' ' && line[0] != '1') || line[i - 1] != '1')
	{
		ft_sprintf(clist->errmsg, FT_ERR_MAP_WALLS);
		return (-1);
	}
	return (0);
}

int8_t
	ft_check_map_line(char *line, uint8_t l, t_cub *clist)
{
	size_t	i;

	i = -1;
	while (line[++i])
	{
		if (l != 1)
		{
			if (ft_first_checks(line, i, clist) < 0)
				return (-1);
		}
		else
		{
			if (ft_second_checks(line, i, clist) < 0)
				return (-1);
		}
	}
	if (ft_check_side_walls(line, i, clist) < 0)
		return (-1);
	return (0);
}