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