]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/multiplay.cxx
Fix bug 191, uninitialised HUD color.
[flightgear.git] / src / Network / multiplay.cxx
index f6e0e30c7865f2b7000d34edb3b209f992ff31f2..a9cd32bf3823009c0fb6002270d321b993f35c35 100644 (file)
@@ -28,8 +28,7 @@
 
 #include <simgear/compiler.h>
 
-#include <string>
-
+#include <cstring>
 #include <iostream>
 #include <map>
 #include <string>
@@ -37,7 +36,7 @@
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/SGMath.hxx>
 
-#include <FDM/flight.hxx>
+#include <FDM/flightProperties.hxx>
 #include <MultiPlayer/mpmessages.hxx>
 
 #include "multiplay.hxx"
@@ -49,6 +48,24 @@ using std::string;
 const char sFG_MULTIPLAY_BID[] = "$Id$";
 const char sFG_MULTIPLAY_HID[] = FG_MULTIPLAY_HID;
 
+typedef std::set<std::string> string_set;
+
+class MPPropertyListener : public SGPropertyChangeListener
+{
+public:
+  MPPropertyListener(FGMultiplay* mp) :
+    _multiplay(mp)
+  {
+  }
+
+  virtual void childAdded(SGPropertyNode*, SGPropertyNode*)
+  {
+    _multiplay->setPropertiesChanged();
+  }
+
+private:
+  FGMultiplay* _multiplay;
+};
 
 /******************************************************************
 * Name: FGMultiplay
@@ -73,6 +90,7 @@ FGMultiplay::FGMultiplay (const string &dir, const int rate, const string &host,
 
   }
 
+  mPropertiesChanged = true;
 }
 
 
@@ -97,19 +115,38 @@ bool FGMultiplay::open() {
     }
 
     set_enabled(true);
-
-    SGPropertyNode* root = globals->get_props();
-
-    /// Build up the id to property map
     
-    for (unsigned i = 0; i < FGMultiplayMgr::numProperties; ++i) {
+    mPropertiesChanged = true;
+    
+    MPPropertyListener* pl = new MPPropertyListener(this);
+    globals->get_props()->addChangeListener(pl, false);
+    return is_enabled();
+}
+
+void FGMultiplay::findProperties()
+{
+  if (!mPropertiesChanged) {
+    return;
+  }
+  
+  mPropertiesChanged = false;
+  
+  for (unsigned i = 0; i < FGMultiplayMgr::numProperties; ++i) {
       const char* name = FGMultiplayMgr::sIdPropertyList[i].name;
-      SGPropertyNode* pNode = root->getNode(name);
-      if (pNode)
-        mPropertyMap[FGMultiplayMgr::sIdPropertyList[i].id] = pNode;
+      SGPropertyNode* pNode = globals->get_props()->getNode(name);
+      if (!pNode) {
+        continue;
+      }
+      
+      int id = FGMultiplayMgr::sIdPropertyList[i].id;
+      if (mPropertyMap.find(id) != mPropertyMap.end()) {
+        continue; // already activated
+      }
+      
+      mPropertyMap[id] = pNode;
+      SG_LOG(SG_NETWORK, SG_INFO, "activating MP property:" << pNode->getPath());
     }
 
-    return is_enabled();
 }
 
 
@@ -121,7 +158,8 @@ bool FGMultiplay::open() {
 bool FGMultiplay::process() {
   using namespace simgear;
   if (get_direction() == SG_IO_OUT) {
-
+    findProperties();
+    
     // check if we have left initialization phase. That will not provide
     // interresting data, also the freeze in simulation time hurts the
     // multiplayer clients
@@ -129,7 +167,7 @@ bool FGMultiplay::process() {
 //     if (sim_time < 20)
 //       return true;
 
-    FGInterface *ifce = cur_fdm_state;
+    FlightProperties ifce;
 
     // put together a motion info struct, you will get that later
     // from FGInterface directly ...
@@ -150,10 +188,10 @@ bool FGMultiplay::process() {
 
     // These are for now converted from lat/lon/alt and euler angles.
     // But this should change in FGInterface ...
-    double lon = ifce->get_Longitude();
-    double lat = ifce->get_Latitude();
+    double lon = ifce.get_Longitude();
+    double lat = ifce.get_Latitude();
     // first the aprioriate structure for the geodetic one
-    SGGeod geod = SGGeod::fromRadFt(lon, lat, ifce->get_Altitude());
+    SGGeod geod = SGGeod::fromRadFt(lon, lat, ifce.get_Altitude());
     // Convert to cartesion coordinate
     motionInfo.position = SGVec3d::fromGeod(geod);
     
@@ -161,28 +199,28 @@ bool FGMultiplay::process() {
     // horizontal local frame
     SGQuatf qEc2Hl = SGQuatf::fromLonLatRad((float)lon, (float)lat);
     // The orientation wrt the horizontal local frame
-    float heading = ifce->get_Psi();
-    float pitch = ifce->get_Theta();
-    float roll = ifce->get_Phi();
+    float heading = ifce.get_Psi();
+    float pitch = ifce.get_Theta();
+    float roll = ifce.get_Phi();
     SGQuatf hlOr = SGQuatf::fromYawPitchRoll(heading, pitch, roll);
     // The orientation of the vehicle wrt the earth centered frame
     motionInfo.orientation = qEc2Hl*hlOr;
 
-    if (!ifce->is_suspended()) {
+    if (!globals->get_subsystem("flight")->is_suspended()) {
       // velocities
-      motionInfo.linearVel = SG_FEET_TO_METER*SGVec3f(ifce->get_U_body(),
-                                                      ifce->get_V_body(),
-                                                      ifce->get_W_body());
-      motionInfo.angularVel = SGVec3f(ifce->get_P_body(),
-                                      ifce->get_Q_body(),
-                                      ifce->get_R_body());
+      motionInfo.linearVel = SG_FEET_TO_METER*SGVec3f(ifce.get_uBody(),
+                                                      ifce.get_vBody(),
+                                                      ifce.get_wBody());
+      motionInfo.angularVel = SGVec3f(ifce.get_P_body(),
+                                      ifce.get_Q_body(),
+                                      ifce.get_R_body());
       
       // accels, set that to zero for now.
       // Angular accelerations are missing from the interface anyway,
       // linear accelerations are screwed up at least for JSBSim.
-//  motionInfo.linearAccel = SG_FEET_TO_METER*SGVec3f(ifce->get_U_dot_body(),
-//                                                    ifce->get_V_dot_body(),
-//                                                    ifce->get_W_dot_body());
+//  motionInfo.linearAccel = SG_FEET_TO_METER*SGVec3f(ifce.get_U_dot_body(),
+//                                                    ifce.get_V_dot_body(),
+//                                                    ifce.get_W_dot_body());
       motionInfo.linearAccel = SGVec3f::zeros();
       motionInfo.angularAccel = SGVec3f::zeros();
     } else {
@@ -243,7 +281,7 @@ bool FGMultiplay::process() {
       motionInfo.properties.push_back(pData);
     }
 
-    FGMultiplayMgr* mpmgr = globals->get_multiplayer_mgr();
+    FGMultiplayMgr* mpmgr = (FGMultiplayMgr*) globals->get_subsystem("mp");
     mpmgr->SendMyPosition(motionInfo);
     
     // Now remove the data
@@ -270,9 +308,11 @@ bool FGMultiplay::process() {
 * Description:  Closes the multiplayer mgrs to stop any further
 * network processing
 ******************************************************************/
-bool FGMultiplay::close() {
-
-  FGMultiplayMgr *mgr = globals->get_multiplayer_mgr();
+bool FGMultiplay::close()
+{
+  mPropertyMap.clear();
+  
+  FGMultiplayMgr* mgr = (FGMultiplayMgr*) globals->get_subsystem("mp");
 
   if (mgr == 0) {
     return false;