-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
122 lines (90 loc) · 5.26 KB
/
utilities.py
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
from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder
from pathfinding.core.grid import Grid
import json, main
from floormatrices import *
from location import roomlocation, stairlocation
totalschedules = json.load(open("wholeschool.json"))
def calc_route(startroom, endroom, period):
finder = AStarFinder()
startloc, startfloor = (roomlocation[startroom][0], roomlocation[startroom][1]), roomlocation[startroom][2]
endloc, endfloor = (roomlocation[endroom][0], roomlocation[endroom][1]), roomlocation[endroom][2]
# Order floors by name in order
# Here floorf0 would be 'below' extensione0
hierarchyext = ['matrixf0', 'matrixe0',]
# Order floors by name in order without the extensions
hierarchyreg = ['matrixf0',]
if startfloor == endfloor:
grid = Grid(matrix = eval('test.schools[period].' + str(startfloor))['pathfinding'])
start = grid.node(startloc[0], startloc[1])
end = grid.node(endloc[0], endloc[1])
route = finder.find_path(start, end, grid)
return route, startfloor, None, None, None
if startfloor != endfloor:
gridstart = Grid(matrix = eval('test.schools[period].' + str(startfloor))['pathfinding'])
gridend = Grid(matrix = eval('test.schools[period].' + str(endfloor))['pathfinding'])
if 'e' in startfloor or 'e' in endfloor:
possible = [(0, 1), (0, 5)]
else:
possible = list(set(stairlocation[startfloor[-2:]]) & set(stairlocation[endfloor[-2:]]))
possibilities = {}
totals = []
for stairwell in possible:
if 'e' in startfloor and 'e' not in endfloor:
startstart = gridstart.node(startloc[0], startloc[1])
startend = gridend.node(stairwell[0], stairwell[1])
endstart = gridend.node(stairwell[0] + 8, stairwell[1])
endend = gridend.node(endloc[0], endloc[1])
routestart = finder.find_path(startstart, startend, gridstart)
routeend = finder.find_path(endstart, endend, gridend)
elif 'e' in endfloor and 'e' not in startfloor:
startstart = gridstart.node(stairwell[0], stairwell[1])
startend = gridstart.node(stairwell[0] + 8, stairwell[1])
endstart = gridend.node(stairwell[0], stairwell[1])
endend = gridend.node(endloc[0], endloc[1])
routestart = finder.find_path(startstart, startend, gridstart)
routeend = finder.find_path(endstart, endend, gridend)
else:
startstart = gridstart.node(startloc[0], startloc[1])
startend = gridstart.node(stairwell[0], stairwell[1])
endstart = gridend.node(stairwell[0], stairwell[1])
endend = gridend.node(endloc[0], endloc[1])
routestart = finder.find_path(startstart, startend, gridstart)
routeend = finder.find_path(endstart, endend, gridend)
total = len(routestart[0]) + len(routeend[0])
if 'e' in startfloor and not endfloor:
total += abs(int(startfloor[-1]) * 2 - int(endfloor[-1]) - 1)
if 'e' in endfloor and not startfloor:
total += abs(int(endfloor[-1]) * 2 - int(startfloor[-1]) - 1)
else:
total += abs(int(startfloor[-1]) - int(endfloor[-1])) * 2
gridstart.cleanup()
gridend.cleanup()
possibilities[total] = stairwell
totals.append(total)
minimum = (possibilities[min(totals)], startfloor)
startstart, startend = gridstart.node(startloc[0], startloc[1]), gridstart.node(minimum[0][0], minimum[0][1])
endstart, endend = gridend.node(minimum[0][0], minimum[0][1]), gridend.node(endloc[0], endloc[1])
routestart, routeend = finder.find_path(startstart, startend, gridstart), finder.find_path(endstart, endend, gridend)
if abs(int(startfloor[-1]) - int(endfloor[-1])) == 1:
return routestart, startfloor, routeend, endfloor, None
else:
stairs = [minimum]
if (0, 1) in stairs[0] or (0, 5) in stairs[0]:
if int(startfloor[-1]) < int(endfloor[-1]):
lower, higher = hierarchyext.index(startfloor[-2:]), hierarchyext.index(endfloor[-2:])
else:
lower, higher = hierarchyext.index(endfloor[-2:]), hierarchyext.index(startfloor[-2:])
for i in hierarchyext[lower + 1:higher]:
if 'f' in i:
stairs.append(((minimum[0][0] + 8, minimum[0][1]), 'matrix' + i))
elif 'e' in i:
stairs.append((minimum[0], 'matrix' + i))
else:
if int(startfloor[-1]) < int(endfloor[-1]):
lower, higher = hierarchyreg.index(startfloor[-2:]), hierarchyreg.index(endfloor[-2:])
else:
lower, higher = hierarchyreg.index(endfloor[-2:]), hierarchyreg.index(startfloor[-2:])
for i in hierarchyreg[lower + 1:higher]:
stairs.append((minimum[0], 'matrix' + i))
return routestart, startfloor, routeend, endfloor, stairs