3 This file is part of the MSP Märklin suite
4 Copyright © 2007-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
11 #include "tracktype.h"
18 Route::Route(Layout &layout, const string &n):
21 layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed));
24 int Route::get_turnout(unsigned id) const
26 map<unsigned, int>::const_iterator i = turnouts.find(id);
32 void Route::add_track(const Track &trk)
34 if(tracks.count(&trk))
39 unsigned valid = check_validity(trk);
41 throw Exception("Not linked to existing tracks");
43 throw Exception("Branching routes not allowed");
45 throw Exception("Route must be smooth");
52 void Route::add_tracks(const set<const Track *> &trks)
54 set<const Track *> pending;
55 for(set<const Track *>::const_iterator i=trks.begin(); i!=trks.end(); ++i)
59 while(!pending.empty())
62 for(set<const Track *>::const_iterator i=pending.begin(); i!=pending.end(); ++i)
63 if(tracks.empty() || check_validity(**i)==7)
72 throw Exception("Could not add all tracks to route");
78 void Route::save(list<DataFile::Statement> &st) const
80 for(map<unsigned, int>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
81 st.push_back((DataFile::Statement("turnout"), i->first, i->second));
84 void Route::update_turnouts()
87 for(set<const Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
88 if(unsigned tid=(*i)->get_turnout_id())
92 const vector<Endpoint> &endpoints = (*i)->get_type().get_endpoints();
93 const vector<Track *> &links = (*i)->get_links();
95 // Build a combined path mask from linked endpoints
97 for(unsigned j=0; j<endpoints.size(); ++j)
99 if(!tracks.count(links[j]))
102 if(unsigned tid2=links[j]->get_turnout_id())
104 const Endpoint &ep = links[j]->get_type().get_endpoints()[links[j]->get_endpoint_by_link(**i)];
105 int p = get_turnout(tid2);
106 if(p>=0 && !(ep.paths&(1<<p)))
108 // The linked track is a turnout and has a path which is incompatible with this endpoint
109 mask &= ~endpoints[j].paths;
113 mask &= endpoints[j].paths;
116 if(mask && !(mask&(mask-1)))
118 // Exactly one possible choice, set the path accordingly
120 for(; (mask && !(mask&1)); mask>>=1, ++path) ;
121 turnouts[tid] = path;
123 else if(!turnouts.count(tid))
124 // More than one possible choice, and no existing entry - set as undecided
128 // Remove any turnouts that do not exist in the route
129 for(map<unsigned, int>::iterator i=turnouts.begin(); i!=turnouts.end();)
131 if(!found.count(i->first))
138 unsigned Route::check_validity(const Track &trk) const
141 for(set<const Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
143 int epi=(*i)->get_endpoint_by_link(trk);
146 // Linked to an existing track - good
148 const vector<Endpoint> &endpoints = (*i)->get_type().get_endpoints();
149 if(unsigned tid=(*i)->get_turnout_id())
151 int r = get_turnout(tid);
154 // Linking to a turnout with path set is only good if we're continuing that path
155 if(endpoints[epi].paths&(1<<r))
160 // Linked to a turnout with no path set - find out other linked tracks
162 const vector<Track *> &links = (*i)->get_links();
164 for(unsigned k=0; k<endpoints.size(); ++k)
165 if(tracks.count(links[k]))
171 // Only good if at most one other track is linked to the turnout
175 if(epj>=0 && !(endpoints[epi].paths&endpoints[epj].paths))
176 // Impossible path through the turnout - not good
182 // Linked to something linear - good
190 void Route::track_removed(Track &t)
196 Route::Loader::Loader(Route &r):
197 DataFile::BasicLoader<Route>(r)
199 add("turnout", &Loader::turnout);
202 void Route::Loader::turnout(unsigned id, unsigned path)
204 obj.turnouts[id] = path;
207 } // namespace Marklin