]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIAircraft.cxx
Merge branch 'next' into durk-atc
[flightgear.git] / src / AIModel / AIAircraft.cxx
index f117810c17f447e29884d8e496cbf265e434efd7..a153db7f7dc8fd6dcc13ac303207afb84415b201 100644 (file)
@@ -1,4 +1,4 @@
-// FGAIAircraft - FGAIBase-derived class creates an AI airplane
+// // FGAIAircraft - FGAIBase-derived class creates an AI airplane
 //
 // Written by David Culp, started October 2003.
 //
@@ -29,6 +29,7 @@
 #include <Scenery/scenery.hxx>
 #include <Scenery/tilemgr.hxx>
 #include <Airports/dynamics.hxx>
+#include <Airports/simple.hxx>
 
 #include <string>
 #include <math.h>
@@ -51,11 +52,15 @@ using std::string;
 
 static string tempReg;
 
-FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) {
+FGAIAircraft::FGAIAircraft(FGAISchedule *ref) :
+     /* HOT must be disabled for AI Aircraft,
+      * otherwise traffic detection isn't working as expected.*/
+     FGAIBase(otAircraft, false) 
+{
     trafficRef = ref;
     if (trafficRef) {
         groundOffset = trafficRef->getGroundOffset();
-       setCallSign(trafficRef->getCallSign());
+        setCallSign(trafficRef->getCallSign());
     }
     else
         groundOffset = 0;
@@ -83,6 +88,7 @@ FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) {
 
     holdPos = false;
     needsTaxiClearance = false;
+    _needsGroundElevation = true;
 
     _performance = 0; //TODO initialize to JET_TRANSPORT from PerformanceDB
     dt = 0;
@@ -163,7 +169,12 @@ void FGAIAircraft::setPerformance(const std::string& acclass) {
      handleATCRequests(); // ATC also has a word to say
      updateSecondaryTargetValues(); // target roll, vertical speed, pitch
      updateActualState(); 
-     UpdateRadar(manager);
+    // We currently have one situation in which an AIAircraft object is used that is not attached to the 
+    // AI manager. In this particular case, the AIAircraft is used to shadow the user's aircraft's behavior in the AI world.
+    // Since we perhaps don't want a radar entry of our own aircraft, the following conditional should probably be adequate
+    // enough
+     if (manager)
+        UpdateRadar(manager);
      checkVisibility();
   }
 
@@ -178,6 +189,8 @@ void FGAIAircraft::checkVisibility()
 
 void FGAIAircraft::AccelTo(double speed) {
     tgt_speed = speed;
+    if (!isStationary())
+        _needsGroundElevation = true;
 }
 
 
@@ -272,6 +285,19 @@ void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) {
     if (! leadPointReached(curr)) {
         controlHeading(curr);
         controlSpeed(curr, next);
+            if (speed < 0) { 
+                cerr << getCallSign() 
+                     << ": verifying lead distance to waypoint : " 
+                     << fp->getCurrentWaypoint()->name << " "
+                     << fp->getLeadDistance() << ". Distance to go " 
+                     << (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)) 
+                     << ". Target speed = " 
+                     << tgt_speed
+                     << ". Current speed = "
+                     << speed
+                     << ". Minimum Bearing " << minBearing
+                     << endl;
+            }
     } else {
         if (curr->finished)      //end of the flight plan
         {
@@ -328,17 +354,44 @@ void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) {
                 use_perf_vs = false;
                 tgt_vs = (curr->crossat - altitude_ft) / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)
                          / 6076.0 / speed*60.0);
+                checkTcas();
                 tgt_altitude_ft = curr->crossat;
             } else {
                 use_perf_vs = true;
             }
         }
-        tgt_speed = prev->speed;
+        AccelTo(prev->speed);
         hdg_lock = alt_lock = true;
         no_roll = prev->on_ground;
     }
 }
 
+void FGAIAircraft::checkTcas(void)
+{
+    if (props->getIntValue("tcas/threat-level",0)==3)
+    {
+        int RASense = props->getIntValue("tcas/ra-sense",0);
+        if ((RASense>0)&&(tgt_vs<4000))
+            // upward RA: climb!
+            tgt_vs = 4000;
+        else
+        if (RASense<0)
+        {
+            // downward RA: descend!
+            if (altitude_ft < 1000)
+            {
+                // too low: level off
+                if (tgt_vs>0)
+                    tgt_vs = 0;
+            }
+            else
+            {
+                if (tgt_vs >- 4000)
+                    tgt_vs = -4000;
+            }
+        }
+    }
+}
 
 void FGAIAircraft::initializeFlightPlan() {
 }
@@ -402,6 +455,8 @@ bool FGAIAircraft::loadNextLeg(double distance) {
 void FGAIAircraft::getGroundElev(double dt) {
     dt_elev_count += dt;
 
+    if (!needGroundElevation())
+        return;
     // Update minimally every three secs, but add some randomness
     // to prevent all AI objects doing this in synchrony
     if (dt_elev_count < (3.0) + (rand() % 10))
@@ -419,20 +474,26 @@ void FGAIAircraft::getGroundElev(double dt) {
         }
 
         double range = 500.0;
-        if (!globals->get_tile_mgr()->scenery_available(pos, range)) {
-            // Try to shedule tiles for that position.
-            globals->get_tile_mgr()->schedule_tiles_at( pos, range );
+        if (globals->get_tile_mgr()->schedule_scenery(pos, range, 5.0))
+        {
+            double alt;
+            if (getGroundElevationM(SGGeod::fromGeodM(pos, 20000), alt, 0))
+            {
+                tgt_altitude_ft = alt * SG_METER_TO_FEET;
+                if (isStationary())
+                {
+                    // aircraft is stationary and we obtained altitude for this spot - we're done.
+                    _needsGroundElevation = false;
+                }
+            }
         }
-
-        double alt;
-        if (getGroundElevationM(SGGeod::fromGeodM(pos, 20000), alt, 0))
-            tgt_altitude_ft = alt * SG_METER_TO_FEET;
     }
 }
 
 
 void FGAIAircraft::doGroundAltitude() {
-    if (fabs(altitude_ft - (tgt_altitude_ft+groundOffset)) > 1000.0)
+    if ((fabs(altitude_ft - (tgt_altitude_ft+groundOffset)) > 1000.0)||
+        (isStationary()))
         altitude_ft = (tgt_altitude_ft + groundOffset);
     else
         altitude_ft += 0.1 * ((tgt_altitude_ft+groundOffset) - altitude_ft);
@@ -591,6 +652,7 @@ void FGAIAircraft::handleFirstWaypoint() {
         tgt_vs = (curr->crossat - prev->altitude)
                  / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)
                     / 6076.0 / prev->speed*60.0);
+        checkTcas();
         tgt_altitude_ft = curr->crossat;
     } else {
         use_perf_vs = true;
@@ -602,6 +664,7 @@ void FGAIAircraft::handleFirstWaypoint() {
         Transform();             // make sure aip is initialized.
         getGroundElev(60.1);     // make sure it's executed first time around, so force a large dt value
         doGroundAltitude();
+        _needsGroundElevation = true; // check ground elevation again (maybe scenery wasn't available yet)
     }
     // Make sure to announce the aircraft's position
     announcePositionToController();
@@ -635,7 +698,15 @@ bool FGAIAircraft::leadPointReached(FGAIFlightPlan::waypoint* curr) {
     //cerr << "2" << endl;
     double lead_dist = fp->getLeadDistance();
     // experimental: Use fabs, because speed can be negative (I hope) during push_back.
-
+    if ((dist_to_go < fabs(10.0* speed)) && (speed < 0) && (tgt_speed < 0) && fp->getCurrentWaypoint()->name == string("PushBackPoint")) {
+          tgt_speed = -(dist_to_go / 10.0);
+          if (tgt_speed > -0.5) {
+                tgt_speed = -0.5;
+          }
+          if (fp->getPreviousWaypoint()->speed < tgt_speed) {
+              fp->getPreviousWaypoint()->speed = tgt_speed;
+          }
+    }
     if (lead_dist < fabs(2*speed)) {
       //don't skip over the waypoint
       lead_dist = fabs(2*speed);
@@ -895,8 +966,8 @@ void FGAIAircraft::updateHeading() {
                 bank_sense = 1.0;
             }
             //if (trafficRef)
-               //cerr << trafficRef->getCallSign() << " Heading " 
-                //     << hdg << ". Target " << tgt_heading <<  ". Diff " << fabs(sum - tgt_heading) << ". Speed " << speed << endl;
+            // cerr << trafficRef->getCallSign() << " Heading " 
+            //         << hdg << ". Target " << tgt_heading <<  ". Diff " << fabs(sum - tgt_heading) << ". Speed " << speed << endl;
             //if (headingDiff > 60) {
             groundTargetSpeed = tgt_speed; // * cos(headingDiff * SG_DEGREES_TO_RADIANS);
                 //groundTargetSpeed = tgt_speed - tgt_speed * (headingDiff/180);
@@ -905,27 +976,34 @@ void FGAIAircraft::updateHeading() {
             //}
             if (sign(groundTargetSpeed) != sign(tgt_speed))
                 groundTargetSpeed = 0.21 * sign(tgt_speed); // to prevent speed getting stuck in 'negative' mode
-
-            if (headingDiff > 30.0) {
-                // invert if pushed backward
-                headingChangeRate += 10.0 * dt * sign(roll);
-
-                // Clamp the maximum steering rate to 30 degrees per second,
-                // But only do this when the heading error is decreasing.
-                if ((headingDiff < headingError)) {
-                    if (headingChangeRate > 30)
-                        headingChangeRate = 30;
-                    else if (headingChangeRate < -30)
-                        headingChangeRate = -30;
+            
+            // Only update the target values when we're not moving because otherwise we might introduce an enormous target change rate while waiting a the gate, or holding.
+            if (speed != 0) {
+                if (headingDiff > 30.0) {
+                    // invert if pushed backward
+                    headingChangeRate += 10.0 * dt * sign(roll);
+
+                    // Clamp the maximum steering rate to 30 degrees per second,
+                    // But only do this when the heading error is decreasing.
+                    if ((headingDiff < headingError)) {
+                        if (headingChangeRate > 30)
+                            headingChangeRate = 30;
+                        else if (headingChangeRate < -30)
+                            headingChangeRate = -30;
+                    }
+                } else {
+                    if (speed != 0) {
+                        if (fabs(headingChangeRate) > headingDiff)
+                            headingChangeRate = headingDiff*sign(roll);
+                        else
+                            headingChangeRate += dt * sign(roll);
+                    }
                 }
-            } else {
-                   if (fabs(headingChangeRate) > headingDiff)
-                       headingChangeRate = headingDiff*sign(roll);
-                   else
-                       headingChangeRate += dt * sign(roll);
             }
-
-           hdg += headingChangeRate * dt * (fabs(speed) / 15);
+            if (trafficRef)
+               //cerr << trafficRef->getCallSign() << " Heading " 
+                //     << hdg << ". Target " << tgt_heading <<  ". Diff " << fabs(sum - tgt_heading) << ". Speed " << speed << "Heading change rate : " << headingChangeRate << " bacnk sence " << bank_sense << endl;
+           hdg += headingChangeRate * dt * sqrt(fabs(speed) / 15);
             headingError = headingDiff;
         } else {
             if (fabs(speed) > 1.0) {
@@ -1012,6 +1090,7 @@ void FGAIAircraft::updateVerticalSpeedTarget() {
         }
     } //else 
     //    tgt_vs = 0.0;
+    checkTcas();
 }
 
 void FGAIAircraft::updatePitchAngleTarget() {
@@ -1046,12 +1125,12 @@ string FGAIAircraft::atGate() {
 void FGAIAircraft::handleATCRequests() {
     //TODO implement NullController for having no ATC to save the conditionals
     if (controller) {
-        controller->update(getID(),
-                           pos.getLatitudeDeg(),
-                           pos.getLongitudeDeg(),
-                           hdg,
-                           speed,
-                           altitude_ft, dt);
+        controller->updateAircraftInformation(getID(),
+                                              pos.getLatitudeDeg(),
+                                              pos.getLongitudeDeg(),
+                                              hdg,
+                                              speed,
+                                              altitude_ft, dt);
         processATC(controller->getInstruction(getID()));
     }
 }
@@ -1170,4 +1249,4 @@ time_t FGAIAircraft::checkForArrivalTime(string wptName) {
           cerr << "Checking arrival time: ete " << ete << ". Time to go : " << secondsToGo << ". Track length = " << tracklength << endl;
      }
      return (ete - secondsToGo); // Positive when we're too slow...
-}
\ No newline at end of file
+}