From: Mikko Rasa Date: Fri, 4 Apr 2014 16:43:27 +0000 (+0300) Subject: Create multiple routes if the planned route crosses itself X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6cfe927c34e7b866818502b39073b74b47666ba7;p=r2c2.git Create multiple routes if the planned route crosses itself --- diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index 9ba860b..cd79a61 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -66,24 +66,49 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal) { for(vector::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i) { - i->route = new Route(i->train->get_layout()); - i->route->set_name("Router"); - i->route->set_temporary(true); + Route *route = new Route(i->train->get_layout()); + route->set_name("Router"); + route->set_temporary(true); + i->routes.push_front(route); + + for(unsigned j=0; j<3; ++j) + i->track_history[j] = 0; } for(const RoutingStep *i=&goal; i; i=i->prev) { for(vector::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); - j->info->route->add_track(*j->track); + + Route *route = j->info->routes.front(); + if(route->has_track(*j->track)) + { + 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]); + 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]; + history[0] = j->track.track(); } } for(vector::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i) { - i->router->set_route(i->route); + for(list::iterator j=i->routes.begin(); j!=i->routes.end(); ++j) + i->router->add_route(**j); const TrainRoutingState *current_wait = 0; for(list::const_iterator j=i->waits.begin(); j!=i->waits.end(); ++j) if(!current_wait || (*j)->track.track()!=current_wait->track.track()) @@ -99,8 +124,7 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal) TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t): train(&t), speed(train->get_maximum_speed()), - router(train->get_ai_of_type()), - route(0) + router(train->get_ai_of_type()) { // If no maximum speed is specified, use a sensible default if(!speed) diff --git a/source/libr2c2/trainrouteplanner.h b/source/libr2c2/trainrouteplanner.h index 215cbfd..f799420 100644 --- a/source/libr2c2/trainrouteplanner.h +++ b/source/libr2c2/trainrouteplanner.h @@ -24,7 +24,8 @@ private: Train *train; float speed; TrainRouter *router; - Route *route; + std::list routes; + Track *track_history[3]; std::list waits; TrainRoutingInfo(Train &);