42_solong/bonus/map.c

140 lines
3.1 KiB
C
Raw Normal View History

2023-01-05 13:04:29 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/05 13:51:49 by cchauvet #+# #+# */
2023-01-13 09:10:27 -05:00
/* Updated: 2023/01/13 13:56:07 by cchauvet ### ########.fr */
2023-01-05 13:04:29 -05:00
/* */
/* ************************************************************************** */
#include "solong.h"
char **ft_readfile(char *path)
{
char **map;
int fd;
2023-01-06 13:37:36 -05:00
size_t nb_line;
2023-01-05 13:04:29 -05:00
fd = open(path, O_RDONLY);
nb_line = 1;
2023-01-09 14:41:06 -05:00
map = ft_calloc(sizeof(char *), 2);
2023-01-05 13:04:29 -05:00
if (map == NULL)
return (NULL);
map[0] = get_next_line(fd);
while (map[nb_line - 1] != NULL)
{
2023-01-06 13:37:36 -05:00
ft_strchr(map[nb_line - 1], '\n')[0] = '\0';
2023-01-05 13:04:29 -05:00
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);
}
2023-01-06 13:37:36 -05:00
2023-01-13 09:10:27 -05:00
void ft_fill_pos(t_map *map)
2023-01-09 14:41:06 -05:00
{
size_t x;
size_t y;
2023-01-13 09:10:27 -05:00
y = 1;
while (map->patern[y + 1] != NULL)
2023-01-09 14:41:06 -05:00
{
x = 0;
2023-01-13 09:10:27 -05:00
while (x < ft_strlen(map->patern[y]))
2023-01-09 14:41:06 -05:00
{
2023-01-13 09:10:27 -05:00
if (map->patern[y][x] == 'P')
2023-01-09 14:41:06 -05:00
{
2023-01-13 09:10:27 -05:00
map->player_pos[0] = x;
map->player_pos[1] = y;
2023-01-09 14:41:06 -05:00
}
2023-01-13 09:10:27 -05:00
if (map->patern[y][x] == 'E')
2023-01-09 14:41:06 -05:00
{
2023-01-13 09:10:27 -05:00
map->exit_pos[0] = x;
map->exit_pos[1] = y;
2023-01-09 14:41:06 -05:00
}
x++;
}
y++;
}
}
2023-01-06 13:37:36 -05:00
t_map *ft_getmap(char *path)
{
t_map *map;
2023-01-09 14:41:06 -05:00
map = ft_calloc(sizeof(t_map), 1);
2023-01-06 13:37:36 -05:00
if (map == NULL)
return (NULL);
map->patern = ft_readfile(path);
if (ft_map_is_correct(map) == 0)
2023-01-13 09:10:27 -05:00
{
ft_freer_tab_ultimate(1, map->patern);
free(map);
2023-01-06 13:37:36 -05:00
return (NULL);
2023-01-13 09:10:27 -05:00
}
2023-01-06 13:37:36 -05:00
return (map);
}
2023-01-10 13:04:46 -05:00
static char *ft_get_line(const char *line, ssize_t player_x)
{
char *out;
2023-01-10 13:04:46 -05:00
ssize_t start;
ssize_t stop;
ssize_t i;
ssize_t y;
out = ft_strgen('0', RENDER_DISTANCE * 2 + 1);
if (out == NULL)
return (NULL);
2023-01-10 13:04:46 -05:00
start = player_x - (RENDER_DISTANCE);
if (start < 0)
start = 0;
stop = player_x + RENDER_DISTANCE + 1;
if (stop > (ssize_t) ft_strlen(line))
stop = ft_strlen(line);
i = 0;
2023-01-10 13:04:46 -05:00
y = player_x - RENDER_DISTANCE - start;
if (y < 0)
y = -y;
while (start + i < stop)
{
2023-01-10 13:04:46 -05:00
out[y + i] = line[i + start];
i++;
}
return (out);
}
2023-01-06 13:37:36 -05:00
char **ft_get_player_map(t_map map)
{
char **player_map;
2023-01-10 13:04:46 -05:00
ssize_t y;
ssize_t i;
2023-01-06 13:37:36 -05:00
2023-01-09 14:41:06 -05:00
player_map = ft_calloc(RENDER_DISTANCE * 2 + 2, sizeof(char *));
2023-01-06 13:37:36 -05:00
if (player_map == NULL)
return (NULL);
2023-01-10 13:04:46 -05:00
i = map.player_pos[1] - RENDER_DISTANCE;
if (i < 0)
i = 0;
y = -1;
while (++y < RENDER_DISTANCE * 2 + 1)
2023-01-06 13:37:36 -05:00
{
if (map.player_pos[1] + y >= RENDER_DISTANCE && map.patern[i] != NULL)
2023-01-10 13:04:46 -05:00
player_map[y] = ft_get_line(map.patern[i++], map.player_pos[0]);
else
player_map[y] = ft_strgen('0', RENDER_DISTANCE * 2 + 1);
2023-01-06 13:37:36 -05:00
if (player_map[y] == NULL)
{
ft_cancel(player_map, y);
2023-01-06 13:37:36 -05:00
return (NULL);
}
}
player_map[y] = NULL;
return (player_map);
}