vehicles.back()->attach_back(*veh);
vehicles.push_back(veh);
veh->set_train(this);
+ signal_vehicle_added.emit(vehicles.size()-1, *veh);
}
void Train::remove_vehicle(unsigned i)
throw out_of_range("Train::remove_vehicle");
if(i==0)
throw logic_error("can't remove locomotive");
- delete vehicles[i];
+
+ Vehicle *veh = vehicles[i];
vehicles.erase(vehicles.begin()+i);
+ veh->detach_front();
if(i<vehicles.size())
+ {
+ veh->detach_back();
vehicles[i-1]->attach_back(*vehicles[i]);
+ }
+ signal_vehicle_removed.emit(i, *veh);
+ delete veh;
}
unsigned Train::get_n_vehicles() const
allocator.start_from(block);
accurate_position = false;
+ last_entry_block = BlockIter();
if(reverse)
vehicles.front()->place(block.reverse().track_iter(), 0, Vehicle::FRONT_BUFFER);
allocator.clear();
accurate_position = false;
+ last_entry_block = BlockIter();
for(vector<Vehicle *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
(*i)->unplace();
driver.set_loco_reverse(address, r);
allocator.reverse();
+ last_entry_block = BlockIter();
}
if(speed_quantizer)
}
}
}
+ else if(intent_to_move && !allocator.is_active())
+ allocator.set_active(true);
else if(allocator.is_active() && !intent_to_move && !stop_timeout)
stop_timeout = 2*Time::sec;
}
void Train::sensor_state_changed(Sensor &sensor, Sensor::State state)
{
- Block *block = 0;
- if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
- block = &tc->get_block();
- else
+ if(!current_speed_step || state!=Sensor::MAYBE_ACTIVE)
return;
- if(block->get_train()==this && state==Sensor::MAYBE_ACTIVE)
+ Block *block = sensor.get_block();
+ if(!block || block->get_train()!=this)
+ return;
+
+ if(last_entry_block && &*last_entry_block!=block)
+ {
+ for(BlockIter i=last_entry_block.next(); (i && &*i!=block); i=i.next())
+ if(i->get_train()!=this || i->get_sensor_id())
+ return;
+ }
+
+ if(dynamic_cast<TrackCircuit *>(&sensor))
{
- if(last_entry_block)
+ if(last_entry_block && pure_speed && speed_quantizer)
{
- float travel_distance = -1;
- if(pure_speed && speed_quantizer && current_speed_step>0)
- travel_distance = 0;
+ float travel_distance = 0;
for(BlockIter i=last_entry_block; &*i!=block; i=i.next())
- {
- if(i->get_sensor_id())
- return;
- if(travel_distance>=0)
- travel_distance += i->get_path_length(i.entry());
- }
+ travel_distance += i->get_path_length(i.entry());
if(travel_distance>0)
{
if(!track) // XXX Probably unnecessary
return 0;
- BlockIter block = track.block_iter();
- if(&*block==until_block)
+ if(&track->get_block()==until_block)
return 0;
// Account for the vehicle's offset on its current track
result = track->get_type().get_path_length(track->get_active_path())-result;
result -= veh.get_type().get_length()/2;
+ BlockIter block = track.block_iter();
+
// Count remaining distance in the vehicle's current block
for(track=track.next(); &track->get_block()==&*block; track=track.next())
result += track->get_type().get_path_length(track->get_active_path());