X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftimetable.cpp;h=5cd4b646bc6e198a0a908e35b8d651529d315d36;hb=9a7097fed866060efb8738f8b2f17d5c96a6c980;hp=0d68a7be8aea5c88f515a60192e4aac62d6267ce;hpb=100c7f252e145f095205c55a178ab57bb8da7e75;p=r2c2.git diff --git a/source/libr2c2/timetable.cpp b/source/libr2c2/timetable.cpp index 0d68a7b..5cd4b64 100644 --- a/source/libr2c2/timetable.cpp +++ b/source/libr2c2/timetable.cpp @@ -17,7 +17,9 @@ Timetable::Timetable(Train &t): TrainAI(t), current_row(rows.end()), update_pending(false), - sync_to_clock(true) + sync_to_clock(true), + late_arrival(false), + next_depart(rows.end()) { if(!train.get_ai_of_type()) new AIControl(train); @@ -25,6 +27,7 @@ Timetable::Timetable(Train &t): new TrainRouter(train); train.signal_ai_event.connect(sigc::mem_fun(this, &Timetable::event)); + train.get_layout().get_clock().signal_discontinuity.connect(sigc::mem_fun(this, &Timetable::clock_discontinuity)); } void Timetable::append_row(const Row &r) @@ -89,21 +92,20 @@ void Timetable::tick(const Time::TimeDelta &dt) if(update_pending && !train.get_block_allocator().is_active()) update_route(); - if(current_row->type==DEPART) - { - const Clock &clock = train.get_layout().get_clock(); + if(next_depart!=rows.end() && next_depart!=current_row && passed_row(*next_depart, dt)) + late_arrival = true; +} - Time::TimeDelta t = clock.get_current_time(); - if(ttime) - t += Time::day; +bool Timetable::passed_row(const Row &row, const Time::TimeDelta &dt) const +{ + const Clock &clock = train.get_layout().get_clock(); - Time::TimeDelta b = t-dt*clock.get_rate(); - if(btime) - { - train.ai_message(Message("set-target-speed", train.get_maximum_speed())); - ++current_row; - } - } + Time::TimeDelta t = clock.get_current_time(); + if(t &st) const @@ -176,23 +178,28 @@ void Timetable::update_route() current_row = depart; for(list::const_iterator i=depart; i!=rows.end(); ++i) { - if(i->type==ARRIVE) + if(i->type==DEPART) { - train.ai_message(Message("add-waypoint", i->target)); - break; + Time::TimeDelta dt; + if(late_arrival) + dt = Time::min; + else + { + dt = i->time-clock.get_current_time(); + while(dttype==DEPART) + else { - 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); + next_depart = find_trip(arrive, 0); if(next_depart==rows.end()) next_depart = find_trip(rows.begin(), 0); if(next_depart!=rows.end()) @@ -202,11 +209,18 @@ void Timetable::update_route() dt += Time::day; train.ai_message(Message("set-trip-duration", dt/clock.get_rate())); } + + late_arrival = false; } void Timetable::event(TrainAI &, const Message &msg) { - if(msg.type=="arrived") + if(msg.type=="departed") + { + if(current_row->type==DEPART) + ++current_row; + } + else if(msg.type=="arrived") { if(current_row->type==ARRIVE) record_time(); @@ -230,10 +244,17 @@ void Timetable::record_time() signal_row_modified.emit(i, *current_row); } +void Timetable::clock_discontinuity() +{ + update_pending = true; + sync_to_clock = true; +} + Timetable::Row::Row(): type(ARRIVE), - target(0) + target(0), + direction(TrackChain::UNSPECIFIED) { } void Timetable::Row::save(list &st) const @@ -241,6 +262,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)); } @@ -265,9 +288,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) @@ -280,7 +305,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); }