X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouteplanner.cpp;h=ad97cf79f77c0255f23b9c1be7ed5cf0cfe58e69;hb=5b71cb905051d654c150cb0e098cade0cb502105;hp=e083087ba1b64354bef11f662dd4ea2bc36cf011;hpb=2225814e69913aecaee53b0505d1b92197621b10;p=r2c2.git diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index e083087..ad97cf7 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -16,6 +16,7 @@ namespace R2C2 { TrainRoutePlanner::TrainRoutePlanner(Layout &layout): goal(0), + path_switch_bias(15*Time::sec), timeout(10*Time::sec), result(PENDING), thread(0) @@ -147,7 +148,12 @@ void TrainRoutePlanner::add_steps(const RoutingStep &step) { list new_steps; step.create_successors(new_steps); + if(new_steps.empty()) + return; + new_steps.sort(); + if(!queue.empty() && new_steps.front().cost_estimateget_maximum_speed()), first_noncritical(train->get_last_critical_block().next().block()), router(train->get_ai_of_type()), @@ -254,6 +261,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(); @@ -352,19 +363,24 @@ TrainRoutePlanner::TrainRoutingState::~TrainRoutingState() Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() const { - return ((track->get_type().get_path_length(path)-offset)/info->speed)*Time::sec+delay; + return ((occupied_tracks->path_length-offset)/info->speed)*Time::sec+delay; } 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; @@ -489,12 +505,22 @@ void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path) offset = 0; } +void TrainRoutePlanner::TrainRoutingState::set_path(unsigned p) +{ + path = p; + OccupiedTrack *next_occ = occupied_tracks->next; + if(!--occupied_tracks->refcount) + delete occupied_tracks; + occupied_tracks = new OccupiedTrack(*track, path, next_occ); + update_estimate(); +} + void TrainRoutePlanner::TrainRoutingState::update_estimate() { TrackIter iter = track.reverse(path); 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; + remaining_estimate += occupied_tracks->path_length-offset; } bool TrainRoutePlanner::TrainRoutingState::is_viable() const @@ -508,13 +534,14 @@ bool TrainRoutePlanner::TrainRoutingState::is_viable() const TrainRoutePlanner::RoutingStep::RoutingStep(): + preferred(false), prev(0) { } TrainRoutePlanner::RoutingStep::RoutingStep(const RoutingStep *p): time(p->time), - penalty(p->penalty), cost_estimate(p->cost_estimate), + preferred(false), trains(p->trains), prev(p) { } @@ -560,13 +587,6 @@ void TrainRoutePlanner::RoutingStep::create_successors(list &new_st create_successor(next, train_index, i, new_steps); } - new_steps.sort(); - for(list::iterator i=new_steps.begin(); ++i!=new_steps.end(); ) - { - i->penalty += 5*Time::sec; - i->update_estimate(); - } - if(entry_ep.paths!=train.track->get_type().get_paths() && !train.critical) { /* Create a waiting state before the track if there's at least one path @@ -594,8 +614,7 @@ void TrainRoutePlanner::RoutingStep::create_successor(RoutingStep &next, unsigne { TrainRoutingState &train = next.trains[train_index]; - train.path = path; - train.update_estimate(); + train.set_path(path); next.update_estimate(); if(next.is_viable()) new_steps.push_back(next); @@ -622,12 +641,18 @@ 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; } else if(i->state==BLOCKED) + { + i->estimated_wait = Time::zero; i->state = MOVING; + } } else i->state = BLOCKED; @@ -703,7 +728,7 @@ void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt) void TrainRoutePlanner::RoutingStep::update_estimate() { - cost_estimate = penalty; + cost_estimate = Time::zero; for(vector::const_iterator i=trains.begin(); i!=trains.end(); ++i) if(i->remaining_estimate>=0) cost_estimate += i->wait_time+i->estimated_wait+((i->distance_traveled+i->remaining_estimate)/i->info->speed)*Time::sec; @@ -732,6 +757,8 @@ bool TrainRoutePlanner::RoutingStep::is_goal() const bool TrainRoutePlanner::RoutingStep::operator<(const RoutingStep &other) const { + if(preferred!=other.preferred) + return preferred>other.preferred; return cost_estimate