]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIAircraft.cxx
Merge branch 'ehofman/config' into next
[flightgear.git] / src / AIModel / AIAircraft.cxx
index 6559bbcbd7ffee1c014d01c772b3feec8e7822c5..eb8469fdece8dd419a721b5c7262caf3ddc9a6af 100644 (file)
@@ -33,6 +33,7 @@
 #include <string>
 #include <math.h>
 #include <time.h>
+
 #ifdef _MSC_VER
 #  include <float.h>
 #  define finite _finite
@@ -79,11 +80,12 @@ FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) {
     alt_lock = false;
     roll = 0;
     headingChangeRate = 0.0;
+    headingError = 0;
 
     holdPos = false;
 
     _performance = 0; //TODO initialize to JET_TRANSPORT from PerformanceDB
-    
+    dt = 0;
 }
 
 
@@ -167,19 +169,9 @@ void FGAIAircraft::setPerformance(const std::string& acclass) {
 
 void FGAIAircraft::checkVisibility() 
 {
-     double visibility_meters = fgGetDouble("/environment/visibility-m");
-
-     FGViewer* vw = globals->get_current_view();
-     double course, distance;
-
-     SGWayPoint current(pos.getLongitudeDeg(), pos.getLatitudeDeg(), 0);
-     SGWayPoint view (vw->getLongitude_deg(), vw->getLatitude_deg(), 0);
-     view.CourseAndDistance(current, &course, &distance);
-     if (distance > visibility_meters) {
-         invisible = true;
-      } else {
-         invisible = false;
-      }
+  double visibility_meters = fgGetDouble("/environment/visibility-m");
+  FGViewer* vw = globals->get_current_view();
+  invisible = (SGGeodesy::distanceM(vw->getPosition(), pos) > visibility_meters);
 }
 
 
@@ -287,8 +279,8 @@ void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) {
         }
 
         //TODO let the fp handle this (loading of next leg)
-        fp->IncrementWaypoint((bool) trafficRef);
-        if (!(fp->getNextWaypoint()) && trafficRef)
+        fp->IncrementWaypoint( trafficRef != 0 );
+        if (!(fp->getNextWaypoint()) && trafficRef != 0)
             if (!loadNextLeg()) {
                 setDie(true);
                 return;
@@ -409,14 +401,9 @@ void FGAIAircraft::getGroundElev(double dt) {
     // Only do the proper hitlist stuff if we are within visible range of the viewer.
     if (!invisible) {
         double visibility_meters = fgGetDouble("/environment/visibility-m");
-
         FGViewer* vw = globals->get_current_view();
-        double course, distance;
-
-        SGWayPoint current(pos.getLongitudeDeg(), pos.getLatitudeDeg(), 0);
-        SGWayPoint view (vw->getLongitude_deg(), vw->getLatitude_deg(), 0);
-        view.CourseAndDistance(current, &course, &distance);
-        if (distance > visibility_meters) {
+        
+        if (SGGeodesy::distanceM(vw->getPosition(), pos) > visibility_meters) {
             return;
         }
 
@@ -427,7 +414,7 @@ void FGAIAircraft::getGroundElev(double dt) {
         }
 
         double alt;
-        if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(pos, 20000), alt, 0))
+        if (getGroundElevationM(SGGeod::fromGeodM(pos, 20000), alt, 0))
             tgt_altitude_ft = alt * SG_METER_TO_FEET;
     }
 }
@@ -460,15 +447,6 @@ void FGAIAircraft::announcePositionToController() {
         case 4:              //Take off tower controller
             if (trafficRef->getDepartureAirport()->getDynamics()) {
                 controller = trafficRef->getDepartureAirport()->getDynamics()->getTowerController();
-                //if (trafficRef->getDepartureAirport()->getId() == "EHAM") {
-                //string trns = trafficRef->getCallSign() + " at runway " + fp->getRunway() + 
-                //              ". Ready for departure. " + trafficRef->getFlightType() + " to " +
-                //              trafficRef->getArrivalAirport()->getId();
-                //fgSetString("/sim/messages/atc", trns.c_str());
-                //  if (controller == 0) {
-                //cerr << "Error in assigning controller at " << trafficRef->getDepartureAirport()->getId() << endl;
-                //}
-                //}
             } else {
                 cerr << "Error: Could not find Dynamics at airport : " << trafficRef->getDepartureAirport()->getId() << endl;
             }
@@ -659,16 +637,10 @@ bool FGAIAircraft::leadPointReached(FGAIFlightPlan::waypoint* curr) {
 
 
 bool FGAIAircraft::aiTrafficVisible() {
-    double userLatitude  = fgGetDouble("/position/latitude-deg");
-    double userLongitude = fgGetDouble("/position/longitude-deg");
-    double course, distance;
-
-    SGWayPoint current(pos.getLongitudeDeg(), pos.getLatitudeDeg(), 0);
-    SGWayPoint user (userLongitude, userLatitude, 0);
-
-    user.CourseAndDistance(current, &course, &distance);
-
-    return ((distance * SG_METER_TO_NM) <= TRAFFICTOAIDISTTODIE);
+  SGGeod userPos(SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"), 
+    fgGetDouble("/position/latitude-deg")));
+  
+  return (SGGeodesy::distanceNm(userPos, pos) <= TRAFFICTOAIDISTTODIE);
 }