X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouteplanner.cpp;h=b6d2f07cb66203ded55f3471110f94a0270287c0;hb=528c4f8760ac54fb2d1f3002248ecf6774956030;hp=9bed406f102576c893a6f3b890ad031d36329840;hpb=19a73af1fb956636cb8b9d563ffb6a42e827a5bf;p=r2c2.git diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index 9bed406..b6d2f07 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; @@ -214,18 +230,18 @@ void TrainRoutePlanner::finalize_plan() TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t): train(&t), speed(train->get_maximum_speed()), - first_noncritical(train->get_first_noncritical_block().block()), + 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 @@ -502,23 +519,14 @@ void TrainRoutePlanner::RoutingStep::create_successors(list &new_st const TrackType::Endpoint &entry_ep = train.track.endpoint(); if(train.critical) { - train.path = train.track->get_type().coerce_path(train.track.entry(), train.track->get_active_path()); - train.update_estimate(); - next.update_estimate(); - if(next.is_viable()) - new_steps.push_back(next); + unsigned critical_path = train.track->get_type().coerce_path(train.track.entry(), train.track->get_active_path()); + create_successor(next, train_index, critical_path, new_steps); } else { for(unsigned i=0; entry_ep.paths>>i; ++i) if(entry_ep.has_path(i)) - { - train.path = i; - train.update_estimate(); - next.update_estimate(); - if(next.is_viable()) - new_steps.push_back(next); - } + create_successor(next, train_index, i, new_steps); } new_steps.sort(); @@ -540,6 +548,17 @@ void TrainRoutePlanner::RoutingStep::create_successors(list &new_st } } +void TrainRoutePlanner::RoutingStep::create_successor(RoutingStep &next, unsigned train_index, unsigned path, list &new_steps) +{ + TrainRoutingState &train = next.trains[train_index]; + + train.path = path; + train.update_estimate(); + next.update_estimate(); + if(next.is_viable()) + new_steps.push_back(next); +} + bool TrainRoutePlanner::RoutingStep::update_states() { bool changes = false;