/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* map.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cchauvet patern[y + 1] != NULL) { x = 0; while (x < ft_strlen(map->patern[y])) { if (map->patern[y][x] == 'P') { map->player_pos[0] = x; map->player_pos[1] = y; } if (map->patern[y][x] == 'E') { map->exit_pos[0] = x; map->exit_pos[1] = y; } x++; } y++; } } t_map *ft_getmap(char *path) { t_map *map; map = ft_calloc(sizeof(t_map), 1); if (map == NULL) return (NULL); map->patern = ft_readfile(path); if (ft_map_is_correct(map) == 0) { ft_freer_tab_ultimate(1, map->patern); free(map); return (NULL); } return (map); } static char *ft_get_line(const char *line, ssize_t player_x) { char *out; 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); 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; y = player_x - RENDER_DISTANCE - start; if (y < 0) y = -y; while (start + i < stop) { out[y + i] = line[i + start]; i++; } return (out); } char **ft_get_player_map(t_map map) { char **player_map; ssize_t y; ssize_t i; player_map = ft_calloc(RENDER_DISTANCE * 2 + 2, sizeof(char *)); if (player_map == NULL) return (NULL); i = map.player_pos[1] - RENDER_DISTANCE; if (i < 0) i = 0; y = -1; while (++y < 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]); else player_map[y] = ft_strgen('0', RENDER_DISTANCE * 2 + 1); if (player_map[y] == NULL) { ft_cancel(player_map, y); return (NULL); } } player_map[y] = NULL; return (player_map); }