/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* map.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: kdx #include static t_err check_path(const char *path) { if (ft_strlen(path) <= ft_strlen(".ber")) return (1); if (ft_strncmp(path + ft_strlen(path) - ft_strlen(".ber"), ".ber", 4)) return (1); return (0); } static t_err find_exit(t_map *map) { size_t i; i = 0; while (i < (map->width + 1) * map->height) { if (map->data[i] == TILE_EXIT) { map->exit[0] = (i % (map->width + 1)) * TSIZE; map->exit[1] = (i / (map->width + 1)) * TSIZE; return (0); } i++; } return (1); } t_err map_load(t_map *map, const char *path) { int fd; t_err err; if (check_path(path)) return (ERR_FILENAME); fd = open(path, O_RDONLY); if (fd < 0) return (ERR_OPEN); map->data = (unsigned char *)readall(fd); close(fd); if (map->data == NULL) return (ERR_READ); err = map_verify(map, (unsigned const char *)map->data); if (err) return (err); if (find_exit(map)) return (ERR_MISSING); return (ERR_NONE); } void map_destroy(t_map *map) { ft_free(map->data); map->data = NULL; } const char *map_load_error(t_err err) { if (err == ERR_OPEN) return ("Failed to open file."); if (err == ERR_READ) return ("Failed to read file."); if (err == ERR_MALLOC) return ("Failed to malloc."); if (err == ERR_INVALID_CHAR) return ("Found invalid character in loaded map."); if (err == ERR_WRONG_DIMENSION) return ("Wrong map dimensions."); if (err == ERR_FILENAME) return ("Invalid filename."); if (err == ERR_MISSING) return ("Missing required character."); if (err == ERR_DUPLICATE) return ("Duplicate unique character."); if (err == ERR_OPEN_BORDER) return ("Borders aren't closed properly."); if (err == ERR_NO_PATH) return ("Map is impossible."); return ("No error."); }