]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/layout.cpp
Maintain a Block pointer in Track
[r2c2.git] / source / libmarklin / layout.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
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>
14 #include "block.h"
15 #include "catalogue.h"
16 #include "driver.h"
17 #include "layout.h"
18 #include "route.h"
19 #include "track.h"
20 #include "tracktype.h"
21 #include "train.h"
22 #include "vehicletype.h"
23
24 using namespace std;
25 using namespace Msp;
26
27 namespace Marklin {
28
29 Layout::Layout(Catalogue &c, Driver *d):
30         catalogue(c),
31         driver(d),
32         next_turnout_id(0x800)
33 {
34         if(driver)
35                 driver->signal_sensor.connect(sigc::mem_fun(this, &Layout::sensor_event));
36 }
37
38 Layout::~Layout()
39 {
40         delete driver;
41         while(!trains.empty())
42                 delete trains.begin()->second;
43         while(!routes.empty())
44                 delete *routes.begin();
45         while(!tracks.empty())
46                 delete *tracks.begin();
47         while(!blocks.empty())
48                 delete *blocks.begin();
49 }
50
51 Driver &Layout::get_driver() const
52 {
53         if(!driver)
54                 throw InvalidState("No driver");
55         return *driver;
56 }
57
58 void Layout::add_track(Track &t)
59 {
60         if(tracks.insert(&t).second)
61         {
62                 create_blocks();
63                 signal_track_added.emit(t);
64         }
65 }
66
67 void Layout::remove_track(Track &t)
68 {
69         if(tracks.erase(&t))
70         {
71                 create_blocks(t);
72                 signal_track_removed.emit(t);
73         }
74 }
75
76 unsigned Layout::allocate_turnout_id(bool dbl)
77 {
78         unsigned result = next_turnout_id++;
79         if(dbl)
80                 ++next_turnout_id;
81         return result;
82 }
83
84 void Layout::add_block(Block &b)
85 {
86         blocks.insert(&b);
87 }
88
89 Block &Layout::get_block(unsigned id) const
90 {
91         for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
92                 if((*i)->get_id()==id)
93                         return **i;
94
95         throw KeyError("Unknown block", lexical_cast(id));
96 }
97
98 void Layout::create_blocks()
99 {
100         set<Track *> used_tracks;
101         for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
102         {
103                 const set<Track *> &btracks = (*i)->get_tracks();
104                 used_tracks.insert(btracks.begin(), btracks.end());
105         }
106
107         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
108                 if(used_tracks.count(*i)==0)
109                 {
110                         Block *block = new Block(*this, **i);
111                         used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
112                 }
113
114         for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
115                 for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
116                         if(j!=i)
117                                 (*i)->check_link(**j);
118 }
119
120 void Layout::create_blocks(Track &track)
121 {
122         /* Must collect the blocks in a set first while all tracks are still
123         guaranteed to have blocks and to avoid duplicate deletes */
124         set<Block *> del_blocks;
125
126         del_blocks.insert(&track.get_block());
127
128         const vector<Track *> &links = track.get_links();
129         for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
130                 if(*i)
131                         del_blocks.insert(&(*i)->get_block());
132
133         for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
134                 delete *i;
135
136         create_blocks();
137 }
138
139 void Layout::remove_block(Block &b)
140 {
141         blocks.erase(&b);
142 }
143
144 void Layout::add_route(Route &r)
145 {
146         if(routes.insert(&r).second)
147                 signal_route_added.emit(r);
148 }
149
150 Route &Layout::get_route(const string &name) const
151 {
152         for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
153                 if((*i)->get_name()==name)
154                         return **i;
155         throw KeyError("Unknown route", name);
156 }
157
158 void Layout::update_routes()
159 {
160         for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
161                 (*i)->update_turnouts();
162 }
163
164 void Layout::remove_route(Route &r)
165 {
166         if(routes.erase(&r))
167                 signal_route_removed.emit(r);
168 }
169
170 void Layout::add_train(Train &t)
171 {
172         if(trains.count(t.get_address()))
173                 throw KeyError("Duplicate train address", lexical_cast(t.get_address()));
174
175         trains[t.get_address()] = &t;
176         signal_train_added.emit(t);
177 }
178
179 Train &Layout::get_train(unsigned addr) const
180 {
181         map<unsigned, Train *>::const_iterator i = trains.find(addr);
182         if(i==trains.end())
183                 throw KeyError("Unknown train", lexical_cast(addr));
184         return *i->second;
185 }
186
187 void Layout::remove_train(Train &t)
188 {
189         if(trains.erase(t.get_address()))
190                 signal_train_removed.emit(t);
191 }
192
193 void Layout::add_vehicle(Vehicle &v)
194 {
195         if(vehicles.insert(&v).second)
196                 signal_vehicle_added.emit(v);
197 }
198
199 void Layout::remove_vehicle(Vehicle &v)
200 {
201         if(vehicles.erase(&v))
202                 signal_vehicle_removed.emit(v);
203 }
204
205 void Layout::tick()
206 {
207         if(driver)
208                 driver->tick();
209
210         Time::TimeStamp t = Time::now();
211         Time::TimeDelta dt;
212         if(last_tick)
213                 dt = t-last_tick;
214         last_tick = t;
215
216         for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
217                 i->second->tick(t, dt);
218 }
219
220 void Layout::emergency(const string &msg)
221 {
222         if(driver)
223                 driver->halt(true);
224         IO::print("Emergency: %s\n", msg);
225         signal_emergency.emit(msg);
226 }
227
228 void Layout::save(const string &fn)
229 {
230         IO::BufferedFile out(fn, IO::M_WRITE);
231         DataFile::Writer writer(out);
232
233         if(!base.empty())
234                 writer.write((DataFile::Statement("base"), base));
235
236         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
237         {
238                 DataFile::Statement st("track");
239                 st.append((*i)->get_type().get_article_number());
240                 (*i)->save(st.sub);
241                 writer.write(st);
242         }
243
244         for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
245         {
246                 if((*i)->is_temporary())
247                         continue;
248
249                 DataFile::Statement st("route");
250                 (*i)->save(st.sub);
251                 writer.write(st);
252         }
253 }
254
255 void Layout::save_trains(const string &fn)
256 {
257         IO::BufferedFile out(fn, IO::M_WRITE);
258         DataFile::Writer writer(out);
259
260         for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
261         {
262                 DataFile::Statement st("train");
263                 st.append(i->second->get_locomotive_type().get_article_number());
264                 st.append(i->second->get_address());
265                 i->second->save(st.sub);
266                 writer.write(st);
267         }
268 }
269
270 void Layout::sensor_event(unsigned addr, bool state)
271 {
272         if(state)
273         {
274                 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
275                         if((*i)->get_sensor_id()==addr)
276                         {
277                                 if(!(*i)->get_train())
278                                         emergency(format("Unreserved sensor %d triggered", addr));
279                                 break;
280                         }
281         }
282 }
283
284
285 Layout::Loader::Loader(Layout &l):
286         DataFile::BasicLoader<Layout>(l),
287         new_tracks(false)
288 {
289         add("base",  &Layout::base);
290         add("route", static_cast<void (Loader::*)()>(&Loader::route));
291         add("route", static_cast<void (Loader::*)(const string &)>(&Loader::route));
292         add("track", &Loader::track);
293         add("train", &Loader::train);
294 }
295
296 void Layout::Loader::finish()
297 {
298         for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
299                 (*i)->check_slope();
300 }
301
302 void Layout::Loader::route()
303 {
304         Route *rte = new Route(obj);
305         load_sub(*rte);
306 }
307
308 void Layout::Loader::route(const string &n)
309 {
310         Route *rte = new Route(obj);
311         rte->set_name(n);
312         load_sub(*rte);
313 }
314
315 void Layout::Loader::track(unsigned art_nr)
316 {
317         Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
318         load_sub(*trk);
319         new_tracks = true;
320         for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
321                 if(*i!=trk)
322                         trk->snap_to(**i, true);
323 }
324
325 void Layout::Loader::train(unsigned art_nr, unsigned addr)
326 {
327         Train *trn = new Train(obj, obj.catalogue.get_vehicle(art_nr), addr);
328         load_sub(*trn);
329 }
330
331 } // namespace Marklin