X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Froute.cpp;h=20017d240a429562a1e7e3907999eb40c2a2ebb6;hb=02c9a9779954d993cb73fe5f7a72b0847e87f633;hp=fa2286cc65aef625848101bc7b7c77c9d020d055;hpb=444c7efb2fadb10e98197a62b791829d821370c0;p=r2c2.git diff --git a/source/libmarklin/route.cpp b/source/libmarklin/route.cpp index fa2286c..20017d2 100644 --- a/source/libmarklin/route.cpp +++ b/source/libmarklin/route.cpp @@ -5,18 +5,21 @@ Copyright © 2007-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ +#include "layout.h" +#include "route.h" #include "track.h" #include "tracktype.h" -#include "route.h" using namespace std; using namespace Msp; namespace Marklin { -Route::Route(const string &n): +Route::Route(Layout &layout, const string &n): name(n) -{ } +{ + layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed)); +} int Route::get_turnout(unsigned id) const { @@ -89,34 +92,40 @@ void Route::update_turnouts() const vector &endpoints = (*i)->get_type().get_endpoints(); const vector &links = (*i)->get_links(); + // Build a combined path mask from linked endpoints unsigned mask = 15; for(unsigned j=0; jget_turnout_id()) { const Endpoint &ep = links[j]->get_type().get_endpoints()[links[j]->get_endpoint_by_link(**i)]; - int r = get_turnout(tid2); - if(r>=0 && !(ep.routes&(1<=0 && !(ep.paths&(1<>=1, ++route) ; - turnouts[tid] = route; + // Exactly one possible choice, set the path accordingly + unsigned path = 0; + for(; (mask && !(mask&1)); mask>>=1, ++path) ; + turnouts[tid] = path; } - else + else if(!turnouts.count(tid)) + // More than one possible choice, and no existing entry - set as undecided turnouts[tid] = -1; } + // Remove any turnouts that do not exist in the route for(map::iterator i=turnouts.begin(); i!=turnouts.end();) { if(!found.count(i->first)) @@ -134,6 +143,7 @@ unsigned Route::check_validity(const Track &trk) const int epi=(*i)->get_endpoint_by_link(trk); if(epi>=0) { + // Linked to an existing track - good result |= 1; const vector &endpoints = (*i)->get_type().get_endpoints(); if(unsigned tid=(*i)->get_turnout_id()) @@ -141,11 +151,13 @@ unsigned Route::check_validity(const Track &trk) const int r = get_turnout(tid); if(r>=0) { - if(endpoints[epi].routes&(1< &links = (*i)->get_links(); int epj = -1; @@ -155,15 +167,19 @@ unsigned Route::check_validity(const Track &trk) const ++count; epj = k; } + + // Only good if at most one other track is linked to the turnout if(count<=1) { result |= 2; - if(epj>=0 && !(endpoints[epi].routes&endpoints[epj].routes)) + if(epj>=0 && !(endpoints[epi].paths&endpoints[epj].paths)) + // Impossible path through the turnout - not good result &= 3; } } } else + // Linked to something linear - good result |= 2; } } @@ -171,6 +187,11 @@ unsigned Route::check_validity(const Track &trk) const return result; } +void Route::track_removed(Track &t) +{ + tracks.erase(&t); +} + Route::Loader::Loader(Route &r): DataFile::BasicLoader(r) @@ -178,9 +199,9 @@ Route::Loader::Loader(Route &r): add("turnout", &Loader::turnout); } -void Route::Loader::turnout(unsigned id, unsigned route) +void Route::Loader::turnout(unsigned id, unsigned path) { - obj.turnouts[id] = route; + obj.turnouts[id] = path; } } // namespace Marklin