mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2025-12-15 21:56:57 +01:00
102 lines
3.2 KiB
C
102 lines
3.2 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_find_player_position.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/01/23 19:54:51 by fgras-ca #+# #+# */
|
|
/* Updated: 2024/01/23 20:22:20 by fgras-ca ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../cub3d.h"
|
|
|
|
static void initialize_variables(int *i, int *line_number, int *column_number, bool *found_player)
|
|
{
|
|
*i = 0;
|
|
*line_number = 0;
|
|
*column_number = 0;
|
|
*found_player = false;
|
|
}
|
|
|
|
static bool check_for_player(char current_char, bool *found_player)
|
|
{
|
|
if (current_char == 'N' || current_char == 'S' || current_char == 'E' || current_char == 'W')
|
|
{
|
|
if (*found_player)
|
|
{
|
|
printf("Multiple player start positions found. Invalid map.\n");
|
|
return false;
|
|
}
|
|
*found_player = true;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static void update_player_info(t_structure_map *map_info, int line_number, int column_number, char player_direction)
|
|
{
|
|
map_info->player_x = column_number * map_info->mapS + map_info->mapS / 2;
|
|
map_info->player_y = line_number * map_info->mapS + map_info->mapS / 2;
|
|
map_info->player_direction = player_direction;
|
|
printf("Player found: x = %f, y = %f, direction = %c\n", map_info->player_x, map_info->player_y, map_info->player_direction);
|
|
}
|
|
|
|
|
|
static void update_position(char current_char, int *line_number, int *column_number)
|
|
{
|
|
if (current_char == '\n')
|
|
{
|
|
(*line_number)++;
|
|
*column_number = 0;
|
|
}
|
|
else
|
|
{
|
|
(*column_number)++;
|
|
}
|
|
}
|
|
|
|
static bool check_for_multiple_players(bool found_player)
|
|
{
|
|
if (found_player)
|
|
{
|
|
printf("Multiple player start positions found. Invalid map.\n");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool find_player_position_and_direction(const char *map_content, int length, t_structure_map *map_info)
|
|
{
|
|
int i;
|
|
int line_number;
|
|
int column_number;
|
|
bool found_player;
|
|
|
|
initialize_variables(&i, &line_number, &column_number, &found_player);
|
|
while (i < length)
|
|
{
|
|
char current_char = map_content[i];
|
|
if (!found_player && (current_char == 'N' || current_char == 'S' || current_char == 'E' || current_char == 'W'))
|
|
{
|
|
if (check_for_multiple_players(found_player))
|
|
{
|
|
found_player = true;
|
|
update_player_info(map_info, line_number, column_number, current_char);
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
calculate_map(map_info);
|
|
update_position(current_char, &line_number, &column_number);
|
|
i++;
|
|
}
|
|
if (!found_player)
|
|
{
|
|
printf("Player start position not found. Invalid map.\n");
|
|
return false;
|
|
}
|
|
return true;
|
|
} |