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