]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/trainstatus.cpp
Avoid arriving too early if there's an unexpected stop on the last route
[r2c2.git] / source / libr2c2 / trainstatus.cpp
1 #include <msp/strings/format.h>
2 #include "catalogue.h"
3 #include "layout.h"
4 #include "train.h"
5 #include "trainstatus.h"
6 #include "vehicle.h"
7
8 using namespace Msp;
9
10 namespace R2C2 {
11
12 TrainStatus::TrainStatus(Train &t):
13         TrainAI(t),
14         speed(-2)
15 {
16         check();
17 }
18
19 void TrainStatus::tick(const Time::TimeDelta &)
20 {
21         check();
22 }
23
24 void TrainStatus::check()
25 {
26         float scale = train.get_layout().get_catalogue().get_scale();
27         int s = static_cast<int>(train.get_quantized_speed()*3.6/scale+0.5);
28         if(s==0 && train.get_block_allocator().is_active())
29                 s = -1;
30
31         if(s!=speed)
32         {
33                 if(s>0)
34                 {
35                         status = format("Traveling %d kmh", s);
36                         if(unsigned step = train.get_speed_step())
37                                 status += format(" (%d)", step);
38                 }
39                 else if(s==-1)
40                         status = "Waiting";
41                 else if(!train.get_vehicle(0).is_placed())
42                         status = "Unplaced";
43                 else
44                         status = "Stopped";
45
46                 speed = s;
47
48                 signal_changed.emit(status);
49                 signal_event.emit(Message("status-changed", status));
50         }
51 }
52
53 } // namespace R2C2