diff --git a/Makefile b/Makefile index 4414c32..ebce9dd 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,7 @@ LDFLAGS += $(MLXFLAGS) SRC =\ ft_addons/ft_inrange.c \ + ft_addons/ft_strchrs.c \ mlx_layer/mlx_init.c \ mlx_layer/looks.c \ mlx_layer/mooves.c \ diff --git a/ft_addons/ft_strchrs.c b/ft_addons/ft_strchrs.c new file mode 100644 index 0000000..7f4db63 --- /dev/null +++ b/ft_addons/ft_strchrs.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchrs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/20 15:56:28 by bgoulard #+# #+# */ +/* Updated: 2024/12/20 15:58:47 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +void *ft_strchrs(const char *str, const char *chrs) +{ + while (*str) + { + if (ft_strchr(chrs, *str)) + return ((void *)str); + str++; + } + return (NULL); +} diff --git a/includes/cub3d_struct.h b/includes/cub3d_struct.h index bd936d0..c788560 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 14:47:00 by bgoulard ### ########.fr */ +/* Updated: 2024/12/20 15:42:21 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -138,6 +138,7 @@ typedef enum e_error ERROR_PARSE_NO_BG_COLOR, ERROR_PARSE_ALREADY_SET, ERROR_PARSE_META_IN_MAP, + ERROR_PARSE_MULTIPLE_PLAYER, ERROR_CLI, ERROR_MLX, ERROR_TEXTURE_FORMAT, diff --git a/includes/ft_addons.h b/includes/ft_addons.h index c531d3d..743a8ef 100644 --- a/includes/ft_addons.h +++ b/includes/ft_addons.h @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/16 06:09:01 by bgoulard #+# #+# */ -/* Updated: 2024/12/16 15:34:31 by bgoulard ### ########.fr */ +/* Updated: 2024/12/20 15:57:10 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,4 +30,6 @@ bool ft_inrange_ex(int value, int min, int max); /// @return true if the value is in the range, false otherwise bool ft_inrange(int value, int min, int max); +void *ft_strchrs(const char *str, const char *chrs); + #endif diff --git a/parsing/load_tiles.c b/parsing/load_tiles.c index 555e4ad..85448bf 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 14:46:40 by bgoulard ### ########.fr */ +/* Updated: 2024/12/20 15:59:04 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,22 +15,13 @@ #include "cub3d_parsing.h" #include "ft_string.h" +#include "ft_addons.h" #include "ft_vector.h" #include "ft_math.h" +#include #include -void *ft_strchrs(const char *str, const char *chrs) -{ - while (*str) - { - if (ft_strchr(chrs, *str)) - return ((void *)str); - str++; - } - return (NULL); -} - int str_to_tile(const char *str, t_tile *tile, size_t size) { size_t i; @@ -74,15 +65,44 @@ t_vector *load_vector(t_map *map) return (str_map); } +#define EPMP ERROR_PARSE_MULTIPLE_PLAYER + +static bool multiple_player_same_line(const char *str) +{ + const bool p_symbol[8] = {\ + !ft_strchr(str, 'S'), !ft_strchr(str, 'E'), !ft_strchr(str, 'W'), \ + !ft_strchr(str, 'N'), !(ft_strrchr(str, 'N') - ft_strchr(str, 'N')), \ + !(ft_strrchr(str, 'E') - ft_strchr(str, 'E')), \ + !(ft_strrchr(str, 'W') - ft_strchr(str, 'W')), \ + !(ft_strrchr(str, 'S') - ft_strchr(str, 'S')), }; + int i; + int j; + + i = 0; + while (i < 4) + { + j = 0; + while (j < 4) + if (p_symbol[j++] == true && p_symbol[i] == true) + return (true); + i++; + } + i = 0; + while (i < 4) + if (p_symbol[i++ + 4]) + return (true); + 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) - return (ft_vec_destroy(&str_map), info->last_error = ERROR_PARSE, \ - EXIT_FAILURE); + 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; diff --git a/sources/error.c b/sources/error.c index 65ba971..dab3d02 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 14:43:52 by bgoulard ### ########.fr */ +/* Updated: 2024/12/20 15:52:43 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,6 +41,7 @@ const char **get_error_message(void) err_msg[ERROR_TEXTURE_MISSING] = "texture missing"; 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"; return (err_msg); } diff --git a/sources/main.c b/sources/main.c index fe1d716..0c35e60 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */ -/* Updated: 2024/12/17 17:17:12 by bgoulard ### ########.fr */ +/* Updated: 2024/12/20 15:54:59 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,12 +41,12 @@ void run_cub3d(t_info *info) if (!info->mlx_ptr) return (sv_errno(info, ERROR_MLX), (void)0); parse_map(info); + if (info->last_error != NO_ERROR) + return ; if (init_mlx_env(info) != NO_ERROR) return (c3_perror(info)); if (info->cli_ctx.debug) ft_putstr_fd("no debug mod on production run", STDERR_FILENO); - if (info->last_error != NO_ERROR) - return ; if (info->cli_ctx.no_graphics == true) return ; info->camera.screen_buff = mlx_new_image(info->mlx_ptr, info->screen_size.x,