]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouteplanner.cpp
Use set_route for the first route
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
index 3f8739347d455405f1f2a6fef03c6b2c02c5e3a9..581bd03fa86f9b3786d0989f96e687130f459ac2 100644 (file)
@@ -2,6 +2,7 @@
 #include "layout.h"
 #include "route.h"
 #include "train.h"
+#include "trainroutemetric.h"
 #include "trainrouteplanner.h"
 #include "trainrouter.h"
 #include "vehicle.h"
@@ -21,60 +22,99 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout)
                        routed_trains.push_back(info);
        }
 
-       steps.push_back(RoutingStep());
-       RoutingStep &start = steps.back();
+       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;
-       for(list<RoutingStep>::iterator i=steps.begin(); i!=steps.end(); ++i)
+       while(!queue.empty())
        {
-               if(i->is_goal())
+               const RoutingStep &step = get_step();
+               if(step.is_goal())
                {
-                       goal = &*i;
+                       goal = &step;
                        break;
                }
 
-               add_steps(*i);
+               add_steps(step);
        }
 
        if(goal)
                create_routes(*goal);
 }
 
+const TrainRoutePlanner::RoutingStep &TrainRoutePlanner::get_step()
+{
+       steps.splice(steps.end(), queue, queue.begin());
+       return steps.back();
+}
+
 void TrainRoutePlanner::add_steps(const RoutingStep &step)
 {
        list<RoutingStep> new_steps;
        step.create_successors(new_steps);
        new_steps.sort();
-       steps.merge(new_steps);
+       queue.merge(new_steps);
 }
 
 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)
+               {
+                       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())
@@ -90,8 +130,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)
@@ -156,6 +195,8 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
                        break;
                iter = iter.next();
        }
+
+       update_estimate();
 }
 
 TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState &other):
@@ -168,6 +209,9 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState
        state(other.state),
        delay(other.delay),
        waypoint(other.waypoint),
+       distance_traveled(other.distance_traveled),
+       remaining_estimate(other.remaining_estimate),
+       wait_time(other.wait_time),
        blocked_by(other.blocked_by)
 {
        ++occupied_tracks->refcount;
@@ -233,6 +277,30 @@ void TrainRoutePlanner::TrainRoutingState::advance(float distance)
                }
                --occupied_tracks->n_tracks;
        }
+
+       distance_traveled += distance;
+       remaining_estimate -= distance;
+}
+
+void TrainRoutePlanner::TrainRoutingState::advance(const Time::TimeDelta &dt)
+{
+       if(delay>=dt)
+       {
+               delay -= dt;
+               return;
+       }
+
+       float secs = dt/Time::sec;
+       if(delay)
+       {
+               secs -= delay/Time::sec;
+               delay = Time::zero;
+       }
+
+       if(state==MOVING)
+               advance(info->speed*secs);
+       else if(state!=ARRIVED)
+               wait_time += secs*Time::sec;
 }
 
 void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path)
@@ -245,6 +313,14 @@ void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path)
        offset = 0;
 }
 
+void TrainRoutePlanner::TrainRoutingState::update_estimate()
+{
+       TrackIter iter = track.reverse(path);
+       float distance = info->router->get_metric(waypoint).get_distance_from(*iter.track(), iter.entry());
+       distance += track->get_type().get_path_length(path)-offset;
+       remaining_estimate = distance;
+}
+
 
 TrainRoutePlanner::RoutingStep::RoutingStep():
        prev(0)
@@ -252,6 +328,7 @@ TrainRoutePlanner::RoutingStep::RoutingStep():
 
 TrainRoutePlanner::RoutingStep::RoutingStep(const RoutingStep *p):
        time(p->time),
+       cost_estimate(p->cost_estimate),
        trains(p->trains),
        prev(p)
 { }
@@ -291,6 +368,8 @@ void TrainRoutePlanner::RoutingStep::create_successors(list<RoutingStep> &new_st
                if(next_entry_ep.has_path(i))
                {
                        train.path = i;
+                       train.update_estimate();
+                       next.update_estimate();
                        if(next.is_viable())
                                new_steps.push_back(next);
                }
@@ -390,21 +469,23 @@ void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt)
 {
        time += dt;
        for(vector<TrainRoutingState>::iterator i=trains.begin(); i!=trains.end(); ++i)
-       {
-               if(i->delay)
-               {
-                       i->delay -= dt;
-                       if(i->delay>Time::zero)
-                               continue;
-                       i->delay = Time::zero;
-               }
-               else if(i->state==MOVING)
-                       i->advance(i->info->speed*(dt/Time::sec));
-       }
+               i->advance(dt);
+}
+
+void TrainRoutePlanner::RoutingStep::update_estimate()
+{
+       cost_estimate = Time::zero;
+       for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+               if(i->remaining_estimate>=0)
+                       cost_estimate += i->wait_time+((i->distance_traveled+i->remaining_estimate)/i->info->speed)*Time::sec;
 }
 
 bool TrainRoutePlanner::RoutingStep::is_viable() const
 {
+       for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+               if(i->remaining_estimate<0)
+                       return false;
+
        for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
                if(i->state==MOVING)
                        return true;
@@ -422,7 +503,7 @@ bool TrainRoutePlanner::RoutingStep::is_goal() const
 
 bool TrainRoutePlanner::RoutingStep::operator<(const RoutingStep &other) const
 {
-       return time<other.time;
+       return cost_estimate<other.cost_estimate;
 }
 
 } // namespace R2C2