]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouteplanner.cpp
Eliminate the one step delay in adding tracks to routes
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
index 7ee655a1d435acff22ff55cf3ac19bbd38fa0830..30d9f5e482ee054aa4a5dee7df770abbddd743a7 100644 (file)
@@ -14,7 +14,9 @@ using namespace Msp;
 namespace R2C2 {
 
 TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
-       result(PENDING)
+       goal(0),
+       result(PENDING),
+       thread(0)
 {
        const map<unsigned, Train *> &trains = layout.get_trains();
        for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
@@ -25,38 +27,40 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
        }
 }
 
-void TrainRoutePlanner::plan()
+TrainRoutePlanner::~TrainRoutePlanner()
 {
-       steps.clear();
-       queue.clear();
-       result = PENDING;
+       delete thread;
+}
 
-       queue.push_back(RoutingStep());
-       RoutingStep &start = queue.back();
-       for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
-               start.trains.push_back(TrainRoutingState(*i));
-       start.update_estimate();
+TrainRoutePlanner::Result TrainRoutePlanner::plan()
+{
+       prepare_plan();
+       create_plan();
+       if(result==PENDING)
+               finalize_plan();
 
-       const RoutingStep *goal = 0;
-       while(!queue.empty())
-       {
-               const RoutingStep &step = get_step();
-               if(step.is_goal())
-               {
-                       goal = &step;
-                       break;
-               }
+       return result;
+}
 
-               add_steps(step);
-       }
+void TrainRoutePlanner::plan_async()
+{
+       if(thread)
+               throw logic_error("already planning");
+
+       prepare_plan();
+       thread = new PlanningThread(*this);
+}
 
-       if(goal)
+TrainRoutePlanner::Result TrainRoutePlanner::check()
+{
+       if(result==PENDING && goal)
        {
-               create_routes(*goal);
-               result = COMPLETE;
+               finalize_plan();
+               delete thread;
+               thread = 0;
        }
-       else
-               result = FAILED;
+
+       return result;
 }
 
 const list<Route *> &TrainRoutePlanner::get_routes_for(const Train &train) const
@@ -84,6 +88,37 @@ const TrainRoutePlanner::RoutingStep &TrainRoutePlanner::get_step()
        return steps.back();
 }
 
+void TrainRoutePlanner::prepare_plan()
+{
+       steps.clear();
+       queue.clear();
+       goal = 0;
+       result = PENDING;
+
+       queue.push_back(RoutingStep());
+       RoutingStep &start = queue.back();
+       for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
+               start.trains.push_back(TrainRoutingState(*i));
+       start.update_estimate();
+}
+
+void TrainRoutePlanner::create_plan()
+{
+       while(!queue.empty())
+       {
+               const RoutingStep &step = get_step();
+               if(step.is_goal())
+               {
+                       goal = &step;
+                       return;
+               }
+
+               add_steps(step);
+       }
+
+       result = FAILED;
+}
+
 void TrainRoutePlanner::add_steps(const RoutingStep &step)
 {
        list<RoutingStep> new_steps;
@@ -92,19 +127,19 @@ void TrainRoutePlanner::add_steps(const RoutingStep &step)
        queue.merge(new_steps);
 }
 
-void TrainRoutePlanner::create_routes(const RoutingStep &goal)
+void TrainRoutePlanner::finalize_plan()
 {
        for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
        {
                i->routes.clear();
                i->sequence.clear();
-               for(unsigned j=0; j<3; ++j)
+               for(unsigned j=0; j<2; ++j)
                        i->track_history[j] = 0;
        }
 
        map<Track *, TrainRouter::SequencePoint *> sequenced_tracks;
        unsigned sequence = steps.size();
-       for(const RoutingStep *i=&goal; i; i=i->prev)
+       for(const RoutingStep *i=goal; i; i=i->prev)
                for(vector<TrainRoutingState>::const_iterator j=i->trains.begin(); j!=i->trains.end(); ++j)
                {
                        Track **history = j->info->track_history;
@@ -133,16 +168,13 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal)
                                route = new Route(j->info->train->get_layout());
                                route->set_name("Router");
                                route->set_temporary(true);
-                               for(unsigned k=2; k>0; --k)
-                                       if(history[k])
-                                               route->add_track(*history[k]);
+                               for(unsigned k=0; (k<2 && history[k]); ++k)
+                                       route->add_track(*history[k]);
                                j->info->routes.push_front(route);
                        }
 
-                       if(history[0])
-                               route->add_track(*history[0]);
-                       for(unsigned k=2; k>0; --k)
-                               history[k] = history[k-1];
+                       route->add_track(*j->track.track());
+                       history[1] = history[0];
                        history[0] = j->track.track();
 
                        bool waitable = j->track.endpoint().paths!=j->track->get_type().get_paths();
@@ -166,6 +198,8 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal)
                                --sequence;
                        }
                }
+
+       result = COMPLETE;
 }
 
 
@@ -548,4 +582,16 @@ bool TrainRoutePlanner::RoutingStep::operator<(const RoutingStep &other) const
        return cost_estimate<other.cost_estimate;
 }
 
+
+TrainRoutePlanner::PlanningThread::PlanningThread(TrainRoutePlanner &p):
+       planner(p)
+{
+       launch();
+}
+
+void TrainRoutePlanner::PlanningThread::main()
+{
+       planner.create_plan();
+}
+
 } // namespace R2C2