+ for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
+ if(i->block==&block)
+ return i->entry;
+ for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
+ if(i->block==&block)
+ return i->entry;
+ return -1;
+}
+
+void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
+{
+ if(stop_timeout && t>=stop_timeout)
+ {
+ release_blocks(rsv_blocks);
+ end_of_route = false;
+ stop_timeout = Time::TimeStamp();
+ }
+
+ if(cur_track)
+ {
+ unsigned path = cur_track->get_active_path();
+
+ offset += get_real_speed(current_speed)*(dt/Time::sec);
+ float path_len = cur_track->get_type().get_path_length(path);
+ if(offset>path_len)
+ {
+ unsigned out = cur_track->traverse(cur_track_ep, path);
+ Track *next = cur_track->get_link(out);
+
+ bool ok = false;
+ for(list<BlockRef>::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
+ ok = i->block->get_tracks().count(next);
+
+ if(ok)
+ {
+ if(next)
+ cur_track_ep = next->get_endpoint_by_link(*cur_track);
+ cur_track = next;
+ offset = 0;
+ }
+ else
+ offset = path_len-0.001;
+ }
+
+ if(cur_track)
+ pos = cur_track->get_point(cur_track_ep, path, offset).pos;
+ }
+}
+
+void Train::save(list<DataFile::Statement> &st) const
+{
+ st.push_back((DataFile::Statement("name"), name));
+ for(unsigned i=0; i<=14; ++i)
+ 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())
+ {
+ list<BlockRef> blocks = cur_blocks;
+ if(reverse)
+ reverse_blocks(blocks);
+
+ Block *prev = blocks.front().block->get_endpoints()[blocks.front().entry].link;
+ st.push_back((DataFile::Statement("block_hint"), prev->get_id()));
+
+ for(list<BlockRef>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ st.push_back((DataFile::Statement("block"), i->block->get_id()));
+ }
+
+ if(route)
+ st.push_back((DataFile::Statement("route"), route->get_name()));
+}
+
+void Train::loco_speed_event(unsigned addr, unsigned speed, bool rev)
+{
+ if(addr==address)
+ {
+ current_speed = speed;
+ reverse = rev;
+
+ signal_speed_changed.emit(current_speed);
+ signal_reverse_changed.emit(reverse);
+ }
+}
+
+void Train::loco_func_event(unsigned addr, unsigned func, bool state)
+{
+ if(addr==address || (addr==address+1 && loco_type.get_max_function()>4))
+ {
+ if(addr==address+1)
+ func += 4;
+ if(state)
+ functions |= 1<<func;
+ else
+ functions &= ~(1<<func);