findPathFrom: aBuildingName1 to: aBuildingName2
"Finds a path between the buildings and stores it into route"
| queue nodeToAdd currNode pointsCol pC building1 building2 sn1 sn2 s1 currStreet streetsSeenCol sNames |
queue _ Queue new.
building1 _ map buildingWithName: aBuildingName1.
Building2 _ map buildingWithName: aBuildingName2.
sn1 _ building1 streetName.
sn2 _ building2 streetName.
s1 _ map streetWithName: sn1.
pointsCol _ OrderedCollection new.
streetsSeenCol _ OrderedCollection new.
pointsCol add: (building1 streetLocation).
"If both buildings are on the same street, return"
(building1 streetName = building2 streetName) ifTrue:
[ pointsCol add: building2 streetLocation.
route _ pointsCol.
^self
].
sNames _ (((s1 exit) keys) asOrderedCollection).
"Add the exit points of the first street to the queue"
1 to: ((sNames) size) do:
[:number | nodeToAdd _ StreetNode new.
nodeToAdd name: (sNames at: number).
pC _ (pointsCol copy).
pC add: ((s1 exit) at: (nodeToAdd name)).
nodeToAdd pointList: pC.
queue enqueue: nodeToAdd.
streetsSeenCol add: (nodeToAdd name).
].
streetsSeenCol add: sn1.
"(true) whileTrue:"
50 timesRepeat:
[ | seen cN |
seen _ false.
currNode _ queue dequeue.
currStreet _ map streetWithName: (currNode name).
((currNode name) = sn2) ifTrue:
[ currNode addPoint: building2 streetLocation.
route _ (currNode pointList).
Transcript show: 'About to return'; cr.
Transcript show: (route printString).
^self
].
sNames _ (((currStreet exit) keys) asOrderedCollection).
1 to: ((sNames) size) do:
[:number | seen _ false.
streetsSeenCol do:
[:aName | ((sNames at: number) = aName) ifTrue:
[ seen _ true.].
].
(seen = false) ifTrue:
[ nodeToAdd _ StreetNode new.
nodeToAdd name: (sNames at: number).
cN _ (currNode deepCopy).
cN addPoint: ((currStreet exit) at: (nodeToAddname)).
nodeToAdd pointList: (cN pointList).
queue enqueue: nodeToAdd.
streetsSeenCol add: (nodeToAdd name).
].
].
].