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()),
36 Layout &layout = train.get_layout();
37 layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
38 layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::sensor_state_changed));
40 const set<Track *> &tracks = layout.get_all<Track>();
41 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
42 if((*i)->get_turnout_id())
44 (*i)->signal_path_changing.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changing), sigc::ref(**i))));
45 (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
49 void BlockAllocator::set_active(bool a)
56 release_blocks_end(cur_blocks_end);
61 void BlockAllocator::start_from(const BlockIter &block)
64 throw invalid_argument("BlockAllocator::start_from");
70 void BlockAllocator::rewind_to(const Block &block)
75 BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(block));
78 release_blocks_end(i);
83 void BlockAllocator::clear()
85 release_blocks_begin(blocks.end());
91 void BlockAllocator::stop_at(const Block *block)
93 stop_at_block = block;
98 const BlockIter &BlockAllocator::first() const
101 throw logic_error("no blocks");
102 return blocks.front();
105 const BlockIter &BlockAllocator::last() const
108 throw logic_error("no blocks");
109 BlockList::const_iterator i = --blocks.end();
110 if(i->block()==pending_block)
115 const BlockIter &BlockAllocator::last_current() const
118 throw logic_error("no blocks");
119 if(cur_blocks_end==blocks.begin())
120 throw logic_error("internal error (no current blocks)");
121 BlockList::const_iterator i = cur_blocks_end;
125 const BlockIter &BlockAllocator::iter_for(const Block &block) const
127 BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(block));
129 throw key_error(&block);
133 bool BlockAllocator::has_block(const Block &block) const
135 return find_if(blocks.begin(), blocks.end(), BlockMatch(block))!=blocks.end();
138 bool BlockAllocator::is_block_current(const Block &block) const
140 BlockList::const_iterator end = cur_blocks_end;
141 return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end;
144 void BlockAllocator::reserve_more()
147 throw logic_error("no blocks");
149 BlockIter start = blocks.back();
150 if(&*start==stop_at_block)
152 else if(&*start==pending_block)
154 TrackIter track = start.track_iter();
155 if(track->is_path_changing() || !track.endpoint().has_path(track->get_active_path()))
161 // See how many sensor blocks and how much track we already have
164 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
166 if((*i)->get_sensor_id())
169 dist += (*i)->get_path_length(i->entry());
172 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
173 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
175 BlockIter block = start;
177 SetFlag setf(reserving);
181 BlockIter prev = block;
182 block = block.next();
183 if(!block || block->get_endpoints().size()<2)
184 // The track ends here
187 if(block->get_turnout_id() && !prev->get_turnout_id())
189 /* We are arriving at a turnout. See if we have enough blocks and
190 distance reserved. */
191 if(nsens>=3 && dist>=min_dist)
195 if(!reserve_block(block))
197 pending_block = &*block;
201 if(cur_blocks_end==blocks.end())
204 TrackIter track = block.track_iter();
205 if(track->is_path_changing())
207 pending_block = &*block;
212 const TrackType::Endpoint &entry_ep = track.endpoint();
213 unsigned path = track->get_active_path();
214 if(!entry_ep.has_path(path))
216 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
217 if(entry_ep.has_common_paths(exit_ep))
219 unsigned mask = entry_ep.paths&exit_ep.paths;
220 for(path=0; mask>1; ++path, mask>>=1) ;
222 track->set_active_path(path);
223 if(track->is_path_changing())
225 pending_block = &*block;
230 // XXX Do something here
235 if(&*block==stop_at_block)
238 if(block->get_sensor_id())
241 dist += block->get_path_length(block.entry());
244 // Make any sensorless blocks at the beginning immediately current
245 while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id())
249 bool BlockAllocator::reserve_block(const BlockIter &block)
251 /* Add it to the list first to present a consistent state in block_reserved
253 blocks.push_back(block);
256 if(!block->reserve(&train))
271 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
273 for(BlockList::iterator i=blocks.begin(); i!=end; )
277 void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
279 // Guard against decrementing blocks.begin()
280 if(begin==blocks.begin())
281 return release_blocks_begin(blocks.end());
283 if(begin==blocks.end())
286 /* Release the blocks in reverse order so that a consistent state is
287 presented in block_reserved signal. */
289 for(BlockList::iterator i=--blocks.end(); !done; )
296 void BlockAllocator::release_block(const BlockList::iterator &i)
299 throw logic_error("cannot release while advancing");
300 if(i==cur_blocks_end)
302 if(&**i==pending_block)
310 void BlockAllocator::reverse()
312 release_blocks_end(cur_blocks_end);
314 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
321 void BlockAllocator::turnout_path_changing(Track &track)
323 BlockList::iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(track.get_block()));
327 release_blocks_end(i);
328 pending_block = &track.get_block();
332 void BlockAllocator::turnout_path_changed(Track &track)
334 if(&track.get_block()==pending_block && !reserving)
338 void BlockAllocator::block_reserved(Block &block, const Train *tr)
340 if(&block==pending_block && !tr && !reserving)
344 void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
347 if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
348 block = &tc->get_block();
352 if(block->get_train()!=&train)
355 if(state==Sensor::MAYBE_ACTIVE)
357 // Find the first sensor block from our reserved blocks that isn't this sensor
358 BlockList::iterator end;
360 for(end=cur_blocks_end; end!=blocks.end(); ++end)
361 if((*end)->get_sensor_id())
378 /* Advance the train to the new blocks. Update cur_blocks_end first
379 to keep things in sync. */
380 SetFlag setf(advancing);
381 BlockList::iterator i = cur_blocks_end;
382 cur_blocks_end = end;
384 train.signal_advanced.emit(**i);
391 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
393 else if(state==Sensor::INACTIVE)
395 const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1);
396 const Block &veh_block = veh.get_track()->get_block();
397 const Driver &driver = train.get_layout().get_driver();
399 /* Sensors aren't guaranteed to be detriggered in order. Go through the
400 block list and locate the first sensor that's still active. */
401 BlockList::iterator end = blocks.end();
402 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
404 // Avoid freeing blocks that still hold the train's vehicles
408 if((*i)->get_sensor_id())
410 if(driver.get_sensor((*i)->get_sensor_id()))
417 if(end!=blocks.end())
418 // Free blocks up to the last inactive sensor
419 release_blocks_begin(++end);
423 void BlockAllocator::save(list<DataFile::Statement> &st) const
425 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
427 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
429 if(train.get_controller().get_reverse())
431 cur_blocks.reverse();
432 prev = cur_blocks.front().next();
435 prev = cur_blocks.front().flip();
437 st.push_back((DataFile::Statement("hint"), prev->get_id()));
439 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
440 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
445 BlockAllocator::Loader::Loader(BlockAllocator &ba):
446 DataFile::ObjectLoader<BlockAllocator>(ba),
449 add("block", &Loader::block);
450 add("hint", &Loader::hint);
453 void BlockAllocator::Loader::block(unsigned id)
461 blk = &obj.train.get_layout().get_block(id);
463 catch(const key_error &)
471 entry = blk->get_endpoint_by_link(*prev_block);
475 obj.blocks.push_back(BlockIter(blk, entry));
476 blk->reserve(&obj.train);
478 if(blk->get_sensor_id())
479 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
484 void BlockAllocator::Loader::hint(unsigned id)
488 prev_block = &obj.train.get_layout().get_block(id);
490 catch(const key_error &)