From 8c31771a3252f3717ce53fd33567c74003c7cd1a Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 30 Mar 2014 15:35:34 +0300 Subject: [PATCH] Start refactoring TrainRoutePlanner OOP calls for the logic to be close to the data it uses, so let's put it there. --- source/libr2c2/trainrouteplanner.cpp | 147 ++++++++++++++++----------- source/libr2c2/trainrouteplanner.h | 6 +- 2 files changed, 92 insertions(+), 61 deletions(-) diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index c5519e4..f517afd 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -40,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); } @@ -53,65 +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) - { - if(i->state==ARRIVED) - continue; - - 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; - - 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; + if(!next.update_states()) + return true; + if(next.check_deadlocks()) + return false; - 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; -} + list::iterator i; + for(i=steps.begin(); (i!=steps.end() && !(next<*i)); ++i) ; + steps.insert(i, next); -int TrainRoutePlanner::find_next_train(RoutingStep &step) -{ - Time::TimeDelta min_dt; - int next_train = -1; - for(unsigned i=0; irouter->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 @@ -277,7 +229,8 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState back_offset(other.back_offset), state(other.state), delay(other.delay), - waypoint(other.waypoint) + waypoint(other.waypoint), + blocked_by(other.blocked_by) { ++occupied_tracks->refcount; } @@ -345,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; i