]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/train.cpp
Foundations of using physics simulation for trains
[r2c2.git] / source / libmarklin / train.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include <msp/strings/formatter.h>
10 #include <msp/time/units.h>
11 #include <msp/time/utils.h>
12 #include "aicontrol.h"
13 #include "catalogue.h"
14 #include "driver.h"
15 #include "layout.h"
16 #include "locotype.h"
17 #include "route.h"
18 #include "simplephysics.h"
19 #include "tracktype.h"
20 #include "train.h"
21 #include "vehicle.h"
22
23 using namespace std;
24 using namespace Msp;
25
26 namespace Marklin {
27
28 Train::Train(Layout &l, const LocoType &t, unsigned a):
29         layout(l),
30         loco_type(t),
31         address(a),
32         pending_block(0),
33         control(new AIControl(*this, new SimplePhysics)),
34         active(false),
35         current_speed(0),
36         speed_changing(false),
37         reverse(false),
38         functions(0),
39         route(0),
40         next_route(0),
41         end_of_route(false),
42         status("Unplaced"),
43         travel_dist(0),
44         pure_speed(false),
45         real_speed(15)
46 {
47         vehicles.push_back(new Vehicle(layout, loco_type));
48
49         layout.add_train(*this);
50
51         layout.get_driver().add_loco(address);
52         layout.get_driver().signal_loco_speed.connect(sigc::mem_fun(this, &Train::loco_speed_event));
53         layout.get_driver().signal_loco_function.connect(sigc::mem_fun(this, &Train::loco_func_event));
54
55         layout.signal_block_reserved.connect(sigc::mem_fun(this, &Train::block_reserved));
56         layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Train::sensor_event));
57         layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Train::turnout_event));
58 }
59
60 Train::~Train()
61 {
62         for(vector<Vehicle *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
63                 delete *i;
64         layout.remove_train(*this);
65 }
66
67 void Train::set_name(const string &n)
68 {
69         name = n;
70
71         signal_name_changed.emit(name);
72 }
73
74 Vehicle &Train::get_vehicle(unsigned i)
75 {
76         if(i>=vehicles.size())
77                 throw InvalidParameterValue("Vehicle index out of range");
78         return *vehicles[i];
79 }
80
81 const Vehicle &Train::get_vehicle(unsigned i) const
82 {
83         if(i>=vehicles.size())
84                 throw InvalidParameterValue("Vehicle index out of range");
85         return *vehicles[i];
86 }
87
88 void Train::set_control(const string &n, float v)
89 {
90         control->set_control(n, v);
91         signal_control_changed.emit(n, control->get_control(n).value);
92 }
93
94 void Train::set_active(bool a)
95 {
96         if(a==active)
97                 return;
98         if(!a && control->get_speed())
99                 throw InvalidState("Can't deactivate while moving");
100
101         active = a;
102         if(active)
103         {
104                 stop_timeout = Time::TimeStamp();
105                 reserve_more();
106         }
107         else
108         {
109                 stop_timeout = Time::now()+2*Time::sec;
110                 set_status("Stopped");
111         }
112 }
113
114 void Train::set_function(unsigned func, bool state)
115 {
116         if(!loco_type.get_functions().count(func))
117                 throw InvalidParameterValue("Invalid function");
118         if(func<5)
119                 layout.get_driver().set_loco_function(address, func, state);
120         else
121                 layout.get_driver().set_loco_function(address+1, func-4, state);
122 }
123
124 bool Train::get_function(unsigned func) const
125 {
126         return (functions>>func)&1;
127 }
128
129 void Train::set_route(const Route *r)
130 {
131         if(!rsv_blocks.empty())
132         {
133                 for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
134                         if(i->block->get_sensor_id())
135                         {
136                                 release_blocks(rsv_blocks, ++i, rsv_blocks.end());
137                                 break;
138                         }
139         }
140
141         route = r;
142         next_route = 0;
143         end_of_route = false;
144
145         if(route)
146         {
147                 BlockRef &last = (rsv_blocks.empty() ? cur_blocks.back() : rsv_blocks.back());
148                 BlockRef next = last.next();
149                 const Block::Endpoint &ep = next.block->get_endpoints()[next.entry];
150                 if(!route->get_tracks().count(ep.track))
151                 {
152                         next_route = route;
153                         route = Route::find(*ep.track, ep.track_ep, *next_route);
154                 }
155         }
156
157         if(active)
158                 reserve_more();
159
160         signal_route_changed.emit(route);
161 }
162
163 void Train::go_to(const Track &to)
164 {
165         for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
166                 if(i->block->get_tracks().count(const_cast<Track *>(&to)))
167                 {
168                         signal_arrived.emit();
169                         set_route(0);
170                         return;
171                 }
172
173         BlockRef *last = 0;
174         if(rsv_blocks.empty())
175                 last = &cur_blocks.back();
176         else
177         {
178                 for(list<BlockRef>::iterator i=rsv_blocks.begin(); (i!=rsv_blocks.end() && !last); ++i)
179                         if(i->block->get_sensor_id())
180                                 last = &*i;
181         }
182
183         BlockRef next = last->next();
184         const Block::Endpoint &ep = next.block->get_endpoints()[next.entry];
185
186         set_route(Route::find(*ep.track, ep.track_ep, to));
187 }
188
189 void Train::place(Block &block, unsigned entry)
190 {
191         if(control->get_speed())
192                 throw InvalidState("Must be stopped before placing");
193
194         release_blocks(rsv_blocks);
195         release_blocks(cur_blocks);
196
197         set_active(false);
198
199         if(!block.reserve(this))
200         {
201                 set_status("Unplaced");
202                 return;
203         }
204
205         cur_blocks.push_back(BlockRef(&block, entry));
206         if(reverse)
207         {
208                 unsigned exit = block.traverse(entry);
209                 const Block::Endpoint &bep = block.get_endpoints()[exit];
210                 Track *track = bep.track->get_link(bep.track_ep);
211                 unsigned ep = track->get_endpoint_by_link(*bep.track);
212                 vehicles.front()->place(track, ep, 0, Vehicle::FRONT_BUFFER);
213         }
214         else
215         {
216                 const Block::Endpoint &bep = block.get_endpoints()[entry];
217                 vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
218         }
219 }
220
221 bool Train::free_block(Block &block)
222 {
223         unsigned nsens = 0;
224         for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
225         {
226                 if(i->block==&block)
227                 {
228                         if(nsens<1)
229                                 return false;
230                         release_blocks(rsv_blocks, i, rsv_blocks.end());
231                         return true;
232                 }
233                 else if(i->block->get_sensor_id())
234                         ++nsens;
235         }
236
237         return false;
238 }
239
240 int Train::get_entry_to_block(Block &block) const
241 {
242         for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
243                 if(i->block==&block)
244                         return i->entry;
245         for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
246                 if(i->block==&block)
247                         return i->entry;
248         return -1;
249 }
250
251 float Train::get_reserved_distance() const
252 {
253         Vehicle &veh = *(reverse ? vehicles.back() : vehicles.front());
254         const VehicleType &vtype = veh.get_type();
255
256         Track *track = veh.get_track();
257         unsigned entry = veh.get_entry();
258
259         float result = -vtype.get_length()/2;
260         if(reverse)
261         {
262                 entry = track->traverse(entry);
263                 result += veh.get_offset();
264         }
265         else
266                 result -= veh.get_offset();
267
268         bool first = true;
269         list<BlockRef>::const_iterator block = cur_blocks.begin();
270         while(1)
271         {
272                 if(!first || !reverse)
273                         result += track->get_type().get_path_length(track->get_active_path());
274                 first = false;
275
276                 unsigned exit = track->traverse(entry);
277                 Track *next = track->get_link(exit);
278
279                 while(!block->block->get_tracks().count(next))
280                 {
281                         ++block;
282                         if(block==cur_blocks.end())
283                                 block = rsv_blocks.begin();
284                         if(block==rsv_blocks.end())
285                                 return result;
286                 }
287
288                 entry = next->get_endpoint_by_link(*track);
289                 track = next;
290         }
291 }
292
293 void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
294 {
295         if(!active && stop_timeout && t>=stop_timeout)
296         {
297                 release_blocks(rsv_blocks);
298                 end_of_route = false;
299                 stop_timeout = Time::TimeStamp();
300         }
301
302         control->tick(dt);
303         float speed = control->get_speed();
304         unsigned speed_notch = find_speed(abs(speed));
305
306         if(speed && (speed<0)!=reverse)
307         {
308                 layout.get_driver().set_loco_reverse(address, speed<0);
309                 reverse = speed<0;
310
311                 release_blocks(rsv_blocks);
312                 reverse_blocks(cur_blocks);
313
314                 reserve_more();
315         }
316         if(speed_notch!=current_speed && !speed_changing)
317         {
318                 speed_changing = true;
319                 layout.get_driver().set_loco_speed(address, speed_notch);
320
321                 pure_speed = false;
322
323                 if(speed_notch)
324                         set_status(format("Traveling %d kmh", get_travel_speed()));
325                 else
326                         set_status("Waiting");
327         }
328
329         if(speed)
330         {
331                 if(!active)
332                         set_active(true);
333
334                 Track *track = vehicles[0]->get_track();
335
336                 bool ok = false;
337                 for(list<BlockRef>::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
338                         ok = i->block->get_tracks().count(track);
339
340                 if(ok)
341                 {
342                         float d = get_real_speed(current_speed)*(dt/Time::sec);
343                         vehicles[0]->advance(reverse ? -d : d);
344                 }
345         }
346 }
347
348 void Train::save(list<DataFile::Statement> &st) const
349 {
350         st.push_back((DataFile::Statement("name"), name));
351         for(unsigned i=0; i<=14; ++i)
352                 if(real_speed[i].weight)
353                         st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
354
355         if(!cur_blocks.empty())
356         {
357                 list<BlockRef> blocks = cur_blocks;
358                 if(reverse)
359                         reverse_blocks(blocks);
360
361                 Block *prev = blocks.front().block->get_endpoints()[blocks.front().entry].link;
362                 st.push_back((DataFile::Statement("block_hint"), prev->get_id()));
363
364                 for(list<BlockRef>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
365                         st.push_back((DataFile::Statement("block"), i->block->get_id()));
366         }
367
368         if(route)
369                 st.push_back((DataFile::Statement("route"), route->get_name()));
370 }
371
372 void Train::loco_speed_event(unsigned addr, unsigned speed, bool)
373 {
374         if(addr==address)
375         {
376                 current_speed = speed;
377                 speed_changing = false;
378         }
379 }
380
381 void Train::loco_func_event(unsigned addr, unsigned func, bool state)
382 {
383         if(addr==address || (addr==address+1 && loco_type.get_max_function()>4))
384         {
385                 if(addr==address+1)
386                         func += 4;
387                 if(state)
388                         functions |= 1<<func;
389                 else
390                         functions &= ~(1<<func);
391
392                 signal_function_changed.emit(func, state);
393         }
394 }
395
396 void Train::sensor_event(unsigned addr, bool state)
397 {
398         if(state)
399         {
400                 // Find the first sensor block from our reserved blocks that isn't this sensor
401                 list<BlockRef>::iterator i;
402                 for(i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
403                         if(i->block->get_sensor_id() && i->block->get_sensor_id()!=addr)
404                                 break;
405
406                 if(i!=rsv_blocks.begin())
407                 {
408                         // Compute speed and update related state
409                         float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
410
411                         if(pure_speed)
412                         {
413                                 RealSpeed &rs = real_speed[current_speed];
414                                 rs.add(travel_dist/travel_time_secs, travel_time_secs);
415                                 set_status(format("Traveling %d kmh", get_travel_speed()));
416                         }
417
418                         travel_dist = 0;
419                         float block_len;
420                         for(list<BlockRef>::iterator j=rsv_blocks.begin(); j!=i; ++j)
421                         {
422                                 j->block->traverse(j->entry, &block_len);
423                                 travel_dist += block_len;
424
425                                 if(j->block->get_sensor_id()==addr)
426                                 {
427                                         const Block::Endpoint &bep = j->block->get_endpoints()[j->entry];
428                                         if(reverse)
429                                         {
430                                                 Track *track = bep.track->get_link(bep.track_ep);
431                                                 unsigned ep = track->get_endpoint_by_link(*bep.track);
432                                                 vehicles.back()->place(track, ep, 0, Vehicle::BACK_AXLE);
433                                         }
434                                         else
435                                                 vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::FRONT_AXLE);
436                                 }
437                         }
438                         last_entry_time = Time::now();
439                         pure_speed = true;
440
441                         // Check if we've reached the next route
442                         if(next_route)
443                         {
444                                 const set<const Track *> &rtracks = next_route->get_tracks();
445                                 for(list<BlockRef>::iterator j=rsv_blocks.begin(); j!=i; ++j)
446                                         if(rtracks.count(j->block->get_endpoints()[j->entry].track))
447                                         {
448                                                 route = next_route;
449                                                 next_route = 0;
450                                                 // XXX Exceptions?
451                                                 signal_route_changed.emit(route);
452                                                 break;
453                                         }
454                         }
455
456                         // Move blocks up to the next sensor to our current blocks
457                         cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
458
459                         // Try to get more blocks if we're moving
460                         if(active)
461                         {
462                                 unsigned nsens = reserve_more();
463                                 if(!nsens && end_of_route)
464                                 {
465                                         signal_arrived.emit();
466                                         set_route(0);
467                                 }
468                         }
469                 }
470         }
471         else
472         {
473                 // Find the first sensor in our current blocks that's still active
474                 list<BlockRef>::iterator end = cur_blocks.begin();
475                 for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
476                         if(i->block->get_sensor_id())
477                         {
478                                 if(layout.get_driver().get_sensor(i->block->get_sensor_id()))
479                                         break;
480                                 else
481                                         end = i;
482                         }
483                 
484                 if(end!=cur_blocks.begin())
485                 {
486                         // Free blocks up to the last inactive sensor
487                         ++end;
488                         release_blocks(cur_blocks, cur_blocks.begin(), end);
489                 }
490         }
491 }
492
493 void Train::turnout_event(unsigned addr, bool)
494 {
495         if(pending_block)
496         {
497                 unsigned pending_addr = pending_block->get_turnout_id();
498                 bool double_addr = (*pending_block->get_tracks().begin())->get_type().is_double_address();
499                 if(addr==pending_addr || (double_addr && addr==pending_addr+1))
500                         reserve_more();
501         }
502 }
503
504 void Train::block_reserved(const Block &block, const Train *train)
505 {
506         if(&block==pending_block && !train)
507                 reserve_more();
508 }
509
510 unsigned Train::reserve_more()
511 {
512         BlockRef *last = 0;
513         if(!rsv_blocks.empty())
514                 last = &rsv_blocks.back();
515         else if(!cur_blocks.empty())
516                 last = &cur_blocks.back();
517         if(!last)
518                 return 0;
519
520         pending_block = 0;
521
522         // See how many sensor blocks we already have
523         unsigned nsens = 0;
524         for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
525                 if(i->block->get_sensor_id())
526                         ++nsens;
527         
528         const Route *cur_route = 0;
529         if(route)
530         {
531                 unsigned exit = last->block->traverse(last->entry);
532                 Track *track = last->block->get_endpoints()[exit].track;
533                 if(route->get_tracks().count(track))
534                         cur_route = route;
535                 else if(next_route && next_route->get_tracks().count(track))
536                         cur_route = next_route;
537         }
538
539         bool got_more = false;
540         BlockRef *good = last;
541         unsigned good_sens = nsens;
542         while(good_sens<3)
543         {
544                 // Traverse to the next block
545                 unsigned exit = last->block->traverse(last->entry);
546                 Block *link = last->block->get_link(exit);
547                 if(!link)
548                         break;
549
550                 int entry = link->get_endpoint_by_link(*last->block);
551                 if(entry<0)
552                         throw LogicError("Block links are inconsistent!");
553
554                 const Block::Endpoint &entry_ep = link->get_endpoints()[entry];
555
556                 if(cur_route)
557                 {
558                         if(cur_route!=next_route && next_route && next_route->get_tracks().count(entry_ep.track))
559                                 cur_route = next_route;
560                         else if(!cur_route->get_tracks().count(entry_ep.track))
561                         {
562                                 // Keep the blocks if we arrived at the end of the route
563                                 good = last;
564                                 good_sens = nsens;
565                                 end_of_route = true;
566                                 break;
567                         }
568                 }
569                 else if(route && route->get_tracks().count(entry_ep.track))
570                         cur_route = route;
571
572                 if(!link->reserve(this))
573                 {
574                         // If we found another train and it's not headed straight for us, we can keep the blocks we got
575                         int other_entry = link->get_train()->get_entry_to_block(*link);
576                         if(other_entry<0)
577                                 throw LogicError("Block reservation inconsistency");
578                         if(static_cast<unsigned>(entry)!=link->traverse(other_entry))
579                         {
580                                 good = last;
581                                 good_sens = nsens;
582                         }
583                         pending_block = link;
584                         break;
585                 }
586
587                 if(link->get_turnout_id())
588                 {
589                         const Endpoint &track_ep = entry_ep.track->get_type().get_endpoints()[entry_ep.track_ep];
590
591                         // Keep the blocks reserved so far, as either us or the other train can diverge
592                         good = last;
593                         good_sens = nsens;
594
595                         // Figure out what path we'd like to take on the turnout
596                         int path = -1;
597                         if(cur_route)
598                                 path = cur_route->get_turnout(link->get_turnout_id());
599                         if(path<0)
600                                 path = entry_ep.track->get_active_path();
601                         if(!((track_ep.paths>>path)&1))
602                         {
603                                 for(unsigned i=0; track_ep.paths>>i; ++i)
604                                         if((track_ep.paths>>i)&1)
605                                                 path = i;
606                         }
607
608                         if(path!=static_cast<int>(entry_ep.track->get_active_path()))
609                         {
610                                 // The turnout is set to wrong path - switch and wait for it
611                                 link->reserve(0);
612                                 pending_block = link;
613                                 entry_ep.track->set_active_path(path);
614                                 break;
615                         }
616                 }
617
618                 rsv_blocks.push_back(BlockRef(link, entry));
619                 last = &rsv_blocks.back();
620                 if(last->block->get_sensor_id())
621                 {
622                         ++nsens;
623                         got_more = true;
624                 }
625         }
626
627         // Unreserve blocks that were not good
628         while(!rsv_blocks.empty() && last!=good)
629         {
630                 last->block->reserve(0);
631                 rsv_blocks.erase(--rsv_blocks.end());
632                 if(!rsv_blocks.empty())
633                         last = &rsv_blocks.back();
634         }
635
636         return good_sens;
637 }
638
639 float Train::get_real_speed(unsigned i) const
640 {
641         if(real_speed[i].weight)
642                 return real_speed[i].speed;
643
644         unsigned low;
645         unsigned high;
646         for(low=i; low>0; --low)
647                 if(real_speed[low].weight)
648                         break;
649         for(high=i; high<14; ++high)
650                 if(real_speed[high].weight)
651                         break;
652
653         if(real_speed[high].weight)
654         {
655                 if(real_speed[low].weight)
656                 {
657                         float f = float(i-low)/(high-low);
658                         return real_speed[low].speed*(1-f)+real_speed[high].speed*f;
659                 }
660                 else
661                         return real_speed[high].speed*float(i)/high;
662         }
663         else if(real_speed[low].weight)
664                 return real_speed[low].speed*float(i)/low;
665         else
666                 return 0;
667 }
668
669 unsigned Train::find_speed(float real) const
670 {
671         if(real<=real_speed[0].speed)
672                 return 0;
673
674         unsigned low = 0;
675         unsigned high = 0;
676         for(unsigned i=0; (!high && i<=14); ++i)
677                 if(real_speed[i].weight)
678                 {
679                         if(real_speed[i].speed<real)
680                                 low = i;
681                         else
682                                 high = i;
683                 }
684         if(!high)
685         {
686                 if(!low)
687                         return 0;
688                 return min(static_cast<unsigned>(low*real/real_speed[low].speed), 14U);
689         }
690
691         float f = (real-real_speed[low].speed)/(real_speed[high].speed-real_speed[low].speed);
692         return static_cast<unsigned>(low*(1-f)+high*f+0.5);
693 }
694
695 float Train::get_travel_speed() const
696 {
697         float speed = get_real_speed(current_speed);
698         float scale = layout.get_catalogue().get_scale();
699         return static_cast<int>(round(speed/scale*3.6/5))*5;
700 }
701
702 void Train::set_status(const string &s)
703 {
704         status = s;
705         signal_status_changed.emit(s);
706 }
707
708 void Train::release_blocks(list<BlockRef> &blocks)
709 {
710         release_blocks(blocks, blocks.begin(), blocks.end());
711 }
712
713 void Train::release_blocks(list<BlockRef> &blocks, list<BlockRef>::iterator begin, list<BlockRef>::iterator end)
714 {
715         while(begin!=end)
716         {
717                 Block *block = begin->block;
718                 blocks.erase(begin++);
719                 block->reserve(0);
720         }
721 }
722
723 void Train::reverse_blocks(list<BlockRef> &blocks) const
724 {
725         blocks.reverse();
726         for(list<BlockRef>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
727                 i->entry = i->block->traverse(i->entry);
728 }
729
730
731 Train::BlockRef::BlockRef(Block *b, unsigned e):
732         block(b),
733         entry(e)
734 { }
735
736 Train::BlockRef Train::BlockRef::next() const
737 {
738         Block *blk = block->get_endpoints()[block->traverse(entry)].link;
739         if(!blk)
740                 throw InvalidState("At end of line");
741
742         int ep = blk->get_endpoint_by_link(*block);
743         if(ep<0)
744                 throw LogicError("Block links are inconsistent");
745
746         return BlockRef(blk, ep);
747 }
748
749
750 Train::RealSpeed::RealSpeed():
751         speed(0),
752         weight(0)
753 { }
754
755 void Train::RealSpeed::add(float s, float w)
756 {
757         speed = (speed*weight+s*w)/(weight+w);
758         weight = min(weight+w, 300.0f);
759 }
760
761
762 Train::Loader::Loader(Train &t):
763         DataFile::BasicLoader<Train>(t),
764         prev_block(0)
765 {
766         add("block",       &Loader::block);
767         add("block_hint",  &Loader::block_hint);
768         add("name",        &Loader::name);
769         add("real_speed",  &Loader::real_speed);
770         add("route",       &Loader::route);
771 }
772
773 void Train::Loader::block(unsigned id)
774 {
775         Block &blk = obj.layout.get_block(id);
776         int entry = -1;
777         if(prev_block)
778                 entry = blk.get_endpoint_by_link(*prev_block);
779         if(entry<0)
780                 entry = 0;
781
782         blk.reserve(&obj);
783         obj.cur_blocks.push_back(BlockRef(&blk, entry));
784         obj.set_status("Stopped");
785         const Block::Endpoint &bep = blk.get_endpoints()[entry];
786         obj.vehicles.back()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
787
788         if(blk.get_sensor_id())
789                 obj.layout.get_driver().set_sensor(blk.get_sensor_id(), true);
790
791         prev_block = &blk;
792 }
793
794 void Train::Loader::block_hint(unsigned id)
795 {
796         prev_block = &obj.layout.get_block(id);
797 }
798
799 void Train::Loader::name(const string &n)
800 {
801         obj.set_name(n);
802 }
803
804 void Train::Loader::real_speed(unsigned i, float speed, float weight)
805 {
806         obj.real_speed[i].speed = speed;
807         obj.real_speed[i].weight = weight;
808 }
809
810 void Train::Loader::route(const string &n)
811 {
812         obj.set_route(&obj.layout.get_route(n));
813 }
814
815 } // namespace Marklin