Skip to content

Commit

Permalink
#16: Adjust to the path structure changes and fix a few bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
danports committed May 18, 2020
1 parent 589b89c commit 7646457
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
30 changes: 22 additions & 8 deletions src/railnetwork/apis/railnetwork
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,15 @@ function RailNetwork:buildGraph()
-- If direction is not set or is 0, we assume the line does not continue beyond this point.
local direction = node.location.direction
if direction and direction > 0 and index < #sorted then
table.insert(node.edges, {destination = formatLocation(sorted[index + 1].location)})
table.insert(node.edges, {
destination = formatLocation(sorted[index + 1].location),
distance = distanceBetween(node.location, sorted[index + 1].location)
})
elseif direction and direction < 0 and index > 1 then
table.insert(node.edges, {destination = formatLocation(sorted[index - 1].location)})
table.insert(node.edges, {
destination = formatLocation(sorted[index - 1].location),
distance = distanceBetween(node.location, sorted[index - 1].location)
})
end
end
end
Expand Down Expand Up @@ -194,21 +200,29 @@ function RailNetwork:findRoute(trip)
end
if not locationsMatch(trip.origin, originNode) then
-- Add an edge for the segment between the origin and the first location in the network.
table.insert(path, 1, {edge = {destination = formatLocation(originNode)}, weight = 1})
path.origin = formatLocation(trip.origin)
table.insert(path.edges, 1, {
edge = {destination = formatLocation(originNode)},
weight = 1
})
end
if not locationsMatch(trip.destination, destinationNode) then
-- Add an edge for the segment between the last location in the network and the final destination.
table.insert(path, {edge = {destination = formatLocation(trip.destination)}, weight = 1})
path.destination = formatLocation(trip.destination)
table.insert(path.edges, {
edge = {destination = formatLocation(trip.destination)},
weight = 1
})
end
return path
end

local function routeMatches(a, b)
if #a ~= #b then
if #a.edges ~= #b.edges then
return false
end
for key, edge in ipairs(a) do
if edge.destination ~= b[key].destination then
for key, edge in ipairs(a.edges) do
if edge.destination ~= b.edges[key].destination then
return false
end
end
Expand Down Expand Up @@ -242,7 +256,7 @@ function RailNetwork:findRoutes(trip)

addRoute(trip, {})
for tag in pairs(self.tags) do
addRoute({[tag] = true})
addRoute(trip, {[tag] = true})
end
return routes
end
Expand Down
5 changes: 2 additions & 3 deletions src/railstation/startup
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ function selectDepartureRoute(trip)
if #routeOptions == 1 then
local key, route = next(trip.routes)
print(string.format("Only one route to %s (%s); preparing departure: %s",
railnetwork.formatLocation(trip.destination), formatRouteTags(routeOptions[1]),
graph.formatPath(railnetwork.formatLocation(trip.origin), route)))
railnetwork.formatLocation(trip.destination), formatRouteTags(routeOptions[1]), graph.formatPath(route)))
trip.routes = nil
handleDeparture(trip)
return
Expand All @@ -75,9 +74,9 @@ function selectDepartureRoute(trip)
if not selected then
return
end
print(string.format("Preparing departure: %s", graph.formatPath(trip.routes[selected])))
trip.routes = nil
trip.tags = selected[1]
print(string.format("Preparing departure: %s", graph.formatPath(railnetwork.formatLocation(trip.origin), trip.routes[selected])))
handleDeparture(trip)
end

Expand Down

0 comments on commit 7646457

Please sign in to comment.