]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.cxx
Don't duplicate userpos in in AIObjects.
[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 <string.h>
28
29 #include <simgear/compiler.h>
30
31 #include <string>
32
33 #include <osg/ref_ptr>
34 #include <osg/Node>
35 #include <osgDB/FileUtils>
36
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/scene/model/modellib.hxx>
39 #include <simgear/scene/util/SGNodeMasks.hxx>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/props/props.hxx>
42
43 #include <Main/globals.hxx>
44 #include <Scenery/scenery.hxx>
45 #include <Scripting/NasalSys.hxx>
46 #include <Scripting/NasalModelData.hxx>
47 #include <Sound/fg_fx.hxx>
48
49 #include "AIBase.hxx"
50 #include "AIManager.hxx"
51
52 const char *default_model = "Models/Geometry/glider.ac";
53 const double FGAIBase::e = 2.71828183;
54 const double FGAIBase::lbs_to_slugs = 0.031080950172;   //conversion factor
55
56 using namespace simgear;
57
58 FGAIBase::FGAIBase(object_type ot, bool enableHot) :
59     _max_speed(300),
60     _name(""),
61     _parent(""),
62     props( NULL ),
63     model_removed( fgGetNode("/ai/models/model-removed", true) ),
64     manager( NULL ),
65     _installed(false),
66     fp( NULL ),
67     _impact_lat(0),
68     _impact_lon(0),
69     _impact_elev(0),
70     _impact_hdg(0),
71     _impact_pitch(0),
72     _impact_roll(0),
73     _impact_speed(0),
74     _refID( _newAIModelID() ),
75     _otype(ot),
76     _initialized(false),
77     _modeldata(0),
78     _fx(0)
79 {
80     tgt_heading = hdg = tgt_altitude_ft = tgt_speed = 0.0;
81     tgt_roll = roll = tgt_pitch = tgt_yaw = tgt_vs = vs = pitch = 0.0;
82     bearing = elevation = range = rdot = 0.0;
83     x_shift = y_shift = rotation = 0.0;
84     in_range = false;
85     invisible = false;
86     no_roll = true;
87     life = 900;
88     delete_me = false;
89     _impact_reported = false;
90     _collision_reported = false;
91     _expiry_reported = false;
92
93     _subID = 0;
94
95     _x_offset = 0;
96     _y_offset = 0;
97     _z_offset = 0;
98
99     _pitch_offset = 0;
100     _roll_offset = 0;
101     _yaw_offset = 0;
102
103     pos = SGGeod::fromDeg(0, 0);
104     speed = 0;
105     altitude_ft = 0;
106     speed_north_deg_sec = 0;
107     speed_east_deg_sec = 0;
108     turn_radius_ft = 0;
109
110     ft_per_deg_lon = 0;
111     ft_per_deg_lat = 0;
112
113     horiz_offset = 0;
114     vert_offset = 0;
115     ht_diff = 0;
116
117     serviceable = false;
118
119     fp = 0;
120
121     rho = 1;
122     T = 280;
123     p = 1e5;
124     a = 340;
125     Mach = 0;
126
127     // explicitly disable HOT for (most) AI models
128     if (!enableHot)
129         aip.getSceneGraph()->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
130 }
131
132 FGAIBase::~FGAIBase() {
133     // Unregister that one at the scenery manager
134     removeModel();
135
136     if (props) {
137         SGPropertyNode* parent = props->getParent();
138
139         if (parent)
140             model_removed->setStringValue(props->getPath());
141     }
142
143     removeSoundFx();
144
145     if (fp)
146         delete fp;
147     fp = 0;
148 }
149
150 /** Cleanly remove the model
151  * and let the scenery database pager do the clean-up work.
152  */
153 void
154 FGAIBase::removeModel()
155 {
156     if (!_model.valid())
157         return;
158
159     FGScenery* pSceneryManager = globals->get_scenery();
160     if (pSceneryManager)
161     {
162         osg::ref_ptr<osg::Object> temp = _model.get();
163         pSceneryManager->get_scene_graph()->removeChild(aip.getSceneGraph());
164         // withdraw from SGModelPlacement and drop own reference (unref)
165         aip.init( 0 );
166         _model = 0;
167         // pass it on to the pager, to be be deleted in the pager thread
168         pSceneryManager->getPager()->queueDeleteRequest(temp);
169     }
170     else
171     {
172         SG_LOG(SG_AI, SG_ALERT, "AIBase: Could not unload model. Missing scenery manager!");
173     }
174 }
175
176 void FGAIBase::readFromScenario(SGPropertyNode* scFileNode)
177 {
178     if (!scFileNode)
179         return;
180
181     setPath(scFileNode->getStringValue("model",
182             fgGetString("/sim/multiplay/default-model", default_model)));
183
184     setHeading(scFileNode->getDoubleValue("heading", 0.0));
185     setSpeed(scFileNode->getDoubleValue("speed", 0.0));
186     setAltitude(scFileNode->getDoubleValue("altitude", 0.0));
187     setLongitude(scFileNode->getDoubleValue("longitude", 0.0));
188     setLatitude(scFileNode->getDoubleValue("latitude", 0.0));
189     setBank(scFileNode->getDoubleValue("roll", 0.0));
190
191     SGPropertyNode* submodels = scFileNode->getChild("submodels");
192
193     if (submodels) {
194         setServiceable(submodels->getBoolValue("serviceable", false));
195         setSMPath(submodels->getStringValue("path", ""));
196     }
197
198 }
199
200 void FGAIBase::update(double dt) {
201
202     if (_otype == otStatic)
203         return;
204
205     if (_otype == otBallistic)
206         CalculateMach();
207
208     ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.getLatitudeRad());
209     ft_per_deg_lon = 365228.16 * cos(pos.getLatitudeRad());
210
211     if ( _fx )
212     {
213         // update model's audio sample values
214         _fx->set_position_geod( pos );
215
216         SGQuatd orient = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
217         _fx->set_orientation( orient );
218
219         SGVec3d velocity;
220         velocity = SGVec3d( speed_north_deg_sec, speed_east_deg_sec,
221                             pitch*speed );
222         _fx->set_velocity( velocity );
223     }
224     else if ((_modeldata)&&(_modeldata->needInitilization()))
225     {
226         // process deferred nasal initialization,
227         // which must be done in main thread
228         _modeldata->init();
229
230         // sound initialization
231         if (fgGetBool("/sim/sound/aimodels/enabled",false))
232         {
233             const string& fxpath = _modeldata->get_sound_path();
234             if (fxpath != "")
235             {
236                 props->setStringValue("sim/sound/path", fxpath.c_str());
237
238                 // initialize the sound configuration
239                 std::stringstream name;
240                 name <<  "aifx:";
241                 name << _refID;
242                 _fx = new FGFX(name.str(), props);
243                 _fx->init();
244             }
245         }
246     }
247 }
248
249 /** update LOD properties of the model */
250 void FGAIBase::updateLOD()
251 {
252     double maxRangeDetail = fgGetDouble("/sim/rendering/static-lod/ai-detailed", 10000.0);
253     double maxRangeBare   = fgGetDouble("/sim/rendering/static-lod/ai-bare", 20000.0);
254     if (_model.valid())
255     {
256         if( maxRangeDetail == 0.0 )
257         {
258             // disable LOD
259             _model->setRange(0, 0.0,     FLT_MAX);
260             _model->setRange(1, FLT_MAX, FLT_MAX);
261         }
262         else
263         {
264             _model->setRange(0, 0.0, maxRangeDetail);
265             _model->setRange(1, maxRangeDetail,maxRangeBare);
266         }
267     }
268 }
269
270 void FGAIBase::Transform() {
271
272     if (!invisible) {
273         aip.setVisible(true);
274         aip.setPosition(pos);
275
276         if (no_roll)
277             aip.setOrientation(0.0, pitch, hdg);
278         else
279             aip.setOrientation(roll, pitch, hdg);
280
281         aip.update();
282     } else {
283         aip.setVisible(false);
284         aip.update();
285     }
286
287 }
288
289 bool FGAIBase::init(bool search_in_AI_path)
290 {
291     if (_model.valid())
292     {
293         SG_LOG(SG_AI, SG_ALERT, "AIBase: Cannot initialize a model multiple times! " << model_path);
294         return false;
295     }
296
297     string f;
298     if(search_in_AI_path)
299     {
300     // setup a modified Options structure, with only the $fg-root/AI defined;
301     // we'll check that first, then give the normal search logic a chance.
302     // this ensures that models in AI/ are preferred to normal models, where
303     // both exist.
304         osg::ref_ptr<osgDB::ReaderWriter::Options> 
305           opt(osg::clone(osgDB::Registry::instance()->getOptions(), osg::CopyOp::SHALLOW_COPY));
306
307         SGPath ai_path(globals->get_fg_root(), "AI");
308         opt->setDatabasePath(ai_path.str());
309         
310         f = osgDB::findDataFile(model_path, opt.get());
311     }
312
313     if (f.empty()) {
314       f = simgear::SGModelLib::findDataFile(model_path);
315     }
316     
317     if(f.empty())
318         f = fgGetString("/sim/multiplay/default-model", default_model);
319     else
320         _installed = true;
321
322     _modeldata = new FGAIModelData(props);
323     osg::Node * mdl = SGModelLib::loadDeferredModel(f, props, _modeldata);
324
325     _model = new osg::LOD;
326     _model->setName("AI-model range animation node");
327
328     _model->addChild( mdl, 0, FLT_MAX );
329     _model->setCenterMode(osg::LOD::USE_BOUNDING_SPHERE_CENTER);
330     _model->setRangeMode(osg::LOD::DISTANCE_FROM_EYE_POINT);
331 //    We really need low-resolution versions of AI/MP aircraft.
332 //    Or at least dummy "stubs" with some default silhouette.
333 //        _model->addChild( SGModelLib::loadPagedModel(fgGetString("/sim/multiplay/default-model", default_model),
334 //                                                    props, new FGNasalModelData(props)), FLT_MAX, FLT_MAX);
335     updateLOD();
336
337     initModel(mdl);
338     if (_model.valid() && _initialized == false) {
339         aip.init( _model.get() );
340         aip.setVisible(true);
341         invisible = false;
342         globals->get_scenery()->get_scene_graph()->addChild(aip.getSceneGraph());
343         _initialized = true;
344
345         SG_LOG(SG_AI, SG_DEBUG, "AIBase: Loaded model " << model_path);
346
347     } else if (!model_path.empty()) {
348         SG_LOG(SG_AI, SG_WARN, "AIBase: Could not load model " << model_path);
349         // not properly installed...
350         _installed = false;
351     }
352
353     setDie(false);
354     return true;
355 }
356
357 void FGAIBase::initModel(osg::Node *node)
358 {
359     if (_model.valid()) { 
360
361         if( _path != ""){
362             props->setStringValue("submodels/path", _path.c_str());
363             SG_LOG(SG_AI, SG_DEBUG, "AIBase: submodels/path " << _path);
364         }
365
366         if( _parent!= ""){
367             props->setStringValue("parent-name", _parent.c_str());
368         }
369
370         fgSetString("/ai/models/model-added", props->getPath().c_str());
371     } else if (!model_path.empty()) {
372         SG_LOG(SG_AI, SG_WARN, "AIBase: Could not load model " << model_path);
373     }
374
375     setDie(false);
376 }
377
378
379 bool FGAIBase::isa( object_type otype ) {
380     return otype == _otype;
381 }
382
383
384 void FGAIBase::bind() {
385     _tiedProperties.setRoot(props);
386     tie("id", SGRawValueMethods<FGAIBase,int>(*this,
387         &FGAIBase::getID));
388     tie("velocities/true-airspeed-kt",  SGRawValuePointer<double>(&speed));
389     tie("velocities/vertical-speed-fps",
390         SGRawValueMethods<FGAIBase,double>(*this,
391         &FGAIBase::_getVS_fps,
392         &FGAIBase::_setVS_fps));
393
394     tie("position/altitude-ft",
395         SGRawValueMethods<FGAIBase,double>(*this,
396         &FGAIBase::_getAltitude,
397         &FGAIBase::_setAltitude));
398     tie("position/latitude-deg",
399         SGRawValueMethods<FGAIBase,double>(*this,
400         &FGAIBase::_getLatitude,
401         &FGAIBase::_setLatitude));
402     tie("position/longitude-deg",
403         SGRawValueMethods<FGAIBase,double>(*this,
404         &FGAIBase::_getLongitude,
405         &FGAIBase::_setLongitude));
406
407     tie("position/global-x",
408         SGRawValueMethods<FGAIBase,double>(*this,
409         &FGAIBase::_getCartPosX,
410         0));
411     tie("position/global-y",
412         SGRawValueMethods<FGAIBase,double>(*this,
413         &FGAIBase::_getCartPosY,
414         0));
415     tie("position/global-z",
416         SGRawValueMethods<FGAIBase,double>(*this,
417         &FGAIBase::_getCartPosZ,
418         0));
419     tie("callsign",
420         SGRawValueMethods<FGAIBase,const char*>(*this,
421         &FGAIBase::_getCallsign,
422         0));
423
424     tie("orientation/pitch-deg",   SGRawValuePointer<double>(&pitch));
425     tie("orientation/roll-deg",    SGRawValuePointer<double>(&roll));
426     tie("orientation/true-heading-deg", SGRawValuePointer<double>(&hdg));
427
428     tie("radar/in-range", SGRawValuePointer<bool>(&in_range));
429     tie("radar/bearing-deg",   SGRawValuePointer<double>(&bearing));
430     tie("radar/elevation-deg", SGRawValuePointer<double>(&elevation));
431     tie("radar/range-nm", SGRawValuePointer<double>(&range));
432     tie("radar/h-offset", SGRawValuePointer<double>(&horiz_offset));
433     tie("radar/v-offset", SGRawValuePointer<double>(&vert_offset));
434     tie("radar/x-shift", SGRawValuePointer<double>(&x_shift));
435     tie("radar/y-shift", SGRawValuePointer<double>(&y_shift));
436     tie("radar/rotation", SGRawValuePointer<double>(&rotation));
437     tie("radar/ht-diff-ft", SGRawValuePointer<double>(&ht_diff));
438     tie("subID", SGRawValuePointer<int>(&_subID));
439     tie("controls/lighting/nav-lights", SGRawValueFunctions<bool>(_isNight));
440
441     props->setBoolValue("controls/lighting/beacon", true);
442     props->setBoolValue("controls/lighting/strobe", true);
443     props->setBoolValue("controls/glide-path", true);
444
445     props->setStringValue("controls/flight/lateral-mode", "roll");
446     props->setDoubleValue("controls/flight/target-hdg", hdg);
447     props->setDoubleValue("controls/flight/target-roll", roll);
448
449     props->setStringValue("controls/flight/longitude-mode", "alt");
450     props->setDoubleValue("controls/flight/target-alt", altitude_ft);
451     props->setDoubleValue("controls/flight/target-pitch", pitch);
452
453     props->setDoubleValue("controls/flight/target-spd", speed);
454
455     props->setBoolValue("sim/sound/avionics/enabled", false);
456     props->setDoubleValue("sim/sound/avionics/volume", 0.0);
457     props->setBoolValue("sim/sound/avionics/external-view", false);
458     props->setBoolValue("sim/current-view/internal", false);
459 }
460
461 void FGAIBase::unbind() {
462     _tiedProperties.Untie();
463
464     props->setBoolValue("/sim/controls/radar", true);
465
466     removeSoundFx();
467 }
468
469 void FGAIBase::removeSoundFx() {
470     // drop reference to sound effects now
471     if (_fx)
472     {
473         // must remove explicitly - since the sound manager also keeps a reference
474         _fx->unbind();
475         // now drop last reference - kill the object
476         _fx = 0;
477     }
478 }
479
480 double FGAIBase::UpdateRadar(FGAIManager* manager)
481 {
482     bool control = fgGetBool("/sim/controls/radar", true);
483
484     if(!control) return 0;
485
486     double radar_range_m = fgGetDouble("/instrumentation/radar/range");
487     bool force_on = fgGetBool("/instrumentation/radar/debug-mode", false);
488     radar_range_m *= SG_NM_TO_METER  * 1.1; // + 10%
489     radar_range_m *= radar_range_m; // squared
490     
491     double d2 = distSqr(SGVec3d::fromGeod(pos), globals->get_aircraft_position_cart());
492     double range_ft = sqrt(d2) * SG_METER_TO_FEET;
493     
494     if (!force_on && (d2 > radar_range_m)) {
495         return range_ft * range_ft;
496     }
497     
498     props->setBoolValue("radar/in-range", true);
499
500     // copy values from the AIManager
501     double user_heading   = manager->get_user_heading();
502     double user_pitch     = manager->get_user_pitch();
503   
504     range = range_ft * SG_FEET_TO_METER * SG_METER_TO_NM;
505
506     // calculate bearing to target
507     bearing = SGGeodesy::courseDeg(globals->get_aircraft_position(), pos);
508
509     // calculate look left/right to target, without yaw correction
510     horiz_offset = bearing - user_heading;
511     SG_NORMALIZE_RANGE(horiz_offset, -180.0, 180.0);
512    
513     // calculate elevation to target
514     ht_diff = altitude_ft - globals->get_aircraft_position().getElevationFt();
515     elevation = atan2( ht_diff, range_ft ) * SG_RADIANS_TO_DEGREES;
516
517     // calculate look up/down to target
518     vert_offset = elevation - user_pitch;
519
520     /* this calculation needs to be fixed, but it isn't important anyway
521     // calculate range rate
522     double recip_bearing = bearing + 180.0;
523     if (recip_bearing > 360.0) recip_bearing -= 360.0;
524     double my_horiz_offset = recip_bearing - hdg;
525     if (my_horiz_offset > 180.0) my_horiz_offset -= 360.0;
526     if (my_horiz_offset < -180.0) my_horiz_offset += 360.0;
527     rdot = (-user_speed * cos( horiz_offset * SG_DEGREES_TO_RADIANS ))
528     +(-speed * 1.686 * cos( my_horiz_offset * SG_DEGREES_TO_RADIANS ));
529     */
530
531     // now correct look left/right for yaw
532     // horiz_offset += user_yaw; // FIXME: WHY WOULD WE WANT TO ADD IN SIDE-SLIP HERE?
533
534     // calculate values for radar display
535     y_shift = range * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
536     x_shift = range * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
537     
538     rotation = hdg - user_heading;
539     SG_NORMALIZE_RANGE(rotation, 0.0, 360.0);
540
541     return range_ft * range_ft;
542 }
543
544 /*
545 * Getters and Setters
546 */
547
548 SGVec3d FGAIBase::getCartPosAt(const SGVec3d& _off) const {
549     // Transform that one to the horizontal local coordinate system.
550     SGQuatd hlTrans = SGQuatd::fromLonLat(pos);
551
552     // and postrotate the orientation of the AIModel wrt the horizontal
553     // local frame
554     hlTrans *= SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
555
556     // The offset converted to the usual body fixed coordinate system
557     // rotated to the earth fixed coordinates axis
558     SGVec3d off = hlTrans.backTransform(_off);
559
560     // Add the position offset of the AIModel to gain the earth centered position
561     SGVec3d cartPos = SGVec3d::fromGeod(pos);
562
563     return cartPos + off;
564 }
565
566 SGVec3d FGAIBase::getCartPos() const {
567     SGVec3d cartPos = SGVec3d::fromGeod(pos);
568     return cartPos;
569 }
570
571 bool FGAIBase::getGroundElevationM(const SGGeod& pos, double& elev,
572                                    const simgear::BVHMaterial** material) const {
573     return globals->get_scenery()->get_elevation_m(pos, elev, material,
574                                                    _model.get());
575 }
576
577 double FGAIBase::_getCartPosX() const {
578     SGVec3d cartPos = getCartPos();
579     return cartPos.x();
580 }
581
582 double FGAIBase::_getCartPosY() const {
583     SGVec3d cartPos = getCartPos();
584     return cartPos.y();
585 }
586
587 double FGAIBase::_getCartPosZ() const {
588     SGVec3d cartPos = getCartPos();
589     return cartPos.z();
590 }
591
592 void FGAIBase::_setLongitude( double longitude ) {
593     pos.setLongitudeDeg(longitude);
594 }
595
596 void FGAIBase::_setLatitude ( double latitude )  {
597     pos.setLatitudeDeg(latitude);
598 }
599
600 void FGAIBase::_setSubID( int s ) {
601     _subID = s;
602 }
603
604 bool FGAIBase::setParentNode() {
605
606     if (_parent == ""){
607        SG_LOG(SG_AI, SG_ALERT, "AIBase: " << _name
608             << " parent not set ");
609        return false;
610     }
611
612     const SGPropertyNode_ptr ai = fgGetNode("/ai/models", true);
613
614     for (int i = ai->nChildren() - 1; i >= -1; i--) {
615         SGPropertyNode_ptr model;
616
617         if (i < 0) { // last iteration: selected model
618             model = _selected_ac;
619         } else {
620             model = ai->getChild(i);
621             //const string& path = ai->getPath();
622             const string name = model->getStringValue("name");
623
624             if (!model->nChildren()){
625                 continue;
626             }
627             if (name == _parent) {
628                 _selected_ac = model;  // save selected model for last iteration
629                 break;
630             }
631
632         }
633         if (!model)
634             continue;
635
636     }// end for loop
637
638     if (_selected_ac != 0){
639         const string name = _selected_ac->getStringValue("name");
640         return true;
641     } else {
642         SG_LOG(SG_AI, SG_ALERT, "AIBase: " << _name
643             << " parent not found: dying ");
644         setDie(true);
645         return false;
646     }
647
648 }
649
650 double FGAIBase::_getLongitude() const {
651     return pos.getLongitudeDeg();
652 }
653
654 double FGAIBase::_getLatitude() const {
655     return pos.getLatitudeDeg();
656 }
657
658 double FGAIBase::_getElevationFt() const {
659     return pos.getElevationFt();
660 }
661
662 double FGAIBase::_getRdot() const {
663     return rdot;
664 }
665
666 double FGAIBase::_getVS_fps() const {
667     return vs/60.0;
668 }
669
670 double FGAIBase::_get_speed_east_fps() const {
671     return speed_east_deg_sec * ft_per_deg_lon;
672 }
673
674 double FGAIBase::_get_speed_north_fps() const {
675     return speed_north_deg_sec * ft_per_deg_lat;
676 }
677
678 void FGAIBase::_setVS_fps( double _vs ) {
679     vs = _vs*60.0;
680 }
681
682 double FGAIBase::_getAltitude() const {
683     return altitude_ft;
684 }
685
686 double FGAIBase::_getAltitudeAGL(SGGeod inpos, double start){
687     getGroundElevationM(SGGeod::fromGeodM(inpos, start),
688         _elevation_m, NULL);
689     return inpos.getElevationFt() - _elevation_m * SG_METER_TO_FEET;
690 }
691
692 bool FGAIBase::_getServiceable() const {
693     return serviceable;
694 }
695
696 SGPropertyNode* FGAIBase::_getProps() const {
697     return props;
698 }
699
700 void FGAIBase::_setAltitude( double _alt ) {
701     setAltitude( _alt );
702 }
703
704 bool FGAIBase::_isNight() {
705     return (fgGetFloat("/sim/time/sun-angle-rad") > 1.57);
706 }
707
708 bool FGAIBase::_getCollisionData() {
709     return _collision_reported;
710 }
711
712 bool FGAIBase::_getExpiryData() {
713     return _expiry_reported;
714 }
715
716 bool FGAIBase::_getImpactData() {
717     return _impact_reported;
718 }
719
720 double FGAIBase::_getImpactLat() const {
721     return _impact_lat;
722 }
723
724 double FGAIBase::_getImpactLon() const {
725     return _impact_lon;
726 }
727
728 double FGAIBase::_getImpactElevFt() const {
729     return _impact_elev * SG_METER_TO_FEET;
730 }
731
732 double FGAIBase::_getImpactPitch() const {
733     return _impact_pitch;
734 }
735
736 double FGAIBase::_getImpactRoll() const {
737     return _impact_roll;
738 }
739
740 double FGAIBase::_getImpactHdg() const {
741     return _impact_hdg;
742 }
743
744 double FGAIBase::_getImpactSpeed() const {
745     return _impact_speed;
746 }
747
748 int FGAIBase::getID() const {
749     return  _refID;
750 }
751
752 int FGAIBase::_getSubID() const {
753     return  _subID;
754 }
755
756 double FGAIBase::_getSpeed() const {
757     return speed;
758 }
759
760 double FGAIBase::_getRoll() const {
761     return roll;
762 }
763
764 double FGAIBase::_getPitch() const {
765     return pitch;
766 }
767
768 double FGAIBase::_getHeading() const {
769     return hdg;
770 }
771
772 double  FGAIBase::_getXOffset() const {
773     return _x_offset;
774 }
775
776 double  FGAIBase::_getYOffset() const {
777     return _y_offset;
778 }
779
780 double  FGAIBase::_getZOffset() const {
781     return _z_offset;
782 }
783
784 const char* FGAIBase::_getPath() const {
785     return model_path.c_str();
786 }
787
788 const char* FGAIBase::_getSMPath() const {
789     return _path.c_str();
790 }
791
792 const char* FGAIBase::_getName() const {
793     return _name.c_str();
794 }
795
796 const char* FGAIBase::_getCallsign() const {
797     return _callsign.c_str();
798 }
799
800 const char* FGAIBase::_getSubmodel() const {
801     return _submodel.c_str();
802 }
803
804 void FGAIBase::CalculateMach() {
805     // Calculate rho at altitude, using standard atmosphere
806     // For the temperature T and the pressure p,
807     double altitude = altitude_ft;
808
809     if (altitude < 36152) {             // curve fits for the troposphere
810         T = 59 - 0.00356 * altitude;
811         p = 2116 * pow( ((T + 459.7) / 518.6) , 5.256);
812     } else if ( 36152 < altitude && altitude < 82345 ) {    // lower stratosphere
813         T = -70;
814         p = 473.1 * pow( e , 1.73 - (0.000048 * altitude) );
815     } else {                                    //  upper stratosphere
816         T = -205.05 + (0.00164 * altitude);
817         p = 51.97 * pow( ((T + 459.7) / 389.98) , -11.388);
818     }
819
820     rho = p / (1718 * (T + 459.7));
821
822     // calculate the speed of sound at altitude
823     // a = sqrt ( g * R * (T + 459.7))
824     // where:
825     // a = speed of sound [ft/s]
826     // g = specific heat ratio, which is usually equal to 1.4
827     // R = specific gas constant, which equals 1716 ft-lb/slug/R
828     a = sqrt ( 1.4 * 1716 * (T + 459.7));
829
830     // calculate Mach number
831     Mach = speed/a;
832
833     // cout  << "Speed(ft/s) "<< speed <<" Altitude(ft) "<< altitude << " Mach " << Mach << endl;
834 }
835
836 int FGAIBase::_newAIModelID() {
837     static int id = 0;
838
839     if (!++id)
840         id++;   // id = 0 is not allowed.
841
842     return id;
843 }
844
845
846 FGAIModelData::FGAIModelData(SGPropertyNode *root)
847   : _nasal( new FGNasalModelDataProxy(root) ),
848     _ready(false),
849     _initialized(false)
850 {
851 }
852
853 FGAIModelData::~FGAIModelData()
854 {
855     delete _nasal;
856     _nasal = NULL;
857 }
858
859 void FGAIModelData::modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *n)
860 {
861     // WARNING: Called in a separate OSG thread! Only use thread-safe stuff here...
862     if (_ready)
863         return;
864
865     _fxpath = prop->getStringValue("sound/path");
866     _nasal->modelLoaded(path, prop, n);
867
868     _ready = true;
869 }