From 61076826580d31fef97ad6240dc3e3154ed0bbbf Mon Sep 17 00:00:00 2001
From: Seth Alves <seth.alves@gmail.com>
Date: Mon, 20 Apr 2015 16:36:56 -0700
Subject: [PATCH] handle ignoring simulation data echoed back to us differently

---
 libraries/entities/src/EntityItem.cpp        | 27 ++++++++++++++++++--
 libraries/entities/src/EntityItem.h          |  3 +--
 libraries/entities/src/EntityTreeElement.cpp | 11 +-------
 3 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp
index 512687d224..d221c0bba4 100644
--- a/libraries/entities/src/EntityItem.cpp
+++ b/libraries/entities/src/EntityItem.cpp
@@ -313,8 +313,7 @@ int EntityItem::expectedBytes() {
 }
 
 
-int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLeftToRead, ReadBitstreamToTreeParams& args,
-                                         bool ignoreServerPacket) {
+int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLeftToRead, ReadBitstreamToTreeParams& args) {
 
     if (args.bitstreamVersion < VERSION_ENTITIES_SUPPORT_SPLIT_MTU) {
     
@@ -325,6 +324,14 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
         return 0;
     }
 
+    // if this bitstream indicates that this node is the simulation owner, ignore any physics-related updates.
+    glm::vec3 savePosition = _position;
+    glm::quat saveRotation = _rotation;
+    glm::vec3 saveVelocity = _velocity;
+    glm::vec3 saveGravity = _gravity;
+    glm::vec3 saveAcceleration = _acceleration;
+
+
     // Header bytes
     //    object ID [16 bytes]
     //    ByteCountCoded(type code) [~1 byte]
@@ -398,6 +405,8 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
         quint64 lastEditedFromBuffer = 0;
         quint64 lastEditedFromBufferAdjusted = 0;
 
+        bool ignoreServerPacket = false;
+
         // TODO: we could make this encoded as a delta from _created
         // _lastEdited
         memcpy(&lastEditedFromBuffer, dataAt, sizeof(lastEditedFromBuffer));
@@ -608,6 +617,20 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef
             _lastSimulated = now;
         }
     }
+
+
+    auto nodeList = DependencyManager::get<NodeList>();
+    const QUuid& myNodeID = nodeList->getSessionUUID();
+    if (_simulatorID == myNodeID) {
+        // the packet that produced this bitstream originally came from physics simulations performed by
+        // this node, so our version has to be newer than what the packet contained.
+        _position = savePosition;
+        _rotation = saveRotation;
+        _velocity = saveVelocity;
+        _gravity = saveGravity;
+        _acceleration = saveAcceleration;
+    }
+
     return bytesRead;
 }
 
diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h
index 3a6a00d2ba..ad856698a2 100644
--- a/libraries/entities/src/EntityItem.h
+++ b/libraries/entities/src/EntityItem.h
@@ -113,8 +113,7 @@ public:
     static EntityItemID readEntityItemIDFromBuffer(const unsigned char* data, int bytesLeftToRead, 
                                     ReadBitstreamToTreeParams& args);
 
-    virtual int readEntityDataFromBuffer(const unsigned char* data, int bytesLeftToRead, ReadBitstreamToTreeParams& args,
-                                         bool ignoreServerPacket = false);
+    virtual int readEntityDataFromBuffer(const unsigned char* data, int bytesLeftToRead, ReadBitstreamToTreeParams& args);
 
     virtual int readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead, 
                                                 ReadBitstreamToTreeParams& args,
diff --git a/libraries/entities/src/EntityTreeElement.cpp b/libraries/entities/src/EntityTreeElement.cpp
index 9de7329594..349bd51372 100644
--- a/libraries/entities/src/EntityTreeElement.cpp
+++ b/libraries/entities/src/EntityTreeElement.cpp
@@ -716,10 +716,6 @@ int EntityTreeElement::readElementDataFromBuffer(const unsigned char* data, int
         bytesRead += sizeof(numberOfEntities);
 
         if (bytesLeftToRead >= (int)(numberOfEntities * expectedBytesPerEntity)) {
-            // look up the Id of this Node
-            auto nodeList = DependencyManager::get<NodeList>();
-            QUuid myNodeID = nodeList->getSessionUUID();
-
             for (uint16_t i = 0; i < numberOfEntities; i++) {
                 int bytesForThisEntity = 0;
                 EntityItemID entityItemID;
@@ -744,12 +740,7 @@ int EntityTreeElement::readElementDataFromBuffer(const unsigned char* data, int
                     bool bestFitBefore = bestFitEntityBounds(entityItem);
                     EntityTreeElement* currentContainingElement = _myTree->getContainingElement(entityItemID);
 
-                    // this Node was the original source of this packet, so read it, but ignore it.
-                    /// XXX what should be done here?  Do we need to ignore packets which show us as simulator owner?  XXX
-                    // bool shouldIgnore = (entityItem && entityItem->getSimulatorID() == myNodeID); XXX
-                    bool shouldIgnore = false;
-
-                    bytesForThisEntity = entityItem->readEntityDataFromBuffer(dataAt, bytesLeftToRead, args, shouldIgnore);
+                    bytesForThisEntity = entityItem->readEntityDataFromBuffer(dataAt, bytesLeftToRead, args);
                     if (entityItem->getDirtyFlags()) {
                         _myTree->entityChanged(entityItem);
                     }