]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIManager.cxx
Make the scenerio's work again (now for real) and a small number of updates.
[flightgear.git] / src / AIModel / AIManager.cxx
index 3cc50714ab6e3892b9981b95259e0bcfb8739f1d..e066b5a09135417219599e94b6550238c14cf843 100644 (file)
@@ -36,22 +36,22 @@ 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();
-  ids.clear();
 }
 
 FGAIManager::~FGAIManager() {
   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();
-  ids.clear();
 }
 
 
@@ -65,15 +65,15 @@ void FGAIManager::init() {
   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]));
 }
 
 
@@ -91,14 +91,15 @@ void FGAIManager::update(double dt) {
         if (!enabled)
             return;
 
-        _dt = dt;      
+        _dt = 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];
+                   (*ai_list_itr)->unbind();
                    delete (*ai_list_itr);
-                   --numObjects;
                    if ( ai_list_itr == ai_list.begin() ) {
                        ai_list.erase(ai_list_itr);
                        ai_list_itr = ai_list.begin();
@@ -121,46 +122,13 @@ void FGAIManager::update(double dt) {
 }
 
 
-// This function returns the next available ID
-int FGAIManager::assignID() {
-  int maxint = 30000;
-  int x; 
-  bool used;
-  for (x=1; 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( FGAIModelEntity *entity ) {
+void*
+FGAIManager::createAircraft( FGAIModelEntity *entity ) {
      
         FGAIAircraft* ai_plane = new FGAIAircraft(this);
         ai_list.push_back(ai_plane);
-        ai_plane->setID( assignID() );
-        ++numObjects;
+        ++numObjects[0];
+        ++numObjects[FGAIBase::otAircraft];
         if (entity->m_class == "light") {
           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
         } else if (entity->m_class == "ww2_fighter") {
@@ -189,15 +157,16 @@ int FGAIManager::createAircraft( FGAIModelEntity *entity ) {
 
         ai_plane->init();
         ai_plane->bind();
-        return ai_plane->getID();
+        return ai_plane;
 }
 
-int FGAIManager::createShip( FGAIModelEntity *entity ) {
+void*
+FGAIManager::createShip( FGAIModelEntity *entity ) {
 
         FGAIShip* ai_ship = new FGAIShip(this);
         ai_list.push_back(ai_ship);
-        ai_ship->setID( assignID() );
-        ++numObjects;
+        ++numObjects[0];
+        ++numObjects[FGAIBase::otShip];
         ai_ship->setHeading(entity->heading);
         ai_ship->setSpeed(entity->speed);
         ai_ship->setPath(entity->path);
@@ -212,15 +181,16 @@ int FGAIManager::createShip( FGAIModelEntity *entity ) {
 
         ai_ship->init();
         ai_ship->bind();
-        return ai_ship->getID();
+        return ai_ship;
 }
 
-int FGAIManager::createBallistic( FGAIModelEntity *entity ) {
+void*
+FGAIManager::createBallistic( FGAIModelEntity *entity ) {
 
         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
         ai_list.push_back(ai_ballistic);
-        ai_ballistic->setID( assignID() );    
-        ++numObjects;
+        ++numObjects[0];
+        ++numObjects[FGAIBase::otBallistic];
         ai_ballistic->setAzimuth(entity->azimuth);
         ai_ballistic->setElevation(entity->elevation);
         ai_ballistic->setSpeed(entity->speed);
@@ -231,20 +201,23 @@ int FGAIManager::createBallistic( FGAIModelEntity *entity ) {
         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->setWeight(entity->weight);
         ai_ballistic->init();
         ai_ballistic->bind();
-        return ai_ballistic->getID();
+        return ai_ballistic;
 }
 
-int FGAIManager::createStorm( FGAIModelEntity *entity ) {
+void*
+FGAIManager::createStorm( FGAIModelEntity *entity ) {
 
         FGAIStorm* ai_storm = new FGAIStorm(this);
-        ai_list.push_back(ai_storm);
-        ai_storm->setID( assignID() );
-        ++numObjects;
+        ++numObjects[0];
+        ++numObjects[FGAIBase::otStorm];
         ai_storm->setHeading(entity->heading);
         ai_storm->setSpeed(entity->speed);
         ai_storm->setPath(entity->path);
@@ -253,34 +226,37 @@ int FGAIManager::createStorm( FGAIModelEntity *entity ) {
         ai_storm->setLatitude(entity->latitude);
         ai_storm->init();
         ai_storm->bind();
-        return ai_storm->getID();
+        ai_list.push_back(ai_storm);
+        return ai_storm;
 }
 
-int FGAIManager::createThermal( FGAIModelEntity *entity ) {
+void*
+FGAIManager::createThermal( FGAIModelEntity *entity ) {
 
         FGAIThermal* ai_thermal = new FGAIThermal(this);
-        ai_list.push_back(ai_thermal);
-        ai_thermal->setID( assignID() );
-        ++numObjects;
+        ++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();
+        ai_list.push_back(ai_thermal);
+        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()];
+              (*ai_list_itr)->unbind();
               delete (*ai_list_itr);
               ai_list.erase(ai_list_itr);
-              --ai_list_itr;
-              --numObjects;
-              return;
+
+              break;
             }
             ++ai_list_itr;
         }
@@ -312,31 +288,25 @@ void FGAIManager::processThermal( FGAIThermal* thermal ) {
 }
 
 
-void FGAIManager::processScenario( string filename ) {
+void FGAIManager::processScenario( 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") {
+      if ( !strcmp(en->m_type, "aircraft")) {
          createAircraft( en );
 
-      } else if (en->m_class == "ship") {
+      } else if ( !strcmp(en->m_type, "ship")) {
            createShip( en );
 
-      } else if (en->m_class == "storm") {
+      } else if ( !strcmp(en->m_type, "thunderstorm")) {
         createStorm( en );
 
-      } else if (en->m_class == "thermal") {
+      } else if ( !strcmp(en->m_type, "thermal")) {
         createThermal( en );
 
-      } else if (en->m_class == "ballistic") {
+      } else if ( !strcmp(en->m_type, "ballistic")) {
         createBallistic( en );
       }      
     }
@@ -345,15 +315,4 @@ void FGAIManager::processScenario( string filename ) {
   delete s;
 }
 
-int FGAIManager::getNum( FGAIBase::object_type ot ) {
-  ai_list_iterator itr = ai_list.begin();
-  int count = 0;
-  while(itr != ai_list.end()) {
-      if ((*itr)->getType() == ot) {
-         ++count;
-      }
-      ++itr;
-  }  
-  return count;
-}
-
+//end AIManager.cxx