]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIMultiplayer.cxx
Vivian Meazza: AI escorts
[flightgear.git] / src / AIModel / AIMultiplayer.cxx
old mode 100755 (executable)
new mode 100644 (file)
index dc9d7e9..ab9cc5c
@@ -29,6 +29,8 @@
 
 #include "AIMultiplayer.hxx"
 
+#include <simgear/scene/util/SGNodeMasks.hxx>
+
 // #define SG_DEBUG SG_ALERT
 
 FGAIMultiplayer::FGAIMultiplayer() : FGAIBase(otMultiplayer) {
@@ -37,27 +39,31 @@ FGAIMultiplayer::FGAIMultiplayer() : FGAIBase(otMultiplayer) {
    mTimeOffsetSet = false;
    mAllowExtrapolation = true;
    mLagAdjustSystemSpeed = 10;
+
+   aip.getSceneGraph()->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
+
 }
 
 
 FGAIMultiplayer::~FGAIMultiplayer() {
 }
 
-bool FGAIMultiplayer::init() {
+bool FGAIMultiplayer::init(bool search_in_AI_path) {
+    props->setStringValue("sim/model/path", model_path.c_str());
     //refuel_node = fgGetNode("systems/refuel/contact", true);
     isTanker = false; // do this until this property is
                       // passed over the net
 
-    string str1 = mCallSign;
+    string str1 = _getCallsign();
     string str2 = "MOBIL";
 
-    unsigned int loc1= str1.find( str2, 0 );
+    string::size_type loc1= str1.find( str2, 0 );
     if ( (loc1 != string::npos && str2 != "") ){
         //        cout << " string found "     << str2 << " in " << str1 << endl;
         isTanker = true;
         //        cout << "isTanker " << isTanker << " " << mCallSign <<endl;
     }
-   return FGAIBase::init();
+   return FGAIBase::init(search_in_AI_path);
 }
 
 void FGAIMultiplayer::bind() {
@@ -73,7 +79,7 @@ SGRawValueMethods<FGAIMultiplayer, type>(*this, &FGAIMultiplayer::get##name)
 SGRawValueMethods<FGAIMultiplayer, type>(*this, \
       &FGAIMultiplayer::get##name, &FGAIMultiplayer::set##name)
 
-    props->tie("callsign", AIMPROProp(const char *, CallSign));
+    //props->tie("callsign", AIMPROProp(const char *, CallSign));
 
     props->tie("controls/allow-extrapolation",
                AIMPRWProp(bool, AllowExtrapolation));
@@ -88,7 +94,7 @@ SGRawValueMethods<FGAIMultiplayer, type>(*this, \
 void FGAIMultiplayer::unbind() {
     FGAIBase::unbind();
 
-    props->untie("callsign");
+    //props->untie("callsign");
     props->untie("controls/allow-extrapolation");
     props->untie("controls/lag-adjust-system-speed");
     props->untie("refuel/contact");
@@ -96,6 +102,8 @@ void FGAIMultiplayer::unbind() {
 
 void FGAIMultiplayer::update(double dt)
 {
+  using namespace simgear;
+
   if (dt <= 0)
     return;
 
@@ -129,7 +137,8 @@ void FGAIMultiplayer::update(double dt)
     mTimeOffset = curentPkgTime - curtime - lag;
   } else {
     double offset = curentPkgTime - curtime - lag;
-    if (!mAllowExtrapolation && offset + lag < mTimeOffset) {
+    if ((!mAllowExtrapolation && offset + lag < mTimeOffset)
+        || (offset - 10 > mTimeOffset)) {
       mTimeOffset = offset;
       SG_LOG(SG_GENERAL, SG_DEBUG, "Resetting time offset adjust system to "
              "avoid extrapolation: time offset = " << mTimeOffset);
@@ -198,19 +207,19 @@ void FGAIMultiplayer::update(double dt)
         {
           //cout << "Found " << pIt->second->getPath() << ":";
           switch ((*firstPropIt)->type) {
-            case SGPropertyNode::INT:  
-            case SGPropertyNode::BOOL:
-            case SGPropertyNode::LONG:        
+            case props::INT:
+            case props::BOOL:
+            case props::LONG:
               pIt->second->setIntValue((*firstPropIt)->int_value);
               //cout << "Int: " << (*firstPropIt)->int_value << "\n";
               break;
-            case SGPropertyNode::FLOAT:
-            case SGPropertyNode::DOUBLE:
+            case props::FLOAT:
+            case props::DOUBLE:
               pIt->second->setFloatValue((*firstPropIt)->float_value);
               //cout << "Flo: " << (*firstPropIt)->float_value << "\n";
               break;
-            case SGPropertyNode::STRING:
-            case SGPropertyNode::UNSPECIFIED:
+            case props::STRING:
+            case props::UNSPECIFIED:
               pIt->second->setStringValue((*firstPropIt)->string_value);
               //cout << "Str: " << (*firstPropIt)->string_value << "\n";    
               break;
@@ -272,23 +281,23 @@ void FGAIMultiplayer::update(double dt)
             int ival;
             float val;
             switch ((*prevPropIt)->type) {
-              case SGPropertyNode::INT:   
-              case SGPropertyNode::BOOL:
-              case SGPropertyNode::LONG:        
-                ival = (int) (1-tau)*((double) (*prevPropIt)->int_value) +
-                  tau*((double) (*nextPropIt)->int_value);
+              case props::INT:
+              case props::BOOL:
+              case props::LONG:
+                ival = (int) (0.5+(1-tau)*((double) (*prevPropIt)->int_value) +
+                  tau*((double) (*nextPropIt)->int_value));
                 pIt->second->setIntValue(ival);
                 //cout << "Int: " << ival << "\n";
                 break;
-              case SGPropertyNode::FLOAT:
-              case SGPropertyNode::DOUBLE:
+              case props::FLOAT:
+              case props::DOUBLE:
                 val = (1-tau)*(*prevPropIt)->float_value +
                   tau*(*nextPropIt)->float_value;
                 //cout << "Flo: " << val << "\n";
                 pIt->second->setFloatValue(val);
                 break;
-              case SGPropertyNode::STRING:
-              case SGPropertyNode::UNSPECIFIED:
+              case props::STRING:
+              case props::UNSPECIFIED:
                 //cout << "Str: " << (*nextPropIt)->string_value << "\n";
                 pIt->second->setStringValue((*nextPropIt)->string_value);
                 break;
@@ -385,19 +394,19 @@ void FGAIMultiplayer::update(double dt)
       if (pIt != mPropertyMap.end())
       {
         switch ((*firstPropIt)->type) {
-          case SGPropertyNode::INT:      
-          case SGPropertyNode::BOOL:
-          case SGPropertyNode::LONG:        
+          case props::INT:
+          case props::BOOL:
+          case props::LONG:
             pIt->second->setIntValue((*firstPropIt)->int_value);
             //cout << "Int: " << (*firstPropIt)->int_value << "\n";
             break;
-          case SGPropertyNode::FLOAT:
-          case SGPropertyNode::DOUBLE:
+          case props::FLOAT:
+          case props::DOUBLE:
             pIt->second->setFloatValue((*firstPropIt)->float_value);
             //cout << "Flo: " << (*firstPropIt)->float_value << "\n";
             break;
-          case SGPropertyNode::STRING:
-          case SGPropertyNode::UNSPECIFIED:
+          case props::STRING:
+          case props::UNSPECIFIED:
             pIt->second->setStringValue((*firstPropIt)->string_value);
             //cout << "Str: " << (*firstPropIt)->string_value << "\n";
             break;
@@ -468,9 +477,18 @@ FGAIMultiplayer::addMotionInfo(const FGExternalMotionData& motionInfo,
                                long stamp)
 {
   mLastTimestamp = stamp;
-  // Drop packets arriving out of order
-  if (!mMotionInfo.empty() && motionInfo.time < mMotionInfo.rbegin()->first)
-    return;
+
+  if (!mMotionInfo.empty()) {
+    double diff = motionInfo.time - mMotionInfo.rbegin()->first;
+
+    // packet is very old -- MP has probably reset (incl. his timebase)
+    if (diff < -10.0)
+      mMotionInfo.clear();
+
+    // drop packets arriving out of order
+    else if (diff < 0.0)
+      return;
+  }
   mMotionInfo[motionInfo.time] = motionInfo;
 }