]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/ATCutils.cxx
AI plane should go around instead of landing on user if user dawdles on runway now
[flightgear.git] / src / ATC / ATCutils.cxx
index eb89d579a4fbb92375e03a15c691ff656ea76f90..3bd30e10a9278f77a839e456285ba6d27d7d9d0e 100644 (file)
@@ -30,6 +30,7 @@
 #include <Main/globals.hxx>
 
 #include "ATCutils.hxx"
+#include "ATCProjection.hxx"
 
 // Convert any number to spoken digits
 string ConvertNumToSpokenDigits(string n) {
@@ -51,10 +52,19 @@ string ConvertNumToSpokenDigits(string n) {
                        str += " ";
                }
        }
-
        return(str);
 }
 
+
+// Convert an integer to spoken digits
+string ConvertNumToSpokenDigits(int n) {
+       char buf[12];   // should be big enough!!
+       sprintf(buf, "%i", n);
+       string tempstr1 = buf;
+       return(ConvertNumToSpokenDigits(tempstr1));
+}
+
+
 // Convert a 2 digit rwy number to a spoken-style string
 string ConvertRwyNumToSpokenString(int n) {
        string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
@@ -79,7 +89,7 @@ string ConvertRwyNumToSpokenString(int n) {
        return(str);
 }
 
-// Assumes we get a two-digit string optionally appended with R or L
+// Assumes we get a two-digit string optionally appended with L, R or C
 // eg 01 07L 29R 36
 // Anything else is not guaranteed to be handled correctly!
 string ConvertRwyNumToSpokenString(string s) {
@@ -87,7 +97,16 @@ string ConvertRwyNumToSpokenString(string s) {
                return(ConvertRwyNumToSpokenString(atoi(s.c_str())));
        } else {
                string r = ConvertRwyNumToSpokenString(atoi(s.substr(0,2).c_str()));
-               return(r += (s.substr(2,1) == "L" ? " left" : " right"));       // Warning - not much error checking there!
+               if(s.substr(2,1) == "L") {
+                       r += " left";
+               } else if(s.substr(2,1) == "R") {
+                       r += " right";
+               } else if(s.substr(2,1) == "C") {
+                       r += " center";
+               } else {
+                       SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix " << s.substr(2,1) << " from runway ID " << s << " in ConvertRwyNumToSpokenString(...)");
+               }
+               return(r);
        }
 }
        
@@ -127,6 +146,8 @@ string GetPhoneticIdent(int i) {
        return("Error");
 }
 
+//================================================================================================================
+
 // Given two positions (lat & lon in degrees), get the HORIZONTAL separation (in meters)
 double dclGetHorizontalSeparation(Point3D pos1, Point3D pos2) {
        double x;       //East-West separation
@@ -237,27 +258,40 @@ void dclBoundHeading(double &hdg) {
        }
 }
 
+// smallest difference between two angles in degrees
+// difference is negative if a1 > a2 and positive if a2 > a1
+double GetAngleDiff_deg( const double &a1, const double &a2) {
+  
+  double a3 = a2 - a1;
+  while (a3 < 180.0) a3 += 360.0;
+  while (a3 > 180.0) a3 -= 360.0;
+
+  return a3;
+}
+
+//================================================================================================================
+
 // Airport stuff.  The next two functions are straight copies of their fg.... equivalents
 // in fg_init.cxx, and are just here temporarily until some rationalisation occurs.
 // find basic airport location info from airport database
 bool dclFindAirportID( const string& id, FGAirport *a ) {
-    if ( id.length() ) {
-        SGPath path( globals->get_fg_root() );
-        path.append( "Airports" );
-        path.append( "simple.mk4" );
-        FGAirports airports( path.c_str() );
+    FGAirport result;
 
+    if ( id.length() ) {
         SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
 
-        if ( ! airports.search( id, a ) ) {
+        result = globals->get_airports()->search( id );
+        if ( result.id.empty() ) {
             SG_LOG( SG_GENERAL, SG_ALERT,
-                    "Failed to find " << id << " in " << path.str() );
+                    "Failed to find " << id << " in basic.dat.gz" );
             return false;
         }
     } else {
         return false;
     }
 
+    *a = result;
+
     SG_LOG( SG_GENERAL, SG_INFO,
             "Position for " << id << " is ("
             << a->longitude << ", "
@@ -280,3 +314,25 @@ double dclGetAirportElev( const string& id ) {
         return -9999.0;
     }
 }
+
+// Runway stuff
+// Given a Point3D (lon/lat/elev) and an FGRunway struct, determine if the point lies on the runway
+bool OnRunway(Point3D pt, const FGRunway& rwy) {
+       FGATCAlignedProjection ortho;
+       Point3D centre(rwy.lon, rwy.lat, 0.0);  // We don't need the elev
+       ortho.Init(centre, rwy.heading);
+       
+       Point3D xyc = ortho.ConvertToLocal(centre);
+       Point3D xyp = ortho.ConvertToLocal(pt);
+       
+       //cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
+       //cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
+       
+       if((fabs(xyp.y() - xyc.y()) < ((rwy.length/2.0) + 5.0)) 
+               && (fabs(xyp.x() - xyc.x()) < (rwy.width/2.0))) {
+               return(true);
+       }
+       
+       return(false);
+}
+