-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_map.c
107 lines (98 loc) · 2.86 KB
/
init_map.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbougssi <rbougssi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/19 16:36:17 by rbougssi #+# #+# */
/* Updated: 2021/01/21 12:13:18 by rbougssi ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void get_h(t_game *game, int i)
{
int j;
j = 0;
while (game->raw_map[j] && game->raw_map[j] != '\n')
j++;
game->char_map[i] = ft_substr(game->raw_map, 0, j);
game->raw_map += j + 1;
j = 0;
while (game->char_map[i][j])
{
if (whereis(game->char_map[i][j], " \t012NSWE") < 0)
p_err(game, "invalid chars in map\n");
j++;
}
if (ft_strlen(game->char_map[i]) > game->map_w)
game->map_w = ft_strlen(game->char_map[i]);
}
void fill_int_map(t_game *game, t_bool *is, int i, int j)
{
if (j > ft_strlen(game->char_map[i]) || game->char_map[i][j] <= ' ')
game->map[i][j] = -1;
else if (whereis(game->char_map[i][j], "012") >= 0)
game->map[i][j] = game->char_map[i][j] - '0';
else if (whereis(game->char_map[i][j], "NSWE") >= 0)
{
if (*is)
p_err(game, "player already somewhere else\n");
else
{
game->cam_dir = whereis(game->char_map[i][j], "NWSE");
game->map[i][j] = 0;
game->pos[0] = i;
game->pos[1] = j;
*is = TRUE;
}
}
if (game->map[i][j] == 2)
game->nb_sprites++;
}
void init_map(t_game *game)
{
t_bool is_player_pos;
int i;
int j;
is_player_pos = FALSE;
game->map = ft_calloc(game->map_h, sizeof(int *));
game->nb_sprites = 0;
i = 0;
while (i < game->map_h)
{
game->map[i] = ft_calloc(game->map_w, sizeof(int));
j = 0;
while (j < game->map_w)
fill_int_map(game, &is_player_pos, i, j++);
free(game->char_map[i]);
i++;
}
if (!is_player_pos)
p_err(game, "where is the player??\n");
free(game->char_map);
}
t_bool check_map_ij(t_game *game, int i, int j)
{
return (!((i == 0 || j == 0 || i == game->map_h - 1 || j == game->map_w - 1
|| game->map[i - 1][j] == -1 || game->map[i + 1][j] == -1
|| game->map[i][j - 1] == -1 || game->map[i][j + 1] == -1)
&& game->map[i][j] != 1 && game->map[i][j] != -1));
}
void check_map(t_game *game)
{
int i;
int j;
i = 0;
while (i < game->map_h)
{
j = 0;
while (j < game->map_w)
{
if (!check_map_ij(game, i, j))
p_err(game, "invalid map\n");
j++;
}
i++;
}
}