]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/FGFDM.cpp
FGPUIDialog: fix reading from already free'd memory.
[flightgear.git] / src / FDM / YASim / FGFDM.cpp
1 #ifdef HAVE_CONFIG_H
2 #  include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <Main/fg_props.hxx>
9
10 #include "Math.hpp"
11 #include "Jet.hpp"
12 #include "SimpleJet.hpp"
13 #include "Gear.hpp"
14 #include "Hook.hpp"
15 #include "Launchbar.hpp"
16 #include "Atmosphere.hpp"
17 #include "PropEngine.hpp"
18 #include "Propeller.hpp"
19 #include "PistonEngine.hpp"
20 #include "TurbineEngine.hpp"
21 #include "Rotor.hpp"
22 #include "Rotorpart.hpp"
23 #include "Hitch.hpp"
24
25 #include "FGFDM.hpp"
26
27 namespace yasim {
28
29 // Some conversion factors
30 static const float KTS2MPS = 0.514444444444;
31 static const float FT2M = 0.3048;
32 static const float DEG2RAD = 0.0174532925199;
33 static const float RPM2RAD = 0.10471975512;
34 static const float LBS2N = 4.44822;
35 static const float LBS2KG = 0.45359237;
36 static const float KG2LBS = 2.2046225;
37 static const float CM2GALS = 264.172037284;
38 static const float HP2W = 745.700;
39 static const float INHG2PA = 3386.389;
40 static const float K2DEGF = 1.8;
41 static const float K2DEGFOFFSET = -459.4;
42 static const float CIN2CM = 1.6387064e-5;
43 static const float YASIM_PI = 3.14159265358979323846;
44
45 static const float NM2FTLB = (1/(LBS2N*FT2M));
46
47 // Stubs, so that this can be compiled without the FlightGear
48 // binary.  What's the best way to handle this?
49
50 //     float fgGetFloat(char* name, float def) { return 0; }
51 //     void fgSetFloat(char* name, float val) {}
52
53 FGFDM::FGFDM()
54 {
55     _vehicle_radius = 0.0f;
56
57     _nextEngine = 0;
58
59     // Map /controls/flight/elevator to the approach elevator control.  This
60     // should probably be settable, but there are very few aircraft
61     // who trim their approaches using things other than elevator.
62     _airplane.setElevatorControl(parseAxis("/controls/flight/elevator-trim"));
63
64     // FIXME: read seed from somewhere?
65     int seed = 0;
66     _turb = new Turbulence(10, seed);
67 }
68
69 FGFDM::~FGFDM()
70 {
71     for(int i=0; i<_axes.size(); i++) {
72         AxisRec* a = (AxisRec*)_axes.get(i);
73         delete[] a->name;
74         delete a;
75     }
76
77     for(int i=0; i<_thrusters.size(); i++) {
78         EngRec* er = (EngRec*)_thrusters.get(i);
79         delete[] er->prefix;
80         delete er->eng;
81         delete er;
82     }
83
84     for(int i=0; i<_weights.size(); i++) {
85         WeightRec* wr = (WeightRec*)_weights.get(i);
86         delete[] wr->prop;
87         delete wr;
88     }
89
90     for(int i=0; i<_controlProps.size(); i++)
91         delete (PropOut*)_controlProps.get(i);
92
93     delete _turb;
94 }
95
96 void FGFDM::iterate(float dt)
97 {
98     getExternalInput(dt);
99     _airplane.iterate(dt);
100
101     // Do fuel stuff
102     for(int i=0; i<_airplane.numThrusters(); i++) {
103         Thruster* t = _airplane.getThruster(i);
104
105         bool out_of_fuel = _fuel_props[i]._out_of_fuel->getBoolValue();
106         t->setFuelState(!out_of_fuel);
107
108         double consumed = _fuel_props[i]._fuel_consumed_lbs->getDoubleValue();
109         _fuel_props[i]._fuel_consumed_lbs->setDoubleValue(
110                 consumed + dt * KG2LBS * t->getFuelFlow());
111     }
112     for(int i=0; i<_airplane.numTanks(); i++) {
113         _airplane.setFuel(i, LBS2KG * _tank_level_lbs[i]->getFloatValue());
114     }
115     _airplane.calcFuelWeights();
116     
117     setOutputProperties(dt);
118 }
119
120 Airplane* FGFDM::getAirplane()
121 {
122     return &_airplane;
123 }
124
125 void FGFDM::init()
126 {
127     _turb_magnitude_norm = fgGetNode("/environment/turbulence/magnitude-norm", true);
128     _turb_rate_hz        = fgGetNode("/environment/turbulence/rate-hz", true);
129     _gross_weight_lbs    = fgGetNode("/yasim/gross-weight-lbs", true);
130
131     // Allows the user to start with something other than full fuel
132     _airplane.setFuelFraction(fgGetFloat("/sim/fuel-fraction", 1));
133
134     // stash engine/thruster properties
135     _thrust_props.clear();
136     for (int i=0; i<_thrusters.size(); i++) {
137         SGPropertyNode_ptr node = fgGetNode("engines/engine", i, true);
138         Thruster* t = ((EngRec*)_thrusters.get(i))->eng;
139
140         ThrusterProps tp;
141         tp._running =       node->getChild("running", 0, true);
142         tp._cranking =      node->getChild("cranking", 0, true);
143         tp._prop_thrust =   node->getChild("prop-thrust", 0, true); // Deprecated name
144         tp._thrust_lbs =    node->getChild("thrust-lbs", 0, true);
145         tp._fuel_flow_gph = node->getChild("fuel-flow-gph", 0, true);
146
147         if(t->getPropEngine())
148         {
149             tp._rpm = node->getChild("rpm", 0, true);
150             tp._torque_ftlb = node->getChild("torque-ftlb", 0, true);
151
152             PropEngine* p = t->getPropEngine();
153             if(p->getEngine()->isPistonEngine())
154             {
155                 tp._mp_osi =   node->getChild("mp-osi",   0, true);
156                 tp._mp_inhg =  node->getChild("mp-inhg",  0, true);
157                 tp._egt_degf = node->getChild("egt-degf", 0, true);
158
159                 tp._oil_temperature_degf = node->getChild("oil-temperature-degf", 0, true);
160                 tp._boost_gauge_inhg =     node->getChild("boost-gauge-inhg", 0, true);
161             } else if(p->getEngine()->isTurbineEngine()) {
162                 tp._n2 = node->getChild("n2", 0, true);
163             }
164         }
165
166         if(t->getJet())
167         {
168             tp._n1 =       node->getChild("n1",       0, true);
169             tp._n2 =       node->getChild("n2",       0, true);
170             tp._epr =      node->getChild("epr",      0, true);
171             tp._egt_degf = node->getChild("egt-degf", 0, true);
172         }
173         _thrust_props.push_back(tp);
174     }
175
176     // stash properties for fuel state
177     _fuel_props.clear();
178     for(int i=0; i<_airplane.numThrusters(); i++) {
179         SGPropertyNode_ptr e = fgGetNode("engines/engine", i, true);
180         FuelProps f;
181         f._out_of_fuel       = e->getChild("out-of-fuel", 0, true);
182         f._fuel_consumed_lbs = e->getChild("fuel-consumed-lbs", 0, true);
183         _fuel_props.push_back(f);
184     }
185
186     // initialize tanks and stash properties for tank level
187     _tank_level_lbs.clear();
188     for(int i=0; i<_airplane.numTanks(); i++) {
189         char buf[256];
190         sprintf(buf, "/consumables/fuel/tank[%d]/level-lbs", i);
191         fgSetDouble(buf, _airplane.getFuel(i) * KG2LBS);
192         _tank_level_lbs.push_back(fgGetNode(buf, true));
193
194         double density = _airplane.getFuelDensity(i);
195         sprintf(buf, "/consumables/fuel/tank[%d]/density-ppg", i);
196         fgSetDouble(buf, density * (KG2LBS/CM2GALS));
197
198 // set in TankProperties class
199 //        sprintf(buf, "/consumables/fuel/tank[%d]/level-gal_us", i);
200 //        fgSetDouble(buf, _airplane.getFuel(i) * CM2GALS / density);
201
202         sprintf(buf, "/consumables/fuel/tank[%d]/capacity-gal_us", i);
203         fgSetDouble(buf, CM2GALS * _airplane.getTankCapacity(i)/density);
204     }
205
206     // This has a nasty habit of being false at startup.  That's not
207     // good.
208     fgSetBool("/controls/gear/gear-down", true);
209
210     _airplane.getModel()->setTurbulence(_turb);
211 }
212
213 // Not the worlds safest parser.  But it's short & sweet.
214 void FGFDM::startElement(const char* name, const XMLAttributes &atts)
215 {
216     XMLAttributes* a = (XMLAttributes*)&atts;
217     float v[3];
218     char buf[64];
219
220     if(eq(name, "airplane")) {
221         _airplane.setWeight(attrf(a, "mass") * LBS2KG);
222     } else if(eq(name, "approach")) {
223         float spd = attrf(a, "speed") * KTS2MPS;
224         float alt = attrf(a, "alt", 0) * FT2M;
225         float aoa = attrf(a, "aoa", 0) * DEG2RAD;
226         float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
227         _airplane.setApproach(spd, alt, aoa, attrf(a, "fuel", 0.2),gla);
228         _cruiseCurr = false;
229     } else if(eq(name, "cruise")) {
230         float spd = attrf(a, "speed") * KTS2MPS;
231         float alt = attrf(a, "alt") * FT2M;
232         float gla = attrf(a, "glide-angle", 0) * DEG2RAD;
233         _airplane.setCruise(spd, alt, attrf(a, "fuel", 0.5),gla);
234         _cruiseCurr = true;
235     } else if(eq(name, "solve-weight")) {
236         int idx = attri(a, "idx");
237         float wgt = attrf(a, "weight") * LBS2KG;
238         _airplane.addSolutionWeight(!_cruiseCurr, idx, wgt);
239     } else if(eq(name, "cockpit")) {
240         v[0] = attrf(a, "x");
241         v[1] = attrf(a, "y");
242         v[2] = attrf(a, "z");
243         _airplane.setPilotPos(v);
244     } else if(eq(name, "rotor")) {
245         _airplane.getModel()->getRotorgear()->addRotor(parseRotor(a, name));
246     } else if(eq(name, "rotorgear")) {
247         Rotorgear* r = _airplane.getModel()->getRotorgear();
248         _currObj = r;
249         #define p(x) if (a->hasAttribute(#x)) r->setParameter((char *)#x,attrf(a,#x) );
250         #define p2(x,y) if (a->hasAttribute(y)) r->setParameter((char *)#x,attrf(a,y) );
251         p2(max_power_engine,"max-power-engine")
252         p2(engine_prop_factor,"engine-prop-factor")
253         p(yasimdragfactor)
254         p(yasimliftfactor)
255         p2(max_power_rotor_brake,"max-power-rotor-brake")
256         p2(rotorgear_friction,"rotorgear-friction")
257         p2(engine_accel_limit,"engine-accel-limit")
258         #undef p
259         #undef p2
260         r->setInUse();
261     } else if(eq(name, "wing")) {
262         _airplane.setWing(parseWing(a, name));
263     } else if(eq(name, "hstab")) {
264         _airplane.setTail(parseWing(a, name));
265     } else if(eq(name, "vstab") || eq(name, "mstab")) {
266         _airplane.addVStab(parseWing(a, name));
267     } else if(eq(name, "piston-engine")) {
268         parsePistonEngine(a);
269     } else if(eq(name, "turbine-engine")) {
270         parseTurbineEngine(a);
271     } else if(eq(name, "propeller")) {
272         parsePropeller(a);
273     } else if(eq(name, "thruster")) {
274         SimpleJet* j = new SimpleJet();
275         _currObj = j;
276         v[0] = attrf(a, "x"); v[1] = attrf(a, "y"); v[2] = attrf(a, "z");
277         j->setPosition(v);
278         _airplane.addThruster(j, 0, v);
279         v[0] = attrf(a, "vx"); v[1] = attrf(a, "vy"); v[2] = attrf(a, "vz");
280         j->setDirection(v);
281         j->setThrust(attrf(a, "thrust") * LBS2N);
282     } else if(eq(name, "jet")) {
283         Jet* j = new Jet();
284         _currObj = j;
285         v[0] = attrf(a, "x");
286         v[1] = attrf(a, "y");
287         v[2] = attrf(a, "z");
288         float mass = attrf(a, "mass") * LBS2KG;
289         j->setMaxThrust(attrf(a, "thrust") * LBS2N,
290                         attrf(a, "afterburner", 0) * LBS2N);
291         j->setVectorAngle(attrf(a, "rotate", 0) * DEG2RAD);
292         j->setReverseThrust(attrf(a, "reverse", 0.2));
293
294         float n1min = attrf(a, "n1-idle", 55);
295         float n1max = attrf(a, "n1-max", 102);
296         float n2min = attrf(a, "n2-idle", 73);
297         float n2max = attrf(a, "n2-max", 103);
298         j->setRPMs(n1min, n1max, n2min, n2max);
299
300         j->setTSFC(attrf(a, "tsfc", 0.8));
301         if(a->hasAttribute("egt"))  j->setEGT(attrf(a, "egt"));
302         if(a->hasAttribute("epr"))  j->setEPR(attrf(a, "epr"));
303         if(a->hasAttribute("exhaust-speed"))
304             j->setVMax(attrf(a, "exhaust-speed") * KTS2MPS);
305         if(a->hasAttribute("spool-time"))
306             j->setSpooling(attrf(a, "spool-time"));
307         
308         j->setPosition(v);
309         _airplane.addThruster(j, mass, v);
310         sprintf(buf, "/engines/engine[%d]", _nextEngine++);
311         EngRec* er = new EngRec();
312         er->eng = j;
313         er->prefix = dup(buf);
314         _thrusters.add(er);
315     } else if(eq(name, "hitch")) {
316         Hitch* h = new Hitch(a->getValue("name"));
317         _currObj = h;
318         v[0] = attrf(a, "x");
319         v[1] = attrf(a, "y");
320         v[2] = attrf(a, "z");
321         h->setPosition(v);
322         if(a->hasAttribute("force-is-calculated-by-other")) h->setForceIsCalculatedByOther(attrb(a,"force-is-calculated-by-other"));
323         _airplane.addHitch(h);
324     } else if(eq(name, "tow")) {
325         Hitch* h = (Hitch*)_currObj;
326         if(a->hasAttribute("length"))
327             h->setTowLength(attrf(a, "length"));
328         if(a->hasAttribute("elastic-constant"))
329             h->setTowElasticConstant(attrf(a, "elastic-constant"));
330         if(a->hasAttribute("break-force"))
331             h->setTowBreakForce(attrf(a, "break-force"));
332         if(a->hasAttribute("weight-per-meter"))
333             h->setTowWeightPerM(attrf(a, "weight-per-meter"));
334         if(a->hasAttribute("mp-auto-connect-period"))
335             h->setMpAutoConnectPeriod(attrf(a, "mp-auto-connect-period"));
336     } else if(eq(name, "winch")) {
337         Hitch* h = (Hitch*)_currObj;
338         double pos[3];
339         pos[0] = attrd(a, "x",0);
340         pos[1] = attrd(a, "y",0);
341         pos[2] = attrd(a, "z",0);
342         h->setWinchPosition(pos);
343         if(a->hasAttribute("max-speed"))
344             h->setWinchMaxSpeed(attrf(a, "max-speed"));
345         if(a->hasAttribute("power"))
346             h->setWinchPower(attrf(a, "power") * 1000);
347         if(a->hasAttribute("max-force"))
348             h->setWinchMaxForce(attrf(a, "max-force"));
349         if(a->hasAttribute("initial-tow-length"))
350             h->setWinchInitialTowLength(attrf(a, "initial-tow-length"));
351         if(a->hasAttribute("max-tow-length"))
352             h->setWinchMaxTowLength(attrf(a, "max-tow-length"));
353         if(a->hasAttribute("min-tow-length"))
354             h->setWinchMinTowLength(attrf(a, "min-tow-length"));
355     } else if(eq(name, "gear")) {
356         Gear* g = new Gear();
357         _currObj = g;
358         v[0] = attrf(a, "x");
359         v[1] = attrf(a, "y");
360         v[2] = attrf(a, "z");
361         g->setPosition(v);
362         float nrm = Math::mag3(v);
363         if (_vehicle_radius < nrm)
364             _vehicle_radius = nrm;
365         if(a->hasAttribute("upx")) {
366             v[0] = attrf(a, "upx");
367             v[1] = attrf(a, "upy");
368             v[2] = attrf(a, "upz");
369             Math::unit3(v, v);
370         } else {
371             v[0] = 0;
372             v[1] = 0;
373             v[2] = 1;
374         }
375         for(int i=0; i<3; i++)
376             v[i] *= attrf(a, "compression", 1);
377         g->setCompression(v);
378         g->setBrake(attrf(a, "skid", 0));
379         g->setInitialLoad(attrf(a, "initial-load", 0));
380         g->setStaticFriction(attrf(a, "sfric", 0.8));
381         g->setDynamicFriction(attrf(a, "dfric", 0.7));
382         g->setSpring(attrf(a, "spring", 1));
383         g->setDamping(attrf(a, "damp", 1));
384         if(a->hasAttribute("on-water")) g->setOnWater(attrb(a,"on-water"));
385         if(a->hasAttribute("on-solid")) g->setOnSolid(attrb(a,"on-solid"));
386         if(a->hasAttribute("ignored-by-solver")) g->setIgnoreWhileSolving(attrb(a,"ignored-by-solver"));
387         g->setSpringFactorNotPlaning(attrf(a, "spring-factor-not-planing", 1));
388         g->setSpeedPlaning(attrf(a, "speed-planing", 0) * KTS2MPS);
389         g->setReduceFrictionByExtension(attrf(a, "reduce-friction-by-extension", 0));
390         _airplane.addGear(g);
391     } else if(eq(name, "hook")) {
392         Hook* h = new Hook();
393         _currObj = h;
394         v[0] = attrf(a, "x");
395         v[1] = attrf(a, "y");
396         v[2] = attrf(a, "z");
397         h->setPosition(v);
398         float length = attrf(a, "length", 1.0);
399         h->setLength(length);
400         float nrm = length+Math::mag3(v);
401         if (_vehicle_radius < nrm)
402             _vehicle_radius = nrm;
403         h->setDownAngle(attrf(a, "down-angle", 70) * DEG2RAD);
404         h->setUpAngle(attrf(a, "up-angle", 0) * DEG2RAD);
405         _airplane.addHook(h);
406     } else if(eq(name, "launchbar")) {
407         Launchbar* l = new Launchbar();
408         _currObj = l;
409         v[0] = attrf(a, "x");
410         v[1] = attrf(a, "y");
411         v[2] = attrf(a, "z");
412         l->setLaunchbarMount(v);
413         v[0] = attrf(a, "holdback-x", v[0]);
414         v[1] = attrf(a, "holdback-y", v[1]);
415         v[2] = attrf(a, "holdback-z", v[2]);
416         l->setHoldbackMount(v);
417         float length = attrf(a, "length", 1.0);
418         l->setLength(length);
419         l->setDownAngle(attrf(a, "down-angle", 45) * DEG2RAD);
420         l->setUpAngle(attrf(a, "up-angle", -45) * DEG2RAD);
421         l->setHoldbackLength(attrf(a, "holdback-length", 2.0));
422         _airplane.addLaunchbar(l);
423     } else if(eq(name, "fuselage")) {
424         float b[3];
425         v[0] = attrf(a, "ax");
426         v[1] = attrf(a, "ay");
427         v[2] = attrf(a, "az");
428         b[0] = attrf(a, "bx");
429         b[1] = attrf(a, "by");
430         b[2] = attrf(a, "bz");
431         float taper = attrf(a, "taper", 1);
432         float mid = attrf(a, "midpoint", 0.5);
433         float cx = attrf(a, "cx", 1);
434         float cy = attrf(a, "cy", 1);
435         float cz = attrf(a, "cz", 1);
436         float idrag = attrf(a, "idrag", 1);
437         _airplane.addFuselage(v, b, attrf(a, "width"), taper, mid, 
438             cx, cy, cz, idrag);
439     } else if(eq(name, "tank")) {
440         v[0] = attrf(a, "x");
441         v[1] = attrf(a, "y");
442         v[2] = attrf(a, "z");
443         float density = 6.0; // gasoline, in lbs/gal
444         if(a->hasAttribute("jet")) density = 6.72; 
445         density *= LBS2KG*CM2GALS;
446         _airplane.addTank(v, attrf(a, "capacity") * LBS2KG, density);
447     } else if(eq(name, "ballast")) {
448         v[0] = attrf(a, "x");
449         v[1] = attrf(a, "y");
450         v[2] = attrf(a, "z");
451         _airplane.addBallast(v, attrf(a, "mass") * LBS2KG);
452     } else if(eq(name, "weight")) {
453         parseWeight(a);
454     } else if(eq(name, "stall")) {
455         Wing* w = (Wing*)_currObj;
456         w->setStall(attrf(a, "aoa") * DEG2RAD);
457         w->setStallWidth(attrf(a, "width", 2) * DEG2RAD);
458         w->setStallPeak(attrf(a, "peak", 1.5));
459     } else if(eq(name, "flap0")) {
460         ((Wing*)_currObj)->setFlap0(attrf(a, "start"), attrf(a, "end"),
461                                     attrf(a, "lift"), attrf(a, "drag"));
462     } else if(eq(name, "flap1")) {
463         ((Wing*)_currObj)->setFlap1(attrf(a, "start"), attrf(a, "end"),
464                                     attrf(a, "lift"), attrf(a, "drag"));
465     } else if(eq(name, "slat")) {
466         ((Wing*)_currObj)->setSlat(attrf(a, "start"), attrf(a, "end"),
467                                    attrf(a, "aoa"), attrf(a, "drag"));
468     } else if(eq(name, "spoiler")) {
469         ((Wing*)_currObj)->setSpoiler(attrf(a, "start"), attrf(a, "end"),
470                                       attrf(a, "lift"), attrf(a, "drag"));
471     /* } else if(eq(name, "collective")) {
472         ((Rotor*)_currObj)->setcollective(attrf(a, "min"), attrf(a, "max"));
473     } else if(eq(name, "cyclic")) {
474         ((Rotor*)_currObj)->setcyclic(attrf(a, "ail"), attrf(a, "ele"));
475     */                               
476     } else if(eq(name, "actionpt")) {
477         v[0] = attrf(a, "x");
478         v[1] = attrf(a, "y");
479         v[2] = attrf(a, "z");
480         ((Thruster*)_currObj)->setPosition(v);
481     } else if(eq(name, "dir")) {
482         v[0] = attrf(a, "x");
483         v[1] = attrf(a, "y");
484         v[2] = attrf(a, "z");
485         ((Thruster*)_currObj)->setDirection(v);
486     } else if(eq(name, "control-setting")) {
487         // A cruise or approach control setting
488         const char* axis = a->getValue("axis");
489         float value = attrf(a, "value", 0);
490         if(_cruiseCurr)
491             _airplane.addCruiseControl(parseAxis(axis), value);
492         else
493             _airplane.addApproachControl(parseAxis(axis), value);
494     } else if(eq(name, "control-input")) {
495
496         // A mapping of input property to a control
497         int axis = parseAxis(a->getValue("axis"));
498         int control = parseOutput(a->getValue("control"));
499         int opt = 0;
500         opt |= a->hasAttribute("split") ? ControlMap::OPT_SPLIT : 0;
501         opt |= a->hasAttribute("invert") ? ControlMap::OPT_INVERT : 0;
502         opt |= a->hasAttribute("square") ? ControlMap::OPT_SQUARE : 0;
503         
504         ControlMap* cm = _airplane.getControlMap();
505         if(a->hasAttribute("src0")) {
506                            cm->addMapping(axis, control, _currObj, opt,
507                            attrf(a, "src0"), attrf(a, "src1"), 
508                            attrf(a, "dst0"), attrf(a, "dst1"));
509         } else {
510             cm->addMapping(axis, control, _currObj, opt);
511         }
512     } else if(eq(name, "control-output")) {
513         // A property output for a control on the current object
514         ControlMap* cm = _airplane.getControlMap();
515         int type = parseOutput(a->getValue("control"));
516         int handle = cm->getOutputHandle(_currObj, type);
517
518         PropOut* p = new PropOut();
519         p->prop = fgGetNode(a->getValue("prop"), true);
520         p->handle = handle;
521         p->type = type;
522         p->left = !(a->hasAttribute("side") &&
523                         eq("right", a->getValue("side")));
524         p->min = attrf(a, "min", cm->rangeMin(type));
525         p->max = attrf(a, "max", cm->rangeMax(type));
526         _controlProps.add(p);
527
528     } else if(eq(name, "control-speed")) {
529         ControlMap* cm = _airplane.getControlMap();
530         int type = parseOutput(a->getValue("control"));
531         int handle = cm->getOutputHandle(_currObj, type);
532         float time = attrf(a, "transition-time", 0);
533         
534         cm->setTransitionTime(handle, time);
535     } else {
536         SG_LOG(SG_FLIGHT,SG_ALERT,"Unexpected tag '"
537                << name << "' found in YASim aircraft description");
538         exit(1);
539     }
540 }
541
542 void FGFDM::getExternalInput(float dt)
543 {
544     char buf[256];
545
546     _turb->setMagnitude(_turb_magnitude_norm->getFloatValue());
547     _turb->update(dt, _turb_rate_hz->getFloatValue());
548
549     // The control axes
550     ControlMap* cm = _airplane.getControlMap();
551     cm->reset();
552
553     for(int i=0; i<_axes.size(); i++) {
554         AxisRec* a = (AxisRec*)_axes.get(i);
555         float val = fgGetFloat(a->name, 0);
556         cm->setInput(a->handle, val);
557     }
558     cm->applyControls(dt);
559
560     // Weights
561     for(int i=0; i<_weights.size(); i++) {
562         WeightRec* wr = (WeightRec*)_weights.get(i);
563         _airplane.setWeight(wr->handle, LBS2KG * fgGetFloat(wr->prop));
564     }
565
566     for(int i=0; i<_thrusters.size(); i++) {
567         EngRec* er = (EngRec*)_thrusters.get(i);
568         Thruster* t = er->eng;
569
570         if(t->getPropEngine()) {
571             PropEngine* p = t->getPropEngine();
572             sprintf(buf, "%s/rpm", er->prefix);
573             p->setOmega(fgGetFloat(buf, 500) * RPM2RAD);
574         }
575     }
576 }
577
578 // Linearly "seeks" a property by the specified fraction of the way to
579 // the target value.  Used to emulate "slowly changing" output values.
580 static void moveprop(SGPropertyNode* node, const char* prop,
581                     float target, float frac)
582 {
583     float val = node->getFloatValue(prop);
584     if(frac > 1) frac = 1;
585     if(frac < 0) frac = 0;
586     val += (target - val) * frac;
587     node->setFloatValue(prop, val);
588 }
589
590 void FGFDM::setOutputProperties(float dt)
591 {
592     float grossWgt = _airplane.getModel()->getBody()->getTotalMass() * KG2LBS;
593     _gross_weight_lbs->setFloatValue(grossWgt);
594
595     ControlMap* cm = _airplane.getControlMap();
596     for(int i=0; i<_controlProps.size(); i++) {
597         PropOut* p = (PropOut*)_controlProps.get(i);
598         float val = (p->left
599                      ? cm->getOutput(p->handle)
600                      : cm->getOutputR(p->handle));
601         float rmin = cm->rangeMin(p->type);
602         float rmax = cm->rangeMax(p->type);
603         float frac = (val - rmin) / (rmax - rmin);
604         val = frac*(p->max - p->min) + p->min;
605         p->prop->setFloatValue(val);
606     }
607
608     for(int i=0; i<_airplane.getRotorgear()->getNumRotors(); i++) {
609         Rotor*r=(Rotor*)_airplane.getRotorgear()->getRotor(i);
610         int j = 0;
611         float f;
612         char b[256];
613         while((j = r->getValueforFGSet(j, b, &f)))
614             if(b[0]) fgSetFloat(b,f);
615         j=0;
616         while((j = _airplane.getRotorgear()->getValueforFGSet(j, b, &f)))
617             if(b[0]) fgSetFloat(b,f);
618         for(j=0; j < r->numRotorparts(); j+=r->numRotorparts()>>2) {
619             Rotorpart* s = (Rotorpart*)r->getRotorpart(j);
620             char *b;
621             int k;
622             for(k=0; k<2; k++) {
623                 b=s->getAlphaoutput(k);
624                 if(b[0]) fgSetFloat(b, s->getAlpha(k));
625             }
626         }
627     }
628
629     // Use the density of the first tank, or a dummy value if no tanks
630     float fuelDensity = 1.0;
631     if(_airplane.numTanks())
632         fuelDensity = _airplane.getFuelDensity(0);
633     for(int i=0; i<_thrusters.size(); i++) {
634         EngRec* er = (EngRec*)_thrusters.get(i);
635         Thruster* t = er->eng;
636         SGPropertyNode * node = fgGetNode("engines/engine", i, true);
637
638         ThrusterProps& tp = _thrust_props[i];
639
640         // Set: running, cranking, prop-thrust, max-hp, power-pct
641         tp._running->setBoolValue(t->isRunning());
642         tp._cranking->setBoolValue(t->isCranking());
643
644         float tmp[3];
645         t->getThrust(tmp);
646         float lbs = Math::mag3(tmp) * (KG2LBS/9.8);
647         tp._prop_thrust->setFloatValue(lbs); // Deprecated name
648         tp._thrust_lbs->setFloatValue(lbs);
649         tp._fuel_flow_gph->setFloatValue(
650                 (t->getFuelFlow()/fuelDensity) * 3600 * CM2GALS);
651
652         if(t->getPropEngine()) {
653             PropEngine* p = t->getPropEngine();
654             tp._rpm->setFloatValue(p->getOmega() * (1/RPM2RAD));
655             tp._torque_ftlb->setFloatValue(
656                     p->getEngine()->getTorque() * NM2FTLB);
657
658             if(p->getEngine()->isPistonEngine()) {
659                 PistonEngine* pe = p->getEngine()->isPistonEngine();
660                 tp._mp_osi->setFloatValue(pe->getMP() * (1/INHG2PA));
661                 tp._mp_inhg->setFloatValue(pe->getMP() * (1/INHG2PA));
662                 tp._egt_degf->setFloatValue(
663                         pe->getEGT() * K2DEGF + K2DEGFOFFSET);
664                 tp._oil_temperature_degf->setFloatValue(
665                         pe->getOilTemp() * K2DEGF + K2DEGFOFFSET);
666                 tp._boost_gauge_inhg->setFloatValue(
667                         pe->getBoost() * (1/INHG2PA));
668             } else if(p->getEngine()->isTurbineEngine()) {
669                 TurbineEngine* te = p->getEngine()->isTurbineEngine();
670                 tp._n2->setFloatValue(te->getN2());
671             }
672         }
673
674         if(t->getJet()) {
675             Jet* j = t->getJet();
676             tp._n1->setFloatValue(j->getN1());
677             tp._n2->setFloatValue(j->getN2());
678             tp._epr->setFloatValue(j->getEPR());
679             tp._egt_degf->setFloatValue(
680                     j->getEGT() * K2DEGF + K2DEGFOFFSET);
681
682             // These are "unmodeled" values that are still needed for
683             // many cockpits.  Tie them all to the N1 speed, but
684             // normalize the numbers to the range [0:1] so the
685             // cockpit code can scale them to the right values.
686             float pnorm = j->getPerfNorm();
687             moveprop(node, "oilp-norm", pnorm, dt/3); // 3s seek time
688             moveprop(node, "oilt-norm", pnorm, dt/30); // 30s 
689             moveprop(node, "itt-norm", pnorm, dt/1); // 1s
690         }
691     }
692 }
693
694 Wing* FGFDM::parseWing(XMLAttributes* a, const char* type)
695 {
696     Wing* w = new Wing();
697
698     float defDihed = 0;
699     if(eq(type, "vstab"))
700         defDihed = 90;
701     else
702         w->setMirror(true);
703
704     float pos[3];
705     pos[0] = attrf(a, "x");
706     pos[1] = attrf(a, "y");
707     pos[2] = attrf(a, "z");
708     w->setBase(pos);
709
710     w->setLength(attrf(a, "length"));
711     w->setChord(attrf(a, "chord"));
712     w->setSweep(attrf(a, "sweep", 0) * DEG2RAD);
713     w->setTaper(attrf(a, "taper", 1));
714     w->setDihedral(attrf(a, "dihedral", defDihed) * DEG2RAD);
715     w->setCamber(attrf(a, "camber", 0));
716
717     // These come in with positive indicating positive AoA, but the
718     // internals expect a rotation about the left-pointing Y axis, so
719     // invert the sign.
720     w->setIncidence(attrf(a, "incidence", 0) * DEG2RAD * -1);
721     w->setTwist(attrf(a, "twist", 0) * DEG2RAD * -1);
722
723     // The 70% is a magic number that sorta kinda seems to match known
724     // throttle settings to approach speed.
725     w->setInducedDrag(0.7*attrf(a, "idrag", 1));
726
727     float effect = attrf(a, "effectiveness", 1);
728     w->setDragScale(w->getDragScale()*effect);
729
730     _currObj = w;
731     return w;
732 }
733
734 Rotor* FGFDM::parseRotor(XMLAttributes* a, const char* type)
735 {
736     Rotor* w = new Rotor();
737
738     // float defDihed = 0;
739
740     float pos[3];
741     pos[0] = attrf(a, "x");
742     pos[1] = attrf(a, "y");
743     pos[2] = attrf(a, "z");
744     w->setBase(pos);
745
746     float normal[3];
747     normal[0] = attrf(a, "nx");
748     normal[1] = attrf(a, "ny");
749     normal[2] = attrf(a, "nz");
750     w->setNormal(normal);
751
752     float forward[3];
753     forward[0] = attrf(a, "fx");
754     forward[1] = attrf(a, "fy");
755     forward[2] = attrf(a, "fz");
756     w->setForward(forward);
757
758     w->setMaxCyclicail(attrf(a, "maxcyclicail", 7.6));
759     w->setMaxCyclicele(attrf(a, "maxcyclicele", 4.94));
760     w->setMinCyclicail(attrf(a, "mincyclicail", -7.6));
761     w->setMinCyclicele(attrf(a, "mincyclicele", -4.94));
762     w->setMaxCollective(attrf(a, "maxcollective", 15.8));
763     w->setMinCollective(attrf(a, "mincollective", -0.2));
764     w->setDiameter(attrf(a, "diameter", 10.2));
765     w->setWeightPerBlade(attrf(a, "weightperblade", 44));
766     w->setNumberOfBlades(attrf(a, "numblades", 4));
767     w->setRelBladeCenter(attrf(a, "relbladecenter", 0.7));
768     w->setDynamic(attrf(a, "dynamic", 0.7));
769     w->setDelta3(attrf(a, "delta3", 0));
770     w->setDelta(attrf(a, "delta", 0));
771     w->setTranslift(attrf(a, "translift", 0.05));
772     w->setC2(attrf(a, "dragfactor", 1));
773     w->setStepspersecond(attrf(a, "stepspersecond", 120));
774     w->setPhiNull((attrf(a, "phi0", 0))*YASIM_PI/180);
775     w->setRPM(attrf(a, "rpm", 424));
776     w->setRelLenHinge(attrf(a, "rellenflaphinge", 0.07));
777     w->setAlpha0((attrf(a, "flap0", -5))*YASIM_PI/180);
778     w->setAlphamin((attrf(a, "flapmin", -15))/180*YASIM_PI);
779     w->setAlphamax((attrf(a, "flapmax",  15))*YASIM_PI/180);
780     w->setAlpha0factor(attrf(a, "flap0factor", 1));
781     w->setTeeterdamp(attrf(a,"teeterdamp",.0001));
782     w->setMaxteeterdamp(attrf(a,"maxteeterdamp",1000));
783     w->setRelLenTeeterHinge(attrf(a,"rellenteeterhinge",0.01));
784     w->setBalance(attrf(a,"balance",1.0));
785     w->setMinTiltYaw(attrf(a,"mintiltyaw",0.0));
786     w->setMinTiltPitch(attrf(a,"mintiltpitch",0.0));
787     w->setMinTiltRoll(attrf(a,"mintiltroll",0.0));
788     w->setMaxTiltYaw(attrf(a,"maxtiltyaw",0.0));
789     w->setMaxTiltPitch(attrf(a,"maxtiltpitch",0.0));
790     w->setMaxTiltRoll(attrf(a,"maxtiltroll",0.0));
791     w->setTiltCenterX(attrf(a,"tiltcenterx",0.0));
792     w->setTiltCenterY(attrf(a,"tiltcentery",0.0));
793     w->setTiltCenterZ(attrf(a,"tiltcenterz",0.0));
794     w->setDownwashFactor(attrf(a, "downwashfactor", 1));
795     if(attrb(a,"ccw"))
796        w->setCcw(1); 
797     if(attrb(a,"sharedflaphinge"))
798        w->setSharedFlapHinge(true); 
799
800     if(a->hasAttribute("name"))
801        w->setName(a->getValue("name") );
802     if(a->hasAttribute("alphaout0"))
803        w->setAlphaoutput(0,a->getValue("alphaout0") );
804     if(a->hasAttribute("alphaout1"))  w->setAlphaoutput(1,a->getValue("alphaout1") );
805     if(a->hasAttribute("alphaout2"))  w->setAlphaoutput(2,a->getValue("alphaout2") );
806     if(a->hasAttribute("alphaout3"))  w->setAlphaoutput(3,a->getValue("alphaout3") );
807     if(a->hasAttribute("coneout"))  w->setAlphaoutput(4,a->getValue("coneout") );
808     if(a->hasAttribute("yawout"))   w->setAlphaoutput(5,a->getValue("yawout") );
809     if(a->hasAttribute("rollout"))  w->setAlphaoutput(6,a->getValue("rollout") );
810
811     w->setPitchA(attrf(a, "pitch-a", 10));
812     w->setPitchB(attrf(a, "pitch-b", 10));
813     w->setForceAtPitchA(attrf(a, "forceatpitch-a", 3000));
814     w->setPowerAtPitch0(attrf(a, "poweratpitch-0", 300));
815     w->setPowerAtPitchB(attrf(a, "poweratpitch-b", 3000));
816     if(attrb(a,"notorque"))
817        w->setNotorque(1); 
818
819 #define p(x) if (a->hasAttribute(#x)) w->setParameter((char *)#x,attrf(a,#x) );
820 #define p2(x,y) if (a->hasAttribute(y)) w->setParameter((char *)#x,attrf(a,y) );
821     p2(translift_ve,"translift-ve")
822     p2(translift_maxfactor,"translift-maxfactor")
823     p2(ground_effect_constant,"ground-effect-constant")
824     p2(vortex_state_lift_factor,"vortex-state-lift-factor")
825     p2(vortex_state_c1,"vortex-state-c1")
826     p2(vortex_state_c2,"vortex-state-c2")
827     p2(vortex_state_c3,"vortex-state_c3")
828     p2(vortex_state_e1,"vortex-state-e1")
829     p2(vortex_state_e2,"vortex-state-e2")
830     p(twist)
831     p2(number_of_segments,"number-of-segments")
832     p2(number_of_parts,"number-of-parts")
833     p2(rel_len_where_incidence_is_measured,"rel-len-where-incidence-is-measured")
834     p(chord)
835     p(taper)
836     p2(airfoil_incidence_no_lift,"airfoil-incidence-no-lift")
837     p2(rel_len_blade_start,"rel-len-blade-start")
838     p2(incidence_stall_zero_speed,"incidence-stall-zero-speed")
839     p2(incidence_stall_half_sonic_speed,"incidence-stall-half-sonic-speed")
840     p2(lift_factor_stall,"lift-factor-stall")
841     p2(stall_change_over,"stall-change-over")
842     p2(drag_factor_stall,"drag-factor-stall")
843     p2(airfoil_lift_coefficient,"airfoil-lift-coefficient")
844     p2(airfoil_drag_coefficient0,"airfoil-drag-coefficient0")
845     p2(airfoil_drag_coefficient1,"airfoil-drag-coefficient1")
846     p2(cyclic_factor,"cyclic-factor")
847     p2(rotor_correction_factor,"rotor-correction-factor")
848 #undef p
849 #undef p2
850     _currObj = w;
851     return w;
852 }
853
854 void FGFDM::parsePistonEngine(XMLAttributes* a)
855 {
856     float engP = attrf(a, "eng-power") * HP2W;
857     float engS = attrf(a, "eng-rpm") * RPM2RAD;
858
859     PistonEngine* eng = new PistonEngine(engP, engS);
860
861     if(a->hasAttribute("displacement"))
862         eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
863
864     if(a->hasAttribute("compression"))
865         eng->setCompression(attrf(a, "compression"));        
866
867     if(a->hasAttribute("min-throttle"))
868         eng->setMinThrottle(attrf(a, "min-throttle"));
869
870     if(a->hasAttribute("turbo-mul")) {
871         float mul = attrf(a, "turbo-mul");
872         float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
873         eng->setTurboParams(mul, mp);
874         eng->setTurboLag(attrf(a, "turbo-lag", 2));
875     }
876
877     if(a->hasAttribute("supercharger"))
878         eng->setSupercharger(attrb(a, "supercharger"));
879
880     ((PropEngine*)_currObj)->setEngine(eng);
881 }
882
883 void FGFDM::parseTurbineEngine(XMLAttributes* a)
884 {
885     float power = attrf(a, "eng-power") * HP2W;
886     float omega = attrf(a, "eng-rpm") * RPM2RAD;
887     float alt = attrf(a, "alt") * FT2M;
888     float flatRating = attrf(a, "flat-rating") * HP2W;
889     TurbineEngine* eng = new TurbineEngine(power, omega, alt, flatRating);
890
891     if(a->hasAttribute("n2-low-idle"))
892         eng->setN2Range(attrf(a, "n2-low-idle"), attrf(a, "n2-high-idle"),
893                         attrf(a, "n2-max"));
894
895     // Nasty units conversion: lbs/hr per hp -> kg/s per watt
896     if(a->hasAttribute("bsfc"))
897         eng->setFuelConsumption(attrf(a, "bsfc") * (LBS2KG/(3600*HP2W)));
898
899     ((PropEngine*)_currObj)->setEngine(eng);
900 }
901
902 void FGFDM::parsePropeller(XMLAttributes* a)
903 {
904     // Legacy Handling for the old engines syntax:
905     PistonEngine* eng = 0;
906     if(a->hasAttribute("eng-power")) {
907         SG_LOG(SG_FLIGHT,SG_ALERT, "WARNING: "
908                << "Legacy engine definition in YASim configuration file.  "
909                << "Please fix.");
910         float engP = attrf(a, "eng-power") * HP2W;
911         float engS = attrf(a, "eng-rpm") * RPM2RAD;
912         eng = new PistonEngine(engP, engS);
913         if(a->hasAttribute("displacement"))
914             eng->setDisplacement(attrf(a, "displacement") * CIN2CM);
915         if(a->hasAttribute("compression"))
916             eng->setCompression(attrf(a, "compression"));        
917         if(a->hasAttribute("turbo-mul")) {
918             float mul = attrf(a, "turbo-mul");
919             float mp = attrf(a, "wastegate-mp", 1e6) * INHG2PA;
920             eng->setTurboParams(mul, mp);
921         }
922     }
923
924     // Now parse the actual propeller definition:
925     float cg[3];
926     cg[0] = attrf(a, "x");
927     cg[1] = attrf(a, "y");
928     cg[2] = attrf(a, "z");
929     float mass = attrf(a, "mass") * LBS2KG;
930     float moment = attrf(a, "moment");
931     float radius = attrf(a, "radius");
932     float speed = attrf(a, "cruise-speed") * KTS2MPS;
933     float omega = attrf(a, "cruise-rpm") * RPM2RAD;
934     float power = attrf(a, "cruise-power") * HP2W;
935     float rho = Atmosphere::getStdDensity(attrf(a, "cruise-alt") * FT2M);
936
937     Propeller* prop = new Propeller(radius, speed, omega, rho, power);
938     PropEngine* thruster = new PropEngine(prop, eng, moment);
939     _airplane.addThruster(thruster, mass, cg);
940
941     // Set the stops (fine = minimum pitch, coarse = maximum pitch)
942     float fine_stop = attrf(a, "fine-stop", 0.25f);
943     float coarse_stop = attrf(a, "coarse-stop", 4.0f);
944     prop->setStops(fine_stop, coarse_stop);
945
946     if(a->hasAttribute("takeoff-power")) {
947         float power0 = attrf(a, "takeoff-power") * HP2W;
948         float omega0 = attrf(a, "takeoff-rpm") * RPM2RAD;
949         prop->setTakeoff(omega0, power0);
950     }
951
952     if(a->hasAttribute("max-rpm")) {
953         float max = attrf(a, "max-rpm") * RPM2RAD;
954         float min = attrf(a, "min-rpm") * RPM2RAD;
955         thruster->setVariableProp(min, max);
956     }
957
958     if(attrb(a, "contra"))
959         thruster->setContraPair(true);
960
961     if(a->hasAttribute("manual-pitch")) {
962         prop->setManualPitch();
963     }
964
965     thruster->setGearRatio(attrf(a, "gear-ratio", 1));
966
967     char buf[64];
968     sprintf(buf, "/engines/engine[%d]", _nextEngine++);
969     EngRec* er = new EngRec();
970     er->eng = thruster;
971     er->prefix = dup(buf);
972     _thrusters.add(er);
973
974     _currObj = thruster;
975 }
976
977 // Turns a string axis name into an integer for use by the
978 // ControlMap.  Creates a new axis if this one hasn't been defined
979 // yet.
980 int FGFDM::parseAxis(const char* name)
981 {
982     for(int i=0; i<_axes.size(); i++) {
983         AxisRec* a = (AxisRec*)_axes.get(i);
984         if(eq(a->name, name))
985             return a->handle;
986     }
987
988     // Not there, make a new one.
989     AxisRec* a = new AxisRec();
990     a->name = dup(name);
991     fgGetNode( a->name, true ); // make sure the property name exists
992     a->handle = _airplane.getControlMap()->newInput();
993     _axes.add(a);
994     return a->handle;
995 }
996
997 int FGFDM::parseOutput(const char* name)
998 {
999     if(eq(name, "THROTTLE"))  return ControlMap::THROTTLE;
1000     if(eq(name, "MIXTURE"))   return ControlMap::MIXTURE;
1001     if(eq(name, "CONDLEVER")) return ControlMap::CONDLEVER;
1002     if(eq(name, "STARTER"))   return ControlMap::STARTER;
1003     if(eq(name, "MAGNETOS"))  return ControlMap::MAGNETOS;
1004     if(eq(name, "ADVANCE"))   return ControlMap::ADVANCE;
1005     if(eq(name, "REHEAT"))    return ControlMap::REHEAT;
1006     if(eq(name, "BOOST"))     return ControlMap::BOOST;
1007     if(eq(name, "VECTOR"))    return ControlMap::VECTOR;
1008     if(eq(name, "PROP"))      return ControlMap::PROP;
1009     if(eq(name, "BRAKE"))     return ControlMap::BRAKE;
1010     if(eq(name, "STEER"))     return ControlMap::STEER;
1011     if(eq(name, "EXTEND"))    return ControlMap::EXTEND;
1012     if(eq(name, "HEXTEND"))   return ControlMap::HEXTEND;
1013     if(eq(name, "LEXTEND"))   return ControlMap::LEXTEND;
1014         if(eq(name, "LACCEL"))    return ControlMap::LACCEL;
1015     if(eq(name, "INCIDENCE")) return ControlMap::INCIDENCE;
1016     if(eq(name, "FLAP0"))     return ControlMap::FLAP0;
1017     if(eq(name, "FLAP0EFFECTIVENESS"))   return ControlMap::FLAP0EFFECTIVENESS;
1018     if(eq(name, "FLAP1"))     return ControlMap::FLAP1;
1019     if(eq(name, "FLAP1EFFECTIVENESS"))   return ControlMap::FLAP1EFFECTIVENESS;
1020     if(eq(name, "SLAT"))      return ControlMap::SLAT;
1021     if(eq(name, "SPOILER"))   return ControlMap::SPOILER;
1022     if(eq(name, "CASTERING")) return ControlMap::CASTERING;
1023     if(eq(name, "PROPPITCH")) return ControlMap::PROPPITCH;
1024     if(eq(name, "PROPFEATHER")) return ControlMap::PROPFEATHER;
1025     if(eq(name, "COLLECTIVE")) return ControlMap::COLLECTIVE;
1026     if(eq(name, "CYCLICAIL")) return ControlMap::CYCLICAIL;
1027     if(eq(name, "CYCLICELE")) return ControlMap::CYCLICELE;
1028     if(eq(name, "TILTROLL")) return ControlMap::TILTROLL;
1029     if(eq(name, "TILTPITCH")) return ControlMap::TILTPITCH;
1030     if(eq(name, "TILTYAW")) return ControlMap::TILTYAW;
1031     if(eq(name, "ROTORGEARENGINEON")) return ControlMap::ROTORENGINEON;
1032     if(eq(name, "ROTORBRAKE")) return ControlMap::ROTORBRAKE;
1033     if(eq(name, "ROTORENGINEMAXRELTORQUE")) 
1034         return ControlMap::ROTORENGINEMAXRELTORQUE;
1035     if(eq(name, "ROTORRELTARGET")) return ControlMap::ROTORRELTARGET;
1036     if(eq(name, "ROTORBALANCE")) return ControlMap::ROTORBALANCE;
1037     if(eq(name, "REVERSE_THRUST")) return ControlMap::REVERSE_THRUST;
1038     if(eq(name, "WASTEGATE")) return ControlMap::WASTEGATE;
1039     if(eq(name, "WINCHRELSPEED")) return ControlMap::WINCHRELSPEED;
1040     if(eq(name, "HITCHOPEN")) return ControlMap::HITCHOPEN;
1041     if(eq(name, "PLACEWINCH")) return ControlMap::PLACEWINCH;
1042     if(eq(name, "FINDAITOW")) return ControlMap::FINDAITOW;
1043
1044     SG_LOG(SG_FLIGHT,SG_ALERT,"Unrecognized control type '"
1045            << name << "' in YASim aircraft description.");
1046     exit(1);
1047
1048 }
1049
1050 void FGFDM::parseWeight(XMLAttributes* a)
1051 {
1052     WeightRec* wr = new WeightRec();
1053
1054     float v[3];
1055     v[0] = attrf(a, "x");
1056     v[1] = attrf(a, "y");
1057     v[2] = attrf(a, "z");
1058
1059     wr->prop = dup(a->getValue("mass-prop"));
1060     wr->size = attrf(a, "size", 0);
1061     wr->handle = _airplane.addWeight(v, wr->size);
1062
1063     _weights.add(wr);
1064 }
1065
1066 bool FGFDM::eq(const char* a, const char* b)
1067 {
1068     // Figure it out for yourself. :)
1069     while(*a && *b && *a == *b) { a++; b++; }
1070     return !(*a || *b);
1071 }
1072
1073 char* FGFDM::dup(const char* s)
1074 {
1075     int len=0;
1076     while(s[len++]);
1077     char* s2 = new char[len+1];
1078     char* p = s2;
1079     while((*p++ = *s++));
1080     s2[len] = 0;
1081     return s2;
1082 }
1083
1084 int FGFDM::attri(XMLAttributes* atts, const char* attr)
1085 {
1086     if(!atts->hasAttribute(attr)) {
1087         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1088                "' in YASim aircraft description");
1089         exit(1);
1090     }
1091     return attri(atts, attr, 0);
1092 }
1093
1094 int FGFDM::attri(XMLAttributes* atts, const char* attr, int def)
1095 {
1096     const char* val = atts->getValue(attr);
1097     if(val == 0) return def;
1098     else         return atol(val);
1099 }
1100
1101 float FGFDM::attrf(XMLAttributes* atts, const char* attr)
1102 {
1103     if(!atts->hasAttribute(attr)) {
1104         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1105                "' in YASim aircraft description");
1106         exit(1);
1107     }
1108     return attrf(atts, attr, 0);
1109 }
1110
1111 float FGFDM::attrf(XMLAttributes* atts, const char* attr, float def)
1112 {
1113     const char* val = atts->getValue(attr);
1114     if(val == 0) return def;
1115     else         return (float)atof(val);    
1116 }
1117
1118 double FGFDM::attrd(XMLAttributes* atts, const char* attr)
1119 {
1120     if(!atts->hasAttribute(attr)) {
1121         SG_LOG(SG_FLIGHT,SG_ALERT,"Missing '" << attr <<
1122                "' in YASim aircraft description");
1123         exit(1);
1124     }
1125     return attrd(atts, attr, 0);
1126 }
1127
1128 double FGFDM::attrd(XMLAttributes* atts, const char* attr, double def)
1129 {
1130     const char* val = atts->getValue(attr);
1131     if(val == 0) return def;
1132     else         return atof(val);
1133 }
1134
1135 // ACK: the dreaded ambiguous string boolean.  Remind me to shoot Maik
1136 // when I have a chance. :).  Unless you have a parser that can check
1137 // symbol constants (we don't), this kind of coding is just a Bad
1138 // Idea.  This implementation, for example, silently returns a boolean
1139 // falsehood for values of "1", "yes", "True", and "TRUE".  Which is
1140 // especially annoying preexisting boolean attributes in the same
1141 // parser want to see "1" and will choke on a "true"...
1142 //
1143 // Unfortunately, this usage creeped into existing configuration files
1144 // while I wasn't active, and it's going to be hard to remove.  Issue
1145 // a warning to nag people into changing their ways for now...
1146 bool FGFDM::attrb(XMLAttributes* atts, const char* attr)
1147 {
1148     const char* val = atts->getValue(attr);
1149     if(val == 0) return false;
1150
1151     if(eq(val,"true")) {
1152         SG_LOG(SG_FLIGHT, SG_ALERT, "Warning: " <<
1153                "deprecated 'true' boolean in YASim configuration file.  " <<
1154                "Use numeric booleans (attribute=\"1\") instead");
1155         return true;
1156     }
1157     return attri(atts, attr, 0) ? true : false;
1158 }
1159
1160 }; // namespace yasim