]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/trainstatus.cpp
Remove status from Train and turn it into a TrainAI
[r2c2.git] / source / libr2c2 / trainstatus.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2011  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/strings/formatter.h>
9 #include "catalogue.h"
10 #include "layout.h"
11 #include "train.h"
12 #include "trainstatus.h"
13 #include "vehicle.h"
14
15 using namespace Msp;
16
17 #include <msp/io/print.h>
18
19 namespace R2C2 {
20
21 TrainStatus::TrainStatus(Train &t):
22         TrainAI(t),
23         speed(-2)
24 {
25         check();
26 }
27
28 void TrainStatus::tick(const Time::TimeStamp &, const Time::TimeDelta &)
29 {
30         check();
31 }
32
33 void TrainStatus::check()
34 {
35         float scale = train.get_layout().get_catalogue().get_scale();
36         int s = static_cast<int>(train.get_quantized_speed()*3.6/scale+0.5);
37         if(s==0 && train.is_active())
38                 s = -1;
39
40         if(s!=speed)
41         {
42                 if(s>0)
43                 {
44                         status = format("Traveling %d kmh", s);
45                         if(unsigned step = train.get_speed_step())
46                                 status += format(" (%d)", step);
47                 }
48                 else if(s==-1)
49                         status = "Waiting";
50                 else if(!train.get_vehicle(0).get_track())
51                         status = "Unplaced";
52                 else
53                         status = "Stopped";
54
55                 speed = s;
56
57                 signal_changed.emit(status);
58                 signal_event.emit(Message("status-changed", status));
59         }
60 }
61
62 } // namespace R2C2