From ce5f342b5ad39d22946e8a8a731a0285691055dd Mon Sep 17 00:00:00 2001
From: till busch <buti@bux.at>
Date: Tue, 4 Mar 2008 20:49:02 +0100
Subject: [PATCH] model-paging v0.3.0 (beta)

---
 src/AIModel/AIBase.cxx           |   40 +++++++++++++++++++++++++++----------
 src/AIModel/AIBase.hxx           |    1 +
 src/AIModel/AICarrier.cxx        |   37 +++++++++++++++++++++-------------
 src/AIModel/AICarrier.hxx        |    1 +
 src/AIModel/Makefile.am          |    1 +
 src/AIModel/submodel.cxx         |    6 ++--
 src/Main/main.cxx                |   14 ++++++++++++-
 src/Model/model_panel.cxx        |    2 +-
 src/MultiPlayer/multiplaymgr.cxx |    5 +--
 9 files changed, 74 insertions(+), 33 deletions(-)

diff --git a/src/AIModel/AIBase.cxx b/src/AIModel/AIBase.cxx
index 35be09b..d6ba8d4 100644
--- a/src/AIModel/AIBase.cxx
+++ b/src/AIModel/AIBase.cxx
@@ -24,7 +24,6 @@
 #  include <config.h>
 #endif
 
-
 #include <simgear/compiler.h>
 
 #include STL_STRING
@@ -47,6 +46,7 @@
 #include <Scripting/NasalSys.hxx>
 
 #include "AIBase.hxx"
+#include "AIModelData.hxx"
 #include "AIManager.hxx"
 
 const double FGAIBase::e = 2.71828183;
@@ -157,16 +157,19 @@ void FGAIBase::Transform() {
 bool FGAIBase::init(bool search_in_AI_path) {
 
     if (!model_path.empty()) {
-
         if ( search_in_AI_path
                 && (model_path.substr(model_path.size() - 4, 4) == ".xml")) {
-            SGPath ai_path("AI");
+            SGPath ai_path(globals->get_fg_root());
+            ai_path.append("AI");
             ai_path.append(model_path);
-            try {
-                model = load3DModel( globals->get_fg_root(), ai_path.str(), props,
+            if(ai_path.exists())
+            {
+              try {
+                  model = load3DModel( globals->get_fg_root(), ai_path.str(), props,
                         globals->get_sim_time_sec() );
-            } catch (const sg_exception &e) {
-                model = NULL;
+              } catch (const sg_exception &e) {
+                  model = NULL;
+              }
             }
         } else
             model = NULL;
@@ -181,12 +184,29 @@ bool FGAIBase::init(bool search_in_AI_path) {
         }
 
     }
-
     if (model.get()) {
         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());
+
+    } else if (!model_path.empty()) {
+        SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load model " << model_path);
+    }
+
+    setDie(false);
+    return true;
+}
+
+void FGAIBase::initModel(osg::Node *node)
+{
+//  model->setNodeMask(model->getNodeMask() & ~SG_NODEMASK_TERRAIN_BIT);
+    if (model.get()) {
+//        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());
 
     } else if (!model_path.empty()) {
@@ -195,7 +215,6 @@ bool FGAIBase::init(bool search_in_AI_path) {
 
     props->setStringValue("submodels/path", _path.c_str());
     setDie(false);
-    return true;
 }
 
 
@@ -205,8 +224,7 @@ osg::Node* FGAIBase::load3DModel(const string& fg_root,
                       double sim_time_sec)
 {
   model = sgLoad3DModel(fg_root, path, prop_root, sim_time_sec, 0,
-                        new FGNasalModelData(prop_root));
-  model->setNodeMask(model->getNodeMask() & ~SG_NODEMASK_TERRAIN_BIT);
+                        new FGAIModelData(this, prop_root));
   return model.get();
 }
 
diff --git a/src/AIModel/AIBase.hxx b/src/AIModel/AIBase.hxx
index 0bdd6f8..3779057 100644
--- a/src/AIModel/AIBase.hxx
+++ b/src/AIModel/AIBase.hxx
@@ -54,6 +54,7 @@ public:
     virtual void readFromScenario(SGPropertyNode* scFileNode);
 
     virtual bool init(bool search_in_AI_path=false);
+    virtual void initModel(osg::Node *node);
     virtual void update(double dt);
     virtual void bind();
     virtual void unbind();
diff --git a/src/AIModel/AICarrier.cxx b/src/AIModel/AICarrier.cxx
index d3fa369..4c49c28 100644
--- a/src/AIModel/AICarrier.cxx
+++ b/src/AIModel/AICarrier.cxx
@@ -71,6 +71,7 @@ public:
         != mSolidObjects.end()) {
       mFoundHot = true;
       mUserData = FGAICarrierHardware::newSolid(mCarrier);
+      SG_LOG(SG_GENERAL, SG_ALERT, "AICarrierVisitor::apply() solidObject" );
     }
     node.setUserData(mUserData.get());
 
@@ -313,20 +314,6 @@ bool FGAICarrier::init(bool search_in_AI_path) {
     if (!FGAIShip::init(search_in_AI_path))
         return false;
 
-    // process the 3d model here
-    // mark some objects solid, mark the wires ...
-
-    // The model should be used for altitude computations.
-    // To avoid that every detail in a carrier 3D model will end into
-    // the aircraft local cache, only set the HOT traversal bit on
-    // selected objects.
-    osg::Node* sel = aip.getSceneGraph();
-    // Clear the HOT traversal flag
-    // Selectively set that flag again for wires/cats/solid objects.
-    // Attach a pointer to this carrier class to those objects.
-    FGCarrierVisitor carrierVisitor(this, wire_objects, catapult_objects, solid_objects);
-    sel->accept(carrierVisitor);
-
     _longitude_node = fgGetNode("/position/longitude-deg", true);
     _latitude_node = fgGetNode("/position/latitude-deg", true);
     _altitude_node = fgGetNode("/position/altitude-ft", true);
@@ -357,6 +344,28 @@ bool FGAICarrier::init(bool search_in_AI_path) {
     return true;
 }
 
+void FGAICarrier::initModel(osg::Node *node)
+{
+    SG_LOG(SG_GENERAL, SG_ALERT, "AICarrier::initModel()" );
+    FGAIShip::initModel(node);
+    // process the 3d model here
+    // mark some objects solid, mark the wires ...
+
+    // The model should be used for altitude computations.
+    // To avoid that every detail in a carrier 3D model will end into
+    // the aircraft local cache, only set the HOT traversal bit on
+    // selected objects.
+
+    // osg::Node* sel = aip.getSceneGraph();
+    // Clear the HOT traversal flag
+    // Selectively set that flag again for wires/cats/solid objects.
+    // Attach a pointer to this carrier class to those objects.
+    SG_LOG(SG_GENERAL, SG_ALERT, "AICarrier::initModel() visit" );
+    FGCarrierVisitor carrierVisitor(this, wire_objects, catapult_objects, solid_objects);
+    node->accept(carrierVisitor);
+
+}
+
 void FGAICarrier::bind() {
     FGAIShip::bind();
 
diff --git a/src/AIModel/AICarrier.hxx b/src/AIModel/AICarrier.hxx
index 33c6740..0e88ba4 100644
--- a/src/AIModel/AICarrier.hxx
+++ b/src/AIModel/AICarrier.hxx
@@ -103,6 +103,7 @@ public:
     bool OutsideBox();
 
     bool init(bool search_in_AI_path=false);
+    void initModel(osg::Node *node);
 
     virtual const char* getTypeString(void) const { return "carrier"; }
 
diff --git a/src/AIModel/Makefile.am b/src/AIModel/Makefile.am
index 6dbfd7b..350d69e 100644
--- a/src/AIModel/Makefile.am
+++ b/src/AIModel/Makefile.am
@@ -3,6 +3,7 @@ noinst_LIBRARIES = libAIModel.a
 libAIModel_a_SOURCES = submodel.cxx submodel.hxx 	\
 			AIManager.hxx AIManager.cxx \
 			AIBase.hxx AIBase.cxx 	\
+			AIModelData.cxx \
 			AIAircraft.hxx AIAircraft.cxx \
 			AIMultiplayer.hxx AIMultiplayer.cxx \
 			AIShip.hxx AIShip.cxx \
diff --git a/src/AIModel/submodel.cxx b/src/AIModel/submodel.cxx
index e246523..774820d 100644
--- a/src/AIModel/submodel.cxx
+++ b/src/AIModel/submodel.cxx
@@ -514,7 +514,7 @@ void FGSubmodelMgr::loadAI()
     sm_list = ai->get_ai_list();
 
     if (sm_list.empty()) {
-        SG_LOG(SG_GENERAL, SG_ALERT, "Submodels: Unable to read AI submodel list");
+        SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
         return;
     }
 
@@ -559,7 +559,7 @@ void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
                 "Submodels: Trying to read AI submodels file: " << config.str());
         readProperties(config.str(), &root);
     } catch (const sg_exception &e) {
-        SG_LOG(SG_GENERAL, SG_ALERT,
+        SG_LOG(SG_GENERAL, SG_DEBUG,
                 "Submodels: Unable to read AI submodels file: " << config.str());
         return;
     }
@@ -664,7 +664,7 @@ void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable)
         readProperties(config.str(), &root);
 
     } catch (const sg_exception &e) {
-        SG_LOG(SG_GENERAL, SG_ALERT,
+        SG_LOG(SG_GENERAL, SG_DEBUG,
                 "Submodels: Unable to read AI submodels file: " << config.str());
         return;
     }
diff --git a/src/Main/main.cxx b/src/Main/main.cxx
index d90002a..338b7fe 100644
--- a/src/Main/main.cxx
+++ b/src/Main/main.cxx
@@ -53,6 +53,7 @@
 #include <simgear/scene/material/matlib.hxx>
 #include <simgear/scene/model/animation.hxx>
 #include <simgear/scene/sky/sky.hxx>
+#include <simgear/scene/model/CheckSceneryVisitor.hxx>
 #include <Time/light.hxx>
 #include <Include/general.hxx>
 #include <Aircraft/replay.hxx>
@@ -137,12 +138,23 @@ void fgUpdateTimeDepCalcs() {
         // the aircraft.
         double range = 50.0;
         if (globals->get_tile_mgr()->scenery_available(lat, lon, range)) {
-            SG_LOG(SG_FLIGHT,SG_INFO, "Finally initializing fdm");
+
+            double alt = globals->get_current_view()->getSGLocation()->getAltitudeASL_ft();
+            double elev;
+            globals->get_scenery()->get_elevation_m(lat, lon, 9000, elev, 0);
+
+            SG_LOG(SG_FLIGHT,SG_ALERT, "elev=" << elev);
+            SGVec3f p = SGVec3f::fromGeod(SGGeod::fromDegM(lon,lat,elev));
+            simgear::CheckSceneryVisitor csnv(p.osg());
+            globals->get_scenery()->get_scene_graph()->accept(csnv);
+            if(csnv.isLoaded()) {
+            SG_LOG(SG_FLIGHT,SG_ALERT, "Finally initializing fdm");
             cur_fdm_state->init();
             if ( cur_fdm_state->get_bound() ) {
                 cur_fdm_state->unbind();
             }
             cur_fdm_state->bind();
+            }
         }
     }
 
diff --git a/src/Model/model_panel.cxx b/src/Model/model_panel.cxx
index ef98544..497a824 100644
--- a/src/Model/model_panel.cxx
+++ b/src/Model/model_panel.cxx
@@ -39,7 +39,7 @@ fgLoad3DModelPanel( const string &fg_root, const string &path,
                     SGPropertyNode *prop_root,
                     double sim_time_sec, const SGPath& livery )
 {
-  osg::Node* node = sgLoad3DModel( fg_root, path, prop_root, sim_time_sec,
+  osg::Node* node = sgLoad3DModel_internal( fg_root, path, prop_root, sim_time_sec,
                                    load_panel, 0, livery );
   node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
   return node;
diff --git a/src/MultiPlayer/multiplaymgr.cxx b/src/MultiPlayer/multiplaymgr.cxx
index d04dde9..f59b403 100644
--- a/src/MultiPlayer/multiplaymgr.cxx
+++ b/src/MultiPlayer/multiplaymgr.cxx
@@ -23,7 +23,7 @@
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
-// $Id: multiplaymgr.cxx,v 1.20 2008-02-29 16:13:02 mfranz Exp $
+// $Id: multiplaymgr.cxx,v 1.19 2007-12-08 09:49:16 mfranz Exp $
 //  
 //////////////////////////////////////////////////////////////////////
 
@@ -48,7 +48,7 @@
 
 // These constants are provided so that the ident 
 // command can list file versions
-const char sMULTIPLAYMGR_BID[] = "$Id: multiplaymgr.cxx,v 1.20 2008-02-29 16:13:02 mfranz Exp $";
+const char sMULTIPLAYMGR_BID[] = "$Id: multiplaymgr.cxx,v 1.19 2007-12-08 09:49:16 mfranz Exp $";
 const char sMULTIPLAYMGR_HID[] = MULTIPLAYTXMGR_HID;
 
 // A static map of protocol property id values to property paths,
@@ -148,7 +148,6 @@ FGMultiplayMgr::sIdPropertyList[] = {
   {1006, "controls/armament/station[0]/jettison-all",  SGPropertyNode::BOOL},
 
   {1100, "sim/model/variant", SGPropertyNode::INT},
-  {1101, "sim/model/livery/file", SGPropertyNode::STRING},
 
   {10001, "sim/multiplay/transmission-freq-hz",  SGPropertyNode::STRING},
   {10002, "sim/multiplay/chat",  SGPropertyNode::STRING},
-- 
1.5.2.5


