X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FAIModel%2FAIBase.cxx;h=0945a13e6f7308a5cb9e716709402ffe041b9164;hb=89b41395d861a49ad543af166c0bd41c14dd0d2b;hp=2d7edd550f9addc07353f74e159afcda3c341709;hpb=0025dfb9bccc45d7f96b1badbe7c867679669c9e;p=flightgear.git diff --git a/src/AIModel/AIBase.cxx b/src/AIModel/AIBase.cxx index 2d7edd550..0945a13e6 100644 --- a/src/AIModel/AIBase.cxx +++ b/src/AIModel/AIBase.cxx @@ -24,6 +24,8 @@ # include #endif +#include + #include #include @@ -36,6 +38,7 @@ #include #include #include +#include #include #include @@ -72,6 +75,7 @@ FGAIBase::FGAIBase(object_type ot, bool enableHot) : _refID( _newAIModelID() ), _otype(ot), _initialized(false), + _modeldata(0), _fx(0) { @@ -139,10 +143,17 @@ FGAIBase::~FGAIBase() { if (parent) model_removed->setStringValue(props->getPath()); } - delete _fx; - _fx = 0; - delete fp; + if (_fx && _refID != 0 && _refID != 1) { + SGSoundMgr *smgr = globals->get_soundmgr(); + stringstream name; + name << "aifx:"; + name << _refID; + smgr->remove(name.str()); + } + + if (fp) + delete fp; fp = 0; } @@ -152,6 +163,9 @@ FGAIBase::~FGAIBase() { void FGAIBase::removeModel() { + if (!_model.valid()) + return; + FGScenery* pSceneryManager = globals->get_scenery(); if (pSceneryManager) { @@ -161,7 +175,7 @@ FGAIBase::removeModel() aip.init( 0 ); _model = 0; // pass it on to the pager, to be be deleted in the pager thread - pSceneryManager->getPagerSingleton()->queueDeleteRequest(temp); + pSceneryManager->getPager()->queueDeleteRequest(temp); } else { @@ -213,9 +227,34 @@ void FGAIBase::update(double dt) { _fx->set_orientation( orient ); SGVec3d velocity; - velocity = SGVec3d( speed_north_deg_sec, speed_east_deg_sec, pitch*speed ); + velocity = SGVec3d( speed_north_deg_sec, speed_east_deg_sec, + pitch*speed ); _fx->set_velocity( velocity ); } + else if ((_modeldata)&&(_modeldata->needInitilization())) + { + // process deferred nasal initialization, + // which must be done in main thread + _modeldata->init(); + + // sound initialization + if (fgGetBool("/sim/sound/aimodels/enabled",false)) + { + string fxpath = _modeldata->get_sound_path(); + if (fxpath != "") + { + props->setStringValue("sim/sound/path", fxpath.c_str()); + + // initialize the sound configuration + SGSoundMgr *smgr = globals->get_soundmgr(); + stringstream name; + name << "aifx:"; + name << _refID; + _fx = new FGFX(smgr, name.str(), props); + _fx->init(); + } + } + } } /** update LOD properties of the model */ @@ -258,8 +297,14 @@ void FGAIBase::Transform() { } -bool FGAIBase::init(bool search_in_AI_path) { - +bool FGAIBase::init(bool search_in_AI_path) +{ + if (_model.valid()) + { + SG_LOG(SG_AI, SG_ALERT, "AIBase: Cannot initialize a model multiple times! " << model_path); + return false; + } + string f; if(search_in_AI_path) { @@ -285,13 +330,8 @@ bool FGAIBase::init(bool search_in_AI_path) { else _installed = true; - osg::Node * mdl = SGModelLib::loadDeferredModel(f, props, new FGNasalModelData(props)); - - if (_model.valid()) - { - // reinit, dump the old model - removeModel(); - } + _modeldata = new FGAIModelData(props); + osg::Node * mdl = SGModelLib::loadDeferredModel(f, props, _modeldata); _model = new osg::LOD; _model->setName("AI-model range animation node"); @@ -314,17 +354,6 @@ bool FGAIBase::init(bool search_in_AI_path) { // Get the sound-path tag from the configuration file and store it // in the property tree. - string fxpath = props->getStringValue("sim/sound/path"); - if ( !fxpath.empty() ) - { - props->setStringValue("sim/sound/path", fxpath.c_str()); - - // initialize the sound configuration - SGSoundMgr *smgr = globals->get_soundmgr(); - _fx = new FGFX(smgr, "aifx:"+f, props); - _fx->init(); - } - _initialized = true; } else if (!model_path.empty()) { @@ -365,61 +394,62 @@ bool FGAIBase::isa( object_type otype ) { void FGAIBase::bind() { - props->tie("id", SGRawValueMethods(*this, + _tiedProperties.setRoot(props); + tie("id", SGRawValueMethods(*this, &FGAIBase::getID)); - props->tie("velocities/true-airspeed-kt", SGRawValuePointer(&speed)); - props->tie("velocities/vertical-speed-fps", + tie("velocities/true-airspeed-kt", SGRawValuePointer(&speed)); + tie("velocities/vertical-speed-fps", SGRawValueMethods(*this, &FGAIBase::_getVS_fps, &FGAIBase::_setVS_fps)); - props->tie("position/altitude-ft", + tie("position/altitude-ft", SGRawValueMethods(*this, &FGAIBase::_getAltitude, &FGAIBase::_setAltitude)); - props->tie("position/latitude-deg", + tie("position/latitude-deg", SGRawValueMethods(*this, &FGAIBase::_getLatitude, &FGAIBase::_setLatitude)); - props->tie("position/longitude-deg", + tie("position/longitude-deg", SGRawValueMethods(*this, &FGAIBase::_getLongitude, &FGAIBase::_setLongitude)); - props->tie("position/global-x", + tie("position/global-x", SGRawValueMethods(*this, &FGAIBase::_getCartPosX, 0)); - props->tie("position/global-y", + tie("position/global-y", SGRawValueMethods(*this, &FGAIBase::_getCartPosY, 0)); - props->tie("position/global-z", + tie("position/global-z", SGRawValueMethods(*this, &FGAIBase::_getCartPosZ, 0)); - props->tie("callsign", + tie("callsign", SGRawValueMethods(*this, &FGAIBase::_getCallsign, 0)); - props->tie("orientation/pitch-deg", SGRawValuePointer(&pitch)); - props->tie("orientation/roll-deg", SGRawValuePointer(&roll)); - props->tie("orientation/true-heading-deg", SGRawValuePointer(&hdg)); - - props->tie("radar/in-range", SGRawValuePointer(&in_range)); - props->tie("radar/bearing-deg", SGRawValuePointer(&bearing)); - props->tie("radar/elevation-deg", SGRawValuePointer(&elevation)); - props->tie("radar/range-nm", SGRawValuePointer(&range)); - props->tie("radar/h-offset", SGRawValuePointer(&horiz_offset)); - props->tie("radar/v-offset", SGRawValuePointer(&vert_offset)); - props->tie("radar/x-shift", SGRawValuePointer(&x_shift)); - props->tie("radar/y-shift", SGRawValuePointer(&y_shift)); - props->tie("radar/rotation", SGRawValuePointer(&rotation)); - props->tie("radar/ht-diff-ft", SGRawValuePointer(&ht_diff)); - props->tie("subID", SGRawValuePointer(&_subID)); - props->tie("controls/lighting/nav-lights", - SGRawValueFunctions(_isNight)); + tie("orientation/pitch-deg", SGRawValuePointer(&pitch)); + tie("orientation/roll-deg", SGRawValuePointer(&roll)); + tie("orientation/true-heading-deg", SGRawValuePointer(&hdg)); + + tie("radar/in-range", SGRawValuePointer(&in_range)); + tie("radar/bearing-deg", SGRawValuePointer(&bearing)); + tie("radar/elevation-deg", SGRawValuePointer(&elevation)); + tie("radar/range-nm", SGRawValuePointer(&range)); + tie("radar/h-offset", SGRawValuePointer(&horiz_offset)); + tie("radar/v-offset", SGRawValuePointer(&vert_offset)); + tie("radar/x-shift", SGRawValuePointer(&x_shift)); + tie("radar/y-shift", SGRawValuePointer(&y_shift)); + tie("radar/rotation", SGRawValuePointer(&rotation)); + tie("radar/ht-diff-ft", SGRawValuePointer(&ht_diff)); + tie("subID", SGRawValuePointer(&_subID)); + tie("controls/lighting/nav-lights", SGRawValueFunctions(_isNight)); + props->setBoolValue("controls/lighting/beacon", true); props->setBoolValue("controls/lighting/strobe", true); props->setBoolValue("controls/glide-path", true); @@ -438,41 +468,15 @@ void FGAIBase::bind() { props->setDoubleValue("sim/sound/avionics/volume", 0.0); props->setBoolValue("sim/sound/avionics/external-view", false); props->setBoolValue("sim/current-view/internal", false); - } void FGAIBase::unbind() { - props->untie("id"); - props->untie("velocities/true-airspeed-kt"); - props->untie("velocities/vertical-speed-fps"); + _tiedProperties.Untie(); - props->untie("position/altitude-ft"); - props->untie("position/latitude-deg"); - props->untie("position/longitude-deg"); - props->untie("position/global-x"); - props->untie("position/global-y"); - props->untie("position/global-z"); - props->untie("callsign"); - - props->untie("orientation/pitch-deg"); - props->untie("orientation/roll-deg"); - props->untie("orientation/true-heading-deg"); - - props->untie("radar/in-range"); - props->untie("radar/bearing-deg"); - props->untie("radar/elevation-deg"); - props->untie("radar/range-nm"); - props->untie("radar/h-offset"); - props->untie("radar/v-offset"); - props->untie("radar/x-shift"); - props->untie("radar/y-shift"); - props->untie("radar/rotation"); - props->untie("radar/ht-diff-ft"); - - props->untie("controls/lighting/nav-lights"); - - props->setBoolValue("/sim/controls/radar/", true); + props->setBoolValue("/sim/controls/radar", true); + // drop reference to sound effects now + _fx = 0; } double FGAIBase::UpdateRadar(FGAIManager* manager) { @@ -884,3 +888,28 @@ int FGAIBase::_newAIModelID() { return id; } + +FGAIModelData::FGAIModelData(SGPropertyNode *root) + : _nasal( new FGNasalModelDataProxy(root) ), + _ready(false), + _initialized(false) +{ +} + +FGAIModelData::~FGAIModelData() +{ + delete _nasal; + _nasal = NULL; +} + +void FGAIModelData::modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *n) +{ + // WARNING: Called in a separate OSG thread! Only use thread-safe stuff here... + if (_ready) + return; + + _fxpath = prop->getStringValue("sound/path"); + _nasal->modelLoaded(path, prop, n); + + _ready = true; +}