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