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