Layout::Layout(Catalogue &c, Driver *d):
catalogue(c),
- driver(d)
+ driver(d),
+ next_turnout_id(0x800)
{
if(driver)
driver->signal_sensor.connect(sigc::mem_fun(this, &Layout::sensor_event));
while(!trains.empty())
delete trains.begin()->second;
while(!routes.empty())
- delete routes.begin()->second;
+ delete *routes.begin();
while(!tracks.empty())
delete *tracks.begin();
while(!blocks.empty())
}
}
+unsigned Layout::allocate_turnout_id(bool dbl)
+{
+ unsigned result = next_turnout_id++;
+ if(dbl)
+ ++next_turnout_id;
+ return result;
+}
+
void Layout::add_block(Block &b)
{
blocks.insert(&b);
void Layout::add_route(Route &r)
{
- if(routes.count(r.get_name()))
- throw KeyError("Duplicate route name", r.get_name());
-
- routes[r.get_name()] = &r;
- signal_route_added.emit(r);
+ if(routes.insert(&r).second)
+ signal_route_added.emit(r);
}
Route &Layout::get_route(const string &name) const
{
- map<string, Route *>::const_iterator i = routes.find(name);
- if(i==routes.end())
- throw KeyError("Unknown route", name);
- return *i->second;
+ for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
+ if((*i)->get_name()==name)
+ return **i;
+ throw KeyError("Unknown route", name);
+}
+
+void Layout::update_routes()
+{
+ for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
+ (*i)->update_turnouts();
}
void Layout::remove_route(Route &r)
{
- if(routes.erase(r.get_name()))
+ if(routes.erase(&r))
signal_route_removed.emit(r);
}
writer.write(st);
}
- for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
+ for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
{
- if(i->second->is_temporary())
+ if((*i)->is_temporary())
continue;
DataFile::Statement st("route");
- st.append(i->first);
- i->second->save(st.sub);
+ (*i)->save(st.sub);
writer.write(st);
}
}
}
}
-void Layout::check_links()
-{
- for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
- (*i)->break_links();
-
- list<Track *> flext;
- for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
- {
- if((*i)->get_flex())
- flext.push_back(*i);
- else
- {
- for(set<Track *>::iterator j=i; j!=tracks.end(); ++j)
- if(j!=i)
- (*i)->snap_to(**j, true);
- }
- }
-
- for(list<Track *>::iterator i=flext.begin(); i!=flext.end(); ++i)
- for(set<Track *>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
- if(*j!=*i)
- (*i)->snap_to(**j, true);
-}
-
-void Layout::check_routes()
-{
- for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
- {
- if(i->second->is_temporary())
- continue;
-
- /* We must copy the turnout map, since adding tracks to the route will
- (temporarily) mess it up */
- const map<unsigned, int> turnouts = i->second->get_turnouts();
-
- // Find any turnout in the route
- Track *track = 0;
- unsigned trk_path = 0;
- for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
- {
- map<unsigned, int>::const_iterator k = turnouts.find((*j)->get_turnout_id());
- if(k!=turnouts.end())
- {
- track = *j;
- trk_path = k->second;
- break;
- }
- }
-
- if(!track)
- continue;
-
- // Find an applicable endpoint
- const vector<Endpoint> &eps = track->get_type().get_endpoints();
- unsigned ep = 0;
- for(unsigned j=0; j<eps.size(); ++j)
- if(eps[j].paths&(1<<trk_path))
- {
- ep = j;
- break;
- }
-
- Track *start = 0;
- while(1)
- {
- // Traverse the track and get the next one
- if(track->get_type().get_endpoints().size()<2)
- break;
- unsigned out_ep = track->traverse(ep, trk_path);
- Track *next = track->get_links()[out_ep];
- if(!next || next == start)
- break;
-
- ep = next->get_endpoint_by_link(*track);
- if(next->get_type().is_turnout())
- {
- // Select correct path across the turnout, or break if we hit an unknown turnout
- map<unsigned, int>::const_iterator j = turnouts.find(next->get_turnout_id());
- if(j==turnouts.end())
- break;
- trk_path = j->second;
- }
- else
- {
- trk_path = 0;
-
- /* Start adding tracks when we find the first non-turnout. This
- prevents the occurrence of ambiguities while adding the tracks */
- if(!start)
- start = next;
- }
-
- if(start)
- i->second->add_track(*next);
-
- track = next;
- }
- }
-}
-
void Layout::sensor_event(unsigned addr, bool state)
{
if(state)
new_tracks(false)
{
add("base", &Layout::base);
- add("route", &Loader::route);
+ add("route", static_cast<void (Loader::*)()>(&Loader::route));
+ add("route", static_cast<void (Loader::*)(const string &)>(&Loader::route));
add("track", &Loader::track);
add("train", &Loader::train);
}
void Layout::Loader::finish()
{
- if(new_tracks)
- obj.check_links();
- obj.check_routes();
-
for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
(*i)->check_slope();
}
+void Layout::Loader::route()
+{
+ Route *rte = new Route(obj);
+ load_sub(*rte);
+}
+
void Layout::Loader::route(const string &n)
{
- Route *rte = new Route(obj, n);
+ Route *rte = new Route(obj);
+ rte->set_name(n);
load_sub(*rte);
}
Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
load_sub(*trk);
new_tracks = true;
+ for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
+ if(*i!=trk)
+ trk->snap_to(**i, true);
}
void Layout::Loader::train(unsigned art_nr, unsigned addr)