]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/block.cpp
Make Route hold non-const Tracks to match Block
[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 int Block::get_endpoint_by_link(Block &other) const
76 {
77         for(unsigned i=0; i<endpoints.size(); ++i)
78                 if(endpoints[i].link==&other)
79                         return i;
80
81         return -1;
82 }
83
84 unsigned Block::traverse(unsigned epi, float *len) const
85 {
86         return traverse(epi, 0, len);
87 }
88
89 unsigned Block::traverse(unsigned epi, const Route *route, float *len) const
90 {
91         if(epi>=endpoints.size())
92                 throw InvalidParameterValue("Endpoint index out of range");
93
94         const Endpoint &ep = endpoints[epi];
95         Track *track = ep.track;
96         unsigned track_ep = ep.track_ep;
97
98         if(len)
99                 *len = 0;
100
101         while(1)
102         {
103                 int cur_path = -1;
104                 if(track->get_turnout_id() && route)
105                         cur_path = route->get_turnout(track->get_turnout_id());
106                 if(cur_path==-1)
107                         cur_path = track->get_active_path();
108
109                 if(len)
110                         *len += track->get_type().get_path_length(cur_path);
111
112                 unsigned other_ep = track->traverse(track_ep, cur_path);
113                 for(unsigned i=0; i<endpoints.size(); ++i)
114                         if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
115                                 return i;
116
117                 Track *next = track->get_link(other_ep);
118                 if(!tracks.count(next))
119                         throw LogicError("Block traversal strayed out of the block");
120                 track_ep = next->get_endpoint_by_link(*track);
121                 track = next;
122         }
123 }
124
125 void Block::check_link(Block &other)
126 {
127         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
128         {
129                 if(i->link)
130                         continue;
131
132                 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
133                         if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
134                         {
135                                 i->link = &other;
136                                 j->link = this;
137
138                                 determine_id();
139                                 other.determine_id();
140                         }
141         }
142 }
143
144 void Block::break_link(Block &other)
145 {
146         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
147                 if(i->link==&other)
148                 {
149                         i->link = 0;
150                         other.break_link(*this);
151                         determine_id();
152                 }
153 }
154
155 Block *Block::get_link(unsigned epi) const
156 {
157         if(epi>=endpoints.size())
158                 throw InvalidParameterValue("Endpoint index out of range");
159         return endpoints[epi].link;
160 }
161
162 bool Block::reserve(Train *t)
163 {
164         if(!t || !train)
165         {
166                 train = t;
167                 layout.signal_block_reserved.emit(*this, train);
168                 return true;
169         }
170         else
171                 return false;
172 }
173
174 void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
175 {
176         visited.insert(&track);
177
178         const vector<Marklin::Endpoint> &eps = track.get_type().get_endpoints();
179         for(unsigned i=0; i<eps.size(); ++i)
180         {
181                 if(i==track_ep) continue;
182                 Track *link = track.get_link(i);
183                 if(!link) continue;
184                 if(visited.count(link)) continue;
185                 if(!(eps[i].paths&eps[track_ep].paths)) continue;
186
187                 if(tracks.count(link))
188                         find_paths(*link, link->get_endpoint_by_link(track), path, visited);
189                 else
190                 {
191                         for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
192                                 if(j->track==&track && j->track_ep==i)
193                                         j->paths |= path;
194                 }
195         }
196 }
197
198 void Block::determine_id()
199 {
200         if(sensor_id)
201                 id = 0x1000|sensor_id;
202         else if(turnout_id)
203                 id = 0x2000|turnout_id;
204         else if(endpoints.size()==2)
205         {
206                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
207                 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
208                 if(id2<id1)
209                         swap(id1, id2);
210                 id = (id1<<16)|id2;
211         }
212         else if(endpoints.size()==1)
213         {
214                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
215                 id = 0x10000 | id1;
216         }
217 }
218
219
220 Block::Endpoint::Endpoint(Track *t, unsigned e):
221         track(t),
222         track_ep(e),
223         link(0),
224         paths(0)
225 { }
226
227 } // namespace Marklin