-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrossing_paths.c
127 lines (116 loc) · 3.15 KB
/
crossing_paths.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
117
118
119
120
121
122
123
124
125
126
127
/* ************************************************************************** */
/* */
/* :::::::: */
/* crossing_paths.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"
static void assign_room2(t_map *map, int id, int dist)
{
int i;
i = 0;
if (id == map->stop)
return ;
while (map->rooms[id]->in_out[i] > 0)
{
if (map->rooms[map->rooms[id]->in_out[i]]->dist < 1)
{
map->rooms[map->rooms[id]->in_out[i]]->dist = dist;
if (id == map->start)
map->rooms[map->rooms[id]->in_out[i]]->path_id = \
map->rooms[id]->in_out[i];
else
map->rooms[map->rooms[id]->in_out[i]]->path_id = \
map->rooms[id]->path_id;
}
i++;
}
}
void assign_distance2(t_map *map, int id, int dist)
{
int i;
if (dist == 100)
assign_room2(map, map->start, dist - 1);
else
{
i = 0;
while (i < 2097151)
{
if (map->rooms[i] && map->rooms[i]->dist == dist && dist > 0)
assign_room2(map, i, dist - 1);
i++;
}
}
if (dist > 0)
assign_distance2(map, id, dist - 1);
map->rooms[map->start]->dist = 100;
map->rooms[map->stop]->dist = 0;
}
void check_dup(int **path, int *nr)
{
int i;
i = 0;
while (i < *nr)
{
if (path[i][0] == path[*nr][0])
{
path[*nr][0] = 0;
(*nr)--;
break ;
}
i++;
}
}
void get_xpath(t_map *map, int id, int **path, int dist)
{
int i;
int *tmp;
i = 0;
tmp = (int[]){0, 0};
while (map->rooms[id]->in_out[i] > 0)
{
if (map->rooms[map->rooms[id]->in_out[i]]->path_id == path[0][0]
&& map->rooms[map->rooms[id]->in_out[i]]->dist > dist
&& map->rooms[map->rooms[id]->in_out[i]]->dist > tmp[1])
{
tmp[0] = map->rooms[id]->in_out[i];
tmp[1] = map->rooms[map->rooms[id]->in_out[i]]->dist;
}
i++;
}
if (tmp[0] != 0 && tmp[1] > dist)
path[0][99 - tmp[1]] = tmp[0];
if (tmp[0] != 0 && tmp[1] > dist && dist == 0)
path[0][100 - tmp[1]] = map->stop;
if (tmp[0] != 0 && tmp[1] > dist && tmp[1] < 99)
get_xpath(map, tmp[0], path, tmp[1]);
}
void crossing_paths(t_map *map)
{
int i;
int j;
i = 101;
assign_distance2(map, map->stop, 100);
map->path = (int**)ft_memalloc(sizeof(int*) * 101);
while (i > 0)
{
i--;
map->path[i] = (int*)ft_memalloc(sizeof(int) * 101);
}
j = 0;
while (map->rooms[map->stop]->in_out[i] > 0)
{
map->path[j][0] = map->rooms[map->rooms[map->stop]->in_out[i]]->path_id;
check_dup(map->path, &j);
j++;
get_xpath(map, map->stop, &(map->path[j - 1]), 0);
map->path[j][0] = 0;
i++;
}
assign_paths(map, 0, (int[]){0, 0, 0}, 999.0f);
}