]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouteplanner.cpp
Filter out non-viable routing steps before adding them to the list
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
index cc69e3c2020bf856cd705200124fe9cff29e1c4a..c5519e45d7db1c581932b7a48e4efe8d5aba8181 100644 (file)
@@ -1,3 +1,4 @@
+#include "catalogue.h"
 #include "layout.h"
 #include "route.h"
 #include "train.h"
@@ -55,6 +56,9 @@ bool TrainRoutePlanner::update_states(RoutingStep &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;
@@ -142,7 +146,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 +155,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 +200,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<TrainRouter>()),
        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):
@@ -279,8 +290,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
@@ -348,13 +358,19 @@ void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt)
                        i->delay = 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<TrainRoutingState>::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<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)