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");
42 void BlockAllocator::clear()
44 release_blocks_begin(blocks.end());
49 void BlockAllocator::stop_at(const Block *block)
51 stop_at_block = block;
54 const BlockIter &BlockAllocator::first() const
57 throw logic_error("no blocks");
58 return blocks.front();
61 const BlockIter &BlockAllocator::last() const
64 throw logic_error("no blocks");
65 BlockList::const_iterator i = --blocks.end();
66 if(i->block()==pending_block)
71 const BlockIter &BlockAllocator::last_current() const
74 throw logic_error("no blocks");
75 if(cur_blocks_end==blocks.begin())
76 throw logic_error("internal error (no current blocks)");
77 BlockList::const_iterator i = cur_blocks_end;
81 const BlockIter &BlockAllocator::iter_for(const Block &block) const
83 BlockList::const_iterator i = find_block(blocks.begin(), blocks.end(), block);
85 throw key_error(&block);
89 bool BlockAllocator::has_block(const Block &block) const
91 return find_block(blocks.begin(), blocks.end(), block)!=blocks.end();
94 bool BlockAllocator::is_block_current(const Block &block) const
96 return find_block(blocks.begin(), cur_blocks_end, block)!=cur_blocks_end;
99 BlockAllocator::BlockList::const_iterator BlockAllocator::find_block(const BlockList::const_iterator &begin, const BlockList::const_iterator &end, const Block &block) const
101 BlockList::const_iterator i;
102 for(i=begin; (i!=end && &**i!=&block); ++i) ;
106 void BlockAllocator::reserve_more()
109 throw logic_error("no blocks");
111 BlockIter start = blocks.back();
112 if(&*start==stop_at_block)
114 else if(&*start==pending_block)
116 TrackIter track = start.track_iter();
117 if(!track.endpoint().has_path(track->get_active_path()))
123 // See how many sensor blocks and how much track we already have
126 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
128 if((*i)->get_sensor_id())
131 dist += (*i)->get_path_length(i->entry());
134 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
135 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
137 BlockIter block = start;
139 SetFlag setf(reserving);
143 BlockIter prev = block;
144 block = block.next();
145 if(!block || block->get_endpoints().size()<2)
146 // The track ends here
149 if(block->get_turnout_id() && !prev->get_turnout_id())
151 /* We are arriving at a turnout. See if we have enough blocks and
152 distance reserved. */
153 if(nsens>=3 && dist>=min_dist)
157 if(!reserve_block(block))
160 if(cur_blocks_end==blocks.end())
163 TrackIter track = block.track_iter();
164 if(track->is_path_changing())
166 pending_block = &*block;
171 const TrackType::Endpoint &entry_ep = track.endpoint();
172 unsigned path = track->get_active_path();
173 if(!entry_ep.has_path(path))
175 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
176 if(entry_ep.has_common_paths(exit_ep))
178 unsigned mask = entry_ep.paths&exit_ep.paths;
179 for(path=0; mask>1; ++path, mask>>=1) ;
181 track->set_active_path(path);
182 if(track->is_path_changing())
184 pending_block = &*block;
189 // XXX Do something here
194 if(&*block==stop_at_block)
197 if(block->get_sensor_id())
200 dist += block->get_path_length(block.entry());
203 // Make any sensorless blocks at the beginning immediately current
204 while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id())
208 bool BlockAllocator::reserve_block(const BlockIter &block)
210 /* Add it to the list first to present a consistent state in block_reserved
212 blocks.push_back(block);
215 if(!block->reserve(&train))
230 void BlockAllocator::release_until(const Block &block)
232 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
233 if(i->block()==&block)
235 if(++i!=cur_blocks_end)
236 release_blocks_begin(i);
241 bool BlockAllocator::release_from(const Block &block)
243 bool have_sensor = false;
244 for(BlockList::iterator i=cur_blocks_end; i!=blocks.end(); ++i)
246 if(i->block()==&block)
249 release_blocks_end(i);
252 else if((*i)->get_sensor_id())
259 void BlockAllocator::release_noncurrent()
261 release_blocks_end(cur_blocks_end);
264 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
266 for(BlockList::iterator i=blocks.begin(); i!=end; )
270 void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
272 // Guard against decrementing blocks.begin()
273 if(begin==blocks.begin())
274 return release_blocks_begin(blocks.end());
276 /* Release the blocks in reverse order so that a consistent state is
277 presented in block_reserved signal. */
279 for(BlockList::iterator i=blocks.end(); !done; )
286 void BlockAllocator::release_block(const BlockList::iterator &i)
288 if(i==cur_blocks_end)
290 if(&**i==pending_block)
298 void BlockAllocator::reverse()
300 release_noncurrent();
302 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
306 void BlockAllocator::turnout_path_changed(Track &track)
308 if(&track.get_block()==pending_block && !reserving)
312 void BlockAllocator::block_reserved(Block &block, const Train *tr)
314 if(&block==pending_block && !tr && !reserving)
318 void BlockAllocator::block_state_changed(Block &block, Block::State state)
320 if(block.get_train()!=&train)
323 if(state==Block::MAYBE_ACTIVE)
325 // Find the first sensor block from our reserved blocks that isn't this sensor
326 BlockList::iterator end;
328 for(end=cur_blocks_end; end!=blocks.end(); ++end)
329 if((*end)->get_sensor_id())
346 // Move blocks up to the next sensor to our current blocks
347 for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
348 train.signal_advanced.emit(**j);
349 cur_blocks_end = end;
352 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
354 else if(state==Block::INACTIVE)
356 const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1);
357 const Block &veh_block = veh.get_track()->get_block();
358 const Driver &driver = train.get_layout().get_driver();
360 /* Sensors aren't guaranteed to be detriggered in order. Go through the
361 block list and locate the first sensor that's still active. */
362 BlockList::iterator end = blocks.end();
363 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
365 // Avoid freeing blocks that still hold the train's vehicles
369 if((*i)->get_sensor_id())
371 if(driver.get_sensor((*i)->get_sensor_id()))
378 if(end!=blocks.end())
379 // Free blocks up to the last inactive sensor
380 release_blocks_begin(++end);
384 void BlockAllocator::save(list<DataFile::Statement> &st) const
386 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
388 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
390 if(train.get_controller().get_reverse())
392 cur_blocks.reverse();
393 prev = cur_blocks.front().next();
396 prev = cur_blocks.front().flip();
398 st.push_back((DataFile::Statement("hint"), prev->get_id()));
400 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
401 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
406 BlockAllocator::Loader::Loader(BlockAllocator &ba):
407 DataFile::ObjectLoader<BlockAllocator>(ba),
410 add("block", &Loader::block);
411 add("hint", &Loader::hint);
414 void BlockAllocator::Loader::block(unsigned id)
422 blk = &obj.train.get_layout().get_block(id);
424 catch(const key_error &)
432 entry = blk->get_endpoint_by_link(*prev_block);
436 obj.blocks.push_back(BlockIter(blk, entry));
437 blk->reserve(&obj.train);
439 if(blk->get_sensor_id())
440 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
445 void BlockAllocator::Loader::hint(unsigned id)
449 prev_block = &obj.train.get_layout().get_block(id);
451 catch(const key_error &)