]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIManager.cxx
Interim windows build fix
[flightgear.git] / src / AIModel / AIManager.cxx
index 18d1d648a8788d5741bcfcc56f2a47a93bd141e8..4faf23e591e5887ffb0937ad15f276d40028e67c 100644 (file)
@@ -32,6 +32,7 @@
 #include <boost/foreach.hpp>
 
 #include <Main/globals.hxx>
+#include <Main/fg_props.hxx>
 #include <Airports/airport.hxx>
 #include <Scripting/NasalSys.hxx>
 
@@ -85,6 +86,9 @@ public:
         }
         
         FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
+        if (!nasalSys)
+            return;
+        
         std::string moduleName = "scenario_" + _internalName;
         if (!_unloadScript.empty()) {
             nasalSys->createModule(moduleName.c_str(), moduleName.c_str(),
@@ -127,11 +131,11 @@ FGAIManager::init() {
     wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
 
     user_altitude_agl_node  = fgGetNode("/position/altitude-agl-ft", true);
-    user_yaw_node       = fgGetNode("/orientation/side-slip-deg", true);
     user_speed_node     = fgGetNode("/velocities/uBody-fps", true);
     
     globals->get_commands()->addCommand("load-scenario", this, &FGAIManager::loadScenarioCommand);
     globals->get_commands()->addCommand("unload-scenario", this, &FGAIManager::unloadScenarioCommand);
+    _environmentVisiblity = fgGetNode("/environment/visibility-m");
 }
 
 void
@@ -176,6 +180,16 @@ void
 FGAIManager::shutdown()
 {
     unloadAllScenarios();
+    
+    BOOST_FOREACH(FGAIBase* ai, ai_list) {
+        ai->unbind();
+    }
+    
+    ai_list.clear();
+    _environmentVisiblity.clear();
+    
+    globals->get_commands()->removeCommand("load-scenario");
+    globals->get_commands()->removeCommand("unload-scenario");
 }
 
 void
@@ -287,8 +301,14 @@ FGAIManager::attach(FGAIBase *model)
     p->setBoolValue("valid", true);
 }
 
+bool FGAIManager::isVisible(const SGGeod& pos) const
+{
+  double visibility_meters = _environmentVisiblity->getDoubleValue();
+  return ( dist(globals->get_view_position_cart(), SGVec3d::fromGeod(pos)) ) <= visibility_meters;
+}
+
 int
-FGAIManager::getNumAiObjects(void) const
+FGAIManager::getNumAiObjects() const
 {
     return ai_list.size();
 }
@@ -296,9 +316,7 @@ FGAIManager::getNumAiObjects(void) const
 void
 FGAIManager::fetchUserState( void ) {
 
-    user_yaw       = user_yaw_node->getDoubleValue();
     globals->get_aircraft_orientation(user_heading, user_pitch, user_roll);
-
     user_speed     = user_speed_node->getDoubleValue() * 0.592484;
     wind_from_east = wind_from_east_node->getDoubleValue();
     wind_from_north   = wind_from_north_node->getDoubleValue();
@@ -391,7 +409,14 @@ FGAIBasePtr FGAIManager::addObject(const SGPropertyNode* definition)
     }
 
     ai->readFromScenario(const_cast<SGPropertyNode*>(definition));
-    attach(ai);
+       if((ai->isValid())){
+               attach(ai);
+        SG_LOG(SG_AI, SG_DEBUG, "attached scenario " << ai->_getName());
+       }
+       else{
+               ai->setDie(true);
+               SG_LOG(SG_AI, SG_ALERT, "killed invalid scenario "  << ai->_getName());
+       }
     return ai;
 }
 
@@ -408,6 +433,17 @@ bool FGAIManager::removeObject(const SGPropertyNode* args)
     return false;
 }
 
+FGAIBasePtr FGAIManager::getObjectFromProperty(const SGPropertyNode* aProp) const
+{
+    BOOST_FOREACH(FGAIBase* ai, get_ai_list()) {
+        if (ai->_getProps() == aProp) {
+            return ai;
+        }
+    } // of AI objects iteration
+    
+    return NULL;
+}
+
 bool
 FGAIManager::loadScenario( const string &filename )
 {