1 #include <msp/core/maputils.h>
2 #include <msp/core/raii.h>
3 #include "blockallocator.h"
8 #include "trackcircuit.h"
18 struct BlockAllocator::BlockMatch
22 BlockMatch(const Block &b): block(b) { }
24 bool operator()(const BlockIter &bi) const { return &*bi==█ }
28 BlockAllocator::BlockAllocator(Train &t):
30 cur_blocks_end(blocks.end()),
35 Layout &layout = train.get_layout();
36 layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
37 layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::sensor_state_changed));
39 const set<Track *> &tracks = layout.get_all<Track>();
40 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
41 if((*i)->get_turnout_id())
43 (*i)->signal_path_changing.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changing), sigc::ref(**i))));
44 (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
48 void BlockAllocator::set_active(bool a)
55 release_blocks_end(cur_blocks_end);
60 void BlockAllocator::start_from(const BlockIter &block)
63 throw invalid_argument("BlockAllocator::start_from");
69 void BlockAllocator::rewind_to(const Block &block)
74 BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(block));
77 release_blocks_end(i);
82 void BlockAllocator::clear()
85 release_blocks_begin(blocks.end());
90 void BlockAllocator::stop_at(const Block *block)
92 stop_at_block = block;
97 const BlockIter &BlockAllocator::first() const
100 throw logic_error("no blocks");
101 return blocks.front();
104 const BlockIter &BlockAllocator::last() const
107 throw logic_error("no blocks");
108 BlockList::const_iterator i = --blocks.end();
109 if(i->block()==pending_block)
114 const BlockIter &BlockAllocator::last_current() const
117 throw logic_error("no blocks");
118 if(cur_blocks_end==blocks.begin())
119 throw logic_error("internal error (no current blocks)");
120 BlockList::const_iterator i = cur_blocks_end;
124 const BlockIter &BlockAllocator::iter_for(const Block &block) const
126 BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(block));
128 throw key_error(&block);
132 bool BlockAllocator::has_block(const Block &block) const
134 return find_if(blocks.begin(), blocks.end(), BlockMatch(block))!=blocks.end();
137 bool BlockAllocator::is_block_current(const Block &block) const
139 BlockList::const_iterator end = cur_blocks_end;
140 return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end;
143 void BlockAllocator::reserve_more()
146 throw logic_error("no blocks");
148 BlockIter start = blocks.back();
149 if(&*start==stop_at_block)
151 else if(&*start==pending_block)
153 TrackIter track = start.track_iter();
154 if(!track.endpoint().has_path(track->get_active_path()))
160 // See how many sensor blocks and how much track we already have
163 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
165 if((*i)->get_sensor_id())
168 dist += (*i)->get_path_length(i->entry());
171 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
172 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
174 BlockIter block = start;
176 SetFlag setf(reserving);
180 BlockIter prev = block;
181 block = block.next();
182 if(!block || block->get_endpoints().size()<2)
183 // The track ends here
186 if(block->get_turnout_id() && !prev->get_turnout_id())
188 /* We are arriving at a turnout. See if we have enough blocks and
189 distance reserved. */
190 if(nsens>=3 && dist>=min_dist)
194 if(!reserve_block(block))
197 if(cur_blocks_end==blocks.end())
200 TrackIter track = block.track_iter();
201 if(track->is_path_changing())
203 pending_block = &*block;
208 const TrackType::Endpoint &entry_ep = track.endpoint();
209 unsigned path = track->get_active_path();
210 if(!entry_ep.has_path(path))
212 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
213 if(entry_ep.has_common_paths(exit_ep))
215 unsigned mask = entry_ep.paths&exit_ep.paths;
216 for(path=0; mask>1; ++path, mask>>=1) ;
218 track->set_active_path(path);
219 if(track->is_path_changing())
221 pending_block = &*block;
226 // XXX Do something here
231 if(&*block==stop_at_block)
234 if(block->get_sensor_id())
237 dist += block->get_path_length(block.entry());
240 // Make any sensorless blocks at the beginning immediately current
241 while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id())
245 bool BlockAllocator::reserve_block(const BlockIter &block)
247 /* Add it to the list first to present a consistent state in block_reserved
249 blocks.push_back(block);
252 if(!block->reserve(&train))
267 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
269 for(BlockList::iterator i=blocks.begin(); i!=end; )
273 void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
275 // Guard against decrementing blocks.begin()
276 if(begin==blocks.begin())
277 return release_blocks_begin(blocks.end());
279 if(begin==blocks.end())
282 /* Release the blocks in reverse order so that a consistent state is
283 presented in block_reserved signal. */
285 for(BlockList::iterator i=--blocks.end(); !done; )
292 void BlockAllocator::release_block(const BlockList::iterator &i)
294 if(i==cur_blocks_end)
296 if(&**i==pending_block)
304 void BlockAllocator::reverse()
306 release_blocks_end(cur_blocks_end);
308 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
315 void BlockAllocator::turnout_path_changing(Track &track)
317 BlockList::iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(track.get_block()));
321 release_blocks_end(i);
322 pending_block = &track.get_block();
326 void BlockAllocator::turnout_path_changed(Track &track)
328 if(&track.get_block()==pending_block && !reserving)
332 void BlockAllocator::block_reserved(Block &block, const Train *tr)
334 if(&block==pending_block && !tr && !reserving)
338 void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
341 if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
342 block = &tc->get_block();
346 if(block->get_train()!=&train)
349 if(state==Sensor::MAYBE_ACTIVE)
351 // Find the first sensor block from our reserved blocks that isn't this sensor
352 BlockList::iterator end;
354 for(end=cur_blocks_end; end!=blocks.end(); ++end)
355 if((*end)->get_sensor_id())
372 // Move blocks up to the next sensor to our current blocks
373 for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
374 train.signal_advanced.emit(**j);
375 cur_blocks_end = end;
381 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
383 else if(state==Sensor::INACTIVE)
385 const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1);
386 const Block &veh_block = veh.get_track()->get_block();
387 const Driver &driver = train.get_layout().get_driver();
389 /* Sensors aren't guaranteed to be detriggered in order. Go through the
390 block list and locate the first sensor that's still active. */
391 BlockList::iterator end = blocks.end();
392 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
394 // Avoid freeing blocks that still hold the train's vehicles
398 if((*i)->get_sensor_id())
400 if(driver.get_sensor((*i)->get_sensor_id()))
407 if(end!=blocks.end())
408 // Free blocks up to the last inactive sensor
409 release_blocks_begin(++end);
413 void BlockAllocator::save(list<DataFile::Statement> &st) const
415 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
417 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
419 if(train.get_controller().get_reverse())
421 cur_blocks.reverse();
422 prev = cur_blocks.front().next();
425 prev = cur_blocks.front().flip();
427 st.push_back((DataFile::Statement("hint"), prev->get_id()));
429 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
430 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
435 BlockAllocator::Loader::Loader(BlockAllocator &ba):
436 DataFile::ObjectLoader<BlockAllocator>(ba),
439 add("block", &Loader::block);
440 add("hint", &Loader::hint);
443 void BlockAllocator::Loader::block(unsigned id)
451 blk = &obj.train.get_layout().get_block(id);
453 catch(const key_error &)
461 entry = blk->get_endpoint_by_link(*prev_block);
465 obj.blocks.push_back(BlockIter(blk, entry));
466 blk->reserve(&obj.train);
468 if(blk->get_sensor_id())
469 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
474 void BlockAllocator::Loader::hint(unsigned id)
478 prev_block = &obj.train.get_layout().get_block(id);
480 catch(const key_error &)