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(Block &block, unsigned entry)
35 release_blocks(blocks.begin(), blocks.end());
37 blocks.push_back(BlockIter(&block, entry));
38 if(!block.reserve(&train))
45 void BlockAllocator::clear()
47 release_blocks(blocks.begin(), blocks.end());
52 void BlockAllocator::stop_at(const Block *block)
54 stop_at_block = block;
57 const BlockIter &BlockAllocator::first() const
60 throw logic_error("no blocks");
61 return blocks.front();
64 const BlockIter &BlockAllocator::last() const
67 throw logic_error("no blocks");
68 BlockList::const_iterator i = --blocks.end();
69 if(i->block()==pending_block)
74 const BlockIter &BlockAllocator::last_current() const
77 throw logic_error("no blocks");
78 if(cur_blocks_end==blocks.begin())
79 throw logic_error("internal error (no current blocks)");
80 BlockList::const_iterator i = cur_blocks_end;
84 int BlockAllocator::get_entry_to_block(const Block &block) const
86 for(BlockList::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
87 if(i->block()==&block)
92 void BlockAllocator::reserve_more()
95 throw logic_error("no blocks");
97 BlockIter start = blocks.back();
98 if(&*start==stop_at_block)
100 else if(&*start==pending_block)
102 TrackIter track = start.track_iter();
103 if(!track.endpoint().has_path(track->get_active_path()))
109 // See how many sensor blocks and how much track we already have
112 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
114 if((*i)->get_sensor_id())
117 dist += (*i)->get_path_length(i->entry());
120 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
121 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
123 BlockIter block = start;
125 SetFlag setf(reserving);
129 BlockIter prev = block;
130 block = block.next();
131 if(!block || block->get_endpoints().size()<2)
132 // The track ends here
135 if(block->get_turnout_id() && !prev->get_turnout_id())
137 /* We are arriving at a turnout. See if we have enough blocks and
138 distance reserved. */
139 if(nsens>=3 && dist>=min_dist)
143 blocks.push_back(block);
144 if(!block->reserve(&train))
147 pending_block = &*block;
151 if(cur_blocks_end==blocks.end())
154 TrackIter track = block.track_iter();
155 if(track->is_path_changing())
157 pending_block = &*block;
162 const TrackType::Endpoint &entry_ep = track.endpoint();
163 unsigned path = track->get_active_path();
164 if(!entry_ep.has_path(path))
166 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
167 if(entry_ep.has_common_paths(exit_ep))
169 unsigned mask = entry_ep.paths&exit_ep.paths;
170 for(path=0; mask>1; ++path, mask>>=1) ;
172 track->set_active_path(path);
173 if(track->is_path_changing())
175 pending_block = &*block;
180 // XXX Do something here
185 if(&*block==stop_at_block)
188 if(block->get_sensor_id())
191 dist += block->get_path_length(block.entry());
194 // Make any sensorless blocks at the beginning immediately current
195 while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id())
199 void BlockAllocator::release_until(const Block &block)
201 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
202 if(i->block()==&block)
204 if(++i!=cur_blocks_end)
205 release_blocks(blocks.begin(), i);
210 bool BlockAllocator::release_from(const Block &block)
212 bool have_sensor = false;
213 for(BlockList::iterator i=cur_blocks_end; i!=blocks.end(); ++i)
215 if(i->block()==&block)
218 release_blocks(i, blocks.end());
221 else if((*i)->get_sensor_id())
228 void BlockAllocator::release_noncurrent()
230 release_blocks(cur_blocks_end, blocks.end());
233 void BlockAllocator::release_blocks(const BlockList::iterator &b, const BlockList::iterator &e)
235 for(BlockList::iterator i=b; i!=e; )
237 if(cur_blocks_end==i)
246 void BlockAllocator::reverse()
248 release_noncurrent();
250 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
254 void BlockAllocator::turnout_path_changed(Track &track)
256 for(list<BlockIter>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
257 if((*i)->get_turnout_id()==track.get_turnout_id() && !reserving && &**i==pending_block)
261 void BlockAllocator::block_reserved(Block &block, const Train *tr)
263 if(&block==pending_block && !tr && !reserving)
267 void BlockAllocator::block_state_changed(Block &block, Block::State state)
269 if(state==Block::MAYBE_ACTIVE)
271 // Find the first sensor block from our reserved blocks that isn't this sensor
272 BlockList::iterator end;
274 for(end=cur_blocks_end; end!=blocks.end(); ++end)
275 if((*end)->get_sensor_id())
292 // Move blocks up to the next sensor to our current blocks
293 for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
294 train.signal_advanced.emit(**j);
295 cur_blocks_end = end;
298 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
300 else if(state==Block::INACTIVE)
302 const Vehicle &veh = train.get_controller().get_reverse() ? train.get_vehicle(0) : train.get_vehicle(train.get_n_vehicles()-1);
304 // Find the first sensor in our current blocks that's still active
305 BlockList::iterator end = blocks.begin();
306 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
308 if((*i)->has_track(*veh.get_track()))
310 if((*i)->get_sensor_id())
312 if(train.get_layout().get_driver().get_sensor((*i)->get_sensor_id()))
322 if(end!=blocks.begin() && end!=cur_blocks_end)
323 // Free blocks up to the last inactive sensor
324 release_blocks(blocks.begin(), end);
328 void BlockAllocator::save(list<DataFile::Statement> &st) const
330 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
332 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
334 if(train.get_controller().get_reverse())
336 cur_blocks.reverse();
337 prev = cur_blocks.front().next();
340 prev = cur_blocks.front().flip();
342 st.push_back((DataFile::Statement("hint"), prev->get_id()));
344 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
345 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
350 BlockAllocator::Loader::Loader(BlockAllocator &ba):
351 DataFile::ObjectLoader<BlockAllocator>(ba),
354 add("block", &Loader::block);
355 add("hint", &Loader::hint);
358 void BlockAllocator::Loader::block(unsigned id)
366 blk = &obj.train.get_layout().get_block(id);
368 catch(const key_error &)
376 entry = blk->get_endpoint_by_link(*prev_block);
380 obj.blocks.push_back(BlockIter(blk, entry));
381 blk->reserve(&obj.train);
383 if(blk->get_sensor_id())
384 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
389 void BlockAllocator::Loader::hint(unsigned id)
393 prev_block = &obj.train.get_layout().get_block(id);
395 catch(const key_error &)