1 #include <msp/core/maputils.h>
2 #include <msp/core/raii.h>
4 #include "blockallocator.h"
9 #include "trackcircuit.h"
10 #include "trackiter.h"
19 struct BlockAllocator::BlockMatch
23 BlockMatch(const Block &b): block(b) { }
25 bool operator()(const BlockIter &bi) const { return &*bi==█ }
29 BlockAllocator::BlockAllocator(Train &t):
32 cur_blocks_end(blocks.end()),
39 Layout &layout = train.get_layout();
40 layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
41 layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::sensor_state_changed));
42 layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &BlockAllocator::halt_event));
44 const set<Track *> &tracks = layout.get_all<Track>();
45 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
46 if((*i)->get_type().is_turnout())
48 (*i)->signal_path_changing.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changing), sigc::ref(**i))));
49 (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
53 void BlockAllocator::set_active(bool a)
60 release_blocks_end(cur_blocks_end);
65 bool BlockAllocator::start_from(const BlockIter &block)
68 throw invalid_argument("BlockAllocator::start_from");
70 float remaining_length = 0;
71 unsigned n_vehs = train.get_n_vehicles();
72 for(unsigned i=0; i<n_vehs; ++i)
73 remaining_length += train.get_vehicle(i).get_type().get_length();
77 BlockList blocks_to_reserve;
78 for(BlockIter b=block; b; b=b.next())
82 blocks_to_reserve.push_back(b);
83 for(TrackIter t=b.track_iter(); (t && &t->get_block()==&*b); t=t.next())
84 remaining_length -= t->get_path_length();
85 if(remaining_length<=0)
89 if(remaining_length>0)
92 for(BlockList::iterator i=blocks_to_reserve.begin(); i!=blocks_to_reserve.end(); ++i)
97 (*i)->reserve(&train);
102 while(i!=blocks_to_reserve.begin())
114 void BlockAllocator::rewind_to(const Block &block)
119 BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(block));
122 release_blocks_end(i);
127 void BlockAllocator::clear()
129 release_blocks_begin(blocks.end());
136 void BlockAllocator::stop_at(const Block *block)
138 stop_at_block = block;
143 const BlockIter &BlockAllocator::first() const
146 throw logic_error("no blocks");
147 return blocks.front();
150 const BlockIter &BlockAllocator::last() const
153 throw logic_error("no blocks");
154 BlockList::const_iterator i = --blocks.end();
155 if(i->block()==pending_block)
160 const BlockIter &BlockAllocator::last_current() const
163 throw logic_error("no blocks");
164 if(cur_blocks_end==blocks.begin())
165 throw logic_error("internal error (no current blocks)");
166 BlockList::const_iterator i = cur_blocks_end;
170 const BlockIter &BlockAllocator::iter_for(const Block &block) const
172 BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(block));
174 throw key_error(&block);
178 bool BlockAllocator::has_block(const Block &block) const
180 return find_if(blocks.begin(), blocks.end(), BlockMatch(block))!=blocks.end();
183 bool BlockAllocator::is_block_current(const Block &block) const
185 BlockList::const_iterator end = cur_blocks_end;
186 return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end;
189 void BlockAllocator::reserve_more()
192 throw logic_error("no blocks");
194 if(train.get_layout().get_driver().is_halted())
197 BlockIter start = blocks.back();
198 if(&*start==stop_at_block)
200 else if(&*start==pending_block)
202 TrackIter track = start.track_iter();
203 if(track->is_path_changing() || !track.endpoint().has_path(track->get_active_path()))
209 // See how many sensor blocks and how much track we already have
212 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
214 if((*i)->get_sensor_address())
217 dist += (*i)->get_path_length(i->entry());
220 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
221 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
223 BlockIter block = start;
225 SetFlag setf(reserving);
229 BlockIter prev = block;
230 block = block.next();
231 if(!block || block->get_endpoints().size()<2)
232 // The track ends here
235 if(block->get_turnout_address() && !prev->get_turnout_address())
237 /* We are arriving at a turnout. See if we have enough blocks and
238 distance reserved. */
239 if(nsens>=3 && dist>=min_dist)
243 if(!reserve_block(block))
245 pending_block = &*block;
249 if(cur_blocks_end==blocks.end())
252 TrackIter track = block.track_iter();
253 if(track->is_path_changing())
255 pending_block = &*block;
260 unsigned path = track->get_active_path();
261 if(!track.endpoint().has_path(path))
263 path = track->get_type().coerce_path(track.entry(), path);
264 track->set_active_path(path);
265 if(track->is_path_changing())
267 pending_block = &*block;
273 if(&*block==stop_at_block)
276 if(block->get_sensor_address())
279 dist += block->get_path_length(block.entry());
284 update_next_sensor(0);
285 // Immediately advance to just before the next sensor
286 advance_front(next_sensor);
290 bool BlockAllocator::reserve_block(const BlockIter &block)
292 /* Add it to the list first to present a consistent state in block_reserved
294 blocks.push_back(block);
296 bool first_reserve = (cur_blocks_end==blocks.end());
302 if(!block->reserve(&train))
305 cur_blocks_end = blocks.end();
315 cur_blocks_end = blocks.end();
321 void BlockAllocator::advance_front(const Block *block, bool inclusive)
323 BlockList::iterator end;
326 end = cur_blocks_end;
329 end = find_if(end, blocks.end(), BlockMatch(*block));
330 if(inclusive && end!=blocks.end())
336 if(end==blocks.end() && blocks.back().block()==pending_block)
339 SetFlag setf(advancing);
340 BlockList::iterator i = cur_blocks_end;
341 // Update cur_blocks_end first to keep things consistent.
342 cur_blocks_end = end;
344 signal_advanced.emit(**i, (*i)->get_sensor());
347 void BlockAllocator::advance_front(const Sensor *sensor)
350 advance_front(sensor->get_block(), dynamic_cast<const BeamGate *>(sensor));
352 advance_front(0, false);
355 void BlockAllocator::advance_back()
357 bool rev = train.get_controller().get_reverse();
358 const Vehicle &veh = train.get_vehicle(rev ? 0 : train.get_n_vehicles()-1);
359 const Block &veh_block = veh.get_placement().get_position(rev ? VehiclePlacement::FRONT_AXLE : VehiclePlacement::BACK_AXLE)->get_block();
361 bool pending_cleared = false;
363 /* Sensors aren't guaranteed to be detriggered in order. Go through the
364 block list and locate the first sensor that's still active. */
365 BlockList::iterator end = blocks.end();
366 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
369 list<Sensor *> sensors;
371 /* Collect all sensors from the block in the order they are expected to
373 for(TrackIter j=i->track_iter(); (j && &j->get_block()==block); j=j.next())
374 if(!j->get_attachments().empty())
376 Track::AttachmentList attachments = j->get_attachments_ordered(j.entry());
377 for(Track::AttachmentList::const_iterator k=attachments.begin(); k!=attachments.end(); ++k)
378 if(BeamGate *gate = dynamic_cast<BeamGate *>(*k))
379 sensors.push_back(gate);
382 if(Sensor *sensor = (*i)->get_sensor())
383 sensors.push_back(sensor);
385 /* See if any sensor is still active, and record the position of the
386 last inactive sensor. */
387 bool active_sensor = false;
388 for(list<Sensor *>::const_iterator j=sensors.begin(); (!active_sensor && j!=sensors.end()); ++j)
390 if((*j)->get_state())
391 active_sensor = true;
396 if(block==pending_block)
397 pending_cleared = true;
399 // Stop if we encounter an active sensor or the train's last vehicle
400 if(block==&veh_block || active_sensor)
402 if(end!=blocks.end())
404 /* If the last inactive sensor was in an earlier block, release
405 that block as well. */
408 release_blocks_begin(end);
409 signal_rear_advanced.emit(*last());
420 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
422 for(BlockList::iterator i=blocks.begin(); i!=end; )
426 void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
428 // Guard against decrementing blocks.begin()
429 if(begin==blocks.begin())
430 return release_blocks_begin(blocks.end());
432 if(begin==blocks.end())
435 /* Release the blocks in reverse order so that a consistent state is
436 presented in block_reserved signal. */
438 for(BlockList::iterator i=--blocks.end(); !done; )
445 void BlockAllocator::release_block(const BlockList::iterator &i)
448 throw logic_error("cannot release while advancing");
449 if(i==cur_blocks_end)
451 if(next_sensor && &**i==next_sensor->get_block())
453 if(&**i==pending_block)
461 void BlockAllocator::reverse()
463 release_blocks_end(cur_blocks_end);
465 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
472 void BlockAllocator::turnout_path_changing(Track &track)
474 BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(track.get_block()));
478 release_blocks_end(i);
479 pending_block = &track.get_block();
483 void BlockAllocator::turnout_path_changed(Track &track)
485 if(&track.get_block()==pending_block && !reserving)
489 void BlockAllocator::block_reserved(Block &block, const Train *tr)
491 if(&block==pending_block && !tr && !reserving)
495 void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
497 Block *block = sensor.get_block();
498 if(!block || block->get_train()!=&train)
501 if(state==Sensor::MAYBE_ACTIVE)
503 if(&sensor==next_sensor)
505 if(is_block_current(*block))
506 signal_advanced.emit(*block, &sensor);
507 update_next_sensor(next_sensor);
508 advance_front(next_sensor);
513 else if(!is_block_current(*block))
514 train.get_layout().emergency(block, "Sensor for "+train.get_name()+" triggered out of order");
516 else if(state==Sensor::INACTIVE)
520 void BlockAllocator::update_next_sensor(Sensor *after)
522 BeamGate *after_gate = dynamic_cast<BeamGate *>(after);
524 BlockList::iterator i = cur_blocks_end;
529 i = find_if(i, blocks.end(), BlockMatch(*after->get_block()));
532 for(; i!=blocks.end(); ++i)
534 if(Sensor *sensor = (*i)->get_sensor())
536 if(!after_gate && sensor!=next_sensor)
538 next_sensor = sensor;
544 for(TrackIter j=i->track_iter(); (j && &j->get_block()==block); j=j.next())
545 if(!j->get_attachments().empty())
547 Track::AttachmentList attachments = j->get_attachments_ordered(j.entry());
548 for(Track::AttachmentList::const_iterator k=attachments.begin(); k!=attachments.end(); ++k)
549 if(BeamGate *gate = dynamic_cast<BeamGate *>(*k))
568 void BlockAllocator::halt_event(bool halted)
570 if(active && !halted)
574 void BlockAllocator::save(list<DataFile::Statement> &st) const
576 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
578 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
580 if(train.get_controller().get_reverse())
582 cur_blocks.reverse();
583 prev = cur_blocks.front().next();
586 prev = cur_blocks.front().flip();
588 st.push_back((DataFile::Statement("hint"), prev->get_id()));
590 for(BlockList::const_iterator i=cur_blocks.begin(); (i!=cur_blocks.end() && i->block()!=pending_block); ++i)
591 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
596 BlockAllocator::Loader::Loader(BlockAllocator &ba):
597 DataFile::ObjectLoader<BlockAllocator>(ba),
600 add("block", &Loader::block);
601 add("hint", &Loader::hint);
604 void BlockAllocator::Loader::block(unsigned id)
612 blk = &obj.train.get_layout().get_block(id);
614 catch(const key_error &)
622 entry = blk->get_endpoint_by_link(*prev_block);
626 obj.blocks.push_back(BlockIter(blk, entry));
627 blk->reserve(&obj.train);
629 if(blk->get_sensor_address())
630 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_address(), true);
635 void BlockAllocator::Loader::hint(unsigned id)
639 prev_block = &obj.train.get_layout().get_block(id);
641 catch(const key_error &)