]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/blockallocator.cpp
Split block allocation to a separate class
[r2c2.git] / source / libr2c2 / blockallocator.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/core/raii.h>
3 #include "blockallocator.h"
4 #include "catalogue.h"
5 #include "driver.h"
6 #include "layout.h"
7 #include "trackiter.h"
8 #include "train.h"
9 #include "vehicle.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace R2C2 {
15
16 BlockAllocator::BlockAllocator(Train &t):
17         train(t),
18         cur_blocks_end(blocks.end()),
19         pending_block(0),
20         stop_at_block(0),
21         reserving(false)
22 {
23         Layout &layout = train.get_layout();
24         layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
25         layout.signal_block_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::block_state_changed));
26
27         const set<Track *> &tracks = layout.get_tracks();
28         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
29                 if((*i)->get_turnout_id())
30                         (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
31 }
32
33 void BlockAllocator::start_from(Block &block, unsigned entry)
34 {
35         release_blocks(blocks.begin(), blocks.end());
36
37         blocks.push_back(BlockIter(&block, entry));
38         if(!block.reserve(&train))
39         {
40                 blocks.pop_back();
41                 return;
42         }
43 }
44
45 void BlockAllocator::clear()
46 {
47         release_blocks(blocks.begin(), blocks.end());
48         pending_block = 0;
49         stop_at_block = 0;
50 }
51
52 void BlockAllocator::stop_at(const Block *block)
53 {
54         stop_at_block = block;
55 }
56
57 const BlockIter &BlockAllocator::first() const
58 {
59         if(blocks.empty())
60                 throw logic_error("no blocks");
61         return blocks.front();
62 }
63
64 const BlockIter &BlockAllocator::last() const
65 {
66         if(blocks.empty())
67                 throw logic_error("no blocks");
68         BlockList::const_iterator i = --blocks.end();
69         if(i->block()==pending_block)
70                 --i;
71         return *i;
72 }
73
74 const BlockIter &BlockAllocator::last_current() const
75 {
76         if(blocks.empty())
77                 throw logic_error("no blocks");
78         if(cur_blocks_end==blocks.begin())
79                 throw logic_error("internal error (no current blocks)");
80         BlockList::const_iterator i = cur_blocks_end;
81         return *--i;
82 }
83
84 int BlockAllocator::get_entry_to_block(const Block &block) const
85 {
86         for(BlockList::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
87                 if(i->block()==&block)
88                         return i->entry();
89         return -1;
90 }
91
92 void BlockAllocator::reserve_more()
93 {
94         if(blocks.empty())
95                 throw logic_error("no blocks");
96
97         BlockIter start = blocks.back();
98         if(&*start==stop_at_block)
99                 return;
100         else if(&*start==pending_block)
101         {
102                 TrackIter track = start.track_iter();
103                 if(!track.endpoint().has_path(track->get_active_path()))
104                         return;
105         }
106
107         pending_block = 0;
108
109         // See how many sensor blocks and how much track we already have
110         unsigned nsens = 0;
111         float dist = 0;
112         for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
113         {
114                 if((*i)->get_sensor_id())
115                         ++nsens;
116                 if(nsens>0)
117                         dist += (*i)->get_path_length(i->entry());
118         }
119
120         float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
121         float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
122
123         BlockIter block = start;
124
125         SetFlag setf(reserving);
126
127         while(1)
128         {
129                 BlockIter prev = block;
130                 block = block.next();
131                 if(!block || block->get_endpoints().size()<2)
132                         // The track ends here
133                         break;
134
135                 if(block->get_turnout_id() && !prev->get_turnout_id())
136                 {
137                         /* We are arriving at a turnout.  See if we have enough blocks and
138                         distance reserved. */
139                         if(nsens>=3 && dist>=min_dist)
140                                 break;
141                 }
142
143                 blocks.push_back(block);
144                 if(!block->reserve(&train))
145                 {
146                         blocks.pop_back();
147                         pending_block = &*block;
148                         break;
149                 }
150
151                 if(cur_blocks_end==blocks.end())
152                         --cur_blocks_end;
153
154                 TrackIter track = block.track_iter();
155                 if(track->is_path_changing())
156                 {
157                         pending_block = &*block;
158                         break;
159                 }
160                 else
161                 {
162                         const TrackType::Endpoint &entry_ep = track.endpoint();
163                         unsigned path = track->get_active_path();
164                         if(!entry_ep.has_path(path))
165                         {
166                                 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
167                                 if(entry_ep.has_common_paths(exit_ep))
168                                 {
169                                         unsigned mask = entry_ep.paths&exit_ep.paths;
170                                         for(path=0; mask>1; ++path, mask>>=1) ;
171
172                                         track->set_active_path(path);
173                                         if(track->is_path_changing())
174                                         {
175                                                 pending_block = &*block;
176                                                 break;
177                                         }
178                                 }
179                                 else
180                                         // XXX Do something here
181                                         break;
182                         }
183                 }
184
185                 if(&*block==stop_at_block)
186                         break;
187
188                 if(block->get_sensor_id())
189                         ++nsens;
190                 if(nsens>0)
191                         dist += block->get_path_length(block.entry());
192         }
193
194         // Make any sensorless blocks at the beginning immediately current
195         while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id())
196                 ++cur_blocks_end;
197 }
198
199 void BlockAllocator::release_until(const Block &block)
200 {
201         for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
202                 if(i->block()==&block)
203                 {
204                         if(++i!=cur_blocks_end)
205                                 release_blocks(blocks.begin(), i);
206                         return;
207                 }
208 }
209
210 bool BlockAllocator::release_from(const Block &block)
211 {
212         bool have_sensor = false;
213         for(BlockList::iterator i=cur_blocks_end; i!=blocks.end(); ++i)
214         {
215                 if(i->block()==&block)
216                 {
217                         if(have_sensor)
218                                 release_blocks(i, blocks.end());
219                         return have_sensor;
220                 }
221                 else if((*i)->get_sensor_id())
222                         have_sensor = true;
223         }
224
225         return false;
226 }
227
228 void BlockAllocator::release_noncurrent()
229 {
230         release_blocks(cur_blocks_end, blocks.end());
231 }
232
233 void BlockAllocator::release_blocks(const BlockList::iterator &b, const BlockList::iterator &e)
234 {
235         for(BlockList::iterator i=b; i!=e; )
236         {
237                 if(cur_blocks_end==i)
238                         cur_blocks_end = e;
239
240                 Block &block = **i;
241                 blocks.erase(i++);
242                 block.reserve(0);
243         }
244 }
245
246 void BlockAllocator::reverse()
247 {
248         release_noncurrent();
249         blocks.reverse();
250         for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
251                 *i = i->reverse();
252 }
253
254 void BlockAllocator::turnout_path_changed(Track &track)
255 {
256         for(list<BlockIter>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
257                 if((*i)->get_turnout_id()==track.get_turnout_id() && !reserving && &**i==pending_block)
258                         reserve_more();
259 }
260
261 void BlockAllocator::block_reserved(Block &block, const Train *tr)
262 {
263         if(&block==pending_block && !tr && !reserving)
264                 reserve_more();
265 }
266
267 void BlockAllocator::block_state_changed(Block &block, Block::State state)
268 {
269         if(state==Block::MAYBE_ACTIVE)
270         {
271                 // Find the first sensor block from our reserved blocks that isn't this sensor
272                 BlockList::iterator end;
273                 unsigned result = 0;
274                 for(end=cur_blocks_end; end!=blocks.end(); ++end)
275                         if((*end)->get_sensor_id())
276                         {
277                                 if(&**end!=&block)
278                                 {
279                                         if(result==0)
280                                                 result = 2;
281                                         else if(result==1)
282                                                 break;
283                                 }
284                                 else if(result==0)
285                                         result = 1;
286                                 else if(result==2)
287                                         result = 3;
288                         }
289
290                 if(result==1)
291                 {
292                         // Move blocks up to the next sensor to our current blocks
293                         for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
294                                 train.signal_advanced.emit(**j);
295                         cur_blocks_end = end;
296                 }
297                 else if(result==3)
298                         train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
299         }
300         else if(state==Block::INACTIVE)
301         {
302                 const Vehicle &veh = train.get_controller().get_reverse() ? train.get_vehicle(0) : train.get_vehicle(train.get_n_vehicles()-1);
303
304                 // Find the first sensor in our current blocks that's still active
305                 BlockList::iterator end = blocks.begin();
306                 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
307                 {
308                         if((*i)->has_track(*veh.get_track()))
309                                 break;
310                         if((*i)->get_sensor_id())
311                         {
312                                 if(train.get_layout().get_driver().get_sensor((*i)->get_sensor_id()))
313                                         break;
314                                 else
315                                 {
316                                         end = i;
317                                         ++end;
318                                 }
319                         }
320                 }
321                 
322                 if(end!=blocks.begin() && end!=cur_blocks_end)
323                         // Free blocks up to the last inactive sensor
324                         release_blocks(blocks.begin(), end);
325         }
326 }
327
328 void BlockAllocator::save(list<DataFile::Statement> &st) const
329 {
330         if(!blocks.empty() && cur_blocks_end!=blocks.begin())
331         {
332                 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
333                 BlockIter prev;
334                 if(train.get_controller().get_reverse())
335                 {
336                         cur_blocks.reverse();
337                         prev = cur_blocks.front().next();
338                 }
339                 else
340                         prev = cur_blocks.front().flip();
341
342                 st.push_back((DataFile::Statement("hint"), prev->get_id()));
343
344                 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
345                         st.push_back((DataFile::Statement("block"), (*i)->get_id()));
346         }
347 }
348
349
350 BlockAllocator::Loader::Loader(BlockAllocator &ba):
351         DataFile::ObjectLoader<BlockAllocator>(ba),
352         valid(true)
353 {
354         add("block", &Loader::block);
355         add("hint",  &Loader::hint);
356 }
357
358 void BlockAllocator::Loader::block(unsigned id)
359 {
360         if(!valid)
361                 return;
362
363         Block *blk;
364         try
365         {
366                 blk = &obj.train.get_layout().get_block(id);
367         }
368         catch(const key_error &)
369         {
370                 valid = false;
371                 return;
372         }
373
374         int entry = -1;
375         if(prev_block)
376                 entry = blk->get_endpoint_by_link(*prev_block);
377         if(entry<0)
378                 entry = 0;
379
380         obj.blocks.push_back(BlockIter(blk, entry));
381         blk->reserve(&obj.train);
382
383         if(blk->get_sensor_id())
384                 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
385
386         prev_block = blk;
387 }
388
389 void BlockAllocator::Loader::hint(unsigned id)
390 {
391         try
392         {
393                 prev_block = &obj.train.get_layout().get_block(id);
394         }
395         catch(const key_error &)
396         {
397                 valid = false;
398         }
399 }
400
401 } // namespace R2C2