X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftimetable.cpp;h=93f61c50608d0b56839bf6ba59af8ba6b4410c47;hb=e8d2abb48b5236cc3455a035628292ae7908240e;hp=42414f63d0baf3ecfe14fcbda9afb4de8c3669e3;hpb=997e14793d8e58d3cb14a495970b36b1e0cad39b;p=r2c2.git 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)