]> git.mxchange.org Git - flightgear.git/commitdiff
Tidy up the code a bit
authorehofman <ehofman>
Sun, 21 Dec 2003 13:42:01 +0000 (13:42 +0000)
committerehofman <ehofman>
Sun, 21 Dec 2003 13:42:01 +0000 (13:42 +0000)
src/AIModel/AIAircraft.cxx
src/AIModel/AIAircraft.hxx
src/AIModel/AIManager.cxx
src/AIModel/AIManager.hxx

index 010eaffc3dbad609c1f2aedb844c09f45daa2a4d..49346f99ac2e0aaeb8b81449347f25fa5effeb40 100644 (file)
@@ -33,6 +33,19 @@ SG_USING_STD(string);
 
 #include "AIAircraft.hxx"
 
+
+const FGAIAircraft::PERF_STRUCT FGAIAircraft::settings[] = {
+    // light aircraft
+    {2.0, 2.0,  450.0, 1000.0,  70.0,  80.0, 100.0,  80.0,  60.0},
+    // ww2_fighter
+    {4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0},
+    // jet_transport
+    {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0},
+    // jet_fighter
+    {7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0}
+};
+
+
 FGAIAircraft::FGAIAircraft() {
 
    // set heading and altitude locks
@@ -57,7 +70,7 @@ void FGAIAircraft::update(double dt) {
    FGAIBase::update(dt);
 }
 
-void FGAIAircraft::SetPerformance(PERF_STRUCT ps) {
+void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
    
    performance = ps;
 } 
@@ -83,8 +96,8 @@ void FGAIAircraft::Run(double dt) {
    // adjust speed
    double speed_diff = tgt_speed - speed;
    if (fabs(speed_diff) > 0.2) {
-     if (speed_diff > 0.0) speed += performance.accel * dt;
-     if (speed_diff < 0.0) speed -= performance.decel * dt;
+     if (speed_diff > 0.0) speed += performance->accel * dt;
+     if (speed_diff < 0.0) speed -= performance->decel * dt;
    } 
    
    // convert speed to degrees per second
@@ -136,10 +149,10 @@ void FGAIAircraft::Run(double dt) {
      double altitude_ft = altitude * 3.28084;
      if (altitude_ft < tgt_altitude) {
       tgt_vs = tgt_altitude - altitude_ft;
-      if (tgt_vs > performance.climb_rate) tgt_vs = performance.climb_rate;
+      if (tgt_vs > performance->climb_rate) tgt_vs = performance->climb_rate;
      } else {
       tgt_vs = tgt_altitude - altitude_ft;
-      if (tgt_vs  < (-performance.descent_rate)) tgt_vs = -performance.descent_rate;
+      if (tgt_vs  < (-performance->descent_rate)) tgt_vs = -performance->descent_rate;
      }
    }
 
index 759cca93c36e780357229b0ebd86c996493c190e..43aab0384390c969dda1899b00d4f1dc2eedaed9 100644 (file)
@@ -29,8 +29,25 @@ SG_USING_STD(string);
 
 
 class FGAIAircraft : public FGAIBase {
+
+private:
+
+       typedef struct {
+            double accel;
+            double decel;
+            double climb_rate;
+            double descent_rate;
+            double takeoff_speed;
+            double climb_speed;
+            double cruise_speed;
+            double descent_speed;
+            double land_speed;
+       } PERF_STRUCT;
        
 public:
+
+        enum aircraft_e {LIGHT=0, WW2_FIGHTER, JET_TRANSPORT, JET_FIGHTER};
+        static const PERF_STRUCT settings[];
        
        FGAIAircraft();
        ~FGAIAircraft();
@@ -38,7 +55,7 @@ public:
        bool init();
        void update(double dt);
 
-        void SetPerformance(PERF_STRUCT ps);
+        void SetPerformance(const PERF_STRUCT *ps);
         void AccelTo(double speed);
         void PitchTo(double angle);
         void RollTo(double angle);
@@ -53,7 +70,7 @@ private:
 
         double dt; 
 
-        PERF_STRUCT performance;
+        const PERF_STRUCT *performance;
 
        void Run(double dt);
         double sign(double x); 
index 9e9b310f26cca9dc65cecbb01100022124e4c81d..c6dbda134f277d4c002ff17dd2fbdde60a063300 100644 (file)
@@ -42,25 +42,29 @@ FGAIManager::~FGAIManager() {
 
 void FGAIManager::init() {
   SGPropertyNode * node = fgGetNode("sim/ai", true);
+
   for (int i = 0; i < node->nChildren(); i++) {
     const SGPropertyNode * entry = node->getChild(i);
+
     if (!strcmp(entry->getName(), "entry")) {
       if (!strcmp(entry->getStringValue("type", ""), "aircraft")) { 
         FGAIAircraft* ai_plane = new FGAIAircraft;
         ai_list.push_back(ai_plane);
-        if (!strcmp(entry->getStringValue("class", ""), "light")) {
-          PERF_STRUCT ps = {2.0, 2.0, 450.0, 1000.0, 70.0, 80.0, 100.0, 80.0, 60.0};
-          ai_plane->SetPerformance(ps);
-        } else if (!strcmp(entry->getStringValue("class", ""), "ww2_fighter")) {
-          PERF_STRUCT ps = {4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0};
-          ai_plane->SetPerformance(ps);
-        } else if (!strcmp(entry->getStringValue("class", ""), "jet_transport")) {
-          PERF_STRUCT ps = {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0};
-          ai_plane->SetPerformance(ps);
-        } else if (!strcmp(entry->getStringValue("class", ""), "jet_fighter")) {
-          PERF_STRUCT ps = {7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0};
-          ai_plane->SetPerformance(ps);
+
+        string model_class = entry->getStringValue("class", "");
+        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") {
+          ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
+
+        } else if (model_class == "jet_fighter") {
+          ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
         }
+
         ai_plane->setHeading(entry->getDoubleValue("heading"));
         ai_plane->setSpeed(entry->getDoubleValue("speed-KTAS"));
         ai_plane->setPath(entry->getStringValue("path"));
@@ -92,7 +96,7 @@ void FGAIManager::init() {
         ai_ballistic->setLatitude(entry->getDoubleValue("latitude"));
         ai_ballistic->init();
       } 
-     }
+    }
   }
 
   initDone = true;
index 032cc01001233973f577d83aa094827575e2575c..32098fcd7efa8d6822766fec883d450575417a7b 100644 (file)
 #include <Main/fg_props.hxx>
 #include <list>
 #include "AIBase.hxx"
+#include "AIAircraft.hxx"
 
 SG_USING_STD(list);
 
- struct PERF_STRUCT {
-   double accel;
-   double decel;
-   double climb_rate;
-   double descent_rate;
-   double takeoff_speed;
-   double climb_speed;
-   double cruise_speed;
-   double descent_speed;
-   double land_speed;
-  };
-
 
 class FGAIManager : public SGSubsystem
 {