-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_readinglast.c
More file actions
128 lines (118 loc) · 3.1 KB
/
Copy pathft_readinglast.c
File metadata and controls
128 lines (118 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_readinglast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gconde-m <gconde-m@student.42madrid.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/03 06:48:50 by gconde-m #+# #+# */
/* Updated: 2020/06/03 07:17:36 by gconde-m ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
static void check_and_set_values(t_struct *x, int i, int j)
{
if ((*x->map_str < '0' || *x->map_str > '2') &&
(*x->map_str != 'N' && *x->map_str != 'S' &&
*x->map_str != 'E' && *x->map_str != 'W' && *x->map_str != ' ' &&
*x->map_str != '\n'))
ft_exitinerror(x);
if (*x->map_str == '2')
x->num++;
else
{
if (ft_isalpha(*x->map_str))
{
if (x->posx != 0)
ft_exitinerror(x);
x->posx = i + 0.5;
x->posy = j + 0.5;
x->dir = *x->map_str;
*x->map_str = '0';
}
if (*x->map_str == ' ')
*x->map_str = '3';
}
}
static void check_and_set_map(t_struct *x)
{
int i;
i = 0;
if (!(x->worldmap = malloc(sizeof(int*) * x->map_height)))
ft_exitinerror(x);
ft_bzero(x->worldmap, sizeof(int*));
while (i < x->map_height)
{
if (!(x->worldmap[i] = malloc(sizeof(int) * x->map_width)))
ft_exitinerror(x);
i++;
}
}
void ft_check_map(t_struct *x)
{
int i;
int j;
i = 0;
x->map_str_pos = x->map_str;
while (i < x->map_height)
{
j = 0;
while (j < x->map_width)
{
check_and_set_values(x, i, j);
if (*x->map_str == '\n')
x->worldmap[i][j] = 3;
else
x->worldmap[i][j] = *x->map_str - '0';
if (*x->map_str != '\n')
x->map_str++;
j++;
}
x->map_str++;
i++;
}
ft_validate_map(x);
}
int ft_validate_map_2(t_struct *x, int row, int col)
{
char c;
int ok;
if (row < 0 || col < 0 || row >= x->map_height || col >= x->map_width)
return (1);
c = x->worldmap[row][col];
if (c == 3)
return (1);
else if (c == 4 || c == 1 || c == 5)
return (0);
if (x->worldmap[row][col] == 0)
x->worldmap[row][col] = 4;
else
x->worldmap[row][col] = 5;
ok = ft_validate_map_2(x, row, col - 1);
ok = ok == 0 ? ft_validate_map_2(x, row, col + 1) : ok;
ok = ok == 0 ? ft_validate_map_2(x, row - 1, col) : ok;
ok = ok == 0 ? ft_validate_map_2(x, row + 1, col) : ok;
return (ok);
}
void reading_all(t_struct *x, char *s1)
{
int fd;
char *line;
if (!(fd = open(s1, O_RDONLY)))
ft_exitinerror(x);
while ((x->zread = get_next_line(fd, &line)) >= 0)
{
ft_check(x, line);
free(line);
if (x->zread == 0)
{
if (x->map_height < 3 || x->map_width < 3)
ft_exitinerror(x);
check_and_set_map(x);
ft_check_map(x);
break ;
}
}
if (x->zread < 0)
ft_exitinerror(x);
}