From e7cc81387ec2ed71470f2c099b45be1eaf493fd4 Mon Sep 17 00:00:00 2001 From: Baptiste Goulard coderc de lacam Date: Fri, 20 Dec 2024 17:12:49 +0100 Subject: [PATCH] Fix: added check on missing player --- Makefile | 1 + includes/cub3d_parsing.h | 4 +- includes/cub3d_struct.h | 3 +- parsing/load_tiles.c | 80 ++++++++++------------------------------ parsing/set_player.c | 67 +++++++++++++++++++++++++++++++++ sources/error.c | 3 +- 6 files changed, 95 insertions(+), 63 deletions(-) create mode 100644 parsing/set_player.c diff --git a/Makefile b/Makefile index ebce9dd..c4ae664 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ SRC =\ parsing/load_textures.c \ parsing/load_bgs.c \ parsing/traverse_map.c \ + parsing/set_player.c \ parsing/load_file.c \ parsing/utils.c \ parsing/map.c \ diff --git a/includes/cub3d_parsing.h b/includes/cub3d_parsing.h index d66f95a..45d3287 100644 --- a/includes/cub3d_parsing.h +++ b/includes/cub3d_parsing.h @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/11 14:41:07 by bgoulard #+# #+# */ -/* Updated: 2024/12/01 17:53:02 by bgoulard ### ########.fr */ +/* Updated: 2024/12/20 17:07:58 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define CUB3D_PARSING_H # include "cub3d_struct.h" +# include "ft_vector_types.h" # include # define C3D_PRS_PLS "SO" @@ -27,6 +28,7 @@ t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos); bool is_identifier(const char *str, const char **id_str); +int set_player(t_info *info, int i, t_vector *str_map); void *load_file(void *data); void *load_bgs(void *data); diff --git a/includes/cub3d_struct.h b/includes/cub3d_struct.h index 3bc24e9..34985dc 100644 --- a/includes/cub3d_struct.h +++ b/includes/cub3d_struct.h @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/28 13:53:54 by bgoulard #+# #+# */ -/* Updated: 2024/12/20 16:54:34 by bgoulard ### ########.fr */ +/* Updated: 2024/12/20 17:01:40 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -140,6 +140,7 @@ typedef enum e_error ERROR_PARSE_ALREADY_SET, ERROR_PARSE_META_IN_MAP, ERROR_PARSE_MULTIPLE_PLAYER, + ERROR_NO_PLAYER, ERROR_CLI, ERROR_MLX, ERROR_TEXTURE_FORMAT, diff --git a/parsing/load_tiles.c b/parsing/load_tiles.c index 1a17126..de2abf5 100644 --- a/parsing/load_tiles.c +++ b/parsing/load_tiles.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/01 17:47:15 by bgoulard #+# #+# */ -/* Updated: 2024/12/20 16:54:12 by bgoulard ### ########.fr */ +/* Updated: 2024/12/20 17:09:44 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ #include "ft_math.h" #include +#include #include int str_to_tile(const char *str, t_tile *tile, size_t size) @@ -67,69 +68,14 @@ t_vector *load_vector(t_map *map) return (str_map); } -#define EPMP ERROR_PARSE_MULTIPLE_PLAYER +#define EPMIM ERROR_PARSE_META_IN_MAP +#define ENP ERROR_NO_PLAYER -static bool multiple_player_same_line(const char *str) +t_info *load_tiles_norm(t_info *info, t_vector *str_map) { - const char *identifiers = "NEWS"; - int i; - int j; + size_t i; i = 0; - while (i < 4) - { - if (ft_strchr(str, identifiers[i]) != ft_strrchr(str, identifiers[i])) - return (true); - j = i + 1; - while (j < 4) - if (ft_strchr(str, identifiers[j++]) && \ - ft_strchr(str, identifiers[i])) - return (true); - i++; - } - return (false); -} - -int set_player(t_info *info, int i, t_vector *str_map) -{ - t_dpoint pos; - char *str; - - ft_bzero(&pos, sizeof(t_dpoint)); - if (info->player.pos.x != 0 || info->player.pos.y != 0 || i == 0 || \ - multiple_player_same_line(ft_vec_at(str_map, i))) - return (ft_vec_destroy(&str_map), sv_errno(info, EPMP), EXIT_FAILURE); - str = ft_strchrs(ft_vec_at(str_map, i), "SNWE"); - pos.y = i + .5; - pos.x = str - (char *)ft_vec_at(str_map, i) + .5; - info->player.dir = (t_dpoint){.x = 0, 0}; - info->player.pos = pos; - info->player.pos_i = (t_ipoint){.x = (int)pos.x, .y = (int)pos.y}; - if (*str == 'N') - info->player.dir.y = -1; - else if (*str == 'S') - info->player.dir.y = 1; - else if (*str == 'W') - info->player.dir.x = -1; - else if (*str == 'E') - info->player.dir.x = 1; - info->player.plane = (t_dpoint){.x = info->player.dir.y, \ - .y = -info->player.dir.x}; - return (EXIT_SUCCESS); -} - -#define EPMIM ERROR_PARSE_META_IN_MAP - -void *load_tiles(void *data) -{ - t_info *info; - t_vector *str_map; - size_t i; - - info = (t_info *)data; - str_map = load_vector(&info->map); - if (!str_map) - return (sv_errno(info, ERROR_MALLOC), NULL); info->map.map = ft_calloc(sizeof(t_tile), (info->map.size.y * info->map.size.x)); if (!info->map.map) @@ -145,5 +91,19 @@ void *load_tiles(void *data) return (NULL); i++; } + if (info->player.pos_i.x == 0) + return (ft_vec_destroy(&str_map), sv_errno(info, ENP), NULL); return (ft_vec_destroy(&str_map), info); } + +void *load_tiles(void *data) +{ + t_info *info; + t_vector *str_map; + + info = (t_info *)data; + str_map = load_vector(&info->map); + if (!str_map) + return (sv_errno(info, ERROR_MALLOC), NULL); + return (load_tiles_norm(info, str_map)); +} diff --git a/parsing/set_player.c b/parsing/set_player.c new file mode 100644 index 0000000..a53b0a2 --- /dev/null +++ b/parsing/set_player.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_player.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/20 17:05:46 by bgoulard #+# #+# */ +/* Updated: 2024/12/20 17:09:59 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" +#include "ft_vector.h" +#include "ft_string.h" +#include "ft_addons.h" + +#define EPMP ERROR_PARSE_MULTIPLE_PLAYER + +static bool multiple_player_same_line(const char *str) +{ + const char *identifiers = "NEWS"; + int i; + int j; + + i = 0; + while (i < 4) + { + if (ft_strchr(str, identifiers[i]) != ft_strrchr(str, identifiers[i])) + return (true); + j = i + 1; + while (j < 4) + if (ft_strchr(str, identifiers[j++]) && \ + ft_strchr(str, identifiers[i])) + return (true); + i++; + } + return (false); +} + +int set_player(t_info *info, int i, t_vector *str_map) +{ + t_dpoint pos; + char *str; + + ft_bzero(&pos, sizeof(t_dpoint)); + if (info->player.pos.x != 0 || info->player.pos.y != 0 || i == 0 || \ + multiple_player_same_line(ft_vec_at(str_map, i))) + return (ft_vec_destroy(&str_map), sv_errno(info, EPMP), EXIT_FAILURE); + str = ft_strchrs(ft_vec_at(str_map, i), "SNWE"); + pos.y = i + .5; + pos.x = str - (char *)ft_vec_at(str_map, i) + .5; + info->player.dir = (t_dpoint){.x = 0, 0}; + info->player.pos = pos; + info->player.pos_i = (t_ipoint){.x = (int)pos.x, .y = (int)pos.y}; + if (*str == 'N') + info->player.dir.y = -1; + else if (*str == 'S') + info->player.dir.y = 1; + else if (*str == 'W') + info->player.dir.x = -1; + else if (*str == 'E') + info->player.dir.x = 1; + info->player.plane = (t_dpoint){.x = info->player.dir.y, \ + .y = -info->player.dir.x}; + return (EXIT_SUCCESS); +} diff --git a/sources/error.c b/sources/error.c index dab3d02..f8fd685 100644 --- a/sources/error.c +++ b/sources/error.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 11:09:00 by rparodi #+# #+# */ -/* Updated: 2024/12/20 15:52:43 by bgoulard ### ########.fr */ +/* Updated: 2024/12/20 17:02:37 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,6 +42,7 @@ const char **get_error_message(void) err_msg[ERROR_PARSE_NO_BG_COLOR] = "no background color provided"; err_msg[ERROR_PARSE_META_IN_MAP] = "meta data in map (should be above)"; err_msg[ERROR_PARSE_MULTIPLE_PLAYER] = "multiple player in map"; + err_msg[ERROR_NO_PLAYER] = "no player in map"; return (err_msg); }