-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_read_file5_map.c
More file actions
109 lines (101 loc) · 2.64 KB
/
ft_read_file5_map.c
File metadata and controls
109 lines (101 loc) · 2.64 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_read_file5_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gmartin- <gmartin-@student.42madrid.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/08 12:06:49 by gmartin- #+# #+# */
/* Updated: 2020/07/09 12:10:58 by gmartin- ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int ft_check_config(t_file *f)
{
int i;
i = -1;
while (i < 8)
if (f->countmap[++i] > 1)
return (-1);
return (0);
}
int ft_handle_map_read(t_file *f)
{
int i;
char *temp;
i = 0;
if (ft_isdigit(f->line[0]) || f->line[0] == ' ')
{
if (ft_check_config(f) == -1)
ft_handle_error("LINEA DUPLICADA");
i = ft_strlen(f->line);
if (f->buff == NULL)
temp = ft_strdup(f->line);
else
temp = ft_strjoin(f->buff, f->line);
free(f->buff);
f->buff = temp;
temp = ft_strjoin(f->buff, "\n");
free(f->buff);
f->buff = temp;
if (f->ncolmax == 0 || f->ncolmax < i)
f->ncolmax = i;
f->nfil++;
}
return (f->rtn);
}
void ft_filling_matrix(t_file *f, int k, int i, int j)
{
if (f->buff[k] == '0' || f->buff[k] == '1' || f->buff[k] == '2')
{
f->map[i][j] = (int)f->buff[k] - '0';
if (f->buff[k] == '2')
f->sprite_num++;
}
else if (f->buff[k] == ' ')
f->map[i][j] = 4;
else if (f->buff[k] == 'N' || f->buff[k] == 'S'
|| f->buff[k] == 'E' || f->buff[k] == 'W')
{
if (f->dir == '\0')
{
f->map[i][j] = 0;
f->dir = f->buff[k];
ft_calcdirns(f);
ft_calcdirew(f);
f->pos[0] = i;
f->currentpos.x = i + 0.5;
f->pos[1] = j;
f->currentpos.y = j + 0.5;
}
}
else
ft_handle_error("YUJU");
}
int alloc_map(t_file *f)
{
int i[3];
i[0] = -1;
i[2] = 0;
if (!(f->map = ft_calloc(f->nfil, sizeof(int *))))
return (0);
else
{
while (++i[0] < f->nfil && f->buff[i[2]])
{
if (!(f->map[i[0]] = ft_calloc(f->ncolmax, sizeof(int *))))
return (0);
i[1] = -1;
while (++i[1] < f->ncolmax && f->buff[i[2]] != '\n')
{
ft_filling_matrix(f, i[2], i[0], i[1]);
i[2]++;
}
i[2]++;
}
f->mapreaded = 1;
if (f->dir == '\0')
ft_handle_error("ERROR: NOT INIT PLAYER\n");
}
return (0);
}