116 lines
2.8 KiB
C
116 lines
2.8 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* map.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/01/05 13:51:49 by cchauvet #+# #+# */
|
|
/* Updated: 2023/01/08 14:48:51 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "solong.h"
|
|
|
|
char **ft_readfile(char *path)
|
|
{
|
|
char **map;
|
|
int fd;
|
|
size_t nb_line;
|
|
|
|
fd = open(path, O_RDONLY);
|
|
nb_line = 1;
|
|
map = malloc(sizeof(char *) * 2);
|
|
if (map == NULL)
|
|
return (NULL);
|
|
map[0] = get_next_line(fd);
|
|
while (map[nb_line - 1] != NULL)
|
|
{
|
|
ft_strchr(map[nb_line - 1], '\n')[0] = '\0';
|
|
map[nb_line] = get_next_line(fd);
|
|
map = ft_tabrealloc(map, nb_line + 1, nb_line + 2);
|
|
if (map == NULL)
|
|
return (NULL);
|
|
nb_line++;
|
|
}
|
|
return (map);
|
|
}
|
|
|
|
t_map *ft_getmap(char *path)
|
|
{
|
|
t_map *map;
|
|
|
|
map = malloc(sizeof(t_map));
|
|
if (map == NULL)
|
|
return (NULL);
|
|
map->exit[2] = 0;
|
|
map->player_pos[2] = 0;
|
|
map->patern = ft_readfile(path);
|
|
if (ft_map_is_correct(map) == 0)
|
|
return (NULL);
|
|
return (map);
|
|
}
|
|
|
|
static char *ft_get_line(const char *line, size_t player_x)
|
|
{
|
|
char *out;
|
|
size_t start;
|
|
size_t stop;
|
|
size_t temp;
|
|
size_t i;
|
|
|
|
temp = ft_strlen(line);
|
|
if (player_x + RENDER_DISTANCE > temp)
|
|
stop = temp;
|
|
else
|
|
stop = player_x + RENDER_DISTANCE;
|
|
if (player_x < RENDER_DISTANCE + 1)
|
|
start = RENDER_DISTANCE - player_x;
|
|
else
|
|
start = player_x - RENDER_DISTANCE;
|
|
if (RENDER_DISTANCE > player_x)
|
|
temp = 0;
|
|
else
|
|
temp = player_x - RENDER_DISTANCE;
|
|
out = ft_strgen('0', RENDER_DISTANCE * 2 + 1);
|
|
if (out == NULL)
|
|
return (NULL);
|
|
i = 0;
|
|
while (i < stop + 1)
|
|
{
|
|
out[start + i] = line[temp + i];
|
|
i++;
|
|
}
|
|
return (out);
|
|
}
|
|
|
|
char **ft_get_player_map(t_map map)
|
|
{
|
|
char **player_map;
|
|
size_t y;
|
|
size_t i;
|
|
|
|
player_map = malloc(RENDER_DISTANCE * 2 + 2);
|
|
if (player_map == NULL)
|
|
return (NULL);
|
|
i = 0;
|
|
y = 0;
|
|
while (y < RENDER_DISTANCE * 2 + 1)
|
|
{
|
|
player_map[y] = ft_strgen('0', RENDER_DISTANCE * 2 + 1);
|
|
if (map.player_pos[1] + y >= RENDER_DISTANCE && map.patern[i] != NULL)
|
|
{
|
|
player_map[y] = ft_get_line(map.patern[i], map.player_pos[0]);
|
|
i++;
|
|
}
|
|
if (player_map[y] == NULL)
|
|
{
|
|
ft_cancel(player_map, y);
|
|
return (NULL);
|
|
}
|
|
y++;
|
|
}
|
|
player_map[y] = NULL;
|
|
return (player_map);
|
|
}
|