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()),
37 Layout &layout = train.get_layout();
38 layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
39 layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::sensor_state_changed));
41 const set<Track *> &tracks = layout.get_all<Track>();
42 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
43 if((*i)->get_turnout_id())
45 (*i)->signal_path_changing.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changing), sigc::ref(**i))));
46 (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
50 void BlockAllocator::set_active(bool a)
57 release_blocks_end(cur_blocks_end);
62 void BlockAllocator::start_from(const BlockIter &block)
65 throw invalid_argument("BlockAllocator::start_from");
71 void BlockAllocator::rewind_to(const Block &block)
76 BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(block));
79 release_blocks_end(i);
84 void BlockAllocator::clear()
86 release_blocks_begin(blocks.end());
93 void BlockAllocator::stop_at(const Block *block)
95 stop_at_block = block;
100 const BlockIter &BlockAllocator::first() const
103 throw logic_error("no blocks");
104 return blocks.front();
107 const BlockIter &BlockAllocator::last() const
110 throw logic_error("no blocks");
111 BlockList::const_iterator i = --blocks.end();
112 if(i->block()==pending_block)
117 const BlockIter &BlockAllocator::last_current() const
120 throw logic_error("no blocks");
121 if(cur_blocks_end==blocks.begin())
122 throw logic_error("internal error (no current blocks)");
123 BlockList::const_iterator i = cur_blocks_end;
127 const BlockIter &BlockAllocator::iter_for(const Block &block) const
129 BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(block));
131 throw key_error(&block);
135 bool BlockAllocator::has_block(const Block &block) const
137 return find_if(blocks.begin(), blocks.end(), BlockMatch(block))!=blocks.end();
140 bool BlockAllocator::is_block_current(const Block &block) const
142 BlockList::const_iterator end = cur_blocks_end;
143 return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end;
146 void BlockAllocator::reserve_more()
149 throw logic_error("no blocks");
151 BlockIter start = blocks.back();
152 if(&*start==stop_at_block)
154 else if(&*start==pending_block)
156 TrackIter track = start.track_iter();
157 if(track->is_path_changing() || !track.endpoint().has_path(track->get_active_path()))
163 // See how many sensor blocks and how much track we already have
166 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
168 if((*i)->get_sensor_id())
171 dist += (*i)->get_path_length(i->entry());
174 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
175 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
177 BlockIter block = start;
179 SetFlag setf(reserving);
183 BlockIter prev = block;
184 block = block.next();
185 if(!block || block->get_endpoints().size()<2)
186 // The track ends here
189 if(block->get_turnout_id() && !prev->get_turnout_id())
191 /* We are arriving at a turnout. See if we have enough blocks and
192 distance reserved. */
193 if(nsens>=3 && dist>=min_dist)
197 if(!reserve_block(block))
199 pending_block = &*block;
203 if(cur_blocks_end==blocks.end())
206 TrackIter track = block.track_iter();
207 if(track->is_path_changing())
209 pending_block = &*block;
214 const TrackType::Endpoint &entry_ep = track.endpoint();
215 unsigned path = track->get_active_path();
216 if(!entry_ep.has_path(path))
218 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
219 if(entry_ep.has_common_paths(exit_ep))
221 unsigned mask = entry_ep.paths&exit_ep.paths;
222 for(path=0; mask>1; ++path, mask>>=1) ;
224 track->set_active_path(path);
225 if(track->is_path_changing())
227 pending_block = &*block;
232 // XXX Do something here
237 if(&*block==stop_at_block)
240 if(block->get_sensor_id())
243 dist += block->get_path_length(block.entry());
248 update_next_sensor(0);
249 // Immediately advance to just before the next sensor
250 advance_to(next_sensor ? next_sensor->get_block() : 0);
254 bool BlockAllocator::reserve_block(const BlockIter &block)
256 /* Add it to the list first to present a consistent state in block_reserved
258 blocks.push_back(block);
261 if(!block->reserve(&train))
276 void BlockAllocator::advance_to(const Block *block)
278 BlockList::iterator end;
280 end = find_if(cur_blocks_end, blocks.end(), BlockMatch(*block));
284 SetFlag setf(advancing);
285 BlockList::iterator i = cur_blocks_end;
286 // Update cur_blocks_end first to keep things consistent.
287 cur_blocks_end = end;
289 train.signal_advanced.emit(**i);
292 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
294 for(BlockList::iterator i=blocks.begin(); i!=end; )
298 void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
300 // Guard against decrementing blocks.begin()
301 if(begin==blocks.begin())
302 return release_blocks_begin(blocks.end());
304 if(begin==blocks.end())
307 /* Release the blocks in reverse order so that a consistent state is
308 presented in block_reserved signal. */
310 for(BlockList::iterator i=--blocks.end(); !done; )
317 void BlockAllocator::release_block(const BlockList::iterator &i)
320 throw logic_error("cannot release while advancing");
321 if(i==cur_blocks_end)
323 if(next_sensor && &**i==next_sensor->get_block())
325 if(&**i==pending_block)
333 void BlockAllocator::reverse()
335 release_blocks_end(cur_blocks_end);
337 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
344 void BlockAllocator::turnout_path_changing(Track &track)
346 BlockList::iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(track.get_block()));
350 release_blocks_end(i);
351 pending_block = &track.get_block();
355 void BlockAllocator::turnout_path_changed(Track &track)
357 if(&track.get_block()==pending_block && !reserving)
361 void BlockAllocator::block_reserved(Block &block, const Train *tr)
363 if(&block==pending_block && !tr && !reserving)
367 void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
369 Block *block = sensor.get_block();
370 if(!block || block->get_train()!=&train)
373 if(state==Sensor::MAYBE_ACTIVE)
375 if(&sensor==next_sensor)
377 update_next_sensor(next_sensor);
378 advance_to(next_sensor ? next_sensor->get_block() : 0);
383 else if(!is_block_current(*block))
384 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
386 else if(state==Sensor::INACTIVE)
388 const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1);
389 const Block &veh_block = veh.get_track()->get_block();
391 /* Sensors aren't guaranteed to be detriggered in order. Go through the
392 block list and locate the first sensor that's still active. */
393 BlockList::iterator end = blocks.end();
394 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
396 // Avoid freeing blocks that still hold the train's vehicles
400 if(Sensor *s = (*i)->get_sensor())
402 if(s->get_state()>=Sensor::MAYBE_INACTIVE)
409 if(end!=blocks.end())
410 // Free blocks up to the last inactive sensor
411 release_blocks_begin(++end);
415 void BlockAllocator::update_next_sensor(Sensor *after)
417 BlockList::iterator i = cur_blocks_end;
419 i = find_if(i, blocks.end(), BlockMatch(*after->get_block()));
421 for(; i!=blocks.end(); ++i)
422 if(Sensor *sensor = (*i)->get_sensor())
423 if(sensor!=next_sensor)
425 next_sensor = sensor;
432 void BlockAllocator::save(list<DataFile::Statement> &st) const
434 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
436 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
438 if(train.get_controller().get_reverse())
440 cur_blocks.reverse();
441 prev = cur_blocks.front().next();
444 prev = cur_blocks.front().flip();
446 st.push_back((DataFile::Statement("hint"), prev->get_id()));
448 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
449 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
454 BlockAllocator::Loader::Loader(BlockAllocator &ba):
455 DataFile::ObjectLoader<BlockAllocator>(ba),
458 add("block", &Loader::block);
459 add("hint", &Loader::hint);
462 void BlockAllocator::Loader::block(unsigned id)
470 blk = &obj.train.get_layout().get_block(id);
472 catch(const key_error &)
480 entry = blk->get_endpoint_by_link(*prev_block);
484 obj.blocks.push_back(BlockIter(blk, entry));
485 blk->reserve(&obj.train);
487 if(blk->get_sensor_id())
488 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
493 void BlockAllocator::Loader::hint(unsigned id)
497 prev_block = &obj.train.get_layout().get_block(id);
499 catch(const key_error &)