3 This file is part of R²C²
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/core/refptr.h>
10 #include <msp/datafile/parser.h>
11 #include <msp/datafile/writer.h>
12 #include <msp/io/print.h>
13 #include <msp/time/utils.h>
15 #include "catalogue.h"
20 #include "tracktype.h"
22 #include "vehicletype.h"
30 bool zone_order(const R2C2::Zone *z1, const R2C2::Zone *z2)
32 return z1->get_number()<z2->get_number();
40 Layout::Layout(Catalogue &c, Driver *d):
43 next_turnout_id(0x800)
46 driver->signal_sensor.connect(sigc::mem_fun(this, &Layout::sensor_event));
52 while(!trains.empty())
53 delete trains.begin()->second;
54 while(!routes.empty())
55 delete *routes.begin();
56 while(!tracks.empty())
57 delete *tracks.begin();
58 while(!blocks.empty())
59 delete *blocks.begin();
62 Driver &Layout::get_driver() const
65 throw InvalidState("No driver");
69 void Layout::add_track(Track &t)
71 if(tracks.insert(&t).second)
74 signal_track_added.emit(t);
78 void Layout::remove_track(Track &t)
83 signal_track_removed.emit(t);
87 unsigned Layout::allocate_turnout_id(bool dbl)
89 set<unsigned> used_ids;
90 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
91 if((*i)->get_turnout_id())
92 used_ids.insert((*i)->get_turnout_id());
94 unsigned result = next_turnout_id;
95 while(used_ids.count(result) || (dbl && used_ids.count(result+1)))
97 next_turnout_id = result+1+dbl;
102 void Layout::add_block(Block &b)
107 Block &Layout::get_block(unsigned id) const
109 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
110 if((*i)->get_id()==id)
113 throw KeyError("Unknown block", lexical_cast(id));
116 void Layout::create_blocks()
118 set<Track *> used_tracks;
119 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
121 const set<Track *> &btracks = (*i)->get_tracks();
122 used_tracks.insert(btracks.begin(), btracks.end());
125 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
126 if(used_tracks.count(*i)==0)
128 Block *block = new Block(*this, **i);
129 used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
132 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
133 for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
135 (*i)->check_link(**j);
138 void Layout::create_blocks(Track &track)
140 /* Must collect the blocks in a set first while all tracks are still
141 guaranteed to have blocks and to avoid duplicate deletes */
142 set<Block *> del_blocks;
144 del_blocks.insert(&track.get_block());
146 const vector<Track *> &links = track.get_links();
147 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
149 del_blocks.insert(&(*i)->get_block());
151 for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
157 void Layout::remove_block(Block &b)
162 void Layout::add_route(Route &r)
164 if(routes.insert(&r).second)
165 signal_route_added.emit(r);
168 Route &Layout::get_route(const string &name) const
170 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
171 if((*i)->get_name()==name)
173 throw KeyError("Unknown route", name);
176 void Layout::update_routes()
178 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
179 (*i)->update_turnouts();
182 void Layout::remove_route(Route &r)
185 signal_route_removed.emit(r);
188 void Layout::add_zone(Zone &z)
190 if(zones.insert(&z).second)
191 signal_zone_added.emit(z);
194 Layout::ZoneArray Layout::get_zones(const string &group) const
197 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
198 if((*i)->get_group()==group)
199 result.push_back(*i);
201 sort(result.begin(), result.end(), zone_order);
206 Zone &Layout::get_zone(const string &group, unsigned num) const
208 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
209 if((*i)->get_group()==group && (*i)->get_number()==num)
212 throw KeyError("Unknown zone", format("%s %d", group, num));
215 void Layout::remove_zone(Zone &z)
218 signal_zone_removed.emit(z);
221 void Layout::add_train(Train &t)
223 if(trains.count(t.get_address()))
224 throw KeyError("Duplicate train address", lexical_cast(t.get_address()));
226 trains[t.get_address()] = &t;
227 signal_train_added.emit(t);
230 Train &Layout::get_train(unsigned addr) const
232 map<unsigned, Train *>::const_iterator i = trains.find(addr);
234 throw KeyError("Unknown train", lexical_cast(addr));
238 void Layout::remove_train(Train &t)
240 if(trains.erase(t.get_address()))
241 signal_train_removed.emit(t);
244 void Layout::add_vehicle(Vehicle &v)
246 if(vehicles.insert(&v).second)
247 signal_vehicle_added.emit(v);
250 void Layout::remove_vehicle(Vehicle &v)
252 if(vehicles.erase(&v))
253 signal_vehicle_removed.emit(v);
261 Time::TimeStamp t = Time::now();
267 for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
268 i->second->tick(t, dt);
271 void Layout::emergency(const string &msg)
275 IO::print("Emergency: %s\n", msg);
276 signal_emergency.emit(msg);
279 void Layout::save(const string &fn) const
281 IO::BufferedFile out(fn, IO::M_WRITE);
282 DataFile::Writer writer(out);
285 writer.write((DataFile::Statement("base"), base));
287 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
289 DataFile::Statement st("track");
290 st.append((*i)->get_type().get_article_number());
295 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
297 if((*i)->is_temporary())
300 DataFile::Statement st("route");
305 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
307 DataFile::Statement st("zone");
313 void Layout::save_trains(const string &fn) const
315 IO::BufferedFile out(fn, IO::M_WRITE);
316 DataFile::Writer writer(out);
318 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
320 DataFile::Statement st("train");
321 st.append(i->second->get_locomotive_type().get_article_number());
322 st.append(i->second->get_address());
323 st.append(i->second->get_protocol());
324 i->second->save(st.sub);
329 void Layout::sensor_event(unsigned addr, bool state)
333 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
334 if((*i)->get_sensor_id()==addr)
336 if(!(*i)->get_train())
337 emergency(format("Unreserved sensor %d triggered", addr));
344 Layout::Loader::Loader(Layout &l):
345 DataFile::BasicLoader<Layout>(l),
348 add("base", &Layout::base);
349 add("route", static_cast<void (Loader::*)()>(&Loader::route));
350 add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
351 add("train", static_cast<void (Loader::*)(ArticleNumber, unsigned, const std::string &)>(&Loader::train));
352 add("zone", &Loader::zone);
354 // Deprecated aliases
355 add("route", static_cast<void (Loader::*)(const string &)>(&Loader::route));
356 add("track", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
357 add("train", static_cast<void (Loader::*)(unsigned, unsigned, const std::string &)>(&Loader::train));
360 void Layout::Loader::finish()
362 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
366 void Layout::Loader::route()
368 Route *rte = new Route(obj);
372 void Layout::Loader::route(const string &n)
374 Route *rte = new Route(obj);
379 void Layout::Loader::track(unsigned art_nr)
381 track(ArticleNumber(art_nr));
384 void Layout::Loader::track(ArticleNumber art_nr)
386 Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
389 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
391 trk->snap_to(**i, true);
394 void Layout::Loader::train(unsigned art_nr, unsigned addr, const std::string &proto)
396 train(ArticleNumber(art_nr), addr, proto);
399 void Layout::Loader::train(ArticleNumber art_nr, unsigned addr, const std::string &proto)
401 Train *trn = new Train(obj, obj.catalogue.get_vehicle(art_nr), addr, proto);
405 void Layout::Loader::zone()
407 Zone *zne = new Zone(obj);