blob: a3ccc5afb633d0c2a5aee992531a238b4bfc6c22 (
plain)
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
129
130
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_verify.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 08:14:30 by kdx #+# #+# */
/* Updated: 2022/10/16 00:14:35 by kdx ### ########.fr */
/* */
/* ************************************************************************** */
#include "map.h"
static t_err only_chars(const unsigned char *data)
{
int accepted[256];
size_t i;
i = 0;
while (i < 256)
accepted[i++] = 0;
accepted[TILE_NONE] = 1;
accepted[TILE_WALL] = 1;
accepted[TILE_COLLECTIBLE] = 1;
accepted[TILE_EXIT] = 1;
accepted[TILE_PLAYER] = 1;
accepted['\n'] = 1;
i = 0;
while (data[i] != '\0')
if (!accepted[data[i++]])
return (1);
return (0);
}
static t_err find_uniques(const unsigned char *data)
{
int uniques[256];
size_t i;
size_t to_be_found;
i = 0;
while (i < 256)
uniques[i++] = 0;
uniques['P'] = 1;
uniques['E'] = 1;
uniques['C'] = 5;
to_be_found = 3;
i = 0;
while (data[i] != '\0')
{
if (uniques[data[i]] && uniques[data[i]] != 7)
{
if (uniques[data[i]] == 3)
return (ERR_DUPLICATE);
if ((uniques[data[i]] & 2) == 0)
to_be_found--;
uniques[data[i]] |= 2;
}
i++;
}
return (ERR_MISSING + (to_be_found == 0) * (ERR_NONE - ERR_MISSING));
}
static t_err sensible_dimensions(t_map *map, const unsigned char *data)
{
size_t prev_newline;
if (ft_strchr((const char *)data, '\n') == NULL)
return (1);
prev_newline = 0;
map->width = 0;
map->height = 0;
while (*data != '\0')
{
if (*data++ == '\n')
{
if (map->width == 0)
map->width = prev_newline;
if (prev_newline == 0 || map->width != prev_newline)
return (1);
prev_newline = 0;
map->height++;
}
else
prev_newline++;
}
if (prev_newline != 0 || map->width == 0 || map->height == 0)
return (1);
return (0);
}
static t_err valid_borders(t_map *map)
{
size_t x;
size_t y;
x = 0;
while (x < map->width)
{
if (map->data[x] != TILE_WALL
|| map_get(map, x * TSIZE, (map->height - 1) * TSIZE) != TILE_WALL)
return (ERR_OPEN_BORDER);
x += 1;
}
y = 0;
while (y < map->height)
{
if (map_get(map, 0, y * TSIZE) != TILE_WALL
|| map_get(map, (map->width - 1) * TSIZE, y * TSIZE) != TILE_WALL)
return (ERR_OPEN_BORDER);
y += 1;
}
return (ERR_NONE);
}
t_err map_verify(t_map *map, const unsigned char *data)
{
if (only_chars(data))
return (ERR_INVALID_CHAR);
if (find_uniques(data))
return (find_uniques(data));
if (sensible_dimensions(map, data))
return (ERR_WRONG_DIMENSION);
if (valid_borders(map))
return (ERR_OPEN_BORDER);
if (!map_pathfind(map))
return (ERR_NO_PATH);
return (ERR_NONE);
}
|