]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouteplanner.cpp
Make route planning threaded
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
index 581bd03fa86f9b3786d0989f96e687130f459ac2..b4f01ab6a860084901add49d3ab21de3405d49d0 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/maputils.h>
 #include "catalogue.h"
 #include "layout.h"
 #include "route.h"
@@ -12,7 +13,10 @@ using namespace Msp;
 
 namespace R2C2 {
 
-TrainRoutePlanner::TrainRoutePlanner(Layout &layout)
+TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
+       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)
@@ -21,6 +25,75 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout)
                if(info.router && info.router->get_destination())
                        routed_trains.push_back(info);
        }
+}
+
+TrainRoutePlanner::~TrainRoutePlanner()
+{
+       delete thread;
+}
+
+TrainRoutePlanner::Result TrainRoutePlanner::plan()
+{
+       prepare_plan();
+       create_plan();
+       if(result==PENDING)
+               finalize_plan();
+
+       return result;
+}
+
+void TrainRoutePlanner::plan_async()
+{
+       if(thread)
+               throw logic_error("already planning");
+
+       prepare_plan();
+       thread = new PlanningThread(*this);
+}
+
+TrainRoutePlanner::Result TrainRoutePlanner::check()
+{
+       if(result==PENDING && goal)
+       {
+               finalize_plan();
+               delete thread;
+               thread = 0;
+       }
+
+       return result;
+}
+
+const list<Route *> &TrainRoutePlanner::get_routes_for(const Train &train) const
+{
+       return get_train_info(train).routes;
+}
+
+const list<TrainRouter::SequencePoint> &TrainRoutePlanner::get_sequence_for(const Train &train) const
+{
+       return get_train_info(train).sequence;
+}
+
+const TrainRoutePlanner::TrainRoutingInfo &TrainRoutePlanner::get_train_info(const Train &train) const
+{
+       for(vector<TrainRoutingInfo>::const_iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
+               if(i->train==&train)
+                       return *i;
+
+       throw key_error(train.get_name());
+}
+
+const TrainRoutePlanner::RoutingStep &TrainRoutePlanner::get_step()
+{
+       steps.splice(steps.end(), queue, queue.begin());
+       return steps.back();
+}
+
+void TrainRoutePlanner::prepare_plan()
+{
+       steps.clear();
+       queue.clear();
+       goal = 0;
+       result = PENDING;
 
        queue.push_back(RoutingStep());
        RoutingStep &start = queue.back();
@@ -29,29 +102,21 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout)
        start.update_estimate();
 }
 
-void TrainRoutePlanner::plan()
+void TrainRoutePlanner::create_plan()
 {
-       const RoutingStep *goal = 0;
        while(!queue.empty())
        {
                const RoutingStep &step = get_step();
                if(step.is_goal())
                {
                        goal = &step;
-                       break;
+                       return;
                }
 
                add_steps(step);
        }
 
-       if(goal)
-               create_routes(*goal);
-}
-
-const TrainRoutePlanner::RoutingStep &TrainRoutePlanner::get_step()
-{
-       steps.splice(steps.end(), queue, queue.begin());
-       return steps.back();
+       result = FAILED;
 }
 
 void TrainRoutePlanner::add_steps(const RoutingStep &step)
@@ -62,38 +127,50 @@ 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)
        {
-               Route *route = new Route(i->train->get_layout());
-               route->set_name("Router");
-               route->set_temporary(true);
-               i->routes.push_front(route);
-
+               i->routes.clear();
+               i->sequence.clear();
                for(unsigned j=0; j<3; ++j)
                        i->track_history[j] = 0;
        }
 
-       for(const RoutingStep *i=&goal; i; i=i->prev)
-       {
+       map<Track *, TrainRouter::SequencePoint *> sequenced_tracks;
+       unsigned sequence = steps.size();
+       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;
                        if(j->track.track()==history[0])
                                continue;
 
-                       if(j->state==WAITING || j->state==BLOCKED)
-                               j->info->waits.push_front(&*j);
+                       Route *route = 0;
+                       bool start_new_route = true;
+                       if(!j->info->routes.empty())
+                       {
+                               route = j->info->routes.front();
+                               start_new_route = route->has_track(*j->track);
+                               if(!start_new_route)
+                               {
+                                       unsigned nls = j->track->get_n_link_slots();
+                                       for(unsigned k=0; (!start_new_route && k<nls); ++k)
+                                       {
+                                               Track *link = j->track->get_link(k);
+                                               start_new_route = (link && link!=history[0] && route->has_track(*link));
+                                       }
+                               }
+                       }
 
-                       Route *route = j->info->routes.front();
-                       if(route->has_track(*j->track))
+                       if(start_new_route)
                        {
                                route = new Route(j->info->train->get_layout());
                                route->set_name("Router");
                                route->set_temporary(true);
                                for(unsigned k=2; k>0; --k)
-                                       route->add_track(*history[k]);
+                                       if(history[k])
+                                               route->add_track(*history[k]);
                                j->info->routes.push_front(route);
                        }
 
@@ -102,28 +179,30 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal)
                        for(unsigned k=2; k>0; --k)
                                history[k] = history[k-1];
                        history[0] = j->track.track();
-               }
-       }
 
-       for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
-       {
-               for(list<Route *>::iterator j=i->routes.begin(); j!=i->routes.end(); ++j)
-               {
-                       if(j==i->routes.begin())
-                               i->router->set_route(*j);
-                       else
-                               i->router->add_route(**j);
-               }
-
-               const TrainRoutingState *current_wait = 0;
-               for(list<const TrainRoutingState *>::const_iterator j=i->waits.begin(); j!=i->waits.end(); ++j)
-                       if(!current_wait || (*j)->track.track()!=current_wait->track.track())
+                       bool waitable = j->track.endpoint().paths!=j->track->get_type().get_paths();
+                       map<Track *, TrainRouter::SequencePoint *>::iterator k = sequenced_tracks.find(j->track.track());
+                       if(k!=sequenced_tracks.end())
                        {
-                               Block &block = (*j)->track.next()->get_block();
-                               i->router->add_wait(block, 0);
-                               current_wait = *j;
+                               if(!k->second->preceding_train)
+                               {
+                                       k->second->preceding_train = j->info->train;
+                                       k->second->sequence_in = sequence;
+                               }
+                               j->info->sequence.push_front(TrainRouter::SequencePoint(j->track->get_block(), sequence));
+                               if(waitable)
+                                       k->second = &j->info->sequence.front();
+                               --sequence;
                        }
-       }
+                       else if(waitable)
+                       {
+                               j->info->sequence.push_front(TrainRouter::SequencePoint(j->track->get_block(), sequence));
+                               sequenced_tracks[j->track.track()] = &j->info->sequence.front();
+                               --sequence;
+                       }
+               }
+
+       result = COMPLETE;
 }
 
 
@@ -506,4 +585,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