]> git.mxchange.org Git - flightgear.git/commitdiff
David Culp:
authorehofman <ehofman>
Sun, 7 Mar 2004 12:08:46 +0000 (12:08 +0000)
committerehofman <ehofman>
Sun, 7 Mar 2004 12:08:46 +0000 (12:08 +0000)
I added some things to the AI stuff to improve the AIThermal processing.
Before, all the thermals were processed in order, and the last one overwrote
the prior one.  Now, only the data from the nearest thermal is kept.  This
way a tile can be populated with many thermals, and (as long as they have the
same diameter) the one nearest the airplane correctly takes effect.  This
will make us ready for the next step, "auto-thermaling", where FlightGear's
tile manager can cover a tile with thermals, and set the thermal strength
based on land-use type.

I moved the enumerated object_type to the base class.  When an AI object is
created it now sets the _otype variable in the base class.  This lets the AI
manager find out what kind of AI object it is dealing with, using the base
pointer.  I also added a function isa() to the base class, so the manager can
process objects differently based on their type.

The AI manager now sends AIThermal processing to a different function, where
only the data from the nearest thermal is kept.  After the manager processes
all the AI objects, then the results from the nearest thermal are applied to
wind-from-down.

src/AIModel/AIAircraft.cxx
src/AIModel/AIBallistic.cxx
src/AIModel/AIBase.cxx
src/AIModel/AIBase.hxx
src/AIModel/AIManager.cxx
src/AIModel/AIManager.hxx
src/AIModel/AIShip.cxx
src/AIModel/AIStorm.cxx
src/AIModel/AIThermal.cxx
src/AIModel/AIThermal.hxx

index 40c790a16543d89174b75f7370c59cf52c261756..ab293b98ba4388c7799076a2ddd91f256b7b2aa0 100644 (file)
@@ -54,11 +54,12 @@ FGAIAircraft *FGAIAircraft::_self = NULL;
 FGAIAircraft::FGAIAircraft(FGAIManager* mgr) {
    manager = mgr;   
    _self = this;
+   _type_str = "aircraft";
+   _otype = otAircraft;
 
    // set heading and altitude locks
    hdg_lock = false;
    alt_lock = false;
-   _type_str = "aircraft";
 }
 
 
index 0e7249e4e0c4cd6df51f22ee00f67f66771126d7..c712a989b70f2a074b40eaf9f8c4d69b25547573 100644 (file)
@@ -30,6 +30,7 @@
 FGAIBallistic::FGAIBallistic(FGAIManager* mgr) {
     manager = mgr;
     _type_str = "ballistic";
+    _otype = otBallistic;
 }
 
 FGAIBallistic::~FGAIBallistic() {
index 123a936e8bb16621ebd1e340ac89f2d34d4a0cc9..bb1af2279ad82017c5e666fcafdbee922ac6944b 100644 (file)
@@ -52,6 +52,7 @@ FGAIBase::FGAIBase() {
     x_shift = y_shift = rotation = 0.0;
     invisible = true;
     model_path = "";
+    _otype = otNull;
 }
 
 FGAIBase::~FGAIBase() {
@@ -103,6 +104,12 @@ bool FGAIBase::init() {
    return true;
 }
 
+bool FGAIBase::isa( object_type otype ) {
+ if ( otype == _otype ) { return true; }
+ else { return false; } 
+}
+
+
 void FGAIBase::bind() {
    props->tie("id", SGRawValuePointer<int>(&id));
    props->tie("velocities/true-airspeed-kt",  SGRawValuePointer<double>(&speed));
index a9a00408cbd85aab1e229d86c0d1b324850357df..44ebd61e83559af3fe0d4de1d303bde93bb996ed 100644 (file)
@@ -41,6 +41,9 @@ public:
     virtual void update(double dt);
     inline Point3D GetPos() { return(pos); }
 
+    enum object_type { otNull, otAircraft, otShip, otBallistic,
+                       otRocket, otStorm, otThermal };
+
     virtual bool init();
     virtual void bind();
     virtual void unbind();
@@ -103,6 +106,7 @@ protected:
 
     static FGAIBase *_self;
     const char *_type_str;
+    object_type _otype;
 
 public:
 
@@ -129,6 +133,7 @@ public:
     static double _getRotation();
 
     static bool _isNight();
+    bool isa( object_type otype );
 };
 
 
index 394951f09b4405526dbc4054930fd8723e0a17f4..ceffc4c4a8c8b2d1f2e0a87f2bf3cea1fa96c8b1 100644 (file)
@@ -37,6 +37,7 @@ SG_USING_STD(list);
 FGAIManager::FGAIManager() {
   initDone = false;
   numObjects = 0;
+  _dt = 0.0;
   dt_count = 9;
 }
 
@@ -54,6 +55,7 @@ FGAIManager::~FGAIManager() {
 void FGAIManager::init() {
   int rval;
   root = fgGetNode("sim/ai", true);
+  wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
 
   for (int i = 0; i < root->nChildren(); i++) {
     const SGPropertyNode * entry = root->getChild(i);
@@ -127,10 +129,16 @@ void FGAIManager::unbind() {
 
 
 void FGAIManager::update(double dt) {
-       
+
+        // initialize these for finding nearest thermals
+        range_nearest = 10000.0;
+        strength = 0.0;
+
+        _dt = dt;      
+
         ai_list_itr = ai_list.begin();
         while(ai_list_itr != ai_list.end()) {
-                if ((*ai_list_itr)->getDie()) {
+                if ((*ai_list_itr)->getDie()) {      
                    freeID((*ai_list_itr)->getID());
                    delete (*ai_list_itr);
                    ai_list.erase(ai_list_itr);
@@ -138,10 +146,15 @@ void FGAIManager::update(double dt) {
                    --numObjects;
                 } else {
                    fetchUserState();
-                   (*ai_list_itr)->update(dt);
+                   if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
+                       processThermal((FGAIThermal*)*ai_list_itr); 
+                   } else { 
+                      (*ai_list_itr)->update(_dt);
+                   }
                 }
                 ++ai_list_itr;
         }
+        wind_from_down->setDoubleValue( strength );
 }
 
 
@@ -277,7 +290,7 @@ int FGAIManager::createThermal( double latitude, double longitude,
         ++numObjects;
         ai_thermal->setLongitude(longitude);
         ai_thermal->setLatitude(latitude);
-        ai_thermal->setStrength(strength);
+        ai_thermal->setMaxStrength(strength);
         ai_thermal->setDiameter(diameter / 6076.11549);
         ai_thermal->init();
         ai_thermal->bind();
@@ -313,3 +326,13 @@ void FGAIManager::fetchUserState( void ) {
      dt_count = 0;
    }
 }
+
+
+// only keep the results from the nearest thermal
+void FGAIManager::processThermal( FGAIThermal* thermal ) {
+  thermal->update(_dt);
+  if ( thermal->_getRange() < range_nearest ) {
+     range_nearest = thermal->_getRange();
+     strength = thermal->getStrength();
+  }
+}
index 5b0e962b17de668352aa5a6c89fc8da586c9bcda..537d5145eeee77db767ca5b4d00ee76bb704b05e 100644 (file)
@@ -29,6 +29,7 @@
 #include "AIBase.hxx"
 
 SG_USING_STD(list);
+class FGAIThermal;
 
 
 class FGAIManager : public SGSubsystem
@@ -53,8 +54,6 @@ private:
 
 public:
 
-    enum object_type { otAircraft, otShip, otBallistic, otRocket, otStorm, otThermal };
-
     FGAIManager();
     ~FGAIManager();
 
@@ -120,6 +119,7 @@ private:
     bool initDone;
     int numObjects;
     SGPropertyNode* root;
+    SGPropertyNode* wind_from_down;
 
     double user_latitude;
     double user_longitude;
@@ -128,9 +128,15 @@ private:
     double user_pitch;
     double user_yaw;
     double user_speed;
+    double _dt;
     int dt_count;
     void fetchUserState( void );
 
+    // used by thermals
+    double range_nearest;
+    double strength;
+    void processThermal( FGAIThermal* thermal ); 
+
 };
 
 #endif  // _FG_AIMANAGER_HXX
index d9fc379fe3092e24448ce362578cfe7c5c50c59f..c1d4ce809a3c081241964f7818e87187df6f7236 100644 (file)
 
 FGAIShip::FGAIShip(FGAIManager* mgr) {
    manager = mgr;
+   _type_str = "ship";
+   _otype = otShip;
+
    hdg_lock = false;
    rudder = 0.0;
-   _type_str = "ship";
 }
 
 FGAIShip::~FGAIShip() {
index fa09aad79b519d24be7f0110a7016e91eae82938..532a3f1060564570ba9a4ebc1a8a1080f5a6caec 100644 (file)
@@ -40,6 +40,7 @@ FGAIStorm::FGAIStorm(FGAIManager* mgr) {
    manager = mgr;   
    _self = this;
    _type_str = "thunderstorm";
+   _otype = otStorm;
 }
 
 
index 812f5e01e9f0fe4099533177b75de5cf39266763..7ceafcee9f99b4048f4dae0ba45580e4c58680b7 100644 (file)
@@ -40,8 +40,10 @@ FGAIThermal::FGAIThermal(FGAIManager* mgr) {
    manager = mgr;   
    _self = this;
    _type_str = "thermal";
-   strength = 6.0;
+   _otype = otThermal;
+   max_strength = 6.0;
    diameter = 0.5;
+   strength = factor = 0.0;
 }
 
 
@@ -51,8 +53,7 @@ FGAIThermal::~FGAIThermal() {
 
 
 bool FGAIThermal::init() {
-   wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
-   scaler = 8.0 * strength / (diameter * diameter * diameter);
+   factor = 8.0 * max_strength / (diameter * diameter * diameter);
    return FGAIBase::init();
 }
 
@@ -100,13 +101,13 @@ void FGAIThermal::Run(double dt) {
    double range_ft = sqrt( lat_range*lat_range + lon_range*lon_range );
    range = range_ft / 6076.11549;
 
-   // Set rising air if within range. 
+   // Calculate speed of rising air if within range. 
    // Air vertical speed is maximum at center of thermal,
    // and decreases to zero at the edge (as distance cubed).
    if (range < (diameter * 0.5)) {
-     wind_from_down->setDoubleValue( strength - (range * range * range * scaler) );
+     strength = max_strength - ( range * range * range * factor );
    } else {
-     wind_from_down->setDoubleValue( 0.0 );
+     strength = 0.0;
    }
 }
 
index c4eb171d12c1c2fcfa8583ca91279e572faa615e..1da663ff864602f553121c0d2692adb8d2090573 100644 (file)
@@ -40,8 +40,10 @@ public:
         virtual void unbind();
        void update(double dt);
 
-        inline void setStrength( double s ) { strength = s; };
+        inline void setMaxStrength( double s ) { max_strength = s; };
         inline void setDiameter( double d ) { diameter = d; };
+        inline double getStrength() const { return strength; };
+        inline double getDiameter() const { return diameter; };
 
 protected:
         static FGAIThermal *_self;
@@ -50,10 +52,10 @@ private:
 
         double dt; 
        void Run(double dt);
-        SGPropertyNode* wind_from_down;
+        double max_strength;
         double strength;
         double diameter;
-        double scaler;
+        double factor;
 };