From a2f0bde9da402ddfd47ec54d42b83585c3296ebf Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Thu, 12 Mar 2020 23:47:37 +0100
Subject: Secured parsing

---
 Makefile                |  1 +
 inc/cub3d.h             |  1 +
 inc/cub3d_defines.h     |  6 ++++--
 inc/cub3d_structs.h     |  1 +
 map/map_one.cub         |  1 -
 src/ft_check_map_line.c |  1 +
 src/ft_check_missing.c  | 21 ++++++++++++++++++---
 src/ft_draw_map.c       |  2 +-
 src/ft_get_topsp.c      | 30 ++++++++++++++++++++++++++++++
 src/ft_init_map.c       | 23 ++++++++++++++++-------
 10 files changed, 73 insertions(+), 14 deletions(-)
 create mode 100644 src/ft_get_topsp.c

diff --git a/Makefile b/Makefile
index 4b27716..6f471b7 100644
--- a/Makefile
+++ b/Makefile
@@ -48,6 +48,7 @@ SRCS_NAME	+= ft_get_music.c
 SRCS_NAME	+= ft_get_darkness.c
 SRCS_NAME	+= ft_get_traps.c
 SRCS_NAME	+= ft_get_sprite_spawns.c
+SRCS_NAME	+= ft_get_topsp.c
 SRCS_NAME	+= ft_set_minimap_scale.c
 SRCS_NAME	+= ft_check_missing.c
 SRCS_NAME	+= ft_check_not_found.c
diff --git a/inc/cub3d.h b/inc/cub3d.h
index c08a994..2103295 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -118,6 +118,7 @@ int8_t			ft_check_ext(const char *filep, const char *ext);
 int8_t			ft_check_not_found(const char *path);
 int				ft_get_map_first_line(char *line, t_cub *clist);
 int				ft_get_map_core(int fd, t_cub *clist);
+void			ft_get_topsp(char c, t_cub *clist);
 void			ft_check_map_surrounds(t_map *ml, t_cub *cl);
 int				ft_check_missing(t_cub *clist);
 int				ft_missing_error(const char *err, t_cub *clist);
diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h
index 12c8580..2fbd325 100644
--- a/inc/cub3d_defines.h
+++ b/inc/cub3d_defines.h
@@ -26,7 +26,8 @@ enum
 	FT_RET_MAP_ERR,
 	FT_RET_NO_MAP,
 	FT_RET_READ_ERR,
-	FT_RET_BMP_ERR
+	FT_RET_BMP_ERR,
+	FT_ERR_MISSING
 }	retvals;
 
 /*
@@ -116,8 +117,9 @@ enum
 
 #	define FT_CHRST_VALID_PARSE		"RNSEWFCLMT"
 #	define FT_CHRST_MAP_ENTRY		"0123456789NSEWLT "
+#	define FT_CHRST_SPRITES			"23456789"
 #	define FT_CHRST_SPAWN			"NSEW"
-#	define FT_CHRST_MAP_NON_WALL	"02NESWLT"
+#	define FT_CHRST_MAP_NON_WALL	"023456789NESWLT"
 #	define FT_CHRST_COLLISION		"123456789 "
 #	define FT_CHRST_DETECT			"1L"
 
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h
index d0e0845..dd652ee 100644
--- a/inc/cub3d_structs.h
+++ b/inc/cub3d_structs.h
@@ -198,6 +198,7 @@ typedef struct			s_map
 	uint8_t				istraps;
 	uint8_t				darklvl;
 	uint8_t				scale;
+	int8_t				topsp;
 	uint32_t			nlx;
 	uint32_t			nly;
 	float				x_floor_step;
diff --git a/map/map_one.cub b/map/map_one.cub
index e9cebca..61ae184 100644
--- a/map/map_one.cub
+++ b/map/map_one.cub
@@ -14,7 +14,6 @@ S4 ./media/img/linus.xpm
 S5 ./media/img/crapaud.xpm
 S6 ./media/img/larry.xpm
 S7 ./media/img/linuz.xpm
-S8 ./media/img/linuz.xpm
 
 
 T ./media/img/spikes.xpm
diff --git a/src/ft_check_map_line.c b/src/ft_check_map_line.c
index c88e1f5..b9fd636 100644
--- a/src/ft_check_map_line.c
+++ b/src/ft_check_map_line.c
@@ -39,6 +39,7 @@ static int8_t
 	}
 	if (line[i] == 'T')
 		clist->mlist.istraps = 1;
+	ft_get_topsp(line[i], clist);
 	return (0);
 }
 
diff --git a/src/ft_check_missing.c b/src/ft_check_missing.c
index 51c0c23..d01a77f 100644
--- a/src/ft_check_missing.c
+++ b/src/ft_check_missing.c
@@ -20,7 +20,22 @@ int
 	ft_dprintf(STDERR_FILENO, "Error\n");
 	ft_dprintf(STDERR_FILENO,
 			"\033[1;31m%s %s\033[0m\n", FT_ERR_MISS_ELEMENT, err);
-	return (ft_exit(1, clist));
+	return (ft_exit(FT_ERR_MISSING, clist));
+}
+
+static int
+	ft_check_missing_sprites(t_cub *clist)
+{
+	int8_t	i;
+
+	i = 0;
+	while ((i + 2) <= clist->mlist.topsp)
+	{
+		if (!clist->mlist.sprite_path[i][0])
+			return (ft_missing_error(FT_ERR_MISS_SPRITE, clist));
+		i++;
+	}
+	return (0);
 }
 
 static int
@@ -46,7 +61,7 @@ static int
 		return (ft_missing_error(FT_ERR_MISS_NLVL_PATH, clist));
 	if (clist->mlist.istraps && !clist->mlist.traps_path[0])
 		return (ft_missing_error(FT_ERR_MISS_TRAP, clist));
-	return (0);
+	return (ft_check_missing_sprites(clist));
 }
 
 int
@@ -60,7 +75,7 @@ int
 		return (ft_missing_error(FT_ERR_MISS_EAST, clist));
 	else if (!clist->mlist.we_tex_path[0])
 		return (ft_missing_error(FT_ERR_MISS_WEST, clist));
-	else if (!clist->mlist.sprite_path[0])
+	else if (!clist->mlist.sprite_path[0][0])
 		return (ft_missing_error(FT_ERR_MISS_SPRITE, clist));
 	else if (clist->wlist.x_size == 0 || clist->wlist.y_size == 0)
 		return (ft_missing_error(FT_ERR_MISS_RESOLUTION, clist));
diff --git a/src/ft_draw_map.c b/src/ft_draw_map.c
index fc2613b..5d37566 100644
--- a/src/ft_draw_map.c
+++ b/src/ft_draw_map.c
@@ -55,7 +55,7 @@ void
 			else if (map[y][x] == '2')
 				ft_draw_square(scale + 9 + (x * (scale)),
 					ft_y_offset(clist) - 9 + (y * (scale)), 0x0033ccff, clist);
-			else if (ft_ischarset("0LNSEWT", map[y][x]))
+			else if (ft_ischarset(FT_CHRST_MAP_NON_WALL, map[y][x]))
 				ft_draw_square(scale + 9 + (x * (scale)), ft_y_offset(clist)
 					- 9 + (y * (scale)), 0x006afa6a, clist);
 			x++;
diff --git a/src/ft_get_topsp.c b/src/ft_get_topsp.c
new file mode 100644
index 0000000..ee16714
--- /dev/null
+++ b/src/ft_get_topsp.c
@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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>
+
+void
+	ft_get_topsp(char c, t_cub *clist)
+{
+	int8_t	x;
+
+	if ((x = ft_strlchr(FT_CHRST_SPRITES, c)) > 0)
+	{
+		if ((x + 2) > clist->mlist.topsp)
+		{
+			clist->mlist.topsp = x + 2;
+		}
+	}
+}
diff --git a/src/ft_init_map.c b/src/ft_init_map.c
index 8f6bb3d..813a795 100644
--- a/src/ft_init_map.c
+++ b/src/ft_init_map.c
@@ -56,6 +56,19 @@ static int8_t
 	return (0);
 }
 
+int8_t
+	ft_init_map_norme(t_map *mlist)
+{
+	mlist->scale = 0;
+	mlist->nlx = 0;
+	mlist->nly = 0;
+	mlist->sprite_var = 0;
+	mlist->topsp = 0;
+	ft_bzero(mlist->sprite_order, 4096);
+	ft_bzero(mlist->traps_order, 512);
+	return (0);
+}
+
 int8_t
 	ft_init_map(t_map *mlist)
 {
@@ -63,6 +76,8 @@ int8_t
 		return (-1);
 	mlist->map[1] = 0;
 	mlist->mcmd_words[1] = 0;
+	if (ft_calloc_sprites(mlist) < 0)
+		return (-1);
 	mlist->map_w = 0;
 	mlist->map_h = 0;
 	mlist->mapl_len = 0;
@@ -80,11 +95,5 @@ int8_t
 	mlist->sprite_nbr = 0;
 	mlist->traps_nbr = 0;
 	mlist->darklvl = 0;
-	mlist->scale = 0;
-	mlist->nlx = 0;
-	mlist->nly = 0;
-	mlist->sprite_var = 0;
-	ft_bzero(mlist->sprite_order, 4096);
-	ft_bzero(mlist->traps_order, 512);
-	return (ft_calloc_sprites(mlist));
+	return (ft_init_map_norme(mlist));
 }
-- 
cgit v1.2.3