]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIManager.cxx
Durk Talsma:
[flightgear.git] / src / AIModel / AIManager.cxx
index 97769619c9224996f43cd9641405db6eb8fdedcc..96742864e3fb574593e04a542ef3fa9ee0b84cd3 100644 (file)
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
 
+#include <Airports/simple.hxx>
+#include <Traffic/SchedFlight.hxx>
+#include <Traffic/Schedule.hxx>
+#include <Traffic/TrafficMgr.hxx>
+
 #include <list>
 
 #include "AIManager.hxx"
@@ -30,6 +35,8 @@
 #include "AIBallistic.hxx"
 #include "AIStorm.hxx"
 #include "AIThermal.hxx"
+#include "AICarrier.hxx"
+#include "AIStatic.hxx"
 
 SG_USING_STD(list);
 
@@ -45,12 +52,18 @@ FGAIManager::FGAIManager() {
 }
 
 FGAIManager::~FGAIManager() {
-  ai_list_itr = ai_list.begin();
+  ai_list_iterator ai_list_itr = ai_list.begin();
   while(ai_list_itr != ai_list.end()) {
+      (*ai_list_itr)->unbind();
       delete (*ai_list_itr);
       ++ai_list_itr;
     }
   ai_list.clear();
+  ModelVecIterator i = loadedModels.begin();
+  while (i != loadedModels.end())
+    {
+      i->getModelId()->deRef();
+    }
 }
 
 
@@ -62,14 +75,33 @@ void FGAIManager::init() {
       return;
 
   wind_from_down_node = fgGetNode("/environment/wind-from-down-fps", true);
-  scenario_filename = root->getNode("scenario", true)->getStringValue();
-
-  if (scenario_filename != "") processScenario( scenario_filename );
+  wind_from_east_node  = fgGetNode("/environment/wind-from-east-fps",true);
+  wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
+
+  user_latitude_node  = fgGetNode("/position/latitude-deg", true);
+  user_longitude_node = fgGetNode("/position/longitude-deg", true);
+  user_altitude_node  = fgGetNode("/position/altitude-ft", true);
+  user_heading_node   = fgGetNode("/orientation/heading-deg", true);
+  user_pitch_node     = fgGetNode("/orientation/pitch-deg", true);
+  user_yaw_node       = fgGetNode("/orientation/side-slip-deg", true);
+  user_speed_node     = fgGetNode("/velocities/uBody-fps", true);
+
+  for(int i = 0 ; i < root->nChildren() ; i++) {
+    SGPropertyNode *aiEntry = root->getChild( i );
+       if( !strcmp( aiEntry->getName(), "scenario" ) ) {
+      scenario_filename = aiEntry->getStringValue();
+      if (scenario_filename != "") processScenario( scenario_filename );
+       }
+  }
   initDone = true;
 }
 
 
+void FGAIManager::reinit() {
+   update(0.0);
+}
+
+
 void FGAIManager::bind() {
    root = globals->get_props()->getNode("ai/models", true);
    root->tie("count", SGRawValuePointer<int>(&numObjects[0]));
@@ -86,18 +118,21 @@ void FGAIManager::update(double dt) {
         // initialize these for finding nearest thermals
         range_nearest = 10000.0;
         strength = 0.0;
+       FGTrafficManager *tmgr = (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
 
         if (!enabled)
             return;
 
-        _dt = dt;      
+        _dt = dt;
 
-        ai_list_itr = ai_list.begin();
+        ai_list_iterator ai_list_itr = ai_list.begin();
         while(ai_list_itr != ai_list.end()) {
                 if ((*ai_list_itr)->getDie()) {      
-                   delete (*ai_list_itr);
+                 tmgr->release((*ai_list_itr)->getID());
                    --numObjects[(*ai_list_itr)->getType()];
                    --numObjects[0];
+                   (*ai_list_itr)->unbind();
+                   delete (*ai_list_itr);
                    if ( ai_list_itr == ai_list.begin() ) {
                        ai_list.erase(ai_list_itr);
                        ai_list_itr = ai_list.begin();
@@ -115,15 +150,15 @@ void FGAIManager::update(double dt) {
                 }
                 ++ai_list_itr;
         }
-        wind_from_down_node->setDoubleValue( strength );
+        wind_from_down_node->setDoubleValue( strength ); // for thermals
 
 }
 
 
 void*
-FGAIManager::createAircraft( FGAIModelEntity *entity ) {
+FGAIManager::createAircraft( FGAIModelEntity *entity,   FGAISchedule *ref) {
      
-        FGAIAircraft* ai_plane = new FGAIAircraft(this);
+        FGAIAircraft* ai_plane = new FGAIAircraft(this, ref);
         ai_list.push_back(ai_plane);
         ++numObjects[0];
         ++numObjects[FGAIBase::otAircraft];
@@ -141,9 +176,11 @@ FGAIManager::createAircraft( FGAIModelEntity *entity ) {
         } else {
           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
         }
+        ai_plane->setAcType(entity->acType);
+        ai_plane->setCompany(entity->company);
         ai_plane->setHeading(entity->heading);
         ai_plane->setSpeed(entity->speed);
-        ai_plane->setPath(entity->path);
+        ai_plane->setPath(entity->path.c_str());
         ai_plane->setAltitude(entity->altitude);
         ai_plane->setLongitude(entity->longitude);
         ai_plane->setLatitude(entity->latitude);
@@ -152,7 +189,9 @@ FGAIManager::createAircraft( FGAIModelEntity *entity ) {
         if ( entity->fp ) {
           ai_plane->SetFlightPlan(entity->fp);
         }
-
+        if (entity->repeat) {
+          ai_plane->GetFlightPlan()->setRepeat(true);
+        }
         ai_plane->init();
         ai_plane->bind();
         return ai_plane;
@@ -160,6 +199,8 @@ FGAIManager::createAircraft( FGAIModelEntity *entity ) {
 
 void*
 FGAIManager::createShip( FGAIModelEntity *entity ) {
+    
+     // cout << "creating ship" << endl;    
 
         FGAIShip* ai_ship = new FGAIShip(this);
         ai_list.push_back(ai_ship);
@@ -167,11 +208,12 @@ FGAIManager::createShip( FGAIModelEntity *entity ) {
         ++numObjects[FGAIBase::otShip];
         ai_ship->setHeading(entity->heading);
         ai_ship->setSpeed(entity->speed);
-        ai_ship->setPath(entity->path);
+        ai_ship->setPath(entity->path.c_str());
         ai_ship->setAltitude(entity->altitude);
         ai_ship->setLongitude(entity->longitude);
         ai_ship->setLatitude(entity->latitude);
-        ai_ship->setBank(entity->rudder);
+        ai_ship->setRudder(entity->rudder);
+        ai_ship->setName(entity->name);
 
         if ( entity->fp ) {
            ai_ship->setFlightPlan(entity->fp);
@@ -182,6 +224,48 @@ FGAIManager::createShip( FGAIModelEntity *entity ) {
         return ai_ship;
 }
 
+void*
+FGAIManager::createCarrier( FGAIModelEntity *entity ) {
+    
+    // cout << "creating carrier" << endl;
+
+        FGAICarrier* ai_carrier = new FGAICarrier(this);
+        ai_list.push_back(ai_carrier);
+        ++numObjects[0];
+        ++numObjects[FGAIBase::otCarrier];
+        ai_carrier->setHeading(entity->heading);
+        ai_carrier->setSpeed(entity->speed);
+        ai_carrier->setPath(entity->path.c_str());
+        ai_carrier->setAltitude(entity->altitude);
+        ai_carrier->setLongitude(entity->longitude);
+        ai_carrier->setLatitude(entity->latitude);
+        ai_carrier->setRudder(entity->rudder);
+        ai_carrier->setSolidObjects(entity->solid_objects);
+        ai_carrier->setWireObjects(entity->wire_objects);
+        ai_carrier->setCatapultObjects(entity->catapult_objects);
+        ai_carrier->setParkingPositions(entity->ppositions);
+        ai_carrier->setRadius(entity->radius);
+        ai_carrier->setSign(entity->pennant_number);
+        ai_carrier->setName(entity->name);
+        ai_carrier->setFlolsOffset(entity->flols_offset);
+        ai_carrier->setWind_from_east(entity->wind_from_east);
+        ai_carrier->setWind_from_north(entity->wind_from_north);
+        ai_carrier->setTACANChannelID(entity->TACAN_channel_ID);
+        ai_carrier->setMaxLat(entity->max_lat);
+        ai_carrier->setMinLat(entity->min_lat);
+        ai_carrier->setMaxLong(entity->max_long);
+        ai_carrier->setMinLong(entity->min_long);
+        
+
+        if ( entity->fp ) {
+           ai_carrier->setFlightPlan(entity->fp);
+        }
+
+        ai_carrier->init();
+        ai_carrier->bind();
+        return ai_carrier;
+}
+
 void*
 FGAIManager::createBallistic( FGAIModelEntity *entity ) {
 
@@ -192,16 +276,20 @@ FGAIManager::createBallistic( FGAIModelEntity *entity ) {
         ai_ballistic->setAzimuth(entity->azimuth);
         ai_ballistic->setElevation(entity->elevation);
         ai_ballistic->setSpeed(entity->speed);
-        ai_ballistic->setPath(entity->path);
+        ai_ballistic->setPath(entity->path.c_str());
         ai_ballistic->setAltitude(entity->altitude);
         ai_ballistic->setLongitude(entity->longitude);
         ai_ballistic->setLatitude(entity->latitude);
         ai_ballistic->setDragArea(entity->eda);
         ai_ballistic->setLife(entity->life);
         ai_ballistic->setBuoyancy(entity->buoyancy);
-       ai_ballistic->setWind_from_east(entity->wind_from_east);
-       ai_ballistic->setWind_from_north(entity->wind_from_north);
-       ai_ballistic->setWind(entity->wind);
+        ai_ballistic->setWind_from_east(entity->wind_from_east);
+        ai_ballistic->setWind_from_north(entity->wind_from_north);
+        ai_ballistic->setWind(entity->wind);
+        ai_ballistic->setRoll(entity->roll);
+        ai_ballistic->setCd(entity->cd);
+        ai_ballistic->setMass(entity->mass);
+        ai_ballistic->setStabilisation(entity->aero_stabilised);
         ai_ballistic->init();
         ai_ballistic->bind();
         return ai_ballistic;
@@ -211,17 +299,20 @@ void*
 FGAIManager::createStorm( FGAIModelEntity *entity ) {
 
         FGAIStorm* ai_storm = new FGAIStorm(this);
-        ai_list.push_back(ai_storm);
         ++numObjects[0];
         ++numObjects[FGAIBase::otStorm];
         ai_storm->setHeading(entity->heading);
         ai_storm->setSpeed(entity->speed);
-        ai_storm->setPath(entity->path);
-        ai_storm->setAltitude(entity->altitude);
+        ai_storm->setPath(entity->path.c_str());
+        ai_storm->setAltitude(entity->altitude); 
+        ai_storm->setDiameter(entity->diameter / 6076.11549); 
+        ai_storm->setHeight(entity->height_msl); 
+        ai_storm->setStrengthNorm(entity->strength); 
         ai_storm->setLongitude(entity->longitude);
         ai_storm->setLatitude(entity->latitude);
         ai_storm->init();
         ai_storm->bind();
+        ai_list.push_back(ai_storm);
         return ai_storm;
 }
 
@@ -229,24 +320,45 @@ void*
 FGAIManager::createThermal( FGAIModelEntity *entity ) {
 
         FGAIThermal* ai_thermal = new FGAIThermal(this);
-        ai_list.push_back(ai_thermal);
         ++numObjects[0];
         ++numObjects[FGAIBase::otThermal];
         ai_thermal->setLongitude(entity->longitude);
         ai_thermal->setLatitude(entity->latitude);
         ai_thermal->setMaxStrength(entity->strength);
         ai_thermal->setDiameter(entity->diameter / 6076.11549);
+        ai_thermal->setHeight(entity->height_msl);
         ai_thermal->init();
         ai_thermal->bind();
+        ai_list.push_back(ai_thermal);
         return ai_thermal;
 }
 
-void FGAIManager::destroyObject( void* ID ) {
-        ai_list_itr = ai_list.begin();
+void*
+FGAIManager::createStatic( FGAIModelEntity *entity ) {
+     
+     // cout << "creating static object" << endl;    
+
+        FGAIStatic* ai_static = new FGAIStatic(this);
+        ai_list.push_back(ai_static);
+        ++numObjects[0];
+        ++numObjects[FGAIBase::otStatic];
+        ai_static->setHeading(entity->heading);
+        ai_static->setPath(entity->path.c_str());
+        ai_static->setAltitude(entity->altitude);
+        ai_static->setLongitude(entity->longitude);
+        ai_static->setLatitude(entity->latitude);
+        ai_static->init();
+        ai_static->bind();
+        return ai_static;
+}
+
+void FGAIManager::destroyObject( int ID ) {
+        ai_list_iterator ai_list_itr = ai_list.begin();
         while(ai_list_itr != ai_list.end()) {
             if ((*ai_list_itr)->getID() == ID) {
               --numObjects[0];
               --numObjects[(*ai_list_itr)->getType()];
+              (*ai_list_itr)->unbind();
               delete (*ai_list_itr);
               ai_list.erase(ai_list_itr);
 
@@ -256,19 +368,19 @@ void FGAIManager::destroyObject( void* ID ) {
         }
 }
 
-// fetch the user's state every 10 sim cycles
+
 void FGAIManager::fetchUserState( void ) {
-   ++dt_count;
-   if (dt_count == 10) {
-     user_latitude  = fgGetDouble("/position/latitude-deg");
-     user_longitude = fgGetDouble("/position/longitude-deg");
-     user_altitude  = fgGetDouble("/position/altitude-ft");
-     user_heading   = fgGetDouble("/orientation/heading-deg");
-     user_pitch     = fgGetDouble("/orientation/pitch-deg");
-     user_yaw       = fgGetDouble("/orientation/side-slip-deg");
-     user_speed     = fgGetDouble("/velocities/uBody-fps") * 0.592484;
-     dt_count = 0;
-   }
+     user_latitude  = user_latitude_node->getDoubleValue();
+     user_longitude = user_longitude_node->getDoubleValue();
+     user_altitude  = user_altitude_node->getDoubleValue();
+     user_heading   = user_heading_node->getDoubleValue();
+     user_pitch     = user_pitch_node->getDoubleValue();
+     user_yaw       = user_yaw_node->getDoubleValue();
+     user_speed     = user_speed_node->getDoubleValue() * 0.592484;
+     wind_from_east = wind_from_east_node->getDoubleValue();
+     wind_from_north = wind_from_north_node->getDoubleValue();
+     
+     
 }
 
 
@@ -282,36 +394,102 @@ void FGAIManager::processThermal( FGAIThermal* thermal ) {
 }
 
 
-void FGAIManager::processScenario( string filename ) {
+void FGAIManager::processScenario( const string &filename ) {
   FGAIScenario* s = new FGAIScenario( filename );
-
   for (int i=0;i<s->nEntries();i++) {
     FGAIModelEntity* en = s->getNextEntry();
 
     if (en) {
-      en->fp = NULL;
-      if (en->flightplan != ""){
-        en->fp = new FGAIFlightPlan( en->flightplan );
-      }
-      if (en->m_class == "aircraft") {
-         createAircraft( en );
+      if ( en->m_type == "aircraft") {
+        createAircraft( en );
 
-      } else if (en->m_class == "ship") {
-           createShip( en );
+      } else if ( en->m_type == "ship") {
+        createShip( en );
 
-      } else if (en->m_class == "storm") {
+      } else if ( en->m_type == "carrier") {
+        createCarrier( en );
+
+      } else if ( en->m_type == "thunderstorm") {
         createStorm( en );
 
-      } else if (en->m_class == "thermal") {
+      } else if ( en->m_type == "thermal") {
         createThermal( en );
 
-      } else if (en->m_class == "ballistic") {
+      } else if ( en->m_type == "ballistic") {
         createBallistic( en );
-      }      
+
+      } else if ( en->m_type == "static") {
+        createStatic( en );
+      }            
     }
   }
 
   delete s;
 }
 
+// This code keeps track of models that have already been loaded
+// Eventually we'd prbably need to find a way to keep track of models
+// that are unloaded again
+ssgBranch * FGAIManager::getModel(const string& path)
+{
+  ModelVecIterator i = loadedModels.begin();
+  while (i != loadedModels.end())
+    {
+      if (i->getPath() == path)
+       return i->getModelId();
+      i++;
+    }
+  return 0;
+}
+
+void FGAIManager::setModel(const string& path, ssgBranch *model)
+{
+  loadedModels.push_back(FGModelID(path,model));
+}
+
+bool FGAIManager::getStartPosition(const string& id, const string& pid,
+                                   Point3D& geodPos, double& heading,
+                                   sgdVec3 uvw)
+{
+  SGPropertyNode* root = fgGetNode("sim/ai", true);
+  if (!root->getNode("enabled", true)->getBoolValue())
+      return 0;
+
+  bool found = false;
+  for(int i = 0 ; (!found) && i < root->nChildren() ; i++) {
+    SGPropertyNode *aiEntry = root->getChild( i );
+       if( !strcmp( aiEntry->getName(), "scenario" ) ) {
+        string filename = aiEntry->getStringValue();
+        FGAIScenario* s = new FGAIScenario( filename );
+        for (int i=0; i<s->nEntries(); i++) {
+            FGAIModelEntity* en = s->getNextEntry();
+            if (en && en->m_type == "carrier" &&
+                (en->pennant_number == id || en->name == id)) {
+            FGAICarrier* ai_carrier = new FGAICarrier(0);
+            ai_carrier->setHeading(en->heading);
+            ai_carrier->setSpeed(en->speed);
+            ai_carrier->setAltitude(en->altitude);
+            ai_carrier->setLongitude(en->longitude);
+            ai_carrier->setLatitude(en->latitude);
+            ai_carrier->setBank(en->rudder);
+            ai_carrier->setParkingPositions(en->ppositions);
+      ai_carrier->setWind_from_east(en->wind_from_east);
+      ai_carrier->setWind_from_north(en->wind_from_north);
+      //ai_carrier->setTACANFreq(en->TACAN_freq);
+
+            if (ai_carrier->getParkPosition(pid, geodPos, heading, uvw)) {
+                delete ai_carrier;
+                found = true;
+                break;
+            }
+
+            delete ai_carrier;
+        }
+      }
+      delete s;
+    }
+  }
+  return found;
+}
+
+//end AIManager.cxx