]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/native_gui.cxx
Make the AI models a bit more intelligent. The Gear should be extended and retracted...
[flightgear.git] / src / Network / native_gui.cxx
index 5dbcf9520c0e8617f6b4d50b72e52e084a373601..bad4a0b096e094f2673d6000fe537d6f432e0de4 100644 (file)
 #include <simgear/io/iochannel.hxx>
 #include <simgear/timing/sg_time.hxx>
 
+#include <Cockpit/radiostack.hxx>
 #include <FDM/flight.hxx>
 #include <Time/tmp.hxx>
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
+#include <Scenery/scenery.hxx>
 
 #include "native_gui.hxx"
 
@@ -45,6 +47,9 @@
 #endif
 
 
+// #define FG_USE_NETWORK_BYTE_ORDER
+#if defined( FG_USE_NETWORK_BYTE_ORDER )
+
 // The function htond is defined this way due to the way some
 // processors and OSes treat floating point values.  Some will raise
 // an exception whenever a "bad" floating point value is loaded into a
@@ -69,6 +74,21 @@ static void htond (double &x)
         return;
     }
 }
+static void htonf (float &x)   
+{
+    if ( sgIsLittleEndian() ) {
+        int    *Float_Overlay;
+        int     Holding_Buffer;
+    
+        Float_Overlay = (int *) &x;
+        Holding_Buffer = Float_Overlay [0];
+    
+        Float_Overlay [0] = htonl (Holding_Buffer);
+    } else {
+        return;
+    }
+}
+#endif
 
 
 FGNativeGUI::FGNativeGUI() {
@@ -125,54 +145,121 @@ void FGProps2NetGUI( FGNetGUI *net ) {
         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
     }
 
-    // the following really aren't used in this context
+    // Environment
     net->cur_time = globals->get_time_params()->get_cur_time();
     net->warp = globals->get_warp();
+    net->ground_elev = globals->get_scenery()->get_cur_elev();
+
+    // Approach
+    net->tuned_freq = current_radiostack->get_navcom1()->get_nav_freq();
+    net->nav_radial = current_radiostack->get_navcom1()->get_nav_target_radial();
+    net->in_range = current_radiostack->get_navcom1()->get_nav_inrange();
+
+    if ( current_radiostack->get_navcom1()->get_nav_loc() ) {
+        // is an ILS
+        net->dist_nm
+            = current_radiostack->get_navcom1()->get_nav_gs_dist_signed()
+              * SG_METER_TO_NM;
+    } else {
+        // is a VOR
+        net->dist_nm = current_radiostack->get_navcom1()->get_nav_loc_dist()
+            * SG_METER_TO_NM;
+    }
 
+    net->course_deviation_deg
+        = current_radiostack->get_navcom1()->get_nav_reciprocal_radial()
+        - current_radiostack->get_navcom1()->get_nav_target_radial();
+    if ( net->course_deviation_deg < -1000.0 
+         || net->course_deviation_deg > 1000.0 )
+    {
+        // Sanity check ...
+        net->course_deviation_deg = 0.0;
+    }
+    while ( net->course_deviation_deg >  180.0 ) {
+        net->course_deviation_deg -= 360.0;
+    }
+    while ( net->course_deviation_deg < -180.0 ) {
+        net->course_deviation_deg += 360.0;
+    }
+    if ( fabs(net->course_deviation_deg) > 90.0 )
+        net->course_deviation_deg
+            = ( net->course_deviation_deg<0.0
+                ? -net->course_deviation_deg - 180.0
+                : -net->course_deviation_deg + 180.0 );
+
+    if ( current_radiostack->get_navcom1()->get_nav_loc() ) {
+        // is an ILS
+        net->gs_deviation_deg
+            = current_radiostack->get_navcom1()->get_nav_gs_deflection()
+            / 5.0;
+    } else {
+        // is an ILS
+        net->gs_deviation_deg = -9999.0;
+    }
+
+#if defined( FG_USE_NETWORK_BYTE_ORDER )
     // Convert the net buffer to network format
     net->version = htonl(net->version);
 
     htond(net->longitude);
     htond(net->latitude);
-    htond(net->altitude);
-    htond(net->phi);
-    htond(net->theta);
-    htond(net->psi);
-    htond(net->vcas);
-    htond(net->climb_rate);
+    htonf(net->altitude);
+    htonf(net->phi);
+    htonf(net->theta);
+    htonf(net->psi);
+    htonf(net->vcas);
+    htonf(net->climb_rate);
 
     for ( i = 0; i < net->num_tanks; ++i ) {
-        htond(net->fuel_quantity[i]);
+        htonf(net->fuel_quantity[i]);
     }
     net->num_tanks = htonl(net->num_tanks);
 
     net->cur_time = htonl( net->cur_time );
     net->warp = htonl( net->warp );
+    net->ground_elev = htonl( net->ground_elev );
+
+    htonf(net->tuned_freq);
+    htonf(net->nav_radial);
+    net->in_range = htonl(net->in_range);
+    htonf(net->dist_nm);
+    htonf(net->course_deviation_deg);
+    htonf(net->gs_deviation_deg);
+#endif
 }
 
 
 void FGNetGUI2Props( FGNetGUI *net ) {
     int i;
 
+#if defined( FG_USE_NETWORK_BYTE_ORDER )
     // Convert to the net buffer from network format
     net->version = ntohl(net->version);
 
     htond(net->longitude);
     htond(net->latitude);
-    htond(net->altitude);
-    htond(net->phi);
-    htond(net->theta);
-    htond(net->psi);
-    htond(net->vcas);
-    htond(net->climb_rate);
+    htonf(net->altitude);
+    htonf(net->phi);
+    htonf(net->theta);
+    htonf(net->psi);
+    htonf(net->vcas);
+    htonf(net->climb_rate);
 
     net->num_tanks = htonl(net->num_tanks);
     for ( i = 0; i < net->num_tanks; ++i ) {
-       htond(net->fuel_quantity[i]);
+       htonf(net->fuel_quantity[i]);
     }
 
     net->cur_time = ntohl(net->cur_time);
     net->warp = ntohl(net->warp);
+    net->ground_elev = htonl( net->ground_elev );
+
+    htonf(net->tuned_freq);
+    net->in_range = htonl(net->in_range);
+    htonf(net->dist_nm);
+    htonf(net->course_deviation_deg);
+    htonf(net->gs_deviation_deg);
+#endif
 
     if ( net->version == FG_NET_GUI_VERSION ) {
         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
@@ -200,6 +287,17 @@ void FGNetGUI2Props( FGNetGUI *net ) {
        }
 
         globals->set_warp( net->warp );
+        globals->get_scenery()->set_cur_elev( net->ground_elev );
+
+        // Approach
+        fgSetDouble( "/radios/nav[0]/frequencies/selected-mhz",
+                     net->tuned_freq );
+        fgSetBool( "/radios/nav[0]/in-range", net->in_range );
+        fgSetDouble( "/radios/dme/distance-nm", net->dist_nm );
+        fgSetDouble( "/radios/nav[0]/heading-needle-deflection",
+                     net->course_deviation_deg );
+        fgSetDouble( "/radios/nav[0]/gs-needle-deflection",
+                     net->gs_deviation_deg );
     } else {
        SG_LOG( SG_IO, SG_ALERT,
                 "Error: version mismatch in FGNetNativeGUI2Props()" );