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):
31 cur_blocks_end(blocks.end()),
38 Layout &layout = train.get_layout();
39 layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
40 layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::sensor_state_changed));
42 const set<Track *> &tracks = layout.get_all<Track>();
43 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
44 if((*i)->get_turnout_id())
46 (*i)->signal_path_changing.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changing), sigc::ref(**i))));
47 (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
51 void BlockAllocator::set_active(bool a)
58 release_blocks_end(cur_blocks_end);
63 void BlockAllocator::start_from(const BlockIter &block)
66 throw invalid_argument("BlockAllocator::start_from");
72 void BlockAllocator::rewind_to(const Block &block)
77 BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(block));
80 release_blocks_end(i);
85 void BlockAllocator::clear()
87 release_blocks_begin(blocks.end());
94 void BlockAllocator::stop_at(const Block *block)
96 stop_at_block = block;
101 const BlockIter &BlockAllocator::first() const
104 throw logic_error("no blocks");
105 return blocks.front();
108 const BlockIter &BlockAllocator::last() const
111 throw logic_error("no blocks");
112 BlockList::const_iterator i = --blocks.end();
113 if(i->block()==pending_block)
118 const BlockIter &BlockAllocator::last_current() const
121 throw logic_error("no blocks");
122 if(cur_blocks_end==blocks.begin())
123 throw logic_error("internal error (no current blocks)");
124 BlockList::const_iterator i = cur_blocks_end;
128 const BlockIter &BlockAllocator::iter_for(const Block &block) const
130 BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(block));
132 throw key_error(&block);
136 bool BlockAllocator::has_block(const Block &block) const
138 return find_if(blocks.begin(), blocks.end(), BlockMatch(block))!=blocks.end();
141 bool BlockAllocator::is_block_current(const Block &block) const
143 BlockList::const_iterator end = cur_blocks_end;
144 return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end;
147 void BlockAllocator::reserve_more()
150 throw logic_error("no blocks");
152 BlockIter start = blocks.back();
153 if(&*start==stop_at_block)
155 else if(&*start==pending_block)
157 TrackIter track = start.track_iter();
158 if(track->is_path_changing() || !track.endpoint().has_path(track->get_active_path()))
164 // See how many sensor blocks and how much track we already have
167 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
169 if((*i)->get_sensor_id())
172 dist += (*i)->get_path_length(i->entry());
175 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
176 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
178 BlockIter block = start;
180 SetFlag setf(reserving);
184 BlockIter prev = block;
185 block = block.next();
186 if(!block || block->get_endpoints().size()<2)
187 // The track ends here
190 if(block->get_turnout_id() && !prev->get_turnout_id())
192 /* We are arriving at a turnout. See if we have enough blocks and
193 distance reserved. */
194 if(nsens>=3 && dist>=min_dist)
198 if(!reserve_block(block))
200 pending_block = &*block;
204 if(cur_blocks_end==blocks.end())
207 TrackIter track = block.track_iter();
208 if(track->is_path_changing())
210 pending_block = &*block;
215 const TrackType::Endpoint &entry_ep = track.endpoint();
216 unsigned path = track->get_active_path();
217 if(!entry_ep.has_path(path))
219 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
220 if(entry_ep.has_common_paths(exit_ep))
222 unsigned mask = entry_ep.paths&exit_ep.paths;
223 for(path=0; mask>1; ++path, mask>>=1) ;
225 track->set_active_path(path);
226 if(track->is_path_changing())
228 pending_block = &*block;
233 // XXX Do something here
238 if(&*block==stop_at_block)
241 if(block->get_sensor_id())
244 dist += block->get_path_length(block.entry());
249 update_next_sensor(0);
250 // Immediately advance to just before the next sensor
251 advance_front(next_sensor);
255 bool BlockAllocator::reserve_block(const BlockIter &block)
257 /* Add it to the list first to present a consistent state in block_reserved
259 blocks.push_back(block);
262 if(!block->reserve(&train))
277 void BlockAllocator::advance_front(const Block *block, bool inclusive)
279 BlockList::iterator end;
282 end = cur_blocks_end;
285 end = find_if(end, blocks.end(), BlockMatch(*block));
286 if(inclusive && end!=blocks.end())
292 SetFlag setf(advancing);
293 BlockList::iterator i = cur_blocks_end;
294 // Update cur_blocks_end first to keep things consistent.
295 cur_blocks_end = end;
297 train.signal_advanced.emit(**i);
300 void BlockAllocator::advance_front(const Sensor *sensor)
303 advance_front(sensor->get_block(), dynamic_cast<const BeamGate *>(sensor));
305 advance_front(0, false);
308 void BlockAllocator::advance_back()
310 const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1);
311 const Block &veh_block = veh.get_track()->get_block();
313 /* Sensors aren't guaranteed to be detriggered in order. Go through the
314 block list and locate the first sensor that's still active. */
315 BlockList::iterator end = blocks.end();
316 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
319 list<Sensor *> sensors;
321 /* Collect all sensors from the block in the order they are expected to
323 for(TrackIter j=i->track_iter(); (j && &j->get_block()==block); j=j.next())
324 if(!j->get_attachments().empty())
326 Track::AttachmentList attachments = j->get_attachments_ordered(j.entry());
327 for(Track::AttachmentList::const_iterator k=attachments.begin(); k!=attachments.end(); ++k)
328 if(BeamGate *gate = dynamic_cast<BeamGate *>(*k))
329 sensors.push_back(gate);
332 if(Sensor *sensor = (*i)->get_sensor())
333 sensors.push_back(sensor);
335 /* See if any sensor is still active, and record the position of the
336 last inactive sensor. */
337 bool active_sensor = false;
338 for(list<Sensor *>::const_iterator j=sensors.begin(); (!active_sensor && j!=sensors.end()); ++j)
340 if((*j)->get_state())
341 active_sensor = true;
346 // Stop if we encounter an active sensor or the train's last vehicle
347 if(block==&veh_block || active_sensor)
349 if(end!=blocks.end())
351 /* If the last inactive sensor was in an earlier block, release
352 that block as well. */
355 release_blocks_begin(end);
362 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
364 for(BlockList::iterator i=blocks.begin(); i!=end; )
368 void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
370 // Guard against decrementing blocks.begin()
371 if(begin==blocks.begin())
372 return release_blocks_begin(blocks.end());
374 if(begin==blocks.end())
377 /* Release the blocks in reverse order so that a consistent state is
378 presented in block_reserved signal. */
380 for(BlockList::iterator i=--blocks.end(); !done; )
387 void BlockAllocator::release_block(const BlockList::iterator &i)
390 throw logic_error("cannot release while advancing");
391 if(i==cur_blocks_end)
393 if(next_sensor && &**i==next_sensor->get_block())
395 if(&**i==pending_block)
403 void BlockAllocator::reverse()
405 release_blocks_end(cur_blocks_end);
407 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
414 void BlockAllocator::turnout_path_changing(Track &track)
416 BlockList::iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(track.get_block()));
420 release_blocks_end(i);
421 pending_block = &track.get_block();
425 void BlockAllocator::turnout_path_changed(Track &track)
427 if(&track.get_block()==pending_block && !reserving)
431 void BlockAllocator::block_reserved(Block &block, const Train *tr)
433 if(&block==pending_block && !tr && !reserving)
437 void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
439 Block *block = sensor.get_block();
440 if(!block || block->get_train()!=&train)
443 if(state==Sensor::MAYBE_ACTIVE)
445 if(&sensor==next_sensor)
447 update_next_sensor(next_sensor);
448 advance_front(next_sensor);
453 else if(!is_block_current(*block))
454 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
456 else if(state==Sensor::INACTIVE)
460 void BlockAllocator::update_next_sensor(Sensor *after)
462 BeamGate *after_gate = dynamic_cast<BeamGate *>(after);
464 BlockList::iterator i = cur_blocks_end;
469 i = find_if(i, blocks.end(), BlockMatch(*after->get_block()));
472 for(; i!=blocks.end(); ++i)
474 if(Sensor *sensor = (*i)->get_sensor())
476 if(!after_gate && sensor!=next_sensor)
478 next_sensor = sensor;
484 for(TrackIter j=i->track_iter(); (j && &j->get_block()==block); j=j.next())
485 if(!j->get_attachments().empty())
487 Track::AttachmentList attachments = j->get_attachments_ordered(j.entry());
488 for(Track::AttachmentList::const_iterator k=attachments.begin(); k!=attachments.end(); ++k)
489 if(BeamGate *gate = dynamic_cast<BeamGate *>(*k))
508 void BlockAllocator::save(list<DataFile::Statement> &st) const
510 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
512 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
514 if(train.get_controller().get_reverse())
516 cur_blocks.reverse();
517 prev = cur_blocks.front().next();
520 prev = cur_blocks.front().flip();
522 st.push_back((DataFile::Statement("hint"), prev->get_id()));
524 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
525 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
530 BlockAllocator::Loader::Loader(BlockAllocator &ba):
531 DataFile::ObjectLoader<BlockAllocator>(ba),
534 add("block", &Loader::block);
535 add("hint", &Loader::hint);
538 void BlockAllocator::Loader::block(unsigned id)
546 blk = &obj.train.get_layout().get_block(id);
548 catch(const key_error &)
556 entry = blk->get_endpoint_by_link(*prev_block);
560 obj.blocks.push_back(BlockIter(blk, entry));
561 blk->reserve(&obj.train);
563 if(blk->get_sensor_id())
564 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
569 void BlockAllocator::Loader::hint(unsigned id)
573 prev_block = &obj.train.get_layout().get_block(id);
575 catch(const key_error &)