]> git.tdb.fi Git - r2c2.git/commitdiff
Refactor TrainRoutePlanner so that the same object may be used again
authorMikko Rasa <tdb@tdb.fi>
Sun, 13 Apr 2014 17:02:42 +0000 (20:02 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 13 Apr 2014 18:47:36 +0000 (21:47 +0300)
This is unlikely to be needed, but it makes the design more robust.

source/libr2c2/trainrouteplanner.cpp

index dbdf293c89b03293761b7273adfd13886c218b58..8126ec5a48270ab65f224cd4f7fb2ee91869e034 100644 (file)
@@ -21,16 +21,19 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout)
                if(info.router && info.router->get_destination())
                        routed_trains.push_back(info);
        }
+}
+
+void TrainRoutePlanner::plan()
+{
+       steps.clear();
+       queue.clear();
 
        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::plan()
-{
        const RoutingStep *goal = 0;
        while(!queue.empty())
        {
@@ -66,11 +69,8 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal)
 {
        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;
        }
@@ -84,15 +84,20 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal)
                        if(j->track.track()==history[0])
                                continue;
 
-                       Route *route = j->info->routes.front();
-                       bool start_new_route = route->has_track(*j->track);
-                       if(!start_new_route)
+                       Route *route = 0;
+                       bool start_new_route = true;
+                       if(!j->info->routes.empty())
                        {
-                               unsigned nls = j->track->get_n_link_slots();
-                               for(unsigned k=0; (!start_new_route && k<nls); ++k)
+                               route = j->info->routes.front();
+                               start_new_route = route->has_track(*j->track);
+                               if(!start_new_route)
                                {
-                                       Track *link = j->track->get_link(k);
-                                       start_new_route = (link && link!=history[0] && route->has_track(*link));
+                                       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));
+                                       }
                                }
                        }
 
@@ -102,7 +107,8 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal)
                                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);
                        }