X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Flibr2c2%2Ftrainrouteplanner.cpp;h=f517afd0fd142785ac83cbfa8240cda2507f2de7;hb=8c31771a3252f3717ce53fd33567c74003c7cd1a;hp=b98b37e7ee1b2c9b41d6b4a0c41a7f545052f03b;hpb=29878263d8faf9b7e5845e8b1c3d52b139a255cf;p=r2c2.git diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index b98b37e..f517afd 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -1,3 +1,4 @@ +#include "catalogue.h" #include "layout.h" #include "route.h" #include "train.h" @@ -39,7 +40,7 @@ void TrainRoutePlanner::plan() if(update_states(*i)) { - int next_train = find_next_train(*i); + int next_train = i->find_next_train(); if(next_train>=0) add_steps(*i, next_train); } @@ -52,62 +53,16 @@ void TrainRoutePlanner::plan() bool TrainRoutePlanner::update_states(RoutingStep &step) { RoutingStep next(&step); - bool changes = false; - for(vector::iterator i=next.trains.begin(); i!=next.trains.end(); ++i) - { - TrainState old_state = i->state; - if(i->state==BLOCKED) - i->state = MOVING; - - TrackIter next_track = i->track.next(i->path); - if(!next_track) - return false; + if(!next.update_states()) + return true; + if(next.check_deadlocks()) + return false; - for(vector::iterator j=next.trains.begin(); j!=next.trains.end(); ++j) - if(j!=i) - { - if(j->track.track()==next_track.track()) - { - unsigned other_exit = j->track.reverse(j->path).entry(); - if(next_track.entry()==other_exit) - return false; - } - else if(!j->is_occupied(*next_track)) - continue; - - i->state = BLOCKED; - } - - if(i->state!=old_state) - changes = true; - } - - if(changes) - { - list::iterator i; - for(i=steps.begin(); (i!=steps.end() && !(next<*i)); ++i) ; - steps.insert(i, next); - } - - return !changes; -} - -int TrainRoutePlanner::find_next_train(RoutingStep &step) -{ - Time::TimeDelta min_dt; - int next_train = -1; - for(unsigned i=0; i::iterator i; + for(i=steps.begin(); (i!=steps.end() && !(next<*i)); ++i) ; + steps.insert(i, next); - return next_train; + return false; } void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index) @@ -142,7 +97,8 @@ void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index) if(next_entry_ep.has_path(i)) { next.trains[train_index].path = i; - new_steps.push_back(next); + if(next.is_viable()) + new_steps.push_back(next); } if(next_entry_ep.paths!=next_track->get_type().get_paths()) @@ -150,7 +106,8 @@ void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index) RoutingStep wait(&step); wait.advance(dt); wait.trains[train_index].state = WAITING; - new_steps.push_back(wait); + if(wait.is_viable()) + new_steps.push_back(wait); } } @@ -194,9 +151,14 @@ void TrainRoutePlanner::create_routes(RoutingStep &goal) TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t): train(&t), + speed(train->get_maximum_speed()), router(train->get_ai_of_type()), route(0) -{ } +{ + // If no maximum speed is specified, use a sensible default + if(!speed) + speed = 20*train->get_layout().get_catalogue().get_scale(); +} TrainRoutePlanner::OccupiedTrack::OccupiedTrack(Track &t, unsigned p, OccupiedTrack *n): @@ -233,7 +195,8 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf): occupied_tracks(0), state(MOVING), delay(info->router->get_departure_delay()), - waypoint(info->router->get_n_waypoints() ? 0 : -1) + waypoint(info->router->get_n_waypoints() ? 0 : -1), + blocked_by(-1) { const Vehicle *veh = &info->train->get_vehicle(0); // TODO margins @@ -265,7 +228,9 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState offset(other.offset), back_offset(other.back_offset), state(other.state), - waypoint(other.waypoint) + delay(other.delay), + waypoint(other.waypoint), + blocked_by(other.blocked_by) { ++occupied_tracks->refcount; } @@ -278,8 +243,7 @@ TrainRoutePlanner::TrainRoutingState::~TrainRoutingState() Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() const { - // TODO Consider the speed of the train - return (track->get_type().get_path_length(path)-offset)*Time::sec+delay; + return ((track->get_type().get_path_length(path)-offset)/info->speed)*Time::sec+delay; } bool TrainRoutePlanner::TrainRoutingState::is_occupied(Track &trk) const @@ -334,6 +298,80 @@ TrainRoutePlanner::RoutingStep::RoutingStep(RoutingStep *p): prev(p) { } +bool TrainRoutePlanner::RoutingStep::update_states() +{ + bool changes = false; + for(vector::iterator i=trains.begin(); i!=trains.end(); ++i) + { + if(i->state==ARRIVED) + continue; + + TrainState old_state = i->state; + + TrackIter next_track = i->track.next(i->path); + if(next_track) + { + i->blocked_by = get_occupant(*next_track); + if(i->blocked_by>=0) + i->state = BLOCKED; + else if(i->state==BLOCKED) + i->state = MOVING; + } + else + i->state = BLOCKED; + + if(i->state!=old_state) + changes = true; + } + + return changes; +} + +bool TrainRoutePlanner::RoutingStep::check_deadlocks() const +{ + for(unsigned i=0; i(i)) + return true; + } + + return false; +} + +int TrainRoutePlanner::RoutingStep::get_occupant(Track &track) const +{ + for(unsigned i=0; idelay = Time::zero; } else if(i->state==MOVING) - { - float distance = dt/Time::sec; - i->advance(distance); - } + i->advance(i->info->speed*(dt/Time::sec)); } } +bool TrainRoutePlanner::RoutingStep::is_viable() const +{ + for(vector::const_iterator i=trains.begin(); i!=trains.end(); ++i) + if(i->state==MOVING) + return true; + + return false; +} + bool TrainRoutePlanner::RoutingStep::is_goal() const { for(vector::const_iterator i=trains.begin(); i!=trains.end(); ++i)