From: Mikko Rasa Date: Sun, 13 Apr 2014 17:02:42 +0000 (+0300) Subject: Refactor TrainRoutePlanner so that the same object may be used again X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=df72b71642bbc5b9a4e5010ebca8643fbeea3ca8;p=r2c2.git Refactor TrainRoutePlanner so that the same object may be used again This is unlikely to be needed, but it makes the design more robust. --- diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index dbdf293..8126ec5 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -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::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::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 && kinfo->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 && ktrack->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); }