X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouteplanner.cpp;h=a05e97ea7fb2fed688651a3b6b81c104d3d5d19c;hb=bb75bc76ddd99e7d3edaf6e7dbd3719ea332052c;hp=99700699c452a21a81ef51bdad8dd25b5cab8e5e;hpb=e8d2abb48b5236cc3455a035628292ae7908240e;p=r2c2.git diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index 9970069..a05e97e 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -22,14 +22,18 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout): for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) { TrainRoutingInfo info(*i->second); - if(info.router && info.router->get_destination()) + if(info.destination) routed_trains.push_back(info); } } TrainRoutePlanner::~TrainRoutePlanner() { - delete thread; + if(thread) + { + thread->join(); + delete thread; + } } TrainRoutePlanner::Result TrainRoutePlanner::plan() @@ -55,9 +59,13 @@ TrainRoutePlanner::Result TrainRoutePlanner::check() { if(result==PENDING && goal) { + if(thread) + { + thread->join(); + delete thread; + thread = 0; + } finalize_plan(); - delete thread; - thread = 0; } return result; @@ -206,8 +214,28 @@ void TrainRoutePlanner::finalize_plan() TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t): train(&t), speed(train->get_maximum_speed()), - router(train->get_ai_of_type()) + first_noncritical(train->get_first_noncritical_block().block()), + router(train->get_ai_of_type()), + destination(0), + has_duration(false) { + if(router) + { + destination = router->get_destination(); + if(destination) + { + waypoints.resize(router->get_n_waypoints()); + metrics.resize(waypoints.size()+1); + metrics[0] = &router->get_metric(-1); + for(unsigned i=0; iget_waypoint(i); + metrics[i+1] = &router->get_metric(i); + } + has_duration = router->get_trip_duration(); + } + } + // If no maximum speed is specified, use a sensible default if(!speed) speed = 20*train->get_layout().get_catalogue().get_scale(); @@ -245,11 +273,12 @@ TrainRoutePlanner::OccupiedTrack::~OccupiedTrack() TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf): info(&inf), + critical(true), occupied_tracks(0), state(MOVING), delay(info->router->get_departure_delay()), duration(info->router->get_trip_duration()), - waypoint(info->router->get_n_waypoints() ? 0 : -1), + waypoint(info->waypoints.empty() ? -1 : 0), blocked_by(-1) { const Vehicle *veh = &info->train->get_vehicle(0); @@ -280,6 +309,7 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState info(other.info), track(other.track), path(other.path), + critical(other.critical), occupied_tracks(other.occupied_tracks), offset(other.offset), back_offset(other.back_offset), @@ -308,7 +338,7 @@ Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() c bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const { - if(state==ARRIVED && !duration && info->router->get_trip_duration()) + if(state==ARRIVED && !duration && info->has_duration) return false; OccupiedTrack *occ = occupied_tracks; @@ -320,21 +350,23 @@ bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const bool TrainRoutePlanner::TrainRoutingState::check_arrival() { - TrainRouter &router = *info->router; TrackIter next_track = track.next(path); - if(waypoint<0 && router.is_destination(*track) && !router.is_destination(*next_track)) + if(waypoint<0 && info->destination->has_track(*track) && !info->destination->has_track(*next_track)) { state = ARRIVED; return true; } - else if(waypoint>=0 && router.is_waypoint(waypoint, *track) && !router.is_waypoint(waypoint, *next_track)) + else if(waypoint>=0 && info->waypoints[waypoint]->has_track(*track) && !info->waypoints[waypoint]->has_track(*next_track)) { ++waypoint; - if(waypoint>=static_cast(router.get_n_waypoints())) + if(waypoint>=static_cast(info->waypoints.size())) waypoint = -1; } + if(info->first_noncritical->has_track(*track)) + critical = false; + return false; } @@ -400,7 +432,7 @@ void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path) void TrainRoutePlanner::TrainRoutingState::update_estimate() { TrackIter iter = track.reverse(path); - float distance = info->router->get_metric(waypoint).get_distance_from(*iter.track(), iter.entry()); + float distance = info->metrics[waypoint+1]->get_distance_from(*iter.track(), iter.entry()); distance += track->get_type().get_path_length(path)-offset; remaining_estimate = distance; } @@ -412,6 +444,7 @@ TrainRoutePlanner::RoutingStep::RoutingStep(): TrainRoutePlanner::RoutingStep::RoutingStep(const RoutingStep *p): time(p->time), + penalty(p->penalty), cost_estimate(p->cost_estimate), trains(p->trains), prev(p) @@ -448,21 +481,41 @@ void TrainRoutePlanner::RoutingStep::create_successors(list &new_st train.advance_track(0); const TrackType::Endpoint &next_entry_ep = next_track.endpoint(); - for(unsigned i=0; next_entry_ep.paths>>i; ++i) - if(next_entry_ep.has_path(i)) - { - train.path = i; - train.update_estimate(); - next.update_estimate(); - if(next.is_viable()) - new_steps.push_back(next); - } + 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); + } + else + { + for(unsigned i=0; next_entry_ep.paths>>i; ++i) + if(next_entry_ep.has_path(i)) + { + train.path = i; + train.update_estimate(); + next.update_estimate(); + if(next.is_viable()) + new_steps.push_back(next); + } + } + + new_steps.sort(); + for(list::iterator i=new_steps.begin(); ++i!=new_steps.end(); ) + { + i->penalty += 5*Time::sec; + i->update_estimate(); + } - if(next_entry_ep.paths!=next_track->get_type().get_paths()) + if(next_entry_ep.paths!=next_track->get_type().get_paths() && !train.critical) { RoutingStep wait(this); wait.advance(dt); wait.trains[train_index].state = WAITING; + wait.penalty += 15*Time::sec; + wait.update_estimate(); if(wait.is_viable()) new_steps.push_back(wait); } @@ -558,7 +611,7 @@ void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt) void TrainRoutePlanner::RoutingStep::update_estimate() { - cost_estimate = Time::zero; + cost_estimate = penalty; for(vector::const_iterator i=trains.begin(); i!=trains.end(); ++i) if(i->remaining_estimate>=0) cost_estimate += i->wait_time+((i->distance_traveled+i->remaining_estimate)/i->info->speed)*Time::sec;