+/* $Id$
+
+This file is part of R²C²
+Copyright © 2011 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <msp/strings/formatter.h>
+#include "catalogue.h"
+#include "layout.h"
+#include "train.h"
+#include "trainstatus.h"
+#include "vehicle.h"
+
+using namespace Msp;
+
+#include <msp/io/print.h>
+
+namespace R2C2 {
+
+TrainStatus::TrainStatus(Train &t):
+ TrainAI(t),
+ speed(-2)
+{
+ check();
+}
+
+void TrainStatus::tick(const Time::TimeStamp &, const Time::TimeDelta &)
+{
+ check();
+}
+
+void TrainStatus::check()
+{
+ float scale = train.get_layout().get_catalogue().get_scale();
+ int s = static_cast<int>(train.get_quantized_speed()*3.6/scale+0.5);
+ if(s==0 && train.is_active())
+ s = -1;
+
+ if(s!=speed)
+ {
+ if(s>0)
+ {
+ status = format("Traveling %d kmh", s);
+ if(unsigned step = train.get_speed_step())
+ status += format(" (%d)", step);
+ }
+ else if(s==-1)
+ status = "Waiting";
+ else if(!train.get_vehicle(0).get_track())
+ status = "Unplaced";
+ else
+ status = "Stopped";
+
+ speed = s;
+
+ signal_changed.emit(status);
+ signal_event.emit(Message("status-changed", status));
+ }
+}
+
+} // namespace R2C2