]> git.tdb.fi Git - r2c2.git/commitdiff
Implement a departure delay for TrainRouter
authorMikko Rasa <tdb@tdb.fi>
Wed, 19 Mar 2014 20:18:37 +0000 (22:18 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 19 Mar 2014 20:21:07 +0000 (22:21 +0200)
source/libr2c2/trainrouteplanner.cpp
source/libr2c2/trainrouteplanner.h
source/libr2c2/trainrouter.cpp
source/libr2c2/trainrouter.h

index c5455899e6befc6f9f7e7c6830e7234d1357e5f5..99731c6f45c2068684144dd1e8352f8be7f92b22 100644 (file)
@@ -224,7 +224,8 @@ TrainRoutePlanner::OccupiedTrack::~OccupiedTrack()
 TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
        info(&inf),
        occupied_tracks(0),
-       state(MOVING)
+       state(MOVING),
+       delay(info->router->get_departure_delay())
 {
        const Vehicle *veh = &info->train->get_vehicle(0);
        // TODO margins
@@ -269,7 +270,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)*Time::sec+delay;
 }
 
 bool TrainRoutePlanner::TrainRoutingState::is_occupied(Track &trk) const
@@ -328,11 +329,20 @@ 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)
+               {
+                       i->delay -= dt;
+                       if(i->delay>Time::zero)
+                               continue;
+                       i->delay = Time::zero;
+               }
+               else if(i->state==MOVING)
                {
                        float distance = dt/Time::sec;
                        i->advance(distance);
                }
+       }
 }
 
 bool TrainRoutePlanner::RoutingStep::is_goal() const
index 726ebe4229e5fb9d7a3b931bff9e1d8602e5c9f3..19fb329bade0a2745e30a6c995244035ad77ac96 100644 (file)
@@ -59,6 +59,7 @@ private:
                float offset;
                float back_offset;
                TrainState state;
+               Msp::Time::TimeDelta delay;
 
                TrainRoutingState(TrainRoutingInfo &);
                TrainRoutingState(const TrainRoutingState &);
index b5a5bef11f674f3301a36c1304558bc63b2bc8fa..462af3853d5f04200eef5a0ca657e2006daad28e 100644 (file)
@@ -110,6 +110,12 @@ bool TrainRouter::is_destination(Track &track) const
                return false;
 }
 
+void TrainRouter::set_departure_delay(const Time::TimeDelta &d)
+{
+       delay = d;
+       update_pending = true;
+}
+
 void TrainRouter::message(const Message &msg)
 {
        if(msg.type=="set-route")
@@ -135,10 +141,19 @@ void TrainRouter::message(const Message &msg)
                else
                        set_destination(*msg.value.value<const Zone *>());
        }
+       else if(msg.type=="set-departure-delay")
+               set_departure_delay(msg.value.value<Time::TimeDelta>());
 }
 
-void TrainRouter::tick(const Time::TimeDelta &)
+void TrainRouter::tick(const Time::TimeDelta &dt)
 {
+       if(delay)
+       {
+               delay -= dt;
+               if(delay<=Time::zero)
+                       delay = Time::zero;
+       }
+
        if(update_pending)
                create_plans(train.get_layout());
 
index b36a0135dfcde0850e6a5036cdea30d915ce00b4..28862a221995577d8cb204ca1c546d766fbf8c6b 100644 (file)
@@ -44,6 +44,7 @@ private:
        const Zone *dest_zone;
        const Block *dest_block;
        std::list<Wait> waits;
+       Msp::Time::TimeDelta delay;
 
        bool update_pending;
 
@@ -61,6 +62,8 @@ public:
        void set_destination(const Block &);
        bool has_destination() const;
        bool is_destination(Track &) const;
+       void set_departure_delay(const Msp::Time::TimeDelta &);
+       const Msp::Time::TimeDelta &get_departure_delay() const { return delay; }
 
        virtual void message(const Message &);
        virtual void tick(const Msp::Time::TimeDelta &);