2 #include <msp/core/maputils.h>
3 #include <msp/core/refptr.h>
4 #include <msp/datafile/parser.h>
5 #include <msp/datafile/writer.h>
6 #include <msp/io/print.h>
7 #include <msp/time/utils.h>
14 #include "tracktype.h"
16 #include "vehicletype.h"
24 bool zone_order(const R2C2::Zone *z1, const R2C2::Zone *z2)
26 return z1->get_number()<z2->get_number();
34 Layout::Layout(Catalogue &c, Driver *d):
37 next_turnout_id(0x800)
45 while(!trains.empty())
46 delete trains.begin()->second;
47 while(!routes.empty())
48 delete *routes.begin();
50 delete *zones.begin();
51 while(!tracks.empty())
52 delete *tracks.begin();
53 while(!blocks.empty())
54 delete *blocks.begin();
57 Driver &Layout::get_driver() const
60 throw logic_error("!driver");
64 void Layout::add_track(Track &t)
66 if(tracks.insert(&t).second)
69 signal_track_added.emit(t);
73 void Layout::remove_track(Track &t)
78 signal_track_removed.emit(t);
82 Track *Layout::pick_track(const Vector &start, const Vector &ray)
84 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
85 if((*i)->collide_ray(start, ray))
91 unsigned Layout::allocate_turnout_id()
93 set<unsigned> used_ids;
94 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
95 if((*i)->get_turnout_id())
96 used_ids.insert((*i)->get_turnout_id());
98 unsigned result = next_turnout_id;
99 while(used_ids.count(result))
101 next_turnout_id = result+1;
106 void Layout::add_block(Block &b)
109 b.signal_reserved.connect(sigc::bind<0>(signal_block_reserved, sigc::ref(b)));
110 if(b.get_sensor_id())
112 b.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::block_state_changed), sigc::ref(b)));
113 b.signal_state_changed.connect(sigc::bind<0>(signal_block_state_changed, sigc::ref(b)));
117 Block &Layout::get_block(unsigned id) const
119 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
120 if((*i)->get_id()==id)
126 void Layout::create_blocks()
128 set<Track *> used_tracks;
129 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
131 const set<Track *> &btracks = (*i)->get_tracks();
132 used_tracks.insert(btracks.begin(), btracks.end());
135 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
136 if(used_tracks.count(*i)==0)
138 Block *block = new Block(*this, **i);
139 used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
142 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
143 for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
145 (*i)->check_link(**j);
148 void Layout::create_blocks(Track &track)
150 /* Must collect the blocks in a set first while all tracks are still
151 guaranteed to have blocks and to avoid duplicate deletes */
152 set<Block *> del_blocks;
154 del_blocks.insert(&track.get_block());
156 const vector<Track *> &links = track.get_links();
157 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
159 del_blocks.insert(&(*i)->get_block());
161 for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
167 void Layout::remove_block(Block &b)
172 void Layout::add_route(Route &r)
174 if(routes.insert(&r).second)
175 signal_route_added.emit(r);
178 Route &Layout::get_route(const string &name) const
180 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
181 if((*i)->get_name()==name)
183 throw key_error(name);
186 void Layout::update_routes()
188 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
189 (*i)->update_turnouts();
192 void Layout::remove_route(Route &r)
195 signal_route_removed.emit(r);
198 void Layout::add_zone(Zone &z)
200 if(zones.insert(&z).second)
201 signal_zone_added.emit(z);
204 Layout::ZoneArray Layout::get_zones(const string &group) const
207 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
208 if((*i)->get_group()==group)
209 result.push_back(*i);
211 sort(result.begin(), result.end(), zone_order);
216 Zone &Layout::get_zone(const string &group, unsigned num) const
218 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
219 if((*i)->get_group()==group && (*i)->get_number()==num)
222 throw key_error(format("%s %d", group, num));
225 void Layout::remove_zone(Zone &z)
228 signal_zone_removed.emit(z);
231 void Layout::add_train(Train &t)
233 insert_unique(trains, t.get_address(), &t);
234 signal_train_added.emit(t);
237 Train &Layout::get_train(unsigned addr) const
239 return *get_item(trains, addr);
242 void Layout::remove_train(Train &t)
244 if(trains.erase(t.get_address()))
245 signal_train_removed.emit(t);
248 void Layout::add_vehicle(Vehicle &v)
250 if(vehicles.insert(&v).second)
251 signal_vehicle_added.emit(v);
254 void Layout::remove_vehicle(Vehicle &v)
256 if(vehicles.erase(&v))
257 signal_vehicle_removed.emit(v);
265 Time::TimeStamp t = Time::now();
271 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
273 for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
274 i->second->tick(t, dt);
277 void Layout::emergency(const string &msg)
281 IO::print("Emergency: %s\n", msg);
282 signal_emergency.emit(msg);
285 void Layout::save(const string &fn) const
287 IO::BufferedFile out(fn, IO::M_WRITE);
288 DataFile::Writer writer(out);
291 writer.write((DataFile::Statement("base"), base));
293 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
295 DataFile::Statement st("track");
296 st.append((*i)->get_type().get_article_number());
301 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
303 if((*i)->is_temporary())
306 DataFile::Statement st("route");
311 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
313 DataFile::Statement st("zone");
319 void Layout::save_dynamic(const string &fn) const
321 IO::BufferedFile out(fn, IO::M_WRITE);
322 DataFile::Writer writer(out);
324 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
325 if(unsigned tid = (*i)->get_turnout_id())
326 writer.write((DataFile::Statement("turnout"), tid, (*i)->get_active_path()));
328 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
330 DataFile::Statement st("train");
331 st.append(i->second->get_locomotive_type().get_article_number());
332 st.append(i->second->get_address());
333 st.append(i->second->get_protocol());
334 i->second->save(st.sub);
339 void Layout::block_state_changed(Block &block, Block::State state)
341 if(state==Block::ACTIVE && !block.get_train())
342 emergency(format("Unreserved sensor %d triggered", block.get_sensor_id()));
346 Layout::Loader::Loader(Layout &l):
347 DataFile::ObjectLoader<Layout>(l),
350 add("base", &Layout::base);
351 add("route", static_cast<void (Loader::*)()>(&Loader::route));
352 add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
353 add("train", static_cast<void (Loader::*)(ArticleNumber, unsigned, const std::string &)>(&Loader::train));
354 add("turnout", &Loader::turnout);
355 add("zone", &Loader::zone);
357 // Deprecated aliases
358 add("route", static_cast<void (Loader::*)(const string &)>(&Loader::route));
359 add("track", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
360 add("train", static_cast<void (Loader::*)(unsigned, unsigned, const std::string &)>(&Loader::train));
363 void Layout::Loader::finish()
365 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
369 void Layout::Loader::route()
371 Route *rte = new Route(obj);
375 void Layout::Loader::route(const string &n)
377 Route *rte = new Route(obj);
382 void Layout::Loader::track(unsigned art_nr)
384 track(ArticleNumber(art_nr));
387 void Layout::Loader::track(ArticleNumber art_nr)
389 Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
392 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
394 trk->snap_to(**i, true);
397 void Layout::Loader::train(unsigned art_nr, unsigned addr, const std::string &proto)
399 train(ArticleNumber(art_nr), addr, proto);
402 void Layout::Loader::train(ArticleNumber art_nr, unsigned addr, const std::string &proto)
404 Train *trn = new Train(obj, obj.catalogue.get_vehicle(art_nr), addr, proto);
408 void Layout::Loader::turnout(unsigned addr, unsigned path)
411 obj.driver->set_turnout(addr, path);
414 void Layout::Loader::zone()
416 Zone *zne = new Zone(obj);