+#include <algorithm>
#include <msp/strings/format.h>
#include "aicontrol.h"
#include "clock.h"
Timetable::Timetable(Train &t):
TrainAI(t),
current_row(rows.end()),
- update_pending(false)
+ update_pending(false),
+ sync_to_clock(true),
+ late_arrival(false),
+ next_depart(rows.end())
{
if(!train.get_ai_of_type<AIControl>())
new AIControl(train);
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)
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(t<current_row->time)
- 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(b<current_row->time)
- {
- train.ai_message(Message("set-target-speed", train.get_maximum_speed()));
- ++current_row;
- }
- }
+ Time::TimeDelta t = clock.get_current_time();
+ if(t<row.time)
+ t += Time::day;
+
+ Time::TimeDelta b = t-dt*clock.get_rate();
+ return b<row.time;
}
void Timetable::save(list<DataFile::Statement> &st) const
}
}
-void Timetable::check_update(list<Row>::const_iterator i)
+void Timetable::check_update(const list<Row>::const_iterator &i)
{
for(list<Row>::const_iterator j=current_row; (j!=rows.end() && j!=i); ++j)
if(j->type==ARRIVE)
update_pending = true;
}
+list<Timetable::Row>::iterator Timetable::find_trip(const list<Row>::iterator &begin, list<Row>::iterator *arrive)
+{
+ list<Row>::iterator i = find_if(begin, rows.end(), RowTypeMatch(DEPART));
+ if(i==rows.end())
+ return i;
+
+ list<Row>::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<Row>::iterator depart = rows.end();
- for(list<Row>::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<Row>::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<Row>::iterator arrive;
+ list<Row>::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;
current_row = depart;
for(list<Row>::const_iterator i=depart; i!=rows.end(); ++i)
{
- if(i->type==ARRIVE)
+ if(i->type==DEPART)
{
- train.ai_message(Message("set-destination", i->target));
- break;
+ Time::TimeDelta dt;
+ if(late_arrival)
+ dt = Time::min;
+ else
+ {
+ dt = i->time-clock.get_current_time();
+ while(dt<Time::zero)
+ dt += Time::day;
+ }
+ train.ai_message(Message("set-departure-delay", dt/clock.get_rate()));
}
- else if(i->type==DEPART)
+ else
{
- const Clock &clock = train.get_layout().get_clock();
- Time::TimeDelta dt = i->time-clock.get_current_time();
- while(dt<Time::zero)
- dt += Time::day;
- dt /= clock.get_rate();
- train.ai_message(Message("set-departure-delay", dt));
+ train.ai_message(Message("add-waypoint", TrainRouter::Waypoint(*i->target, i->direction)));
+ if(i->type==ARRIVE)
+ break;
}
- else if(i->type==THROUGH)
- train.ai_message(Message("add-waypoint", i->target));
}
+
+ 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()));
+ }
+
+ 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();
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<DataFile::Statement> &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));
}
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)
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);
}