X-Git-Url: http://git.tdb.fi/?p=r2c2.git;a=blobdiff_plain;f=source%2Flibr2c2%2Ftimetable.cpp;h=5fa36395825d0afdb6dfb92df83e76f97d3cd6cb;hp=42414f63d0baf3ecfe14fcbda9afb4de8c3669e3;hb=d990c03a06a494ce3596862ce61e2a5684dea7e1;hpb=76e277499fec494a96a1120692a1c6b4dc85ba60 diff --git a/source/libr2c2/timetable.cpp b/source/libr2c2/timetable.cpp index 42414f6..5fa3639 100644 --- a/source/libr2c2/timetable.cpp +++ b/source/libr2c2/timetable.cpp @@ -1,3 +1,4 @@ +#include #include #include "aicontrol.h" #include "clock.h" @@ -15,7 +16,8 @@ namespace R2C2 { Timetable::Timetable(Train &t): TrainAI(t), current_row(rows.end()), - update_pending(false) + update_pending(false), + sync_to_clock(true) { if(!train.get_ai_of_type()) new AIControl(train); @@ -114,7 +116,7 @@ void Timetable::save(list &st) const } } -void Timetable::check_update(list::const_iterator i) +void Timetable::check_update(const list::const_iterator &i) { for(list::const_iterator j=current_row; (j!=rows.end() && j!=i); ++j) if(j->type==ARRIVE) @@ -122,28 +124,47 @@ 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;; ) - { - if(i==rows.end()) - { - i = rows.begin(); - depart = rows.end(); - } + const Clock &clock = train.get_layout().get_clock(); - if(i->type==DEPART) - depart = i; - else if(depart!=rows.end() && i->type==ARRIVE) - break; + if(sync_to_clock) + { + sync_to_clock = false; + current_row = rows.begin(); + for(list::iterator i=rows.begin(); i!=rows.end(); ++i) + if(i->type==DEPART && i->time>=clock.get_current_time()) + { + current_row = i; + break; + } + } - ++i; - if(i==current_row) + list::iterator arrive; + list::iterator depart = find_trip(current_row, &arrive); + if(depart==rows.end()) + { + depart = find_trip(rows.begin(), &arrive); + if(depart==rows.end()) { current_row = rows.end(); return; @@ -155,22 +176,30 @@ void Timetable::update_route() current_row = depart; for(list::const_iterator i=depart; i!=rows.end(); ++i) { - if(i->type==ARRIVE) - { - train.ai_message(Message("set-destination", i->target)); - break; - } - else if(i->type==DEPART) + if(i->type==DEPART) { - const Clock &clock = train.get_layout().get_clock(); Time::TimeDelta dt = i->time-clock.get_current_time(); while(dttarget, i->direction))); + if(i->type==ARRIVE) + break; } - else if(i->type==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())); } } @@ -203,7 +232,8 @@ void Timetable::record_time() Timetable::Row::Row(): type(ARRIVE), - target(0) + target(0), + direction(TrackChain::UNSPECIFIED) { } void Timetable::Row::save(list &st) const @@ -211,6 +241,8 @@ void Timetable::Row::save(list &st) const st.push_back((DataFile::Statement("type"), type)); st.push_back((DataFile::Statement("time"), time.raw())); st.push_back(target->save_reference()); + if(direction) + st.push_back((DataFile::Statement("direction"), direction)); } @@ -235,9 +267,11 @@ Timetable::Row::Loader::Loader(Row &r, Layout &l): layout(l) { add("block", &Loader::block); + add("direction", &Row::direction); add("time", &Loader::time); add("type", &Row::type); add("zone", &Loader::zone); + add("zone", &Loader::zone_numbered); } void Timetable::Row::Loader::block(unsigned id) @@ -250,7 +284,12 @@ void Timetable::Row::Loader::time(Time::RawTime t) obj.time = Time::TimeDelta(t); } -void Timetable::Row::Loader::zone(const string &name, unsigned number) +void Timetable::Row::Loader::zone(const string &name) +{ + zone_numbered(name, 0); +} + +void Timetable::Row::Loader::zone_numbered(const string &name, unsigned number) { obj.target = &layout.get_zone(name, number); }