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));
43 const set<Track *> &tracks = layout.get_all<Track>();
44 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
45 if((*i)->get_turnout_id())
47 (*i)->signal_path_changing.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changing), sigc::ref(**i))));
48 (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
52 void BlockAllocator::set_active(bool a)
59 release_blocks_end(cur_blocks_end);
64 bool BlockAllocator::start_from(const BlockIter &block)
67 throw invalid_argument("BlockAllocator::start_from");
69 float remaining_length = 0;
70 unsigned n_vehs = train.get_n_vehicles();
71 for(unsigned i=0; i<n_vehs; ++i)
72 remaining_length += train.get_vehicle(i).get_type().get_length();
76 BlockList blocks_to_reserve;
77 for(BlockIter b=block; b; b=b.next())
81 blocks_to_reserve.push_back(b);
82 for(TrackIter t=b.track_iter(); (t && &t->get_block()==&*b); t=t.next())
83 remaining_length -= t->get_path_length();
84 if(remaining_length<=0)
88 if(remaining_length>0)
91 for(BlockList::iterator i=blocks_to_reserve.begin(); i!=blocks_to_reserve.end(); ++i)
96 (*i)->reserve(&train);
101 while(i!=blocks_to_reserve.begin())
113 void BlockAllocator::rewind_to(const Block &block)
118 BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(block));
121 release_blocks_end(i);
126 void BlockAllocator::clear()
128 release_blocks_begin(blocks.end());
135 void BlockAllocator::stop_at(const Block *block)
137 stop_at_block = block;
142 const BlockIter &BlockAllocator::first() const
145 throw logic_error("no blocks");
146 return blocks.front();
149 const BlockIter &BlockAllocator::last() const
152 throw logic_error("no blocks");
153 BlockList::const_iterator i = --blocks.end();
154 if(i->block()==pending_block)
159 const BlockIter &BlockAllocator::last_current() const
162 throw logic_error("no blocks");
163 if(cur_blocks_end==blocks.begin())
164 throw logic_error("internal error (no current blocks)");
165 BlockList::const_iterator i = cur_blocks_end;
169 const BlockIter &BlockAllocator::iter_for(const Block &block) const
171 BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(block));
173 throw key_error(&block);
177 bool BlockAllocator::has_block(const Block &block) const
179 return find_if(blocks.begin(), blocks.end(), BlockMatch(block))!=blocks.end();
182 bool BlockAllocator::is_block_current(const Block &block) const
184 BlockList::const_iterator end = cur_blocks_end;
185 return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end;
188 void BlockAllocator::reserve_more()
191 throw logic_error("no blocks");
193 BlockIter start = blocks.back();
194 if(&*start==stop_at_block)
196 else if(&*start==pending_block)
198 TrackIter track = start.track_iter();
199 if(track->is_path_changing() || !track.endpoint().has_path(track->get_active_path()))
205 // See how many sensor blocks and how much track we already have
208 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
210 if((*i)->get_sensor_id())
213 dist += (*i)->get_path_length(i->entry());
216 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
217 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
219 BlockIter block = start;
221 SetFlag setf(reserving);
225 BlockIter prev = block;
226 block = block.next();
227 if(!block || block->get_endpoints().size()<2)
228 // The track ends here
231 if(block->get_turnout_id() && !prev->get_turnout_id())
233 /* We are arriving at a turnout. See if we have enough blocks and
234 distance reserved. */
235 if(nsens>=3 && dist>=min_dist)
239 if(!reserve_block(block))
241 pending_block = &*block;
245 if(cur_blocks_end==blocks.end())
248 TrackIter track = block.track_iter();
249 if(track->is_path_changing())
251 pending_block = &*block;
256 const TrackType::Endpoint &entry_ep = track.endpoint();
257 unsigned path = track->get_active_path();
258 if(!entry_ep.has_path(path))
260 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
261 if(entry_ep.has_common_paths(exit_ep))
263 unsigned mask = entry_ep.paths&exit_ep.paths;
264 for(path=0; mask>1; ++path, mask>>=1) ;
266 track->set_active_path(path);
267 if(track->is_path_changing())
269 pending_block = &*block;
274 // XXX Do something here
279 if(&*block==stop_at_block)
282 if(block->get_sensor_id())
285 dist += block->get_path_length(block.entry());
290 update_next_sensor(0);
291 // Immediately advance to just before the next sensor
292 advance_front(next_sensor);
296 bool BlockAllocator::reserve_block(const BlockIter &block)
298 /* Add it to the list first to present a consistent state in block_reserved
300 blocks.push_back(block);
302 bool first_reserve = (cur_blocks_end==blocks.end());
308 if(!block->reserve(&train))
311 cur_blocks_end = blocks.end();
321 cur_blocks_end = blocks.end();
327 void BlockAllocator::advance_front(const Block *block, bool inclusive)
329 BlockList::iterator end;
332 end = cur_blocks_end;
335 end = find_if(end, blocks.end(), BlockMatch(*block));
336 if(inclusive && end!=blocks.end())
342 SetFlag setf(advancing);
343 BlockList::iterator i = cur_blocks_end;
344 // Update cur_blocks_end first to keep things consistent.
345 cur_blocks_end = end;
347 train.signal_advanced.emit(**i);
350 void BlockAllocator::advance_front(const Sensor *sensor)
353 advance_front(sensor->get_block(), dynamic_cast<const BeamGate *>(sensor));
355 advance_front(0, false);
358 void BlockAllocator::advance_back()
360 bool rev = train.get_controller().get_reverse();
361 const Vehicle &veh = train.get_vehicle(rev ? 0 : train.get_n_vehicles()-1);
362 const Block &veh_block = veh.get_placement().get_position(rev ? VehiclePlacement::FRONT_AXLE : VehiclePlacement::BACK_AXLE)->get_block();
364 /* Sensors aren't guaranteed to be detriggered in order. Go through the
365 block list and locate the first sensor that's still active. */
366 BlockList::iterator end = blocks.end();
367 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
370 list<Sensor *> sensors;
372 /* Collect all sensors from the block in the order they are expected to
374 for(TrackIter j=i->track_iter(); (j && &j->get_block()==block); j=j.next())
375 if(!j->get_attachments().empty())
377 Track::AttachmentList attachments = j->get_attachments_ordered(j.entry());
378 for(Track::AttachmentList::const_iterator k=attachments.begin(); k!=attachments.end(); ++k)
379 if(BeamGate *gate = dynamic_cast<BeamGate *>(*k))
380 sensors.push_back(gate);
383 if(Sensor *sensor = (*i)->get_sensor())
384 sensors.push_back(sensor);
386 /* See if any sensor is still active, and record the position of the
387 last inactive sensor. */
388 bool active_sensor = false;
389 for(list<Sensor *>::const_iterator j=sensors.begin(); (!active_sensor && j!=sensors.end()); ++j)
391 if((*j)->get_state())
392 active_sensor = true;
397 // Stop if we encounter an active sensor or the train's last vehicle
398 if(block==&veh_block || active_sensor)
400 if(end!=blocks.end())
402 /* If the last inactive sensor was in an earlier block, release
403 that block as well. */
406 release_blocks_begin(end);
413 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
415 for(BlockList::iterator i=blocks.begin(); i!=end; )
419 void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
421 // Guard against decrementing blocks.begin()
422 if(begin==blocks.begin())
423 return release_blocks_begin(blocks.end());
425 if(begin==blocks.end())
428 /* Release the blocks in reverse order so that a consistent state is
429 presented in block_reserved signal. */
431 for(BlockList::iterator i=--blocks.end(); !done; )
438 void BlockAllocator::release_block(const BlockList::iterator &i)
441 throw logic_error("cannot release while advancing");
442 if(i==cur_blocks_end)
444 if(next_sensor && &**i==next_sensor->get_block())
446 if(&**i==pending_block)
454 void BlockAllocator::reverse()
456 release_blocks_end(cur_blocks_end);
458 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
465 void BlockAllocator::turnout_path_changing(Track &track)
467 BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(track.get_block()));
471 release_blocks_end(i);
472 pending_block = &track.get_block();
476 void BlockAllocator::turnout_path_changed(Track &track)
478 if(&track.get_block()==pending_block && !reserving)
482 void BlockAllocator::block_reserved(Block &block, const Train *tr)
484 if(&block==pending_block && !tr && !reserving)
488 void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
490 Block *block = sensor.get_block();
491 if(!block || block->get_train()!=&train)
494 if(state==Sensor::MAYBE_ACTIVE)
496 if(&sensor==next_sensor)
498 update_next_sensor(next_sensor);
499 advance_front(next_sensor);
504 else if(!is_block_current(*block))
505 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
507 else if(state==Sensor::INACTIVE)
511 void BlockAllocator::update_next_sensor(Sensor *after)
513 BeamGate *after_gate = dynamic_cast<BeamGate *>(after);
515 BlockList::iterator i = cur_blocks_end;
520 i = find_if(i, blocks.end(), BlockMatch(*after->get_block()));
523 for(; i!=blocks.end(); ++i)
525 if(Sensor *sensor = (*i)->get_sensor())
527 if(!after_gate && sensor!=next_sensor)
529 next_sensor = sensor;
535 for(TrackIter j=i->track_iter(); (j && &j->get_block()==block); j=j.next())
536 if(!j->get_attachments().empty())
538 Track::AttachmentList attachments = j->get_attachments_ordered(j.entry());
539 for(Track::AttachmentList::const_iterator k=attachments.begin(); k!=attachments.end(); ++k)
540 if(BeamGate *gate = dynamic_cast<BeamGate *>(*k))
559 void BlockAllocator::save(list<DataFile::Statement> &st) const
561 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
563 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
565 if(train.get_controller().get_reverse())
567 cur_blocks.reverse();
568 prev = cur_blocks.front().next();
571 prev = cur_blocks.front().flip();
573 st.push_back((DataFile::Statement("hint"), prev->get_id()));
575 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
576 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
581 BlockAllocator::Loader::Loader(BlockAllocator &ba):
582 DataFile::ObjectLoader<BlockAllocator>(ba),
585 add("block", &Loader::block);
586 add("hint", &Loader::hint);
589 void BlockAllocator::Loader::block(unsigned id)
597 blk = &obj.train.get_layout().get_block(id);
599 catch(const key_error &)
607 entry = blk->get_endpoint_by_link(*prev_block);
611 obj.blocks.push_back(BlockIter(blk, entry));
612 blk->reserve(&obj.train);
614 if(blk->get_sensor_id())
615 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
620 void BlockAllocator::Loader::hint(unsigned id)
624 prev_block = &obj.train.get_layout().get_block(id);
626 catch(const key_error &)