+#include <algorithm>
#include <msp/strings/format.h>
#include "aicontrol.h"
#include "clock.h"
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;; )
+ list<Row>::iterator arrive;
+ list<Row>::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;
train.ai_message(Message("clear-route"));
+ const Clock &clock = train.get_layout().get_clock();
+
current_row = depart;
for(list<Row>::const_iterator i=depart; i!=rows.end(); ++i)
{
}
else if(i->type==DEPART)
{
- const Clock &clock = train.get_layout().get_clock();
Time::TimeDelta dt = i->time-clock.get_current_time();
while(dt<Time::zero)
dt += Time::day;
else if(i->type==THROUGH)
train.ai_message(Message("add-waypoint", i->target));
}
+
+ list<Row>::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)
void save(std::list<Msp::DataFile::Statement> &) const;
};
+ struct RowTypeMatch
+ {
+ RowType type;
+
+ RowTypeMatch(RowType t): type(t) { }
+
+ bool operator()(const Row &r) const { return r.type==type; }
+ };
+
sigc::signal<void, unsigned, const Row &> signal_row_added;
sigc::signal<void, unsigned, const Row &> signal_row_modified;
sigc::signal<void, unsigned> signal_row_removed;
private:
void check_update(std::list<Row>::const_iterator);
+ std::list<Row>::iterator find_trip(const std::list<Row>::iterator &, std::list<Row>::iterator *);
void update_route();
void event(TrainAI &, const Message &);
void record_time();
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)
{
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),
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)
delay = Time::zero;
}
+ if(duration)
+ duration = max(duration-secs*Time::sec, Time::zero);
+
if(state==MOVING)
advance(info->speed*secs);
else if(state!=ARRIVED)
float back_offset;
TrainState state;
Msp::Time::TimeDelta delay;
+ Msp::Time::TimeDelta duration;
int waypoint;
float distance_traveled;
float remaining_estimate;
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")
}
else if(msg.type=="set-departure-delay")
set_departure_delay(msg.value.value<Time::TimeDelta>());
+ else if(msg.type=="set-trip-duration")
+ set_trip_duration(msg.value.value<Time::TimeDelta>());
}
void TrainRouter::tick(const Time::TimeDelta &dt)
if(delay)
{
delay -= dt;
- if(delay<=Time::zero)
+ if(delay<Time::zero)
+ {
+ duration = max(duration+delay, Time::zero);
delay = Time::zero;
+ }
}
+ else if(duration)
+ duration = max(duration-dt, Time::zero);
if(destination_changed && !planner)
start_planning(train.get_layout());
unsigned current_sequence;
bool sequence_check_pending;
Msp::Time::TimeDelta delay;
+ Msp::Time::TimeDelta duration;
Msp::RefPtr<TrainRoutePlanner> planner;
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 &);