fix: add check for 0 or 2+ players on a map

This commit is contained in:
mcolonna 2024-11-18 14:29:56 +01:00
commit 51d20f95d9
10 changed files with 115 additions and 81 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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);
}

View file

@ -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);
}