]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouteplanner.cpp
Allow direction to be specified for routing waypoints
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
index 9f19ffcdfb06ae470447ebeb5f01154845ae49ea..75d746b3c6dd103cd148ae233f90361a189c2328 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/core/maputils.h>
+#include <msp/time/utils.h>
 #include "catalogue.h"
 #include "layout.h"
 #include "route.h"
@@ -15,6 +16,7 @@ namespace R2C2 {
 
 TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
        goal(0),
+       timeout(10*Time::sec),
        result(PENDING),
        thread(0)
 {
@@ -36,6 +38,11 @@ TrainRoutePlanner::~TrainRoutePlanner()
        }
 }
 
+void TrainRoutePlanner::set_timeout(const Time::TimeDelta &t)
+{
+       timeout = t;
+}
+
 TrainRoutePlanner::Result TrainRoutePlanner::plan()
 {
        prepare_plan();
@@ -112,6 +119,8 @@ void TrainRoutePlanner::prepare_plan()
 
 void TrainRoutePlanner::create_plan()
 {
+       Time::TimeStamp timeout_stamp = Time::now()+timeout;
+       unsigned count = 0;
        while(!queue.empty())
        {
                const RoutingStep &step = get_step();
@@ -122,6 +131,13 @@ void TrainRoutePlanner::create_plan()
                }
 
                add_steps(step);
+
+               if(++count>=1000)
+               {
+                       if(Time::now()>timeout_stamp)
+                               break;
+                       count = 0;
+               }
        }
 
        result = FAILED;
@@ -216,16 +232,16 @@ TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
        speed(train->get_maximum_speed()),
        first_noncritical(train->get_last_critical_block().next().block()),
        router(train->get_ai_of_type<TrainRouter>()),
-       waypoints(router ? router->get_n_waypoints() : 0),
        has_duration(false)
 {
-       if(!waypoints.empty())
+       if(unsigned n_wps = router->get_n_waypoints())
        {
-               metrics.resize(waypoints.size());
-               for(unsigned i=0; i<waypoints.size(); ++i)
+               waypoints.reserve(n_wps),
+               metrics.reserve(n_wps);
+               for(unsigned i=0; i<n_wps; ++i)
                {
-                       waypoints[i] = &router->get_waypoint(i);
-                       metrics[i] = &router->get_metric(i);
+                       waypoints.push_back(router->get_waypoint(i));
+                       metrics.push_back(&router->get_metric(i));
                }
                has_duration = router->get_trip_duration();
        }
@@ -346,17 +362,18 @@ bool TrainRoutePlanner::TrainRoutingState::check_arrival()
 {
        TrackIter next_track = track.next(path);
 
-       const TrackChain *wp_chain = info->waypoints[waypoint];
-       if(wp_chain->has_track(*track) && !wp_chain->has_track(*next_track))
-       {
-               if(waypoint+1<info->waypoints.size())
-                       ++waypoint;
-               else
+       const TrainRouter::Waypoint &wp = info->waypoints[waypoint];
+       if(wp.chain->has_track(*track) && !wp.chain->has_track(*next_track))
+               if(wp.direction==TrackChain::UNSPECIFIED || track==wp.chain->iter_for(*track, wp.direction))
                {
-                       state = ARRIVED;
-                       return true;
+                       if(waypoint+1<info->waypoints.size())
+                               ++waypoint;
+                       else
+                       {
+                               state = ARRIVED;
+                               return true;
+                       }
                }
-       }
 
        if(info->first_noncritical->has_track(*track))
                critical = false;
@@ -443,9 +460,9 @@ void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path)
 void TrainRoutePlanner::TrainRoutingState::update_estimate()
 {
        TrackIter iter = track.reverse(path);
-       float distance = info->metrics[waypoint]->get_distance_from(*iter.track(), iter.entry());
-       distance += track->get_type().get_path_length(path)-offset;
-       remaining_estimate = distance;
+       remaining_estimate = info->metrics[waypoint]->get_distance_from(*iter.track(), iter.entry());
+       if(remaining_estimate>=0)
+               remaining_estimate += track->get_type().get_path_length(path)-offset;
 }
 
 bool TrainRoutePlanner::TrainRoutingState::is_viable() const