diff --git a/Makefile b/Makefile index b9f7323..8e433d0 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,8 @@ SRCS = main.c \ player.c \ rectangle_check.c \ utils.c \ - graphics_utils.c + graphics_utils.c \ + location_utils.c LIBFT_FLAGS = -L./libft -l:libft.a OBJ_DIRECTORY = ./.obj/ @@ -33,7 +34,7 @@ OBJ_DIRECTORY = ./.obj/ CC = cc -FLAGS = -c -Wall -Wextra -Werror +FLAGS = -c -Wall -Wextra -Werror -g INCLUDES = ./headers/so_long.h diff --git a/collectible_utils.c b/collectible_utils.c index 6774bc2..1eb9373 100644 --- a/collectible_utils.c +++ b/collectible_utils.c @@ -6,83 +6,93 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 03:24:55 by jbadaire #+# #+# */ -/* Updated: 2023/10/03 03:47:04 by jbadaire ### ########.fr */ +/* Updated: 2023/10/05 16:31:17 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -t_list *load_collectibles(t_data data) { - size_t pos_x; - size_t pos_y; - t_list *list; - t_collectible *collectible; +t_list *load_collectibles(t_world world) { + int pos_x; + int pos_y; + t_list **list; + t_collectible collectible; pos_x = 0; pos_y = 0; - list = malloc(sizeof (t_list)); + list = malloc(sizeof (t_list *)); if(!list) return (NULL); - while (pos_y < data.lenght_y) { - while (pos_x < ft_strlen(data.map[0])) { - if (data.map[pos_y][pos_x] == 'C') { + while (world.map[pos_y]) { + while (world.map[pos_y] && pos_x < (int) ft_strlen(world.map[0])) { + if (world.map[pos_y][pos_x] == 'C') { t_location location; location.x = ++pos_x; location.y = ++pos_y; collectible = create_collectible(location); - if(!collectible) - return (NULL); - ft_lstadd_front(&list, ft_lstnew(collectible)); + ft_lstadd_front(list, ft_lstnew(&collectible)); } pos_x++; } pos_x = 0; pos_y++; } - return (list); + return (*list); } -t_collectible *create_collectible(t_location location) +t_collectible create_collectible(t_location location) { - t_collectible *collectible; + t_collectible collectible; - collectible = malloc(sizeof(t_collectible *)); - if(!collectible) - return (NULL); - collectible->location.x = location.x; - collectible->location.y = location.y; - collectible->collected = _false; + collectible.location = location; + collectible.collected = 0; return (collectible); } -int count_collectibles(t_list *collectibles, t_boolean only_uncollected) +int count_collectibles(t_list *collectibles, t_boolean o_uncollected, t_boolean o_collected) { - t_list *collectibles_copy; t_collectible *col; int count; - collectibles_copy = collectibles; count = 0; col = NULL; - while (collectibles_copy->next != NULL) { - collectibles_copy = collectibles_copy->next; - if(only_uncollected) { - col = ((t_collectible*) collectibles_copy->content); - if(!col->collected) - { - count++; - continue; - } - } - count++; + if (!collectibles) + return (0); + + while (collectibles) + { + col = ((t_collectible*) collectibles->content); + if (col == NULL) + continue; + if(o_uncollected && !col->collected) + count++; + if(o_collected && col->collected) + count++; + collectibles = collectibles->next; } return (count); } + +t_collectible *get_collectible_at(t_world world, t_location location) +{ + t_list *collectibles = world.player->collectibles; + t_collectible *collectible; + + while (collectibles) + { + collectible = ((t_collectible*) collectibles->content); + if(loc_equals(location, collectible->location)) + return collectible; + collectibles = collectibles->next; + } + return (NULL); +} + void update_collectible(t_list **collectibles, t_location location, t_boolean collected) { t_list *collectibles_copy; diff --git a/frees.c b/frees.c index 165350a..cc78c22 100644 --- a/frees.c +++ b/frees.c @@ -6,21 +6,21 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 10:21:10 by jbadaire #+# #+# */ -/* Updated: 2023/10/03 10:54:19 by jbadaire ### ########.fr */ +/* Updated: 2023/10/05 15:06:29 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -void free_map(t_data data) { - size_t lenght_y; +void free_map(t_world world) { + int length_y; - lenght_y = 0; - while (data.lenght_y < lenght_y) { - free(data.map[lenght_y]); - lenght_y++; + length_y = 0; + while (world.length_y < length_y) { + free(world.map[length_y]); + length_y++; } - free(data.map); + free(world.map); } void free_collectibles(t_list **collectibles) { diff --git a/graphics_utils.c b/graphics_utils.c index 9ea8b88..014789d 100644 --- a/graphics_utils.c +++ b/graphics_utils.c @@ -6,7 +6,7 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 12:56:12 by jbadaire #+# #+# */ -/* Updated: 2023/10/03 15:33:57 by jbadaire ### ########.fr */ +/* Updated: 2023/10/05 15:25:18 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,20 +24,22 @@ void *load_texture(char *path, void *mlx) { return (img); } -void draw_type(void *mlx, void *mlx_window, void *texture, t_data data, char c) +void draw_type(void *mlx, void *mlx_window, void *texture, t_world world, char c) { - size_t index_y; - size_t index_x; + int index_y; + int index_x; index_y = 0; index_x = 0; - while (index_y < data.lenght_y) { - while (index_x < ft_strlen(data.map[index_y])) { - if (data.map[index_y][index_x] == c) + + while (world.map[index_y]) { + while (index_x < (int) ft_strlen(world.map[index_y])) { + if (world.map[index_y][index_x] == c) { mlx_put_image_to_window(mlx, mlx_window, texture, index_x * 128, index_y * 128); + } index_x++; } - index_x=0; + index_x = 0; index_y++; } } diff --git a/libft/get_next_line.c b/libft/get_next_line.c index 4e815cc..b997583 100644 --- a/libft/get_next_line.c +++ b/libft/get_next_line.c @@ -18,9 +18,9 @@ * * @param file_descriptor The file descriptor is an integer, * value that represents an open file in the operating system. It - * is used to read or write data to/from the file. In this case, + * is used to read or write world to/from the file. In this case, * the function `get_next_line` takes a file descriptor as a - * parameter to read data from a file. + * parameter to read world from a file. * * @return The function `get_next_line` is returning a pointer to, * a string that contains the next line from the file diff --git a/libft/get_next_line_utils.c b/libft/get_next_line_utils.c index 59ee108..c4ae280 100644 --- a/libft/get_next_line_utils.c +++ b/libft/get_next_line_utils.c @@ -101,7 +101,7 @@ char *ft_str_join(char *r_line, char *buf, int nb_of_char, int len_line) * It may be NULL if no line has been read yet. * * @param buffer The buffer parameter is a pointer to a character array - * that contains the data read from a file or input stream. + * that contains the world read from a file or input stream. * * @param chars_readed The parameter `chars_readed` is an integer that represents * the number of characters read from a file diff --git a/location_utils.c b/location_utils.c new file mode 100644 index 0000000..52e42cf --- /dev/null +++ b/location_utils.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* location_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jbadaire +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/05 15:30:50 by jbadaire #+# #+# */ +/* Updated: 2023/10/05 15:36:04 by jbadaire ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "so_long.h" + +t_location create_location(int x, int y) +{ + t_location location; + + location.x = x; + location.y = y; + return (location); +} + +t_location clone_location(t_location location) +{ + t_location clone; + + clone.x = location.x; + clone.y = location.y; + return (clone); +} + +t_location edit_location(t_location location, int x, int y) +{ + location.x += x; + location.y += y; + return (location); +} + +t_boolean loc_equals(t_location loc_1, t_location loc_2) +{ + return loc_1.x == loc_2.x && loc_1.y == loc_2.y; +} \ No newline at end of file diff --git a/main.c b/main.c index 1689c67..23f6496 100644 --- a/main.c +++ b/main.c @@ -6,53 +6,44 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/02 16:19:55 by jbadaire #+# #+# */ -/* Updated: 2023/10/03 15:45:24 by jbadaire ### ########.fr */ +/* Updated: 2023/10/05 16:31:39 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" #include "stdio.h" #include "minilibx-linux/mlx.h" +#include -int main() { - - t_data data = load_map("/home/jimmy/CLionProjects/so_long/maps/1.ber"); - t_data *copy_map = clone(data); - - int collectibles; - int exit; - - collectibles = 0; - exit = 0; - - printf("Is Rectangle: %b", is_rectangle(data)); - t_location start_location = find_element(data, 'P'); - printf("\nStart location %zu %zu", start_location.x, start_location.y); - t_location exit_location = find_element(data, 'E'); - printf("\nExit location %zu %zu", exit_location.x, exit_location.y); - t_list *list = load_collectibles(data); - t_list *copy = list; - while (copy->next != NULL) { - t_collectible *col = ((t_collectible *) copy->content); - printf("\nCollectible: X %zu : Y %zu collected %b", col->location.x, col->location.y, col->collected); - copy = copy->next; +int main(int argc, char *argv[]) { + (void) argc; + int fd; + if(argc < 2) + { + ft_putstr_fd( "Veuillez inclure le chemin vers une map dans votre commande.\n", 1); + return (-1); + } + fd = open(argv[1], O_RDONLY); + if (fd == -1) + { + ft_putstr_fd("Veuillez inclure le chemin vers une map dans votre commande.\n", 1); + return (-1); } + t_world world = load_map(fd, argv[1]); - printf("\nCollectibles Count: %d", count_collectibles(list, _false)); - printf("\nIs closed: %b", is_closed(data)); + t_location start_location = find_element(world, 'P'); + t_list *collectibles_list = load_collectibles(world); - is_solvable(*copy_map, start_location.x, start_location.y); - printf("\nSolvable "); - size_t index = 0; - while (index < data.lenght_y) { - printf("\nCopyMAP: %s", copy_map->map[index]); - index++; - } + t_player player = init_player(start_location, collectibles_list); + world.player = &player; - collectibles = count_element(*copy_map, 'C'); - exit = count_element(*copy_map, 'E'); - printf("\nStruct error : %b", collectibles != 0 || exit != 0); + if(!is_valid_map(world)) + { + ft_putstr_fd("Veuillez verifier votre map (rectangle, collectibles, sortie, ect..)", 1); + free_map(world); + return (-1); + } void *mlx; @@ -61,24 +52,30 @@ int main() { return (0); } - void *window = mlx_new_window(mlx, 1500, 800, "Hello world!"); - - void *wall = load_texture("./textures/wall.xpm", mlx); - void *grass = load_texture("./textures/grass.xpm", mlx); - void *collectible = load_texture("./textures/collectible.xpm", mlx); - void *ext = load_texture("./textures/exit.xpm", mlx); - void *player = load_texture("./textures/player.xpm", mlx); - draw_type(mlx, window, wall, data, '1'); - draw_type(mlx, window, grass, data, '0'); - draw_type(mlx, window, collectible, data, 'C'); - draw_type(mlx, window, ext, data, 'E'); - draw_type(mlx, window, player, data, 'P'); + void *window = mlx_new_window(mlx, 2500, 2000, "Hello world!"); + void *wall_texture = load_texture("./textures/wall.xpm", mlx); + void *grass_texture = load_texture("./textures/grass.xpm", mlx); + void *exit_texture = load_texture("./textures/exit.xpm", mlx); + void *player_texture = load_texture("./textures/player.xpm", mlx); + void *collectible_texture = load_texture("./textures/collectible.xpm", mlx); - //mlx_hook(window, 2, 1L << 0, on_player_move, &data); + draw_type(mlx, window, wall_texture, world, '1'); + draw_type(mlx, window, grass_texture, world, '0'); + draw_type(mlx, window, exit_texture, world, 'E'); + draw_type(mlx, window, player_texture, world, 'P'); + draw_type(mlx, window, collectible_texture, world, 'C'); + mlx_hook(window, 2, (1L<<0), &on_player_move, &world); mlx_loop(mlx); + mlx_destroy_display(mlx); + mlx_clear_window(mlx, window); + mlx_destroy_window(mlx, window); + free(mlx); + + free_map(world); + return 0; } diff --git a/maps_utils.c b/maps_utils.c index ff3ac4e..2336a03 100644 --- a/maps_utils.c +++ b/maps_utils.c @@ -6,51 +6,67 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/02 18:09:47 by jbadaire #+# #+# */ -/* Updated: 2023/10/03 10:41:46 by jbadaire ### ########.fr */ +/* Updated: 2023/10/05 15:22:01 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" #include -t_data load_map(char *path) { - size_t index; - size_t y; - int fd; - t_data data; +t_world load_map(int fd, char *path) +{ + int index; + int y; + char *s; + t_world world; index = 0; y = 0; - fd = open(path, O_RDONLY); while (get_next_line(fd) != NULL) index++; - data.map = malloc((index) * sizeof(char *)); - if (!data.map) - return (data); + world.map = malloc((index + 1) * sizeof(char *)); + if (!world.map) + return (world); + world.map[index] = NULL; close(fd); fd = open(path, O_RDONLY); - while (y != index) { - data.map[y] = ft_strtrim(get_next_line(fd), "\n"); + while (y <= index) { + s = get_next_line(fd); + if(s != NULL) + world.map[y] = ft_strtrim(s, "\n"); y++; + } - return (data.lenght_y = y, data); + return (world.length_y = index, world); +} + +t_boolean is_valid_map(t_world world) +{ + t_location start_loc; + t_location exit_loc; + //int collectibles; + + start_loc = find_element(world, 'P'); + exit_loc = find_element(world, 'E'); + //collectibles = ft_lstsize(world.player->collectibles); + return (start_loc.x != -1 && exit_loc.x != -1); } -void is_solvable(t_data data, size_t x, size_t y) +void is_solvable(t_world world, int x, int y) { char character; - if(data.map[y] == NULL || y > data.lenght_y) + if(world.map[y] == NULL || y > world.length_y) return; - character = data.map[y][x]; + character = world.map[y][x]; if (character == 'O') return ; if (character == '0' || character == 'C' || character == 'P' || character == 'E') { - data.map[y][x] = 'O'; - is_solvable(data, x - 1, y); - is_solvable(data, x + 1, y); - is_solvable(data, x, y + 1); - is_solvable(data,x, y - 1); + world.map[y][x] = 'O'; + is_solvable(world, x - 1, y); + is_solvable(world, x + 1, y); + is_solvable(world, x, y + 1); + is_solvable(world,x, y - 1); } return ; diff --git a/player.c b/player.c index d877e21..309ce7e 100644 --- a/player.c +++ b/player.c @@ -6,17 +6,90 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 10:22:53 by jbadaire #+# #+# */ -/* Updated: 2023/10/03 15:24:01 by jbadaire ### ########.fr */ +/* Updated: 2023/10/05 18:13:32 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" #include "stdio.h" -int on_player_move(int keycode, t_data *data) +t_player init_player(t_location location, t_list *collectibles) { - (void) data; + t_player player; - printf("PRINT:%d", keycode); + player.location = location; + player.collectibles = collectibles; + return (player); +} + + +int on_player_move(int keycode, t_world *world) +{ + //w = 119 + //s = 115 + //a = 97 + //d = 100 + printf("\nPRINT: %d", keycode); + + + if(!can_move(keycode, world)) + return -1; + + if(keycode == 119) + world->player->location.x += 1; + else if(keycode == 115) + world->player->location.x -= 1; + else if(keycode == 97) + world->player->location.y -= 1; + else if(keycode == 100) + world->player->location.y += 1; + + printf("\nPRINT2: %d", keycode); return 0; -} \ No newline at end of file +} + + + +t_boolean can_move(int code, t_world *world) +{ + char **map; + char c; + int y; + int x; + + c = ' '; + map = world->map; + y = world->player->location.y; + x = world->player->location.x; + if(code == 119 && map[y + 1]) + c = map[y + 1][x]; + else if(code == 115 && map[y - 1]) + c = map[y - 1][x]; + else if(code == 97 && map[y][x - 1]) + c = map[y][x - 1]; + else if(code == 100 && map[y][x + 1]) + c = map[y][x + 1]; + + return (is_solid(c, world->player)); +} + +t_boolean is_solid(char c, t_player *player) +{ + t_player copy; + copy = *player; + t_collectible collectible; + if(!c) + return _false; + while (copy.collectibles && copy.collectibles->next) + { + + collectible = *((t_collectible *) ©.collectibles->content); + if(c == 'E' && !collectible.collected) + return (_false); + copy.collectibles = copy.collectibles->next; + } + + if(c != '1') + return (_false); + return _true; +} diff --git a/rectangle_check.c b/rectangle_check.c index 2f4129f..cd2e8f8 100644 --- a/rectangle_check.c +++ b/rectangle_check.c @@ -6,84 +6,85 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 01:27:59 by jbadaire #+# #+# */ -/* Updated: 2023/10/03 05:34:04 by jbadaire ### ########.fr */ +/* Updated: 2023/10/05 15:26:35 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -t_boolean is_rectangle(t_data data) { - return (is_horizontal_rectangle(data) || is_vertical_rectangle(data)); +t_boolean is_rectangle(t_world world) { + return (is_horizontal_rectangle(world) || + is_vertical_rectangle(world)) && is_closed(world); } -t_boolean is_horizontal_rectangle(t_data data) { - size_t x_lenght; - size_t y_index; +t_boolean is_horizontal_rectangle(t_world world) { + int x_length; + int y_index; - x_lenght = ft_strlen(data.map[0]); + x_length = (int) ft_strlen(world.map[0]); y_index = 0; - while (y_index < data.lenght_y && data.map[y_index]) { - if (ft_strlen(data.map[y_index]) != x_lenght) + while (y_index < world.length_y && world.map[y_index]) { + if ((int) ft_strlen(world.map[y_index]) != x_length) return (_false); y_index++; } - if (x_lenght <= y_index) + if (x_length <= y_index) return (_false); return (_true); } -t_boolean is_vertical_rectangle(t_data data) { - size_t x_index; - size_t y_index; +t_boolean is_vertical_rectangle(t_world world) { + int x_index; + int y_index; - size_t x_lenght; - size_t y_lenght; + int x_length; + int y_length; x_index = 0; y_index = 0; - x_lenght = ft_strlen(data.map[0]); - y_lenght = data.lenght_y; + x_length = (int) ft_strlen(world.map[0]); + y_length = world.length_y; - while (x_index < x_lenght) { - while (x_index < x_lenght && y_index < y_lenght && data.map[y_index][x_index]) + while (x_index < x_length) { + while (x_index < x_length && y_index < y_length && world.map[y_index][x_index]) y_index++; - if (y_index != y_lenght) + if (y_index != y_length) return (_false); y_index = 0; x_index++; } y_index = 0; - while (y_index < y_lenght) - if (data.map[y_index] && ft_strlen(data.map[y_index++]) != x_lenght) + while (y_index < y_length) + if (world.map[y_index] && (int)ft_strlen(world.map[y_index++]) != x_length) return (_false); - if (x_index >= y_lenght) + if (x_index >= y_length) return (_false); return (_true); } -t_boolean is_closed(t_data data) { +t_boolean is_closed(t_world world) { - size_t x_lenght; - size_t x_index; - size_t y_index; + int x_length; + int x_index; + int y_index; - x_lenght = ft_strlen(data.map[0]); + x_length = (int)ft_strlen(world.map[0]); x_index = 0; y_index = 0; - while (x_index < x_lenght) { - if (data.map[0][x_index] != '1' || data.map[data.lenght_y - 1][x_index] != '1') + while (x_index < x_length) { + if (world.map[0][x_index] != '1' || world.map[world.length_y - 1][x_index] != '1') return (_false); x_index++; } - while (y_index < data.lenght_y) { - if (data.map[y_index][0] != '1' || data.map[y_index][x_lenght - 1] != '1') + while (y_index < world.length_y) { + if (world.map[y_index][0] != '1' || world.map[y_index][x_length - 1] != '1') return (_false); y_index++; } diff --git a/so_long.h b/so_long.h index 0d1084a..ed83e75 100644 --- a/so_long.h +++ b/so_long.h @@ -6,7 +6,7 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/02 17:59:52 by jbadaire #+# #+# */ -/* Updated: 2023/10/03 11:29:39 by jbadaire ### ########.fr */ +/* Updated: 2023/10/05 16:30:22 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,47 +18,63 @@ # include "libft/libft.h" # include "stdlib.h" -typedef struct s_data { - - char **map; - size_t lenght_y; - -} t_data; - typedef struct s_location { - size_t x; - size_t y; - + int x; + int y; } t_location; -typedef struct s_collectible { - - t_location location; - t_boolean collected; - +typedef struct s_collectible +{ + t_location location; + int collected; } t_collectible; - -t_data load_map(char *path); -t_boolean is_horizontal_rectangle(t_data data); -t_boolean is_vertical_rectangle(t_data data); -t_boolean is_rectangle(t_data data); -t_boolean is_closed(t_data data); - -t_list *load_collectibles(t_data data); -t_collectible *create_collectible(t_location location); -int count_collectibles(t_list *collectibles, t_boolean only_uncollected); +typedef struct s_player +{ + t_location location; + t_list *collectibles; +} t_player; + +typedef struct s_world +{ + char **map; + int length_y; + t_player *player; +} t_world; + +t_world load_map(int fd, char *path); +t_boolean is_horizontal_rectangle(t_world world); +t_boolean is_vertical_rectangle(t_world world); +t_boolean is_rectangle(t_world world); +t_boolean is_closed(t_world world); +t_boolean is_valid_map(t_world world); + +t_player init_player(t_location location, t_list *collectibles); + +t_list *load_collectibles(t_world world); +t_collectible create_collectible(t_location location); +int count_collectibles(t_list *collectibles, t_boolean o_uncollected, t_boolean o_collected); void update_collectible(t_list **collectibles, t_location location, t_boolean collected); void free_collectibles(t_list **collectibles); +t_boolean is_solid(char c, t_player *player); +t_boolean can_move(int code, t_world *world); -void is_solvable(t_data data, size_t x, size_t y); -t_data *clone(t_data data); -int count_element(t_data data, char type); +void is_solvable(t_world world, int x, int y); +t_world *clone(t_world world); +int count_element(t_world world, char type); void *load_texture(char *path, void *mlx); -void draw_type(void *mlx, void *mlx_window, void *texture, t_data data, char c); -int on_player_move(int keycode, t_data *data); -void free_map(t_data data); -t_location find_element(t_data data, char type); +void draw_type(void *mlx, void *mlx_window, void *texture, t_world world, char c); +int on_player_move(int keycode, t_world *world); +void free_map(t_world world); +t_location find_element(t_world world, char type); + +t_boolean loc_equals(t_location loc_1, t_location loc_2); +t_location edit_location(t_location location, int x, int y); +t_location clone_location(t_location location); +t_location create_location(int x, int y); + +t_collectible *get_collectible_at(t_world world, t_location location); + #endif diff --git a/utils.c b/utils.c index 5e502e6..4a55358 100644 --- a/utils.c +++ b/utils.c @@ -6,24 +6,24 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 10:24:01 by jbadaire #+# #+# */ -/* Updated: 2023/10/03 10:37:46 by jbadaire ### ########.fr */ +/* Updated: 2023/10/05 15:02:04 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -t_location find_element(t_data data, char type) { - size_t pos_x; - size_t pos_y; +t_location find_element(t_world world, char type) { + int pos_x; + int pos_y; t_location location; pos_x = 0; pos_y = 0; location.x = -1; location.y = -1; - while (pos_y < data.lenght_y) { - while (pos_x < ft_strlen(data.map[0])) { - if (data.map[pos_y][pos_x] == type) { + while (world.map[pos_y]) { + while (pos_x < (int) ft_strlen(world.map[0])) { + if (world.map[pos_y][pos_x] == type) { location.x = ++pos_x; location.y = ++pos_y; return (location); @@ -36,18 +36,18 @@ t_location find_element(t_data data, char type) { return (location); } -int count_element(t_data data, char type) +int count_element(t_world world, char type) { - size_t pos_x; - size_t pos_y; + int pos_x; + int pos_y; int count; pos_x = 0; pos_y = 0; count = 0; - while (pos_y < data.lenght_y) { - while (pos_x < ft_strlen(data.map[0])) { - if (data.map[pos_y][pos_x] == type) + while (pos_y < world.length_y) { + while (pos_x < (int) ft_strlen(world.map[0])) { + if (world.map[pos_y][pos_x] == type) count++; pos_x++; @@ -58,22 +58,22 @@ int count_element(t_data data, char type) return (count); } -t_data *clone(t_data data) { - t_data *clone; - size_t index; +t_world *clone(t_world world) { + t_world *clone; + int index; char **content; - clone = malloc(sizeof(t_data)); + clone = malloc(sizeof(t_world)); if(!clone) return (NULL); - clone->lenght_y = data.lenght_y; + clone->length_y = world.length_y; index = 0; - content = malloc(sizeof(char *) * clone->lenght_y); + content = malloc(sizeof(char *) * clone->length_y); if(!content) return (NULL); - while (index < clone->lenght_y) { - content[index] = ft_strdup(data.map[index]); + while (index < clone->length_y) { + content[index] = ft_strdup(world.map[index]); index++; } clone->map = content;