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