address(a),
priority(0),
yielding_to(0),
+ cur_blocks_end(blocks.end()),
pending_block(0),
reserving(false),
advancing(false),
free_noncritical_blocks();
Route *lead = 0;
- if(r && !cur_blocks.empty())
+ if(r && !blocks.empty())
{
- TrackIter first = cur_blocks.front().track_iter();
- TrackIter next = (rsv_blocks.empty() ? cur_blocks : rsv_blocks).back().next().track_iter();
+ TrackIter first = blocks.front().track_iter();
+ TrackIter next = blocks.back().next().track_iter();
if(!r->has_track(*next))
{
lead = Route::find(next, *r);
bool Train::go_to(Track &to)
{
- for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
+ for(BlockList::const_iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
if((*i)->has_track(to))
{
signal_arrived.emit();
free_noncritical_blocks();
- TrackIter next = (rsv_blocks.empty() ? cur_blocks : rsv_blocks).back().next().track_iter();
+ TrackIter next = blocks.back().next().track_iter();
Route *route = Route::find(next, to);
if(!route)
list<RouteRef>::iterator route = routes.begin();
// Follow our routes to find out where we're entering the turnout
- for(TrackLoopIter track = cur_blocks.back().track_iter();;)
+ for(TrackLoopIter track = blocks.front().track_iter();;)
{
if(!advance_route(route, *track))
return false;
break;
}
- unsigned tid = track->get_turnout_id();
- track = track.next(tid ? route->route->get_turnout(tid) : 0);
+ track = track.next(route->route->get_path(*track));
if(!track || track.looped())
return false;
else if(!diversion->has_track(*track))
throw LogicError("Pathfinder returned a bad route");
- unsigned tid = track->get_turnout_id();
- track = track.next(tid ? diversion->get_turnout(tid) : 0);
+ track = track.next(diversion->get_path(*track));
}
if(route==end)
if(controller->get_speed())
throw InvalidState("Must be stopped before placing");
- release_blocks(rsv_blocks);
- release_blocks(cur_blocks);
+ release_blocks();
set_active(false);
accurate_position = false;
return;
}
- cur_blocks.push_back(BlockIter(&block, entry));
+ blocks.push_back(BlockIter(&block, entry));
if(reverse)
{
TrackIter track = BlockIter(&block, entry).reverse().track_iter();
if(controller->get_speed())
throw InvalidState("Must be stopped before unplacing");
- release_blocks(rsv_blocks);
- release_blocks(cur_blocks);
+ release_blocks();
set_active(false);
accurate_position = false;
return false;
unsigned nsens = 0;
- for(BlockList::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+ for(BlockList::iterator i=cur_blocks_end; i!=blocks.end(); ++i)
{
if(i->block()==&block)
{
if(nsens<1)
return false;
- release_blocks(rsv_blocks, i, rsv_blocks.end());
+ release_blocks(i, blocks.end());
return true;
}
else if((*i)->get_sensor_id())
void Train::free_noncritical_blocks()
{
- if(cur_blocks.empty() || rsv_blocks.empty())
+ if(blocks.empty())
return;
if(controller->get_speed()==0)
{
- release_blocks(rsv_blocks);
+ release_blocks(cur_blocks_end, blocks.end());
return;
}
Vehicle &veh = *(reverse ? vehicles.back() : vehicles.front());
TrackIter track(veh.get_track(), veh.get_entry());
- BlockList::iterator block = cur_blocks.begin();
+ BlockList::iterator block = blocks.begin();
bool in_rsv = false;
- while(block!=rsv_blocks.end() && !(*block)->has_track(*track))
+ while(block!=blocks.end() && !(*block)->has_track(*track))
{
++block;
- if(block==cur_blocks.end())
- {
- block = rsv_blocks.begin();
+ if(block==cur_blocks_end)
in_rsv = true;
- }
}
float dist = veh.get_offset();
if(!(*block)->has_track(*track))
{
++block;
- if(block==cur_blocks.end())
- {
- block = rsv_blocks.begin();
+ if(block==cur_blocks_end)
in_rsv = true;
- }
- if(block==rsv_blocks.end())
+ if(block==blocks.end())
return;
if(dist>min_dist && nsens>0)
{
- release_blocks(rsv_blocks, block, rsv_blocks.end());
+ release_blocks(block, blocks.end());
return;
}
int Train::get_entry_to_block(Block &block) const
{
- for(BlockList::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
- if(i->block()==&block)
- return i->entry();
- for(BlockList::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+ for(BlockList::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
if(i->block()==&block)
return i->entry();
return -1;
{
if(!active && stop_timeout && t>=stop_timeout)
{
- release_blocks(rsv_blocks);
+ release_blocks(cur_blocks_end, blocks.end());
end_of_route = false;
stop_timeout = Time::TimeStamp();
}
reverse = controller->get_reverse();
driver.set_loco_reverse(address, reverse);
- release_blocks(rsv_blocks);
- reverse_blocks(cur_blocks);
+ release_blocks(cur_blocks_end, blocks.end());
+ reverse_blocks(blocks);
reserve_more();
}
Track *track = vehicle.get_track();
bool ok = false;
- for(BlockList::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
+ for(BlockList::const_iterator i=blocks.begin(); (!ok && i!=cur_blocks_end); ++i)
ok = (*i)->has_track(*track);
float d = get_real_speed(current_speed)*(dt/Time::sec);
}
}
}
- else if(end_of_route && rsv_blocks.empty())
+ else if(end_of_route && cur_blocks_end==blocks.end())
{
set_active(false);
signal_arrived.emit();
set_route(0);
}
- if(!cur_blocks.empty() && !cur_blocks.front()->get_sensor_id())
+ if(!blocks.empty() && !blocks.front()->get_sensor_id())
{
- float dist = get_reserved_distance_until(&*cur_blocks.front(), true);
+ float dist = get_reserved_distance_until(&*blocks.front(), true);
if(dist>10*layout.get_catalogue().get_scale())
{
- cur_blocks.front()->reserve(0);
- cur_blocks.pop_front();
+ blocks.front()->reserve(0);
+ blocks.pop_front();
}
}
}
if(real_speed[i].weight)
st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
- if(!cur_blocks.empty())
+ if(!blocks.empty() && cur_blocks_end!=blocks.begin())
{
- BlockList blocks = cur_blocks;
+ BlockList blks(blocks.begin(), BlockList::const_iterator(cur_blocks_end));
if(reverse)
- reverse_blocks(blocks);
+ reverse_blocks(blks);
- BlockIter prev = blocks.front().flip();
+ BlockIter prev = blks.front().flip();
st.push_back((DataFile::Statement("block_hint"), prev->get_id()));
- for(BlockList::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ for(BlockList::const_iterator i=blks.begin(); i!=blks.end(); ++i)
st.push_back((DataFile::Statement("block"), (*i)->get_id()));
}
if(state)
{
// Find the first sensor block from our reserved blocks that isn't this sensor
- BlockList::iterator i;
+ BlockList::iterator end;
unsigned result = 0;
- for(i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
- if((*i)->get_sensor_id())
+ for(end=cur_blocks_end; end!=blocks.end(); ++end)
+ if((*end)->get_sensor_id())
{
- if((*i)->get_sensor_id()!=addr)
+ if((*end)->get_sensor_id()!=addr)
{
if(result==0)
result = 2;
result = 3;
}
- if(result==1 && i!=rsv_blocks.begin())
+ if(result==1)
{
// Compute speed and update related state
float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
}
travel_dist = 0;
- for(BlockList::iterator j=rsv_blocks.begin(); j!=i; ++j)
+ for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
{
travel_dist += (*j)->get_path_length(j->entry());
if(routes.size()>1)
{
const set<Track *> &rtracks = (++routes.begin())->route->get_tracks();
- for(BlockList::iterator j=rsv_blocks.begin(); j!=i; ++j)
+ for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
if(rtracks.count((*j)->get_endpoints()[j->entry()].track))
{
routes.pop_front();
}
// Move blocks up to the next sensor to our current blocks
- cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
+ cur_blocks_end = end;
// Try to get more blocks if we're moving
if(active)
const Vehicle &veh = *(reverse ? vehicles.front() : vehicles.back());
// Find the first sensor in our current blocks that's still active
- BlockList::iterator end = cur_blocks.begin();
- for(BlockList::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
+ BlockList::iterator end = blocks.begin();
+ for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
{
if((*i)->has_track(*veh.get_track()))
break;
}
}
- if(end!=cur_blocks.begin() && end!=cur_blocks.end())
+ if(end!=blocks.begin() && end!=cur_blocks_end)
// Free blocks up to the last inactive sensor
- release_blocks(cur_blocks, cur_blocks.begin(), end);
+ release_blocks(blocks.begin(), end);
}
}
unsigned Train::reserve_more()
{
- if(!active)
+ if(!active || blocks.empty())
return 0;
- BlockIter start;
- if(!rsv_blocks.empty())
- start = rsv_blocks.back();
- else if(!cur_blocks.empty())
- start = cur_blocks.back();
- else
- return 0;
+ BlockIter start = blocks.back();
pending_block = 0;
// See how many sensor blocks and how much track we already have
unsigned nsens = 0;
float dist = 0;
- for(BlockList::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+ for(BlockList::const_iterator i=cur_blocks_end; i!=blocks.end(); ++i)
{
if((*i)->get_sensor_id())
++nsens;
if(blocking_train->free_block(*contested_blocks.back()))
{
// Roll back and start actually reserving the blocks
- block = rsv_blocks.back();
+ block = blocks.back();
cur_route = routes.begin();
advance_route(cur_route, *block.track_iter().track());
if(blocking_train->get_priority()==priority)
if(!contested_blocks.empty() && contested_blocks.front()==block)
contested_blocks.pop_front();
- rsv_blocks.push_back(block);
+ blocks.push_back(block);
+ if(cur_blocks_end==blocks.end())
+ --cur_blocks_end;
if(block->get_sensor_id())
++nsens;
if(nsens>0)
}
// Unreserve blocks that were not good
- while(!rsv_blocks.empty() && rsv_blocks.back()!=good)
+ while(blocks.end()!=cur_blocks_end && blocks.back()!=good)
{
- rsv_blocks.back()->reserve(0);
- rsv_blocks.pop_back();
+ if(--blocks.end()==cur_blocks_end)
+ cur_blocks_end = blocks.end();
+ blocks.back()->reserve(0);
+ blocks.pop_back();
}
- if(!rsv_blocks.empty() && rsv_blocks.back()!=start)
+ if(!blocks.back()!=start)
// We got some new blocks, so no longer need to yield
yielding_to = 0;
// Make any sensorless blocks at the beginning immediately current
- BlockList::iterator i;
- for(i=rsv_blocks.begin(); (i!=rsv_blocks.end() && !(*i)->get_sensor_id()); ++i) ;
- if(i!=rsv_blocks.begin())
- cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
+ while(cur_blocks_end!=blocks.end() && !(*cur_blocks_end)->get_sensor_id())
+ ++cur_blocks_end;
if(try_divert && divert(*divert_track))
return reserve_more();
float Train::get_reserved_distance_until(const Block *until_block, bool back) const
{
- if(cur_blocks.empty())
+ if(blocks.empty())
return 0;
Vehicle &veh = *(reverse!=back ? vehicles.back() : vehicles.front());
const VehicleType &vtype = veh.get_type();
TrackIter track(veh.get_track(), veh.get_entry());
- if(!track)
+ if(!track) // XXX Probably unnecessary
return 0;
- BlockList::const_iterator block = cur_blocks.begin();
- while(block!=rsv_blocks.end() && !(*block)->has_track(*track))
- {
+ BlockList::const_iterator block = blocks.begin();
+ while(block!=blocks.end() && !(*block)->has_track(*track))
++block;
- if(block==cur_blocks.end())
- {
- if(back)
- return 0;
- block = rsv_blocks.begin();
- }
- }
- if(block==rsv_blocks.end() || &**block==until_block)
+ if(block==blocks.end() || &**block==until_block)
return 0;
float result = veh.get_offset();
{
if(back)
{
- if(block==cur_blocks.begin())
+ if(block==blocks.begin())
break;
--block;
}
else
{
++block;
- if(block==cur_blocks.end())
- block = rsv_blocks.begin();
- if(block==rsv_blocks.end())
+ if(block==blocks.end())
break;
}
signal_status_changed.emit(s);
}
-void Train::release_blocks(BlockList &blocks)
+void Train::release_blocks()
{
- release_blocks(blocks, blocks.begin(), blocks.end());
+ release_blocks(blocks.begin(), blocks.end());
}
-void Train::release_blocks(BlockList &blocks, BlockList::iterator begin, BlockList::iterator end)
+void Train::release_blocks(BlockList::iterator begin, BlockList::iterator end)
{
while(begin!=end)
{
+ if(begin==cur_blocks_end)
+ cur_blocks_end = end;
+
Block &block = **begin;
blocks.erase(begin++);
block.reserve(0);
}
}
-void Train::reverse_blocks(BlockList &blocks) const
+void Train::reverse_blocks(BlockList &blks) const
{
- blocks.reverse();
- for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ blks.reverse();
+ for(BlockList::iterator i=blks.begin(); i!=blks.end(); ++i)
*i = i->reverse();
}
}
set<Track *> tracks;
- for(BlockList::iterator i=cur_blocks.begin(); i!=rsv_blocks.end(); )
+ for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i)
{
const set<Track *> &btracks = (*i)->get_tracks();
for(set<Track *>::const_iterator j=btracks.begin(); j!=btracks.end(); ++j)
if(!target || !target->has_track(**j))
tracks.insert(*j);
-
- if(++i==cur_blocks.end())
- i = rsv_blocks.begin();
}
lead->add_tracks(tracks);
TrackLoopIter track1 = from;
while(diversion.has_track(*track1))
{
- unsigned tid = track1->get_turnout_id();
- unsigned path = (tid ? diversion.get_turnout(tid) : 0);
+ unsigned path = diversion.get_path(*track1);
diversion_len += track1->get_type().get_path_length(path);
track1 = track1.next(path);
TrackLoopIter track2 = from;
while(1)
{
- unsigned tid = track2->get_turnout_id();
- unsigned path = (tid ? route->route->get_turnout(tid) : 0);
+ unsigned path = route->route->get_path(*track2);
route_len += track2->get_type().get_path_length(path);
bool ok = (track2!=from && diversion.has_track(*track2));
void Train::Loader::finish()
{
- if(!obj.cur_blocks.empty())
+ if(!obj.blocks.empty())
{
- TrackIter track = obj.cur_blocks.front().track_iter();
+ TrackIter track = obj.blocks.front().track_iter();
float offset = 2*obj.layout.get_catalogue().get_scale();
obj.vehicles.back()->place(*track, track.entry(), offset, Vehicle::BACK_BUFFER);
entry = 0;
blk->reserve(&obj);
- obj.cur_blocks.push_back(BlockIter(blk, entry));
+ obj.blocks.push_back(BlockIter(blk, entry));
if(blk->get_sensor_id())
obj.layout.get_driver().set_sensor(blk->get_sensor_id(), true);