]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/block.cpp
Rewrite Block::find_paths using TrackIter
[r2c2.git] / source / libmarklin / block.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 "block.h"
10 #include "layout.h"
11 #include "route.h"
12 #include "trackiter.h"
13 #include "tracktype.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 namespace Marklin {
19
20 Block::Block(Layout &l, Track &start):
21         layout(l),
22         id(0),
23         sensor_id(start.get_sensor_id()),
24         turnout_id(start.get_turnout_id()),
25         train(0)
26 {
27         tracks.insert(&start);
28         start.set_block(this);
29
30         list<Track *> queue;
31         queue.push_back(&start);
32
33         while(!queue.empty())
34         {
35                 Track *track = queue.front();
36                 queue.erase(queue.begin());
37
38                 const vector<Track *> &links = track->get_links();
39                 for(unsigned i=0; i<links.size(); ++i)
40                         if(links[i] && !tracks.count(links[i]))
41                         {
42                                 if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
43                                 {
44                                         queue.push_back(links[i]);
45                                         tracks.insert(links[i]);
46                                         links[i]->set_block(this);
47                                 }
48                                 else
49                                         endpoints.push_back(Endpoint(track, i));
50                         }
51         }
52
53         determine_id();
54
55         for(unsigned i=0; i<endpoints.size(); ++i)
56         {
57                 unsigned path = 1<<i;
58                 endpoints[i].paths |= path;
59                 find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
60         }
61
62         layout.add_block(*this);
63 }
64
65 Block::~Block()
66 {
67         set<Track *> trks = tracks;
68         tracks.clear();
69         for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
70                 (*i)->set_block(0);
71
72         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
73                 if(Block *blk = i->link)
74                 {
75                         i->link = 0;
76                         blk->break_link(*this);
77                 }
78
79         layout.remove_block(*this);
80 }
81
82 bool Block::has_track(Track &t) const
83 {
84         return tracks.count(&t);
85 }
86
87 int Block::get_endpoint_by_link(Block &other) const
88 {
89         for(unsigned i=0; i<endpoints.size(); ++i)
90                 if(endpoints[i].link==&other)
91                         return i;
92
93         return -1;
94 }
95
96 float Block::get_path_length(unsigned entry, const Route *route) const
97 {
98         if(entry>=endpoints.size())
99                 throw InvalidParameterValue("Endpoint index out of range");
100
101         TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep);
102
103         float result = 0;
104         while(t_iter && has_track(*t_iter))
105         {
106                 int path = -1;
107                 if(t_iter->get_turnout_id() && route)
108                         path = route->get_turnout(t_iter->get_turnout_id());
109                 if(path==-1)
110                         path = t_iter->get_active_path();
111
112                 result += t_iter->get_type().get_path_length(path);
113
114                 t_iter = t_iter.next(path);
115         }
116
117         return result;
118 }
119
120 void Block::check_link(Block &other)
121 {
122         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
123         {
124                 if(i->link)
125                         continue;
126
127                 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
128                         if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
129                         {
130                                 i->link = &other;
131                                 j->link = this;
132
133                                 determine_id();
134                                 other.determine_id();
135                         }
136         }
137 }
138
139 void Block::break_link(Block &other)
140 {
141         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
142                 if(i->link==&other)
143                 {
144                         i->link = 0;
145                         other.break_link(*this);
146                         determine_id();
147                 }
148 }
149
150 Block *Block::get_link(unsigned epi) const
151 {
152         if(epi>=endpoints.size())
153                 throw InvalidParameterValue("Endpoint index out of range");
154         return endpoints[epi].link;
155 }
156
157 bool Block::reserve(Train *t)
158 {
159         if(!t || !train)
160         {
161                 train = t;
162                 layout.signal_block_reserved.emit(*this, train);
163                 return true;
164         }
165         else
166                 return false;
167 }
168
169 void Block::find_paths(TrackIter track, unsigned path)
170 {
171         unsigned mask = track->get_type().get_endpoints()[track.entry()].paths;
172         for(unsigned i=0; mask>>i; ++i)
173                 if(mask&(1<<i))
174                 {
175                         TrackIter next = track.next(i);
176                         if(!next)
177                                 continue;
178                         else if(has_track(*next))
179                                 find_paths(track.next(i), path);
180                         else
181                         {
182                                 next = next.flip();
183                                 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
184                                         if(j->track==next.track() && j->track_ep==next.entry())
185                                                 j->paths |= path;
186                         }
187                 }
188 }
189
190 void Block::determine_id()
191 {
192         if(sensor_id)
193                 id = 0x1000|sensor_id;
194         else if(turnout_id)
195                 id = 0x2000|turnout_id;
196         else if(endpoints.size()==2)
197         {
198                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
199                 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
200                 if(id2<id1)
201                         swap(id1, id2);
202                 id = (id1<<16)|id2;
203         }
204         else if(endpoints.size()==1)
205         {
206                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
207                 id = 0x10000 | id1;
208         }
209 }
210
211
212 Block::Endpoint::Endpoint(Track *t, unsigned e):
213         track(t),
214         track_ep(e),
215         link(0),
216         paths(0)
217 { }
218
219 } // namespace Marklin