# include <config.h>
#endif
+#include <string.h>
+
#include <simgear/compiler.h>
#include <string>
#include <simgear/misc/sg_path.hxx>
#include <simgear/scene/model/modellib.hxx>
#include <simgear/scene/util/SGNodeMasks.hxx>
+#include <simgear/sound/soundmgr_openal.hxx>
#include <simgear/debug/logstream.hxx>
#include <simgear/props/props.hxx>
#include <Main/globals.hxx>
#include <Scenery/scenery.hxx>
#include <Scripting/NasalSys.hxx>
+#include <Sound/fg_fx.hxx>
#include "AIBase.hxx"
#include "AIManager.hxx"
using namespace simgear;
-FGAIBase::FGAIBase(object_type ot) :
+FGAIBase::FGAIBase(object_type ot, bool enableHot) :
+ _max_speed(300),
+ _name(""),
+ _parent(""),
props( NULL ),
model_removed( fgGetNode("/ai/models/model-removed", true) ),
manager( NULL ),
+ _installed(false),
fp( NULL ),
-
_impact_lat(0),
_impact_lon(0),
_impact_elev(0),
_impact_pitch(0),
_impact_roll(0),
_impact_speed(0),
-
_refID( _newAIModelID() ),
_otype(ot),
- _initialized(false)
+ _initialized(false),
+ _aimodel(0),
+ _fxpath(""),
+ _fx(0)
+
{
tgt_heading = hdg = tgt_altitude_ft = tgt_speed = 0.0;
tgt_roll = roll = tgt_pitch = tgt_yaw = tgt_vs = vs = pitch = 0.0;
delete_me = false;
_impact_reported = false;
_collision_reported = false;
+ _expiry_reported = false;
+
_subID = 0;
_x_offset = 0;
p = 1e5;
a = 340;
Mach = 0;
+
+ // explicitly disable HOT for (most) AI models
+ if (!enableHot)
+ aip.getSceneGraph()->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
}
FGAIBase::~FGAIBase() {
// Unregister that one at the scenery manager
- if (globals->get_scenery()) {
- globals->get_scenery()->get_scene_graph()->removeChild(aip.getSceneGraph());
- }
+ removeModel();
if (props) {
SGPropertyNode* parent = props->getParent();
if (parent)
model_removed->setStringValue(props->getPath());
}
+
+ if (_refID != 0 && _refID != 1) {
+ SGSoundMgr *smgr = globals->get_soundmgr();
+ smgr->remove("aifx:"+_refID);
+ }
+
delete fp;
fp = 0;
}
+/** Cleanly remove the model
+ * and let the scenery database pager do the clean-up work.
+ */
+void
+FGAIBase::removeModel()
+{
+ FGScenery* pSceneryManager = globals->get_scenery();
+ if (pSceneryManager)
+ {
+ osg::ref_ptr<osg::Object> temp = _model.get();
+ pSceneryManager->get_scene_graph()->removeChild(aip.getSceneGraph());
+ // withdraw from SGModelPlacement and drop own reference (unref)
+ aip.init( 0 );
+ _model = 0;
+ // pass it on to the pager, to be be deleted in the pager thread
+ pSceneryManager->getPagerSingleton()->queueDeleteRequest(temp);
+ }
+ else
+ {
+ SG_LOG(SG_AI, SG_ALERT, "AIBase: Could not unload model. Missing scenery manager!");
+ }
+}
+
void FGAIBase::readFromScenario(SGPropertyNode* scFileNode)
{
if (!scFileNode)
ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.getLatitudeRad());
ft_per_deg_lon = 365228.16 * cos(pos.getLatitudeRad());
+
+ if ( _fx )
+ {
+ // update model's audio sample values
+ _fx->set_position_geod( pos );
+
+ SGQuatd orient = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
+ _fx->set_orientation( orient );
+
+ SGVec3d velocity;
+ velocity = SGVec3d( speed_north_deg_sec, speed_east_deg_sec, pitch*speed );
+ _fx->set_velocity( velocity );
+ }
+ else if (_aimodel)
+ {
+ string fxpath = _aimodel->get_sound_path();
+ if (fxpath != "")
+ {
+ _fxpath = fxpath;
+ props->setStringValue("sim/sound/path", _fxpath.c_str());
+
+ // initialize the sound configuration
+ SGSoundMgr *smgr = globals->get_soundmgr();
+ _fx = new FGFX(smgr, "aifx:"+_refID, props);
+ _fx->init();
+ }
+ }
+}
+
+/** update LOD properties of the model */
+void FGAIBase::updateLOD()
+{
+ double maxRangeDetail = fgGetDouble("/sim/rendering/static-lod/ai-detailed", 10000.0);
+ double maxRangeBare = fgGetDouble("/sim/rendering/static-lod/ai-bare", 20000.0);
+ if (_model.valid())
+ {
+ if( maxRangeDetail == 0.0 )
+ {
+ // disable LOD
+ _model->setRange(0, 0.0, FLT_MAX);
+ _model->setRange(1, FLT_MAX, FLT_MAX);
+ }
+ else
+ {
+ _model->setRange(0, 0.0, maxRangeDetail);
+ _model->setRange(1, maxRangeDetail,maxRangeBare);
+ }
+ }
}
void FGAIBase::Transform() {
}
bool FGAIBase::init(bool search_in_AI_path) {
- osg::ref_ptr<osgDB::ReaderWriter::Options> opt=
- new osgDB::ReaderWriter::Options(*osgDB::Registry::instance()->getOptions());
-
+
+ string f;
if(search_in_AI_path)
{
- SGPath ai_path(globals->get_fg_root());
- ai_path.append("AI");
- opt->getDatabasePathList().push_front(ai_path.str());
+ // setup a modified Options structure, with only the $fg-root/AI defined;
+ // we'll check that first, then give the normal search logic a chance.
+ // this ensures that models in AI/ are preferred to normal models, where
+ // both exist.
+ osg::ref_ptr<osgDB::ReaderWriter::Options>
+ opt(osg::clone(osgDB::Registry::instance()->getOptions(), osg::CopyOp::SHALLOW_COPY));
+
+ SGPath ai_path(globals->get_fg_root(), "AI");
+ opt->setDatabasePath(ai_path.str());
+
+ f = osgDB::findDataFile(model_path, opt.get());
}
- string f = osgDB::findDataFile(model_path, opt.get());
-
+ if (f.empty()) {
+ f = simgear::SGModelLib::findDataFile(model_path);
+ }
+
if(f.empty())
f = fgGetString("/sim/multiplay/default-model", default_model);
+ else
+ _installed = true;
- model = load3DModel(f, props);
+ _aimodel = new FGAIModelData(props);
+ osg::Node * mdl = SGModelLib::loadDeferredModel(f, props, _aimodel);
- if (model.valid() && _initialized == false) {
- aip.init( model.get() );
+ if (_model.valid())
+ {
+ // reinit, dump the old model
+ removeModel();
+ }
+
+ _model = new osg::LOD;
+ _model->setName("AI-model range animation node");
+
+ _model->addChild( mdl, 0, FLT_MAX );
+ _model->setCenterMode(osg::LOD::USE_BOUNDING_SPHERE_CENTER);
+ _model->setRangeMode(osg::LOD::DISTANCE_FROM_EYE_POINT);
+// We really need low-resolution versions of AI/MP aircraft.
+// Or at least dummy "stubs" with some default silhouette.
+// _model->addChild( SGModelLib::loadPagedModel(fgGetString("/sim/multiplay/default-model", default_model),
+// props, new FGNasalModelData(props)), FLT_MAX, FLT_MAX);
+ updateLOD();
+
+ initModel(mdl);
+ if (_model.valid() && _initialized == false) {
+ aip.init( _model.get() );
aip.setVisible(true);
invisible = false;
globals->get_scenery()->get_scene_graph()->addChild(aip.getSceneGraph());
+
+ // Get the sound-path tag from the configuration file and store it
+ // in the property tree.
_initialized = true;
} else if (!model_path.empty()) {
- SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load model " << model_path);
+ SG_LOG(SG_AI, SG_WARN, "AIBase: Could not load model " << model_path);
+ // not properly installed...
+ _installed = false;
}
setDie(false);
void FGAIBase::initModel(osg::Node *node)
{
- if (model.valid()) {
+ if (_model.valid()) {
- fgSetString("/ai/models/model-added", props->getPath().c_str());
+ if( _path != ""){
+ props->setStringValue("submodels/path", _path.c_str());
+ SG_LOG(SG_AI, SG_DEBUG, "AIBase: submodels/path " << _path);
+ }
+
+ if( _parent!= ""){
+ props->setStringValue("parent-name", _parent.c_str());
+ }
+ fgSetString("/ai/models/model-added", props->getPath().c_str());
} else if (!model_path.empty()) {
- SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load model " << model_path);
+ SG_LOG(SG_AI, SG_WARN, "AIBase: Could not load model " << model_path);
}
- props->setStringValue("submodels/path", _path.c_str());
setDie(false);
}
-osg::Node* FGAIBase::load3DModel(const string &path, SGPropertyNode *prop_root)
-{
- model = SGModelLib::loadPagedModel(path, prop_root, new FGNasalModelData(prop_root));
- initModel(model.get());
- return model.get();
-}
-
bool FGAIBase::isa( object_type otype ) {
return otype == _otype;
}
props->setDoubleValue("controls/flight/target-alt", altitude_ft);
props->setDoubleValue("controls/flight/target-pitch", pitch);
- props->setDoubleValue("controls/flight/target-spd", speed);
+ props->setDoubleValue("controls/flight/target-spd", speed);
+
+ props->setBoolValue("sim/sound/avionics/enabled", false);
+ props->setDoubleValue("sim/sound/avionics/volume", 0.0);
+ props->setBoolValue("sim/sound/avionics/external-view", false);
+ props->setBoolValue("sim/current-view/internal", false);
}
hlTrans *= SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
// The offset converted to the usual body fixed coordinate system
- // rotated to the earth fiexed coordinates axis
+ // rotated to the earth fixed coordinates axis
SGVec3d off = hlTrans.backTransform(_off);
// Add the position offset of the AIModel to gain the earth centered position
bool FGAIBase::getGroundElevationM(const SGGeod& pos, double& elev,
const SGMaterial** material) const {
return globals->get_scenery()->get_elevation_m(pos, elev, material,
- model.get());
+ _model.get());
}
double FGAIBase::_getCartPosX() const {
_subID = s;
}
+bool FGAIBase::setParentNode() {
+
+ if (_parent == ""){
+ SG_LOG(SG_AI, SG_ALERT, "AIBase: " << _name
+ << " parent not set ");
+ return false;
+ }
+
+ const SGPropertyNode_ptr ai = fgGetNode("/ai/models", true);
+
+ for (int i = ai->nChildren() - 1; i >= -1; i--) {
+ SGPropertyNode_ptr model;
+
+ if (i < 0) { // last iteration: selected model
+ model = _selected_ac;
+ } else {
+ model = ai->getChild(i);
+ string path = ai->getPath();
+ const string name = model->getStringValue("name");
+
+ if (!model->nChildren()){
+ continue;
+ }
+ if (name == _parent) {
+ _selected_ac = model; // save selected model for last iteration
+ break;
+ }
+
+ }
+ if (!model)
+ continue;
+
+ }// end for loop
+
+ if (_selected_ac != 0){
+ const string name = _selected_ac->getStringValue("name");
+ return true;
+ } else {
+ SG_LOG(SG_AI, SG_ALERT, "AIBase: " << _name
+ << " parent not found: dying ");
+ setDie(true);
+ return false;
+ }
+
+}
+
double FGAIBase::_getLongitude() const {
return pos.getLongitudeDeg();
}
return pos.getLatitudeDeg();
}
-double FGAIBase::_getElevationFt () const {
+double FGAIBase::_getElevationFt() const {
return pos.getElevationFt();
}
}
double FGAIBase::_getVS_fps() const {
- return vs*60.0;
+ return vs/60.0;
}
double FGAIBase::_get_speed_east_fps() const {
}
void FGAIBase::_setVS_fps( double _vs ) {
- vs = _vs/60.0;
+ vs = _vs*60.0;
}
double FGAIBase::_getAltitude() const {
return altitude_ft;
}
+double FGAIBase::_getAltitudeAGL(SGGeod inpos, double start){
+ getGroundElevationM(SGGeod::fromGeodM(inpos, start),
+ _elevation_m, &_material);
+ return inpos.getElevationFt() - _elevation_m * SG_METER_TO_FEET;
+}
+
bool FGAIBase::_getServiceable() const {
return serviceable;
}
return _collision_reported;
}
+bool FGAIBase::_getExpiryData() {
+ return _expiry_reported;
+}
+
bool FGAIBase::_getImpactData() {
return _impact_reported;
}
return id;
}
+
+FGAIModelData::FGAIModelData(SGPropertyNode *root)
+ : _nasal( new FGNasalModelData(root) ),
+ _path("")
+{
+}
+
+FGAIModelData::~FGAIModelData()
+{
+ delete _nasal;
+}
+
+void FGAIModelData::modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *n)
+{
+ const char* fxpath = prop->getStringValue("sound/path");
+ if (fxpath) {
+ string sound_path = string(fxpath);
+ if (sound_path != "") {
+ _path = "/AI/"+sound_path;
+ }
+ }
+ _nasal->modelLoaded(path, prop, n);
+}