]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouteplanner.cpp
Utilize maximum speed information in planning routes
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
index 0de707b16d21c167a88b9f153ac4945c719c647a..809bb3e5c665967a9a6d6d890d7eb58f582c6e15 100644 (file)
@@ -1,3 +1,4 @@
+#include "catalogue.h"
 #include "layout.h"
 #include "route.h"
 #include "train.h"
@@ -16,7 +17,7 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout)
        for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
        {
                TrainRoutingInfo info(*i->second);
-               if(info.router && info.router->has_destination())
+               if(info.router && info.router->get_destination())
                        routed_trains.push_back(info);
        }
 
@@ -121,13 +122,20 @@ void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index)
        RoutingStep next(&step);
        next.advance(dt);
        TrainRouter &router = *train.info->router;
-       if(router.is_destination(*train.track) && !router.is_destination(*next_track))
+       if(train.waypoint<0 && router.is_destination(*train.track) && !router.is_destination(*next_track))
        {
                next.trains[train_index].state = ARRIVED;
                new_steps.push_back(next);
        }
        else
        {
+               if(train.waypoint>=0 && router.is_waypoint(train.waypoint, *train.track) && !router.is_waypoint(train.waypoint, *next_track))
+               {
+                       ++next.trains[train_index].waypoint;
+                       if(next.trains[train_index].waypoint>=static_cast<int>(router.get_n_waypoints()))
+                               next.trains[train_index].waypoint = -1;
+               }
+
                next.trains[train_index].advance_track(0);
 
                const TrackType::Endpoint &next_entry_ep = next_track.endpoint();
@@ -157,6 +165,7 @@ void TrainRoutePlanner::create_routes(RoutingStep &goal)
        {
                i->route = new Route(i->train->get_layout());
                i->route->set_name("Router");
+               i->route->set_temporary(true);
        }
 
        for(RoutingStep *i=&goal; i; i=i->prev)
@@ -186,9 +195,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):
@@ -223,37 +237,23 @@ TrainRoutePlanner::OccupiedTrack::~OccupiedTrack()
 TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
        info(&inf),
        occupied_tracks(0),
-       state(MOVING)
+       state(MOVING),
+       delay(info->router->get_departure_delay()),
+       waypoint(info->router->get_n_waypoints() ? 0 : -1)
 {
        const Vehicle *veh = &info->train->get_vehicle(0);
-       track = veh->get_track_iter();
        // TODO margins
-       offset = veh->get_offset()+veh->get_type().get_length()/2;
+       TrackOffsetIter track_and_offs = veh->get_placement().get_position(VehiclePlacement::FRONT_BUFFER);
+       track = track_and_offs.track_iter();
+       offset = track_and_offs.offset();
        path = track->get_active_path();
 
-       float path_length = track->get_type().get_path_length(path);
-       while(offset>path_length)
-       {
-               offset -= path_length;
-               track = track.next();
-               path = track->get_active_path();
-               path_length = track->get_type().get_path_length(path);
-       }
-
        while(Vehicle *next = veh->get_link(1))
                veh = next;
-       back_offset = veh->get_offset()-veh->get_type().get_length()/2;
-
-       TrackIter iter = veh->get_track_iter();
-       while(back_offset<0)
-       {
-               TrackIter prev = iter.flip().reverse();
-               if(!prev)
-                       break;
-               iter = prev;
-               back_offset += iter->get_type().get_path_length(iter->get_active_path());
-       }
+       track_and_offs = veh->get_placement().get_position(VehiclePlacement::BACK_BUFFER);
+       back_offset = track_and_offs.offset();
 
+       TrackIter iter = track_and_offs.track_iter();
        while(1)
        {
                occupied_tracks = new OccupiedTrack(*iter, iter->get_active_path(), occupied_tracks);
@@ -270,7 +270,9 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState
        occupied_tracks(other.occupied_tracks),
        offset(other.offset),
        back_offset(other.back_offset),
-       state(other.state)
+       state(other.state),
+       delay(other.delay),
+       waypoint(other.waypoint)
 {
        ++occupied_tracks->refcount;
 }
@@ -283,8 +285,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;
+       return ((track->get_type().get_path_length(path)-offset)/info->speed)*Time::sec+delay;
 }
 
 bool TrainRoutePlanner::TrainRoutingState::is_occupied(Track &trk) const
@@ -343,11 +344,17 @@ void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt)
 {
        time += dt;
        for(vector<TrainRoutingState>::iterator i=trains.begin(); i!=trains.end(); ++i)
-               if(i->state==MOVING)
+       {
+               if(i->delay)
                {
-                       float distance = dt/Time::sec;
-                       i->advance(distance);
+                       i->delay -= dt;
+                       if(i->delay>Time::zero)
+                               continue;
+                       i->delay = Time::zero;
                }
+               else if(i->state==MOVING)
+                       i->advance(i->info->speed*(dt/Time::sec));
+       }
 }
 
 bool TrainRoutePlanner::RoutingStep::is_goal() const