]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/YASim/Hitch.cpp
FGPUIDialog: fix reading from already free'd memory.
[flightgear.git] / src / FDM / YASim / Hitch.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 97458f1..45f9bd9
@@ -1,3 +1,7 @@
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include "Math.hpp"
 #include "BodyEnvironment.hpp"
 #include "RigidBody.hpp"
 
 
 #include "Hitch.hpp"
+
+using std::vector;
+
 namespace yasim {
 Hitch::Hitch(const char *name)
 {
-    _node = fgGetNode("/sim/hitches", true)->getNode(name, true);
+    if (fgGetNode("/sim/hitches", true))
+        _node = fgGetNode("/sim/hitches", true)->getNode(name, true);
+    else _node = 0;
     int i;
     for(i=0; i<3; i++)
         _pos[i] = _force[i] = _winchPos[i] = _mp_lpos[i]=_towEndForce[i]=_mp_force[i]=0;
@@ -60,90 +69,57 @@ Hitch::Hitch(const char *name)
     _mp_open_last_state=false;
     _timeLagCorrectedDist=0;
 
-    _node->tie("tow/length",SGRawValuePointer<float>(&_towLength));
-    _node->tie("tow/elastic-constant",SGRawValuePointer<float>(&_towElasticConstant));
-    _node->tie("tow/weight-per-m-kg-m",SGRawValuePointer<float>(&_towWeightPerM));
-    _node->tie("tow/brake-force",SGRawValuePointer<float>(&_towBrakeForce));
-    _node->tie("winch/max-speed-m-s",SGRawValuePointer<float>(&_winchMaxSpeed));
-    _node->tie("winch/rel-speed",SGRawValuePointer<float>(&_winchRelSpeed));
-    _node->tie("winch/initial-tow-length-m",SGRawValuePointer<float>(&_winchInitialTowLength));
-    _node->tie("winch/min-tow-length-m",SGRawValuePointer<float>(&_winchMinTowLength));
-    _node->tie("winch/max-tow-length-m",SGRawValuePointer<float>(&_winchMaxTowLength));
-    _node->tie("winch/global-pos-x",SGRawValuePointer<double>(&_winchPos[0]));
-    _node->tie("winch/global-pos-y",SGRawValuePointer<double>(&_winchPos[1]));
-    _node->tie("winch/global-pos-z",SGRawValuePointer<double>(&_winchPos[2]));
-    _node->tie("winch/max-power",SGRawValuePointer<float>(&_winchPower));
-    _node->tie("winch/max-force",SGRawValuePointer<float>(&_winchMaxForce));
-    _node->tie("winch/actual-force",SGRawValuePointer<float>(&_winchActualForce));
-    _node->tie("tow/end-force-x",SGRawValuePointer<float>(&_reportTowEndForce[0]));
-    _node->tie("tow/end-force-y",SGRawValuePointer<float>(&_reportTowEndForce[1]));
-    _node->tie("tow/end-force-z",SGRawValuePointer<float>(&_reportTowEndForce[2]));
-    _node->tie("force",SGRawValuePointer<float>(&_forceMagnitude));
-    _node->tie("open",SGRawValuePointer<bool>(&_open));
-    _node->tie("force-is-calculated-by-other",SGRawValuePointer<bool>(&_forceIsCalculatedByMaster));
-    _node->tie("local-pos-x",SGRawValuePointer<float>(&_pos[0]));
-    _node->tie("local-pos-y",SGRawValuePointer<float>(&_pos[1]));
-    _node->tie("local-pos-z",SGRawValuePointer<float>(&_pos[2]));
-    _node->tie("tow/dist",SGRawValuePointer<float>(&_dist));
-    _node->tie("tow/dist-time-lag-corrected",SGRawValuePointer<float>(&_timeLagCorrectedDist));
-    _node->tie("tow/connected-to-property-node",SGRawValuePointer<bool>(&_towEndIsConnectedToProperty));
-    _node->tie("tow/connected-to-mp-node",SGRawValuePointer<bool>(&_nodeIsMultiplayer));
-    _node->tie("tow/connected-to-ai-node",SGRawValuePointer<bool>(&_nodeIsAiAircraft));
-    _node->tie("tow/connected-to-ai-or-mp-id",SGRawValuePointer<int>(&_nodeID));
-    _node->tie("debug/hitch-height-above-ground",SGRawValuePointer<float>(&_height_above_ground));
-    _node->tie("debug/tow-end-height-above-ground",SGRawValuePointer<float>(&_winch_height_above_ground));
-    _node->tie("debug/tow-rel-lo-pos",SGRawValuePointer<float>(&_loPosFrac));
-    _node->tie("debug/tow-lowest-pos-height",SGRawValuePointer<float>(&_lowest_tow_height));
-    _node->tie("is-slave",SGRawValuePointer<bool>(&_isSlave));
-    _node->tie("speed-in-tow-direction",SGRawValuePointer<float>(&_speed_in_tow_direction));
-    _node->tie("mp-auto-connect-period",SGRawValuePointer<float>(&_mpAutoConnectPeriod));
-    _node->tie("mp-time-lag",SGRawValuePointer<float>(&_mp_time_lag));
-    _node->setStringValue("tow/node","");
-    _node->setStringValue("tow/connected-to-ai-or-mp-callsign");
-    _node->setBoolValue("broken",false);
+    if (_node)
+    {
+#define TIE(x,v) _tiedProperties.Tie(_node->getNode(x, true),v)
+        TIE("tow/length", SGRawValuePointer<float>(&_towLength));
+        TIE("tow/elastic-constant",SGRawValuePointer<float>(&_towElasticConstant));
+        TIE("tow/weight-per-m-kg-m",SGRawValuePointer<float>(&_towWeightPerM));
+        TIE("tow/brake-force",SGRawValuePointer<float>(&_towBrakeForce));
+        TIE("winch/max-speed-m-s",SGRawValuePointer<float>(&_winchMaxSpeed));
+        TIE("winch/rel-speed",SGRawValuePointer<float>(&_winchRelSpeed));
+        TIE("winch/initial-tow-length-m",SGRawValuePointer<float>(&_winchInitialTowLength));
+        TIE("winch/min-tow-length-m",SGRawValuePointer<float>(&_winchMinTowLength));
+        TIE("winch/max-tow-length-m",SGRawValuePointer<float>(&_winchMaxTowLength));
+        TIE("winch/global-pos-x",SGRawValuePointer<double>(&_winchPos[0]));
+        TIE("winch/global-pos-y",SGRawValuePointer<double>(&_winchPos[1]));
+        TIE("winch/global-pos-z",SGRawValuePointer<double>(&_winchPos[2]));
+        TIE("winch/max-power",SGRawValuePointer<float>(&_winchPower));
+        TIE("winch/max-force",SGRawValuePointer<float>(&_winchMaxForce));
+        TIE("winch/actual-force",SGRawValuePointer<float>(&_winchActualForce));
+        TIE("tow/end-force-x",SGRawValuePointer<float>(&_reportTowEndForce[0]));
+        TIE("tow/end-force-y",SGRawValuePointer<float>(&_reportTowEndForce[1]));
+        TIE("tow/end-force-z",SGRawValuePointer<float>(&_reportTowEndForce[2]));
+        TIE("force",SGRawValuePointer<float>(&_forceMagnitude));
+        TIE("open",SGRawValuePointer<bool>(&_open));
+        TIE("force-is-calculated-by-other",SGRawValuePointer<bool>(&_forceIsCalculatedByMaster));
+        TIE("local-pos-x",SGRawValuePointer<float>(&_pos[0]));
+        TIE("local-pos-y",SGRawValuePointer<float>(&_pos[1]));
+        TIE("local-pos-z",SGRawValuePointer<float>(&_pos[2]));
+        TIE("tow/dist",SGRawValuePointer<float>(&_dist));
+        TIE("tow/dist-time-lag-corrected",SGRawValuePointer<float>(&_timeLagCorrectedDist));
+        TIE("tow/connected-to-property-node",SGRawValuePointer<bool>(&_towEndIsConnectedToProperty));
+        TIE("tow/connected-to-mp-node",SGRawValuePointer<bool>(&_nodeIsMultiplayer));
+        TIE("tow/connected-to-ai-node",SGRawValuePointer<bool>(&_nodeIsAiAircraft));
+        TIE("tow/connected-to-ai-or-mp-id",SGRawValuePointer<int>(&_nodeID));
+        TIE("debug/hitch-height-above-ground",SGRawValuePointer<float>(&_height_above_ground));
+        TIE("debug/tow-end-height-above-ground",SGRawValuePointer<float>(&_winch_height_above_ground));
+        TIE("debug/tow-rel-lo-pos",SGRawValuePointer<float>(&_loPosFrac));
+        TIE("debug/tow-lowest-pos-height",SGRawValuePointer<float>(&_lowest_tow_height));
+        TIE("is-slave",SGRawValuePointer<bool>(&_isSlave));
+        TIE("speed-in-tow-direction",SGRawValuePointer<float>(&_speed_in_tow_direction));
+        TIE("mp-auto-connect-period",SGRawValuePointer<float>(&_mpAutoConnectPeriod));
+        TIE("mp-time-lag",SGRawValuePointer<float>(&_mp_time_lag));
+#undef TIE
+        _node->setStringValue("tow/node","");
+        _node->setStringValue("tow/connected-to-ai-or-mp-callsign");
+        _node->setBoolValue("broken",false);
+    }
 }
 
 Hitch::~Hitch()
 {
-    _node->untie("tow/length");
-    _node->untie("tow/elastic-constant");
-    _node->untie("tow/weight-per-m-kg-m");
-    _node->untie("tow/brake-force");
-    _node->untie("winch/max-speed-m-s");
-    _node->untie("winch/rel-speed");
-    _node->untie("winch/initial-tow-length-m");
-    _node->untie("winch/min-tow-length-m");
-    _node->untie("winch/max-tow-length-m");
-    _node->untie("winch/global-pos-x");
-    _node->untie("winch/global-pos-y");
-    _node->untie("winch/global-pos-z");
-    _node->untie("winch/max-power");
-    _node->untie("winch/max-force");
-    _node->untie("winch/actual-force");
-    _node->untie("tow/end-force-x");
-    _node->untie("tow/end-force-y");
-    _node->untie("tow/end-force-z");
-    _node->untie("force");
-    _node->untie("open");
-    _node->untie("force-is-calculated-by-other");
-    _node->untie("local-pos-x");
-    _node->untie("local-pos-y");
-    _node->untie("local-pos-z");
-    _node->untie("tow/dist");
-    _node->untie("tow/dist-time-lag-corrected");
-    _node->untie("tow/connected-to-property-node");
-    _node->untie("tow/connected-to-mp-node");
-    _node->untie("tow/connected-to-ai-node");
-    _node->untie("tow/connected-to-ai-or-mp-id");
-    _node->untie("debug/hitch-height-above-ground");
-    _node->untie("debug/tow-end-height-above-ground");
-    _node->untie("debug/tow-rel-lo-pos");
-    _node->untie("debug/tow-lowest-pos-height");
-    _node->untie("is-slave");
-    _node->untie("speed-in-tow-direction");
-    _node->untie("mp-auto-connect-period");
-    _node->untie("mp-time-lag");
-
+    _tiedProperties.Untie();
     delete _state;
 }
 
@@ -218,12 +194,12 @@ void Hitch::setForceIsCalculatedByOther(bool b)
     _forceIsCalculatedByMaster=b;
 }
 
-const char *Hitch::getConnectedPropertyNode() const
+std::string Hitch::getConnectedPropertyNode() const
 {
     if (_towEndNode)
         return _towEndNode->getDisplayName();
     else
-        return 0;
+        return std::string("");
 }
 
 void Hitch::setConnectedPropertyNode(const char *nodename)
@@ -580,8 +556,8 @@ void Hitch::integrate (float dt)
             if (_dist>_towLength*1.00001)
             {
                 std::stringstream message;
-                message<<"Could not lock hitch (tow length is insufficient) on hitch '"
-                       <<_node->getPath()<<"' !";
+                message<<"Could not lock hitch (tow length is insufficient) on hitch "
+                       <<_node->getName()<<" "<<_node->getIndex()<<"!";
                 fgSetString("/sim/messages/pilot", message.str().c_str());
                 _open=true;
                 return;
@@ -592,7 +568,7 @@ void Hitch::integrate (float dt)
         if (_node->getBoolValue("broken",false)&&_open)
             message<<"Oh no, the tow is broken";
         else
-            message<<(_open?"Opened hitch '":"Locked hitch '")<<_node->getPath()<<"'!";
+            message<<(_open?"Opened hitch ":"Locked hitch ")<<_node->getName()<<" "<<_node->getIndex()<<"!";
         fgSetString("/sim/messages/pilot", message.str().c_str());
         _oldOpen=_open;
     }
@@ -728,13 +704,8 @@ void Hitch::integrate (float dt)
             }
         }
     }
-    //set the _reported_tow_end_force (smoothed)
-    //smooth it a bit and store it
-    float sc=10.*dt; //100ms
-    float tmp[3];
-    Math::mul3(sc,_towEndForce,tmp);
-    Math::mul3(1.-sc,_reportTowEndForce,_reportTowEndForce);
-    Math::add3(tmp,_reportTowEndForce,_reportTowEndForce);
+    //set the _reported_tow_end_force
+    Math::set3(_towEndForce,_reportTowEndForce);
 
     if (_open) return;
     if (_winchRelSpeed==0) return;