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()),
35 Layout &layout = train.get_layout();
36 layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
37 layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::sensor_state_changed));
39 const set<Track *> &tracks = layout.get_all<Track>();
40 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
41 if((*i)->get_turnout_id())
42 (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
45 void BlockAllocator::start_from(const BlockIter &block)
48 throw invalid_argument("BlockAllocator::start_from");
54 void BlockAllocator::clear()
56 release_blocks_begin(blocks.end());
61 void BlockAllocator::stop_at(const Block *block)
63 stop_at_block = block;
66 const BlockIter &BlockAllocator::first() const
69 throw logic_error("no blocks");
70 return blocks.front();
73 const BlockIter &BlockAllocator::last() const
76 throw logic_error("no blocks");
77 BlockList::const_iterator i = --blocks.end();
78 if(i->block()==pending_block)
83 const BlockIter &BlockAllocator::last_current() const
86 throw logic_error("no blocks");
87 if(cur_blocks_end==blocks.begin())
88 throw logic_error("internal error (no current blocks)");
89 BlockList::const_iterator i = cur_blocks_end;
93 const BlockIter &BlockAllocator::iter_for(const Block &block) const
95 BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(block));
97 throw key_error(&block);
101 bool BlockAllocator::has_block(const Block &block) const
103 return find_if(blocks.begin(), blocks.end(), BlockMatch(block))!=blocks.end();
106 bool BlockAllocator::is_block_current(const Block &block) const
108 BlockList::const_iterator end = cur_blocks_end;
109 return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end;
112 void BlockAllocator::reserve_more()
115 throw logic_error("no blocks");
117 BlockIter start = blocks.back();
118 if(&*start==stop_at_block)
120 else if(&*start==pending_block)
122 TrackIter track = start.track_iter();
123 if(!track.endpoint().has_path(track->get_active_path()))
129 // See how many sensor blocks and how much track we already have
132 for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
134 if((*i)->get_sensor_id())
137 dist += (*i)->get_path_length(i->entry());
140 float approach_margin = 50*train.get_layout().get_catalogue().get_scale();
141 float min_dist = train.get_controller().get_braking_distance()*1.3+approach_margin*2;
143 BlockIter block = start;
145 SetFlag setf(reserving);
149 BlockIter prev = block;
150 block = block.next();
151 if(!block || block->get_endpoints().size()<2)
152 // The track ends here
155 if(block->get_turnout_id() && !prev->get_turnout_id())
157 /* We are arriving at a turnout. See if we have enough blocks and
158 distance reserved. */
159 if(nsens>=3 && dist>=min_dist)
163 if(!reserve_block(block))
166 if(cur_blocks_end==blocks.end())
169 TrackIter track = block.track_iter();
170 if(track->is_path_changing())
172 pending_block = &*block;
177 const TrackType::Endpoint &entry_ep = track.endpoint();
178 unsigned path = track->get_active_path();
179 if(!entry_ep.has_path(path))
181 const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
182 if(entry_ep.has_common_paths(exit_ep))
184 unsigned mask = entry_ep.paths&exit_ep.paths;
185 for(path=0; mask>1; ++path, mask>>=1) ;
187 track->set_active_path(path);
188 if(track->is_path_changing())
190 pending_block = &*block;
195 // XXX Do something here
200 if(&*block==stop_at_block)
203 if(block->get_sensor_id())
206 dist += block->get_path_length(block.entry());
209 // Make any sensorless blocks at the beginning immediately current
210 while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id())
214 bool BlockAllocator::reserve_block(const BlockIter &block)
216 /* Add it to the list first to present a consistent state in block_reserved
218 blocks.push_back(block);
221 if(!block->reserve(&train))
236 bool BlockAllocator::release_from(const Block &block)
238 bool have_sensor = false;
239 for(BlockList::iterator i=cur_blocks_end; i!=blocks.end(); ++i)
241 if(i->block()==&block)
244 release_blocks_end(i);
247 else if((*i)->get_sensor_id())
254 void BlockAllocator::release_noncurrent()
256 release_blocks_end(cur_blocks_end);
259 void BlockAllocator::release_blocks_begin(const BlockList::iterator &end)
261 for(BlockList::iterator i=blocks.begin(); i!=end; )
265 void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
267 // Guard against decrementing blocks.begin()
268 if(begin==blocks.begin())
269 return release_blocks_begin(blocks.end());
271 if(begin==blocks.end())
274 /* Release the blocks in reverse order so that a consistent state is
275 presented in block_reserved signal. */
277 for(BlockList::iterator i=--blocks.end(); !done; )
284 void BlockAllocator::release_block(const BlockList::iterator &i)
286 if(i==cur_blocks_end)
288 if(&**i==pending_block)
296 void BlockAllocator::reverse()
298 release_noncurrent();
300 for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
304 void BlockAllocator::turnout_path_changed(Track &track)
306 if(&track.get_block()==pending_block && !reserving)
310 void BlockAllocator::block_reserved(Block &block, const Train *tr)
312 if(&block==pending_block && !tr && !reserving)
316 void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
319 if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
320 block = &tc->get_block();
324 if(block->get_train()!=&train)
327 if(state==Sensor::MAYBE_ACTIVE)
329 // Find the first sensor block from our reserved blocks that isn't this sensor
330 BlockList::iterator end;
332 for(end=cur_blocks_end; end!=blocks.end(); ++end)
333 if((*end)->get_sensor_id())
350 // Move blocks up to the next sensor to our current blocks
351 for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
352 train.signal_advanced.emit(**j);
353 cur_blocks_end = end;
356 train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
358 else if(state==Sensor::INACTIVE)
360 const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1);
361 const Block &veh_block = veh.get_track()->get_block();
362 const Driver &driver = train.get_layout().get_driver();
364 /* Sensors aren't guaranteed to be detriggered in order. Go through the
365 block list and locate the first sensor that's still active. */
366 BlockList::iterator end = blocks.end();
367 for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
369 // Avoid freeing blocks that still hold the train's vehicles
373 if((*i)->get_sensor_id())
375 if(driver.get_sensor((*i)->get_sensor_id()))
382 if(end!=blocks.end())
383 // Free blocks up to the last inactive sensor
384 release_blocks_begin(++end);
388 void BlockAllocator::save(list<DataFile::Statement> &st) const
390 if(!blocks.empty() && cur_blocks_end!=blocks.begin())
392 BlockList cur_blocks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
394 if(train.get_controller().get_reverse())
396 cur_blocks.reverse();
397 prev = cur_blocks.front().next();
400 prev = cur_blocks.front().flip();
402 st.push_back((DataFile::Statement("hint"), prev->get_id()));
404 for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
405 st.push_back((DataFile::Statement("block"), (*i)->get_id()));
410 BlockAllocator::Loader::Loader(BlockAllocator &ba):
411 DataFile::ObjectLoader<BlockAllocator>(ba),
414 add("block", &Loader::block);
415 add("hint", &Loader::hint);
418 void BlockAllocator::Loader::block(unsigned id)
426 blk = &obj.train.get_layout().get_block(id);
428 catch(const key_error &)
436 entry = blk->get_endpoint_by_link(*prev_block);
440 obj.blocks.push_back(BlockIter(blk, entry));
441 blk->reserve(&obj.train);
443 if(blk->get_sensor_id())
444 obj.train.get_layout().get_driver().set_sensor(blk->get_sensor_id(), true);
449 void BlockAllocator::Loader::hint(unsigned id)
453 prev_block = &obj.train.get_layout().get_block(id);
455 catch(const key_error &)