]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.cxx
Merge branch 'releases/2.2.0' into next
[flightgear.git] / src / AIModel / AIBase.cxx
1 // FGAIBase - abstract base class for AI objects
2 // Written by David Culp, started Nov 2003, based on
3 // David Luff's FGAIEntity class.
4 // - davidculp2@comcast.net
5 //
6 // With additions by Mathias Froehlich & Vivian Meazza 2004 -2007
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <string>
30
31 #include <osg/ref_ptr>
32 #include <osg/Node>
33 #include <osgDB/FileUtils>
34
35 #include <simgear/math/SGMath.hxx>
36 #include <simgear/misc/sg_path.hxx>
37 #include <simgear/scene/model/modellib.hxx>
38 #include <simgear/scene/util/SGNodeMasks.hxx>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/props/props.hxx>
41
42 #include <Main/globals.hxx>
43 #include <Scenery/scenery.hxx>
44 #include <Scripting/NasalSys.hxx>
45
46 #include "AIBase.hxx"
47 #include "AIManager.hxx"
48
49 const char *default_model = "Models/Geometry/glider.ac";
50 const double FGAIBase::e = 2.71828183;
51 const double FGAIBase::lbs_to_slugs = 0.031080950172;   //conversion factor
52
53 using namespace simgear;
54
55 FGAIBase::FGAIBase(object_type ot) :
56     _max_speed(300),
57     _name(""),
58     _parent(""),
59     props( NULL ),
60     model_removed( fgGetNode("/ai/models/model-removed", true) ),
61     manager( NULL ),
62     _installed(false),
63     fp( NULL ),
64     _impact_lat(0),
65     _impact_lon(0),
66     _impact_elev(0),
67     _impact_hdg(0),
68     _impact_pitch(0),
69     _impact_roll(0),
70     _impact_speed(0),
71     _refID( _newAIModelID() ),
72     _otype(ot),
73     _initialized(false)
74
75 {
76     tgt_heading = hdg = tgt_altitude_ft = tgt_speed = 0.0;
77     tgt_roll = roll = tgt_pitch = tgt_yaw = tgt_vs = vs = pitch = 0.0;
78     bearing = elevation = range = rdot = 0.0;
79     x_shift = y_shift = rotation = 0.0;
80     in_range = false;
81     invisible = false;
82     no_roll = true;
83     life = 900;
84     delete_me = false;
85     _impact_reported = false;
86     _collision_reported = false;
87     _expiry_reported = false;
88
89     _subID = 0;
90
91     _x_offset = 0;
92     _y_offset = 0;
93     _z_offset = 0;
94
95     _pitch_offset = 0;
96     _roll_offset = 0;
97     _yaw_offset = 0;
98
99     userpos = SGGeod::fromDeg(0, 0);
100
101     pos = SGGeod::fromDeg(0, 0);
102     speed = 0;
103     altitude_ft = 0;
104     speed_north_deg_sec = 0;
105     speed_east_deg_sec = 0;
106     turn_radius_ft = 0;
107
108     ft_per_deg_lon = 0;
109     ft_per_deg_lat = 0;
110
111     horiz_offset = 0;
112     vert_offset = 0;
113     ht_diff = 0;
114
115     serviceable = false;
116
117     fp = 0;
118
119     rho = 1;
120     T = 280;
121     p = 1e5;
122     a = 340;
123     Mach = 0;
124 }
125
126 FGAIBase::~FGAIBase() {
127     // Unregister that one at the scenery manager
128     if (globals->get_scenery()) {
129         globals->get_scenery()->get_scene_graph()->removeChild(aip.getSceneGraph());
130     }
131
132     if (props) {
133         SGPropertyNode* parent = props->getParent();
134
135         if (parent)
136             model_removed->setStringValue(props->getPath());
137     }
138     delete fp;
139     fp = 0;
140 }
141
142 void FGAIBase::readFromScenario(SGPropertyNode* scFileNode)
143 {
144     if (!scFileNode)
145         return;
146
147     setPath(scFileNode->getStringValue("model",
148             fgGetString("/sim/multiplay/default-model", default_model)));
149
150     setHeading(scFileNode->getDoubleValue("heading", 0.0));
151     setSpeed(scFileNode->getDoubleValue("speed", 0.0));
152     setAltitude(scFileNode->getDoubleValue("altitude", 0.0));
153     setLongitude(scFileNode->getDoubleValue("longitude", 0.0));
154     setLatitude(scFileNode->getDoubleValue("latitude", 0.0));
155     setBank(scFileNode->getDoubleValue("roll", 0.0));
156
157     SGPropertyNode* submodels = scFileNode->getChild("submodels");
158
159     if (submodels) {
160         setServiceable(submodels->getBoolValue("serviceable", false));
161         setSMPath(submodels->getStringValue("path", ""));
162     }
163
164 }
165
166 void FGAIBase::update(double dt) {
167
168     if (_otype == otStatic)
169         return;
170
171     if (_otype == otBallistic)
172         CalculateMach();
173
174     ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.getLatitudeRad());
175     ft_per_deg_lon = 365228.16 * cos(pos.getLatitudeRad());
176 }
177
178 void FGAIBase::Transform() {
179
180     if (!invisible) {
181         aip.setVisible(true);
182         aip.setPosition(pos);
183
184         if (no_roll)
185             aip.setOrientation(0.0, pitch, hdg);
186         else
187             aip.setOrientation(roll, pitch, hdg);
188
189         aip.update();
190     } else {
191         aip.setVisible(false);
192         aip.update();
193     }
194
195 }
196
197 bool FGAIBase::init(bool search_in_AI_path) {
198     
199     string f;
200     if(search_in_AI_path)
201     {
202     // setup a modified Options structure, with only the $fg-root/AI defined;
203     // we'll check that first, then give the normal search logic a chance.
204     // this ensures that models in AI/ are preferred to normal models, where
205     // both exist.
206         osg::ref_ptr<osgDB::ReaderWriter::Options> 
207           opt(osg::clone(osgDB::Registry::instance()->getOptions(), osg::CopyOp::SHALLOW_COPY));
208
209         SGPath ai_path(globals->get_fg_root(), "AI");
210         opt->setDatabasePath(ai_path.str());
211         
212         f = osgDB::findDataFile(model_path, opt.get());
213     }
214
215     if (f.empty()) {
216       f = simgear::SGModelLib::findDataFile(model_path);
217     }
218     
219     if(f.empty())
220         f = fgGetString("/sim/multiplay/default-model", default_model);
221     else
222         _installed = true;
223
224     model = load3DModel(f, props);
225
226     if (model.valid() && _initialized == false) {
227         aip.init( model.get() );
228         aip.setVisible(true);
229         invisible = false;
230         globals->get_scenery()->get_scene_graph()->addChild(aip.getSceneGraph());
231         _initialized = true;
232
233     } else if (!model_path.empty()) {
234         SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load model " << model_path);
235         // not properly installed...
236         _installed = false;
237     }
238
239     setDie(false);
240     return true;
241 }
242
243 void FGAIBase::initModel(osg::Node *node)
244 {
245     if (model.valid()) { 
246
247         if( _path != ""){
248             props->setStringValue("submodels/path", _path.c_str());
249             SG_LOG(SG_INPUT, SG_DEBUG, "AIBase: submodels/path " << _path);
250         }
251
252         if( _parent!= ""){
253             props->setStringValue("parent-name", _parent.c_str());
254         }
255
256         fgSetString("/ai/models/model-added", props->getPath().c_str());
257     } else if (!model_path.empty()) {
258         SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load model " << model_path);
259     }
260
261     setDie(false);
262 }
263
264
265 osg::Node* FGAIBase::load3DModel(const string &path, SGPropertyNode *prop_root)
266 {
267   model = SGModelLib::loadPagedModel(path, prop_root, new FGNasalModelData(prop_root));
268   initModel(model.get());
269   return model.get();
270 }
271
272 bool FGAIBase::isa( object_type otype ) {
273     return otype == _otype;
274 }
275
276
277 void FGAIBase::bind() {
278     props->tie("id", SGRawValueMethods<FGAIBase,int>(*this,
279         &FGAIBase::getID));
280     props->tie("velocities/true-airspeed-kt",  SGRawValuePointer<double>(&speed));
281     props->tie("velocities/vertical-speed-fps",
282         SGRawValueMethods<FGAIBase,double>(*this,
283         &FGAIBase::_getVS_fps,
284         &FGAIBase::_setVS_fps));
285
286     props->tie("position/altitude-ft",
287         SGRawValueMethods<FGAIBase,double>(*this,
288         &FGAIBase::_getAltitude,
289         &FGAIBase::_setAltitude));
290     props->tie("position/latitude-deg",
291         SGRawValueMethods<FGAIBase,double>(*this,
292         &FGAIBase::_getLatitude,
293         &FGAIBase::_setLatitude));
294     props->tie("position/longitude-deg",
295         SGRawValueMethods<FGAIBase,double>(*this,
296         &FGAIBase::_getLongitude,
297         &FGAIBase::_setLongitude));
298
299     props->tie("position/global-x",
300         SGRawValueMethods<FGAIBase,double>(*this,
301         &FGAIBase::_getCartPosX,
302         0));
303     props->tie("position/global-y",
304         SGRawValueMethods<FGAIBase,double>(*this,
305         &FGAIBase::_getCartPosY,
306         0));
307     props->tie("position/global-z",
308         SGRawValueMethods<FGAIBase,double>(*this,
309         &FGAIBase::_getCartPosZ,
310         0));
311     props->tie("callsign",
312         SGRawValueMethods<FGAIBase,const char*>(*this,
313         &FGAIBase::_getCallsign,
314         0));
315
316     props->tie("orientation/pitch-deg",   SGRawValuePointer<double>(&pitch));
317     props->tie("orientation/roll-deg",    SGRawValuePointer<double>(&roll));
318     props->tie("orientation/true-heading-deg", SGRawValuePointer<double>(&hdg));
319
320     props->tie("radar/in-range", SGRawValuePointer<bool>(&in_range));
321     props->tie("radar/bearing-deg",   SGRawValuePointer<double>(&bearing));
322     props->tie("radar/elevation-deg", SGRawValuePointer<double>(&elevation));
323     props->tie("radar/range-nm", SGRawValuePointer<double>(&range));
324     props->tie("radar/h-offset", SGRawValuePointer<double>(&horiz_offset));
325     props->tie("radar/v-offset", SGRawValuePointer<double>(&vert_offset));
326     props->tie("radar/x-shift", SGRawValuePointer<double>(&x_shift));
327     props->tie("radar/y-shift", SGRawValuePointer<double>(&y_shift));
328     props->tie("radar/rotation", SGRawValuePointer<double>(&rotation));
329     props->tie("radar/ht-diff-ft", SGRawValuePointer<double>(&ht_diff));
330     props->tie("subID", SGRawValuePointer<int>(&_subID));
331     props->tie("controls/lighting/nav-lights",
332         SGRawValueFunctions<bool>(_isNight));
333     props->setBoolValue("controls/lighting/beacon", true);
334     props->setBoolValue("controls/lighting/strobe", true);
335     props->setBoolValue("controls/glide-path", true);
336
337     props->setStringValue("controls/flight/lateral-mode", "roll");
338     props->setDoubleValue("controls/flight/target-hdg", hdg);
339     props->setDoubleValue("controls/flight/target-roll", roll);
340
341     props->setStringValue("controls/flight/longitude-mode", "alt");
342     props->setDoubleValue("controls/flight/target-alt", altitude_ft);
343     props->setDoubleValue("controls/flight/target-pitch", pitch);
344
345    props->setDoubleValue("controls/flight/target-spd", speed);
346
347 }
348
349 void FGAIBase::unbind() {
350     props->untie("id");
351     props->untie("velocities/true-airspeed-kt");
352     props->untie("velocities/vertical-speed-fps");
353
354     props->untie("position/altitude-ft");
355     props->untie("position/latitude-deg");
356     props->untie("position/longitude-deg");
357     props->untie("position/global-x");
358     props->untie("position/global-y");
359     props->untie("position/global-z");
360     props->untie("callsign");
361
362     props->untie("orientation/pitch-deg");
363     props->untie("orientation/roll-deg");
364     props->untie("orientation/true-heading-deg");
365
366     props->untie("radar/in-range");
367     props->untie("radar/bearing-deg");
368     props->untie("radar/elevation-deg");
369     props->untie("radar/range-nm");
370     props->untie("radar/h-offset");
371     props->untie("radar/v-offset");
372     props->untie("radar/x-shift");
373     props->untie("radar/y-shift");
374     props->untie("radar/rotation");
375     props->untie("radar/ht-diff-ft");
376
377     props->untie("controls/lighting/nav-lights");
378
379     props->setBoolValue("/sim/controls/radar/", true);
380
381 }
382
383 double FGAIBase::UpdateRadar(FGAIManager* manager) {
384     bool control = fgGetBool("/sim/controls/radar", true);
385
386     if(!control) return 0;
387
388     double radar_range_ft2 = fgGetDouble("/instrumentation/radar/range");
389     bool force_on = fgGetBool("/instrumentation/radar/debug-mode", false);
390     radar_range_ft2 *= SG_NM_TO_METER * SG_METER_TO_FEET * 1.1; // + 10%
391     radar_range_ft2 *= radar_range_ft2;
392
393     double user_latitude  = manager->get_user_latitude();
394     double user_longitude = manager->get_user_longitude();
395     double lat_range = fabs(pos.getLatitudeDeg() - user_latitude) * ft_per_deg_lat;
396     double lon_range = fabs(pos.getLongitudeDeg() - user_longitude) * ft_per_deg_lon;
397     double range_ft2 = lat_range*lat_range + lon_range*lon_range;
398
399     //
400     // Test whether the target is within radar range.
401     //
402     in_range = (range_ft2 && (range_ft2 <= radar_range_ft2));
403
404     if ( in_range || force_on ) {
405         props->setBoolValue("radar/in-range", true);
406
407         // copy values from the AIManager
408         double user_altitude  = manager->get_user_altitude();
409         double user_heading   = manager->get_user_heading();
410         double user_pitch     = manager->get_user_pitch();
411         //double user_yaw       = manager->get_user_yaw();
412         //double user_speed     = manager->get_user_speed();
413
414         // calculate range to target in feet and nautical miles
415         double range_ft = sqrt( range_ft2 );
416         range = range_ft / 6076.11549;
417
418         // calculate bearing to target
419         if (pos.getLatitudeDeg() >= user_latitude) {
420             bearing = atan2(lat_range, lon_range) * SG_RADIANS_TO_DEGREES;
421             if (pos.getLongitudeDeg() >= user_longitude) {
422                 bearing = 90.0 - bearing;
423             } else {
424                 bearing = 270.0 + bearing;
425             }
426         } else {
427             bearing = atan2(lon_range, lat_range) * SG_RADIANS_TO_DEGREES;
428             if (pos.getLongitudeDeg() >= user_longitude) {
429                 bearing = 180.0 - bearing;
430             } else {
431                 bearing = 180.0 + bearing;
432             }
433         }
434
435         // This is an alternate way to compute bearing and distance which
436         // agrees with the original scheme within about 0.1 degrees.
437         //
438         // Point3D start( user_longitude * SGD_DEGREES_TO_RADIANS,
439         //                user_latitude * SGD_DEGREES_TO_RADIANS, 0 );
440         // Point3D dest( pos.getLongitudeRad(), pos.getLatitudeRad(), 0 );
441         // double gc_bearing, gc_range;
442         // calc_gc_course_dist( start, dest, &gc_bearing, &gc_range );
443         // gc_range *= SG_METER_TO_NM;
444         // gc_bearing *= SGD_RADIANS_TO_DEGREES;
445         // printf("orig b = %.3f %.2f  gc b= %.3f, %.2f\n",
446         //        bearing, range, gc_bearing, gc_range);
447
448         // calculate look left/right to target, without yaw correction
449         horiz_offset = bearing - user_heading;
450         if (horiz_offset > 180.0) horiz_offset -= 360.0;
451         if (horiz_offset < -180.0) horiz_offset += 360.0;
452
453         // calculate elevation to target
454         elevation = atan2( altitude_ft - user_altitude, range_ft ) * SG_RADIANS_TO_DEGREES;
455
456         // calculate look up/down to target
457         vert_offset = elevation - user_pitch;
458
459         /* this calculation needs to be fixed, but it isn't important anyway
460         // calculate range rate
461         double recip_bearing = bearing + 180.0;
462         if (recip_bearing > 360.0) recip_bearing -= 360.0;
463         double my_horiz_offset = recip_bearing - hdg;
464         if (my_horiz_offset > 180.0) my_horiz_offset -= 360.0;
465         if (my_horiz_offset < -180.0) my_horiz_offset += 360.0;
466         rdot = (-user_speed * cos( horiz_offset * SG_DEGREES_TO_RADIANS ))
467         +(-speed * 1.686 * cos( my_horiz_offset * SG_DEGREES_TO_RADIANS ));
468         */
469
470         // now correct look left/right for yaw
471         // horiz_offset += user_yaw; // FIXME: WHY WOULD WE WANT TO ADD IN SIDE-SLIP HERE?
472
473         // calculate values for radar display
474         y_shift = range * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
475         x_shift = range * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
476         rotation = hdg - user_heading;
477         if (rotation < 0.0) rotation += 360.0;
478         ht_diff = altitude_ft - user_altitude;
479
480     }
481
482     return range_ft2;
483 }
484
485 /*
486 * Getters and Setters
487 */
488
489 SGVec3d FGAIBase::getCartPosAt(const SGVec3d& _off) const {
490     // Transform that one to the horizontal local coordinate system.
491     SGQuatd hlTrans = SGQuatd::fromLonLat(pos);
492
493     // and postrotate the orientation of the AIModel wrt the horizontal
494     // local frame
495     hlTrans *= SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
496
497     // The offset converted to the usual body fixed coordinate system
498     // rotated to the earth fixed coordinates axis
499     SGVec3d off = hlTrans.backTransform(_off);
500
501     // Add the position offset of the AIModel to gain the earth centered position
502     SGVec3d cartPos = SGVec3d::fromGeod(pos);
503
504     return cartPos + off;
505 }
506
507 SGVec3d FGAIBase::getCartPos() const {
508     SGVec3d cartPos = SGVec3d::fromGeod(pos);
509     return cartPos;
510 }
511
512 bool FGAIBase::getGroundElevationM(const SGGeod& pos, double& elev,
513                                    const SGMaterial** material) const {
514     return globals->get_scenery()->get_elevation_m(pos, elev, material,
515                                                    model.get());
516 }
517
518 double FGAIBase::_getCartPosX() const {
519     SGVec3d cartPos = getCartPos();
520     return cartPos.x();
521 }
522
523 double FGAIBase::_getCartPosY() const {
524     SGVec3d cartPos = getCartPos();
525     return cartPos.y();
526 }
527
528 double FGAIBase::_getCartPosZ() const {
529     SGVec3d cartPos = getCartPos();
530     return cartPos.z();
531 }
532
533 void FGAIBase::_setLongitude( double longitude ) {
534     pos.setLongitudeDeg(longitude);
535 }
536
537 void FGAIBase::_setLatitude ( double latitude )  {
538     pos.setLatitudeDeg(latitude);
539 }
540
541 void FGAIBase::_setUserPos(){
542     userpos.setLatitudeDeg(manager->get_user_latitude());
543     userpos.setLongitudeDeg(manager->get_user_longitude());
544     userpos.setElevationM(manager->get_user_altitude() * SG_FEET_TO_METER);
545 }
546
547 void FGAIBase::_setSubID( int s ) {
548     _subID = s;
549 }
550
551 bool FGAIBase::setParentNode() {
552
553     if (_parent == ""){
554        SG_LOG(SG_GENERAL, SG_ALERT, "AIBase: " << _name
555             << " parent not set ");
556        return false;
557     }
558
559     const SGPropertyNode_ptr ai = fgGetNode("/ai/models", true);
560
561     for (int i = ai->nChildren() - 1; i >= -1; i--) {
562         SGPropertyNode_ptr model;
563
564         if (i < 0) { // last iteration: selected model
565             model = _selected_ac;
566         } else {
567             model = ai->getChild(i);
568             string path = ai->getPath();
569             const string name = model->getStringValue("name");
570
571             if (!model->nChildren()){
572                 continue;
573             }
574             if (name == _parent) {
575                 _selected_ac = model;  // save selected model for last iteration
576                 break;
577             }
578
579         }
580         if (!model)
581             continue;
582
583     }// end for loop
584
585     if (_selected_ac != 0){
586         const string name = _selected_ac->getStringValue("name");
587         return true;
588     } else {
589         SG_LOG(SG_GENERAL, SG_ALERT, "AIBase: " << _name
590             << " parent not found: dying ");
591         setDie(true);
592         return false;
593     }
594
595 }
596
597 double FGAIBase::_getLongitude() const {
598     return pos.getLongitudeDeg();
599 }
600
601 double FGAIBase::_getLatitude() const {
602     return pos.getLatitudeDeg();
603 }
604
605 double FGAIBase::_getElevationFt() const {
606     return pos.getElevationFt();
607 }
608
609 double FGAIBase::_getRdot() const {
610     return rdot;
611 }
612
613 double FGAIBase::_getVS_fps() const {
614     return vs/60.0;
615 }
616
617 double FGAIBase::_get_speed_east_fps() const {
618     return speed_east_deg_sec * ft_per_deg_lon;
619 }
620
621 double FGAIBase::_get_speed_north_fps() const {
622     return speed_north_deg_sec * ft_per_deg_lat;
623 }
624
625 void FGAIBase::_setVS_fps( double _vs ) {
626     vs = _vs*60.0;
627 }
628
629 double FGAIBase::_getAltitude() const {
630     return altitude_ft;
631 }
632
633 double FGAIBase::_getAltitudeAGL(SGGeod inpos, double start){
634     getGroundElevationM(SGGeod::fromGeodM(inpos, start),
635         _elevation_m, &_material);
636     return inpos.getElevationFt() - _elevation_m * SG_METER_TO_FEET;
637 }
638
639 bool FGAIBase::_getServiceable() const {
640     return serviceable;
641 }
642
643 SGPropertyNode* FGAIBase::_getProps() const {
644     return props;
645 }
646
647 void FGAIBase::_setAltitude( double _alt ) {
648     setAltitude( _alt );
649 }
650
651 bool FGAIBase::_isNight() {
652     return (fgGetFloat("/sim/time/sun-angle-rad") > 1.57);
653 }
654
655 bool FGAIBase::_getCollisionData() {
656     return _collision_reported;
657 }
658
659 bool FGAIBase::_getExpiryData() {
660     return _expiry_reported;
661 }
662
663 bool FGAIBase::_getImpactData() {
664     return _impact_reported;
665 }
666
667 double FGAIBase::_getImpactLat() const {
668     return _impact_lat;
669 }
670
671 double FGAIBase::_getImpactLon() const {
672     return _impact_lon;
673 }
674
675 double FGAIBase::_getImpactElevFt() const {
676     return _impact_elev * SG_METER_TO_FEET;
677 }
678
679 double FGAIBase::_getImpactPitch() const {
680     return _impact_pitch;
681 }
682
683 double FGAIBase::_getImpactRoll() const {
684     return _impact_roll;
685 }
686
687 double FGAIBase::_getImpactHdg() const {
688     return _impact_hdg;
689 }
690
691 double FGAIBase::_getImpactSpeed() const {
692     return _impact_speed;
693 }
694
695 int FGAIBase::getID() const {
696     return  _refID;
697 }
698
699 int FGAIBase::_getSubID() const {
700     return  _subID;
701 }
702
703 double FGAIBase::_getSpeed() const {
704     return speed;
705 }
706
707 double FGAIBase::_getRoll() const {
708     return roll;
709 }
710
711 double FGAIBase::_getPitch() const {
712     return pitch;
713 }
714
715 double FGAIBase::_getHeading() const {
716     return hdg;
717 }
718
719 double  FGAIBase::_getXOffset() const {
720     return _x_offset;
721 }
722
723 double  FGAIBase::_getYOffset() const {
724     return _y_offset;
725 }
726
727 double  FGAIBase::_getZOffset() const {
728     return _z_offset;
729 }
730
731 const char* FGAIBase::_getPath() const {
732     return model_path.c_str();
733 }
734
735 const char* FGAIBase::_getSMPath() const {
736     return _path.c_str();
737 }
738
739 const char* FGAIBase::_getName() const {
740     return _name.c_str();
741 }
742
743 const char* FGAIBase::_getCallsign() const {
744     return _callsign.c_str();
745 }
746
747 const char* FGAIBase::_getSubmodel() const {
748     return _submodel.c_str();
749 }
750
751 void FGAIBase::CalculateMach() {
752     // Calculate rho at altitude, using standard atmosphere
753     // For the temperature T and the pressure p,
754     double altitude = altitude_ft;
755
756     if (altitude < 36152) {             // curve fits for the troposphere
757         T = 59 - 0.00356 * altitude;
758         p = 2116 * pow( ((T + 459.7) / 518.6) , 5.256);
759     } else if ( 36152 < altitude && altitude < 82345 ) {    // lower stratosphere
760         T = -70;
761         p = 473.1 * pow( e , 1.73 - (0.000048 * altitude) );
762     } else {                                    //  upper stratosphere
763         T = -205.05 + (0.00164 * altitude);
764         p = 51.97 * pow( ((T + 459.7) / 389.98) , -11.388);
765     }
766
767     rho = p / (1718 * (T + 459.7));
768
769     // calculate the speed of sound at altitude
770     // a = sqrt ( g * R * (T + 459.7))
771     // where:
772     // a = speed of sound [ft/s]
773     // g = specific heat ratio, which is usually equal to 1.4
774     // R = specific gas constant, which equals 1716 ft-lb/slug/R
775     a = sqrt ( 1.4 * 1716 * (T + 459.7));
776
777     // calculate Mach number
778     Mach = speed/a;
779
780     // cout  << "Speed(ft/s) "<< speed <<" Altitude(ft) "<< altitude << " Mach " << Mach << endl;
781 }
782
783 int FGAIBase::_newAIModelID() {
784     static int id = 0;
785
786     if (!++id)
787         id++;   // id = 0 is not allowed.
788
789     return id;
790 }
791