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