]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouteplanner.cpp
Don't access waypoint and metric data if the router has no destination
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
index b4f01ab6a860084901add49d3ab21de3405d49d0..0d0103cb3f4ef274cd1ea4544d2557f87a9fa8c5 100644 (file)
@@ -22,7 +22,7 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
        for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
        {
                TrainRoutingInfo info(*i->second);
-               if(info.router && info.router->get_destination())
+               if(info.destination)
                        routed_trains.push_back(info);
        }
 }
@@ -133,7 +133,7 @@ void TrainRoutePlanner::finalize_plan()
        {
                i->routes.clear();
                i->sequence.clear();
-               for(unsigned j=0; j<3; ++j)
+               for(unsigned j=0; j<2; ++j)
                        i->track_history[j] = 0;
        }
 
@@ -168,16 +168,13 @@ void TrainRoutePlanner::finalize_plan()
                                route = new Route(j->info->train->get_layout());
                                route->set_name("Router");
                                route->set_temporary(true);
-                               for(unsigned k=2; k>0; --k)
-                                       if(history[k])
-                                               route->add_track(*history[k]);
+                               for(unsigned k=0; (k<2 && history[k]); ++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];
+                       route->add_track(*j->track.track());
+                       history[1] = history[0];
                        history[0] = j->track.track();
 
                        bool waitable = j->track.endpoint().paths!=j->track->get_type().get_paths();
@@ -209,8 +206,27 @@ void TrainRoutePlanner::finalize_plan()
 TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
        train(&t),
        speed(train->get_maximum_speed()),
-       router(train->get_ai_of_type<TrainRouter>())
+       router(train->get_ai_of_type<TrainRouter>()),
+       destination(0),
+       has_duration(false)
 {
+       if(router)
+       {
+               destination = router->get_destination();
+               if(destination)
+               {
+                       waypoints.resize(router->get_n_waypoints());
+                       metrics.resize(waypoints.size()+1);
+                       metrics[0] = &router->get_metric(-1);
+                       for(unsigned i=0; i<waypoints.size(); ++i)
+                       {
+                               waypoints[i] = &router->get_waypoint(i);
+                               metrics[i+1] = &router->get_metric(i);
+                       }
+                       has_duration = router->get_trip_duration();
+               }
+       }
+
        // If no maximum speed is specified, use a sensible default
        if(!speed)
                speed = 20*train->get_layout().get_catalogue().get_scale();
@@ -251,7 +267,8 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
        occupied_tracks(0),
        state(MOVING),
        delay(info->router->get_departure_delay()),
-       waypoint(info->router->get_n_waypoints() ? 0 : -1),
+       duration(info->router->get_trip_duration()),
+       waypoint(info->waypoints.empty() ? -1 : 0),
        blocked_by(-1)
 {
        const Vehicle *veh = &info->train->get_vehicle(0);
@@ -287,6 +304,7 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState
        back_offset(other.back_offset),
        state(other.state),
        delay(other.delay),
+       duration(other.duration),
        waypoint(other.waypoint),
        distance_traveled(other.distance_traveled),
        remaining_estimate(other.remaining_estimate),
@@ -309,6 +327,9 @@ Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() c
 
 bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const
 {
+       if(state==ARRIVED && !duration && info->has_duration)
+               return false;
+
        OccupiedTrack *occ = occupied_tracks;
        for(unsigned n=occ->n_tracks; n>0; --n, occ=occ->next)
                if(occ->track==&trk)
@@ -318,18 +339,17 @@ bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const
 
 bool TrainRoutePlanner::TrainRoutingState::check_arrival()
 {
-       TrainRouter &router = *info->router;
        TrackIter next_track = track.next(path);
 
-       if(waypoint<0 && router.is_destination(*track) && !router.is_destination(*next_track))
+       if(waypoint<0 && info->destination->has_track(*track) && !info->destination->has_track(*next_track))
        {
                state = ARRIVED;
                return true;
        }
-       else if(waypoint>=0 && router.is_waypoint(waypoint, *track) && !router.is_waypoint(waypoint, *next_track))
+       else if(waypoint>=0 && info->waypoints[waypoint]->has_track(*track) && !info->waypoints[waypoint]->has_track(*next_track))
        {
                ++waypoint;
-               if(waypoint>=static_cast<int>(router.get_n_waypoints()))
+               if(waypoint>=static_cast<int>(info->waypoints.size()))
                        waypoint = -1;
        }
 
@@ -376,6 +396,9 @@ void TrainRoutePlanner::TrainRoutingState::advance(const Time::TimeDelta &dt)
                delay = Time::zero;
        }
 
+       if(duration)
+               duration = max(duration-secs*Time::sec, Time::zero);
+
        if(state==MOVING)
                advance(info->speed*secs);
        else if(state!=ARRIVED)
@@ -395,7 +418,7 @@ void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path)
 void TrainRoutePlanner::TrainRoutingState::update_estimate()
 {
        TrackIter iter = track.reverse(path);
-       float distance = info->router->get_metric(waypoint).get_distance_from(*iter.track(), iter.entry());
+       float distance = info->metrics[waypoint+1]->get_distance_from(*iter.track(), iter.entry());
        distance += track->get_type().get_path_length(path)-offset;
        remaining_estimate = distance;
 }
@@ -407,6 +430,7 @@ TrainRoutePlanner::RoutingStep::RoutingStep():
 
 TrainRoutePlanner::RoutingStep::RoutingStep(const RoutingStep *p):
        time(p->time),
+       penalty(p->penalty),
        cost_estimate(p->cost_estimate),
        trains(p->trains),
        prev(p)
@@ -453,11 +477,20 @@ void TrainRoutePlanner::RoutingStep::create_successors(list<RoutingStep> &new_st
                                new_steps.push_back(next);
                }
 
+       new_steps.sort();
+       for(list<RoutingStep>::iterator i=new_steps.begin(); ++i!=new_steps.end(); )
+       {
+               i->penalty += 5*Time::sec;
+               i->update_estimate();
+       }
+
        if(next_entry_ep.paths!=next_track->get_type().get_paths())
        {
                RoutingStep wait(this);
                wait.advance(dt);
                wait.trains[train_index].state = WAITING;
+               wait.penalty += 15*Time::sec;
+               wait.update_estimate();
                if(wait.is_viable())
                        new_steps.push_back(wait);
        }
@@ -553,7 +586,7 @@ void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt)
 
 void TrainRoutePlanner::RoutingStep::update_estimate()
 {
-       cost_estimate = Time::zero;
+       cost_estimate = penalty;
        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;