fix: add check for 0 or 2+ players on a map
This commit is contained in:
parent
96cc9096cd
commit
465aacdefe
10 changed files with 115 additions and 81 deletions
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/01 13:59:04 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/11/12 15:10:07 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/11/18 14:26:30 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -26,30 +26,7 @@ typedef enum e_map_wall
|
|||
WALL,
|
||||
} t_map_wall;
|
||||
|
||||
/// @brief An object of the map (player, enemies?)
|
||||
typedef struct s_object
|
||||
{
|
||||
/// @brief Function called when creating the object.
|
||||
///
|
||||
/// @param data Address of the s_object.data pointer.
|
||||
/// @param pos Start position of the object.
|
||||
void (*init)(void **data, t_point_int pos);
|
||||
|
||||
/// @brief Function called when destroying the object (to avoid leaks).
|
||||
///
|
||||
/// @param data Address of the s_object.data pointer.
|
||||
/// @param pos Start position of the object.
|
||||
void (*destroy)(void **data, t_point_int pos);
|
||||
|
||||
/// @brief Function called each tick.
|
||||
///
|
||||
/// @param data Address of the s_object.data pointer.
|
||||
/// @param pos Start position of the object.
|
||||
void (*tick)(void **data, t_point_int pos);
|
||||
|
||||
/// @brief Pointer the object can use to save data.
|
||||
void *data;
|
||||
} t_object;
|
||||
typedef struct s_player t_player;
|
||||
|
||||
/// @brief Represents a case of the map.
|
||||
typedef struct s_map_case
|
||||
|
@ -60,9 +37,10 @@ typedef struct s_map_case
|
|||
/// @brief Is the case empty or a wall?
|
||||
t_map_wall wall;
|
||||
|
||||
/// @brief The object that appears on this case. If there is none,
|
||||
/// every pointer of the struct will be NULL.
|
||||
t_object object;
|
||||
/// @brief Initialize the player. NULL if there is no player on this case.
|
||||
/// @param dest Will be set to the player.
|
||||
/// @param pos Position of the case the player is on.
|
||||
void (*init_player)(t_player *dest, t_point_int pos);
|
||||
} t_map_case;
|
||||
|
||||
/// @brief Each element of g_map_mapping. A case and its associated char.
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/15 17:31:35 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/11/01 20:41:30 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/11/15 15:54:36 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -16,17 +16,18 @@
|
|||
# include "include.h"
|
||||
|
||||
# include "utils.h"
|
||||
# include "map.h"
|
||||
|
||||
/// @brief Create a player facing north (to use with t_object.init).
|
||||
void init_player_n(void **data, t_point_int pos);
|
||||
/// @brief Create a player facing north (to use with t_map_case.init_player).
|
||||
void init_player_n(t_player *dest, t_point_int pos);
|
||||
|
||||
/// @brief Create a player facing south (to use with t_object.init).
|
||||
void init_player_s(void **data, t_point_int pos);
|
||||
/// @brief Create a player facing south (to use with t_map_case.init_player).
|
||||
void init_player_s(t_player *dest, t_point_int pos);
|
||||
|
||||
/// @brief Create a player facing west (to use with t_object.init).
|
||||
void init_player_w(void **data, t_point_int pos);
|
||||
/// @brief Create a player facing west (to use with t_map_case.init_player).
|
||||
void init_player_w(t_player *dest, t_point_int pos);
|
||||
|
||||
/// @brief Create a player facing east (to use with t_object.init).
|
||||
void init_player_e(void **data, t_point_int pos);
|
||||
/// @brief Create a player facing east (to use with t_map_case.init_player).
|
||||
void init_player_e(t_player *dest, t_point_int pos);
|
||||
|
||||
#endif
|
||||
|
|
21
src/map1.c
21
src/map1.c
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/01 17:12:58 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/11/07 18:58:34 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/11/15 15:58:14 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -18,14 +18,14 @@
|
|||
#include "global.h"
|
||||
|
||||
const t_map_mapping_element g_map_mapping[] = {
|
||||
{' ', {false, EMPTY, {NULL, NULL, NULL, NULL}}},
|
||||
{'0', {true, EMPTY, {NULL, NULL, NULL, NULL}}},
|
||||
{'1', {true, WALL, {NULL, NULL, NULL, NULL}}},
|
||||
{'N', {true, EMPTY, {init_player_n, NULL, NULL, NULL}}},
|
||||
{'S', {true, EMPTY, {init_player_s, NULL, NULL, NULL}}},
|
||||
{'E', {true, EMPTY, {init_player_e, NULL, NULL, NULL}}},
|
||||
{'W', {true, EMPTY, {init_player_w, NULL, NULL, NULL}}},
|
||||
{'\0', {true, EMPTY, {NULL, NULL, NULL, NULL}}}};
|
||||
{' ', {false, EMPTY, NULL}},
|
||||
{'0', {true, EMPTY, NULL}},
|
||||
{'1', {true, WALL, NULL}},
|
||||
{'N', {true, EMPTY, init_player_n}},
|
||||
{'S', {true, EMPTY, init_player_s}},
|
||||
{'E', {true, EMPTY, init_player_e}},
|
||||
{'W', {true, EMPTY, init_player_w}},
|
||||
{'\0', {true, EMPTY, NULL}}};
|
||||
|
||||
static bool map_from_file2(t_map *dest, t_stream *stream, int fd)
|
||||
{
|
||||
|
@ -41,7 +41,6 @@ static bool map_from_file2(t_map *dest, t_stream *stream, int fd)
|
|||
}
|
||||
close(fd);
|
||||
free((void *)stream->str);
|
||||
map_init_objects(dest);
|
||||
return (success);
|
||||
}
|
||||
|
||||
|
@ -117,8 +116,6 @@ static bool check_map2(const t_map *map, unsigned int x, unsigned int y)
|
|||
return (true);
|
||||
}
|
||||
|
||||
// TODO check player
|
||||
|
||||
bool check_map(const t_map *map)
|
||||
{
|
||||
unsigned int x;
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/03 15:05:13 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/11/07 19:04:19 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/11/15 15:52:16 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -70,9 +70,11 @@ void fill_zeros(void *dest, size_t size);
|
|||
/// @return true if success, false if error.
|
||||
bool read_map_description(t_map *map, t_stream *stream);
|
||||
|
||||
/// @brief Initialize all objects of the map.
|
||||
/// @brief Initialize map->player.
|
||||
/// @param map Map to use.
|
||||
void map_init_objects(t_map *map);
|
||||
/// @return true if success, false if error
|
||||
/// (0 or >2 players found instead of only one).
|
||||
bool map_init_player(t_map *map);
|
||||
|
||||
bool load_textures(t_map *map);
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/03 15:02:09 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/11/07 18:59:07 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/11/15 16:24:09 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -106,26 +106,34 @@ bool read_map(t_map *d, t_stream *s)
|
|||
}
|
||||
if (!err[1] && !d->cases)
|
||||
err[0] = (write_err("Map description missing\n", NULL), true);
|
||||
(err[1] || err[0]) && (map_destroy(d), false);
|
||||
return (!err[0] && !err[1] && d->cases);
|
||||
return ((!err[1] && !err[0] && d->cases && map_init_player(d))
|
||||
|| (map_destroy(d), false));
|
||||
}
|
||||
|
||||
void map_init_objects(t_map *map)
|
||||
bool map_init_player(t_map *map)
|
||||
{
|
||||
t_point_int pos;
|
||||
int i;
|
||||
|
||||
pos.x = 0;
|
||||
while (pos.x < (int)map->width)
|
||||
map->player.pos.x = -1;
|
||||
pos.x = -1;
|
||||
while (++pos.x < (int)map->width)
|
||||
{
|
||||
pos.y = 0;
|
||||
while (pos.y < (int)map->height)
|
||||
pos.y = -1;
|
||||
while (++pos.y < (int)map->height)
|
||||
{
|
||||
i = pos.y * map->width + pos.x;
|
||||
if (map->cases[i].object.init)
|
||||
map->cases[i].object.init(&map->cases[i].object.data, pos);
|
||||
pos.y++;
|
||||
if (map->cases[i].init_player)
|
||||
{
|
||||
if (map->player.pos.x == -1)
|
||||
map->cases[i].init_player(&map->player, pos);
|
||||
else
|
||||
return (write_err("Map has more than one player.\n", NULL),
|
||||
false);
|
||||
}
|
||||
}
|
||||
pos.x++;
|
||||
}
|
||||
if (map->player.pos.x == -1)
|
||||
return (write_err("Map has no player.\n", NULL), false);
|
||||
return (true);
|
||||
}
|
||||
|
|
30
src/player.c
30
src/player.c
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/15 17:33:11 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/10/31 17:59:13 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/11/15 15:52:25 by mcolonna ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -15,33 +15,29 @@
|
|||
#include "global.h"
|
||||
#include "const.h"
|
||||
|
||||
static void init_player(t_point_int pos, double rot)
|
||||
static void init_player(t_player *dest, t_point_int pos, double rot)
|
||||
{
|
||||
g_map.player.pos.x = pos.x + 0.5;
|
||||
g_map.player.pos.y = pos.y + 0.5;
|
||||
g_map.player.rot = rot;
|
||||
dest->pos.x = pos.x + 0.5;
|
||||
dest->pos.y = pos.y + 0.5;
|
||||
dest->rot = rot;
|
||||
}
|
||||
|
||||
void init_player_n(void **data, t_point_int pos)
|
||||
void init_player_n(t_player *dest, t_point_int pos)
|
||||
{
|
||||
(void)data;
|
||||
init_player(pos, 0);
|
||||
init_player(dest, pos, 0);
|
||||
}
|
||||
|
||||
void init_player_s(void **data, t_point_int pos)
|
||||
void init_player_s(t_player *dest, t_point_int pos)
|
||||
{
|
||||
(void)data;
|
||||
init_player(pos, PI);
|
||||
init_player(dest, pos, PI);
|
||||
}
|
||||
|
||||
void init_player_w(void **data, t_point_int pos)
|
||||
void init_player_w(t_player *dest, t_point_int pos)
|
||||
{
|
||||
(void)data;
|
||||
init_player(pos, 3 * PI / 2);
|
||||
init_player(dest, pos, 3 * PI / 2);
|
||||
}
|
||||
|
||||
void init_player_e(void **data, t_point_int pos)
|
||||
void init_player_e(t_player *dest, t_point_int pos)
|
||||
{
|
||||
(void)data;
|
||||
init_player(pos, PI / 2);
|
||||
init_player(dest, pos, PI / 2);
|
||||
}
|
||||
|
|
13
testmaps/err_manymanyplayers.cub
Normal file
13
testmaps/err_manymanyplayers.cub
Normal file
|
@ -0,0 +1,13 @@
|
|||
F 255,127,0
|
||||
C 0,2,67
|
||||
EA textures/east.xpm
|
||||
NO textures/north.xpm
|
||||
SO textures/south.xpm
|
||||
WE textures/west.xpm
|
||||
111
|
||||
111110111
|
||||
1E000N0W1
|
||||
111110111
|
||||
101
|
||||
101
|
||||
111
|
13
testmaps/err_manyplayers.cub
Normal file
13
testmaps/err_manyplayers.cub
Normal file
|
@ -0,0 +1,13 @@
|
|||
F 255,127,0
|
||||
C 0,2,67
|
||||
EA textures/east.xpm
|
||||
NO textures/north.xpm
|
||||
SO textures/south.xpm
|
||||
WE textures/west.xpm
|
||||
111
|
||||
111110111
|
||||
10000N0W1
|
||||
111110111
|
||||
101
|
||||
101
|
||||
111
|
13
testmaps/err_noplayer.cub
Normal file
13
testmaps/err_noplayer.cub
Normal file
|
@ -0,0 +1,13 @@
|
|||
F 255,127,0
|
||||
C 0,2,67
|
||||
EA textures/east.xpm
|
||||
NO textures/north.xpm
|
||||
SO textures/south.xpm
|
||||
WE textures/west.xpm
|
||||
111
|
||||
111110111
|
||||
100000001
|
||||
111110111
|
||||
101
|
||||
101
|
||||
111
|
13
testmaps/err_playeroutside.cub
Normal file
13
testmaps/err_playeroutside.cub
Normal file
|
@ -0,0 +1,13 @@
|
|||
F 255,127,0
|
||||
C 0,2,67
|
||||
EA textures/east.xpm
|
||||
NO textures/north.xpm
|
||||
SO textures/south.xpm
|
||||
WE textures/west.xpm
|
||||
111
|
||||
111110111
|
||||
100000001
|
||||
111110111
|
||||
N101
|
||||
101
|
||||
111
|
Loading…
Add table
Reference in a new issue