#include "layout.h"
#include "route.h"
#include "track.h"
+#include "trackiter.h"
#include "tracktype.h"
using namespace std;
}
const TrackType &type = lowest.track->get_type();
- const vector<Endpoint> &eps = type.get_endpoints();
+ const vector<TrackType::Endpoint> &eps = type.get_endpoints();
const vector<Track *> &links = lowest.track->get_links();
for(unsigned i=0; i<eps.size(); ++i)
{
}
}
- if(!final)
- throw InvalidParameterValue("Could not find a route");
-
list<Track *> result;
for(Node *node=final; node; node=node->prev)
result.push_front(node->track);
{
list<Track *> tracks = dijkstra(from, ep, goal);
+ if(tracks.empty())
+ return 0;
+
Route *route = new Route(from.get_layout());
for(list<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
route->add_track(**i);
{
found.insert(tid);
- const vector<Endpoint> &endpoints = (*i)->get_type().get_endpoints();
+ const vector<TrackType::Endpoint> &endpoints = (*i)->get_type().get_endpoints();
const vector<Track *> &links = (*i)->get_links();
// Build a combined path mask from linked endpoints
if(unsigned tid2 = links[j]->get_turnout_id())
{
- const Endpoint &ep = links[j]->get_type().get_endpoints()[links[j]->get_endpoint_by_link(**i)];
+ const TrackType::Endpoint &ep = links[j]->get_type().get_endpoints()[links[j]->get_endpoint_by_link(**i)];
int p = get_turnout(tid2);
if(p>=0 && !(ep.paths&(1<<p)))
{
void Route::add_track_chain(Track &start, unsigned ep, const TurnoutMap &trnts)
{
- Track *track = &start;
- while(1)
+ TrackIter iter(&start, ep);
+ while(iter)
{
- if(track->get_type().is_dead_end())
+ if(iter->get_type().is_dead_end())
break;
- if(tracks.count(track))
+ if(has_track(*iter))
break;
int path = 0;
- if(track->get_turnout_id())
+ if(iter->get_turnout_id())
{
- TurnoutMap::const_iterator i = trnts.find(track->get_turnout_id());
+ TurnoutMap::const_iterator i = trnts.find(iter->get_turnout_id());
if(i==trnts.end())
break;
path = i->second;
}
- add_track(*track);
-
- unsigned out_ep = track->traverse(ep, path);
- Track *next = track->get_links()[out_ep];
- if(!next)
- break;
+ add_track(*iter);
- ep = next->get_endpoint_by_link(*track);
- track = next;
+ iter = iter.next(path);
}
}
+bool Route::has_track(Track &t) const
+{
+ return tracks.count(&t);
+}
+
void Route::save(list<DataFile::Statement> &st) const
{
st.push_back((DataFile::Statement("name"), name));
{
// Linked to an existing track - good
result |= 1;
- const vector<Endpoint> &endpoints = (*i)->get_type().get_endpoints();
+ const vector<TrackType::Endpoint> &endpoints = (*i)->get_type().get_endpoints();
if(unsigned tid=(*i)->get_turnout_id())
{
int r = get_turnout(tid);
continue;
unsigned path_mask = 1<<j->second;
- const vector<Endpoint> &eps = (*i)->get_type().get_endpoints();
+ const vector<TrackType::Endpoint> &eps = (*i)->get_type().get_endpoints();
for(unsigned k=0; k<eps.size(); ++k)
if(eps[k].paths&path_mask)
{
obj.add_track_chain(**i, k, turnouts);
break;
}
+
+ break;
}
}