From 7646457c47de960824c4a8adad867d249e738825 Mon Sep 17 00:00:00 2001 From: Dan Ports Date: Sun, 17 May 2020 19:20:18 -0600 Subject: [PATCH] #16: Adjust to the path structure changes and fix a few bugs. --- src/railnetwork/apis/railnetwork | 30 ++++++++++++++++++++++-------- src/railstation/startup | 5 ++--- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/railnetwork/apis/railnetwork b/src/railnetwork/apis/railnetwork index 0378f62..9d06402 100644 --- a/src/railnetwork/apis/railnetwork +++ b/src/railnetwork/apis/railnetwork @@ -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 @@ -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 @@ -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 diff --git a/src/railstation/startup b/src/railstation/startup index 6539ffc..bd5b5b9 100644 --- a/src/railstation/startup +++ b/src/railstation/startup @@ -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 @@ -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