]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIBase.cxx
Merge branch 'durk/traffic'
[flightgear.git] / src / AIModel / AIBase.cxx
index e375f606c917fb0f8defd9ff85b4d58406681806..de107a682bfc8ae356784e42c119304aa80098b3 100644 (file)
 #  include <config.h>
 #endif
 
-
 #include <simgear/compiler.h>
 
-#include STL_STRING
+#include <string>
 
 #include <osg/ref_ptr>
 #include <osg/Node>
+#include <osgDB/FileUtils>
 
-#include <simgear/math/point3d.hxx>
-#include <simgear/math/polar3d.hxx>
-#include <simgear/math/sg_geodesy.hxx>
+#include <simgear/math/SGMath.hxx>
 #include <simgear/misc/sg_path.hxx>
-#include <simgear/scene/model/location.hxx>
-#include <simgear/scene/model/model.hxx>
+#include <simgear/scene/model/modellib.hxx>
 #include <simgear/scene/util/SGNodeMasks.hxx>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/props/props.hxx>
 #include "AIBase.hxx"
 #include "AIManager.hxx"
 
+const char *default_model = "Models/Geometry/glider.ac";
 const double FGAIBase::e = 2.71828183;
 const double FGAIBase::lbs_to_slugs = 0.031080950172;   //conversion factor
 
+using namespace simgear;
 
 FGAIBase::FGAIBase(object_type ot) :
     props( NULL ),
@@ -68,7 +67,8 @@ FGAIBase::FGAIBase(object_type ot) :
     _impact_speed(0),
 
     _refID( _newAIModelID() ),
-    _otype(ot)
+    _otype(ot),
+    _initialized(false)
 {
     tgt_heading = hdg = tgt_altitude_ft = tgt_speed = 0.0;
     tgt_roll = roll = tgt_pitch = tgt_yaw = tgt_vs = vs = pitch = 0.0;
@@ -82,6 +82,40 @@ FGAIBase::FGAIBase(object_type ot) :
     _impact_reported = false;
     _collision_reported = false;
     _subID = 0;
+
+    _x_offset = 0;
+    _y_offset = 0;
+    _z_offset = 0;
+
+    _pitch_offset = 0;
+    _roll_offset = 0;
+    _yaw_offset = 0;
+
+    userpos = SGGeod::fromDeg(0, 0);
+
+    pos = SGGeod::fromDeg(0, 0);
+    speed = 0;
+    altitude_ft = 0;
+    speed_north_deg_sec = 0;
+    speed_east_deg_sec = 0;
+    turn_radius_ft = 0;
+
+    ft_per_deg_lon = 0;
+    ft_per_deg_lat = 0;
+
+    horiz_offset = 0;
+    vert_offset = 0;
+    ht_diff = 0;
+
+    serviceable = false;
+
+    fp = 0;
+
+    rho = 1;
+    T = 280;
+    p = 1e5;
+    a = 340;
+    Mach = 0;
 }
 
 FGAIBase::~FGAIBase() {
@@ -105,7 +139,8 @@ void FGAIBase::readFromScenario(SGPropertyNode* scFileNode)
     if (!scFileNode)
         return;
 
-    setPath(scFileNode->getStringValue("model", "Models/Geometry/glider.ac"));
+    setPath(scFileNode->getStringValue("model",
+            fgGetString("/sim/multiplay/default-model", default_model)));
 
     setHeading(scFileNode->getDoubleValue("heading", 0.0));
     setSpeed(scFileNode->getDoubleValue("speed", 0.0));
@@ -138,6 +173,7 @@ void FGAIBase::update(double dt) {
 void FGAIBase::Transform() {
 
     if (!invisible) {
+        aip.setVisible(true);
         aip.setPosition(pos);
 
         if (no_roll)
@@ -146,63 +182,65 @@ void FGAIBase::Transform() {
             aip.setOrientation(roll, pitch, hdg);
 
         aip.update();
+    } else {
+        aip.setVisible(false);
+        aip.update();
     }
 
 }
 
 bool FGAIBase::init(bool search_in_AI_path) {
+    osg::ref_ptr<osgDB::ReaderWriter::Options> opt=
+        new osgDB::ReaderWriter::Options(*osgDB::Registry::instance()->getOptions());
+
+    if(search_in_AI_path)
+    {
+        SGPath ai_path(globals->get_fg_root());
+        ai_path.append("AI");
+        opt->getDatabasePathList().push_front(ai_path.str());
+    }
 
-    if (!model_path.empty()) {
-
-        if ( search_in_AI_path
-                && (model_path.substr(model_path.size() - 4, 4) == ".xml")) {
-            SGPath ai_path("AI");
-            ai_path.append(model_path);
-            try {
-                model = load3DModel( globals->get_fg_root(), ai_path.str(), props,
-                        globals->get_sim_time_sec() );
-            } catch (const sg_exception &e) {
-                model = NULL;
-            }
-        } else
-            model = NULL;
-
-        if (!model.get()) {
-            try {
-                model = load3DModel( globals->get_fg_root(), model_path, props,
-                        globals->get_sim_time_sec() );
-            } catch (const sg_exception &e) {
-                model = NULL;
-            }
-        }
+    string f = osgDB::findDataFile(model_path, opt.get());
 
-    }
+    if(f.empty())
+        f = fgGetString("/sim/multiplay/default-model", default_model);
+
+    model = load3DModel(f, props);
 
-    if (model.get()) {
+    if (model.valid() && _initialized == false) {
         aip.init( model.get() );
         aip.setVisible(true);
         invisible = false;
         globals->get_scenery()->get_scene_graph()->addChild(aip.getSceneGraph());
-        fgSetString("/ai/models/model-added", props->getPath());
+        _initialized = true;
 
     } else if (!model_path.empty()) {
         SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load model " << model_path);
     }
 
-    props->setStringValue("submodels/path", _path.c_str());
     setDie(false);
     return true;
 }
 
+void FGAIBase::initModel(osg::Node *node)
+{
+    if (model.valid()) {
+
+        fgSetString("/ai/models/model-added", props->getPath().c_str());
 
-osg::Node* FGAIBase::load3DModel(const string& fg_root,
-                      const string &path,
-                      SGPropertyNode *prop_root,
-                      double sim_time_sec)
+    } else if (!model_path.empty()) {
+        SG_LOG(SG_INPUT, 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 = sgLoad3DModel(fg_root, path, prop_root, sim_time_sec, 0,
-                        new FGNasalModelData(prop_root));
-  model->setNodeMask(model->getNodeMask() & ~SG_NODEMASK_TERRAIN_BIT);
+  model = SGModelLib::loadPagedModel(path, prop_root, new FGNasalModelData(prop_root));
+  initModel(model.get());
   return model.get();
 }
 
@@ -245,6 +283,10 @@ void FGAIBase::bind() {
         SGRawValueMethods<FGAIBase,double>(*this,
         &FGAIBase::_getCartPosZ,
         0));
+    props->tie("callsign",
+        SGRawValueMethods<FGAIBase,const char*>(*this,
+        &FGAIBase::_getCallsign,
+        0));
 
     props->tie("orientation/pitch-deg",   SGRawValuePointer<double>(&pitch));
     props->tie("orientation/roll-deg",    SGRawValuePointer<double>(&roll));
@@ -290,6 +332,7 @@ void FGAIBase::unbind() {
     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");
@@ -307,9 +350,16 @@ void FGAIBase::unbind() {
     props->untie("radar/ht-diff-ft");
 
     props->untie("controls/lighting/nav-lights");
+
+    props->setBoolValue("/sim/controls/radar/", true);
+
 }
 
 double FGAIBase::UpdateRadar(FGAIManager* manager) {
+    bool control = fgGetBool("/sim/controls/radar", true);
+
+    if(!control) return 0;
+
     double radar_range_ft2 = fgGetDouble("/instrumentation/radar/range");
     bool force_on = fgGetBool("/instrumentation/radar/debug-mode", false);
     radar_range_ft2 *= SG_NM_TO_METER * SG_METER_TO_FEET * 1.1; // + 10%
@@ -430,18 +480,16 @@ SGVec3d FGAIBase::getCartPosAt(const SGVec3d& _off) const {
 }
 
 SGVec3d FGAIBase::getCartPos() const {
-    // Transform that one to the horizontal local coordinate system.
-    SGQuatd hlTrans = SGQuatd::fromLonLat(pos);
-
-    // and postrotate the orientation of the AIModel wrt the horizontal
-    // local frame
-    hlTrans *= SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
-
     SGVec3d cartPos = SGVec3d::fromGeod(pos);
-
     return cartPos;
 }
 
+bool FGAIBase::getGroundElevationM(const SGGeod& pos, double& elev,
+                                   const SGMaterial** material) const {
+    return globals->get_scenery()->get_elevation_m(pos, elev, material,
+                                                   model.get());
+}
+
 double FGAIBase::_getCartPosX() const {
     SGVec3d cartPos = getCartPos();
     return cartPos.x();
@@ -465,6 +513,12 @@ void FGAIBase::_setLatitude ( double latitude )  {
     pos.setLatitudeDeg(latitude);
 }
 
+void FGAIBase::_setUserPos(){
+    userpos.setLatitudeDeg(manager->get_user_latitude());
+    userpos.setLongitudeDeg(manager->get_user_longitude());
+    userpos.setElevationM(manager->get_user_altitude() * SG_FEET_TO_METER);
+}
+
 void FGAIBase::_setSubID( int s ) {
     _subID = s;
 }
@@ -581,23 +635,38 @@ double FGAIBase::_getHeading() const {
     return hdg;
 }
 
-const char* FGAIBase::_getPath() {
+double  FGAIBase::_getXOffset() const {
+    return _x_offset;
+}
+
+double  FGAIBase::_getYOffset() const {
+    return _y_offset;
+}
+
+double  FGAIBase::_getZOffset() const {
+    return _z_offset;
+}
+
+const char* FGAIBase::_getPath() const {
+    return model_path.c_str();
+}
+
+const char* FGAIBase::_getSMPath() const {
     return _path.c_str();
 }
 
-const char* FGAIBase::_getName() {
+const char* FGAIBase::_getName() const {
     return _name.c_str();
 }
 
-const char* FGAIBase::_getCallsign() {
+const char* FGAIBase::_getCallsign() const {
     return _callsign.c_str();
 }
 
-const char* FGAIBase::_getSubmodel() {
+const char* FGAIBase::_getSubmodel() const {
     return _submodel.c_str();
 }
 
-
 void FGAIBase::CalculateMach() {
     // Calculate rho at altitude, using standard atmosphere
     // For the temperature T and the pressure p,
@@ -621,7 +690,7 @@ void FGAIBase::CalculateMach() {
     // where:
     // a = speed of sound [ft/s]
     // g = specific heat ratio, which is usually equal to 1.4
-    // R = specific gas constant, which equals 1716 ft-lb/slug/°R
+    // R = specific gas constant, which equals 1716 ft-lb/slug/R
     a = sqrt ( 1.4 * 1716 * (T + 459.7));
 
     // calculate Mach number