From 503d2874ab3887c8e36f80991fdb5580ef5ac15c Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 8 Mar 2016 10:59:01 -0800
Subject: [PATCH 01/39] initial vr vj set up

---
 examples/VR-VJ/VRVJSoundEntityScript.js | 35 +++++++++++++++++++
 examples/VR-VJ/cartridgesSpawner.js     | 45 +++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 examples/VR-VJ/VRVJSoundEntityScript.js
 create mode 100644 examples/VR-VJ/cartridgesSpawner.js

diff --git a/examples/VR-VJ/VRVJSoundEntityScript.js b/examples/VR-VJ/VRVJSoundEntityScript.js
new file mode 100644
index 0000000000..68990ca51f
--- /dev/null
+++ b/examples/VR-VJ/VRVJSoundEntityScript.js
@@ -0,0 +1,35 @@
+
+(function() {
+    var _this;
+    Script.include("../libraries/utils.js");
+    VRVJSoundEntity = function() {
+        _this = this;
+     
+    };
+
+    VRVJSoundEntity.prototype = {
+        playSound: function() {
+            _this.soundInjector = Audio.playSound(_this.clip, {position: _this.position, volume: 1.0});
+        },
+
+        preload: function(entityID) {
+            _this.entityID = entityID;
+            _this.position = Entities.getEntityProperties(_this.entityID, "position").position;
+            _this.userData  = getEntityUserData(_this.entityID);
+            _this.clip = SoundCache.getSound(_this.userData.soundURL);
+
+        },
+
+        unload: function() {
+            if (_this.soundInjector) {
+                _this.soundInjector.stop();
+                delete _this.soundInjector;
+            }
+        }
+
+   
+    };
+
+    // entity scripts always need to return a newly constructed object of our type
+    return new VRVJSoundEntity();
+});
\ No newline at end of file
diff --git a/examples/VR-VJ/cartridgesSpawner.js b/examples/VR-VJ/cartridgesSpawner.js
new file mode 100644
index 0000000000..6169a9a421
--- /dev/null
+++ b/examples/VR-VJ/cartridgesSpawner.js
@@ -0,0 +1,45 @@
+  var orientation = Camera.getOrientation();
+  orientation = Quat.safeEulerAngles(orientation);
+  orientation.x = 0;
+  orientation = Quat.fromVec3Degrees(orientation);
+  var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
+
+
+Script.include("../libraries/utils.js");
+
+var SOUND_SCRIPT_URL = Script.resolvePath("VRVJSoundEntityScript.js");
+var soundEntity = Entities.addEntity({
+      type: "Box",
+      dimensions: {x: 0.1, y: 0.1, z: 0.1},
+      color: {red: 200, green: 10, blue: 200},
+      position: center,
+      damping: 1,
+      dynamic: true,
+      script: SOUND_SCRIPT_URL,
+      userData: JSON.stringify({
+        soundURL: "https://s3-us-west-1.amazonaws.com/hifi-content/eric/Sounds/VRVJ/Synth-MarchToWar.wav",
+      }),
+});
+
+var VISUAL_SCRIPT_URL = Script.resolvePath("VRVJVisualEntityScript.js")
+var visualEntity = Entities.addEntity({
+    type: "Sphere",
+    dimensions: {x: 0.1, y: 0.1, z: 0.1},
+    damping: 1,
+    color: {red: 0, green: 200, blue: 10},
+    dynamic: true,
+    position: Vec3.sum(center, {x: 0, y: 0.2, z: 0}),
+    // script: VISUAL_SCRIPT_URLs
+})
+
+Script.setTimeout(function() {
+    // Wait for sounds to load
+    Entities.callEntityMethod(soundEntity, "playSound");
+}, 1000)
+
+function cleanup() {
+    Entities.deleteEntity(soundEntity);
+    Entities.deleteEntity(visualEntity);
+}
+
+Script.scriptEnding.connect(cleanup);
\ No newline at end of file

From 9edc8aa5aad36172ef5e79874b0696c8b26f0072 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 8 Mar 2016 11:34:00 -0800
Subject: [PATCH 02/39] searching for name

---
 ...t.js => VRVJSoundCartridgeEntityScript.js} |  0
 .../VR-VJ/VRVJVisualCartridgeEntityScript.js  | 30 +++++++++++++++++++
 examples/VR-VJ/cartridgesSpawner.js           | 13 ++++----
 3 files changed, 38 insertions(+), 5 deletions(-)
 rename examples/VR-VJ/{VRVJSoundEntityScript.js => VRVJSoundCartridgeEntityScript.js} (100%)
 create mode 100644 examples/VR-VJ/VRVJVisualCartridgeEntityScript.js

diff --git a/examples/VR-VJ/VRVJSoundEntityScript.js b/examples/VR-VJ/VRVJSoundCartridgeEntityScript.js
similarity index 100%
rename from examples/VR-VJ/VRVJSoundEntityScript.js
rename to examples/VR-VJ/VRVJSoundCartridgeEntityScript.js
diff --git a/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js b/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
new file mode 100644
index 0000000000..7742e0c038
--- /dev/null
+++ b/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
@@ -0,0 +1,30 @@
+
+(function() {
+    var _this;
+    Script.include("../libraries/utils.js");
+    VRVJVisualEntity = function() {
+        _this = this;
+        _this.SOUND_LOOP_NAME
+     
+    };
+
+    VRVJVisualEntity.prototype = {
+        
+        releaseGrab: function() {
+            // search for nearby sound loop entities and if found, add it as a parent
+            _this.searchForNearbySoundLoops();
+        },
+
+        searchForNearbySoundLoops: function() {
+
+        },
+
+        preload: function(entityID) {
+            _this.entityID = entityID;
+
+        },
+    };
+
+    // entity scripts always need to return a newly constructed object of our type
+    return new VRVJVisualEntity();
+});
\ No newline at end of file
diff --git a/examples/VR-VJ/cartridgesSpawner.js b/examples/VR-VJ/cartridgesSpawner.js
index 6169a9a421..2416466f8f 100644
--- a/examples/VR-VJ/cartridgesSpawner.js
+++ b/examples/VR-VJ/cartridgesSpawner.js
@@ -7,9 +7,11 @@
 
 Script.include("../libraries/utils.js");
 
-var SOUND_SCRIPT_URL = Script.resolvePath("VRVJSoundEntityScript.js");
+var SOUND_SCRIPT_URL = Script.resolvePath("VRVJSoundCartridgeEntityScript.js");
+var SOUND_CARTRIDGE_NAME = "VRVJ-Sound-Cartridge";
 var soundEntity = Entities.addEntity({
       type: "Box",
+      name: SOUND_CARTRIDGE_NAME,
       dimensions: {x: 0.1, y: 0.1, z: 0.1},
       color: {red: 200, green: 10, blue: 200},
       position: center,
@@ -18,19 +20,20 @@ var soundEntity = Entities.addEntity({
       script: SOUND_SCRIPT_URL,
       userData: JSON.stringify({
         soundURL: "https://s3-us-west-1.amazonaws.com/hifi-content/eric/Sounds/VRVJ/Synth-MarchToWar.wav",
-      }),
+      })
 });
 
-var VISUAL_SCRIPT_URL = Script.resolvePath("VRVJVisualEntityScript.js")
+var VISUAL_SCRIPT_URL = Script.resolvePath("VRVJVisualCartridgeEntityScript.js");
 var visualEntity = Entities.addEntity({
     type: "Sphere",
+    name: "VRVJ-Visual-Cartridge",
     dimensions: {x: 0.1, y: 0.1, z: 0.1},
     damping: 1,
     color: {red: 0, green: 200, blue: 10},
     dynamic: true,
     position: Vec3.sum(center, {x: 0, y: 0.2, z: 0}),
-    // script: VISUAL_SCRIPT_URLs
-})
+    script: VISUAL_SCRIPT_URL
+});
 
 Script.setTimeout(function() {
     // Wait for sounds to load

From deb2c5af632f2c5bb7eb7699c27951c063a3e892 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 8 Mar 2016 18:30:27 -0800
Subject: [PATCH 03/39] parenting visual to sound entity

---
 .../VR-VJ/VRVJSoundCartridgeEntityScript.js   |  2 +-
 .../VR-VJ/VRVJVisualCartridgeEntityScript.js  | 38 ++++++++++++++++---
 examples/VR-VJ/cartridgesSpawner.js           |  4 +-
 3 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/examples/VR-VJ/VRVJSoundCartridgeEntityScript.js b/examples/VR-VJ/VRVJSoundCartridgeEntityScript.js
index 68990ca51f..c8e106c468 100644
--- a/examples/VR-VJ/VRVJSoundCartridgeEntityScript.js
+++ b/examples/VR-VJ/VRVJSoundCartridgeEntityScript.js
@@ -9,7 +9,7 @@
 
     VRVJSoundEntity.prototype = {
         playSound: function() {
-            _this.soundInjector = Audio.playSound(_this.clip, {position: _this.position, volume: 1.0});
+            // _this.soundInjector = Audio.playSound(_this.clip, {position: _this.position, volume: 1.0});
         },
 
         preload: function(entityID) {
diff --git a/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js b/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
index 7742e0c038..5ed5db9e75 100644
--- a/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
+++ b/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
@@ -1,25 +1,51 @@
-
 (function() {
     var _this;
     Script.include("../libraries/utils.js");
+    var NULL_UUID = "{00000000-0000-0000-0000-000000000000}";
+    var ZERO_VEC = {x: 0, y: 0, z: 0};
     VRVJVisualEntity = function() {
         _this = this;
-        _this.SOUND_LOOP_NAME
-     
+        _this.SOUND_LOOP_NAME = "VRVJ-Sound-Cartridge";
+        _this.SOUND_CARTRIDGE_SEARCH_RANGE = 0.1;
+
     };
 
     VRVJVisualEntity.prototype = {
-        
+
         releaseGrab: function() {
-            // search for nearby sound loop entities and if found, add it as a parent
-            _this.searchForNearbySoundLoops();
+            print("RELEASE GRAB")
+                // search for nearby sound loop entities and if found, add it as a parent
+            Script.setTimeout(function() {
+                _this.searchForNearbySoundLoops();
+            }, 100);
         },
 
         searchForNearbySoundLoops: function() {
+            _this.position = Entities.getEntityProperties(_this.entityID, "position").position;
+            var entities = Entities.findEntities(_this.position, _this.SOUND_CARTRIDGE_SEARCH_RANGE);
+            for (var i = 0; i < entities.length; i++) {
+                var entity = entities[i];
+                var props = Entities.getEntityProperties(entity, ["name"]);
+                if (props.name.indexOf(_this.SOUND_LOOP_NAME) !== -1) {
+                    // Need to set a timeout to wait for grab script to stop messing with entity
+                    Entities.editEntity(_this.entityID, {
+                        parentID: entity,
+                        dynamic: false
+                    });
+                    Script.setTimeout(function() {
+                        Entities.editEntity(_this.entityID, {dynamic: true});
+                    }, 100);
+                    return;
+                }
 
+            }
+            Entities.editEntity(_this.entityID, {
+                parentID: NULL_UUID
+            });
         },
 
         preload: function(entityID) {
+            print("YAAAA")
             _this.entityID = entityID;
 
         },
diff --git a/examples/VR-VJ/cartridgesSpawner.js b/examples/VR-VJ/cartridgesSpawner.js
index 2416466f8f..412f3101e7 100644
--- a/examples/VR-VJ/cartridgesSpawner.js
+++ b/examples/VR-VJ/cartridgesSpawner.js
@@ -16,6 +16,7 @@ var soundEntity = Entities.addEntity({
       color: {red: 200, green: 10, blue: 200},
       position: center,
       damping: 1,
+      angularDamping: 1,
       dynamic: true,
       script: SOUND_SCRIPT_URL,
       userData: JSON.stringify({
@@ -23,12 +24,13 @@ var soundEntity = Entities.addEntity({
       })
 });
 
-var VISUAL_SCRIPT_URL = Script.resolvePath("VRVJVisualCartridgeEntityScript.js");
+var VISUAL_SCRIPT_URL = Script.resolvePath("VRVJVisualCartridgeEntityScript.js?v1" + Math.random());
 var visualEntity = Entities.addEntity({
     type: "Sphere",
     name: "VRVJ-Visual-Cartridge",
     dimensions: {x: 0.1, y: 0.1, z: 0.1},
     damping: 1,
+    angularDamping: 1,
     color: {red: 0, green: 200, blue: 10},
     dynamic: true,
     position: Vec3.sum(center, {x: 0, y: 0.2, z: 0}),

From 41df11f6140b6ee2fac23323c3bd61b713f56b3e Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Wed, 9 Mar 2016 09:55:15 -0800
Subject: [PATCH 04/39] spacing

---
 examples/VR-VJ/cartridgesSpawner.js | 1 -
 1 file changed, 1 deletion(-)

diff --git a/examples/VR-VJ/cartridgesSpawner.js b/examples/VR-VJ/cartridgesSpawner.js
index 412f3101e7..397d09a67a 100644
--- a/examples/VR-VJ/cartridgesSpawner.js
+++ b/examples/VR-VJ/cartridgesSpawner.js
@@ -36,7 +36,6 @@ var visualEntity = Entities.addEntity({
     position: Vec3.sum(center, {x: 0, y: 0.2, z: 0}),
     script: VISUAL_SCRIPT_URL
 });
-
 Script.setTimeout(function() {
     // Wait for sounds to load
     Entities.callEntityMethod(soundEntity, "playSound");

From d65dcf7142e6fbea3080691f248d470789758810 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Wed, 9 Mar 2016 11:11:20 -0800
Subject: [PATCH 05/39] no more velocity

---
 examples/VR-VJ/VRVJVisualCartridgeEntityScript.js | 2 +-
 examples/VR-VJ/cartridgesSpawner.js               | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js b/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
index 5ed5db9e75..5f9b58391b 100644
--- a/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
+++ b/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
@@ -33,7 +33,7 @@
                         dynamic: false
                     });
                     Script.setTimeout(function() {
-                        Entities.editEntity(_this.entityID, {dynamic: true});
+                        Entities.editEntity(_this.entityID, {dynamic: true, velocity: ZERO_VEC});
                     }, 100);
                     return;
                 }
diff --git a/examples/VR-VJ/cartridgesSpawner.js b/examples/VR-VJ/cartridgesSpawner.js
index 397d09a67a..3ce8fd953a 100644
--- a/examples/VR-VJ/cartridgesSpawner.js
+++ b/examples/VR-VJ/cartridgesSpawner.js
@@ -1,8 +1,8 @@
-  var orientation = Camera.getOrientation();
+  var orientation = MyAvatar.orientation;
   orientation = Quat.safeEulerAngles(orientation);
   orientation.x = 0;
   orientation = Quat.fromVec3Degrees(orientation);
-  var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
+  var center = Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiply(2, Quat.getFront(orientation)));
 
 
 Script.include("../libraries/utils.js");
@@ -33,7 +33,7 @@ var visualEntity = Entities.addEntity({
     angularDamping: 1,
     color: {red: 0, green: 200, blue: 10},
     dynamic: true,
-    position: Vec3.sum(center, {x: 0, y: 0.2, z: 0}),
+    position: Vec3.subtract(center, {x: 0, y: 0.2, z: 0}),
     script: VISUAL_SCRIPT_URL
 });
 Script.setTimeout(function() {

From c59a58a8337c171c6daef3a52233e02b294634ac Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Wed, 9 Mar 2016 11:22:47 -0800
Subject: [PATCH 06/39] changing color of visual component to match sound
 cartridge

---
 examples/VR-VJ/VRVJVisualCartridgeEntityScript.js | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js b/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
index 5f9b58391b..a37b830a58 100644
--- a/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
+++ b/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
@@ -25,7 +25,7 @@
             var entities = Entities.findEntities(_this.position, _this.SOUND_CARTRIDGE_SEARCH_RANGE);
             for (var i = 0; i < entities.length; i++) {
                 var entity = entities[i];
-                var props = Entities.getEntityProperties(entity, ["name"]);
+                var props = Entities.getEntityProperties(entity, ["name", "color"]);
                 if (props.name.indexOf(_this.SOUND_LOOP_NAME) !== -1) {
                     // Need to set a timeout to wait for grab script to stop messing with entity
                     Entities.editEntity(_this.entityID, {
@@ -33,20 +33,22 @@
                         dynamic: false
                     });
                     Script.setTimeout(function() {
-                        Entities.editEntity(_this.entityID, {dynamic: true, velocity: ZERO_VEC});
+                        Entities.editEntity(_this.entityID, {dynamic: true, velocity: ZERO_VEC, color: props.color});
                     }, 100);
                     return;
                 }
 
             }
             Entities.editEntity(_this.entityID, {
-                parentID: NULL_UUID
+                parentID: NULL_UUID,
+                color: _this.originalColor
             });
         },
 
         preload: function(entityID) {
             print("YAAAA")
             _this.entityID = entityID;
+            _this.originalColor = Entities.getEntityProperties(_this.entityID, "color").color;
 
         },
     };

From b7534fbd57c6165e410d493512fd79f3fdd6e4b7 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 13:52:26 -0700
Subject: [PATCH 07/39] basics of virtual baton

---
 .../batonSoundTestEntityScript.js             | 70 +++++++++++++++++++
 .../batonSoundTestEntitySpawner.js            | 31 ++++++++
 2 files changed, 101 insertions(+)
 create mode 100644 examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
 create mode 100644 examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
new file mode 100644
index 0000000000..ee100a9563
--- /dev/null
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -0,0 +1,70 @@
+
+(function() {
+    Script.include("../../libraries/virtualBaton.js");
+
+    var baton;
+    var iOwn = false;
+    var updateLoopConnected = false;
+
+    var _this;
+    BatonSoundEntity = function() {
+        _this = this;
+        _this.drumSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Drums/deepdrum1.wav");
+        _this.injectorOptions = {position: MyAvatar.position, loop: false, volume: 0.7};
+    };
+
+    function startUpdate() {
+        iOwn = true;
+        updateLoopConnected = true;
+        print("START UPDATE");
+        Script.update.connect(_this.update);
+    }
+
+    function stopUpdateAndReclaim() {
+        // when the baton is release
+        print("CLAIM BATON")
+        if (updateLoopConnected === true) {
+            updateLoopConnected = false;
+            Script.update.disconnect(_this.update);
+        }
+        iOwn = false;
+
+
+        // hook up callbacks to the baton
+        baton.claim(startUpdate, stopUpdateAndReclaim);
+    }
+
+    BatonSoundEntity.prototype = {
+
+        update: function() {
+            if (iOwn === false) {
+                return;
+            }
+
+            print("EBL I AM UPDATING");
+        },
+
+        preload: function(entityID) {
+            _this.entityID = entityID;
+            print("PRELOAD ENTITY SCRIPT!!!");
+            baton = virtualBaton({
+                // One winner for each entity
+                batonName: "io.highfidelity.soundEntityBatonTest:" + _this.entityID
+            });
+            Audio.playSound(_this.drumSound, _this.injectorOptions);
+            stopUpdateAndReclaim();
+        },
+
+        unload: function() {
+            if (updateLoopConnected === true) {
+                updateLoopConnected = false;
+                Script.update.disconnect(_this.update);
+            }
+        }
+
+   
+    };
+
+    // entity scripts always need to return a newly constructed object of our type
+    return new BatonSoundEntity();
+});
\ No newline at end of file
diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js
new file mode 100644
index 0000000000..541eb05baf
--- /dev/null
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js
@@ -0,0 +1,31 @@
+  var orientation = Camera.getOrientation();
+  orientation = Quat.safeEulerAngles(orientation);
+  orientation.x = 0;
+  orientation = Quat.fromVec3Degrees(orientation);
+  var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
+
+  // Math.random ensures no caching of script
+  var SCRIPT_URL = Script.resolvePath("batonSoundTestEntityScript.js?v1" + Math.random())
+
+  var soundEntity = Entities.addEntity({
+      type: "Box",
+      color: {
+          red: 200,
+          green: 10,
+          blue: 10
+      },
+      position: center,
+      dimensions: {
+          x: 0.1,
+          y: 0.1,
+          z: 0.1
+      },
+      script: SCRIPT_URL
+  });
+
+
+  function cleanup() {
+      Entities.deleteEntity(soundEntity);
+  }
+
+  Script.scriptEnding.connect(cleanup);
\ No newline at end of file

From 68dc5a82514392508be2b062623bfa97783a2abb Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 13:57:54 -0700
Subject: [PATCH 08/39] just playing on start update

---
 .../batonSoundTestEntityScript.js                   | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index ee100a9563..fed025f9b3 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -10,10 +10,19 @@
     BatonSoundEntity = function() {
         _this = this;
         _this.drumSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Drums/deepdrum1.wav");
-        _this.injectorOptions = {position: MyAvatar.position, loop: false, volume: 0.7};
+        _this.injectorOptions = {position: MyAvatar.position, loop: false, volume: 1};
+        
     };
 
     function startUpdate() {
+        // We are claiming the baton! So start our clip
+        if (!_this.soundInjector) {
+            // This client hasn't created their injector yet so create one
+           _this.soundInjector = Audio.playSound(_this.drumSound, _this.injectorOptions);
+        } else {
+            // We already have our injector so just restart it
+            _this.soundInjector.restart();
+        }
         iOwn = true;
         updateLoopConnected = true;
         print("START UPDATE");
@@ -41,7 +50,6 @@
                 return;
             }
 
-            print("EBL I AM UPDATING");
         },
 
         preload: function(entityID) {
@@ -51,7 +59,6 @@
                 // One winner for each entity
                 batonName: "io.highfidelity.soundEntityBatonTest:" + _this.entityID
             });
-            Audio.playSound(_this.drumSound, _this.injectorOptions);
             stopUpdateAndReclaim();
         },
 

From e5b2483604e6a97fe6436fdede480d29c107cec5 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 14:09:02 -0700
Subject: [PATCH 09/39] sound seems to be working"

---
 .../batonSoundTestEntityScript.js             | 23 +++++++++++--------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index fed025f9b3..67197a3c9f 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -4,7 +4,7 @@
 
     var baton;
     var iOwn = false;
-    var updateLoopConnected = false;
+    var soundIntervalConnected = false;
 
     var _this;
     BatonSoundEntity = function() {
@@ -23,18 +23,21 @@
             // We already have our injector so just restart it
             _this.soundInjector.restart();
         }
+        print("EBL SETTING TIMEOUT")
+        _this.playSoundInterval = Script.setInterval(function() {
+             print("EBL RESTART");
+            _this.soundInjector.restart();
+        }, _this.drumSound.duration * 1000); // Duration is in seconds so convert to ms
         iOwn = true;
-        updateLoopConnected = true;
-        print("START UPDATE");
-        Script.update.connect(_this.update);
+        soundIntervalConnected = true;
     }
 
     function stopUpdateAndReclaim() {
         // when the baton is release
         print("CLAIM BATON")
-        if (updateLoopConnected === true) {
-            updateLoopConnected = false;
-            Script.update.disconnect(_this.update);
+        if (soundIntervalConnected === true) {
+            soundIntervalConnected = false;
+            Script.clearInterval(_this.playSoundInterval);
         }
         iOwn = false;
 
@@ -63,9 +66,9 @@
         },
 
         unload: function() {
-            if (updateLoopConnected === true) {
-                updateLoopConnected = false;
-                Script.update.disconnect(_this.update);
+            if (soundIntervalConnected === true) {
+                soundIntervalConnected = false;
+                Script.clearInterval(_this.playSoundInterval);
             }
         }
 

From 493a91fa27e0ecafef90f0ad309ae420c280bfd3 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 14:32:22 -0700
Subject: [PATCH 10/39] with debug box

---
 .../batonSoundTestEntityScript.js             | 31 +++++++++++++------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 67197a3c9f..4421ebbc2b 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -4,14 +4,21 @@
 
     var baton;
     var iOwn = false;
-    var soundIntervalConnected = false;
 
     var _this;
     BatonSoundEntity = function() {
         _this = this;
         _this.drumSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Drums/deepdrum1.wav");
         _this.injectorOptions = {position: MyAvatar.position, loop: false, volume: 1};
-        
+        _this.soundIntervalConnected = false;
+        _this.batonDebugModel = Entities.addEntity({
+            type: "Box",
+            color: {red: 200, green: 10, blue: 200},
+            position: Vec3.sum(MyAvatar.position, {x: 0, y: 1, z: 0}),
+            dimensions: {x: 0.5, y: 1, z: 0},
+            parentID: MyAvatar.sessionUUID,
+            visible: false
+        });
     };
 
     function startUpdate() {
@@ -24,24 +31,25 @@
             _this.soundInjector.restart();
         }
         print("EBL SETTING TIMEOUT")
+        Entities.editEntity(_this.batonDebugModel, {visible: true});
         _this.playSoundInterval = Script.setInterval(function() {
              print("EBL RESTART");
             _this.soundInjector.restart();
         }, _this.drumSound.duration * 1000); // Duration is in seconds so convert to ms
         iOwn = true;
-        soundIntervalConnected = true;
+        _this.soundIntervalConnected = true;
     }
 
     function stopUpdateAndReclaim() {
         // when the baton is release
-        print("CLAIM BATON")
-        if (soundIntervalConnected === true) {
-            soundIntervalConnected = false;
+        print("EBL CLAIM BATON")
+        if (_this.soundIntervalConnected === true) {
             Script.clearInterval(_this.playSoundInterval);
+            _this.soundIntervalConnected = false;
+            print("EBL CLEAR INTERVAL")
         }
         iOwn = false;
-
-
+        Entities.editEntity(_this.batonDebugModel, {visible: false});
         // hook up callbacks to the baton
         baton.claim(startUpdate, stopUpdateAndReclaim);
     }
@@ -66,9 +74,12 @@
         },
 
         unload: function() {
-            if (soundIntervalConnected === true) {
-                soundIntervalConnected = false;
+            Entities.deleteEntity(_this.batonDebugModel);
+            if (_this.soundIntervalConnected === true) {
                 Script.clearInterval(_this.playSoundInterval);
+                _this.soundIntervalConnected = false;
+                _this.soundInjector.stop();
+                delete _this.soundInjector;
             }
         }
 

From b4e400f3aa9ad83fe75fe9ec9e47428c83fea450 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 14:42:28 -0700
Subject: [PATCH 11/39] baton sound logging updates

---
 .../batonSoundEntityTest/batonSoundTestEntityScript.js      | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 4421ebbc2b..673a21229e 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -30,10 +30,10 @@
             // We already have our injector so just restart it
             _this.soundInjector.restart();
         }
-        print("EBL SETTING TIMEOUT")
+        print("EBL TAKING OVER THE SOUND")
         Entities.editEntity(_this.batonDebugModel, {visible: true});
         _this.playSoundInterval = Script.setInterval(function() {
-             print("EBL RESTART");
+             print("EBL PLAY THE SOUND");
             _this.soundInjector.restart();
         }, _this.drumSound.duration * 1000); // Duration is in seconds so convert to ms
         iOwn = true;
@@ -65,7 +65,7 @@
 
         preload: function(entityID) {
             _this.entityID = entityID;
-            print("PRELOAD ENTITY SCRIPT!!!");
+            print("EBL PRELOAD ENTITY SCRIPT!!!");
             baton = virtualBaton({
                 // One winner for each entity
                 batonName: "io.highfidelity.soundEntityBatonTest:" + _this.entityID

From 7659aed97a9b91d84921cc8be379e0d7c5d1f9a4 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 14:42:53 -0700
Subject: [PATCH 12/39] dont delete the entity when spawner script shutdown

---
 .../tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js
index 541eb05baf..633870d985 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js
@@ -25,7 +25,7 @@
 
 
   function cleanup() {
-      Entities.deleteEntity(soundEntity);
+      // Entities.deleteEntity(soundEntity);
   }
 
   Script.scriptEnding.connect(cleanup);
\ No newline at end of file

From b8cb71e16afa73ad768d87a47509f84a1b907abd Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 15:02:39 -0700
Subject: [PATCH 13/39] debug

---
 .../tests/batonSoundEntityTest/batonSoundTestEntityScript.js   | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 673a21229e..659a6e1d67 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -68,7 +68,8 @@
             print("EBL PRELOAD ENTITY SCRIPT!!!");
             baton = virtualBaton({
                 // One winner for each entity
-                batonName: "io.highfidelity.soundEntityBatonTest:" + _this.entityID
+                batonName: "io.highfidelity.soundEntityBatonTest:" + _this.entityID,
+                debugFlow: true
             });
             stopUpdateAndReclaim();
         },

From 81901dc73d1b7b4e23f745cbc20111de01fc3c0b Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 15:04:57 -0700
Subject: [PATCH 14/39] log change

---
 .../tests/batonSoundEntityTest/batonSoundTestEntityScript.js    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 659a6e1d67..38c3e0058f 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -33,7 +33,7 @@
         print("EBL TAKING OVER THE SOUND")
         Entities.editEntity(_this.batonDebugModel, {visible: true});
         _this.playSoundInterval = Script.setInterval(function() {
-             print("EBL PLAY THE SOUND");
+             // print("EBL PLAY THE SOUND");
             _this.soundInjector.restart();
         }, _this.drumSound.duration * 1000); // Duration is in seconds so convert to ms
         iOwn = true;

From b4509e428b6ae30cdcbd33afebb3a60c6d0b643a Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 15:37:20 -0700
Subject: [PATCH 15/39] bugs

---
 examples/libraries/virtualBaton.js            | 46 ++++++++++---------
 .../batonSoundTestEntityScript.js             |  4 +-
 2 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/examples/libraries/virtualBaton.js b/examples/libraries/virtualBaton.js
index 63f96a5c1e..319cae006b 100644
--- a/examples/libraries/virtualBaton.js
+++ b/examples/libraries/virtualBaton.js
@@ -145,30 +145,32 @@ function virtualBatonf(options) {
     // It would be great if we had a way to know how many subscribers our channel has. Failing that...
     var nNack = 0, previousNSubscribers = 0, lastGathering = 0, thisTimeout = electionTimeout;
     function nSubscribers() { // Answer the number of subscribers.
-        // To find nQuorum, we need to know how many scripts are being run using this batonName, which isn't
-        // the same as the number of clients!
-        //
-        // If we overestimate by too much, we may fail to reach consensus, which triggers a new
-        // election proposal, so we take the number of acceptors to be the max(nPromises, nAccepted)
-        // + nNack reported in the previous round.
-        //
-        // If we understimate by too much, there can be different pockets on the Internet that each
-        // believe they have agreement on different holders of the baton, which is precisely what
-        // the virtualBaton is supposed to avoid. Therefore we need to allow 'nack' to gather stragglers.
+        // // To find nQuorum, we need to know how many scripts are being run using this batonName, which isn't
+        // // the same as the number of clients!
+        // //
+        // // If we overestimate by too much, we may fail to reach consensus, which triggers a new
+        // // election proposal, so we take the number of acceptors to be the max(nPromises, nAccepted)
+        // // + nNack reported in the previous round.
+        // //
+        // // If we understimate by too much, there can be different pockets on the Internet that each
+        // // believe they have agreement on different holders of the baton, which is precisely what
+        // // the virtualBaton is supposed to avoid. Therefore we need to allow 'nack' to gather stragglers.
 
-        var now = Date.now(), elapsed = now - lastGathering;
-        if (elapsed >= thisTimeout) {
-            previousNSubscribers = Math.max(nPromises, nAccepted) + nNack;
-            lastGathering = now;
-        } // ...otherwise we use the previous value unchanged.
+        // var now = Date.now(), elapsed = now - lastGathering;
+        // if (elapsed >= thisTimeout) {
+        //     previousNSubscribers = Math.max(nPromises, nAccepted) + nNack;
+        //     lastGathering = now;
+        // } // ...otherwise we use the previous value unchanged.
 
-        // On startup, we do one proposal that we cannot possibly close, so that we'll
-        // lock things up for the full electionTimeout to gather responses.
-        if (!previousNSubscribers) {
-            var LARGE_INTEGER = Number.MAX_SAFE_INTEGER || (-1 >>> 1); // QT doesn't define the ECMA constant. Max int will do for our purposes.
-            previousNSubscribers = LARGE_INTEGER;
-        }
-        return previousNSubscribers;
+        // // On startup, we do one proposal that we cannot possibly close, so that we'll
+        // // lock things up for the full electionTimeout to gather responses.
+        // if (!previousNSubscribers) {
+        //     var LARGE_INTEGER = Number.MAX_SAFE_INTEGER || (-1 >>> 1); // QT doesn't define the ECMA constant. Max int will do for our purposes.
+        //     previousNSubscribers = LARGE_INTEGER;
+        // }
+        // return previousNSubscribers;
+        print("EBL NUMBER SUBSCRIBERS ",  AvatarList.getAvatarIdentifiers().length);
+        return AvatarList.getAvatarIdentifiers().length;
     }
 
     // MAIN ALGORITHM
diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 38c3e0058f..96381d8d13 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -1,6 +1,6 @@
 
 (function() {
-    Script.include("../../libraries/virtualBaton.js");
+    Script.include("../../libraries/virtualBaton.js?v2");
 
     var baton;
     var iOwn = false;
@@ -30,7 +30,7 @@
             // We already have our injector so just restart it
             _this.soundInjector.restart();
         }
-        print("EBL TAKING OVER THE SOUND")
+        print("EBL ")
         Entities.editEntity(_this.batonDebugModel, {visible: true});
         _this.playSoundInterval = Script.setInterval(function() {
              // print("EBL PLAY THE SOUND");

From 98e43a8d0fcd923476c5943e51a6ba04ec21f0a1 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 15:54:47 -0700
Subject: [PATCH 16/39] more logging

---
 .../tests/batonSoundEntityTest/batonSoundTestEntityScript.js   | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 96381d8d13..4fc52d46bd 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -30,7 +30,7 @@
             // We already have our injector so just restart it
             _this.soundInjector.restart();
         }
-        print("EBL ")
+        print("EBL START UPDATE")
         Entities.editEntity(_this.batonDebugModel, {visible: true});
         _this.playSoundInterval = Script.setInterval(function() {
              // print("EBL PLAY THE SOUND");
@@ -75,6 +75,7 @@
         },
 
         unload: function() {
+            print("EBL UNLOAD");
             Entities.deleteEntity(_this.batonDebugModel);
             if (_this.soundIntervalConnected === true) {
                 Script.clearInterval(_this.playSoundInterval);

From 51508b467c5804a84de9c9b8fe7afc31ddbe365d Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 15:59:03 -0700
Subject: [PATCH 17/39] uupdate

---
 examples/libraries/virtualBaton.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/examples/libraries/virtualBaton.js b/examples/libraries/virtualBaton.js
index 319cae006b..53cc76bb0c 100644
--- a/examples/libraries/virtualBaton.js
+++ b/examples/libraries/virtualBaton.js
@@ -373,7 +373,7 @@ function virtualBatonf(options) {
     };
 
     // Gather nAcceptors by making two proposals with some gathering time, even without a claim.
-    propose();
+   // propose();
     return exports;
 }
 if (typeof module !== 'undefined') { // Allow testing in nodejs.

From efd56ac22a0e24e5d30be4a6fe9e93aebc0cc33b Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 16:45:23 -0700
Subject: [PATCH 18/39] spawner

---
 .../tests/basicEntityTest/entityScript.js     | 23 ++++++++++++++
 .../tests/basicEntityTest/entitySpawner.js    | 31 +++++++++++++++++++
 2 files changed, 54 insertions(+)
 create mode 100644 examples/tests/basicEntityTest/entityScript.js
 create mode 100644 examples/tests/basicEntityTest/entitySpawner.js

diff --git a/examples/tests/basicEntityTest/entityScript.js b/examples/tests/basicEntityTest/entityScript.js
new file mode 100644
index 0000000000..0c459e17a7
--- /dev/null
+++ b/examples/tests/basicEntityTest/entityScript.js
@@ -0,0 +1,23 @@
+
+(function() {
+    var _this;
+    MyEntity = function() {
+        _this = this;
+     
+    };
+
+    MyEntity.prototype = {
+
+
+        preload: function(entityID) {
+            this.entityID = entityID;
+            print("EBL PRELOAD ENTITY SCRIPT!!!")
+
+        },
+
+   
+    };
+
+    // entity scripts always need to return a newly constructed object of our type
+    return new MyEntity();
+});
\ No newline at end of file
diff --git a/examples/tests/basicEntityTest/entitySpawner.js b/examples/tests/basicEntityTest/entitySpawner.js
new file mode 100644
index 0000000000..1875d770e3
--- /dev/null
+++ b/examples/tests/basicEntityTest/entitySpawner.js
@@ -0,0 +1,31 @@
+  var orientation = Camera.getOrientation();
+  orientation = Quat.safeEulerAngles(orientation);
+  orientation.x = 0;
+  orientation = Quat.fromVec3Degrees(orientation);
+  var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
+
+  // Math.random ensures no caching of script
+  var SCRIPT_URL = Script.resolvePath("myEntityScript.js?v1" + Math.random())
+
+  var myEntity = Entities.addEntity({
+      type: "Sphere",
+      color: {
+          red: 200,
+          green: 10,
+          blue: 200
+      },
+      position: center,
+      dimensions: {
+          x: 1,
+          y: 1,
+          z: 1
+      },
+      script: SCRIPT_URL
+  })
+
+
+  function cleanup() {
+      // Entities.deleteEntity(myEntity);
+  }
+
+  Script.scriptEnding.connect(cleanup);
\ No newline at end of file

From 9c18d6c016b8e0e7876fcaa25de48a27831a8f66 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 16:47:51 -0700
Subject: [PATCH 19/39] change name

---
 .../tests/basicEntityTest/{entityScript.js => myEntityScript.js}  | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename examples/tests/basicEntityTest/{entityScript.js => myEntityScript.js} (100%)

diff --git a/examples/tests/basicEntityTest/entityScript.js b/examples/tests/basicEntityTest/myEntityScript.js
similarity index 100%
rename from examples/tests/basicEntityTest/entityScript.js
rename to examples/tests/basicEntityTest/myEntityScript.js

From adeaa9475e476fac72ff5af8781d2c0550c5249d Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 16:49:55 -0700
Subject: [PATCH 20/39] num

---
 examples/tests/basicEntityTest/myEntityScript.js | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/examples/tests/basicEntityTest/myEntityScript.js b/examples/tests/basicEntityTest/myEntityScript.js
index 0c459e17a7..b4a8885c70 100644
--- a/examples/tests/basicEntityTest/myEntityScript.js
+++ b/examples/tests/basicEntityTest/myEntityScript.js
@@ -11,7 +11,8 @@
 
         preload: function(entityID) {
             this.entityID = entityID;
-            print("EBL PRELOAD ENTITY SCRIPT!!!")
+            var randNum = Math.random().toFixed(3);
+            print("EBL PRELOAD ENTITY SCRIPT!!!", randNum)
 
         },
 

From 2dce75bfb0c8ecde11f1ccdde1753219d8de740f Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Mon, 21 Mar 2016 17:22:18 -0700
Subject: [PATCH 21/39] baton sound

---
 examples/libraries/virtualBaton.js                              | 1 +
 .../tests/batonSoundEntityTest/batonSoundTestEntityScript.js    | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/examples/libraries/virtualBaton.js b/examples/libraries/virtualBaton.js
index 53cc76bb0c..75ff216c6a 100644
--- a/examples/libraries/virtualBaton.js
+++ b/examples/libraries/virtualBaton.js
@@ -373,6 +373,7 @@ function virtualBatonf(options) {
     };
 
     // Gather nAcceptors by making two proposals with some gathering time, even without a claim.
+    print("EBL PROPOSE HAS BEEN COMMENTED OUT!");
    // propose();
     return exports;
 }
diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 4fc52d46bd..844a9b42cb 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -1,6 +1,6 @@
 
 (function() {
-    Script.include("../../libraries/virtualBaton.js?v2");
+    Script.include("../../libraries/virtualBaton.js?v2" + Math.random());
 
     var baton;
     var iOwn = false;

From 2bb11c5dc6d1363d960abf464b16832aac1c3c3c Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 09:59:10 -0700
Subject: [PATCH 22/39] unload baton

---
 .../tests/batonSoundEntityTest/batonSoundTestEntityScript.js    | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 844a9b42cb..fda36e2a01 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -76,6 +76,8 @@
 
         unload: function() {
             print("EBL UNLOAD");
+            virtualBaton.release();
+            virtualBaton.unload();
             Entities.deleteEntity(_this.batonDebugModel);
             if (_this.soundIntervalConnected === true) {
                 Script.clearInterval(_this.playSoundInterval);

From bc8a8ac70371d189b2b17dc912781755e95278e8 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 10:00:44 -0700
Subject: [PATCH 23/39] fix syntax error

---
 .../tests/batonSoundEntityTest/batonSoundTestEntityScript.js  | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index fda36e2a01..ce57050a12 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -76,8 +76,8 @@
 
         unload: function() {
             print("EBL UNLOAD");
-            virtualBaton.release();
-            virtualBaton.unload();
+            baton.release();
+            baton.unload();
             Entities.deleteEntity(_this.batonDebugModel);
             if (_this.soundIntervalConnected === true) {
                 Script.clearInterval(_this.playSoundInterval);

From 130eb6b619a0f68622ae70c0032a2288faabaaac Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 10:07:59 -0700
Subject: [PATCH 24/39] working for one;

---
 examples/libraries/virtualBaton.js            | 49 +++++++++----------
 .../batonSoundTestEntityScript.js             |  6 +--
 2 files changed, 26 insertions(+), 29 deletions(-)

diff --git a/examples/libraries/virtualBaton.js b/examples/libraries/virtualBaton.js
index 75ff216c6a..5adc7672bc 100644
--- a/examples/libraries/virtualBaton.js
+++ b/examples/libraries/virtualBaton.js
@@ -145,32 +145,30 @@ function virtualBatonf(options) {
     // It would be great if we had a way to know how many subscribers our channel has. Failing that...
     var nNack = 0, previousNSubscribers = 0, lastGathering = 0, thisTimeout = electionTimeout;
     function nSubscribers() { // Answer the number of subscribers.
-        // // To find nQuorum, we need to know how many scripts are being run using this batonName, which isn't
-        // // the same as the number of clients!
-        // //
-        // // If we overestimate by too much, we may fail to reach consensus, which triggers a new
-        // // election proposal, so we take the number of acceptors to be the max(nPromises, nAccepted)
-        // // + nNack reported in the previous round.
-        // //
-        // // If we understimate by too much, there can be different pockets on the Internet that each
-        // // believe they have agreement on different holders of the baton, which is precisely what
-        // // the virtualBaton is supposed to avoid. Therefore we need to allow 'nack' to gather stragglers.
+        // To find nQuorum, we need to know how many scripts are being run using this batonName, which isn't
+        // the same as the number of clients!
+        
+        // If we overestimate by too much, we may fail to reach consensus, which triggers a new
+        // election proposal, so we take the number of acceptors to be the max(nPromises, nAccepted)
+        // + nNack reported in the previous round.
+        
+        // If we understimate by too much, there can be different pockets on the Internet that each
+        // believe they have agreement on different holders of the baton, which is precisely what
+        // the virtualBaton is supposed to avoid. Therefore we need to allow 'nack' to gather stragglers.
 
-        // var now = Date.now(), elapsed = now - lastGathering;
-        // if (elapsed >= thisTimeout) {
-        //     previousNSubscribers = Math.max(nPromises, nAccepted) + nNack;
-        //     lastGathering = now;
-        // } // ...otherwise we use the previous value unchanged.
+        var now = Date.now(), elapsed = now - lastGathering;
+        if (elapsed >= thisTimeout) {
+            previousNSubscribers = Math.max(nPromises, nAccepted) + nNack;
+            lastGathering = now;
+        } // ...otherwise we use the previous value unchanged.
 
-        // // On startup, we do one proposal that we cannot possibly close, so that we'll
-        // // lock things up for the full electionTimeout to gather responses.
-        // if (!previousNSubscribers) {
-        //     var LARGE_INTEGER = Number.MAX_SAFE_INTEGER || (-1 >>> 1); // QT doesn't define the ECMA constant. Max int will do for our purposes.
-        //     previousNSubscribers = LARGE_INTEGER;
-        // }
-        // return previousNSubscribers;
-        print("EBL NUMBER SUBSCRIBERS ",  AvatarList.getAvatarIdentifiers().length);
-        return AvatarList.getAvatarIdentifiers().length;
+        // On startup, we do one proposal that we cannot possibly close, so that we'll
+        // lock things up for the full electionTimeout to gather responses.
+        if (!previousNSubscribers) {
+            var LARGE_INTEGER = Number.MAX_SAFE_INTEGER || (-1 >>> 1); // QT doesn't define the ECMA constant. Max int will do for our purposes.
+            previousNSubscribers = LARGE_INTEGER;
+        }
+        return previousNSubscribers;
     }
 
     // MAIN ALGORITHM
@@ -373,8 +371,7 @@ function virtualBatonf(options) {
     };
 
     // Gather nAcceptors by making two proposals with some gathering time, even without a claim.
-    print("EBL PROPOSE HAS BEEN COMMENTED OUT!");
-   // propose();
+   propose();
     return exports;
 }
 if (typeof module !== 'undefined') { // Allow testing in nodejs.
diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index ce57050a12..e42c855f10 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -30,7 +30,7 @@
             // We already have our injector so just restart it
             _this.soundInjector.restart();
         }
-        print("EBL START UPDATE")
+        print("EBL START UPDATE");
         Entities.editEntity(_this.batonDebugModel, {visible: true});
         _this.playSoundInterval = Script.setInterval(function() {
              // print("EBL PLAY THE SOUND");
@@ -41,8 +41,8 @@
     }
 
     function stopUpdateAndReclaim() {
+        print("EBL STOP UPDATE AND RECLAIM")
         // when the baton is release
-        print("EBL CLAIM BATON")
         if (_this.soundIntervalConnected === true) {
             Script.clearInterval(_this.playSoundInterval);
             _this.soundIntervalConnected = false;
@@ -69,7 +69,7 @@
             baton = virtualBaton({
                 // One winner for each entity
                 batonName: "io.highfidelity.soundEntityBatonTest:" + _this.entityID,
-                debugFlow: true
+                // debugFlow: true
             });
             stopUpdateAndReclaim();
         },

From f043b27a5646a5f0713fa9ac795b81081f4219a7 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 10:12:44 -0700
Subject: [PATCH 25/39] hardcoded num avatars

---
 examples/libraries/virtualBaton.js | 46 ++++++++++++++++--------------
 1 file changed, 24 insertions(+), 22 deletions(-)

diff --git a/examples/libraries/virtualBaton.js b/examples/libraries/virtualBaton.js
index 5adc7672bc..5a6e577bc1 100644
--- a/examples/libraries/virtualBaton.js
+++ b/examples/libraries/virtualBaton.js
@@ -145,30 +145,32 @@ function virtualBatonf(options) {
     // It would be great if we had a way to know how many subscribers our channel has. Failing that...
     var nNack = 0, previousNSubscribers = 0, lastGathering = 0, thisTimeout = electionTimeout;
     function nSubscribers() { // Answer the number of subscribers.
-        // To find nQuorum, we need to know how many scripts are being run using this batonName, which isn't
-        // the same as the number of clients!
-        
-        // If we overestimate by too much, we may fail to reach consensus, which triggers a new
-        // election proposal, so we take the number of acceptors to be the max(nPromises, nAccepted)
-        // + nNack reported in the previous round.
-        
-        // If we understimate by too much, there can be different pockets on the Internet that each
-        // believe they have agreement on different holders of the baton, which is precisely what
-        // the virtualBaton is supposed to avoid. Therefore we need to allow 'nack' to gather stragglers.
+        // // To find nQuorum, we need to know how many scripts are being run using this batonName, which isn't
+        // // the same as the number of clients!
+        // //
+        // // If we overestimate by too much, we may fail to reach consensus, which triggers a new
+        // // election proposal, so we take the number of acceptors to be the max(nPromises, nAccepted)
+        // // + nNack reported in the previous round.
+        // //
+        // // If we understimate by too much, there can be different pockets on the Internet that each
+        // // believe they have agreement on different holders of the baton, which is precisely what
+        // // the virtualBaton is supposed to avoid. Therefore we need to allow 'nack' to gather stragglers.
 
-        var now = Date.now(), elapsed = now - lastGathering;
-        if (elapsed >= thisTimeout) {
-            previousNSubscribers = Math.max(nPromises, nAccepted) + nNack;
-            lastGathering = now;
-        } // ...otherwise we use the previous value unchanged.
+        // var now = Date.now(), elapsed = now - lastGathering;
+        // if (elapsed >= thisTimeout) {
+        //     previousNSubscribers = Math.max(nPromises, nAccepted) + nNack;
+        //     lastGathering = now;
+        // } // ...otherwise we use the previous value unchanged.
 
-        // On startup, we do one proposal that we cannot possibly close, so that we'll
-        // lock things up for the full electionTimeout to gather responses.
-        if (!previousNSubscribers) {
-            var LARGE_INTEGER = Number.MAX_SAFE_INTEGER || (-1 >>> 1); // QT doesn't define the ECMA constant. Max int will do for our purposes.
-            previousNSubscribers = LARGE_INTEGER;
-        }
-        return previousNSubscribers;
+        // // On startup, we do one proposal that we cannot possibly close, so that we'll
+        // // lock things up for the full electionTimeout to gather responses.
+        // if (!previousNSubscribers) {
+        //     var LARGE_INTEGER = Number.MAX_SAFE_INTEGER || (-1 >>> 1); // QT doesn't define the ECMA constant. Max int will do for our purposes.
+        //     previousNSubscribers = LARGE_INTEGER;
+        // }
+        // return previousNSubscribers;
+        print("EBL NUMBER SUBSCRIBERS ",  AvatarList.getAvatarIdentifiers().length);
+        return AvatarList.getAvatarIdentifiers().length;
     }
 
     // MAIN ALGORITHM

From 1c876fe110abc24ab3a7957c9e8c2a55be943342 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 10:16:49 -0700
Subject: [PATCH 26/39] update

---
 examples/libraries/virtualBaton.js            | 48 +++++++++----------
 .../batonSoundTestEntityScript.js             |  2 +-
 2 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/examples/libraries/virtualBaton.js b/examples/libraries/virtualBaton.js
index 5a6e577bc1..d6c73db5dd 100644
--- a/examples/libraries/virtualBaton.js
+++ b/examples/libraries/virtualBaton.js
@@ -145,32 +145,32 @@ function virtualBatonf(options) {
     // It would be great if we had a way to know how many subscribers our channel has. Failing that...
     var nNack = 0, previousNSubscribers = 0, lastGathering = 0, thisTimeout = electionTimeout;
     function nSubscribers() { // Answer the number of subscribers.
-        // // To find nQuorum, we need to know how many scripts are being run using this batonName, which isn't
-        // // the same as the number of clients!
-        // //
-        // // If we overestimate by too much, we may fail to reach consensus, which triggers a new
-        // // election proposal, so we take the number of acceptors to be the max(nPromises, nAccepted)
-        // // + nNack reported in the previous round.
-        // //
-        // // If we understimate by too much, there can be different pockets on the Internet that each
-        // // believe they have agreement on different holders of the baton, which is precisely what
-        // // the virtualBaton is supposed to avoid. Therefore we need to allow 'nack' to gather stragglers.
+        // To find nQuorum, we need to know how many scripts are being run using this batonName, which isn't
+        // the same as the number of clients!
+        //
+        // If we overestimate by too much, we may fail to reach consensus, which triggers a new
+        // election proposal, so we take the number of acceptors to be the max(nPromises, nAccepted)
+        // + nNack reported in the previous round.
+        //
+        // If we understimate by too much, there can be different pockets on the Internet that each
+        // believe they have agreement on different holders of the baton, which is precisely what
+        // the virtualBaton is supposed to avoid. Therefore we need to allow 'nack' to gather stragglers.
 
-        // var now = Date.now(), elapsed = now - lastGathering;
-        // if (elapsed >= thisTimeout) {
-        //     previousNSubscribers = Math.max(nPromises, nAccepted) + nNack;
-        //     lastGathering = now;
-        // } // ...otherwise we use the previous value unchanged.
+        var now = Date.now(), elapsed = now - lastGathering;
+        if (elapsed >= thisTimeout) {
+            previousNSubscribers = Math.max(nPromises, nAccepted) + nNack;
+            lastGathering = now;
+        } // ...otherwise we use the previous value unchanged.
 
-        // // On startup, we do one proposal that we cannot possibly close, so that we'll
-        // // lock things up for the full electionTimeout to gather responses.
-        // if (!previousNSubscribers) {
-        //     var LARGE_INTEGER = Number.MAX_SAFE_INTEGER || (-1 >>> 1); // QT doesn't define the ECMA constant. Max int will do for our purposes.
-        //     previousNSubscribers = LARGE_INTEGER;
-        // }
-        // return previousNSubscribers;
-        print("EBL NUMBER SUBSCRIBERS ",  AvatarList.getAvatarIdentifiers().length);
-        return AvatarList.getAvatarIdentifiers().length;
+        // On startup, we do one proposal that we cannot possibly close, so that we'll
+        // lock things up for the full electionTimeout to gather responses.
+        if (!previousNSubscribers) {
+            var LARGE_INTEGER = Number.MAX_SAFE_INTEGER || (-1 >>> 1); // QT doesn't define the ECMA constant. Max int will do for our purposes.
+            previousNSubscribers = LARGE_INTEGER;
+        }
+        return previousNSubscribers;
+        // print("EBL NUMBER SUBSCRIBERS ",  AvatarList.getAvatarIdentifiers().length);
+        // return AvatarList.getAvatarIdentifiers().length;
     }
 
     // MAIN ALGORITHM
diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index e42c855f10..4cbe98a2ce 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -76,7 +76,7 @@
 
         unload: function() {
             print("EBL UNLOAD");
-            baton.release();
+            baton.release(function(){});
             baton.unload();
             Entities.deleteEntity(_this.batonDebugModel);
             if (_this.soundIntervalConnected === true) {

From 94283904b8fbb1d45d26950dd93e75ea5c4a39d1 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 10:26:13 -0700
Subject: [PATCH 27/39] update

---
 .../tests/batonSoundEntityTest/batonSoundTestEntityScript.js    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 4cbe98a2ce..b2f2c2585b 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -76,7 +76,7 @@
 
         unload: function() {
             print("EBL UNLOAD");
-            baton.release(function(){});
+            // baton.release();
             baton.unload();
             Entities.deleteEntity(_this.batonDebugModel);
             if (_this.soundIntervalConnected === true) {

From c9c5d3d8bcbbe0985de422fcaa8a022acae11fc5 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 10:32:04 -0700
Subject: [PATCH 28/39] removed VRVJ files

---
 .../VR-VJ/VRVJSoundCartridgeEntityScript.js   | 35 -----------
 .../VR-VJ/VRVJVisualCartridgeEntityScript.js  | 58 -------------------
 examples/VR-VJ/cartridgesSpawner.js           | 49 ----------------
 .../batonSoundTestEntityScript.js             |  1 -
 4 files changed, 143 deletions(-)
 delete mode 100644 examples/VR-VJ/VRVJSoundCartridgeEntityScript.js
 delete mode 100644 examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
 delete mode 100644 examples/VR-VJ/cartridgesSpawner.js

diff --git a/examples/VR-VJ/VRVJSoundCartridgeEntityScript.js b/examples/VR-VJ/VRVJSoundCartridgeEntityScript.js
deleted file mode 100644
index c8e106c468..0000000000
--- a/examples/VR-VJ/VRVJSoundCartridgeEntityScript.js
+++ /dev/null
@@ -1,35 +0,0 @@
-
-(function() {
-    var _this;
-    Script.include("../libraries/utils.js");
-    VRVJSoundEntity = function() {
-        _this = this;
-     
-    };
-
-    VRVJSoundEntity.prototype = {
-        playSound: function() {
-            // _this.soundInjector = Audio.playSound(_this.clip, {position: _this.position, volume: 1.0});
-        },
-
-        preload: function(entityID) {
-            _this.entityID = entityID;
-            _this.position = Entities.getEntityProperties(_this.entityID, "position").position;
-            _this.userData  = getEntityUserData(_this.entityID);
-            _this.clip = SoundCache.getSound(_this.userData.soundURL);
-
-        },
-
-        unload: function() {
-            if (_this.soundInjector) {
-                _this.soundInjector.stop();
-                delete _this.soundInjector;
-            }
-        }
-
-   
-    };
-
-    // entity scripts always need to return a newly constructed object of our type
-    return new VRVJSoundEntity();
-});
\ No newline at end of file
diff --git a/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js b/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
deleted file mode 100644
index a37b830a58..0000000000
--- a/examples/VR-VJ/VRVJVisualCartridgeEntityScript.js
+++ /dev/null
@@ -1,58 +0,0 @@
-(function() {
-    var _this;
-    Script.include("../libraries/utils.js");
-    var NULL_UUID = "{00000000-0000-0000-0000-000000000000}";
-    var ZERO_VEC = {x: 0, y: 0, z: 0};
-    VRVJVisualEntity = function() {
-        _this = this;
-        _this.SOUND_LOOP_NAME = "VRVJ-Sound-Cartridge";
-        _this.SOUND_CARTRIDGE_SEARCH_RANGE = 0.1;
-
-    };
-
-    VRVJVisualEntity.prototype = {
-
-        releaseGrab: function() {
-            print("RELEASE GRAB")
-                // search for nearby sound loop entities and if found, add it as a parent
-            Script.setTimeout(function() {
-                _this.searchForNearbySoundLoops();
-            }, 100);
-        },
-
-        searchForNearbySoundLoops: function() {
-            _this.position = Entities.getEntityProperties(_this.entityID, "position").position;
-            var entities = Entities.findEntities(_this.position, _this.SOUND_CARTRIDGE_SEARCH_RANGE);
-            for (var i = 0; i < entities.length; i++) {
-                var entity = entities[i];
-                var props = Entities.getEntityProperties(entity, ["name", "color"]);
-                if (props.name.indexOf(_this.SOUND_LOOP_NAME) !== -1) {
-                    // Need to set a timeout to wait for grab script to stop messing with entity
-                    Entities.editEntity(_this.entityID, {
-                        parentID: entity,
-                        dynamic: false
-                    });
-                    Script.setTimeout(function() {
-                        Entities.editEntity(_this.entityID, {dynamic: true, velocity: ZERO_VEC, color: props.color});
-                    }, 100);
-                    return;
-                }
-
-            }
-            Entities.editEntity(_this.entityID, {
-                parentID: NULL_UUID,
-                color: _this.originalColor
-            });
-        },
-
-        preload: function(entityID) {
-            print("YAAAA")
-            _this.entityID = entityID;
-            _this.originalColor = Entities.getEntityProperties(_this.entityID, "color").color;
-
-        },
-    };
-
-    // entity scripts always need to return a newly constructed object of our type
-    return new VRVJVisualEntity();
-});
\ No newline at end of file
diff --git a/examples/VR-VJ/cartridgesSpawner.js b/examples/VR-VJ/cartridgesSpawner.js
deleted file mode 100644
index 3ce8fd953a..0000000000
--- a/examples/VR-VJ/cartridgesSpawner.js
+++ /dev/null
@@ -1,49 +0,0 @@
-  var orientation = MyAvatar.orientation;
-  orientation = Quat.safeEulerAngles(orientation);
-  orientation.x = 0;
-  orientation = Quat.fromVec3Degrees(orientation);
-  var center = Vec3.sum(MyAvatar.getHeadPosition(), Vec3.multiply(2, Quat.getFront(orientation)));
-
-
-Script.include("../libraries/utils.js");
-
-var SOUND_SCRIPT_URL = Script.resolvePath("VRVJSoundCartridgeEntityScript.js");
-var SOUND_CARTRIDGE_NAME = "VRVJ-Sound-Cartridge";
-var soundEntity = Entities.addEntity({
-      type: "Box",
-      name: SOUND_CARTRIDGE_NAME,
-      dimensions: {x: 0.1, y: 0.1, z: 0.1},
-      color: {red: 200, green: 10, blue: 200},
-      position: center,
-      damping: 1,
-      angularDamping: 1,
-      dynamic: true,
-      script: SOUND_SCRIPT_URL,
-      userData: JSON.stringify({
-        soundURL: "https://s3-us-west-1.amazonaws.com/hifi-content/eric/Sounds/VRVJ/Synth-MarchToWar.wav",
-      })
-});
-
-var VISUAL_SCRIPT_URL = Script.resolvePath("VRVJVisualCartridgeEntityScript.js?v1" + Math.random());
-var visualEntity = Entities.addEntity({
-    type: "Sphere",
-    name: "VRVJ-Visual-Cartridge",
-    dimensions: {x: 0.1, y: 0.1, z: 0.1},
-    damping: 1,
-    angularDamping: 1,
-    color: {red: 0, green: 200, blue: 10},
-    dynamic: true,
-    position: Vec3.subtract(center, {x: 0, y: 0.2, z: 0}),
-    script: VISUAL_SCRIPT_URL
-});
-Script.setTimeout(function() {
-    // Wait for sounds to load
-    Entities.callEntityMethod(soundEntity, "playSound");
-}, 1000)
-
-function cleanup() {
-    Entities.deleteEntity(soundEntity);
-    Entities.deleteEntity(visualEntity);
-}
-
-Script.scriptEnding.connect(cleanup);
\ No newline at end of file
diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index b2f2c2585b..938fb8514d 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -33,7 +33,6 @@
         print("EBL START UPDATE");
         Entities.editEntity(_this.batonDebugModel, {visible: true});
         _this.playSoundInterval = Script.setInterval(function() {
-             // print("EBL PLAY THE SOUND");
             _this.soundInjector.restart();
         }, _this.drumSound.duration * 1000); // Duration is in seconds so convert to ms
         iOwn = true;

From 918c61c166f622277e8b13b87b963928a2982767 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 10:34:10 -0700
Subject: [PATCH 29/39] removed extra code from virtualBaton

---
 examples/libraries/virtualBaton.js | 2 --
 1 file changed, 2 deletions(-)

diff --git a/examples/libraries/virtualBaton.js b/examples/libraries/virtualBaton.js
index d6c73db5dd..4e568b6763 100644
--- a/examples/libraries/virtualBaton.js
+++ b/examples/libraries/virtualBaton.js
@@ -169,8 +169,6 @@ function virtualBatonf(options) {
             previousNSubscribers = LARGE_INTEGER;
         }
         return previousNSubscribers;
-        // print("EBL NUMBER SUBSCRIBERS ",  AvatarList.getAvatarIdentifiers().length);
-        // return AvatarList.getAvatarIdentifiers().length;
     }
 
     // MAIN ALGORITHM

From a0e8e00357f9aaf4e8c1aaba030349b1fb95b16f Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 15:02:00 -0700
Subject: [PATCH 30/39] removed cache defeating code and fixed whitespace

---
 examples/libraries/virtualBaton.js                              | 2 +-
 .../tests/batonSoundEntityTest/batonSoundTestEntityScript.js    | 2 +-
 .../tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js   | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/examples/libraries/virtualBaton.js b/examples/libraries/virtualBaton.js
index 4e568b6763..63f96a5c1e 100644
--- a/examples/libraries/virtualBaton.js
+++ b/examples/libraries/virtualBaton.js
@@ -371,7 +371,7 @@ function virtualBatonf(options) {
     };
 
     // Gather nAcceptors by making two proposals with some gathering time, even without a claim.
-   propose();
+    propose();
     return exports;
 }
 if (typeof module !== 'undefined') { // Allow testing in nodejs.
diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 938fb8514d..27ce394dc2 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -1,6 +1,6 @@
 
 (function() {
-    Script.include("../../libraries/virtualBaton.js?v2" + Math.random());
+    Script.include("../../libraries/virtualBaton.js");
 
     var baton;
     var iOwn = false;
diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js
index 633870d985..fdcef8d32c 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntitySpawner.js
@@ -5,7 +5,7 @@
   var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
 
   // Math.random ensures no caching of script
-  var SCRIPT_URL = Script.resolvePath("batonSoundTestEntityScript.js?v1" + Math.random())
+  var SCRIPT_URL = Script.resolvePath("batonSoundTestEntityScript.js")
 
   var soundEntity = Entities.addEntity({
       type: "Box",

From dfffe5f431bc682dc1d2b2d03bae8cb6e348a248 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 15:02:44 -0700
Subject: [PATCH 31/39] more removal

---
 examples/tests/basicEntityTest/entitySpawner.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/examples/tests/basicEntityTest/entitySpawner.js b/examples/tests/basicEntityTest/entitySpawner.js
index 1875d770e3..a2f38f59eb 100644
--- a/examples/tests/basicEntityTest/entitySpawner.js
+++ b/examples/tests/basicEntityTest/entitySpawner.js
@@ -5,7 +5,7 @@
   var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
 
   // Math.random ensures no caching of script
-  var SCRIPT_URL = Script.resolvePath("myEntityScript.js?v1" + Math.random())
+  var SCRIPT_URL = Script.resolvePath("myEntityScript.js")
 
   var myEntity = Entities.addEntity({
       type: "Sphere",

From ad5a111b85c3517a60a2a30bea229fd70bcc0781 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 17:04:47 -0700
Subject: [PATCH 32/39] simple baton

---
 .../batonSoundEntityTest/batonSoundTestEntityScript.js   | 9 ---------
 1 file changed, 9 deletions(-)

diff --git a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
index 27ce394dc2..910378e04e 100644
--- a/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
+++ b/examples/tests/batonSoundEntityTest/batonSoundTestEntityScript.js
@@ -3,7 +3,6 @@
     Script.include("../../libraries/virtualBaton.js");
 
     var baton;
-    var iOwn = false;
 
     var _this;
     BatonSoundEntity = function() {
@@ -35,7 +34,6 @@
         _this.playSoundInterval = Script.setInterval(function() {
             _this.soundInjector.restart();
         }, _this.drumSound.duration * 1000); // Duration is in seconds so convert to ms
-        iOwn = true;
         _this.soundIntervalConnected = true;
     }
 
@@ -47,7 +45,6 @@
             _this.soundIntervalConnected = false;
             print("EBL CLEAR INTERVAL")
         }
-        iOwn = false;
         Entities.editEntity(_this.batonDebugModel, {visible: false});
         // hook up callbacks to the baton
         baton.claim(startUpdate, stopUpdateAndReclaim);
@@ -55,12 +52,6 @@
 
     BatonSoundEntity.prototype = {
 
-        update: function() {
-            if (iOwn === false) {
-                return;
-            }
-
-        },
 
         preload: function(entityID) {
             _this.entityID = entityID;

From 608821b50b0896c682202f1652d83bf92a5bd45f Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 17:06:49 -0700
Subject: [PATCH 33/39] simple baton

---
 .../virtualBaton/batonSimpleEntityScript.js   | 64 +++++++++++++++++++
 .../virtualBaton/batonSimpleEntitySpawner.js  | 31 +++++++++
 2 files changed, 95 insertions(+)
 create mode 100644 examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
 create mode 100644 examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js

diff --git a/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js b/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
new file mode 100644
index 0000000000..77b75b9914
--- /dev/null
+++ b/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
@@ -0,0 +1,64 @@
+(function() {
+    Script.include("../../libraries/virtualBaton.js");
+
+    var _this = this;
+
+
+    this.startUpdate = function() {
+        Entities.editEntity(_this.batonDebugModel, {visible: true});
+
+    }
+
+    this.maybeClaim = function() {
+        if (_this.isBatonOwner === true) {
+            _this.soundIntervalConnected = false;
+        }
+        Entities.editEntity(_this.batonDebugModel, {visible: false});
+        baton.claim(_this.startUpdate, _this.maybeClaim);
+    }
+
+    this.preload = function(entityID) {
+        _this.entityID = entityID
+        _this.batonDebugModel = Entities.addEntity({
+            type: "Box",
+            color: {
+                red: 200,
+                green: 10,
+                blue: 200
+            },
+            position: Vec3.sum(MyAvatar.position, {
+                x: 0,
+                y: 1,
+                z: 0
+            }),
+            dimensions: {
+                x: 0.5,
+                y: 1,
+                z: 0
+            },
+            parentID: MyAvatar.sessionUUID,
+            visible: false
+        });
+        _this.colorsToCycle = [{
+            red: 200,
+            green: 0,
+            blue: 0
+        }, {
+            red: 0,
+            green: 200,
+            blue: 0
+        }, {
+            red: 0,
+            green: 0,
+            blue: 200
+        }];
+        baton = virtualBaton({
+            batonName: "batonSimpleEntityScript:" + _this.entityID
+        });
+        _this.isBatonOwner = false;
+        _this.maybeClaim();
+
+        print("EBL Preload!!");
+    }
+
+});
\ No newline at end of file
diff --git a/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js b/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
new file mode 100644
index 0000000000..e48ef77e8f
--- /dev/null
+++ b/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
@@ -0,0 +1,31 @@
+  var orientation = Camera.getOrientation();
+  orientation = Quat.safeEulerAngles(orientation);
+  orientation.x = 0;
+  orientation = Quat.fromVec3Degrees(orientation);
+  var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
+
+  // Math.random ensures no caching of script
+  var SCRIPT_URL = Script.resolvePath("batonSimpleEntityScript.js?v1" + Math.random());
+
+  var myEntity = Entities.addEntity({
+      type: "Box",
+      color: {
+          red: 200,
+          green: 10,
+          blue: 10
+      },
+      position: center,
+      dimensions: {
+          x: 0.1,
+          y: 0.1,
+          z: 0.1
+      },
+      script: SCRIPT_URL
+  });
+
+
+  function cleanup() {
+      Entities.deleteEntity(myEntity);
+  }
+
+  Script.scriptEnding.connect(cleanup);
\ No newline at end of file

From 107479039ae18420b60d67c24b8128bd9d5fc091 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 17:10:04 -0700
Subject: [PATCH 34/39] update

---
 examples/entityScripts/virtualBaton/batonSimpleEntityScript.js  | 2 +-
 examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js b/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
index 77b75b9914..74f153cd7e 100644
--- a/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
+++ b/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
@@ -11,7 +11,7 @@
 
     this.maybeClaim = function() {
         if (_this.isBatonOwner === true) {
-            _this.soundIntervalConnected = false;
+            _this.isBatonOwner = false;
         }
         Entities.editEntity(_this.batonDebugModel, {visible: false});
         baton.claim(_this.startUpdate, _this.maybeClaim);
diff --git a/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js b/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
index e48ef77e8f..c596b8c5f2 100644
--- a/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
+++ b/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
@@ -9,6 +9,7 @@
 
   var myEntity = Entities.addEntity({
       type: "Box",
+      name: "hifi-baton-entity",
       color: {
           red: 200,
           green: 10,

From 60bb77fd346afa0c42a2af86afc3fe473599ecb5 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 17:12:42 -0700
Subject: [PATCH 35/39] unload baton

---
 .../entityScripts/virtualBaton/batonSimpleEntityScript.js     | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js b/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
index 74f153cd7e..22eb90cc26 100644
--- a/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
+++ b/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
@@ -17,6 +17,10 @@
         baton.claim(_this.startUpdate, _this.maybeClaim);
     }
 
+    this.unload = function() {
+        baton.unload();
+    }
+
     this.preload = function(entityID) {
         _this.entityID = entityID
         _this.batonDebugModel = Entities.addEntity({

From e5364fd186568c4c6606277e1b5f54d80e0572f0 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 17:33:18 -0700
Subject: [PATCH 36/39] update

---
 .../virtualBaton/batonSimpleEntityScript.js   | 71 +++++++++++++------
 .../virtualBaton/batonSimpleEntitySpawner.js  |  9 +--
 examples/libraries/utils.js                   | 13 +++-
 3 files changed, 64 insertions(+), 29 deletions(-)

diff --git a/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js b/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
index 22eb90cc26..7a0076e9f6 100644
--- a/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
+++ b/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
@@ -1,11 +1,27 @@
 (function() {
     Script.include("../../libraries/virtualBaton.js");
+    Script.include("../../libraries/utils.js");
 
     var _this = this;
 
 
     this.startUpdate = function() {
-        Entities.editEntity(_this.batonDebugModel, {visible: true});
+        Entities.editEntity(_this.batonOwnerIndicator, {
+            visible: true
+        });
+
+        // Change color of box
+        Entities.editEntity(_this.entityID, {
+            color: randomColor()
+        });
+
+        _this.position = Entities.getEntityProperties(_this.entityID, "position").position;
+        _this.debugLightProperties.position = Vec3.sum(_this.position, {x: 0, y: 1, z: 0});
+        _this.debugLightProperties.color = randomColor();
+        var debugLight = Entities.addEntity(_this.debugLightProperties);
+        Script.setTimeout(function() {
+            Entities.deleteEntity(debugLight);
+        }, 500);
 
     }
 
@@ -13,17 +29,34 @@
         if (_this.isBatonOwner === true) {
             _this.isBatonOwner = false;
         }
-        Entities.editEntity(_this.batonDebugModel, {visible: false});
+        Entities.editEntity(_this.batonOwnerIndicator, {
+            visible: false
+        });
         baton.claim(_this.startUpdate, _this.maybeClaim);
     }
 
     this.unload = function() {
         baton.unload();
+        Entities.deleteEntity(_this.batonOwnerIndicator);
     }
 
+
     this.preload = function(entityID) {
-        _this.entityID = entityID
-        _this.batonDebugModel = Entities.addEntity({
+        _this.entityID = entityID;
+        _this.setupDebugEntities();
+
+        baton = virtualBaton({
+            batonName: "batonSimpleEntityScript:" + _this.entityID
+        });
+        _this.isBatonOwner = false;
+        _this.maybeClaim();
+
+        print("EBL Preload!!");
+    }
+
+
+    this.setupDebugEntities = function() {
+        _this.batonOwnerIndicator = Entities.addEntity({
             type: "Box",
             color: {
                 red: 200,
@@ -43,26 +76,18 @@
             parentID: MyAvatar.sessionUUID,
             visible: false
         });
-        _this.colorsToCycle = [{
-            red: 200,
-            green: 0,
-            blue: 0
-        }, {
-            red: 0,
-            green: 200,
-            blue: 0
-        }, {
-            red: 0,
-            green: 0,
-            blue: 200
-        }];
-        baton = virtualBaton({
-            batonName: "batonSimpleEntityScript:" + _this.entityID
-        });
-        _this.isBatonOwner = false;
-        _this.maybeClaim();
+    }
 
-        print("EBL Preload!!");
+    _this.debugLightProperties = {
+        type: "Light",
+        name: "hifi-baton-light",
+        dimensions: {
+            x: 10,
+            y: 10,
+            z: 10
+        },
+        falloffRadius: 3,
+        intensity: 20,
     }
 
 });
\ No newline at end of file
diff --git a/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js b/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
index c596b8c5f2..f74c1ad774 100644
--- a/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
+++ b/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
@@ -7,13 +7,13 @@
   // Math.random ensures no caching of script
   var SCRIPT_URL = Script.resolvePath("batonSimpleEntityScript.js?v1" + Math.random());
 
-  var myEntity = Entities.addEntity({
+  var batonBox = Entities.addEntity({
       type: "Box",
       name: "hifi-baton-entity",
       color: {
           red: 200,
-          green: 10,
-          blue: 10
+          green: 200,
+          blue: 200
       },
       position: center,
       dimensions: {
@@ -25,8 +25,9 @@
   });
 
 
+
   function cleanup() {
-      Entities.deleteEntity(myEntity);
+      Entities.deleteEntity(batonBox);
   }
 
   Script.scriptEnding.connect(cleanup);
\ No newline at end of file
diff --git a/examples/libraries/utils.js b/examples/libraries/utils.js
index f4a431a657..2dca32aa4f 100644
--- a/examples/libraries/utils.js
+++ b/examples/libraries/utils.js
@@ -28,7 +28,6 @@ colorMix = function(colorA, colorB, mix) {
     }
     return result;
 }
-
 scaleLine = function (start, end, scale) {
     var v = Vec3.subtract(end, start);
     var length = Vec3.length(v);
@@ -262,12 +261,22 @@ randInt = function(low, high) {
     return Math.floor(randFloat(low, high));
 }
 
+
+randomColor = function() {
+    return {
+                red: randInt(0, 255),
+                green: randInt(0, 255),
+                blue: randInt(0, 255)
+            }
+}
+
+
 hexToRgb = function(hex) {
     var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
     return result ? {
         red: parseInt(result[1], 16),
         green: parseInt(result[2], 16),
-        blue: parseInt(result[3], 16)
+        blue: parseInt(resulta[3], 16)
     } : null;
 }
 

From 7450a2ea3a1df369d291970165aeb2ea8e38a84b Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 17:38:56 -0700
Subject: [PATCH 37/39] update

---
 .../entityScripts/virtualBaton/batonSimpleEntityScript.js    | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js b/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
index 7a0076e9f6..4074113a46 100644
--- a/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
+++ b/examples/entityScripts/virtualBaton/batonSimpleEntityScript.js
@@ -6,6 +6,7 @@
 
 
     this.startUpdate = function() {
+        print("EBL START UPDATE");
         Entities.editEntity(_this.batonOwnerIndicator, {
             visible: true
         });
@@ -26,6 +27,7 @@
     }
 
     this.maybeClaim = function() {
+        print("EBL MAYBE CLAIM");
         if (_this.isBatonOwner === true) {
             _this.isBatonOwner = false;
         }
@@ -36,12 +38,14 @@
     }
 
     this.unload = function() {
+        print("EBL UNLOAD");
         baton.unload();
         Entities.deleteEntity(_this.batonOwnerIndicator);
     }
 
 
     this.preload = function(entityID) {
+        print("EBL Preload!!");
         _this.entityID = entityID;
         _this.setupDebugEntities();
 
@@ -51,7 +55,6 @@
         _this.isBatonOwner = false;
         _this.maybeClaim();
 
-        print("EBL Preload!!");
     }
 
 

From 8ce23e23451d1246910308d69c54f401bfe45fb4 Mon Sep 17 00:00:00 2001
From: ericrius1 <ericrius1>
Date: Tue, 22 Mar 2016 17:41:29 -0700
Subject: [PATCH 38/39] simple baton test

---
 examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js b/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
index f74c1ad774..0a3db8f000 100644
--- a/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
+++ b/examples/entityScripts/virtualBaton/batonSimpleEntitySpawner.js
@@ -5,7 +5,7 @@
   var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(orientation)));
 
   // Math.random ensures no caching of script
-  var SCRIPT_URL = Script.resolvePath("batonSimpleEntityScript.js?v1" + Math.random());
+  var SCRIPT_URL = Script.resolvePath("batonSimpleEntityScript.js");
 
   var batonBox = Entities.addEntity({
       type: "Box",

From e261519b11930fe1c1895441d55e1de70ac2c2c4 Mon Sep 17 00:00:00 2001
From: "James B. Pollack" <jamesbradenpollack@gmail.com>
Date: Fri, 25 Mar 2016 19:29:18 -0700
Subject: [PATCH 39/39] Update utils.js

remove extra letter that would be crashing things
---
 examples/libraries/utils.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/examples/libraries/utils.js b/examples/libraries/utils.js
index 2dca32aa4f..f39f4d7913 100644
--- a/examples/libraries/utils.js
+++ b/examples/libraries/utils.js
@@ -276,7 +276,7 @@ hexToRgb = function(hex) {
     return result ? {
         red: parseInt(result[1], 16),
         green: parseInt(result[2], 16),
-        blue: parseInt(resulta[3], 16)
+        blue: parseInt(result[3], 16)
     } : null;
 }