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