]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/layout.cpp
Major architecture rework
[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/time/utils.h>
13 #include "block.h"
14 #include "catalogue.h"
15 #include "driver.h"
16 #include "layout.h"
17 #include "locotype.h"
18 #include "route.h"
19 #include "track.h"
20 #include "tracktype.h"
21 #include "train.h"
22
23 using namespace std;
24 using namespace Msp;
25
26 namespace Marklin {
27
28 Layout::Layout(Catalogue &c, Driver *d):
29         catalogue(c),
30         driver(d)
31 { }
32
33 Layout::~Layout()
34 {
35         delete driver;
36         while(!trains.empty())
37                 delete trains.begin()->second;
38         while(!routes.empty())
39                 delete routes.begin()->second;
40         while(!tracks.empty())
41                 delete *tracks.begin();
42         while(!blocks.empty())
43                 delete *blocks.begin();
44 }
45
46 Driver &Layout::get_driver() const
47 {
48         if(!driver)
49                 throw InvalidState("No driver");
50         return *driver;
51 }
52
53 void Layout::add_track(Track &t)
54 {
55         if(tracks.insert(&t).second)
56         {
57                 create_blocks();
58                 signal_track_added.emit(t);
59         }
60 }
61
62 void Layout::remove_track(Track &t)
63 {
64         if(tracks.erase(&t))
65         {
66                 create_blocks(t);
67                 signal_track_removed.emit(t);
68         }
69 }
70
71 void Layout::add_block(Block &b)
72 {
73         blocks.insert(&b);
74 }
75
76 Block &Layout::get_block(unsigned id) const
77 {
78         for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
79                 if((*i)->get_id()==id)
80                         return **i;
81
82         throw KeyError("Unknown block", lexical_cast(id));
83 }
84
85 Block &Layout::get_block_by_track(const Track &t) const
86 {
87         for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
88                 if((*i)->get_tracks().count(const_cast<Track *>(&t)))
89                         return **i;
90
91         throw InvalidParameterValue("No block found for track");
92 }
93
94 void Layout::create_blocks()
95 {
96         set<Track *> used_tracks;
97         for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
98         {
99                 const set<Track *> &btracks = (*i)->get_tracks();
100                 used_tracks.insert(btracks.begin(), btracks.end());
101         }
102
103         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
104                 if(used_tracks.count(*i)==0)
105                 {
106                         Block *block = new Block(*this, **i);
107                         used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
108                 }
109
110         for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
111                 for(set<Block *>::iterator j=i; j!=blocks.end(); ++j)
112                         if(j!=i)
113                                 (*i)->check_link(**j);
114 }
115
116 void Layout::create_blocks(const Track &track)
117 {
118         const vector<Track *> &links = track.get_links();
119         for(set<Block *>::iterator i=blocks.begin(); i!=blocks.end();)
120         {
121                 bool del = (*i)->get_tracks().count(const_cast<Track *>(&track));
122                 for(vector<Track *>::const_iterator j=links.begin(); (!del && j!=links.end()); ++j)
123                         del = (*i)->get_tracks().count(*j);
124
125                 if(del)
126                         delete *i++;
127                 else
128                         ++i;
129         }
130
131         create_blocks();
132 }
133
134 void Layout::remove_block(Block &b)
135 {
136         blocks.erase(&b);
137 }
138
139 void Layout::add_route(Route &r)
140 {
141         if(routes.count(r.get_name()))
142                 throw KeyError("Duplicate route name", r.get_name());
143
144         routes[r.get_name()] = &r;
145         signal_route_added.emit(r);
146 }
147
148 Route &Layout::get_route(const string &name) const
149 {
150         map<string, Route *>::const_iterator i = routes.find(name);
151         if(i==routes.end())
152                 throw KeyError("Unknown route", name);
153         return *i->second;
154 }
155
156 void Layout::remove_route(Route &r)
157 {
158         if(routes.erase(r.get_name()))
159                 signal_route_removed.emit(r);
160 }
161
162 void Layout::add_train(Train &t)
163 {
164         if(trains.count(t.get_address()))
165                 throw KeyError("Duplicate train address", lexical_cast(t.get_address()));
166
167         trains[t.get_address()] = &t;
168         signal_train_added.emit(t);
169 }
170
171 Train &Layout::get_train(unsigned addr) const
172 {
173         map<unsigned, Train *>::const_iterator i = trains.find(addr);
174         if(i==trains.end())
175                 throw KeyError("Unknown train", lexical_cast(addr));
176         return *i->second;
177 }
178
179 void Layout::remove_train(Train &t)
180 {
181         if(trains.erase(t.get_address()))
182                 signal_train_removed.emit(t);
183 }
184
185 void Layout::tick()
186 {
187         if(driver)
188                 driver->tick();
189
190         Time::TimeStamp t = Time::now();
191         Time::TimeDelta dt;
192         if(last_tick)
193                 dt = t-last_tick;
194         last_tick = t;
195
196         for(map<unsigned, Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
197                 i->second->tick(t, dt);
198 }
199
200 void Layout::save(const string &fn)
201 {
202         IO::BufferedFile out(fn, IO::M_WRITE);
203         DataFile::Writer writer(out);
204
205         if(!base.empty())
206                 writer.write((DataFile::Statement("base"), base));
207
208         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
209         {
210                 DataFile::Statement st("track");
211                 st.append((*i)->get_type().get_article_number());
212                 (*i)->save(st.sub);
213                 writer.write(st);
214         }
215
216         for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
217         {
218                 DataFile::Statement st("route");
219                 st.append(i->first);
220                 i->second->save(st.sub);
221                 writer.write(st);
222         }
223 }
224
225 void Layout::save_trains(const string &fn)
226 {
227         IO::BufferedFile out(fn, IO::M_WRITE);
228         DataFile::Writer writer(out);
229
230         for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
231         {
232                 DataFile::Statement st("train");
233                 st.append(i->second->get_locomotive_type().get_article_number());
234                 st.append(i->second->get_address());
235                 i->second->save(st.sub);
236                 writer.write(st);
237         }
238 }
239
240 void Layout::check_links()
241 {
242         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
243                 (*i)->break_links();
244
245         list<Track *> flext;
246         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
247         {
248                 if((*i)->get_flex())
249                         flext.push_back(*i);
250                 else
251                 {
252                         for(set<Track *>::iterator j=i; j!=tracks.end(); ++j)
253                                 if(j!=i)
254                                         (*i)->snap_to(**j, true);
255                 }
256         }
257
258         for(list<Track *>::iterator i=flext.begin(); i!=flext.end(); ++i)
259                 for(set<Track *>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
260                         if(*j!=*i)
261                                 (*i)->snap_to(**j, true);
262 }
263
264 void Layout::check_routes()
265 {
266         for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
267         {
268                 /* We must copy the turnout map, since adding tracks to the route will
269                 (temporarily) mess it up */
270                 const map<unsigned, int> turnouts = i->second->get_turnouts();
271
272                 // Find any turnout in the route
273                 Track *track = 0;
274                 unsigned trk_path = 0;
275                 for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
276                 {
277                         map<unsigned, int>::const_iterator k = turnouts.find((*j)->get_turnout_id());
278                         if(k!=turnouts.end())
279                         {
280                                 track = *j;
281                                 trk_path = k->second;
282                                 break;
283                         }
284                 }
285
286                 if(!track)
287                         continue;
288
289                 // Find an applicable endpoint
290                 const vector<Endpoint> &eps = track->get_type().get_endpoints();
291                 unsigned ep = 0;
292                 for(unsigned j=0; j<eps.size(); ++j)
293                         if(eps[j].paths&(1<<trk_path))
294                         {
295                                 ep = j;
296                                 break;
297                         }
298
299                 Track *start = 0;
300                 while(1)
301                 {
302                         // Traverse the track and get the next one
303                         unsigned out_ep = track->traverse(ep, trk_path);
304                         Track *next = track->get_links()[out_ep];
305                         if(!next || next == start)
306                                 break;
307
308                         ep = next->get_endpoint_by_link(*track);
309                         if(next->get_type().is_turnout())
310                         {
311                                 // Select correct path across the turnout, or break if we hit an unknown turnout
312                                 map<unsigned, int>::const_iterator j = turnouts.find(next->get_turnout_id());
313                                 if(j==turnouts.end())
314                                         break;
315                                 trk_path = j->second;
316                         }
317                         else
318                         {
319                                 trk_path = 0;
320
321                                 /* Start adding tracks when we find the first non-turnout.  This
322                                 prevents the occurrence of ambiguities while adding the tracks */
323                                 if(!start)
324                                         start = next;
325                         }
326
327                         if(start)
328                                 i->second->add_track(*next);
329
330                         track = next;
331                 }
332         }
333 }
334
335
336 Layout::Loader::Loader(Layout &l):
337         DataFile::BasicLoader<Layout>(l),
338         new_tracks(false)
339 {
340         add("base",  &Layout::base);
341         add("route", &Loader::route);
342         add("track", &Loader::track);
343         add("train", &Loader::train);
344 }
345
346 void Layout::Loader::finish()
347 {
348         if(new_tracks)
349                 obj.check_links();
350         obj.check_routes();
351
352         for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
353                 (*i)->check_slope();
354 }
355
356 void Layout::Loader::route(const string &n)
357 {
358         Route *rte = new Route(obj, n);
359         load_sub(*rte);
360 }
361
362 void Layout::Loader::track(unsigned art_nr)
363 {
364         Track *trk = new Track(obj, obj.catalogue.get_track(art_nr));
365         load_sub(*trk);
366         new_tracks = true;
367 }
368
369 void Layout::Loader::train(unsigned art_nr, unsigned addr)
370 {
371         Train *trn = new Train(obj, obj.catalogue.get_locomotive(art_nr), addr);
372         load_sub(*trn);
373 }
374
375 } // namespace Marklin