]> git.tdb.fi Git - r2c2.git/commitdiff
Start refactoring TrainRoutePlanner
authorMikko Rasa <tdb@tdb.fi>
Sun, 30 Mar 2014 12:35:34 +0000 (15:35 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 30 Mar 2014 13:37:07 +0000 (16:37 +0300)
OOP calls for the logic to be close to the data it uses, so let's put it
there.

source/libr2c2/trainrouteplanner.cpp
source/libr2c2/trainrouteplanner.h

index c5519e45d7db1c581932b7a48e4efe8d5aba8181..f517afd0fd142785ac83cbfa8240cda2507f2de7 100644 (file)
@@ -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<TrainRoutingState>::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<TrainRoutingState>::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<RoutingStep>::iterator i;
-               for(i=steps.begin(); (i!=steps.end() && !(next<*i)); ++i) ;
-               steps.insert(i, next);
-       }
-
-       return !changes;
-}
+       list<RoutingStep>::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; i<step.trains.size(); ++i)
-               if(step.trains[i].state==MOVING)
-               {
-                       Time::TimeDelta dt = step.trains[i].get_time_to_next_track();
-                       if(dt<min_dt || next_train<0)
-                       {
-                               min_dt = dt;
-                               next_train = i;
-                       }
-               }
-
-       return next_train;
+       return false;
 }
 
 void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index)
@@ -244,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
@@ -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<TrainRoutingState>::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<trains.size(); ++i)
+       {
+               const TrainRoutingState &train = trains[i];
+               if(train.state!=BLOCKED)
+                       continue;
+
+               if(train.blocked_by<0)
+                       return true;
+
+               if(trains[train.blocked_by].blocked_by==static_cast<int>(i))
+                       return true;
+       }
+
+       return false;
+}
+
+int TrainRoutePlanner::RoutingStep::get_occupant(Track &track) const
+{
+       for(unsigned i=0; i<trains.size(); ++i)
+               if(trains[i].is_occupied(track))
+                       return i;
+
+       return -1;
+}
+
+int TrainRoutePlanner::RoutingStep::find_next_train() const
+{
+       Time::TimeDelta min_dt;
+       int next_train = -1;
+       for(unsigned i=0; i<trains.size(); ++i)
+               if(trains[i].state==MOVING)
+               {
+                       Time::TimeDelta dt = trains[i].get_time_to_next_track();
+                       if(dt<min_dt || next_train<0)
+                       {
+                               min_dt = dt;
+                               next_train = i;
+                       }
+               }
+
+       return next_train;
+}
+
 void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt)
 {
        time += dt;
index b241150413905f718ca1eb6255603430fc8c4f64..f50669cc2c53561c5ab29e0aebe52aef89154584 100644 (file)
@@ -62,6 +62,7 @@ private:
                TrainState state;
                Msp::Time::TimeDelta delay;
                int waypoint;
+               int blocked_by;
 
                TrainRoutingState(TrainRoutingInfo &);
                TrainRoutingState(const TrainRoutingState &);
@@ -82,6 +83,10 @@ private:
                RoutingStep();
                RoutingStep(RoutingStep *);
 
+               bool update_states();
+               bool check_deadlocks() const;
+               int get_occupant(Track &) const;
+               int find_next_train() const;
                void advance(const Msp::Time::TimeDelta &);
                bool is_viable() const;
                bool is_goal() const;
@@ -98,7 +103,6 @@ public:
        void plan();
 private:
        bool update_states(RoutingStep &);
-       int find_next_train(RoutingStep &);
        void add_steps(RoutingStep &, unsigned);
        void add_waiting_step(RoutingStep &, unsigned);
        void add_steps(RoutingStep &, TrainRoutingState &train);