1 #include <msp/core/maputils.h>
2 #include <msp/core/raii.h>
3 #include "blockallocator.h"
16 BlockAllocator::BlockAllocator(Train &t):
18 cur_blocks_end(blocks.end()),
23 Layout &layout = train.get_layout();
24 layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
25 layout.signal_block_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::block_state_changed));
27 const set<Track *> &tracks = layout.get_tracks();
28 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
29 if((*i)->get_turnout_id())
30 (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
33 void BlockAllocator::start_from(const BlockIter &block)
36 throw invalid_argument("BlockAllocator::start_from");
38 release_blocks(blocks.begin(), blocks.end());
40 blocks.push_back(block);
41 if(!block->reserve(&train))
48 void BlockAllocator::clear()
50 release_blocks(blocks.begin(), blocks.end());
55 void BlockAllocator::stop_at(const Block *block)
57 stop_at_block = block;
60 const BlockIter &BlockAllocator::first() const
63 throw logic_error("no blocks");
64 return blocks.front();
67 const BlockIter &BlockAllocator::last() const
70 throw logic_error("no blocks");
71 BlockList::const_iterator i = --blocks.end();
72 if(i->block()==pending_block)
77 const BlockIter &BlockAllocator::last_current() const
80 throw logic_error("no blocks");
81 if(cur_blocks_end==blocks.begin())
82 throw logic_error("internal error (no current blocks)");
83 BlockList::const_iterator i = cur_blocks_end;
87 int BlockAllocator::get_entry_to_block(const Block &block) const
89 for(BlockList::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
90 if(i->block()==&block)
95 void BlockAllocator::reserve_more()
98 throw logic_error("no blocks");
100 BlockIter start = blocks.back();
101 if(&*start==stop_at_block)
103 else if(&*start==pending_block)
105 TrackIter track = start.track_iter();
106 if(!track.endpoint().has_path(track->get_active_path()))
112 // See how many sensor blocks and how much track we already have
115 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
117 if((*i)->get_sensor_id())
120 dist += (*i)->get_path_length(i->entry());
123 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
124 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
126 BlockIter block = start;
128 SetFlag setf(reserving);
132 BlockIter prev = block;
133 block = block.next();
134 if(!block || block->get_endpoints().size()<2)
135 // The track ends here
138 if(block->get_turnout_id() && !prev->get_turnout_id())
140 /* We are arriving at a turnout. See if we have enough blocks and
141 distance reserved. */
142 if(nsens>=3 && dist>=min_dist)
146 blocks.push_back(block);
147 if(!block->reserve(&train))
150 pending_block = &*block;
154 if(cur_blocks_end==blocks.end())
157 TrackIter track = block.track_iter();
158 if(track->is_path_changing())
160 pending_block = &*block;
165 const TrackType::Endpoint &entry_ep = track.endpoint();
166 unsigned path = track->get_active_path();
167 if(!entry_ep.has_path(path))
169 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
170 if(entry_ep.has_common_paths(exit_ep))
172 unsigned mask = entry_ep.paths&exit_ep.paths;
173 for(path=0; mask>1; ++path, mask>>=1) ;
175 track->set_active_path(path);
176 if(track->is_path_changing())
178 pending_block = &*block;
183 // XXX Do something here
188 if(&*block==stop_at_block)
191 if(block->get_sensor_id())
194 dist += block->get_path_length(block.entry());
197 // Make any sensorless blocks at the beginning immediately current
198 while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id())
202 void BlockAllocator::release_until(const Block &block)
204 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
205 if(i->block()==&block)
207 if(++i!=cur_blocks_end)
208 release_blocks(blocks.begin(), i);
213 bool BlockAllocator::release_from(const Block &block)
215 bool have_sensor = false;
216 for(BlockList::iterator i=cur_blocks_end; i!=blocks.end(); ++i)
218 if(i->block()==&block)
221 release_blocks(i, blocks.end());
224 else if((*i)->get_sensor_id())
231 void BlockAllocator::release_noncurrent()
233 release_blocks(cur_blocks_end, blocks.end());
236 void BlockAllocator::release_blocks(const BlockList::iterator &b, const BlockList::iterator &e)
238 for(BlockList::iterator i=b; i!=e; )
240 if(cur_blocks_end==i)
249 void BlockAllocator::reverse()
251 release_noncurrent();
253 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
257 void BlockAllocator::turnout_path_changed(Track &track)
259 for(list<BlockIter>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
260 if((*i)->get_turnout_id()==track.get_turnout_id() && !reserving && &**i==pending_block)
264 void BlockAllocator::block_reserved(Block &block, const Train *tr)
266 if(&block==pending_block && !tr && !reserving)
270 void BlockAllocator::block_state_changed(Block &block, Block::State state)
272 if(state==Block::MAYBE_ACTIVE)
274 // Find the first sensor block from our reserved blocks that isn't this sensor
275 BlockList::iterator end;
277 for(end=cur_blocks_end; end!=blocks.end(); ++end)
278 if((*end)->get_sensor_id())
295 // Move blocks up to the next sensor to our current blocks
296 for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
297 train.signal_advanced.emit(**j);
298 cur_blocks_end = end;
301 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
303 else if(state==Block::INACTIVE)
305 const Vehicle &veh = train.get_controller().get_reverse() ? train.get_vehicle(0) : train.get_vehicle(train.get_n_vehicles()-1);
307 // Find the first sensor in our current blocks that's still active
308 BlockList::iterator end = blocks.begin();
309 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
311 if((*i)->has_track(*veh.get_track()))
313 if((*i)->get_sensor_id())
315 if(train.get_layout().get_driver().get_sensor((*i)->get_sensor_id()))
325 if(end!=blocks.begin() && end!=cur_blocks_end)
326 // Free blocks up to the last inactive sensor
327 release_blocks(blocks.begin(), end);
331 void BlockAllocator::save(list<DataFile::Statement> &st) const
333 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
335 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
337 if(train.get_controller().get_reverse())
339 cur_blocks.reverse();
340 prev = cur_blocks.front().next();
343 prev = cur_blocks.front().flip();
345 st.push_back((DataFile::Statement("hint"), prev->get_id()));
347 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
348 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
353 BlockAllocator::Loader::Loader(BlockAllocator &ba):
354 DataFile::ObjectLoader<BlockAllocator>(ba),
357 add("block", &Loader::block);
358 add("hint", &Loader::hint);
361 void BlockAllocator::Loader::block(unsigned id)
369 blk = &obj.train.get_layout().get_block(id);
371 catch(const key_error &)
379 entry = blk->get_endpoint_by_link(*prev_block);
383 obj.blocks.push_back(BlockIter(blk, entry));
384 blk->reserve(&obj.train);
386 if(blk->get_sensor_id())
387 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
392 void BlockAllocator::Loader::hint(unsigned id)
396 prev_block = &obj.train.get_layout().get_block(id);
398 catch(const key_error &)