]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/train.cpp
Fix pathfinder to not block itself in some situations
[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         else if(end_of_route)
347                 set_route(0);
348 }
349
350 void Train::save(list<DataFile::Statement> &st) const
351 {
352         st.push_back((DataFile::Statement("name"), name));
353         for(unsigned i=0; i<=14; ++i)
354                 if(real_speed[i].weight)
355                         st.push_back((DataFile::Statement("real_speed"), i, real_speed[i].speed, real_speed[i].weight));
356
357         if(!cur_blocks.empty())
358         {
359                 list<BlockRef> blocks = cur_blocks;
360                 if(reverse)
361                         reverse_blocks(blocks);
362
363                 Block *prev = blocks.front().block->get_endpoints()[blocks.front().entry].link;
364                 st.push_back((DataFile::Statement("block_hint"), prev->get_id()));
365
366                 for(list<BlockRef>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
367                         st.push_back((DataFile::Statement("block"), i->block->get_id()));
368         }
369
370         if(route)
371                 st.push_back((DataFile::Statement("route"), route->get_name()));
372 }
373
374 void Train::loco_speed_event(unsigned addr, unsigned speed, bool)
375 {
376         if(addr==address)
377         {
378                 current_speed = speed;
379                 speed_changing = false;
380         }
381 }
382
383 void Train::loco_func_event(unsigned addr, unsigned func, bool state)
384 {
385         if(addr==address || (addr==address+1 && loco_type.get_max_function()>4))
386         {
387                 if(addr==address+1)
388                         func += 4;
389                 if(state)
390                         functions |= 1<<func;
391                 else
392                         functions &= ~(1<<func);
393
394                 signal_function_changed.emit(func, state);
395         }
396 }
397
398 void Train::sensor_event(unsigned addr, bool state)
399 {
400         if(state)
401         {
402                 // Find the first sensor block from our reserved blocks that isn't this sensor
403                 list<BlockRef>::iterator i;
404                 for(i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
405                         if(i->block->get_sensor_id() && i->block->get_sensor_id()!=addr)
406                                 break;
407
408                 if(i!=rsv_blocks.begin())
409                 {
410                         // Compute speed and update related state
411                         float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
412
413                         if(pure_speed)
414                         {
415                                 RealSpeed &rs = real_speed[current_speed];
416                                 rs.add(travel_dist/travel_time_secs, travel_time_secs);
417                                 set_status(format("Traveling %d kmh", get_travel_speed()));
418                         }
419
420                         travel_dist = 0;
421                         float block_len;
422                         for(list<BlockRef>::iterator j=rsv_blocks.begin(); j!=i; ++j)
423                         {
424                                 j->block->traverse(j->entry, &block_len);
425                                 travel_dist += block_len;
426
427                                 if(j->block->get_sensor_id()==addr)
428                                 {
429                                         const Block::Endpoint &bep = j->block->get_endpoints()[j->entry];
430                                         if(reverse)
431                                         {
432                                                 Track *track = bep.track->get_link(bep.track_ep);
433                                                 unsigned ep = track->get_endpoint_by_link(*bep.track);
434                                                 vehicles.back()->place(track, ep, 0, Vehicle::BACK_AXLE);
435                                         }
436                                         else
437                                                 vehicles.front()->place(bep.track, bep.track_ep, 0, Vehicle::FRONT_AXLE);
438                                 }
439                         }
440                         last_entry_time = Time::now();
441                         pure_speed = true;
442
443                         // Check if we've reached the next route
444                         if(next_route)
445                         {
446                                 const set<const Track *> &rtracks = next_route->get_tracks();
447                                 for(list<BlockRef>::iterator j=rsv_blocks.begin(); j!=i; ++j)
448                                         if(rtracks.count(j->block->get_endpoints()[j->entry].track))
449                                         {
450                                                 route = next_route;
451                                                 next_route = 0;
452                                                 // XXX Exceptions?
453                                                 signal_route_changed.emit(route);
454                                                 break;
455                                         }
456                         }
457
458                         // Move blocks up to the next sensor to our current blocks
459                         cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
460
461                         // Try to get more blocks if we're moving
462                         if(active)
463                         {
464                                 unsigned nsens = reserve_more();
465                                 if(!nsens && end_of_route)
466                                         signal_arrived.emit();
467                         }
468                 }
469         }
470         else
471         {
472                 // Find the first sensor in our current blocks that's still active
473                 list<BlockRef>::iterator end = cur_blocks.begin();
474                 for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
475                         if(i->block->get_sensor_id())
476                         {
477                                 if(layout.get_driver().get_sensor(i->block->get_sensor_id()))
478                                         break;
479                                 else
480                                         end = i;
481                         }
482                 
483                 if(end!=cur_blocks.begin())
484                 {
485                         // Free blocks up to the last inactive sensor
486                         ++end;
487                         release_blocks(cur_blocks, cur_blocks.begin(), end);
488                 }
489         }
490 }
491
492 void Train::turnout_event(unsigned addr, bool)
493 {
494         if(pending_block)
495         {
496                 unsigned pending_addr = pending_block->get_turnout_id();
497                 bool double_addr = (*pending_block->get_tracks().begin())->get_type().is_double_address();
498                 if(addr==pending_addr || (double_addr && addr==pending_addr+1))
499                         reserve_more();
500         }
501 }
502
503 void Train::block_reserved(const Block &block, const Train *train)
504 {
505         if(&block==pending_block && !train)
506                 reserve_more();
507 }
508
509 unsigned Train::reserve_more()
510 {
511         BlockRef *last = 0;
512         if(!rsv_blocks.empty())
513                 last = &rsv_blocks.back();
514         else if(!cur_blocks.empty())
515                 last = &cur_blocks.back();
516         if(!last)
517                 return 0;
518
519         pending_block = 0;
520
521         // See how many sensor blocks we already have
522         unsigned nsens = 0;
523         for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
524                 if(i->block->get_sensor_id())
525                         ++nsens;
526         
527         if(end_of_route)
528                 return nsens;
529
530         const Route *cur_route = 0;
531         if(route)
532         {
533                 const set<Track *> &tracks = last->block->get_tracks();
534                 for(set<Track *>::const_iterator i=tracks.begin(); (cur_route!=route && i!=tracks.end()); ++i)
535                 {
536                         if(route->get_tracks().count(*i))
537                                 cur_route = route;
538                         else if(next_route && next_route->get_tracks().count(*i))
539                                 cur_route = next_route;
540                 }
541         }
542
543         bool got_more = false;
544         BlockRef *good = last;
545         unsigned good_sens = nsens;
546         while(good_sens<3)
547         {
548                 // Traverse to the next block
549                 unsigned exit = last->block->traverse(last->entry);
550                 Block *link = last->block->get_link(exit);
551                 if(!link)
552                         break;
553
554                 int entry = link->get_endpoint_by_link(*last->block);
555                 if(entry<0)
556                         throw LogicError("Block links are inconsistent!");
557
558                 const Block::Endpoint &entry_ep = link->get_endpoints()[entry];
559
560                 if(cur_route)
561                 {
562                         if(cur_route!=next_route && next_route && next_route->get_tracks().count(entry_ep.track))
563                                 cur_route = next_route;
564                         else if(!cur_route->get_tracks().count(entry_ep.track))
565                         {
566                                 // Keep the blocks if we arrived at the end of the route
567                                 good = last;
568                                 good_sens = nsens;
569                                 end_of_route = true;
570                                 break;
571                         }
572                 }
573                 else if(route && route->get_tracks().count(entry_ep.track))
574                         cur_route = route;
575
576                 if(!link->reserve(this))
577                 {
578                         // If we found another train and it's not headed straight for us, we can keep the blocks we got
579                         int other_entry = link->get_train()->get_entry_to_block(*link);
580                         if(other_entry<0)
581                                 throw LogicError("Block reservation inconsistency");
582                         if(static_cast<unsigned>(entry)!=link->traverse(other_entry))
583                         {
584                                 good = last;
585                                 good_sens = nsens;
586                         }
587                         pending_block = link;
588                         break;
589                 }
590
591                 if(link->get_turnout_id())
592                 {
593                         const Endpoint &track_ep = entry_ep.track->get_type().get_endpoints()[entry_ep.track_ep];
594
595                         // Keep the blocks reserved so far, as either us or the other train can diverge
596                         good = last;
597                         good_sens = nsens;
598
599                         // Figure out what path we'd like to take on the turnout
600                         int path = -1;
601                         if(cur_route)
602                                 path = cur_route->get_turnout(link->get_turnout_id());
603                         if(path<0)
604                                 path = entry_ep.track->get_active_path();
605                         if(!((track_ep.paths>>path)&1))
606                         {
607                                 for(unsigned i=0; track_ep.paths>>i; ++i)
608                                         if((track_ep.paths>>i)&1)
609                                                 path = i;
610                         }
611
612                         if(path!=static_cast<int>(entry_ep.track->get_active_path()))
613                         {
614                                 // The turnout is set to wrong path - switch and wait for it
615                                 link->reserve(0);
616                                 pending_block = link;
617                                 entry_ep.track->set_active_path(path);
618                                 break;
619                         }
620                 }
621
622                 rsv_blocks.push_back(BlockRef(link, entry));
623                 last = &rsv_blocks.back();
624                 if(last->block->get_sensor_id())
625                 {
626                         ++nsens;
627                         got_more = true;
628                 }
629         }
630
631         // Unreserve blocks that were not good
632         while(!rsv_blocks.empty() && last!=good)
633         {
634                 last->block->reserve(0);
635                 rsv_blocks.erase(--rsv_blocks.end());
636                 if(!rsv_blocks.empty())
637                         last = &rsv_blocks.back();
638         }
639
640         return good_sens;
641 }
642
643 float Train::get_real_speed(unsigned i) const
644 {
645         if(real_speed[i].weight)
646                 return real_speed[i].speed;
647
648         unsigned low;
649         unsigned high;
650         for(low=i; low>0; --low)
651                 if(real_speed[low].weight)
652                         break;
653         for(high=i; high<14; ++high)
654                 if(real_speed[high].weight)
655                         break;
656
657         if(real_speed[high].weight)
658         {
659                 if(real_speed[low].weight)
660                 {
661                         float f = float(i-low)/(high-low);
662                         return real_speed[low].speed*(1-f)+real_speed[high].speed*f;
663                 }
664                 else
665                         return real_speed[high].speed*float(i)/high;
666         }
667         else if(real_speed[low].weight)
668                 return real_speed[low].speed*float(i)/low;
669         else
670                 return 0;
671 }
672
673 unsigned Train::find_speed(float real) const
674 {
675         if(real<=real_speed[0].speed)
676                 return 0;
677
678         unsigned low = 0;
679         unsigned high = 0;
680         for(unsigned i=0; (!high && i<=14); ++i)
681                 if(real_speed[i].weight)
682                 {
683                         if(real_speed[i].speed<real)
684                                 low = i;
685                         else
686                                 high = i;
687                 }
688         if(!high)
689         {
690                 if(!low)
691                         return 0;
692                 return min(static_cast<unsigned>(low*real/real_speed[low].speed), 14U);
693         }
694
695         float f = (real-real_speed[low].speed)/(real_speed[high].speed-real_speed[low].speed);
696         return static_cast<unsigned>(low*(1-f)+high*f+0.5);
697 }
698
699 float Train::get_travel_speed() const
700 {
701         float speed = get_real_speed(current_speed);
702         float scale = layout.get_catalogue().get_scale();
703         return static_cast<int>(round(speed/scale*3.6/5))*5;
704 }
705
706 void Train::set_status(const string &s)
707 {
708         status = s;
709         signal_status_changed.emit(s);
710 }
711
712 void Train::release_blocks(list<BlockRef> &blocks)
713 {
714         release_blocks(blocks, blocks.begin(), blocks.end());
715 }
716
717 void Train::release_blocks(list<BlockRef> &blocks, list<BlockRef>::iterator begin, list<BlockRef>::iterator end)
718 {
719         while(begin!=end)
720         {
721                 Block *block = begin->block;
722                 blocks.erase(begin++);
723                 block->reserve(0);
724         }
725 }
726
727 void Train::reverse_blocks(list<BlockRef> &blocks) const
728 {
729         blocks.reverse();
730         for(list<BlockRef>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
731                 i->entry = i->block->traverse(i->entry);
732 }
733
734
735 Train::BlockRef::BlockRef(Block *b, unsigned e):
736         block(b),
737         entry(e)
738 { }
739
740 Train::BlockRef Train::BlockRef::next() const
741 {
742         Block *blk = block->get_endpoints()[block->traverse(entry)].link;
743         if(!blk)
744                 throw InvalidState("At end of line");
745
746         int ep = blk->get_endpoint_by_link(*block);
747         if(ep<0)
748                 throw LogicError("Block links are inconsistent");
749
750         return BlockRef(blk, ep);
751 }
752
753
754 Train::RealSpeed::RealSpeed():
755         speed(0),
756         weight(0)
757 { }
758
759 void Train::RealSpeed::add(float s, float w)
760 {
761         speed = (speed*weight+s*w)/(weight+w);
762         weight = min(weight+w, 300.0f);
763 }
764
765
766 Train::Loader::Loader(Train &t):
767         DataFile::BasicLoader<Train>(t),
768         prev_block(0)
769 {
770         add("block",       &Loader::block);
771         add("block_hint",  &Loader::block_hint);
772         add("name",        &Loader::name);
773         add("real_speed",  &Loader::real_speed);
774         add("route",       &Loader::route);
775 }
776
777 void Train::Loader::block(unsigned id)
778 {
779         Block &blk = obj.layout.get_block(id);
780         int entry = -1;
781         if(prev_block)
782                 entry = blk.get_endpoint_by_link(*prev_block);
783         if(entry<0)
784                 entry = 0;
785
786         blk.reserve(&obj);
787         obj.cur_blocks.push_back(BlockRef(&blk, entry));
788         obj.set_status("Stopped");
789         const Block::Endpoint &bep = blk.get_endpoints()[entry];
790         obj.vehicles.back()->place(bep.track, bep.track_ep, 0, Vehicle::BACK_BUFFER);
791
792         if(blk.get_sensor_id())
793                 obj.layout.get_driver().set_sensor(blk.get_sensor_id(), true);
794
795         prev_block = &blk;
796 }
797
798 void Train::Loader::block_hint(unsigned id)
799 {
800         prev_block = &obj.layout.get_block(id);
801 }
802
803 void Train::Loader::name(const string &n)
804 {
805         obj.set_name(n);
806 }
807
808 void Train::Loader::real_speed(unsigned i, float speed, float weight)
809 {
810         obj.real_speed[i].speed = speed;
811         obj.real_speed[i].weight = weight;
812 }
813
814 void Train::Loader::route(const string &n)
815 {
816         obj.set_route(&obj.layout.get_route(n));
817 }
818
819 } // namespace Marklin