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 Route::Route(Layout &l):
140 layout.remove(*this);
143 void Route::set_temporary(bool t)
148 void Route::update_turnouts()
151 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
152 if((*i)->get_type().is_turnout())
154 found.insert((*i)->get_turnout_address());
158 // Remove any turnouts that do not exist in the route
159 for(map<unsigned, int>::iterator i=turnouts.begin(); i!=turnouts.end();)
161 if(!found.count(i->first))
168 void Route::update_turnout(Track &track)
170 if(!track.get_type().is_turnout())
173 // Build a combined path mask from linked endpoints
174 unsigned nls = track.get_n_link_slots();
175 unsigned mask = track.get_type().get_paths();
176 for(unsigned i=0; i<nls; ++i)
178 Track *link = track.get_link(i);
179 if(!tracks.count(link))
182 unsigned ep_mask = track.get_type().get_endpoint(i).paths;
183 if(link->get_type().is_turnout())
185 const TrackType::Endpoint &link_ep = link->get_type().get_endpoint(link->get_link_slot(track));
186 int p = get_turnout(link->get_turnout_address());
187 if(p>=0 && !link_ep.has_path(p))
188 // The linked track is a turnout and has a path which is incompatible with this endpoint
195 unsigned taddr = track.get_turnout_address();
196 if(mask && !(mask&(mask-1)))
198 // Exactly one possible choice, set the path accordingly
200 for(; (mask && !(mask&1)); mask>>=1, ++path) ;
201 turnouts[taddr] = path;
203 else if(!turnouts.count(taddr))
204 // More than one possible choice, and no existing entry - set as undecided
205 turnouts[taddr] = -1;
208 int Route::get_turnout(unsigned id) const
210 map<unsigned, int>::const_iterator i = turnouts.find(id);
211 if(i!=turnouts.end())
216 unsigned Route::get_path(Track &trk) const
218 if(trk.get_type().is_turnout())
220 map<unsigned, int>::const_iterator i = turnouts.find(trk.get_turnout_address());
221 if(i!=turnouts.end() && i->second>=0)
224 return trk.get_active_path();
227 void Route::add_turnout(Track &track, unsigned path)
229 TrackSet tracks_to_add;
230 tracks_to_add.insert(&track);
231 unsigned nls = track.get_n_link_slots();
232 for(unsigned i=0; i<nls; ++i)
233 if(track.get_type().get_endpoint(i).has_path(path))
235 TrackIter iter = TrackIter(&track, i).flip();
236 for(; (iter && !iter->get_type().is_turnout()); iter=iter.next())
237 tracks_to_add.insert(&*iter);
240 add_tracks(tracks_to_add);
243 void Route::on_track_added(Track &track)
245 update_turnout(track);
246 unsigned nls = track.get_n_link_slots();
247 for(unsigned i=0; i<nls; ++i)
248 if(Track *link = track.get_link(i))
249 if(tracks.count(link))
250 update_turnout(*link);
253 void Route::save(list<DataFile::Statement> &st) const
255 st.push_back((DataFile::Statement("name"), name));
256 for(map<unsigned, int>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
257 st.push_back((DataFile::Statement("turnout"), i->first, i->second));
260 DataFile::Statement Route::save_reference() const
262 return (DataFile::Statement("route"), name);
265 Route *Route::find(const TrackIter &from, Track &to)
267 return create_route(from, TrackMatch(to));
270 Route *Route::find(const TrackIter &from, const Route &to)
272 return create_route(from, TrackInSet(to.get_tracks()));
275 Route *Route::find(const TrackIter &from, const Zone &to)
277 return create_route(from, TrackInSet(to.get_tracks()));
280 Route *Route::find(const TrackIter &from, const set<Track *> &to)
282 return create_route(from, TrackInSet(to));
286 Route::Loader::Loader(Route &r):
287 DataFile::ObjectLoader<Route>(r)
289 add("name", &Loader::name);
290 add("turnout", &Loader::turnout);
293 void Route::Loader::finish()
295 const set<Track *> <racks = obj.layout.get_all<Track>();
296 map<Track *, unsigned> turnout_tracks;
297 for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
298 if((*i)->get_type().is_turnout())
300 TurnoutMap::iterator j = turnouts.find((*i)->get_turnout_address());
301 if(j!=turnouts.end())
302 turnout_tracks[*i] = j->second;
305 for(map<Track *, unsigned>::iterator i=turnout_tracks.begin(); i!=turnout_tracks.end(); )
307 if(obj.check_validity(*i->first)==VALID)
309 obj.add_turnout(*i->first, i->second);
310 turnout_tracks.erase(i);
311 i = turnout_tracks.begin();
318 void Route::Loader::name(const string &n)
323 void Route::Loader::turnout(unsigned id, unsigned path)