aboutsummaryrefslogtreecommitdiff
path: root/parse
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-01-11 12:37:34 +0100
committerCharles <sircharlesaze@gmail.com>2020-01-11 12:37:34 +0100
commitcff05e83256a67b8cb23b16b1e1e6f761ff52f4c (patch)
tree33eb6eae520a085d6774205601f13d07dc99aaf9 /parse
parent0dcba6ff7e68ed13f8e6caadd80b77506b917050 (diff)
downloadcub3d-cff05e83256a67b8cb23b16b1e1e6f761ff52f4c.tar.gz
cub3d-cff05e83256a67b8cb23b16b1e1e6f761ff52f4c.tar.bz2
cub3d-cff05e83256a67b8cb23b16b1e1e6f761ff52f4c.zip
fixing a few segfault, map border checking
Diffstat (limited to 'parse')
-rw-r--r--parse/parse.c19
-rw-r--r--parse/parse_check.c44
2 files changed, 53 insertions, 10 deletions
diff --git a/parse/parse.c b/parse/parse.c
index 145d5ad..84a8957 100644
--- a/parse/parse.c
+++ b/parse/parse.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */
-/* Updated: 2020/01/11 10:12:10 by cacharle ### ########.fr */
+/* Updated: 2020/01/11 11:29:45 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,6 +22,8 @@ t_state *parse(char *filename)
return (NULL);
if ((lines = get_file_lines(filename)) == NULL)
return (state_destroy(state));
+ /* for (int i = 0; lines[i]; i++) */
+ /* printf("%d [%s]\n", i, lines[i]); */
i = -1;
while (lines[++i] != NULL)
{
@@ -46,16 +48,17 @@ char **get_file_lines(char *filename)
{
int fd;
int ret;
- char buf[BUFFER_SIZE];
+ char buf[BUFFER_SIZE + 1];
char *file;
- fd = open(filename, O_RDONLY);
+ if ((fd = open(filename, O_RDONLY)) < 0)
+ return (NULL);
if ((file = ft_strdup("")) == NULL)
return (NULL);
while ((ret = read(fd, buf, BUFFER_SIZE)) > 0)
{
buf[ret] = '\0';
- if ((file = ft_strjoin_free(file, buf, 1)) == NULL)
+ if ((file = ft_strjoin(file, buf)) == NULL)
return (NULL);
}
if (ret == -1)
@@ -122,12 +125,8 @@ t_cell *create_map_row(char *line)
i = 0;
while (*line)
{
- if (*line == '0')
- row[i++] = CELL_EMPTY;
- else if (*line == '1')
- row[i++] = CELL_WALL;
- else if (*line == '2')
- row[i++] = CELL_ITEM;
+ if (*line == '0' || *line == '1' || *line == '2')
+ row[i++] = *line - '0';
else if (*line == 'N')
row[i++] = CELL_LOOK_NORTH;
else if (*line == 'S')
diff --git a/parse/parse_check.c b/parse/parse_check.c
new file mode 100644
index 0000000..7bcfc8b
--- /dev/null
+++ b/parse/parse_check.c
@@ -0,0 +1,44 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* check.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/11 10:59:15 by cacharle #+# #+# */
+/* Updated: 2020/01/11 11:15:04 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "cub3d.h"
+
+t_state *parse_check(t_state *state)
+{
+ int i;
+ int j;
+ int player_count;
+
+ i = -1;
+ while (++i < state->map_width)
+ if (state->map[0][i] != CELL_WALL
+ || state->map[state->map_height - 1][i] != CELL_WALL)
+ return (state_destroy(state));
+ i = -1;
+ while (++i < state->map_height)
+ if (state->map[i][0] != CELL_WALL
+ || state->map[i][state->map_width - 1] != CELL_WALL)
+ return (state_destroy(state));
+ // maybe not necessary
+ /* player_count = 0; */
+ /* i = -1; */
+ /* while (++i < state->map_height) */
+ /* { */
+ /* j = -1; */
+ /* while (++j < state->map_width) */
+ /* if (helper_is_player_cell(state->map[i][j])) */
+ /* player_count++; */
+ /* } */
+ /* if (player_count != 1) */
+ /* return (state_destroy(state)); */
+ return (state);
+}