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