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