]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouteplanner.cpp
Support routing through multiple waypoints
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
index a7bb13ee13fa3fc7c0c8852796e4e4ec8266c7d8..b98b37e7ee1b2c9b41d6b4a0c41a7f545052f03b 100644 (file)
@@ -121,13 +121,20 @@ void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index)
        RoutingStep next(&step);
        next.advance(dt);
        TrainRouter &router = *train.info->router;
-       if(router.is_destination(*train.track) && !router.is_destination(*next_track))
+       if(train.waypoint<0 && router.is_destination(*train.track) && !router.is_destination(*next_track))
        {
                next.trains[train_index].state = ARRIVED;
                new_steps.push_back(next);
        }
        else
        {
+               if(train.waypoint>=0 && router.is_waypoint(train.waypoint, *train.track) && !router.is_waypoint(train.waypoint, *next_track))
+               {
+                       ++next.trains[train_index].waypoint;
+                       if(next.trains[train_index].waypoint>=static_cast<int>(router.get_n_waypoints()))
+                               next.trains[train_index].waypoint = -1;
+               }
+
                next.trains[train_index].advance_track(0);
 
                const TrackType::Endpoint &next_entry_ep = next_track.endpoint();
@@ -225,7 +232,8 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
        info(&inf),
        occupied_tracks(0),
        state(MOVING),
-       delay(info->router->get_departure_delay())
+       delay(info->router->get_departure_delay()),
+       waypoint(info->router->get_n_waypoints() ? 0 : -1)
 {
        const Vehicle *veh = &info->train->get_vehicle(0);
        // TODO margins
@@ -256,7 +264,8 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState
        occupied_tracks(other.occupied_tracks),
        offset(other.offset),
        back_offset(other.back_offset),
-       state(other.state)
+       state(other.state),
+       waypoint(other.waypoint)
 {
        ++occupied_tracks->refcount;
 }