3 This file is part of R²C²
4 Copyright © 2006-2011 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)
51 while(!trains.empty())
52 delete trains.begin()->second;
53 while(!routes.empty())
54 delete *routes.begin();
56 delete *zones.begin();
57 while(!tracks.empty())
58 delete *tracks.begin();
59 while(!blocks.empty())
60 delete *blocks.begin();
63 Driver &Layout::get_driver() const
66 throw InvalidState("No driver");
70 void Layout::add_track(Track &t)
72 if(tracks.insert(&t).second)
75 signal_track_added.emit(t);
79 void Layout::remove_track(Track &t)
84 signal_track_removed.emit(t);
88 Track *Layout::pick_track(const Vector &start, const Vector &ray)
90 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
91 if((*i)->collide_ray(start, ray))
97 unsigned Layout::allocate_turnout_id()
99 set<unsigned> used_ids;
100 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
101 if((*i)->get_turnout_id())
102 used_ids.insert((*i)->get_turnout_id());
104 unsigned result = next_turnout_id;
105 while(used_ids.count(result))
107 next_turnout_id = result+1;
112 void Layout::add_block(Block &b)
115 b.signal_reserved.connect(sigc::bind<0>(signal_block_reserved, sigc::ref(b)));
116 if(b.get_sensor_id())
118 b.signal_state_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::block_state_changed), sigc::ref(b)));
119 b.signal_state_changed.connect(sigc::bind<0>(signal_block_state_changed, sigc::ref(b)));
123 Block &Layout::get_block(unsigned id) const
125 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
126 if((*i)->get_id()==id)
129 throw KeyError("Unknown block", lexical_cast(id));
132 void Layout::create_blocks()
134 set<Track *> used_tracks;
135 for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
137 const set<Track *> &btracks = (*i)->get_tracks();
138 used_tracks.insert(btracks.begin(), btracks.end());
141 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
142 if(used_tracks.count(*i)==0)
144 Block *block = new Block(*this, **i);
145 used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
148 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
149 for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
151 (*i)->check_link(**j);
154 void Layout::create_blocks(Track &track)
156 /* Must collect the blocks in a set first while all tracks are still
157 guaranteed to have blocks and to avoid duplicate deletes */
158 set<Block *> del_blocks;
160 del_blocks.insert(&track.get_block());
162 const vector<Track *> &links = track.get_links();
163 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
165 del_blocks.insert(&(*i)->get_block());
167 for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
173 void Layout::remove_block(Block &b)
178 void Layout::add_route(Route &r)
180 if(routes.insert(&r).second)
181 signal_route_added.emit(r);
184 Route &Layout::get_route(const string &name) const
186 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
187 if((*i)->get_name()==name)
189 throw KeyError("Unknown route", name);
192 void Layout::update_routes()
194 for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
195 (*i)->update_turnouts();
198 void Layout::remove_route(Route &r)
201 signal_route_removed.emit(r);
204 void Layout::add_zone(Zone &z)
206 if(zones.insert(&z).second)
207 signal_zone_added.emit(z);
210 Layout::ZoneArray Layout::get_zones(const string &group) const
213 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
214 if((*i)->get_group()==group)
215 result.push_back(*i);
217 sort(result.begin(), result.end(), zone_order);
222 Zone &Layout::get_zone(const string &group, unsigned num) const
224 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
225 if((*i)->get_group()==group && (*i)->get_number()==num)
228 throw KeyError("Unknown zone", format("%s %d", group, num));
231 void Layout::remove_zone(Zone &z)
234 signal_zone_removed.emit(z);
237 void Layout::add_train(Train &t)
239 if(trains.count(t.get_address()))
240 throw KeyError("Duplicate train address", lexical_cast(t.get_address()));
242 trains[t.get_address()] = &t;
243 signal_train_added.emit(t);
246 Train &Layout::get_train(unsigned addr) const
248 map<unsigned, Train *>::const_iterator i = trains.find(addr);
250 throw KeyError("Unknown train", lexical_cast(addr));
254 void Layout::remove_train(Train &t)
256 if(trains.erase(t.get_address()))
257 signal_train_removed.emit(t);
260 void Layout::add_vehicle(Vehicle &v)
262 if(vehicles.insert(&v).second)
263 signal_vehicle_added.emit(v);
266 void Layout::remove_vehicle(Vehicle &v)
268 if(vehicles.erase(&v))
269 signal_vehicle_removed.emit(v);
277 Time::TimeStamp t = Time::now();
283 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
285 for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
286 i->second->tick(t, dt);
289 void Layout::emergency(const string &msg)
293 IO::print("Emergency: %s\n", msg);
294 signal_emergency.emit(msg);
297 void Layout::save(const string &fn) const
299 IO::BufferedFile out(fn, IO::M_WRITE);
300 DataFile::Writer writer(out);
303 writer.write((DataFile::Statement("base"), base));
305 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
307 DataFile::Statement st("track");
308 st.append((*i)->get_type().get_article_number());
313 for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
315 if((*i)->is_temporary())
318 DataFile::Statement st("route");
323 for(ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
325 DataFile::Statement st("zone");
331 void Layout::save_dynamic(const string &fn) const
333 IO::BufferedFile out(fn, IO::M_WRITE);
334 DataFile::Writer writer(out);
336 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
337 if(unsigned tid = (*i)->get_turnout_id())
338 writer.write((DataFile::Statement("turnout"), tid, (*i)->get_active_path()));
340 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
342 DataFile::Statement st("train");
343 st.append(i->second->get_locomotive_type().get_article_number());
344 st.append(i->second->get_address());
345 st.append(i->second->get_protocol());
346 i->second->save(st.sub);
351 void Layout::block_state_changed(Block &block, Block::State state)
353 if(state==Block::ACTIVE && !block.get_train())
354 emergency(format("Unreserved sensor %d triggered", block.get_sensor_id()));
358 Layout::Loader::Loader(Layout &l):
359 DataFile::BasicLoader<Layout>(l),
362 add("base", &Layout::base);
363 add("route", static_cast<void (Loader::*)()>(&Loader::route));
364 add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
365 add("train", static_cast<void (Loader::*)(ArticleNumber, unsigned, const std::string &)>(&Loader::train));
366 add("turnout", &Loader::turnout);
367 add("zone", &Loader::zone);
369 // Deprecated aliases
370 add("route", static_cast<void (Loader::*)(const string &)>(&Loader::route));
371 add("track", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
372 add("train", static_cast<void (Loader::*)(unsigned, unsigned, const std::string &)>(&Loader::train));
375 void Layout::Loader::finish()
377 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
381 void Layout::Loader::route()
383 Route *rte = new Route(obj);
387 void Layout::Loader::route(const string &n)
389 Route *rte = new Route(obj);
394 void Layout::Loader::track(unsigned art_nr)
396 track(ArticleNumber(art_nr));
399 void Layout::Loader::track(ArticleNumber art_nr)
401 Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
404 for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
406 trk->snap_to(**i, true);
409 void Layout::Loader::train(unsigned art_nr, unsigned addr, const std::string &proto)
411 train(ArticleNumber(art_nr), addr, proto);
414 void Layout::Loader::train(ArticleNumber art_nr, unsigned addr, const std::string &proto)
416 Train *trn = new Train(obj, obj.catalogue.get_vehicle(art_nr), addr, proto);
420 void Layout::Loader::turnout(unsigned addr, unsigned path)
423 obj.driver->set_turnout(addr, path);
426 void Layout::Loader::zone()
428 Zone *zne = new Zone(obj);