#include "trackiter.h"
#include "train.h"
#include "trackchain.h"
+#include "trainroutemetric.h"
#include "trainrouteplanner.h"
#include "trainrouter.h"
TrainRouter::TrainRouter(Train &t):
TrainAI(t),
priority(0),
- arriving(0),
+ arrival(ON_THE_WAY),
destination(0),
- update_pending(false)
+ destination_changed(false),
+ metrics_stale(false),
+ current_sequence(0),
+ sequence_check_pending(false)
{
train.get_layout().signal_block_reserved.connect(sigc::mem_fun(this, &TrainRouter::block_reserved));
train.signal_advanced.connect(sigc::mem_fun(this, &TrainRouter::train_advanced));
+ train.signal_rear_advanced.connect(sigc::mem_fun(this, &TrainRouter::train_rear_advanced));
+}
+
+TrainRouter::~TrainRouter()
+{
+ for(vector<TrainRouteMetric *>::iterator i=metrics.begin(); i!=metrics.end(); ++i)
+ delete *i;
}
void TrainRouter::set_priority(int p)
routes.push_back(lead);
if(r)
routes.push_back(r);
- train.stop_at(0);
- arriving = 0;
- /* TODO destination should also be cleared when manually setting a different
- route, but not when the planner calls this. */
- if(!r)
- {
- destination = 0;
- }
+ destination = 0;
+ waypoints.clear();
+ sequence_points.clear();
+ current_sequence = 0;
+ sequence_check_pending = false;
- train.refresh_blocks_from(*fncb);
-
- const Route *route = get_route();
- signal_route_changed.emit(route);
- signal_event.emit(Message("route-changed", route));
+ route_changed();
return true;
}
-void TrainRouter::add_wait(Block &block, Train *tr)
-{
- Wait wait;
- wait.block = █
- wait.train = tr;
- waits.push_back(wait);
-}
-
const Route *TrainRouter::get_route() const
{
if(routes.empty())
return routes.front();
}
+void TrainRouter::route_changed()
+{
+ BlockIter fncb = train.get_first_noncritical_block();
+
+ reserving_route = routes.begin();
+ bool already_at_end = false;
+ if(!routes.empty())
+ {
+ TrackIter track = train.get_block_allocator().first().track_iter();
+ for(; track; track=track.next())
+ {
+ if(!advance_to_track(reserving_route, *track))
+ {
+ already_at_end = true;
+ break;
+ }
+ if(&track->get_block()==fncb.block())
+ break;
+ }
+ }
+
+ if(!already_at_end)
+ {
+ arrival = ON_THE_WAY;
+ train.stop_at(0);
+ train.refresh_blocks_from(*fncb);
+ }
+
+ const Route *route = get_route();
+ signal_route_changed.emit(route);
+ signal_event.emit(Message("route-changed", route));
+}
+
void TrainRouter::set_destination(const TrackChain &d)
{
destination = &d;
- update_pending = true;
+ destination_changed = true;
+ metrics_stale = true;
}
bool TrainRouter::is_destination(Track &track) const
return false;
}
+void TrainRouter::add_waypoint(const TrackChain &wp)
+{
+ waypoints.push_back(&wp);
+ destination_changed = true;
+ metrics_stale = true;
+}
+
+bool TrainRouter::is_waypoint(unsigned index, Track &track) const
+{
+ if(index>=waypoints.size())
+ throw out_of_range("TrainRouter::is_waypoint");
+
+ return waypoints[index]->has_track(track);
+}
+
+const TrainRouteMetric &TrainRouter::get_metric(int index) const
+{
+ if(!destination)
+ throw logic_error("no metrics");
+ else if(metrics_stale)
+ throw logic_error("metrics are stale");
+
+ if(index<0)
+ return *metrics.front();
+ else if(static_cast<unsigned>(index)>=waypoints.size())
+ throw out_of_range("TrainRouter::get_metric");
+ else
+ return *metrics[index+1];
+}
+
void TrainRouter::set_departure_delay(const Time::TimeDelta &d)
{
delay = d;
- update_pending = true;
+ destination_changed = true;
}
void TrainRouter::message(const Message &msg)
else
set_destination(*msg.value.value<const TrackChain *>());
}
+ else if(msg.type=="add-waypoint")
+ {
+ if(msg.value.check_type<TrackChain *>())
+ add_waypoint(*msg.value.value<TrackChain *>());
+ else
+ add_waypoint(*msg.value.value<const TrackChain *>());
+ }
else if(msg.type=="set-departure-delay")
set_departure_delay(msg.value.value<Time::TimeDelta>());
}
delay = Time::zero;
}
- if(update_pending)
- create_plans(train.get_layout());
+ if(destination_changed)
+ {
+ if(!planner)
+ start_planning(train.get_layout());
+ else if(planner->get_result()!=TrainRoutePlanner::PENDING)
+ {
+ destination_changed = false;
+ if(planner->get_result()==TrainRoutePlanner::COMPLETE)
+ {
+ const list<Route *> &planned_routes = planner->get_routes_for(train);
+ routes.clear();
+ routes.push_back(create_lead_route(0, planned_routes.front()));
+ routes.insert(routes.end(), planned_routes.begin(), planned_routes.end());
+ sequence_points = planner->get_sequence_for(train);
+ current_sequence = 0;
+ sequence_check_pending = false;
+
+ route_changed();
+ }
+ planner = 0;
+ }
+ }
- if(arriving==1 && !train.get_speed())
+ if(sequence_check_pending)
{
- signal_arrived.emit();
- signal_event.emit(Message("arrived"));
- arriving = 2;
+ if(sequence_points.front().is_cleared())
+ train.stop_at(0);
+ sequence_check_pending = false;
}
- else if(arriving==2 && !train.get_block_allocator().is_active())
+
+ if(arrival==RESERVED_TO_END && !train.get_speed())
+ {
+ signal_arrived.emit(destination);
+ signal_event.emit(Message("arrived", destination));
+ arrival = ARRIVED;
+ }
+ else if(arrival==ARRIVED && !train.get_block_allocator().is_active())
set_route(0);
}
void TrainRouter::block_reserved(Block &block, Train *t)
{
+ if(routes.empty())
+ return;
+
if(t!=&train)
{
- if(!waits.empty() && waits.front().block==&block)
+ if(!t)
+ return;
+
+ SequencePoint &sp = sequence_points.front();
+ if(sp.preceding_train==t && sp.block==&block)
{
- train.stop_at(0);
- waits.pop_front();
+ if(sp.is_cleared())
+ train.stop_at(0);
+ else
+ sequence_check_pending = true;
}
+
return;
}
- BlockIter b_iter = t->get_block_allocator().iter_for(block);
-
- RouteList::iterator route = routes.begin();
- if(advance_route(route, block))
+ if(!sequence_points.empty())
{
- // Check if the block is a turnout and set it to proper path
- if(unsigned taddr = block.get_turnout_address())
+ SequencePoint &sp = sequence_points.front();
+ if(sp.block==&block)
{
- int path = (*route)->get_turnout(taddr);
- if(path>=0)
- b_iter.track_iter()->set_active_path(path);
+ current_sequence = sp.sequence_out;
+ sequence_points.pop_front();
}
+ }
- // Check if the next block is still part of the designated route
- BlockIter b_iter_next = b_iter.next(*route);
+ TrackIter track = train.get_block_allocator().iter_for(block).track_iter();
+
+ // Is the block a turnout? If so, set it to the proper path.
+ if(unsigned taddr = block.get_turnout_address())
+ {
+ int path = (*reserving_route)->get_turnout(taddr);
+ if(path>=0)
+ track->set_active_path(path);
+ }
- RouteList::iterator next_route = route;
- if(!advance_route(next_route, *b_iter_next))
+ if(reserving_route==routes.end() || !(*reserving_route)->has_track(*track))
+ {
+ reserving_route = routes.begin();
+ arrival = ON_THE_WAY;
+ track = t->get_block_allocator().first().track_iter();
+ for(; track; track=track.next())
+ {
+ if(!advance_to_track(reserving_route, *track))
+ throw logic_error("internal error (reservation outside of route)");
+ else if(&track->get_block()==&block)
+ break;
+ }
+ }
+
+ // Do we need to move to the next route?
+ for(; track; track=track.next((*reserving_route)->get_path(*track)))
+ {
+ if(!advance_to_track(reserving_route, *track))
{
+ arrival = RESERVED_TO_END;
train.stop_at(&block);
return;
}
+ if(&track->get_block()!=&block)
+ break;
+ }
- if(!waits.empty() && waits.front().block==b_iter_next.block())
+ if(!sequence_points.empty())
+ {
+ SequencePoint &sp = sequence_points.front();
+ if(sp.block==&track->get_block() && !sp.is_cleared())
train.stop_at(&block);
}
}
{
BlockIter b_iter = train.get_block_allocator().iter_for(block);
- // Check if we've reached the next route
- if(routes.size()>1)
+ if(!waypoints.empty())
{
- const Route &route = **++routes.begin();
- if(route.has_track(*b_iter.endpoint().track))
+ const TrackChain &wp = *waypoints.front();
+ TrackIter t_iter = b_iter.track_iter();
+ if(wp.has_track(*t_iter))
{
- routes.pop_front();
- // XXX Exceptions?
- signal_event.emit(Message("route-changed", get_route()));
+ for(; t_iter; t_iter=t_iter.next())
+ {
+ if(!wp.has_track(*t_iter))
+ {
+ waypoints.erase(waypoints.begin());
+ signal_waypoint_reached.emit(&wp);
+ signal_event.emit(Message("waypoint-reached", &wp));
+ break;
+ }
+ else if(!block.has_track(*t_iter))
+ break;
+ }
}
}
+}
- if(!routes.empty())
- {
- b_iter = b_iter.next();
- if(b_iter && !is_on_route(*b_iter))
- arriving = 1;
- }
+void TrainRouter::train_rear_advanced(Block &block)
+{
+ Track &track = *train.get_block_allocator().iter_for(block).endpoint().track;
+
+ // Have we left some routes completely behind?
+ for(RouteList::iterator i=routes.begin(); i!=routes.end(); ++i)
+ if((*i)->has_track(track))
+ {
+ if(i!=routes.begin())
+ {
+ routes.erase(routes.begin(), i);
+ const Route *route = get_route();
+ signal_route_changed.emit(route);
+ signal_event.emit(Message("route-changed", route));
+ }
+ break;
+ }
}
-const Route *TrainRouter::get_route_for_block(const Block &block) const
+void TrainRouter::create_metrics()
{
- const set<Track *> &tracks = block.get_tracks();
- for(RouteList::const_iterator i=routes.begin(); i!=routes.end(); ++i)
- for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
- if((*i)->has_track(**j))
- return *i;
+ for(vector<TrainRouteMetric *>::iterator i=metrics.begin(); i!=metrics.end(); ++i)
+ delete *i;
+ metrics.clear();
+
+ if(!destination)
+ return;
+
+ metrics.push_back(new TrainRouteMetric(*destination));
+ for(vector<const TrackChain *>::const_iterator i=waypoints.begin(); i!=waypoints.end(); ++i)
+ metrics.push_back(new TrainRouteMetric(**i));
- return 0;
+ for(unsigned i=metrics.size(); --i>0; )
+ metrics[i]->chain_to(*metrics[(i+1)%metrics.size()]);
}
Route *TrainRouter::create_lead_route(Route *lead, const Route *target)
lead->set_temporary(true);
}
- set<Track *> tracks;
- for(BlockIter i=train.get_block_allocator().first(); (i && i->get_train()==&train); i=i.next())
+ bool target_reached = false;
+ for(TrackIter i=train.get_block_allocator().first().track_iter(); i; i=i.next())
{
- const set<Track *> &btracks = i->get_tracks();
- for(set<Track *>::const_iterator j=btracks.begin(); j!=btracks.end(); ++j)
- if(!target || !target->has_track(**j))
- tracks.insert(*j);
+ if(i->get_block().get_train()!=&train)
+ break;
+ if(target)
+ {
+ if(target->has_track(*i))
+ target_reached = true;
+ else if(target_reached)
+ break;
+ }
+ lead->add_track(*i);
}
- lead->add_tracks(tracks);
-
return lead;
}
-bool TrainRouter::advance_route(RouteList::iterator &iter, const Block &block)
+bool TrainRouter::is_valid_for_track(const Route &route, Track &track) const
{
- const set<Track *> &tracks = block.get_tracks();
- for(; iter!=routes.end(); ++iter)
- for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
- if((*iter)->has_track(**j))
- return true;
-
- return false;
+ if(!route.has_track(track))
+ return false;
+ if(track.get_type().is_turnout() && route.get_turnout(track.get_turnout_address())<0)
+ return false;
+ return true;
}
-bool TrainRouter::is_on_route(const Block &block)
+bool TrainRouter::advance_to_track(RouteList::iterator &route, Track &track)
{
- RouteList::iterator iter = routes.begin();
- return advance_route(iter, block);
+ if(!is_valid_for_track(**route, track))
+ {
+ ++route;
+ if(route==routes.end())
+ return false;
+ if(!is_valid_for_track(**route, track))
+ throw logic_error("internal error (routes are not continuous)");
+ }
+
+ return true;
}
-void TrainRouter::create_plans(Layout &layout)
+void TrainRouter::start_planning(Layout &layout)
{
- TrainRoutePlanner planner(layout);
- planner.plan();
+ RefPtr<TrainRoutePlanner> planner = new TrainRoutePlanner(layout);
const map<unsigned, Train *> &trains = layout.get_trains();
for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
if(TrainRouter *router = i->second->get_ai_of_type<TrainRouter>())
- router->update_pending = false;
+ {
+ if(router->metrics_stale)
+ {
+ router->create_metrics();
+ router->metrics_stale = false;
+ }
+ router->planner = planner;
+ }
+
+ planner->plan();
}
-TrainRouter::Wait::Wait():
- block(0),
- train(0)
+TrainRouter::SequencePoint::SequencePoint(Block &b, unsigned o):
+ block(&b),
+ preceding_train(0),
+ sequence_in(0),
+ sequence_out(o)
{ }
+bool TrainRouter::SequencePoint::is_cleared() const
+{
+ if(!preceding_train)
+ return true;
+
+ TrainRouter *router = preceding_train->get_ai_of_type<TrainRouter>();
+ return router->get_current_sequence()>=sequence_in;
+}
+
TrainRouter::Loader::Loader(TrainRouter &r):
DataFile::ObjectLoader<TrainRouter>(r)