mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2026-11-09 13:47:10 +01:00
versiontexturepositioncouleurok
This commit is contained in:
102
parsing/ft_find_player_position.c
Normal file
102
parsing/ft_find_player_position.c
Normal file
@@ -0,0 +1,102 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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;
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:43:46 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/16 15:22:35 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/23 20:06:58 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,6 +16,8 @@ bool init_map_info(t_structure_map *map_info, int maxWidth, int height)
|
||||
{
|
||||
map_info->mapX = maxWidth;
|
||||
map_info->mapY = height;
|
||||
map_info->s_win.width = WIDTH;
|
||||
map_info->s_win.height = HEIGHT;
|
||||
map_info->map = malloc(maxWidth * height);
|
||||
if (!map_info->map)
|
||||
{
|
||||
@@ -29,8 +31,7 @@ bool init_map_info(t_structure_map *map_info, int maxWidth, int height)
|
||||
bool copy_map_data_and_check(t_map_params *params)
|
||||
{
|
||||
copy_map_data(params);
|
||||
if (!is_map_closed(params->map_info->map,
|
||||
*params->maxWidth, *params->height))
|
||||
if (!is_map_closed(params->map_info->map, *params->maxWidth, *params->height))
|
||||
{
|
||||
printf("Map is not closed!\n");
|
||||
free(params->map_info->map);
|
||||
@@ -39,33 +40,25 @@ bool copy_map_data_and_check(t_map_params *params)
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool process_map(const char *buffer, int length, t_structure_map *map_info)
|
||||
void calculate_map(t_structure_map *map_info)
|
||||
{
|
||||
int maxwidth;
|
||||
int height;
|
||||
t_map_params map_params;
|
||||
float maps_x;
|
||||
float maps_y;
|
||||
|
||||
maxwidth = 0;
|
||||
height = 0;
|
||||
map_params.map_info = map_info;
|
||||
map_params.buffer = buffer;
|
||||
map_params.length = length;
|
||||
map_params.maxWidth = &maxwidth;
|
||||
map_params.height = &height;
|
||||
map_params.currentWidth = NULL;
|
||||
map_params.isNewLine = NULL;
|
||||
get_map_dimensions(&map_params);
|
||||
if (maxwidth <= 0 || height <= 0)
|
||||
maps_x = (float)map_info->s_win.width / (map_info->mapX * 4);
|
||||
maps_y = (float)map_info->s_win.height / (map_info->mapY * 2);
|
||||
if (maps_x < maps_y)
|
||||
{
|
||||
printf("Invalid map dimensions: maxWidth=%d, height=%d\n",
|
||||
maxwidth, height);
|
||||
return (false);
|
||||
map_info->mapS = (int)maps_x;
|
||||
}
|
||||
if (!init_map_info(map_info, maxwidth, height))
|
||||
else
|
||||
{
|
||||
return (false);
|
||||
map_info->mapS = (int)maps_y;
|
||||
}
|
||||
if (map_info->mapS == 0)
|
||||
{
|
||||
map_info->mapS = 1;
|
||||
}
|
||||
return (copy_map_data_and_check(&map_params));
|
||||
}
|
||||
|
||||
bool parse_map(const char *map_content, int length, t_structure_map *map_info)
|
||||
@@ -73,24 +66,23 @@ bool parse_map(const char *map_content, int length, t_structure_map *map_info)
|
||||
t_map_params map_params;
|
||||
int max_width;
|
||||
int height;
|
||||
|
||||
|
||||
max_width = 0;
|
||||
height = 0;
|
||||
printf("Received map content (length = %d): \n%s\n", length, map_content);
|
||||
map_params.map_info = map_info;
|
||||
map_params.buffer = map_content;
|
||||
map_params.length = length;
|
||||
map_params.maxWidth = &max_width;
|
||||
map_params.height = &height;
|
||||
map_params.currentWidth = NULL;
|
||||
map_params.isNewLine = NULL;
|
||||
get_map_dimensions(&map_params);
|
||||
if (max_width <= 0 || height <= 0)
|
||||
{
|
||||
printf("Invalid map dimensions: maxWidth=%d, height=%d\n", max_width, height);
|
||||
printf("Invalid map dimensions\n");
|
||||
return (false);
|
||||
}
|
||||
if (!init_map_info(map_info, max_width, height))
|
||||
return (false);
|
||||
if (!find_player_position_and_direction(map_content, length, map_info))
|
||||
return (false);
|
||||
return (copy_map_data_and_check(&map_params));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user