3 This file is part of R²C²
4 Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include "vehicletype.h"
15 VehicleType::VehicleType(const ArticleNumber &an):
18 swap_direction(false),
24 unsigned VehicleType::get_max_function() const
28 return (--functions.end())->first;
31 const VehicleType::Axle &VehicleType::get_fixed_axle(unsigned i) const
34 throw InvalidParameterValue("Axle index out of range");
38 const VehicleType::Bogie &VehicleType::get_bogie(unsigned i) const
41 throw InvalidParameterValue("Axle index out of range");
45 const VehicleType::Axle &VehicleType::get_bogie_axle(unsigned i, unsigned j) const
48 throw InvalidParameterValue("Axle index out of range");
49 if(j>=bogies[i].axles.size())
50 throw InvalidParameterValue("Axle index out of range");
51 return bogies[i].axles[j];
54 const VehicleType::Rod &VehicleType::get_rod(unsigned i) const
57 throw InvalidParameterValue("Rod index out of range");
61 float VehicleType::get_front_axle_offset() const
63 float front = length/2;
65 front = axles.front().position;
68 const Bogie &bogie = bogies.front();
69 front = max(front, bogie.position+bogie.axles.front().position);
74 float VehicleType::get_back_axle_offset() const
76 float back = -length/2;
78 back = axles.back().position;
81 const Bogie &bogie = bogies.back();
82 back = min(back, bogie.position+bogie.axles.back().position);
88 VehicleType::Axle::Axle():
95 VehicleType::Bogie::Bogie():
101 VehicleType::Rod::Rod():
111 VehicleType::Loader::Loader(VehicleType &vt):
112 DataFile::ObjectLoader<VehicleType>(vt)
114 add("axle", &Loader::axle);
115 add("bogie", &Loader::bogie);
116 add("function", &Loader::function);
117 add("height", &Loader::height);
118 add("length", &Loader::length);
119 add("locomotive", &VehicleType::locomotive);
120 add("name", &VehicleType::name);
121 add("object", &VehicleType::object);
122 add("rod", &Loader::rod);
123 add("swap_direction", &VehicleType::swap_direction);
124 add("width", &Loader::width);
127 void VehicleType::Loader::axle()
131 obj.axles.push_back(axl);
134 void VehicleType::Loader::bogie()
138 obj.bogies.push_back(bog);
141 void VehicleType::Loader::function(unsigned i, const string &f)
143 obj.functions[i] = f;
146 void VehicleType::Loader::height(float h)
151 void VehicleType::Loader::length(float l)
156 void VehicleType::Loader::rod()
159 Rod::Loader ldr(rd, rod_tags);
161 obj.rods.push_back(rd);
162 if(!ldr.get_tag().empty())
163 rod_tags[ldr.get_tag()] = obj.rods.size()-1;
164 rod_tags["previous"] = obj.rods.size()-1;
167 void VehicleType::Loader::width(float w)
173 VehicleType::Axle::Loader::Loader(Axle &a):
174 DataFile::ObjectLoader<Axle>(a)
176 add("object", &Axle::object);
177 add("position", &Loader::position);
178 add("powered", &Axle::powered);
179 add("wheel_diameter", &Loader::wheel_diameter);
182 void VehicleType::Axle::Loader::position(float p)
184 obj.position = p/1000;
187 void VehicleType::Axle::Loader::wheel_diameter(float d)
189 obj.wheel_dia = d/1000;
193 VehicleType::Bogie::Loader::Loader(Bogie &b):
194 DataFile::ObjectLoader<Bogie>(b)
196 add("axle", &Loader::axle);
197 add("object", &Bogie::object);
198 add("position", &Loader::position);
199 add("rotate_object", &Bogie::rotate_object);
202 void VehicleType::Bogie::Loader::axle()
206 obj.axles.push_back(axl);
209 void VehicleType::Bogie::Loader::position(float p)
211 obj.position = p/1000;
215 VehicleType::Rod::Loader::Loader(Rod &r, const map<string, unsigned> &t):
216 DataFile::ObjectLoader<Rod>(r),
219 add("connect", &Loader::connect);
220 add("limit", &Rod::limit);
221 add("mirror_object", &Rod::mirror_object);
222 add("object", &Rod::object);
223 add("pivot_body", &Loader::pivot_body);
224 add("pivot_axle", &Loader::pivot_axle);
225 add("pivot_axle", &Loader::pivot_bogie_axle);
226 add("pivot_rod", &Loader::pivot_rod);
227 add("position", &Loader::position);
228 add("tag", &Loader::set_tag);
231 void VehicleType::Rod::Loader::connect(const string &t, float px, float pz, float ox, float oz)
233 map<string, unsigned>::const_iterator i = tags.find(t);
235 throw KeyError("Unknown rod tag", t);
236 obj.connect_index = i->second;
237 obj.connect_point = Vector(px/1000, 0, pz/1000);
238 obj.connect_offset = Vector(ox/1000, 0, oz/1000);
241 void VehicleType::Rod::Loader::pivot_body()
246 void VehicleType::Rod::Loader::pivot_axle(unsigned i)
252 void VehicleType::Rod::Loader::pivot_bogie_axle(unsigned i, unsigned j)
254 obj.pivot = BOGIE_AXLE;
256 obj.pivot_index2 = j;
259 void VehicleType::Rod::Loader::pivot_rod(const string &t)
261 map<string, unsigned>::const_iterator i = tags.find(t);
263 throw KeyError("Unknown rod tag", t);
265 obj.pivot_index = i->second;
268 void VehicleType::Rod::Loader::position(float x, float y, float z)
270 obj.pivot_point = Vector(x/1000, y/1000, z/1000);
273 void VehicleType::Rod::Loader::set_tag(const string &t)
279 void operator>>(const LexicalConverter &c, VehicleType::Rod::Limit &l)
281 const string &s = c.get();
283 l = VehicleType::Rod::FIXED;
285 l = VehicleType::Rod::ROTATE;
286 else if(s=="SLIDE_X")
287 l = VehicleType::Rod::SLIDE_X;
289 throw LexicalError("Invalid value for Rod::Limit");