From 54cb832334a83fc266b59e2c9b60ee936db7118f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 23 Feb 2015 12:14:58 +0200 Subject: [PATCH] Consider train length when calculating wait time estimate This allows also estimating wait time when a train is blocked. --- source/libr2c2/trainrouteplanner.cpp | 17 +++++++++++++++-- source/libr2c2/trainrouteplanner.h | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index 87b7cb5..e42e618 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -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()), @@ -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; iget_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; wpwaypoints.size(); ++wp) { float distance = info->metrics[wp]->get_distance_from(trk); if(distance>=0 && distancespeed)*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; diff --git a/source/libr2c2/trainrouteplanner.h b/source/libr2c2/trainrouteplanner.h index 5d52f10..79a6036 100644 --- a/source/libr2c2/trainrouteplanner.h +++ b/source/libr2c2/trainrouteplanner.h @@ -31,6 +31,7 @@ private: struct TrainRoutingInfo { Train *train; + float length; float speed; Block *first_noncritical; TrainRouter *router; -- 2.45.2