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