]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/block.cpp
a29469c4fd9b97e32751987e3c42ac6f903b078e
[r2c2.git] / source / libmarklin / block.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
9 #include "control.h"
10 #include "block.h"
11 #include "tracktype.h"
12 #include "trafficmanager.h"
13 #include "turnout.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 namespace Marklin {
19
20 Block::Block(TrafficManager &tm, Track &start):
21         trfc_mgr(tm),
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
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                                 }
46                                 else
47                                         endpoints.push_back(Endpoint(track, i));
48                         }
49         }
50
51         if(sensor_id)
52                 id = 0x1000|sensor_id;
53         else if(turnout_id)
54                 id = 0x2000|turnout_id;
55
56         for(unsigned i=0; i<endpoints.size(); ++i)
57         {
58                 unsigned path = 1<<i;
59                 endpoints[i].paths |= path;
60                 set<Track *> visited;
61                 find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited);
62         }
63 }
64
65 int Block::get_endpoint_by_link(const Block &other) const
66 {
67         for(unsigned i=0; i<endpoints.size(); ++i)
68                 if(endpoints[i].link==&other)
69                         return i;
70
71         return -1;
72 }
73
74 unsigned Block::traverse(unsigned epi, float *len) const
75 {
76         if(epi>=endpoints.size())
77                 throw InvalidParameterValue("Endpoint index out of range");
78
79         const Endpoint &ep = endpoints[epi];
80         Track *track = ep.track;
81         unsigned track_ep = ep.track_ep;
82
83         if(len)
84                 *len = 0;
85
86         while(1)
87         {
88                 unsigned cur_path = 0;
89                 unsigned tid = track->get_turnout_id();
90                 if(tid)
91                 {
92                         Turnout &turnout = trfc_mgr.get_control().get_turnout(tid);
93                         cur_path = turnout.get_path();
94                 }
95
96                 if(len)
97                         *len += track->get_type().get_path_length(cur_path);
98
99                 unsigned other_ep = track->traverse(track_ep, cur_path);
100                 for(unsigned i=0; i<endpoints.size(); ++i)
101                         if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
102                                 return i;
103
104                 Track *next = track->get_link(other_ep);
105                 if(!tracks.count(next))
106                         throw LogicError("Block traversal strayed out of the block");
107                 track_ep = next->get_endpoint_by_link(*track);
108                 track = next;
109         }
110 }
111
112 void Block::check_link(Block &other)
113 {
114         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
115         {
116                 if(i->link)
117                         continue;
118
119                 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
120                         if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
121                         {
122                                 i->link = &other;
123                                 j->link = this;
124                         }
125         }
126
127         if(!sensor_id && !turnout_id && endpoints.size()==2)
128         {
129                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
130                 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
131                 if(id2<id1)
132                         swap(id1, id2);
133                 id = (id1<<16)|id2;
134         }
135 }
136
137 Block *Block::get_link(unsigned epi) const
138 {
139         if(epi>=endpoints.size())
140                 throw InvalidParameterValue("Endpoint index out of range");
141         return endpoints[epi].link;
142 }
143
144 bool Block::reserve(const Train *t)
145 {
146         if(!t || !train)
147         {
148                 train = t;
149                 trfc_mgr.signal_block_reserved.emit(*this, train);
150                 return true;
151         }
152         else
153                 return false;
154 }
155
156 void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
157 {
158         visited.insert(&track);
159
160         const vector<Marklin::Endpoint> &eps = track.get_type().get_endpoints();
161         for(unsigned i=0; i<eps.size(); ++i)
162         {
163                 if(i==track_ep) continue;
164                 Track *link = track.get_link(i);
165                 if(!link) continue;
166                 if(visited.count(link)) continue;
167                 if(!(eps[i].paths&eps[track_ep].paths)) continue;
168
169                 if(tracks.count(link))
170                         find_paths(*link, link->get_endpoint_by_link(track), path, visited);
171                 else
172                 {
173                         for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
174                                 if(j->track==&track && j->track_ep==i)
175                                         j->paths |= path;
176                 }
177         }
178 }
179
180
181 Block::Endpoint::Endpoint(Track *t, unsigned e):
182         track(t),
183         track_ep(e),
184         link(0),
185         paths(0)
186 { }
187
188 } // namespace Marklin