using namespace Marklin;
+typedef std::pair<const Track *, unsigned> Key;
+
struct Node
{
const Track *track;
template<typename Pred>
list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
{
- // XXX Fails to find some routes - should use track+ep as key
- map<const Track *, Node> track_nodes;
+ map<Key, Node> track_nodes;
priority_queue<Node> nodes;
Node *final = 0;
Node lowest = nodes.top();
nodes.pop();
- if(track_nodes.count(lowest.track))
+ Key key(lowest.track, lowest.ep);
+ if(track_nodes.count(key))
continue;
- Node &ref = track_nodes[lowest.track] = lowest;
+ Node &ref = track_nodes[key] = lowest;
if(goal(*lowest.track))
{
final = &ref;
const vector<Track *> &links = lowest.track->get_links();
for(unsigned i=0; i<eps.size(); ++i)
{
- if(i==lowest.ep || !links[i] || track_nodes.count(links[i]))
+ if(i==lowest.ep || !links[i])
+ continue;
+
+ unsigned link_ep = links[i]->get_endpoint_by_link(*lowest.track);
+ if(track_nodes.count(Key(links[i], link_ep)))
continue;
unsigned mask = eps[i].paths&eps[lowest.ep].paths;
vehicles[0]->advance(reverse ? -d : d);
}
}
+ else if(end_of_route)
+ set_route(0);
}
void Train::save(list<DataFile::Statement> &st) const
{
unsigned nsens = reserve_more();
if(!nsens && end_of_route)
- {
signal_arrived.emit();
- set_route(0);
- }
}
}
}
if(i->block->get_sensor_id())
++nsens;
+ if(end_of_route)
+ return nsens;
+
const Route *cur_route = 0;
if(route)
{
- unsigned exit = last->block->traverse(last->entry);
- Track *track = last->block->get_endpoints()[exit].track;
- if(route->get_tracks().count(track))
- cur_route = route;
- else if(next_route && next_route->get_tracks().count(track))
- cur_route = next_route;
+ const set<Track *> &tracks = last->block->get_tracks();
+ for(set<Track *>::const_iterator i=tracks.begin(); (cur_route!=route && i!=tracks.end()); ++i)
+ {
+ if(route->get_tracks().count(*i))
+ cur_route = route;
+ else if(next_route && next_route->get_tracks().count(*i))
+ cur_route = next_route;
+ }
}
bool got_more = false;