]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIManager.cxx
Make the scenarios work again.
[flightgear.git] / src / AIModel / AIManager.cxx
index 68f60474eff73af358504bb7b7dda33de33b7ecc..3c25f276e99bfacca2b297a3b9cf5fe89910432a 100644 (file)
@@ -1,5 +1,5 @@
 // AIManager.cxx  Based on David Luff's AIMgr:
-// - a global management class for AI objects
+// - a global management type for AI objects
 //
 // Written by David Culp, started October 2003.
 // - davidculp2@comcast.net
@@ -36,10 +36,12 @@ SG_USING_STD(list);
 
 FGAIManager::FGAIManager() {
   initDone = false;
-  numObjects = 0;
+  for (int i=0; i < FGAIBase::MAX_OBJECTS; i++)
+     numObjects[i] = 0;
   _dt = 0.0;
   dt_count = 9;
   scenario_filename = "";
+  ai_list.clear();
 }
 
 FGAIManager::~FGAIManager() {
@@ -49,89 +51,28 @@ FGAIManager::~FGAIManager() {
       ++ai_list_itr;
     }
   ai_list.clear();
-  ids.clear();
 }
 
 
 void FGAIManager::init() {
-  int rval;
   root = fgGetNode("sim/ai", true);
 
   enabled = root->getNode("enabled", true)->getBoolValue();
   if (!enabled)
       return;
 
-
-  wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
-
-  for (int i = 0; i < root->nChildren(); i++) {
-    const SGPropertyNode * entry = root->getChild(i);
-
-    if (!strcmp(entry->getName(), "scenario")){
-      scenario_filename = entry->getStringValue();
-    }
-
-    if (!strcmp(entry->getName(), "entry")) {
-      if (!strcmp(entry->getStringValue("type", ""), "aircraft")) { 
-
-        rval = createAircraft( entry->getStringValue("class", ""),
-                               entry->getStringValue("path"),
-                               entry->getDoubleValue("latitude"),
-                               entry->getDoubleValue("longitude"),
-                               entry->getDoubleValue("altitude-ft"),
-                               entry->getDoubleValue("heading"),
-                               entry->getDoubleValue("speed-KTAS"),
-                               0.0, 
-                               entry->getDoubleValue("bank") );
-
-      } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
-
-        rval = createShip( entry->getStringValue("path"),
-                           entry->getDoubleValue("latitude"),
-                           entry->getDoubleValue("longitude"),
-                           entry->getDoubleValue("altitude-ft"),
-                           entry->getDoubleValue("heading"),
-                           entry->getDoubleValue("speed-KTAS"),
-                           entry->getDoubleValue("rudder") );
-
-      } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
-
-        rval = createBallistic( entry->getStringValue("path"),
-                                entry->getDoubleValue("latitude"),
-                                entry->getDoubleValue("longitude"),
-                                entry->getDoubleValue("altitude-ft"),
-                                entry->getDoubleValue("azimuth"),
-                                entry->getDoubleValue("elevation"),
-                                entry->getDoubleValue("speed") );
-
-      } else if (!strcmp(entry->getStringValue("type", ""), "storm")) {
-
-        rval = createStorm( entry->getStringValue("path"),
-                            entry->getDoubleValue("latitude"),
-                            entry->getDoubleValue("longitude"),
-                            entry->getDoubleValue("altitude-ft"),
-                            entry->getDoubleValue("heading"),
-                            entry->getDoubleValue("speed-KTAS") );
-
-      } else if (!strcmp(entry->getStringValue("type", ""), "thermal")) {
-
-        rval = createThermal( entry->getDoubleValue("latitude"),
-                              entry->getDoubleValue("longitude"),
-                              entry->getDoubleValue("strength-fps"),
-                              entry->getDoubleValue("diameter-ft") );
-
-      }       
-    }
-  }
-
+  wind_from_down_node = fgGetNode("/environment/wind-from-down-fps", true);
+  scenario_filename = root->getNode("scenario", true)->getStringValue();
   if (scenario_filename != "") processScenario( scenario_filename );
+
   initDone = true;
 }
 
 
 void FGAIManager::bind() {
    root = globals->get_props()->getNode("ai/models", true);
-   root->tie("count", SGRawValuePointer<int>(&numObjects));
+   root->tie("count", SGRawValuePointer<int>(&numObjects[0]));
 }
 
 
@@ -154,9 +95,9 @@ void FGAIManager::update(double dt) {
         ai_list_itr = ai_list.begin();
         while(ai_list_itr != ai_list.end()) {
                 if ((*ai_list_itr)->getDie()) {      
-                   freeID((*ai_list_itr)->getID());
+                   --numObjects[(*ai_list_itr)->getType()];
+                   --numObjects[0];
                    delete (*ai_list_itr);
-                   --numObjects;
                    if ( ai_list_itr == ai_list.begin() ) {
                        ai_list.erase(ai_list_itr);
                        ai_list_itr = ai_list.begin();
@@ -174,187 +115,145 @@ void FGAIManager::update(double dt) {
                 }
                 ++ai_list_itr;
         }
-        wind_from_down->setDoubleValue( strength );
-}
+        wind_from_down_node->setDoubleValue( strength );
 
-
-// This function returns the next available ID
-int FGAIManager::assignID() {
-  int maxint = 30000;
-  int x; 
-  bool used;
-  for (x=0; x<maxint; x++) {
-     used = false;
-     id_itr = ids.begin();
-     while( id_itr != ids.end() ) {
-       if ((*id_itr) == x) used = true;
-       ++id_itr;
-     }
-     if (!used) {
-       ids.push_back(x);
-       return x;
-     } 
-  }
-  return -1;  // no available ID's
 }
 
 
-// This function removes an ID from the ID array, making it
-// available for assignment to another AI object
-void FGAIManager::freeID( int ID ) {
-    id_itr = ids.begin();
-    while( id_itr != ids.end() ) {
-      if (*id_itr == ID) {
-        ids.erase( id_itr );
-        return;
-      }
-      ++id_itr;
-    }  
-}
-
-int FGAIManager::createAircraft( string model_class, string path,
-              double latitude, double longitude, double altitude,
-              double heading, double speed, double pitch, double roll ) {
+void*
+FGAIManager::createAircraft( FGAIModelEntity *entity ) {
      
         FGAIAircraft* ai_plane = new FGAIAircraft(this);
         ai_list.push_back(ai_plane);
-        ai_plane->setID( assignID() );
-        ++numObjects;
-        if (model_class == "light") {
+        ++numObjects[0];
+        ++numObjects[FGAIBase::otAircraft];
+        if (entity->m_class == "light") {
           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
-        } else if (model_class == "ww2_fighter") {
+        } else if (entity->m_class == "ww2_fighter") {
           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
-        } else if (model_class ==  "jet_transport") {
+        } else if (entity->m_class ==  "jet_transport") {
           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
-        } else if (model_class == "jet_fighter") {
+        } else if (entity->m_class == "jet_fighter") {
           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
-        } else {
-          ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
-        }
-        ai_plane->setHeading(heading);
-        ai_plane->setSpeed(speed);
-        ai_plane->setPath(path.c_str());
-        ai_plane->setAltitude(altitude);
-        ai_plane->setLongitude(longitude);
-        ai_plane->setLatitude(latitude);
-        ai_plane->setBank(roll);
-        ai_plane->init();
-        ai_plane->bind();
-        return ai_plane->getID();
-}
-
-
-int FGAIManager::createAircraft( string model_class, string path,
-              FGAIFlightPlan* flightplan ) {
-     
-        FGAIAircraft* ai_plane = new FGAIAircraft(this);
-        ai_list.push_back(ai_plane);
-        ai_plane->setID( assignID() );
-        ++numObjects;
-        if (model_class == "light") {
-          ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
-        } else if (model_class == "ww2_fighter") {
-          ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
-        } else if (model_class ==  "jet_transport") {
+        } else if (entity->m_class ==  "tanker") {
           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
-        } else if (model_class == "jet_fighter") {
-          ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
+          ai_plane->SetTanker(true);
         } else {
           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
         }
-        ai_plane->setPath(path.c_str());
-        ai_plane->SetFlightPlan(flightplan);
+        ai_plane->setHeading(entity->heading);
+        ai_plane->setSpeed(entity->speed);
+        ai_plane->setPath(entity->path);
+        ai_plane->setAltitude(entity->altitude);
+        ai_plane->setLongitude(entity->longitude);
+        ai_plane->setLatitude(entity->latitude);
+        ai_plane->setBank(entity->roll);
+
+        if ( entity->fp ) {
+          ai_plane->SetFlightPlan(entity->fp);
+        }
+
         ai_plane->init();
         ai_plane->bind();
-        return ai_plane->getID();
+        return ai_plane;
 }
 
-
-int FGAIManager::createShip( string path, double latitude, double longitude,
-                             double altitude, double heading, double speed,
-                             double rudder ) {
+void*
+FGAIManager::createShip( FGAIModelEntity *entity ) {
 
         FGAIShip* ai_ship = new FGAIShip(this);
         ai_list.push_back(ai_ship);
-        ai_ship->setID( assignID() );
-        ++numObjects;
-        ai_ship->setHeading(heading);
-        ai_ship->setSpeed(speed);
-        ai_ship->setPath(path.c_str());
-        ai_ship->setAltitude(altitude);
-        ai_ship->setLongitude(longitude);
-        ai_ship->setLatitude(latitude);
-        ai_ship->setBank(rudder);
+        ++numObjects[0];
+        ++numObjects[FGAIBase::otShip];
+        ai_ship->setHeading(entity->heading);
+        ai_ship->setSpeed(entity->speed);
+        ai_ship->setPath(entity->path);
+        ai_ship->setAltitude(entity->altitude);
+        ai_ship->setLongitude(entity->longitude);
+        ai_ship->setLatitude(entity->latitude);
+        ai_ship->setBank(entity->rudder);
+
+        if ( entity->fp ) {
+           ai_ship->setFlightPlan(entity->fp);
+        }
+
         ai_ship->init();
         ai_ship->bind();
-        return ai_ship->getID();
+        return ai_ship;
 }
 
-
-int FGAIManager::createBallistic( string path, double latitude, double longitude,
-                                  double altitude, double azimuth, double elevation,
-                                  double speed ) {
+void*
+FGAIManager::createBallistic( FGAIModelEntity *entity ) {
 
         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
         ai_list.push_back(ai_ballistic);
-        ai_ballistic->setID( assignID() );    
-        ++numObjects;
-        ai_ballistic->setAzimuth(azimuth);
-        ai_ballistic->setElevation(elevation);
-        ai_ballistic->setSpeed(speed);
-        ai_ballistic->setPath(path.c_str());
-        ai_ballistic->setAltitude(altitude);
-        ai_ballistic->setLongitude(longitude);
-        ai_ballistic->setLatitude(latitude);
+        ++numObjects[0];
+        ++numObjects[FGAIBase::otBallistic];
+        ai_ballistic->setAzimuth(entity->azimuth);
+        ai_ballistic->setElevation(entity->elevation);
+        ai_ballistic->setSpeed(entity->speed);
+        ai_ballistic->setPath(entity->path);
+        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->setRoll(entity->roll);
+               ai_ballistic->setCd(entity->cd);
+               ai_ballistic->setWeight(entity->weight);
         ai_ballistic->init();
         ai_ballistic->bind();
-        return ai_ballistic->getID();
+        return ai_ballistic;
 }
 
-int FGAIManager::createStorm( string path, double latitude, double longitude,
-                             double altitude, double heading, double speed ) {
+void*
+FGAIManager::createStorm( FGAIModelEntity *entity ) {
 
         FGAIStorm* ai_storm = new FGAIStorm(this);
         ai_list.push_back(ai_storm);
-        ai_storm->setID( assignID() );
-        ++numObjects;
-        ai_storm->setHeading(heading);
-        ai_storm->setSpeed(speed);
-        ai_storm->setPath(path.c_str());
-        ai_storm->setAltitude(altitude);
-        ai_storm->setLongitude(longitude);
-        ai_storm->setLatitude(latitude);
+        ++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->setLongitude(entity->longitude);
+        ai_storm->setLatitude(entity->latitude);
         ai_storm->init();
         ai_storm->bind();
-        return ai_storm->getID();
+        return ai_storm;
 }
 
-int FGAIManager::createThermal( double latitude, double longitude,
-                                double strength, double diameter ) {
+void*
+FGAIManager::createThermal( FGAIModelEntity *entity ) {
 
         FGAIThermal* ai_thermal = new FGAIThermal(this);
         ai_list.push_back(ai_thermal);
-        ai_thermal->setID( assignID() );
-        ++numObjects;
-        ai_thermal->setLongitude(longitude);
-        ai_thermal->setLatitude(latitude);
-        ai_thermal->setMaxStrength(strength);
-        ai_thermal->setDiameter(diameter / 6076.11549);
+        ++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->init();
         ai_thermal->bind();
-        return ai_thermal->getID();
+        return ai_thermal;
 }
 
-void FGAIManager::destroyObject( int ID ) {
+void FGAIManager::destroyObject( void* ID ) {
         ai_list_itr = ai_list.begin();
         while(ai_list_itr != ai_list.end()) {
             if ((*ai_list_itr)->getID() == ID) {
-              freeID( ID );
+              --numObjects[0];
+              --numObjects[(*ai_list_itr)->getType()];
               delete (*ai_list_itr);
               ai_list.erase(ai_list_itr);
-              --ai_list_itr;
-              --numObjects;
-              return;
+
+              break;
             }
             ++ai_list_itr;
         }
@@ -387,15 +286,31 @@ void FGAIManager::processThermal( FGAIThermal* thermal ) {
 
 
 void FGAIManager::processScenario( string filename ) {
-  //cout << "AIManager: creating a scenario." << endl;
   FGAIScenario* s = new FGAIScenario( filename );
   for (int i=0;i<s->nEntries();i++) {
-    FGAIScenario::entry* en = s->getNextEntry();
+    FGAIModelEntity* en = s->getNextEntry();
+
     if (en) {
-      FGAIFlightPlan* f = new FGAIFlightPlan( en->flightplan );
-      createAircraft("jet_transport", "Aircraft/737/Models/boeing733.xml", f);
+      if (en->m_type == "aircraft") {
+         createAircraft( en );
+
+      } else if (en->m_type == "ship") {
+           createShip( en );
+
+      } else if (en->m_type == "storm") {
+        createStorm( en );
+
+      } else if (en->m_type == "thermal") {
+        createThermal( en );
+
+      } else if (en->m_type == "ballistic") {
+        createBallistic( en );
+      }      
     }
   }
+
   delete s;
 }
 
+//end AIManager.cxx