]> git.tdb.fi Git - r2c2.git/commitdiff
Create multiple routes if the planned route crosses itself
authorMikko Rasa <tdb@tdb.fi>
Fri, 4 Apr 2014 16:43:27 +0000 (19:43 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 4 Apr 2014 16:43:27 +0000 (19:43 +0300)
source/libr2c2/trainrouteplanner.cpp
source/libr2c2/trainrouteplanner.h

index 9ba860be29cd765a1ade273779a040e2ccbdcfff..cd79a61c8f947ff64a4533396fc716ca6ff24387 100644 (file)
@@ -66,24 +66,49 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal)
 {
        for(vector<TrainRoutingInfo>::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<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);
-                       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<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
        {
-               i->router->set_route(i->route);
+               for(list<Route *>::iterator j=i->routes.begin(); j!=i->routes.end(); ++j)
+                       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())
@@ -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<TrainRouter>()),
-       route(0)
+       router(train->get_ai_of_type<TrainRouter>())
 {
        // If no maximum speed is specified, use a sensible default
        if(!speed)
index 215cbfd6c9b1660356c375d1d1bb6aa471f8b4d3..f7994200a33d6d3ff39f4029aeb25e263a8fcea9 100644 (file)
@@ -24,7 +24,8 @@ private:
                Train *train;
                float speed;
                TrainRouter *router;
-               Route *route;
+               std::list<Route *> routes;
+               Track *track_history[3];
                std::list<const TrainRoutingState *> waits;
 
                TrainRoutingInfo(Train &);