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();
57 delete *zones.begin();
58 while(!tracks.empty())
59 delete *tracks.begin();
60 while(!blocks.empty())
61 delete *blocks.begin();
64 Driver &Layout::get_driver() const
67 throw InvalidState("No driver");
71 void Layout::add_track(Track &t)
73 if(tracks.insert(&t).second)
76 signal_track_added.emit(t);
80 void Layout::remove_track(Track &t)
85 signal_track_removed.emit(t);
89 unsigned Layout::allocate_turnout_id()
91 set<unsigned> used_ids;
92 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
93 if((*i)->get_turnout_id())
94 used_ids.insert((*i)->get_turnout_id());
96 unsigned result = next_turnout_id;
97 while(used_ids.count(result))
99 next_turnout_id = result+1;
104 void Layout::add_block(Block &b)
109 Block &Layout::get_block(unsigned id) const
111 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
112 if((*i)->get_id()==id)
115 throw KeyError("Unknown block", lexical_cast(id));
118 void Layout::create_blocks()
120 set<Track *> used_tracks;
121 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
123 const set<Track *> &btracks = (*i)->get_tracks();
124 used_tracks.insert(btracks.begin(), btracks.end());
127 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
128 if(used_tracks.count(*i)==0)
130 Block *block = new Block(*this, **i);
131 used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
134 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
135 for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
137 (*i)->check_link(**j);
140 void Layout::create_blocks(Track &track)
142 /* Must collect the blocks in a set first while all tracks are still
143 guaranteed to have blocks and to avoid duplicate deletes */
144 set<Block *> del_blocks;
146 del_blocks.insert(&track.get_block());
148 const vector<Track *> &links = track.get_links();
149 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
151 del_blocks.insert(&(*i)->get_block());
153 for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
159 void Layout::remove_block(Block &b)
164 void Layout::add_route(Route &r)
166 if(routes.insert(&r).second)
167 signal_route_added.emit(r);
170 Route &Layout::get_route(const string &name) const
172 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
173 if((*i)->get_name()==name)
175 throw KeyError("Unknown route", name);
178 void Layout::update_routes()
180 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
181 (*i)->update_turnouts();
184 void Layout::remove_route(Route &r)
187 signal_route_removed.emit(r);
190 void Layout::add_zone(Zone &z)
192 if(zones.insert(&z).second)
193 signal_zone_added.emit(z);
196 Layout::ZoneArray Layout::get_zones(const string &group) const
199 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
200 if((*i)->get_group()==group)
201 result.push_back(*i);
203 sort(result.begin(), result.end(), zone_order);
208 Zone &Layout::get_zone(const string &group, unsigned num) const
210 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
211 if((*i)->get_group()==group && (*i)->get_number()==num)
214 throw KeyError("Unknown zone", format("%s %d", group, num));
217 void Layout::remove_zone(Zone &z)
220 signal_zone_removed.emit(z);
223 void Layout::add_train(Train &t)
225 if(trains.count(t.get_address()))
226 throw KeyError("Duplicate train address", lexical_cast(t.get_address()));
228 trains[t.get_address()] = &t;
229 signal_train_added.emit(t);
232 Train &Layout::get_train(unsigned addr) const
234 map<unsigned, Train *>::const_iterator i = trains.find(addr);
236 throw KeyError("Unknown train", lexical_cast(addr));
240 void Layout::remove_train(Train &t)
242 if(trains.erase(t.get_address()))
243 signal_train_removed.emit(t);
246 void Layout::add_vehicle(Vehicle &v)
248 if(vehicles.insert(&v).second)
249 signal_vehicle_added.emit(v);
252 void Layout::remove_vehicle(Vehicle &v)
254 if(vehicles.erase(&v))
255 signal_vehicle_removed.emit(v);
263 Time::TimeStamp t = Time::now();
269 for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
270 i->second->tick(t, dt);
273 void Layout::emergency(const string &msg)
277 IO::print("Emergency: %s\n", msg);
278 signal_emergency.emit(msg);
281 void Layout::save(const string &fn) const
283 IO::BufferedFile out(fn, IO::M_WRITE);
284 DataFile::Writer writer(out);
287 writer.write((DataFile::Statement("base"), base));
289 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
291 DataFile::Statement st("track");
292 st.append((*i)->get_type().get_article_number());
297 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
299 if((*i)->is_temporary())
302 DataFile::Statement st("route");
307 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
309 DataFile::Statement st("zone");
315 void Layout::save_trains(const string &fn) const
317 IO::BufferedFile out(fn, IO::M_WRITE);
318 DataFile::Writer writer(out);
320 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
322 DataFile::Statement st("train");
323 st.append(i->second->get_locomotive_type().get_article_number());
324 st.append(i->second->get_address());
325 st.append(i->second->get_protocol());
326 i->second->save(st.sub);
331 void Layout::sensor_event(unsigned addr, bool state)
335 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
336 if((*i)->get_sensor_id()==addr)
338 if(!(*i)->get_train())
339 emergency(format("Unreserved sensor %d triggered", addr));
346 Layout::Loader::Loader(Layout &l):
347 DataFile::BasicLoader<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("zone", &Loader::zone);
356 // Deprecated aliases
357 add("route", static_cast<void (Loader::*)(const string &)>(&Loader::route));
358 add("track", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
359 add("train", static_cast<void (Loader::*)(unsigned, unsigned, const std::string &)>(&Loader::train));
362 void Layout::Loader::finish()
364 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
368 void Layout::Loader::route()
370 Route *rte = new Route(obj);
374 void Layout::Loader::route(const string &n)
376 Route *rte = new Route(obj);
381 void Layout::Loader::track(unsigned art_nr)
383 track(ArticleNumber(art_nr));
386 void Layout::Loader::track(ArticleNumber art_nr)
388 Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
391 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
393 trk->snap_to(**i, true);
396 void Layout::Loader::train(unsigned art_nr, unsigned addr, const std::string &proto)
398 train(ArticleNumber(art_nr), addr, proto);
401 void Layout::Loader::train(ArticleNumber art_nr, unsigned addr, const std::string &proto)
403 Train *trn = new Train(obj, obj.catalogue.get_vehicle(art_nr), addr, proto);
407 void Layout::Loader::zone()
409 Zone *zne = new Zone(obj);