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