From 1e8ae1a2940a53e867fc8d4bbadf8daca9b7f0da Mon Sep 17 00:00:00 2001
From: Seth Alves <seth.alves@gmail.com>
Date: Sat, 15 Sep 2018 16:36:55 -0700
Subject: [PATCH 1/9] fix scaling of worn shape entities

---
 libraries/entities-renderer/src/RenderableShapeEntityItem.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libraries/entities-renderer/src/RenderableShapeEntityItem.cpp b/libraries/entities-renderer/src/RenderableShapeEntityItem.cpp
index 71e3a0ff27..5003e36e86 100644
--- a/libraries/entities-renderer/src/RenderableShapeEntityItem.cpp
+++ b/libraries/entities-renderer/src/RenderableShapeEntityItem.cpp
@@ -97,10 +97,10 @@ void ShapeEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& sce
         withWriteLock([&] {
             auto entity = getEntity();
             _position = entity->getWorldPosition();
-            _dimensions = entity->getScaledDimensions();
+            _dimensions = entity->getUnscaledDimensions(); // get unscaled to avoid scaling twice
             _orientation = entity->getWorldOrientation();
             updateModelTransformAndBound();
-            _renderTransform = getModelTransform();
+            _renderTransform = getModelTransform(); // contains parent scale, if this entity scales with its parent
             if (_shape == entity::Sphere) {
                 _renderTransform.postScale(SPHERE_ENTITY_SCALE);
             }

From 46c8083fb994abec215a2cb933d8a2276801bf6c Mon Sep 17 00:00:00 2001
From: David Back <davidback@highfidelity.io>
Date: Wed, 19 Sep 2018 10:13:15 -0700
Subject: [PATCH 2/9] only call selectEntity one time per trigger

---
 .../controllerModules/inEditMode.js           | 27 ++++++++++---------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/scripts/system/controllers/controllerModules/inEditMode.js b/scripts/system/controllers/controllerModules/inEditMode.js
index 2bdd89f141..9f25a1b37c 100644
--- a/scripts/system/controllers/controllerModules/inEditMode.js
+++ b/scripts/system/controllers/controllerModules/inEditMode.js
@@ -73,21 +73,22 @@ Script.include("/~/system/libraries/utils.js");
                             method: "clearSelection",
                             hand: hand
                         }));
+                    } else {
+                        if (this.selectedTarget.type === Picks.INTERSECTED_ENTITY) {
+                            Messages.sendLocalMessage("entityToolUpdates", JSON.stringify({
+                                method: "selectEntity",
+                                entityID: this.selectedTarget.objectID,
+                                hand: hand
+                            }));
+                        } else if (this.selectedTarget.type === Picks.INTERSECTED_OVERLAY) {
+                            Messages.sendLocalMessage("entityToolUpdates", JSON.stringify({
+                                method: "selectOverlay",
+                                overlayID: this.selectedTarget.objectID,
+                                hand: hand
+                            }));
+                        }
                     }
                 }
-                if (this.selectedTarget.type === Picks.INTERSECTED_ENTITY) {
-                    Messages.sendLocalMessage("entityToolUpdates", JSON.stringify({
-                        method: "selectEntity",
-                        entityID: this.selectedTarget.objectID,
-                        hand: hand
-                    }));
-                } else if (this.selectedTarget.type === Picks.INTERSECTED_OVERLAY) {
-                    Messages.sendLocalMessage("entityToolUpdates", JSON.stringify({
-                        method: "selectOverlay",
-                        overlayID: this.selectedTarget.objectID,
-                        hand: hand
-                    }));
-                }
 
                 this.triggerClicked = true;
             }

From d5c0c05ab2bfcaa534ef6c22a207e28386041f5d Mon Sep 17 00:00:00 2001
From: Seth Alves <seth.alves@gmail.com>
Date: Thu, 20 Sep 2018 14:27:24 -0700
Subject: [PATCH 3/9] wait for an entity's script to load before adding it to
 the contain-avatar list.  do this so the script doesn't miss the
 'enterEntity' entity-method due to not being loaded.

---
 libraries/entities-renderer/src/EntityTreeRenderer.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp
index 3d782f69a7..4bbc09ff8a 100644
--- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp
+++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp
@@ -512,7 +512,11 @@ bool EntityTreeRenderer::findBestZoneAndMaybeContainingEntities(QVector<EntityIt
             // be ignored because they can have events fired on them.
             // FIXME - this could be optimized further by determining if the script is loaded
             // and if it has either an enterEntity or leaveEntity method
-            if (isZone || hasScript) {
+            //
+            // also, don't flag a scripted entity as containing the avatar until the script is loaded,
+            // so that the script is awake in time to receive the "entityEntity" call (even if the entity is a zone).
+            if ((!hasScript && isZone) ||
+                (hasScript && !entity->shouldPreloadScript())) {
                 // now check to see if the point contains our entity, this can be expensive if
                 // the entity has a collision hull
                 if (entity->contains(_avatarPosition)) {

From f6e57f54b00082d12b9983f4ac914312d6e64d8b Mon Sep 17 00:00:00 2001
From: Seth Alves <seth.alves@gmail.com>
Date: Fri, 21 Sep 2018 10:26:31 -0700
Subject: [PATCH 4/9] don't call enterEntity until script preload has finished

---
 .../src/EntityTreeRenderer.cpp                |  8 ++++++-
 libraries/entities/src/EntityItem.cpp         | 23 +++++++++++++++++++
 libraries/entities/src/EntityItem.h           | 10 ++++----
 libraries/script-engine/src/ScriptEngine.cpp  |  2 ++
 libraries/script-engine/src/ScriptEngine.h    |  7 ++++++
 5 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp
index 4bbc09ff8a..c78036d5ed 100644
--- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp
+++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp
@@ -187,6 +187,11 @@ void EntityTreeRenderer::resetEntitiesScriptEngine() {
     connect(entityScriptingInterface.data(), &EntityScriptingInterface::hoverLeaveEntity, _entitiesScriptEngine.data(), [&](const EntityItemID& entityID, const PointerEvent& event) {
         _entitiesScriptEngine->callEntityScriptMethod(entityID, "hoverLeaveEntity", event);
     });
+
+    connect(_entitiesScriptEngine.data(), &ScriptEngine::entityScriptPreloadFinished, [&](const EntityItemID& entityID) {
+        EntityItemPointer entity = getTree()->findEntityByID(entityID);
+        entity->setScriptHasFinishedPreload(true);
+    });
 }
 
 void EntityTreeRenderer::clear() {
@@ -516,7 +521,7 @@ bool EntityTreeRenderer::findBestZoneAndMaybeContainingEntities(QVector<EntityIt
             // also, don't flag a scripted entity as containing the avatar until the script is loaded,
             // so that the script is awake in time to receive the "entityEntity" call (even if the entity is a zone).
             if ((!hasScript && isZone) ||
-                (hasScript && !entity->shouldPreloadScript())) {
+                (hasScript && entity->isScriptPreloadFinished())) {
                 // now check to see if the point contains our entity, this can be expensive if
                 // the entity has a collision hull
                 if (entity->contains(_avatarPosition)) {
@@ -976,6 +981,7 @@ void EntityTreeRenderer::checkAndCallPreload(const EntityItemID& entityID, bool
             entity->scriptHasUnloaded();
         }
         if (shouldLoad) {
+            entity->setScriptHasFinishedPreload(false);
             _entitiesScriptEngine->loadEntityScript(entityID, resolveScriptURL(scriptUrl), reload);
             entity->scriptHasPreloaded();
         }
diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp
index 8e382fabd4..7a0e61b29a 100644
--- a/libraries/entities/src/EntityItem.cpp
+++ b/libraries/entities/src/EntityItem.cpp
@@ -3197,3 +3197,26 @@ void EntityItem::setCloneIDs(const QVector<QUuid>& cloneIDs) {
         _cloneIDs = cloneIDs;
     });
 }
+
+bool EntityItem::shouldPreloadScript() const {
+    return !_script.isEmpty() && ((_loadedScript != _script) || (_loadedScriptTimestamp != _scriptTimestamp));
+}
+
+void EntityItem::scriptHasPreloaded() {
+    _loadedScript = _script;
+    _loadedScriptTimestamp = _scriptTimestamp;
+}
+
+void EntityItem::scriptHasUnloaded() {
+    _loadedScript = "";
+    _loadedScriptTimestamp = 0;
+    _scriptPreloadFinished = false;
+}
+
+void EntityItem::setScriptHasFinishedPreload(bool value) {
+    _scriptPreloadFinished = value;
+}
+
+bool EntityItem::isScriptPreloadFinished() {
+    return _scriptPreloadFinished;
+}
diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h
index 490f9b9e6b..405b114ab3 100644
--- a/libraries/entities/src/EntityItem.h
+++ b/libraries/entities/src/EntityItem.h
@@ -470,10 +470,11 @@ public:
     /// We only want to preload if:
     ///    there is some script, and either the script value or the scriptTimestamp
     ///    value have changed since our last preload
-    bool shouldPreloadScript() const { return !_script.isEmpty() &&
-                                              ((_loadedScript != _script) || (_loadedScriptTimestamp != _scriptTimestamp)); }
-    void scriptHasPreloaded() { _loadedScript = _script; _loadedScriptTimestamp = _scriptTimestamp; }
-    void scriptHasUnloaded() { _loadedScript = ""; _loadedScriptTimestamp = 0; }
+    bool shouldPreloadScript() const;
+    void scriptHasPreloaded();
+    void scriptHasUnloaded();
+    void setScriptHasFinishedPreload(bool value);
+    bool isScriptPreloadFinished();
 
     bool getClientOnly() const { return _clientOnly; }
     virtual void setClientOnly(bool clientOnly) { _clientOnly = clientOnly; }
@@ -584,6 +585,7 @@ protected:
     QString _script { ENTITY_ITEM_DEFAULT_SCRIPT }; /// the value of the script property
     QString _loadedScript; /// the value of _script when the last preload signal was sent
     quint64 _scriptTimestamp { ENTITY_ITEM_DEFAULT_SCRIPT_TIMESTAMP }; /// the script loaded property used for forced reload
+    bool _scriptPreloadFinished { false };
 
     QString _serverScripts;
     /// keep track of time when _serverScripts property was last changed
diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp
index cfd155e14b..4d395070d6 100644
--- a/libraries/script-engine/src/ScriptEngine.cpp
+++ b/libraries/script-engine/src/ScriptEngine.cpp
@@ -2442,6 +2442,8 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co
     // if we got this far, then call the preload method
     callEntityScriptMethod(entityID, "preload");
 
+    emit entityScriptPreloadFinished(entityID);
+
     _occupiedScriptURLs.remove(entityScript);
     processDeferredEntityLoads(entityScript, entityID);
 }
diff --git a/libraries/script-engine/src/ScriptEngine.h b/libraries/script-engine/src/ScriptEngine.h
index 08e2c492e8..17afd3dbbd 100644
--- a/libraries/script-engine/src/ScriptEngine.h
+++ b/libraries/script-engine/src/ScriptEngine.h
@@ -712,6 +712,13 @@ signals:
     // script is updated (goes from RUNNING to ERROR_RUNNING_SCRIPT, for example)
     void entityScriptDetailsUpdated();
 
+    /**jsdoc
+     * @function Script.entityScriptPreloadFinished
+     * @returns {Signal}
+     */
+    // Emitted when an entity script has finished running preload
+    void entityScriptPreloadFinished(const EntityItemID& entityID);
+
 protected:
     void init();
 

From 6eb3fa251dbd59420b74a334462023c35961b1d6 Mon Sep 17 00:00:00 2001
From: Seth Alves <seth.alves@gmail.com>
Date: Fri, 21 Sep 2018 10:39:41 -0700
Subject: [PATCH 5/9] guard against null deref

---
 libraries/entities-renderer/src/EntityTreeRenderer.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp
index c78036d5ed..dbbf8af4b9 100644
--- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp
+++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp
@@ -190,7 +190,9 @@ void EntityTreeRenderer::resetEntitiesScriptEngine() {
 
     connect(_entitiesScriptEngine.data(), &ScriptEngine::entityScriptPreloadFinished, [&](const EntityItemID& entityID) {
         EntityItemPointer entity = getTree()->findEntityByID(entityID);
-        entity->setScriptHasFinishedPreload(true);
+        if (entity) {
+            entity->setScriptHasFinishedPreload(true);
+        }
     });
 }
 

From a8f7ff7d8a24d0734c37aae943862aa75ca55644 Mon Sep 17 00:00:00 2001
From: Ken Cooke <ken@highfidelity.io>
Date: Wed, 26 Sep 2018 18:18:20 -0700
Subject: [PATCH 6/9] Add HRTF reset()

---
 libraries/audio/src/AudioHRTF.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/libraries/audio/src/AudioHRTF.h b/libraries/audio/src/AudioHRTF.h
index 8993842d6e..c50b4dfc0b 100644
--- a/libraries/audio/src/AudioHRTF.h
+++ b/libraries/audio/src/AudioHRTF.h
@@ -13,6 +13,7 @@
 #define hifi_AudioHRTF_h
 
 #include <stdint.h>
+#include <string.h>
 
 static const int HRTF_AZIMUTHS = 72;    // 360 / 5-degree steps
 static const int HRTF_TAPS = 64;        // minimum-phase FIR coefficients
@@ -56,6 +57,27 @@ public:
     void setGainAdjustment(float gain) { _gainAdjust = HRTF_GAIN * gain; };
     float getGainAdjustment() { return _gainAdjust; }
 
+    // clear internal state, but retain settings
+    void reset() {
+        // FIR history
+        memset(_firState, 0, sizeof(_firState));
+
+        // integer delay history
+        memset(_delayState, 0, sizeof(_delayState));
+
+        // biquad history
+        memset(_bqState, 0, sizeof(_bqState));
+
+        // parameter history
+        _azimuthState = 0.0f;
+        _distanceState = 0.0f;
+        _gainState = 0.0f;
+
+        // _gainAdjust is retained
+
+        _silentState = false;
+    }
+
 private:
     AudioHRTF(const AudioHRTF&) = delete;
     AudioHRTF& operator=(const AudioHRTF&) = delete;

From 9dc0aa5c8d33b8ae7de1a2bb8eb3bd54d813bf2e Mon Sep 17 00:00:00 2001
From: Ken Cooke <ken@highfidelity.io>
Date: Wed, 26 Sep 2018 18:23:26 -0700
Subject: [PATCH 7/9] HRTF starts in silent state

---
 libraries/audio/src/AudioHRTF.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libraries/audio/src/AudioHRTF.h b/libraries/audio/src/AudioHRTF.h
index c50b4dfc0b..65b28bc5f8 100644
--- a/libraries/audio/src/AudioHRTF.h
+++ b/libraries/audio/src/AudioHRTF.h
@@ -75,7 +75,7 @@ public:
 
         // _gainAdjust is retained
 
-        _silentState = false;
+        _silentState = true;
     }
 
 private:
@@ -110,7 +110,7 @@ private:
     // global and local gain adjustment
     float _gainAdjust = HRTF_GAIN;
 
-    bool _silentState = false;
+    bool _silentState = true;
 };
 
 #endif // AudioHRTF_h

From f96c7f4db472ca27c5e137497226231a4e12a156 Mon Sep 17 00:00:00 2001
From: Howard Stearns <howard@highfidelity.io>
Date: Thu, 27 Sep 2018 17:05:18 -0700
Subject: [PATCH 8/9] minimal time handshake

---
 scripts/system/makeUserConnection.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/system/makeUserConnection.js b/scripts/system/makeUserConnection.js
index 5dee36d147..d205d368dd 100644
--- a/scripts/system/makeUserConnection.js
+++ b/scripts/system/makeUserConnection.js
@@ -32,7 +32,7 @@
     var WAITING_INTERVAL = 100; // ms
     var CONNECTING_INTERVAL = 100; // ms
     var MAKING_CONNECTION_TIMEOUT = 800; // ms
-    var CONNECTING_TIME = 1600; // ms
+    var CONNECTING_TIME = 100; // ms One interval.
     var PARTICLE_RADIUS = 0.15; // m
     var PARTICLE_ANGLE_INCREMENT = 360 / 45; // 1hz
     var HANDSHAKE_SOUND_URL = "https://s3-us-west-1.amazonaws.com/hifi-content/davidkelly/production/audio/4beat_sweep.wav";

From 440e2062f5db2a3a2bb4128a8351f5ca7a73c420 Mon Sep 17 00:00:00 2001
From: Kerry Ivan Kurian <kkurian@gmail.com>
Date: Thu, 27 Sep 2018 22:14:02 -0700
Subject: [PATCH 9/9] Fix application uninstall in marketplace item tester

---
 .../commerce/marketplaceItemTester/MarketplaceItemTester.qml    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/interface/resources/qml/hifi/commerce/marketplaceItemTester/MarketplaceItemTester.qml b/interface/resources/qml/hifi/commerce/marketplaceItemTester/MarketplaceItemTester.qml
index 8f391f24c0..c3d87ca2f5 100644
--- a/interface/resources/qml/hifi/commerce/marketplaceItemTester/MarketplaceItemTester.qml
+++ b/interface/resources/qml/hifi/commerce/marketplaceItemTester/MarketplaceItemTester.qml
@@ -131,7 +131,7 @@ Rectangle {
                             print("Marketplace item tester unsupported assetType " + assetType);
                     }
                 },
-                "trash": function(){
+                "trash": function(resource, assetType){
                     if ("application" === assetType) {
                         Commerce.uninstallApp(resource);
                     }