From: Mikko Rasa Date: Wed, 19 Mar 2014 20:18:37 +0000 (+0200) Subject: Implement a departure delay for TrainRouter X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=a7e320b46a7d4070613b3210c28e65f579750f37;p=r2c2.git Implement a departure delay for TrainRouter --- diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index c545589..99731c6 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -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::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 diff --git a/source/libr2c2/trainrouteplanner.h b/source/libr2c2/trainrouteplanner.h index 726ebe4..19fb329 100644 --- a/source/libr2c2/trainrouteplanner.h +++ b/source/libr2c2/trainrouteplanner.h @@ -59,6 +59,7 @@ private: float offset; float back_offset; TrainState state; + Msp::Time::TimeDelta delay; TrainRoutingState(TrainRoutingInfo &); TrainRoutingState(const TrainRoutingState &); diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index b5a5bef..462af38 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -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()); } + else if(msg.type=="set-departure-delay") + set_departure_delay(msg.value.value()); } -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()); diff --git a/source/libr2c2/trainrouter.h b/source/libr2c2/trainrouter.h index b36a013..28862a2 100644 --- a/source/libr2c2/trainrouter.h +++ b/source/libr2c2/trainrouter.h @@ -44,6 +44,7 @@ private: const Zone *dest_zone; const Block *dest_block; std::list 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 &);