-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_utils.c
116 lines (107 loc) · 2.62 KB
/
main_utils.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
108
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/* */
/* :::::::: */
/* main_utils.c :+: :+: */
/* +:+ */
/* By: pacovali <marvin@codam.nl> +#+ */
/* +#+ */
/* Created: 2019/01/12 18:56:23 by pacovali #+# #+# */
/* Updated: 2019/01/23 17:53:36 by pacovali ######## odam.nl */
/* */
/* ************************************************************************** */
#include "lemin.h"
void free_str_arr(char **s, char ***arr, int flag)
{
int i;
if ((flag == 1 || flag == 2) && *s != 0)
{
free(*s);
*s = 0;
}
if ((flag == 0 || flag == 2) && arr != 0 && *arr != 0)
{
i = 0;
while (arr[0][i])
{
free(arr[0][i]);
i++;
}
free(*arr);
*arr = 0;
}
}
int hash(char *s)
{
unsigned res;
char c;
int i;
i = 0;
res = 0;
if (!s || ft_strlen(s) < 1 || ft_strlen(s) > 4)
exit(ft_printf("error: bad string passed into hash function\n"));
while (s[i] && i < 4)
{
c = s[i];
if (ft_isdigit(c))
c -= 48;
else if (ft_isupper(c))
c -= 55;
else if (ft_islower(c))
c -= 61;
else
c = 62;
res <<= 5;
res ^= c;
i++;
}
return (res);
}
int sort_connections(t_map *map, t_room *room)
{
int i[2];
int tmp;
i[0] = 0;
i[1] = 0;
while (i[0] < 100 && room->in_out[i[0] + 1] > 0)
{
if (map->rooms[room->in_out[i[0] + 1]]->dist
< map->rooms[room->in_out[i[0]]]->dist)
{
tmp = room->in_out[i[0] + 1];
room->in_out[i[0] + 1] = room->in_out[i[0]];
room->in_out[i[0]] = tmp;
i[1]++;
}
i[0]++;
}
if (i[1] > 0)
sort_connections(map, room);
return (i[0] + 1);
}
int sort_connections2(t_map *map, t_room *room)
{
int i[2];
int tmp;
i[0] = 0;
i[1] = 0;
if (room == NULL)
return (0);
while (i[0] + 1 < 100 && room->in_out[i[0] + 1] > 0)
{
if (room->in_out[i[0]] < 2097151
&& map->rooms[room->in_out[i[0]]] != NULL
&& map->rooms[room->in_out[i[0] + 1]] != NULL
&& map->rooms[room->in_out[i[0] + 1]]->dist2
< map->rooms[room->in_out[i[0]]]->dist2)
{
tmp = room->in_out[i[0] + 1];
room->in_out[i[0] + 1] = room->in_out[i[0]];
room->in_out[i[0]] = tmp;
i[1]++;
}
i[0]++;
}
if (i[1] > 0)
sort_connections2(map, room);
return (i[0] + 1);
}