+float Train::get_real_speed(unsigned i) const
+{
+ if(real_speed[i].weight)
+ return real_speed[i].speed;
+
+ unsigned low;
+ unsigned high;
+ for(low=i; low>0; --low)
+ if(real_speed[low].weight)
+ break;
+ for(high=i; high<14; ++high)
+ if(real_speed[high].weight)
+ break;
+
+ if(real_speed[high].weight)
+ {
+ if(real_speed[low].weight)
+ {
+ float f = float(i-low)/(high-low);
+ return real_speed[low].speed*(1-f)+real_speed[high].speed*f;
+ }
+ else
+ return real_speed[high].speed*float(i)/high;
+ }
+ else if(real_speed[low].weight)
+ return real_speed[low].speed*float(i)/low;
+ else
+ return 0;
+}
+
+unsigned Train::find_speed(float real) const
+{
+ if(real<=real_speed[0].speed)
+ return 0;
+
+ unsigned low = 0;
+ unsigned high = 0;
+ unsigned last = 0;
+ for(unsigned i=0; (!high && i<=14); ++i)
+ if(real_speed[i].weight)
+ {
+ last = i;
+ if(real_speed[i].speed<real)
+ low = i;
+ else
+ high = i;
+ }
+ if(!high)
+ {
+ if(!low)
+ {
+ if(real)
+ return 3;
+ else
+ return 0;
+ }
+ return min(min(static_cast<unsigned>(low*real/real_speed[low].speed), 14U), last+3);
+ }
+
+ float f = (real-real_speed[low].speed)/(real_speed[high].speed-real_speed[low].speed);
+ return static_cast<unsigned>(low*(1-f)+high*f+0.5);
+}
+
+float Train::get_travel_speed() const
+{
+ float speed = get_real_speed(current_speed);
+ float scale = layout.get_catalogue().get_scale();
+ return static_cast<int>(round(speed/scale*3.6/5))*5;
+}
+
+void Train::set_status(const string &s)
+{
+ status = s;
+ signal_status_changed.emit(s);
+}
+
+void Train::release_blocks(list<BlockRef> &blocks)
+{
+ release_blocks(blocks, blocks.begin(), blocks.end());
+}
+
+void Train::release_blocks(list<BlockRef> &blocks, list<BlockRef>::iterator begin, list<BlockRef>::iterator end)
+{
+ while(begin!=end)
+ {
+ Block *block = begin->block;
+ blocks.erase(begin++);
+ block->reserve(0);
+ }
+}
+
+void Train::reverse_blocks(list<BlockRef> &blocks) const
+{
+ blocks.reverse();
+ for(list<BlockRef>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ i->entry = i->block->traverse(i->entry);
+}
+
+
+Train::BlockRef::BlockRef(Block *b, unsigned e):
+ block(b),
+ entry(e)
+{ }
+
+Train::BlockRef Train::BlockRef::next() const
+{
+ Block *blk = block->get_endpoints()[block->traverse(entry)].link;
+ if(!blk)
+ throw InvalidState("At end of line");
+
+ int ep = blk->get_endpoint_by_link(*block);
+ if(ep<0)
+ throw LogicError("Block links are inconsistent");
+
+ return BlockRef(blk, ep);
+}
+
+
+Train::RealSpeed::RealSpeed():
+ speed(0),
+ weight(0)
+{ }
+
+void Train::RealSpeed::add(float s, float w)
+{
+ speed = (speed*weight+s*w)/(weight+w);
+ weight = min(weight+w, 300.0f);
+}
+
+
+Train::Loader::Loader(Train &t):
+ DataFile::BasicLoader<Train>(t),
+ prev_block(0),
+ blocks_valid(true)
+{
+ 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);
+ add("vehicle", &Loader::vehicle);
+}
+
+void Train::Loader::finish()
+{
+ if(!obj.cur_blocks.empty())
+ {
+ const BlockRef &blkref = obj.cur_blocks.front();
+ const Block::Endpoint &bep = blkref.block->get_endpoints()[blkref.entry];
+ obj.vehicles.back()->place(*bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
+
+ obj.set_status("Stopped");
+ }
+}
+
+void Train::Loader::block(unsigned id)
+{
+ if(!blocks_valid)
+ return;
+
+ Block *blk;
+ try
+ {
+ blk = &obj.layout.get_block(id);
+ }
+ catch(const KeyError &)
+ {
+ blocks_valid = false;
+ return;
+ }
+
+ int entry = -1;
+ if(prev_block)
+ entry = blk->get_endpoint_by_link(*prev_block);
+ if(entry<0)
+ entry = 0;
+
+ blk->reserve(&obj);
+ obj.cur_blocks.push_back(BlockRef(blk, entry));
+
+ if(blk->get_sensor_id())
+ obj.layout.get_driver().set_sensor(blk->get_sensor_id(), true);
+
+ prev_block = blk;
+}
+
+void Train::Loader::block_hint(unsigned id)
+{
+ try
+ {
+ prev_block = &obj.layout.get_block(id);
+ }
+ catch(const KeyError &)
+ {
+ blocks_valid = false;
+ }
+}
+
+void Train::Loader::name(const string &n)
+{
+ obj.set_name(n);
+}
+
+void Train::Loader::real_speed(unsigned i, float speed, float weight)
+{
+ obj.real_speed[i].speed = speed;
+ obj.real_speed[i].weight = weight;
+}
+
+void Train::Loader::route(const string &n)
+{
+ obj.set_route(&obj.layout.get_route(n));
+}
+
+void Train::Loader::timetable()
+{
+ if(obj.timetable)
+ throw InvalidState("A timetable has already been loaded");
+
+ obj.timetable = new Timetable(obj);
+ load_sub(*obj.timetable);
+}
+
+void Train::Loader::vehicle(unsigned n)
+{
+ const VehicleType &vtype = obj.layout.get_catalogue().get_vehicle(n);
+ Vehicle *veh = new Vehicle(obj.layout, vtype);
+ obj.vehicles.back()->attach_back(*veh);
+ obj.vehicles.push_back(veh);
+}
+