1 #include <msp/core/maputils.h>
2 #include <msp/core/raii.h>
3 #include "blockallocator.h"
8 #include "trackcircuit.h"
18 BlockAllocator::BlockAllocator(Train &t):
20 cur_blocks_end(blocks.end()),
25 Layout &layout = train.get_layout();
26 layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
27 layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::sensor_state_changed));
29 const set<Track *> &tracks = layout.get_all<Track>();
30 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
31 if((*i)->get_turnout_id())
32 (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
35 void BlockAllocator::start_from(const BlockIter &block)
38 throw invalid_argument("BlockAllocator::start_from");
44 void BlockAllocator::clear()
46 release_blocks_begin(blocks.end());
51 void BlockAllocator::stop_at(const Block *block)
53 stop_at_block = block;
56 const BlockIter &BlockAllocator::first() const
59 throw logic_error("no blocks");
60 return blocks.front();
63 const BlockIter &BlockAllocator::last() const
66 throw logic_error("no blocks");
67 BlockList::const_iterator i = --blocks.end();
68 if(i->block()==pending_block)
73 const BlockIter &BlockAllocator::last_current() const
76 throw logic_error("no blocks");
77 if(cur_blocks_end==blocks.begin())
78 throw logic_error("internal error (no current blocks)");
79 BlockList::const_iterator i = cur_blocks_end;
83 const BlockIter &BlockAllocator::iter_for(const Block &block) const
85 BlockList::const_iterator i = find_block(blocks.begin(), blocks.end(), block);
87 throw key_error(&block);
91 bool BlockAllocator::has_block(const Block &block) const
93 return find_block(blocks.begin(), blocks.end(), block)!=blocks.end();
96 bool BlockAllocator::is_block_current(const Block &block) const
98 return find_block(blocks.begin(), cur_blocks_end, block)!=cur_blocks_end;
101 BlockAllocator::BlockList::const_iterator BlockAllocator::find_block(const BlockList::const_iterator &begin, const BlockList::const_iterator &end, const Block &block) const
103 BlockList::const_iterator i;
104 for(i=begin; (i!=end && &**i!=&block); ++i) ;
108 void BlockAllocator::reserve_more()
111 throw logic_error("no blocks");
113 BlockIter start = blocks.back();
114 if(&*start==stop_at_block)
116 else if(&*start==pending_block)
118 TrackIter track = start.track_iter();
119 if(!track.endpoint().has_path(track->get_active_path()))
125 // See how many sensor blocks and how much track we already have
128 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
130 if((*i)->get_sensor_id())
133 dist += (*i)->get_path_length(i->entry());
136 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
137 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
139 BlockIter block = start;
141 SetFlag setf(reserving);
145 BlockIter prev = block;
146 block = block.next();
147 if(!block || block->get_endpoints().size()<2)
148 // The track ends here
151 if(block->get_turnout_id() && !prev->get_turnout_id())
153 /* We are arriving at a turnout. See if we have enough blocks and
154 distance reserved. */
155 if(nsens>=3 && dist>=min_dist)
159 if(!reserve_block(block))
162 if(cur_blocks_end==blocks.end())
165 TrackIter track = block.track_iter();
166 if(track->is_path_changing())
168 pending_block = &*block;
173 const TrackType::Endpoint &entry_ep = track.endpoint();
174 unsigned path = track->get_active_path();
175 if(!entry_ep.has_path(path))
177 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
178 if(entry_ep.has_common_paths(exit_ep))
180 unsigned mask = entry_ep.paths&exit_ep.paths;
181 for(path=0; mask>1; ++path, mask>>=1) ;
183 track->set_active_path(path);
184 if(track->is_path_changing())
186 pending_block = &*block;
191 // XXX Do something here
196 if(&*block==stop_at_block)
199 if(block->get_sensor_id())
202 dist += block->get_path_length(block.entry());
205 // Make any sensorless blocks at the beginning immediately current
206 while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id())
210 bool BlockAllocator::reserve_block(const BlockIter &block)
212 /* Add it to the list first to present a consistent state in block_reserved
214 blocks.push_back(block);
217 if(!block->reserve(&train))
232 bool BlockAllocator::release_from(const Block &block)
234 bool have_sensor = false;
235 for(BlockList::iterator i=cur_blocks_end; i!=blocks.end(); ++i)
237 if(i->block()==&block)
240 release_blocks_end(i);
243 else if((*i)->get_sensor_id())
250 void BlockAllocator::release_noncurrent()
252 release_blocks_end(cur_blocks_end);
255 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
257 for(BlockList::iterator i=blocks.begin(); i!=end; )
261 void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
263 // Guard against decrementing blocks.begin()
264 if(begin==blocks.begin())
265 return release_blocks_begin(blocks.end());
267 /* Release the blocks in reverse order so that a consistent state is
268 presented in block_reserved signal. */
270 for(BlockList::iterator i=blocks.end(); !done; )
277 void BlockAllocator::release_block(const BlockList::iterator &i)
279 if(i==cur_blocks_end)
281 if(&**i==pending_block)
289 void BlockAllocator::reverse()
291 release_noncurrent();
293 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
297 void BlockAllocator::turnout_path_changed(Track &track)
299 if(&track.get_block()==pending_block && !reserving)
303 void BlockAllocator::block_reserved(Block &block, const Train *tr)
305 if(&block==pending_block && !tr && !reserving)
309 void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
312 if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
313 block = &tc->get_block();
317 if(block->get_train()!=&train)
320 if(state==Sensor::MAYBE_ACTIVE)
322 // Find the first sensor block from our reserved blocks that isn't this sensor
323 BlockList::iterator end;
325 for(end=cur_blocks_end; end!=blocks.end(); ++end)
326 if((*end)->get_sensor_id())
343 // Move blocks up to the next sensor to our current blocks
344 for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
345 train.signal_advanced.emit(**j);
346 cur_blocks_end = end;
349 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
351 else if(state==Sensor::INACTIVE)
353 const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1);
354 const Block &veh_block = veh.get_track()->get_block();
355 const Driver &driver = train.get_layout().get_driver();
357 /* Sensors aren't guaranteed to be detriggered in order. Go through the
358 block list and locate the first sensor that's still active. */
359 BlockList::iterator end = blocks.end();
360 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
362 // Avoid freeing blocks that still hold the train's vehicles
366 if((*i)->get_sensor_id())
368 if(driver.get_sensor((*i)->get_sensor_id()))
375 if(end!=blocks.end())
376 // Free blocks up to the last inactive sensor
377 release_blocks_begin(++end);
381 void BlockAllocator::save(list<DataFile::Statement> &st) const
383 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
385 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
387 if(train.get_controller().get_reverse())
389 cur_blocks.reverse();
390 prev = cur_blocks.front().next();
393 prev = cur_blocks.front().flip();
395 st.push_back((DataFile::Statement("hint"), prev->get_id()));
397 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
398 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
403 BlockAllocator::Loader::Loader(BlockAllocator &ba):
404 DataFile::ObjectLoader<BlockAllocator>(ba),
407 add("block", &Loader::block);
408 add("hint", &Loader::hint);
411 void BlockAllocator::Loader::block(unsigned id)
419 blk = &obj.train.get_layout().get_block(id);
421 catch(const key_error &)
429 entry = blk->get_endpoint_by_link(*prev_block);
433 obj.blocks.push_back(BlockIter(blk, entry));
434 blk->reserve(&obj.train);
436 if(blk->get_sensor_id())
437 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
442 void BlockAllocator::Loader::hint(unsigned id)
446 prev_block = &obj.train.get_layout().get_block(id);
448 catch(const key_error &)