#include <queue>
#include <msp/core/maputils.h>
#include <msp/strings/format.h>
+#include <msp/strings/utils.h>
#include "layout.h"
#include "route.h"
#include "track.h"
namespace R2C2 {
+string bad_route::get_message(RouteValidityMask valid)
+{
+ const char *reasons[3];
+ unsigned i = 0;
+ if(!(valid&1))
+ reasons[i++] = "unlinked";
+ else if(!(valid&2))
+ reasons[i++] = "branching";
+ else if(!(valid&4))
+ reasons[i++] = "not smooth";
+ return join(reasons, reasons+i, ", ");
+}
+
+
Route::Route(Layout &l):
layout(l),
temporary(false)
void Route::set_turnout(unsigned addr, unsigned path)
{
if(!addr)
- throw InvalidParameterValue("Invalid turnout address");
+ throw invalid_argument("Route::set_turnout");
int &state = get_item(turnouts, addr);
if(state>=0 && path!=static_cast<unsigned>(state))
- throw InvalidState("Setting conflicts with route");
+ throw logic_error("route conflict");
state = path;
}
{
const TrackType::Endpoint &ep = links[j]->get_type().get_endpoint(links[j]->get_endpoint_by_link(**i));
int p = get_turnout(tid2);
- if(p>=0 && !(ep.paths&(1<<p)))
+ if(p>=0 && !ep.has_path(p))
{
// The linked track is a turnout and has a path which is incompatible with this endpoint
mask &= ~endpoints[j].paths;
if(!tracks.empty())
{
- unsigned valid = check_validity(trk);
- if(!(valid&1))
- throw Exception("Not linked to existing tracks");
- else if(!(valid&2))
- throw Exception("Branching routes not allowed");
- else if(!(valid&4))
- throw Exception("Route must be smooth");
+ RouteValidityMask valid = check_validity(trk);
+ if(valid!=ROUTE_VALID)
+ throw bad_route(valid);
}
tracks.insert(&trk);
while(!pending.empty())
{
- bool found = false;
+ RouteValidityMask valid = ROUTE_INVALID;
for(set<Track *>::const_iterator i=pending.begin(); i!=pending.end(); ++i)
- if(tracks.empty() || check_validity(**i)==7)
+ if(tracks.empty() || (valid=check_validity(**i))==ROUTE_VALID)
{
tracks.insert(*i);
pending.erase(*i);
- found = true;
break;
}
- if(!found)
- throw Exception("Could not add all tracks to route");
+ if(valid!=ROUTE_VALID)
+ throw bad_route(valid);
}
update_turnouts();
st.push_back((DataFile::Statement("turnout"), i->first, i->second));
}
-unsigned Route::check_validity(Track &trk) const
+RouteValidityMask Route::check_validity(Track &trk) const
{
- unsigned result = 4;
+ unsigned result = ROUTE_SMOOTH;
const vector<Track *> &links = trk.get_links();
for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
{
continue;
// Linked to an existing track - good
- result |= 1;
+ result |= ROUTE_LINKED;
if(unsigned tid = (*i)->get_turnout_id())
{
if(path>=0)
{
// Linking to a turnout with path set is only good if we're continuing that path
- if(ep.paths&(1<<path))
- result |= 2;
+ if(ep.has_path(path))
+ result |= ROUTE_LINEAR;
}
else
{
const TrackType::Endpoint &ep2 = tlinks[j]->get_type().get_endpoint(tlinks[j]->get_endpoint_by_link(**i));
path = get_turnout(tid2);
// Ignore a linked turnout with some other path set
- if(path>=0 && !(ep2.paths&(1<<path)))
+ if(path>=0 && !ep2.has_path(path))
continue;
}
++count;
const TrackType::Endpoint &ep2 = (*i)->get_type().get_endpoint(j);
- if(!(ep.paths&ep2.paths))
+ if(!ep.has_common_paths(ep2))
// Impossible path through the turnout - not good
- result &= 3;
+ result &= ~ROUTE_SMOOTH;
}
// Only good if at most one other track is linked to the turnout
if(count<=1)
- result |= 2;
+ result |= ROUTE_LINEAR;
}
}
else
// Linked to something linear - good
- result |= 2;
+ result |= ROUTE_LINEAR;
}
- return result;
+ return static_cast<RouteValidityMask>(result);
}
void Route::track_removed(Track &t)
Route::Loader::Loader(Route &r):
- DataFile::BasicLoader<Route>(r)
+ DataFile::ObjectLoader<Route>(r)
{
add("name", &Route::name);
add("turnout", &Loader::turnout);
if(j==turnouts.end())
continue;
- unsigned path_mask = 1<<j->second;
const vector<TrackType::Endpoint> &eps = (*i)->get_type().get_endpoints();
for(unsigned k=0; k<eps.size(); ++k)
- if(eps[k].paths&path_mask)
+ if(eps[k].has_path(j->second))
{
Track *link = (*i)->get_link(k);
if(!obj.tracks.count(link))