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 bool BlockAllocator::start_from(const BlockIter &block)
66 throw invalid_argument("BlockAllocator::start_from");
68 float remaining_length = 0;
69 unsigned n_vehs = train.get_n_vehicles();
70 for(unsigned i=0; i<n_vehs; ++i)
71 remaining_length += train.get_vehicle(i).get_type().get_length();
75 BlockList blocks_to_reserve;
76 for(BlockIter b=block; b; b=b.next())
80 blocks_to_reserve.push_back(b);
81 for(TrackIter t=b.track_iter(); (t && &t->get_block()==&*b); t=t.next())
82 remaining_length -= t->get_type().get_path_length(t->get_active_path());
83 if(remaining_length<=0)
87 if(remaining_length>0)
90 for(BlockList::iterator i=blocks_to_reserve.begin(); i!=blocks_to_reserve.end(); ++i)
95 (*i)->reserve(&train);
100 while(i!=blocks_to_reserve.begin())
112 void BlockAllocator::rewind_to(const Block &block)
117 BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(block));
120 release_blocks_end(i);
125 void BlockAllocator::clear()
127 release_blocks_begin(blocks.end());
134 void BlockAllocator::stop_at(const Block *block)
136 stop_at_block = block;
141 const BlockIter &BlockAllocator::first() const
144 throw logic_error("no blocks");
145 return blocks.front();
148 const BlockIter &BlockAllocator::last() const
151 throw logic_error("no blocks");
152 BlockList::const_iterator i = --blocks.end();
153 if(i->block()==pending_block)
158 const BlockIter &BlockAllocator::last_current() const
161 throw logic_error("no blocks");
162 if(cur_blocks_end==blocks.begin())
163 throw logic_error("internal error (no current blocks)");
164 BlockList::const_iterator i = cur_blocks_end;
168 const BlockIter &BlockAllocator::iter_for(const Block &block) const
170 BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(block));
172 throw key_error(&block);
176 bool BlockAllocator::has_block(const Block &block) const
178 return find_if(blocks.begin(), blocks.end(), BlockMatch(block))!=blocks.end();
181 bool BlockAllocator::is_block_current(const Block &block) const
183 BlockList::const_iterator end = cur_blocks_end;
184 return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end;
187 void BlockAllocator::reserve_more()
190 throw logic_error("no blocks");
192 BlockIter start = blocks.back();
193 if(&*start==stop_at_block)
195 else if(&*start==pending_block)
197 TrackIter track = start.track_iter();
198 if(track->is_path_changing() || !track.endpoint().has_path(track->get_active_path()))
204 // See how many sensor blocks and how much track we already have
207 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
209 if((*i)->get_sensor_id())
212 dist += (*i)->get_path_length(i->entry());
215 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
216 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
218 BlockIter block = start;
220 SetFlag setf(reserving);
224 BlockIter prev = block;
225 block = block.next();
226 if(!block || block->get_endpoints().size()<2)
227 // The track ends here
230 if(block->get_turnout_id() && !prev->get_turnout_id())
232 /* We are arriving at a turnout. See if we have enough blocks and
233 distance reserved. */
234 if(nsens>=3 && dist>=min_dist)
238 if(!reserve_block(block))
240 pending_block = &*block;
244 if(cur_blocks_end==blocks.end())
247 TrackIter track = block.track_iter();
248 if(track->is_path_changing())
250 pending_block = &*block;
255 const TrackType::Endpoint &entry_ep = track.endpoint();
256 unsigned path = track->get_active_path();
257 if(!entry_ep.has_path(path))
259 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
260 if(entry_ep.has_common_paths(exit_ep))
262 unsigned mask = entry_ep.paths&exit_ep.paths;
263 for(path=0; mask>1; ++path, mask>>=1) ;
265 track->set_active_path(path);
266 if(track->is_path_changing())
268 pending_block = &*block;
273 // XXX Do something here
278 if(&*block==stop_at_block)
281 if(block->get_sensor_id())
284 dist += block->get_path_length(block.entry());
289 update_next_sensor(0);
290 // Immediately advance to just before the next sensor
291 advance_front(next_sensor);
295 bool BlockAllocator::reserve_block(const BlockIter &block)
297 /* Add it to the list first to present a consistent state in block_reserved
299 blocks.push_back(block);
301 bool first_reserve = (cur_blocks_end==blocks.end());
307 if(!block->reserve(&train))
310 cur_blocks_end = blocks.end();
320 cur_blocks_end = blocks.end();
326 void BlockAllocator::advance_front(const Block *block, bool inclusive)
328 BlockList::iterator end;
331 end = cur_blocks_end;
334 end = find_if(end, blocks.end(), BlockMatch(*block));
335 if(inclusive && end!=blocks.end())
341 SetFlag setf(advancing);
342 BlockList::iterator i = cur_blocks_end;
343 // Update cur_blocks_end first to keep things consistent.
344 cur_blocks_end = end;
346 train.signal_advanced.emit(**i);
349 void BlockAllocator::advance_front(const Sensor *sensor)
352 advance_front(sensor->get_block(), dynamic_cast<const BeamGate *>(sensor));
354 advance_front(0, false);
357 void BlockAllocator::advance_back()
359 bool rev = train.get_controller().get_reverse();
360 const Vehicle &veh = train.get_vehicle(rev ? 0 : train.get_n_vehicles()-1);
361 const Block &veh_block = veh.get_placement().get_position(rev ? VehiclePlacement::FRONT_AXLE : VehiclePlacement::BACK_AXLE)->get_block();
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 // Stop if we encounter an active sensor or the train's last vehicle
397 if(block==&veh_block || active_sensor)
399 if(end!=blocks.end())
401 /* If the last inactive sensor was in an earlier block, release
402 that block as well. */
405 release_blocks_begin(end);
412 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
414 for(BlockList::iterator i=blocks.begin(); i!=end; )
418 void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
420 // Guard against decrementing blocks.begin()
421 if(begin==blocks.begin())
422 return release_blocks_begin(blocks.end());
424 if(begin==blocks.end())
427 /* Release the blocks in reverse order so that a consistent state is
428 presented in block_reserved signal. */
430 for(BlockList::iterator i=--blocks.end(); !done; )
437 void BlockAllocator::release_block(const BlockList::iterator &i)
440 throw logic_error("cannot release while advancing");
441 if(i==cur_blocks_end)
443 if(next_sensor && &**i==next_sensor->get_block())
445 if(&**i==pending_block)
453 void BlockAllocator::reverse()
455 release_blocks_end(cur_blocks_end);
457 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
464 void BlockAllocator::turnout_path_changing(Track &track)
466 BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(track.get_block()));
470 release_blocks_end(i);
471 pending_block = &track.get_block();
475 void BlockAllocator::turnout_path_changed(Track &track)
477 if(&track.get_block()==pending_block && !reserving)
481 void BlockAllocator::block_reserved(Block &block, const Train *tr)
483 if(&block==pending_block && !tr && !reserving)
487 void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
489 Block *block = sensor.get_block();
490 if(!block || block->get_train()!=&train)
493 if(state==Sensor::MAYBE_ACTIVE)
495 if(&sensor==next_sensor)
497 update_next_sensor(next_sensor);
498 advance_front(next_sensor);
503 else if(!is_block_current(*block))
504 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
506 else if(state==Sensor::INACTIVE)
510 void BlockAllocator::update_next_sensor(Sensor *after)
512 BeamGate *after_gate = dynamic_cast<BeamGate *>(after);
514 BlockList::iterator i = cur_blocks_end;
519 i = find_if(i, blocks.end(), BlockMatch(*after->get_block()));
522 for(; i!=blocks.end(); ++i)
524 if(Sensor *sensor = (*i)->get_sensor())
526 if(!after_gate && sensor!=next_sensor)
528 next_sensor = sensor;
534 for(TrackIter j=i->track_iter(); (j && &j->get_block()==block); j=j.next())
535 if(!j->get_attachments().empty())
537 Track::AttachmentList attachments = j->get_attachments_ordered(j.entry());
538 for(Track::AttachmentList::const_iterator k=attachments.begin(); k!=attachments.end(); ++k)
539 if(BeamGate *gate = dynamic_cast<BeamGate *>(*k))
558 void BlockAllocator::save(list<DataFile::Statement> &st) const
560 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
562 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
564 if(train.get_controller().get_reverse())
566 cur_blocks.reverse();
567 prev = cur_blocks.front().next();
570 prev = cur_blocks.front().flip();
572 st.push_back((DataFile::Statement("hint"), prev->get_id()));
574 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
575 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
580 BlockAllocator::Loader::Loader(BlockAllocator &ba):
581 DataFile::ObjectLoader<BlockAllocator>(ba),
584 add("block", &Loader::block);
585 add("hint", &Loader::hint);
588 void BlockAllocator::Loader::block(unsigned id)
596 blk = &obj.train.get_layout().get_block(id);
598 catch(const key_error &)
606 entry = blk->get_endpoint_by_link(*prev_block);
610 obj.blocks.push_back(BlockIter(blk, entry));
611 blk->reserve(&obj.train);
613 if(blk->get_sensor_id())
614 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
619 void BlockAllocator::Loader::hint(unsigned id)
623 prev_block = &obj.train.get_layout().get_block(id);
625 catch(const key_error &)