]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/vehicletype.cpp
Make vehicles trigger sensors as they pass over them, allowing full simulation
[r2c2.git] / source / libmarklin / vehicletype.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include "vehicletype.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 namespace Marklin {
14
15 VehicleType::VehicleType(unsigned n):
16         art_nr(n),
17         length(0),
18         width(0),
19         height(0)
20 { }
21
22 unsigned VehicleType::get_max_function() const
23 {
24         if(functions.empty())
25                 return 0;
26         return (--functions.end())->first;
27 }
28
29 float VehicleType::get_front_axle_offset() const
30 {
31         float front = length/2;
32         if(!axles.empty())
33                 front = axles.front().position;
34         if(!bogies.empty())
35         {
36                 const Bogie &bogie = bogies.front();
37                 front = max(front, bogie.position+bogie.axles.front().position);
38         }
39         return front;
40 }
41
42 float VehicleType::get_back_axle_offset() const
43 {
44         float back = -length/2;
45         if(!axles.empty())
46                 back = axles.back().position;
47         if(!bogies.empty())
48         {
49                 const Bogie &bogie = bogies.back();
50                 back = min(back, bogie.position+bogie.axles.back().position);
51         }
52         return back;
53 }
54
55
56 VehicleType::Axle::Axle():
57         position(0),
58         wheel_dia(0),
59         powered(false)
60 { }
61
62
63 VehicleType::Bogie::Bogie():
64         position(0),
65         rotate_object(false)
66 { }
67
68
69 VehicleType::Loader::Loader(VehicleType &vt):
70         DataFile::ObjectLoader<VehicleType>(vt)
71 {
72         add("axle",       &Loader::axle);
73         add("bogie",      &Loader::bogie);
74         add("function",   &Loader::function);
75         add("height",     &Loader::height);
76         add("length",     &Loader::length);
77         add("locomotive", &VehicleType::locomotive);
78         add("object",     &VehicleType::object);
79         add("name",       &VehicleType::name);
80         add("width",      &Loader::width);
81 }
82
83 void VehicleType::Loader::axle()
84 {
85         Axle axl;
86         load_sub(axl);
87         obj.axles.push_back(axl);
88 }
89
90 void VehicleType::Loader::bogie()
91 {
92         Bogie bog;
93         load_sub(bog);
94         obj.bogies.push_back(bog);
95 }
96
97 void VehicleType::Loader::function(unsigned i, const string &f)
98 {
99         obj.functions[i] = f;
100 }
101
102 void VehicleType::Loader::height(float h)
103 {
104         obj.height = h/1000;
105 }
106
107 void VehicleType::Loader::length(float l)
108 {
109         obj.length = l/1000;
110 }
111
112 void VehicleType::Loader::width(float w)
113 {
114         obj.width = w/1000;
115 }
116
117
118 VehicleType::Axle::Loader::Loader(Axle &a):
119         DataFile::ObjectLoader<Axle>(a)
120 {
121         add("position",       &Loader::position);
122         add("powered",        &Axle::powered);
123         add("wheel_diameter", &Loader::wheel_diameter);
124 }
125
126 void VehicleType::Axle::Loader::position(float p)
127 {
128         obj.position = p/1000;
129 }
130
131 void VehicleType::Axle::Loader::wheel_diameter(float d)
132 {
133         obj.wheel_dia = d/1000;
134 }
135
136
137 VehicleType::Bogie::Loader::Loader(Bogie &b):
138         DataFile::ObjectLoader<Bogie>(b)
139 {
140         add("axle",          &Loader::axle);
141         add("object",        &Bogie::object);
142         add("position",      &Loader::position);
143         add("rotate_object", &Bogie::rotate_object);
144 }
145
146 void VehicleType::Bogie::Loader::axle()
147 {
148         Axle axl;
149         load_sub(axl);
150         obj.axles.push_back(axl);
151 }
152
153 void VehicleType::Bogie::Loader::position(float p)
154 {
155         obj.position = p/1000;
156 }
157
158 } // namespace Marklin