]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/layout.cpp
Add framework for generating simple meshes for vehicles
[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         set<unsigned> used_ids;
79         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
80                 if((*i)->get_turnout_id())
81                         used_ids.insert((*i)->get_turnout_id());
82
83         unsigned result = next_turnout_id;
84         while(used_ids.count(result) || (dbl && used_ids.count(result+1)))
85                 ++result;
86         next_turnout_id = result+1+dbl;
87
88         return result;
89 }
90
91 void Layout::add_block(Block &b)
92 {
93         blocks.insert(&b);
94 }
95
96 Block &Layout::get_block(unsigned id) const
97 {
98         for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
99                 if((*i)->get_id()==id)
100                         return **i;
101
102         throw KeyError("Unknown block", lexical_cast(id));
103 }
104
105 void Layout::create_blocks()
106 {
107         set<Track *> used_tracks;
108         for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
109         {
110                 const set<Track *> &btracks = (*i)->get_tracks();
111                 used_tracks.insert(btracks.begin(), btracks.end());
112         }
113
114         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
115                 if(used_tracks.count(*i)==0)
116                 {
117                         Block *block = new Block(*this, **i);
118                         used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
119                 }
120
121         for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
122                 for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
123                         if(j!=i)
124                                 (*i)->check_link(**j);
125 }
126
127 void Layout::create_blocks(Track &track)
128 {
129         /* Must collect the blocks in a set first while all tracks are still
130         guaranteed to have blocks and to avoid duplicate deletes */
131         set<Block *> del_blocks;
132
133         del_blocks.insert(&track.get_block());
134
135         const vector<Track *> &links = track.get_links();
136         for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
137                 if(*i)
138                         del_blocks.insert(&(*i)->get_block());
139
140         for(set<Block *>::iterator i=del_blocks.begin(); i!=del_blocks.end(); ++i)
141                 delete *i;
142
143         create_blocks();
144 }
145
146 void Layout::remove_block(Block &b)
147 {
148         blocks.erase(&b);
149 }
150
151 void Layout::add_route(Route &r)
152 {
153         if(routes.insert(&r).second)
154                 signal_route_added.emit(r);
155 }
156
157 Route &Layout::get_route(const string &name) const
158 {
159         for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
160                 if((*i)->get_name()==name)
161                         return **i;
162         throw KeyError("Unknown route", name);
163 }
164
165 void Layout::update_routes()
166 {
167         for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
168                 (*i)->update_turnouts();
169 }
170
171 void Layout::remove_route(Route &r)
172 {
173         if(routes.erase(&r))
174                 signal_route_removed.emit(r);
175 }
176
177 void Layout::add_train(Train &t)
178 {
179         if(trains.count(t.get_address()))
180                 throw KeyError("Duplicate train address", lexical_cast(t.get_address()));
181
182         trains[t.get_address()] = &t;
183         signal_train_added.emit(t);
184 }
185
186 Train &Layout::get_train(unsigned addr) const
187 {
188         map<unsigned, Train *>::const_iterator i = trains.find(addr);
189         if(i==trains.end())
190                 throw KeyError("Unknown train", lexical_cast(addr));
191         return *i->second;
192 }
193
194 void Layout::remove_train(Train &t)
195 {
196         if(trains.erase(t.get_address()))
197                 signal_train_removed.emit(t);
198 }
199
200 void Layout::add_vehicle(Vehicle &v)
201 {
202         if(vehicles.insert(&v).second)
203                 signal_vehicle_added.emit(v);
204 }
205
206 void Layout::remove_vehicle(Vehicle &v)
207 {
208         if(vehicles.erase(&v))
209                 signal_vehicle_removed.emit(v);
210 }
211
212 void Layout::tick()
213 {
214         if(driver)
215                 driver->tick();
216
217         Time::TimeStamp t = Time::now();
218         Time::TimeDelta dt;
219         if(last_tick)
220                 dt = t-last_tick;
221         last_tick = t;
222
223         for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
224                 i->second->tick(t, dt);
225 }
226
227 void Layout::emergency(const string &msg)
228 {
229         if(driver)
230                 driver->halt(true);
231         IO::print("Emergency: %s\n", msg);
232         signal_emergency.emit(msg);
233 }
234
235 void Layout::save(const string &fn)
236 {
237         IO::BufferedFile out(fn, IO::M_WRITE);
238         DataFile::Writer writer(out);
239
240         if(!base.empty())
241                 writer.write((DataFile::Statement("base"), base));
242
243         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
244         {
245                 DataFile::Statement st("track");
246                 st.append((*i)->get_type().get_article_number());
247                 (*i)->save(st.sub);
248                 writer.write(st);
249         }
250
251         for(set<Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
252         {
253                 if((*i)->is_temporary())
254                         continue;
255
256                 DataFile::Statement st("route");
257                 (*i)->save(st.sub);
258                 writer.write(st);
259         }
260 }
261
262 void Layout::save_trains(const string &fn)
263 {
264         IO::BufferedFile out(fn, IO::M_WRITE);
265         DataFile::Writer writer(out);
266
267         for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
268         {
269                 DataFile::Statement st("train");
270                 st.append(i->second->get_locomotive_type().get_article_number());
271                 st.append(i->second->get_address());
272                 i->second->save(st.sub);
273                 writer.write(st);
274         }
275 }
276
277 void Layout::sensor_event(unsigned addr, bool state)
278 {
279         if(state)
280         {
281                 for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
282                         if((*i)->get_sensor_id()==addr)
283                         {
284                                 if(!(*i)->get_train())
285                                         emergency(format("Unreserved sensor %d triggered", addr));
286                                 break;
287                         }
288         }
289 }
290
291
292 Layout::Loader::Loader(Layout &l):
293         DataFile::BasicLoader<Layout>(l),
294         new_tracks(false)
295 {
296         add("base",  &Layout::base);
297         add("route", static_cast<void (Loader::*)()>(&Loader::route));
298         add("route", static_cast<void (Loader::*)(const string &)>(&Loader::route));
299         add("track", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
300         add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
301         add("train", static_cast<void (Loader::*)(unsigned, unsigned)>(&Loader::train));
302         add("train", static_cast<void (Loader::*)(ArticleNumber, unsigned)>(&Loader::train));
303 }
304
305 void Layout::Loader::finish()
306 {
307         for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
308                 (*i)->check_slope();
309 }
310
311 void Layout::Loader::route()
312 {
313         Route *rte = new Route(obj);
314         load_sub(*rte);
315 }
316
317 void Layout::Loader::route(const string &n)
318 {
319         Route *rte = new Route(obj);
320         rte->set_name(n);
321         load_sub(*rte);
322 }
323
324 void Layout::Loader::track(unsigned art_nr)
325 {
326         track(ArticleNumber(art_nr));
327 }
328
329 void Layout::Loader::track(ArticleNumber art_nr)
330 {
331         Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
332         load_sub(*trk);
333         new_tracks = true;
334         for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
335                 if(*i!=trk)
336                         trk->snap_to(**i, true);
337 }
338
339 void Layout::Loader::train(unsigned art_nr, unsigned addr)
340 {
341         train(ArticleNumber(art_nr), addr);
342 }
343
344 void Layout::Loader::train(ArticleNumber art_nr, unsigned addr)
345 {
346         Train *trn = new Train(obj, obj.catalogue.get_vehicle(art_nr), addr);
347         load_sub(*trn);
348 }
349
350 } // namespace Marklin