3 This file is part of R²C²
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include "vehicletype.h"
15 VehicleType::VehicleType(const ArticleNumber &an):
23 unsigned VehicleType::get_max_function() const
27 return (--functions.end())->first;
30 const VehicleType::Axle &VehicleType::get_axle(unsigned i) const
33 throw InvalidParameterValue("Axle index out of range");
37 const VehicleType::Bogie &VehicleType::get_bogie(unsigned i) const
40 throw InvalidParameterValue("Axle index out of range");
44 const VehicleType::Axle &VehicleType::get_bogie_axle(unsigned i, unsigned j) const
47 throw InvalidParameterValue("Axle index out of range");
48 if(j>=bogies[i].axles.size())
49 throw InvalidParameterValue("Axle index out of range");
50 return bogies[i].axles[j];
53 const VehicleType::Rod &VehicleType::get_rod(unsigned i) const
56 throw InvalidParameterValue("Rod index out of range");
60 float VehicleType::get_front_axle_offset() const
62 float front = length/2;
64 front = axles.front().position;
67 const Bogie &bogie = bogies.front();
68 front = max(front, bogie.position+bogie.axles.front().position);
73 float VehicleType::get_back_axle_offset() const
75 float back = -length/2;
77 back = axles.back().position;
80 const Bogie &bogie = bogies.back();
81 back = min(back, bogie.position+bogie.axles.back().position);
87 VehicleType::Axle::Axle():
94 VehicleType::Bogie::Bogie():
100 VehicleType::Rod::Rod():
110 VehicleType::Loader::Loader(VehicleType &vt):
111 DataFile::ObjectLoader<VehicleType>(vt)
113 add("axle", &Loader::axle);
114 add("bogie", &Loader::bogie);
115 add("function", &Loader::function);
116 add("height", &Loader::height);
117 add("length", &Loader::length);
118 add("locomotive", &VehicleType::locomotive);
119 add("name", &VehicleType::name);
120 add("object", &VehicleType::object);
121 add("rod", &Loader::rod);
122 add("width", &Loader::width);
125 void VehicleType::Loader::axle()
129 obj.axles.push_back(axl);
132 void VehicleType::Loader::bogie()
136 obj.bogies.push_back(bog);
139 void VehicleType::Loader::function(unsigned i, const string &f)
141 obj.functions[i] = f;
144 void VehicleType::Loader::height(float h)
149 void VehicleType::Loader::length(float l)
154 void VehicleType::Loader::rod()
157 Rod::Loader ldr(rd, rod_tags);
159 obj.rods.push_back(rd);
160 if(!ldr.get_tag().empty())
161 rod_tags[ldr.get_tag()] = obj.rods.size()-1;
162 rod_tags["previous"] = obj.rods.size()-1;
165 void VehicleType::Loader::width(float w)
171 VehicleType::Axle::Loader::Loader(Axle &a):
172 DataFile::ObjectLoader<Axle>(a)
174 add("object", &Axle::object);
175 add("position", &Loader::position);
176 add("powered", &Axle::powered);
177 add("wheel_diameter", &Loader::wheel_diameter);
180 void VehicleType::Axle::Loader::position(float p)
182 obj.position = p/1000;
185 void VehicleType::Axle::Loader::wheel_diameter(float d)
187 obj.wheel_dia = d/1000;
191 VehicleType::Bogie::Loader::Loader(Bogie &b):
192 DataFile::ObjectLoader<Bogie>(b)
194 add("axle", &Loader::axle);
195 add("object", &Bogie::object);
196 add("position", &Loader::position);
197 add("rotate_object", &Bogie::rotate_object);
200 void VehicleType::Bogie::Loader::axle()
204 obj.axles.push_back(axl);
207 void VehicleType::Bogie::Loader::position(float p)
209 obj.position = p/1000;
213 VehicleType::Rod::Loader::Loader(Rod &r, const map<string, unsigned> &t):
214 DataFile::ObjectLoader<Rod>(r),
217 add("connect", &Loader::connect);
218 add("limit", &Rod::limit);
219 add("mirror_object", &Rod::mirror_object);
220 add("object", &Rod::object);
221 add("pivot_body", &Loader::pivot_body);
222 add("pivot_axle", &Loader::pivot_axle);
223 add("pivot_axle", &Loader::pivot_bogie_axle);
224 add("pivot_rod", &Loader::pivot_rod);
225 add("position", &Loader::position);
226 add("tag", &Loader::set_tag);
229 void VehicleType::Rod::Loader::connect(const string &t, float px, float pz, float ox, float oz)
231 map<string, unsigned>::const_iterator i = tags.find(t);
233 throw KeyError("Unknown rod tag", t);
234 obj.connect_index = i->second;
235 obj.connect_point = Point(px/1000, 0, pz/1000);
236 obj.connect_offset = Point(ox/1000, 0, oz/1000);
239 void VehicleType::Rod::Loader::pivot_body()
244 void VehicleType::Rod::Loader::pivot_axle(unsigned i)
250 void VehicleType::Rod::Loader::pivot_bogie_axle(unsigned i, unsigned j)
252 obj.pivot = BOGIE_AXLE;
254 obj.pivot_index2 = j;
257 void VehicleType::Rod::Loader::pivot_rod(const string &t)
259 map<string, unsigned>::const_iterator i = tags.find(t);
261 throw KeyError("Unknown rod tag", t);
263 obj.pivot_index = i->second;
266 void VehicleType::Rod::Loader::position(float x, float y, float z)
268 obj.pivot_point = Point(x/1000, y/1000, z/1000);
271 void VehicleType::Rod::Loader::set_tag(const string &t)
277 void operator>>(const LexicalConverter &c, VehicleType::Rod::Limit &l)
279 const string &s = c.get();
281 l = VehicleType::Rod::FIXED;
283 l = VehicleType::Rod::ROTATE;
284 else if(s=="SLIDE_X")
285 l = VehicleType::Rod::SLIDE_X;
287 throw LexicalError("Invalid value for Rod::Limit");