temporary = t;
}
-void Route::set_turnout(unsigned addr, unsigned path)
-{
- if(!addr)
- throw invalid_argument("Route::set_turnout");
- int &state = get_item(turnouts, addr);
- if(state>=0 && path!=static_cast<unsigned>(state))
- throw logic_error("route conflict");
- state = path;
-}
-
void Route::update_turnouts()
{
set<unsigned> found;
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
- if(unsigned tid = (*i)->get_turnout_id())
+ if((*i)->get_type().is_turnout())
{
- found.insert(tid);
-
- const vector<TrackType::Endpoint> &endpoints = (*i)->get_type().get_endpoints();
- const vector<Track *> &links = (*i)->get_links();
-
- // Build a combined path mask from linked endpoints
- unsigned mask = (*i)->get_type().get_paths();
- for(unsigned j=0; j<endpoints.size(); ++j)
- {
- if(!tracks.count(links[j]))
- continue;
-
- if(unsigned tid2 = links[j]->get_turnout_id())
- {
- const TrackType::Endpoint &ep = links[j]->get_type().get_endpoint(links[j]->get_link_slot(**i));
- int p = get_turnout(tid2);
- if(p>=0 && !ep.has_path(p))
- {
- // The linked track is a turnout and has a path which is incompatible with this endpoint
- mask &= ~endpoints[j].paths;
- continue;
- }
- }
- mask &= endpoints[j].paths;
- }
-
- if(mask && !(mask&(mask-1)))
- {
- // Exactly one possible choice, set the path accordingly
- unsigned path = 0;
- for(; (mask && !(mask&1)); mask>>=1, ++path) ;
- turnouts[tid] = path;
- }
- else if(!turnouts.count(tid))
- // More than one possible choice, and no existing entry - set as undecided
- turnouts[tid] = -1;
+ found.insert((*i)->get_turnout_id());
+ update_turnout(**i);
}
// Remove any turnouts that do not exist in the route
}
}
+void Route::update_turnout(Track &track)
+{
+ // Build a combined path mask from linked endpoints
+ unsigned nls = track.get_n_link_slots();
+ unsigned mask = track.get_type().get_paths();
+ for(unsigned i=0; i<nls; ++i)
+ {
+ Track *link = track.get_link(i);
+ if(!tracks.count(link))
+ continue;
+
+ unsigned ep_mask = track.get_type().get_endpoint(i).paths;
+ if(link->get_type().is_turnout())
+ {
+ const TrackType::Endpoint &link_ep = link->get_type().get_endpoint(link->get_link_slot(track));
+ int p = get_turnout(link->get_turnout_id());
+ if(p>=0 && !link_ep.has_path(p))
+ // The linked track is a turnout and has a path which is incompatible with this endpoint
+ ep_mask = ~ep_mask;
+ }
+
+ mask &= ep_mask;
+ }
+
+ unsigned tid = track.get_turnout_id();
+ if(mask && !(mask&(mask-1)))
+ {
+ // Exactly one possible choice, set the path accordingly
+ unsigned path = 0;
+ for(; (mask && !(mask&1)); mask>>=1, ++path) ;
+ turnouts[tid] = path;
+ }
+ else if(!turnouts.count(tid))
+ // More than one possible choice, and no existing entry - set as undecided
+ turnouts[tid] = -1;
+}
+
int Route::get_turnout(unsigned id) const
{
map<unsigned, int>::const_iterator i = turnouts.find(id);
return trk.get_active_path();
}
-void Route::add_track_chain(const TrackIter &start, const TurnoutMap &trnts)
+void Route::add_turnout(Track &track, unsigned path)
{
- if(!start)
- throw invalid_argument("Route::add_track_chain");
-
- TrackIter iter = start;
- while(iter)
- {
- if(iter->get_type().is_dead_end())
- break;
-
- if(has_track(*iter))
- break;
-
- int path = 0;
- if(iter->get_turnout_id())
+ TrackSet tracks_to_add;
+ tracks_to_add.insert(&track);
+ unsigned nls = track.get_n_link_slots();
+ for(unsigned i=0; i<nls; ++i)
+ if(track.get_type().get_endpoint(i).has_path(path))
{
- TurnoutMap::const_iterator i = trnts.find(iter->get_turnout_id());
- if(i==trnts.end())
- break;
-
- path = i->second;
+ TrackIter iter = TrackIter(&track, i).flip();
+ for(; (iter && !iter->get_type().is_turnout()); iter=iter.next())
+ tracks_to_add.insert(&*iter);
}
- add_track(*iter);
-
- iter = iter.next(path);
- }
+ add_tracks(tracks_to_add);
}
-void Route::on_track_added(Track &)
+void Route::on_track_added(Track &track)
{
- update_turnouts();
+ update_turnout(track);
+ unsigned nls = track.get_n_link_slots();
+ for(unsigned i=0; i<nls; ++i)
+ if(Track *link = track.get_link(i))
+ if(tracks.count(link))
+ update_turnout(*link);
}
void Route::save(list<DataFile::Statement> &st) const
void Route::Loader::finish()
{
const set<Track *> <racks = obj.layout.get_all<Track>();
+ map<Track *, unsigned> turnout_tracks;
for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
- {
- unsigned tid = (*i)->get_turnout_id();
- if(!tid)
- continue;
-
- TurnoutMap::iterator j = turnouts.find(tid);
- if(j==turnouts.end())
- continue;
-
- const vector<TrackType::Endpoint> &eps = (*i)->get_type().get_endpoints();
- for(unsigned k=0; k<eps.size(); ++k)
- if(eps[k].has_path(j->second))
- {
- Track *link = (*i)->get_link(k);
- if(!obj.tracks.count(link))
- obj.add_track_chain(TrackIter(link, link->get_link_slot(**i)), turnouts);
- if(!obj.tracks.count(*i))
- obj.add_track_chain(TrackIter(*i, k), turnouts);
- break;
- }
+ if((*i)->get_type().is_turnout())
+ {
+ TurnoutMap::iterator j = turnouts.find((*i)->get_turnout_id());
+ if(j!=turnouts.end())
+ turnout_tracks[*i] = j->second;
+ }
- break;
+ for(map<Track *, unsigned>::iterator i=turnout_tracks.begin(); i!=turnout_tracks.end(); )
+ {
+ if(obj.check_validity(*i->first)==VALID)
+ {
+ obj.add_turnout(*i->first, i->second);
+ turnout_tracks.erase(i);
+ i = turnout_tracks.begin();
+ }
+ else
+ ++i;
}
}