]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouteplanner.cpp
Unify destination and waypoints
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
index 5bffec1d899c535fc295bf2847faea29d7a2987d..08d787805140610a41af21100c4894ea0870c615 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.destination)
+               if(!info.waypoints.empty())
                        routed_trains.push_back(info);
        }
 }
@@ -216,24 +216,18 @@ TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
        speed(train->get_maximum_speed()),
        first_noncritical(train->get_first_noncritical_block().block()),
        router(train->get_ai_of_type<TrainRouter>()),
-       destination(0),
+       waypoints(router ? router->get_n_waypoints() : 0),
        has_duration(false)
 {
-       if(router)
+       if(!waypoints.empty())
        {
-               destination = router->get_destination();
-               if(destination)
+               metrics.resize(waypoints.size());
+               for(unsigned i=0; i<waypoints.size(); ++i)
                {
-                       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();
+                       waypoints[i] = &router->get_waypoint(i);
+                       metrics[i] = &router->get_metric(i);
                }
+               has_duration = router->get_trip_duration();
        }
 
        // If no maximum speed is specified, use a sensible default
@@ -278,7 +272,7 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
        state(MOVING),
        delay(info->router->get_departure_delay()),
        duration(info->router->get_trip_duration()),
-       waypoint(info->waypoints.empty() ? -1 : 0),
+       waypoint(0),
        blocked_by(-1)
 {
        const Vehicle *veh = &info->train->get_vehicle(0);
@@ -352,16 +346,16 @@ bool TrainRoutePlanner::TrainRoutingState::check_arrival()
 {
        TrackIter next_track = track.next(path);
 
-       if(waypoint<0 && info->destination->has_track(*track) && !info->destination->has_track(*next_track))
-       {
-               state = ARRIVED;
-               return true;
-       }
-       else if(waypoint>=0 && info->waypoints[waypoint]->has_track(*track) && !info->waypoints[waypoint]->has_track(*next_track))
+       const TrackChain *wp_chain = info->waypoints[waypoint];
+       if(wp_chain->has_track(*track) && !wp_chain->has_track(*next_track))
        {
-               ++waypoint;
-               if(waypoint>=static_cast<int>(info->waypoints.size()))
-                       waypoint = -1;
+               if(waypoint+1<info->waypoints.size())
+                       ++waypoint;
+               else
+               {
+                       state = ARRIVED;
+                       return true;
+               }
        }
 
        if(info->first_noncritical->has_track(*track))
@@ -449,7 +443,7 @@ void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path)
 void TrainRoutePlanner::TrainRoutingState::update_estimate()
 {
        TrackIter iter = track.reverse(path);
-       float distance = info->metrics[waypoint+1]->get_distance_from(*iter.track(), iter.entry());
+       float distance = info->metrics[waypoint]->get_distance_from(*iter.track(), iter.entry());
        distance += track->get_type().get_path_length(path)-offset;
        remaining_estimate = distance;
 }