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