2 #include <msp/core/maputils.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
19 typedef std::pair<Track *, unsigned> Key;
31 Node(const TrackIter &t):
32 track(t), prev(0), dist(0)
35 Node(const TrackIter &t, Node &r, float d):
36 track(t), prev(&r), dist(prev->dist+d)
39 bool operator<(const Node &other) const
40 { return dist>other.dist; }
47 TrackMatch(Track &t): track(t) { }
49 bool operator()(Track &t) const { return &t==&track; }
54 const set<Track *> &tracks;
56 TrackInSet(const set<Track *> &t): tracks(t) { }
58 bool operator()(Track &t) const { return tracks.count(&t); }
61 template<typename Pred>
62 list<Track *> dijkstra(const TrackIter &from, const Pred &goal)
64 map<Key, Node> track_nodes;
65 priority_queue<Node> nodes;
72 Node lowest = nodes.top();
75 Key key(lowest.track.track(), lowest.track.entry());
76 if(track_nodes.count(key))
79 Node &ref = track_nodes[key] = lowest;
80 if(goal(*lowest.track))
86 const TrackType::Endpoint &ep = lowest.track.endpoint();
87 for(unsigned i=0; ep.paths>>i; ++i)
90 TrackIter next = lowest.track.next(i);
94 if(track_nodes.count(Key(next.track(), next.entry())))
97 nodes.push(Node(next, ref, lowest.track->get_type().get_path_length(i)));
101 list<Track *> result;
102 for(Node *node=final; node; node=node->prev)
103 result.push_front(&*node->track);
108 template<typename Pred>
109 Route *create_route(const TrackIter &from, const Pred &goal)
111 list<Track *> tracks = dijkstra(from, goal);
116 Route *route = new Route(from->get_layout());
117 for(list<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
118 route->add_track(**i);
120 route->set_name("Pathfinder");
121 route->set_temporary(true);
131 string bad_route::get_message(RouteValidityMask valid)
133 const char *reasons[3];
136 reasons[i++] = "unlinked";
138 reasons[i++] = "branching";
140 reasons[i++] = "not smooth";
141 return join(reasons, reasons+i, ", ");
145 Route::Route(Layout &l):
149 layout.add_route(*this);
150 layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed));
155 layout.remove_route(*this);
158 void Route::set_name(const string &n)
161 signal_name_changed.emit(name);
164 void Route::set_temporary(bool t)
169 void Route::set_turnout(unsigned addr, unsigned path)
172 throw invalid_argument("Route::set_turnout");
173 int &state = get_item(turnouts, addr);
174 if(state>=0 && path!=static_cast<unsigned>(state))
175 throw logic_error("route conflict");
179 void Route::update_turnouts()
182 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
183 if(unsigned tid = (*i)->get_turnout_id())
187 const vector<TrackType::Endpoint> &endpoints = (*i)->get_type().get_endpoints();
188 const vector<Track *> &links = (*i)->get_links();
190 // Build a combined path mask from linked endpoints
191 unsigned mask = (*i)->get_type().get_paths();
192 for(unsigned j=0; j<endpoints.size(); ++j)
194 if(!tracks.count(links[j]))
197 if(unsigned tid2 = links[j]->get_turnout_id())
199 const TrackType::Endpoint &ep = links[j]->get_type().get_endpoint(links[j]->get_link_slot(**i));
200 int p = get_turnout(tid2);
201 if(p>=0 && !ep.has_path(p))
203 // The linked track is a turnout and has a path which is incompatible with this endpoint
204 mask &= ~endpoints[j].paths;
208 mask &= endpoints[j].paths;
211 if(mask && !(mask&(mask-1)))
213 // Exactly one possible choice, set the path accordingly
215 for(; (mask && !(mask&1)); mask>>=1, ++path) ;
216 turnouts[tid] = path;
218 else if(!turnouts.count(tid))
219 // More than one possible choice, and no existing entry - set as undecided
223 // Remove any turnouts that do not exist in the route
224 for(map<unsigned, int>::iterator i=turnouts.begin(); i!=turnouts.end();)
226 if(!found.count(i->first))
233 int Route::get_turnout(unsigned id) const
235 map<unsigned, int>::const_iterator i = turnouts.find(id);
236 if(i!=turnouts.end())
241 unsigned Route::get_path(Track &trk) const
243 if(unsigned tid = trk.get_turnout_id())
245 map<unsigned, int>::const_iterator i = turnouts.find(tid);
246 if(i!=turnouts.end())
249 return trk.get_active_path();
252 void Route::add_track(Track &trk)
254 if(tracks.count(&trk))
259 RouteValidityMask valid = check_validity(trk);
260 if(valid!=ROUTE_VALID)
261 throw bad_route(valid);
268 void Route::add_tracks(const set<Track *> &trks)
270 set<Track *> pending;
271 for(set<Track *>::const_iterator i=trks.begin(); i!=trks.end(); ++i)
272 if(!tracks.count(*i))
275 while(!pending.empty())
277 RouteValidityMask valid = ROUTE_INVALID;
278 for(set<Track *>::const_iterator i=pending.begin(); i!=pending.end(); ++i)
279 if(tracks.empty() || (valid=check_validity(**i))==ROUTE_VALID)
286 if(valid!=ROUTE_VALID)
287 throw bad_route(valid);
293 void Route::add_track_chain(Track &start, unsigned ep, const TurnoutMap &trnts)
295 TrackIter iter(&start, ep);
298 if(iter->get_type().is_dead_end())
305 if(iter->get_turnout_id())
307 TurnoutMap::const_iterator i = trnts.find(iter->get_turnout_id());
316 iter = iter.next(path);
320 bool Route::has_track(Track &t) const
322 return tracks.count(&t);
325 void Route::save(list<DataFile::Statement> &st) const
327 st.push_back((DataFile::Statement("name"), name));
328 for(map<unsigned, int>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
329 st.push_back((DataFile::Statement("turnout"), i->first, i->second));
332 RouteValidityMask Route::check_validity(Track &trk) const
334 unsigned result = ROUTE_SMOOTH;
335 const vector<Track *> &links = trk.get_links();
336 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
340 if(!tracks.count(*i))
343 // Linked to an existing track - good
344 result |= ROUTE_LINKED;
346 if(unsigned tid = (*i)->get_turnout_id())
348 const TrackType::Endpoint &ep = (*i)->get_type().get_endpoint((*i)->get_link_slot(trk));
349 int path = get_turnout(tid);
352 // Linking to a turnout with path set is only good if we're continuing that path
353 if(ep.has_path(path))
354 result |= ROUTE_LINEAR;
358 // Linked to a turnout with no path set - check other linked tracks
359 const vector<Track *> &tlinks = (*i)->get_links();
361 for(unsigned j=0; j<tlinks.size(); ++j)
362 if(tracks.count(tlinks[j]))
364 unsigned tid2 = tlinks[j]->get_turnout_id();
367 const TrackType::Endpoint &ep2 = tlinks[j]->get_type().get_endpoint(tlinks[j]->get_link_slot(**i));
368 path = get_turnout(tid2);
369 // Ignore a linked turnout with some other path set
370 if(path>=0 && !ep2.has_path(path))
376 const TrackType::Endpoint &ep2 = (*i)->get_type().get_endpoint(j);
377 if(!ep.has_common_paths(ep2))
378 // Impossible path through the turnout - not good
379 result &= ~ROUTE_SMOOTH;
382 // Only good if at most one other track is linked to the turnout
384 result |= ROUTE_LINEAR;
388 // Linked to something linear - good
389 result |= ROUTE_LINEAR;
392 return static_cast<RouteValidityMask>(result);
395 void Route::track_removed(Track &t)
400 Route *Route::find(const TrackIter &from, Track &to)
402 return create_route(from, TrackMatch(to));
405 Route *Route::find(const TrackIter &from, const Route &to)
407 return create_route(from, TrackInSet(to.get_tracks()));
410 Route *Route::find(const TrackIter &from, const Zone &to)
412 return create_route(from, TrackInSet(to.get_tracks()));
415 Route *Route::find(const TrackIter &from, const set<Track *> &to)
417 return create_route(from, TrackInSet(to));
421 Route::Loader::Loader(Route &r):
422 DataFile::ObjectLoader<Route>(r)
424 add("name", &Route::name);
425 add("turnout", &Loader::turnout);
428 void Route::Loader::finish()
430 const set<Track *> <racks = obj.layout.get_tracks();
431 for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
433 unsigned tid = (*i)->get_turnout_id();
437 TurnoutMap::iterator j = turnouts.find(tid);
438 if(j==turnouts.end())
441 const vector<TrackType::Endpoint> &eps = (*i)->get_type().get_endpoints();
442 for(unsigned k=0; k<eps.size(); ++k)
443 if(eps[k].has_path(j->second))
445 Track *link = (*i)->get_link(k);
446 if(!obj.tracks.count(link))
447 obj.add_track_chain(*link, link->get_link_slot(**i), turnouts);
448 if(!obj.tracks.count(*i))
449 obj.add_track_chain(**i, k, turnouts);
457 void Route::Loader::turnout(unsigned id, unsigned path)