]> git.tdb.fi Git - r2c2.git/commitdiff
Consider train length when calculating wait time estimate
authorMikko Rasa <tdb@tdb.fi>
Mon, 23 Feb 2015 10:14:58 +0000 (12:14 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 23 Feb 2015 10:14:58 +0000 (12:14 +0200)
This allows also estimating wait time when a train is blocked.

source/libr2c2/trainrouteplanner.cpp
source/libr2c2/trainrouteplanner.h

index 87b7cb57f606757f3f2a2cba8f9c3f1095807879..e42e61838d4fdc0c7dfa74fe2aa7b451211c8421 100644 (file)
@@ -237,6 +237,7 @@ void TrainRoutePlanner::finalize_plan()
 
 TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
        train(&t),
+       length(0),
        speed(train->get_maximum_speed()),
        first_noncritical(train->get_last_critical_block().next().block()),
        router(train->get_ai_of_type<TrainRouter>()),
@@ -254,6 +255,10 @@ TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
                has_duration = router->get_trip_duration();
        }
 
+       unsigned n_vehs = train->get_n_vehicles();
+       for(unsigned i=0; i<n_vehs; ++i)
+               length += train->get_vehicle(i).get_type().get_length();
+
        // If no maximum speed is specified, use a sensible default
        if(!speed)
                speed = 20*train->get_layout().get_catalogue().get_scale();
@@ -358,13 +363,18 @@ Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() c
 Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_pass(Track &trk) const
 {
        if(is_occupying(trk))
-               return Time::zero;
+       {
+               float passed_length = 0;
+               for(const OccupiedTrack *occ=occupied_tracks; (occ && occ->track!=&trk); occ=occ->next)
+                       passed_length += occ->path_length;
+               return (max(info->length-passed_length, 0.0f)/info->speed)*Time::sec+delay;
+       }
 
        for(unsigned wp=waypoint; wp<info->waypoints.size(); ++wp)
        {
                float distance = info->metrics[wp]->get_distance_from(trk);
                if(distance>=0 && distance<remaining_estimate)
-                       return ((remaining_estimate-distance)/info->speed)*Time::sec+delay;
+                       return ((remaining_estimate-distance+info->length)/info->speed)*Time::sec+delay;
        }
 
        return Time::day;
@@ -622,6 +632,9 @@ bool TrainRoutePlanner::RoutingStep::update_states()
                                if(i->info->first_noncritical->has_track(*next_track))
                                        i->critical = false;
 
+                               if(i->state!=BLOCKED)
+                                       i->estimated_wait = trains[i->blocked_by].get_time_to_pass(*next_track);
+
                                /* Trains in the WAITING state will also transition to BLOCKED and
                                then to MOVING when the other train has passed. */
                                i->state = BLOCKED;
index 5d52f10186d6699e709fb6c2967dcf1b3cdf5aff..79a6036e6d1fbe5b8c7a6c23a70f616a8d6c6216 100644 (file)
@@ -31,6 +31,7 @@ private:
        struct TrainRoutingInfo
        {
                Train *train;
+               float length;
                float speed;
                Block *first_noncritical;
                TrainRouter *router;