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

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

static int8_t
	ft_check_exists(const int8_t ret, t_cub *clist)
{
	if (ret == 12)
		return (12);
	if (ret == 0 && clist->currlvl == 0 &&
		(clist->wlist.x_size != 0 || clist->wlist.y_size != 0))
		return (-1);
	else if (ret == 1 && (clist->mlist.no_tex_path[0]))
		return (-1);
	else if (ret == 2 && (clist->mlist.so_tex_path[0]))
		return (-1);
	else if (ret == 3 && (clist->mlist.ea_tex_path[0]))
		return (-1);
	else if (ret == 4 && (clist->mlist.we_tex_path[0]))
		return (-1);
	else if (ret == 5 && (clist->mlist.sprite_path[0]))
		return (-1);
	else if (ret == 6 && ((clist->f_rgb.r != -1) || (clist->f_rgb.g != -1)
			|| (clist->f_rgb.b != -1)))
		return (-1);
	else if (ret == 7 && ((clist->c_rgb.r != -1) || (clist->c_rgb.g != -1)
			|| (clist->c_rgb.b != -1)))
		return (-1);
	return (ret);
}

static int8_t
	ft_check_exists_two(const int8_t ret, t_cub *clist)
{
	if (ret == 12)
		return (12);
	if (ret == 8 && (clist->mlist.nlevel_path[0]))
		return (-1);
	if (ret == 9 && (clist->mlist.nl_tex_path[0]))
		return (-1);
	if (ret == 10 && (clist->mlist.music_path[0]))
		return (-1);
	return (ret);
}

static int8_t
	ft_get_id(char **words, t_cub *clist)
{
	int8_t	ret;
	char	ref[12][3];

	ft_sprintf(ref[0], "R");
	ft_sprintf(ref[1], "NO");
	ft_sprintf(ref[2], "SO");
	ft_sprintf(ref[3], "EA");
	ft_sprintf(ref[4], "WE");
	ft_sprintf(ref[5], "S");
	ft_sprintf(ref[6], "F");
	ft_sprintf(ref[7], "C");
	ft_sprintf(ref[8], "L");
	ft_sprintf(ref[9], "LT");
	ft_sprintf(ref[10], "MU");
	ft_sprintf(ref[11], "");
	ret = 0;
	while (ft_strncmp(words[0], ref[ret], 3) && ref[ret][0])
		ret++;
	if (ret == 11)
		ret = 12;
	ret = ft_check_exists(ret, clist);
	ret = ft_check_exists_two(ret, clist);
	return (ret);
}

int8_t
	ft_select_get(char **words, t_cub *clist)
{
	int8_t	id;

	if ((id = ft_get_id(words, clist)) == 12)
	{
		ft_free_words(words);
		return (12);
	}
	if (id < 0 || clist->get_ptr[id](words, clist) < 0)
	{
		ft_free_words(words);
		return ((id < 0) ? (-2) : (-1));
	}
	ft_free_words(words);
	return (id);
}