3 This file is part of the MSP Märklin suite
4 Copyright © 2007-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
11 #include "tracktype.h"
18 Route::Route(Layout &l, const string &n):
22 layout.add_route(*this);
23 layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed));
28 layout.remove_route(*this);
31 int Route::get_turnout(unsigned id) const
33 map<unsigned, int>::const_iterator i = turnouts.find(id);
39 void Route::add_track(const Track &trk)
41 if(tracks.count(&trk))
46 unsigned valid = check_validity(trk);
48 throw Exception("Not linked to existing tracks");
50 throw Exception("Branching routes not allowed");
52 throw Exception("Route must be smooth");
59 void Route::add_tracks(const set<const Track *> &trks)
61 set<const Track *> pending;
62 for(set<const Track *>::const_iterator i=trks.begin(); i!=trks.end(); ++i)
66 while(!pending.empty())
69 for(set<const Track *>::const_iterator i=pending.begin(); i!=pending.end(); ++i)
70 if(tracks.empty() || check_validity(**i)==7)
79 throw Exception("Could not add all tracks to route");
85 void Route::save(list<DataFile::Statement> &st) const
87 for(map<unsigned, int>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
88 st.push_back((DataFile::Statement("turnout"), i->first, i->second));
91 void Route::update_turnouts()
94 for(set<const Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
95 if(unsigned tid=(*i)->get_turnout_id())
99 const vector<Endpoint> &endpoints = (*i)->get_type().get_endpoints();
100 const vector<Track *> &links = (*i)->get_links();
102 // Build a combined path mask from linked endpoints
104 for(unsigned j=0; j<endpoints.size(); ++j)
106 if(!tracks.count(links[j]))
109 if(unsigned tid2=links[j]->get_turnout_id())
111 const Endpoint &ep = links[j]->get_type().get_endpoints()[links[j]->get_endpoint_by_link(**i)];
112 int p = get_turnout(tid2);
113 if(p>=0 && !(ep.paths&(1<<p)))
115 // The linked track is a turnout and has a path which is incompatible with this endpoint
116 mask &= ~endpoints[j].paths;
120 mask &= endpoints[j].paths;
123 if(mask && !(mask&(mask-1)))
125 // Exactly one possible choice, set the path accordingly
127 for(; (mask && !(mask&1)); mask>>=1, ++path) ;
128 turnouts[tid] = path;
130 else if(!turnouts.count(tid))
131 // More than one possible choice, and no existing entry - set as undecided
135 // Remove any turnouts that do not exist in the route
136 for(map<unsigned, int>::iterator i=turnouts.begin(); i!=turnouts.end();)
138 if(!found.count(i->first))
145 unsigned Route::check_validity(const Track &trk) const
148 for(set<const Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
150 int epi=(*i)->get_endpoint_by_link(trk);
153 // Linked to an existing track - good
155 const vector<Endpoint> &endpoints = (*i)->get_type().get_endpoints();
156 if(unsigned tid=(*i)->get_turnout_id())
158 int r = get_turnout(tid);
161 // Linking to a turnout with path set is only good if we're continuing that path
162 if(endpoints[epi].paths&(1<<r))
167 // Linked to a turnout with no path set - find out other linked tracks
169 const vector<Track *> &links = (*i)->get_links();
171 for(unsigned k=0; k<endpoints.size(); ++k)
172 if(tracks.count(links[k]))
178 // Only good if at most one other track is linked to the turnout
182 if(epj>=0 && !(endpoints[epi].paths&endpoints[epj].paths))
183 // Impossible path through the turnout - not good
189 // Linked to something linear - good
197 void Route::track_removed(Track &t)
203 Route::Loader::Loader(Route &r):
204 DataFile::BasicLoader<Route>(r)
206 add("turnout", &Loader::turnout);
209 void Route::Loader::turnout(unsigned id, unsigned path)
211 obj.turnouts[id] = path;
214 } // namespace Marklin