From e8d2abb48b5236cc3455a035628292ae7908240e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 4 Feb 2015 01:25:32 +0200 Subject: [PATCH] Unoccupy destination during planning when the train has departed again --- source/libr2c2/timetable.cpp | 50 +++++++++++++++++++--------- source/libr2c2/timetable.h | 10 ++++++ source/libr2c2/trainrouteplanner.cpp | 8 +++++ source/libr2c2/trainrouteplanner.h | 1 + source/libr2c2/trainrouter.cpp | 14 +++++++- source/libr2c2/trainrouter.h | 3 ++ 6 files changed, 69 insertions(+), 17 deletions(-) diff --git a/source/libr2c2/timetable.cpp b/source/libr2c2/timetable.cpp index 42414f6..93f61c5 100644 --- a/source/libr2c2/timetable.cpp +++ b/source/libr2c2/timetable.cpp @@ -1,3 +1,4 @@ +#include #include #include "aicontrol.h" #include "clock.h" @@ -122,28 +123,33 @@ void Timetable::check_update(list::const_iterator i) update_pending = true; } +list::iterator Timetable::find_trip(const list::iterator &begin, list::iterator *arrive) +{ + list::iterator i = find_if(begin, rows.end(), RowTypeMatch(DEPART)); + if(i==rows.end()) + return i; + + list::iterator j = find_if(i, rows.end(), RowTypeMatch(ARRIVE)); + if(j==rows.end()) + return j; + + if(arrive) + *arrive = j; + return i; +} + void Timetable::update_route() { update_pending = false; if(rows.empty()) return; - list::iterator depart = rows.end(); - for(list::iterator i=current_row;; ) + list::iterator arrive; + list::iterator depart = find_trip(current_row, &arrive); + if(depart==rows.end()) { - if(i==rows.end()) - { - i = rows.begin(); - depart = rows.end(); - } - - if(i->type==DEPART) - depart = i; - else if(depart!=rows.end() && i->type==ARRIVE) - break; - - ++i; - if(i==current_row) + depart = find_trip(rows.begin(), &arrive); + if(depart==rows.end()) { current_row = rows.end(); return; @@ -152,6 +158,8 @@ void Timetable::update_route() train.ai_message(Message("clear-route")); + const Clock &clock = train.get_layout().get_clock(); + current_row = depart; for(list::const_iterator i=depart; i!=rows.end(); ++i) { @@ -162,7 +170,6 @@ void Timetable::update_route() } else if(i->type==DEPART) { - const Clock &clock = train.get_layout().get_clock(); Time::TimeDelta dt = i->time-clock.get_current_time(); while(dttype==THROUGH) train.ai_message(Message("add-waypoint", i->target)); } + + list::iterator next_depart = find_trip(arrive, 0); + if(next_depart==rows.end()) + next_depart = find_trip(rows.begin(), 0); + if(next_depart!=rows.end()) + { + Time::TimeDelta dt = next_depart->time-depart->time; + while(dt<=Time::zero) + dt += Time::day; + train.ai_message(Message("set-trip-duration", dt/clock.get_rate())); + } } void Timetable::event(TrainAI &, const Message &msg) diff --git a/source/libr2c2/timetable.h b/source/libr2c2/timetable.h index da16d3c..f3d6239 100644 --- a/source/libr2c2/timetable.h +++ b/source/libr2c2/timetable.h @@ -56,6 +56,15 @@ public: void save(std::list &) const; }; + struct RowTypeMatch + { + RowType type; + + RowTypeMatch(RowType t): type(t) { } + + bool operator()(const Row &r) const { return r.type==type; } + }; + sigc::signal signal_row_added; sigc::signal signal_row_modified; sigc::signal signal_row_removed; @@ -82,6 +91,7 @@ public: private: void check_update(std::list::const_iterator); + std::list::iterator find_trip(const std::list::iterator &, std::list::iterator *); void update_route(); void event(TrainAI &, const Message &); void record_time(); diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index 30d9f5e..9970069 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -248,6 +248,7 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf): occupied_tracks(0), state(MOVING), delay(info->router->get_departure_delay()), + duration(info->router->get_trip_duration()), waypoint(info->router->get_n_waypoints() ? 0 : -1), blocked_by(-1) { @@ -284,6 +285,7 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState back_offset(other.back_offset), state(other.state), delay(other.delay), + duration(other.duration), waypoint(other.waypoint), distance_traveled(other.distance_traveled), remaining_estimate(other.remaining_estimate), @@ -306,6 +308,9 @@ Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() c bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const { + if(state==ARRIVED && !duration && info->router->get_trip_duration()) + return false; + OccupiedTrack *occ = occupied_tracks; for(unsigned n=occ->n_tracks; n>0; --n, occ=occ->next) if(occ->track==&trk) @@ -373,6 +378,9 @@ void TrainRoutePlanner::TrainRoutingState::advance(const Time::TimeDelta &dt) delay = Time::zero; } + if(duration) + duration = max(duration-secs*Time::sec, Time::zero); + if(state==MOVING) advance(info->speed*secs); else if(state!=ARRIVED) diff --git a/source/libr2c2/trainrouteplanner.h b/source/libr2c2/trainrouteplanner.h index 153eda0..c4850d4 100644 --- a/source/libr2c2/trainrouteplanner.h +++ b/source/libr2c2/trainrouteplanner.h @@ -71,6 +71,7 @@ private: float back_offset; TrainState state; Msp::Time::TimeDelta delay; + Msp::Time::TimeDelta duration; int waypoint; float distance_traveled; float remaining_estimate; diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index bbb87ea..9245774 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -181,6 +181,11 @@ void TrainRouter::set_departure_delay(const Time::TimeDelta &d) destination_changed = true; } +void TrainRouter::set_trip_duration(const Time::TimeDelta &d) +{ + duration = d; +} + void TrainRouter::message(const Message &msg) { if(msg.type=="set-route") @@ -208,6 +213,8 @@ void TrainRouter::message(const Message &msg) } else if(msg.type=="set-departure-delay") set_departure_delay(msg.value.value()); + else if(msg.type=="set-trip-duration") + set_trip_duration(msg.value.value()); } void TrainRouter::tick(const Time::TimeDelta &dt) @@ -215,9 +222,14 @@ void TrainRouter::tick(const Time::TimeDelta &dt) if(delay) { delay -= dt; - if(delay<=Time::zero) + if(delay planner; public: @@ -91,6 +92,8 @@ public: const TrainRouteMetric &get_metric(int = -1) const; void set_departure_delay(const Msp::Time::TimeDelta &); const Msp::Time::TimeDelta &get_departure_delay() const { return delay; } + void set_trip_duration(const Msp::Time::TimeDelta &); + const Msp::Time::TimeDelta &get_trip_duration() const { return duration; } virtual void message(const Message &); virtual void tick(const Msp::Time::TimeDelta &); -- 2.45.2