layout(l),
loco_type(t),
address(a),
+ priority(0),
pending_block(0),
control(new AIControl(*this, new SimplePhysics)),
timetable(0),
layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Train::sensor_event));
layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Train::turnout_event));
+ layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &Train::halt_event));
+
control->signal_control_changed.connect(signal_control_changed);
}
signal_name_changed.emit(name);
}
+void Train::set_priority(int p)
+{
+ priority = p;
+}
+
Vehicle &Train::get_vehicle(unsigned i)
{
if(i>=vehicles.size())
float speed = control->get_speed();
unsigned speed_notch = find_speed(abs(speed));
- if(speed && (speed<0)!=reverse)
+ if(control->get_reverse()!=reverse)
{
- driver.set_loco_reverse(address, speed<0);
- reverse = speed<0;
+ reverse = control->get_reverse();
+ driver.set_loco_reverse(address, reverse);
release_blocks(rsv_blocks);
reverse_blocks(cur_blocks);
{
overshoot_dist += d;
if(overshoot_dist>40*layout.get_catalogue().get_scale())
+ {
layout.emergency(name+" has not arrived at sensor");
+ accurate_position = false;
+ }
}
}
else if(end_of_route)
set_route(0);
+
+ if(!cur_blocks.empty() && !cur_blocks.front().block->get_sensor_id())
+ {
+ Vehicle &veh = *(reverse ? vehicles.front() : vehicles.back());
+
+ list<BlockRef>::iterator i = cur_blocks.begin();
+ const Block::Endpoint &bep = i->block->get_endpoints()[i->entry];
+
+ Track *track = bep.track;
+ unsigned entry = bep.track_ep;
+
+ bool found = false;
+ float dist = veh.get_offset()-veh.get_type().get_length()/2;
+ while(1)
+ {
+ if(track==veh.get_track())
+ {
+ found = true;
+ break;
+ }
+
+ if(i!=cur_blocks.begin())
+ {
+ float path_len = track->get_type().get_path_length(track->get_active_path());
+ dist += path_len;
+ }
+
+ unsigned exit = track->traverse(entry);
+ Track *next = track->get_link(exit);
+ entry = next->get_endpoint_by_link(*track);
+ track = next;
+
+ if(!i->block->get_tracks().count(track))
+ {
+ ++i;
+ if(i==cur_blocks.end())
+ break;
+ }
+ }
+
+ if(found && i!=cur_blocks.begin() && dist>10*layout.get_catalogue().get_scale())
+ {
+ cur_blocks.front().block->reserve(0);
+ cur_blocks.erase(cur_blocks.begin());
+ }
+ }
}
void Train::save(list<DataFile::Statement> &st) const
{
st.push_back((DataFile::Statement("name"), name));
+ st.push_back((DataFile::Statement("priority"), priority));
+
for(vector<Vehicle *>::const_iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
if(i!=vehicles.begin())
st.push_back((DataFile::Statement("vehicle"), (*i)->get_type().get_article_number()));
if(layout.get_driver().get_sensor(i->block->get_sensor_id()))
break;
else
+ {
end = i;
+ ++end;
+ }
}
if(end!=cur_blocks.begin())
- {
// Free blocks up to the last inactive sensor
- ++end;
release_blocks(cur_blocks, cur_blocks.begin(), end);
- }
}
}
}
}
+void Train::halt_event(bool h)
+{
+ if(h)
+ accurate_position = false;
+}
+
void Train::block_reserved(const Block &block, const Train *train)
{
if(&block==pending_block && !train)
else if(route && route->get_tracks().count(entry_ep.track))
cur_route = route;
- if(!link->reserve(this))
+ bool reserved = link->reserve(this);
+ if(!reserved)
{
- // If we found another train and it's not headed straight for us, we can keep the blocks we got
- int other_entry = link->get_train()->get_entry_to_block(*link);
- if(other_entry<0)
- throw LogicError("Block reservation inconsistency");
- if(static_cast<unsigned>(entry)!=link->traverse(other_entry))
+ // Ask a lesser priority train to free the block for us
+ if(link->get_train()->get_priority()<priority)
+ if(link->get_train()->free_block(*link))
+ reserved = link->reserve(this);
+
+ if(!reserved)
{
- good = last;
- good_sens = nsens;
+ // If we found another train and it's not headed straight for us, we can keep the blocks we got
+ int other_entry = link->get_train()->get_entry_to_block(*link);
+ if(other_entry<0)
+ throw LogicError("Block reservation inconsistency");
+ if(static_cast<unsigned>(entry)!=link->traverse(other_entry))
+ {
+ good = last;
+ good_sens = nsens;
+ }
+ pending_block = link;
+ break;
}
- pending_block = link;
- break;
}
if(link->get_turnout_id())
add("block", &Loader::block);
add("block_hint", &Loader::block_hint);
add("name", &Loader::name);
+ add("priority", &Train::priority);
add("real_speed", &Loader::real_speed);
add("route", &Loader::route);
add("timetable", &Loader::timetable);