X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouteplanner.cpp;h=75d746b3c6dd103cd148ae233f90361a189c2328;hb=3dd660ffad729fbd6e75e6401f5c7f27b9013faf;hp=9f19ffcdfb06ae470447ebeb5f01154845ae49ea;hpb=ecd7af790bd3ab7c7e768f68968379e1feea56a9;p=r2c2.git diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index 9f19ffc..75d746b 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -1,4 +1,5 @@ #include +#include #include "catalogue.h" #include "layout.h" #include "route.h" @@ -15,6 +16,7 @@ namespace R2C2 { TrainRoutePlanner::TrainRoutePlanner(Layout &layout): goal(0), + timeout(10*Time::sec), result(PENDING), thread(0) { @@ -36,6 +38,11 @@ TrainRoutePlanner::~TrainRoutePlanner() } } +void TrainRoutePlanner::set_timeout(const Time::TimeDelta &t) +{ + timeout = t; +} + TrainRoutePlanner::Result TrainRoutePlanner::plan() { prepare_plan(); @@ -112,6 +119,8 @@ void TrainRoutePlanner::prepare_plan() void TrainRoutePlanner::create_plan() { + Time::TimeStamp timeout_stamp = Time::now()+timeout; + unsigned count = 0; while(!queue.empty()) { const RoutingStep &step = get_step(); @@ -122,6 +131,13 @@ void TrainRoutePlanner::create_plan() } add_steps(step); + + if(++count>=1000) + { + if(Time::now()>timeout_stamp) + break; + count = 0; + } } result = FAILED; @@ -216,16 +232,16 @@ TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t): speed(train->get_maximum_speed()), first_noncritical(train->get_last_critical_block().next().block()), router(train->get_ai_of_type()), - waypoints(router ? router->get_n_waypoints() : 0), has_duration(false) { - if(!waypoints.empty()) + if(unsigned n_wps = router->get_n_waypoints()) { - metrics.resize(waypoints.size()); - for(unsigned i=0; iget_waypoint(i); - metrics[i] = &router->get_metric(i); + waypoints.push_back(router->get_waypoint(i)); + metrics.push_back(&router->get_metric(i)); } has_duration = router->get_trip_duration(); } @@ -346,17 +362,18 @@ bool TrainRoutePlanner::TrainRoutingState::check_arrival() { TrackIter next_track = track.next(path); - const TrackChain *wp_chain = info->waypoints[waypoint]; - if(wp_chain->has_track(*track) && !wp_chain->has_track(*next_track)) - { - if(waypoint+1waypoints.size()) - ++waypoint; - else + const TrainRouter::Waypoint &wp = info->waypoints[waypoint]; + if(wp.chain->has_track(*track) && !wp.chain->has_track(*next_track)) + if(wp.direction==TrackChain::UNSPECIFIED || track==wp.chain->iter_for(*track, wp.direction)) { - state = ARRIVED; - return true; + if(waypoint+1waypoints.size()) + ++waypoint; + else + { + state = ARRIVED; + return true; + } } - } if(info->first_noncritical->has_track(*track)) critical = false; @@ -443,9 +460,9 @@ void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path) void TrainRoutePlanner::TrainRoutingState::update_estimate() { TrackIter iter = track.reverse(path); - float distance = info->metrics[waypoint]->get_distance_from(*iter.track(), iter.entry()); - distance += track->get_type().get_path_length(path)-offset; - remaining_estimate = distance; + remaining_estimate = info->metrics[waypoint]->get_distance_from(*iter.track(), iter.entry()); + if(remaining_estimate>=0) + remaining_estimate += track->get_type().get_path_length(path)-offset; } bool TrainRoutePlanner::TrainRoutingState::is_viable() const