From ba43a18529673341a993f24c1bb10e5528fc1158 Mon Sep 17 00:00:00 2001
From: Philip Rosedale <philip@highfidelity.io>
Date: Wed, 4 Jan 2017 23:48:24 -0800
Subject: [PATCH 1/7] simple chair entity script

---
 scripts/tutorials/entity_scripts/chair.js | 157 ++++++++++++++++++++++
 1 file changed, 157 insertions(+)
 create mode 100644 scripts/tutorials/entity_scripts/chair.js

diff --git a/scripts/tutorials/entity_scripts/chair.js b/scripts/tutorials/entity_scripts/chair.js
new file mode 100644
index 0000000000..eaf21e170e
--- /dev/null
+++ b/scripts/tutorials/entity_scripts/chair.js
@@ -0,0 +1,157 @@
+//  chair.js
+//
+//  Restrictions right now:  
+//  Chair objects need to be set as not colliding with avatars, so that they pull avatar 
+//  avatar into collision with them.  Also they need to be at or above standing height 
+//  (like a stool).  
+//    
+//  Copyright 2016 High Fidelity, Inc.
+//
+//  Distributed under the Apache License, Version 2.0.
+//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
+//
+
+(function(){ 
+    
+    var UPDATE_INTERVAL_MSECS = 1000;       //  Update the spring/object at 45Hz
+    var SETTINGS_INTERVAL_MSECS = 1000;     //  Periodically check user data for updates 
+    var DEFAULT_SIT_DISTANCE = 1.0;         //  How strong/stiff is the spring?  
+    var HYSTERESIS = 1.2;
+
+    var SITTING = 0;
+    var LEAVING = 1; 
+    var STANDING = 2;
+
+    var state = STANDING;
+    var sitDistance = DEFAULT_SIT_DISTANCE;
+    var standDistance = DEFAULT_SIT_DISTANCE / 3;
+    var entity;
+    var props;
+    var checkTimer = false;
+    var settingsTimer = false;
+    var sitting = false;
+
+    var _this;
+    
+    var WANT_DEBUG = true;
+    function debugPrint(string) {
+        if (WANT_DEBUG) {
+            print(string);
+        }
+    }
+
+    function howFarAway(position) {
+        return Vec3.distance(MyAvatar.position, position);
+    }
+
+    function isSeatOpen(position, distance) {
+        closest = true;
+        AvatarList.getAvatarIdentifiers().forEach(function(avatarSessionUUID) {
+            var avatar = AvatarList.getAvatar(avatarSessionUUID);
+            if (Vec3.distance(avatar.position, position) < distance) {
+                closest = false;
+            } 
+        });
+        return closest;
+    }
+
+    function enterSitPose() {
+        var rot;
+        var UPPER_LEG_ANGLE = 240;
+        var LOWER_LEG_ANGLE = -80;
+        rot = Quat.safeEulerAngles(MyAvatar.getJointRotation("RightUpLeg"));
+        MyAvatar.setJointData("RightUpLeg", Quat.fromPitchYawRollDegrees(UPPER_LEG_ANGLE, rot.y, rot.z), MyAvatar.getJointTranslation("RightUpLeg"));
+        rot = Quat.safeEulerAngles(MyAvatar.getJointRotation("RightLeg"));
+        MyAvatar.setJointData("RightLeg", Quat.fromPitchYawRollDegrees(LOWER_LEG_ANGLE, rot.y, rot.z), MyAvatar.getJointTranslation("RightLeg"));
+        rot = Quat.safeEulerAngles(MyAvatar.getJointRotation("LeftUpLeg"));
+        MyAvatar.setJointData("LeftUpLeg", Quat.fromPitchYawRollDegrees(UPPER_LEG_ANGLE, rot.y, rot.z), MyAvatar.getJointTranslation("LeftUpLeg"));
+        rot = Quat.safeEulerAngles(MyAvatar.getJointRotation("LeftLeg"));
+        MyAvatar.setJointData("LeftLeg", Quat.fromPitchYawRollDegrees(LOWER_LEG_ANGLE, rot.y, rot.z), MyAvatar.getJointTranslation("LeftLeg"));
+    }
+    function leaveSitPose() {
+         MyAvatar.clearJointData("RightUpLeg");
+         MyAvatar.clearJointData("LeftUpLeg");
+         MyAvatar.clearJointData("RightLeg");
+         MyAvatar.clearJointData("LeftLeg");
+    }
+
+    function moveToSeat(position, rotation) {
+        var eulers = Quat.safeEulerAngles(MyAvatar.orientation);
+        eulers.y = Quat.safeEulerAngles(rotation).y;
+        MyAvatar.position = position;
+        MyAvatar.orientation = Quat.fromPitchYawRollDegrees(eulers.x, eulers.y, eulers.z);
+    }
+
+    this.preload = function(entityID) { 
+        //  Load the sound and range from the entity userData fields, and note the position of the entity.
+        debugPrint("chair preload");
+        entity = entityID;
+        _this = this;
+        checkTimer = Script.setInterval(this.maybeSitOrStand, UPDATE_INTERVAL_MSECS);
+        settingsTimer = Script.setInterval(this.checkSettings, SETTINGS_INTERVAL_MSECS);
+    }; 
+
+    this.maybeSitOrStand = function() {
+        props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
+        // First, check if the entity is far enough away to not need to do anything with it
+        var howFar = howFarAway(props.position);
+        if ((state === STANDING) && (howFar < sitDistance) && isSeatOpen(props.position, standDistance)) {
+            moveToSeat(props.position, props.rotation);
+            //MyAvatar.characterControllerEnabled = true;
+            enterSitPose();
+            state = SITTING;
+            debugPrint("Sitting");
+        } else if ((state === SITTING) && (howFar > standDistance)) {
+            leaveSitPose();
+            state = LEAVING;
+            MyAvatar.characterControllerEnabled = true;
+            debugPrint("Leaving");
+        } else if ((state === LEAVING) && (howFar > sitDistance * HYSTERESIS)) {
+            state = STANDING;
+            debugPrint("Standing");
+        }
+    }
+
+    this.clickDownOnEntity = function(entityID, mouseEvent) { 
+        //  If entity is clicked, jump to seat
+        
+        props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
+        if ((state === STANDING) && isSeatOpen(props.position, standDistance)) {
+            moveToSeat(props.position, props.rotation);
+            //MyAvatar.characterControllerEnabled = false;
+            enterSitPose();
+            state = SITTING;
+            debugPrint("Sitting");
+        }
+    }
+
+    this.checkSettings = function() {
+        var dataProps = Entities.getEntityProperties(entity, [ "userData" ]);
+        if (dataProps.userData) {
+            var data = JSON.parse(dataProps.userData);
+            if (data.sitDistance) {
+                if (!(sitDistance === data.sitDistance)) {
+                    debugPrint("Read new sit distance: " + data.sitDistance); 
+                }
+                sitDistance = data.sitDistance;
+            }
+            if (data.standDistance) {
+                if (!(standDistance === data.standDistance)) {
+                    debugPrint("Read new stand distance: " + data.standDistance); 
+                }
+                standDistance = data.standDistance;
+            }
+        }
+    }
+
+    this.unload = function(entityID) { 
+        debugPrint("chair unload");
+        if (checkTimer) {
+            Script.clearInterval(checkTimer);
+        }
+        if (settingsTimer) {
+            Script.clearInterval(settingsTimer);
+        }
+    }; 
+
+}) 

From 88429c4033c73b0b27d167e15c17b1fe33538266 Mon Sep 17 00:00:00 2001
From: Philip Rosedale <philip@highfidelity.io>
Date: Wed, 4 Jan 2017 23:51:05 -0800
Subject: [PATCH 2/7] wrong file

---
 .../tutorials/entity_scripts/springAway.js    | 130 ------------------
 1 file changed, 130 deletions(-)
 delete mode 100644 scripts/tutorials/entity_scripts/springAway.js

diff --git a/scripts/tutorials/entity_scripts/springAway.js b/scripts/tutorials/entity_scripts/springAway.js
deleted file mode 100644
index 1652db216f..0000000000
--- a/scripts/tutorials/entity_scripts/springAway.js
+++ /dev/null
@@ -1,130 +0,0 @@
-//  springAway.js
-//
-//  If you attach this entity script to an object, the object will spring away from  
-//  your avatar's hands.  Useful for making a beachball or basketball, because you
-//  can bounce it on your hands.
-//
-//  You can change the force applied by the script by setting userData to contain
-//  a value 'strength', which will otherwise default to DEFAULT_STRENGTH
-//
-//  Note that the use of dimensions.x as the size of the entity means that it 
-//  needs to be spherical for this to look correct.  
-//   
-//  Copyright 2016 High Fidelity, Inc.
-//
-//  Distributed under the Apache License, Version 2.0.
-//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
-//
-
-(function(){ 
-    
-    var UPDATE_INTERVAL_MSECS = 1000/45;    //  Update the spring/object at 45Hz
-    var SETTINGS_INTERVAL_MSECS = 1000;     //  Periodically check user data for updates 
-    var TOO_FAR = 5.0;                      //  If the object 
-    var DEFAULT_STRENGTH = 3.0;             //  How strong/stiff is the spring?  
-
-
-    var entity;
-    var props;
-    var checkTimer = false;
-    var settingsTimer = false;
-    var strength = DEFAULT_STRENGTH;
-    var _this;
-    
-    var WANT_DEBUG = false;
-    function debugPrint(string) {
-        if (WANT_DEBUG) {
-            print(string);
-        }
-    }
-
-    function howFarAway(position) {
-        return Vec3.distance(MyAvatar.position, position);
-    }
-
-    function springForce(position, center, radius) {
-        //
-        //  Return a vector corresponding to a normalized spring force ranging from 1 at 
-        //  exact center to zero at distance radius from center.
-        //
-        var distance = Vec3.distance(position, center);
-        return Vec3.multiply(1.0 - distance / radius, Vec3.normalize(Vec3.subtract(center, position)));
-    }
-
-    function fingerPosition(which) {
-        //
-        //  Get the worldspace position of either the tip of the index finger (jointName "RightHandIndex3", etc), or 
-        //  fall back to the controller position if that doesn't exist.
-        //
-        var joint = MyAvatar.getJointPosition(which === "RIGHT" ? "RightHandIndex3" : "LeftHandIndex3");
-        if (Vec3.length(joint) > 0) {
-            return joint;
-        } else {
-            return Vec3.sum(MyAvatar.position,
-                        Vec3.multiplyQbyV(MyAvatar.orientation, 
-                            Controller.getPoseValue(which === "RIGHT" ? Controller.Standard.RightHand : Controller.Standard.LeftHand).translation)); 
-        }  
-    }
-
-    this.preload = function(entityID) { 
-        //  Load the sound and range from the entity userData fields, and note the position of the entity.
-        debugPrint("springAway preload");
-        entity = entityID;
-        _this = this;
-        checkTimer = Script.setInterval(this.maybePush, UPDATE_INTERVAL_MSECS);
-        settingsTimer = Script.setInterval(this.checkSettings, SETTINGS_INTERVAL_MSECS);
-    }; 
-
-    this.maybePush = function() {
-        props = Entities.getEntityProperties(entity, [ "position", "dimensions", "velocity" ]);
-
-        // First, check if the entity is far enough away to not need to do anything with it
-        
-        if (howFarAway(props.position) - props.dimensions.x / 2 > TOO_FAR) {
-            return;
-        }
-
-        var rightFingerPosition = fingerPosition("RIGHT");
-        var leftFingerPosition = fingerPosition("LEFT");
-
-        var addVelocity = { x: 0, y: 0, z: 0 };
-        
-        
-        if (Vec3.distance(leftFingerPosition, props.position) < props.dimensions.x / 2) {
-            addVelocity = Vec3.sum(addVelocity, Vec3.multiply(springForce(leftFingerPosition, props.position, props.dimensions.x), strength));
-        }
-        if (Vec3.distance(rightFingerPosition, props.position) < props.dimensions.x / 2) {
-            addVelocity = Vec3.sum(addVelocity, Vec3.multiply(springForce(rightFingerPosition, props.position, props.dimensions.x), strength));
-        }
-
-        if (Vec3.length(addVelocity) > 0) {
-            Entities.editEntity(entity, {
-                velocity:  Vec3.sum(props.velocity, addVelocity)
-            });
-        }
-    }
-
-    this.checkSettings = function() {
-        var dataProps = Entities.getEntityProperties(entity, [ "userData" ]);
-        if (dataProps.userData) {
-            var data = JSON.parse(dataProps.userData);
-            if (data.strength) {
-                if (!(strength === data.strength)) {
-                    debugPrint("Read new spring strength: " + data.strength); 
-                }
-                strength = data.strength;
-            }
-        }
-    }
-
-    this.unload = function(entityID) { 
-        debugPrint("springAway unload");
-        if (checkTimer) {
-            Script.clearInterval(checkTimer);
-        }
-        if (settingsTimer) {
-            Script.clearInterval(settingsTimer);
-        }
-    }; 
-
-}) 

From 0ad9a8625b23d0099b8d8caba7f4e477488cacae Mon Sep 17 00:00:00 2001
From: Philip Rosedale <philip@highfidelity.io>
Date: Thu, 5 Jan 2017 12:58:26 -0800
Subject: [PATCH 3/7] Added far trigger

---
 scripts/tutorials/entity_scripts/chair.js | 57 +++++++++++++++--------
 1 file changed, 37 insertions(+), 20 deletions(-)

diff --git a/scripts/tutorials/entity_scripts/chair.js b/scripts/tutorials/entity_scripts/chair.js
index eaf21e170e..27646ad744 100644
--- a/scripts/tutorials/entity_scripts/chair.js
+++ b/scripts/tutorials/entity_scripts/chair.js
@@ -16,15 +16,16 @@
     var UPDATE_INTERVAL_MSECS = 1000;       //  Update the spring/object at 45Hz
     var SETTINGS_INTERVAL_MSECS = 1000;     //  Periodically check user data for updates 
     var DEFAULT_SIT_DISTANCE = 1.0;         //  How strong/stiff is the spring?  
-    var HYSTERESIS = 1.2;
+    var HYSTERESIS = 1.1;
 
+    var sitTarget = { x: 0, y: 0, z: 0 };   //  Offset where your butt should go relative
+                                            //  to the object's center.
     var SITTING = 0;
-    var LEAVING = 1; 
-    var STANDING = 2;
+    var STANDING = 1;
 
     var state = STANDING;
     var sitDistance = DEFAULT_SIT_DISTANCE;
-    var standDistance = DEFAULT_SIT_DISTANCE / 3;
+
     var entity;
     var props;
     var checkTimer = false;
@@ -48,7 +49,8 @@
         closest = true;
         AvatarList.getAvatarIdentifiers().forEach(function(avatarSessionUUID) {
             var avatar = AvatarList.getAvatar(avatarSessionUUID);
-            if (Vec3.distance(avatar.position, position) < distance) {
+            if (avatarSessionUUID && Vec3.distance(avatar.position, position) < distance) {
+                debugPrint("Seat Occupied!");
                 closest = false;
             } 
         });
@@ -78,7 +80,7 @@
     function moveToSeat(position, rotation) {
         var eulers = Quat.safeEulerAngles(MyAvatar.orientation);
         eulers.y = Quat.safeEulerAngles(rotation).y;
-        MyAvatar.position = position;
+        MyAvatar.position = Vec3.sum(position, Vec3.multiplyQbyV(props.rotation, sitTarget));
         MyAvatar.orientation = Quat.fromPitchYawRollDegrees(eulers.x, eulers.y, eulers.z);
     }
 
@@ -95,36 +97,41 @@
         props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
         // First, check if the entity is far enough away to not need to do anything with it
         var howFar = howFarAway(props.position);
-        if ((state === STANDING) && (howFar < sitDistance) && isSeatOpen(props.position, standDistance)) {
+        if ((state === STANDING) && (howFar < sitDistance) && isSeatOpen(props.position, sitDistance)) {
             moveToSeat(props.position, props.rotation);
             //MyAvatar.characterControllerEnabled = true;
             enterSitPose();
             state = SITTING;
-            debugPrint("Sitting");
-        } else if ((state === SITTING) && (howFar > standDistance)) {
+            debugPrint("Sitting from being close");
+        } else if ((state === SITTING) && (howFar > sitDistance * HYSTERESIS)) {
             leaveSitPose();
-            state = LEAVING;
-            MyAvatar.characterControllerEnabled = true;
-            debugPrint("Leaving");
-        } else if ((state === LEAVING) && (howFar > sitDistance * HYSTERESIS)) {
             state = STANDING;
+            MyAvatar.characterControllerEnabled = true;
             debugPrint("Standing");
-        }
+        } 
     }
 
     this.clickDownOnEntity = function(entityID, mouseEvent) { 
         //  If entity is clicked, jump to seat
         
         props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
-        if ((state === STANDING) && isSeatOpen(props.position, standDistance)) {
+        if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) {
             moveToSeat(props.position, props.rotation);
             //MyAvatar.characterControllerEnabled = false;
             enterSitPose();
             state = SITTING;
-            debugPrint("Sitting");
+            debugPrint("Sitting from mouse click");
         }
     }
 
+    this.startFarTrigger = function() {
+            moveToSeat(props.position, props.rotation);
+            //MyAvatar.characterControllerEnabled = false;
+            enterSitPose();
+            state = SITTING;
+            debugPrint("Sitting from far trigger");
+    }
+
     this.checkSettings = function() {
         var dataProps = Entities.getEntityProperties(entity, [ "userData" ]);
         if (dataProps.userData) {
@@ -135,15 +142,25 @@
                 }
                 sitDistance = data.sitDistance;
             }
-            if (data.standDistance) {
-                if (!(standDistance === data.standDistance)) {
-                    debugPrint("Read new stand distance: " + data.standDistance); 
+            if (data.sitTarget) {
+                if (data.sitTarget.y && (data.sitTarget.y != sitTarget.y)) {
+                    debugPrint("Read new sitTarget.y: " + data.sitTarget.y); 
+                    sitTarget.y = data.sitTarget.y;
+                }
+                if (data.sitTarget.x && (data.sitTarget.x != sitTarget.x)) {
+                    debugPrint("Read new sitTarget.x: " + data.sitTarget.x); 
+                    sitTarget.x = data.sitTarget.x;
+                }
+                if (data.sitTarget.z && (data.sitTarget.z != sitTarget.z)) {
+                    debugPrint("Read new sitTarget.z: " + data.sitTarget.z); 
+                    sitTarget.z = data.sitTarget.z;
                 }
-                standDistance = data.standDistance;
             }
         }
     }
 
+
+
     this.unload = function(entityID) { 
         debugPrint("chair unload");
         if (checkTimer) {

From a689d5200403b5fbc2509b276eefa8e603cbfeb2 Mon Sep 17 00:00:00 2001
From: Philip Rosedale <philip@highfidelity.io>
Date: Thu, 5 Jan 2017 13:08:50 -0800
Subject: [PATCH 4/7] wrap far trigger too with seated check

---
 scripts/tutorials/entity_scripts/chair.js | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/scripts/tutorials/entity_scripts/chair.js b/scripts/tutorials/entity_scripts/chair.js
index 27646ad744..09c2e283c2 100644
--- a/scripts/tutorials/entity_scripts/chair.js
+++ b/scripts/tutorials/entity_scripts/chair.js
@@ -99,7 +99,6 @@
         var howFar = howFarAway(props.position);
         if ((state === STANDING) && (howFar < sitDistance) && isSeatOpen(props.position, sitDistance)) {
             moveToSeat(props.position, props.rotation);
-            //MyAvatar.characterControllerEnabled = true;
             enterSitPose();
             state = SITTING;
             debugPrint("Sitting from being close");
@@ -117,7 +116,6 @@
         props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
         if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) {
             moveToSeat(props.position, props.rotation);
-            //MyAvatar.characterControllerEnabled = false;
             enterSitPose();
             state = SITTING;
             debugPrint("Sitting from mouse click");
@@ -125,11 +123,12 @@
     }
 
     this.startFarTrigger = function() {
+        if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) {
             moveToSeat(props.position, props.rotation);
-            //MyAvatar.characterControllerEnabled = false;
             enterSitPose();
             state = SITTING;
             debugPrint("Sitting from far trigger");
+        }
     }
 
     this.checkSettings = function() {

From 65b9aaed1b81458aded1310ff3fd7f9b4f799550 Mon Sep 17 00:00:00 2001
From: Philip Rosedale <philip@highfidelity.io>
Date: Thu, 5 Jan 2017 13:13:50 -0800
Subject: [PATCH 5/7] one more try

---
 scripts/tutorials/entity_scripts/chair.js | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/scripts/tutorials/entity_scripts/chair.js b/scripts/tutorials/entity_scripts/chair.js
index 09c2e283c2..379450c1f6 100644
--- a/scripts/tutorials/entity_scripts/chair.js
+++ b/scripts/tutorials/entity_scripts/chair.js
@@ -111,8 +111,7 @@
     }
 
     this.clickDownOnEntity = function(entityID, mouseEvent) { 
-        //  If entity is clicked, jump to seat
-        
+        //  If entity is clicked, sit
         props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
         if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) {
             moveToSeat(props.position, props.rotation);
@@ -123,6 +122,8 @@
     }
 
     this.startFarTrigger = function() {
+        //  If entity is far clicked, sit
+        props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
         if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) {
             moveToSeat(props.position, props.rotation);
             enterSitPose();

From 07ecf604857f2aa5158d985191fa7cbdd33ecdee Mon Sep 17 00:00:00 2001
From: Philip Rosedale <philip@highfidelity.io>
Date: Thu, 5 Jan 2017 13:48:31 -0800
Subject: [PATCH 6/7] remove sit when close

---
 scripts/tutorials/entity_scripts/chair.js | 35 ++++++++++-------------
 1 file changed, 15 insertions(+), 20 deletions(-)

diff --git a/scripts/tutorials/entity_scripts/chair.js b/scripts/tutorials/entity_scripts/chair.js
index 379450c1f6..f94f3d76e4 100644
--- a/scripts/tutorials/entity_scripts/chair.js
+++ b/scripts/tutorials/entity_scripts/chair.js
@@ -13,9 +13,9 @@
 
 (function(){ 
     
-    var UPDATE_INTERVAL_MSECS = 1000;       //  Update the spring/object at 45Hz
+    var CHECK_INTERVAL_MSECS = 250;         //  When sitting, check for need to stand up
     var SETTINGS_INTERVAL_MSECS = 1000;     //  Periodically check user data for updates 
-    var DEFAULT_SIT_DISTANCE = 1.0;         //  How strong/stiff is the spring?  
+    var DEFAULT_SIT_DISTANCE = 1.0;         //  How far away from the chair can you sit?
     var HYSTERESIS = 1.1;
 
     var sitTarget = { x: 0, y: 0, z: 0 };   //  Offset where your butt should go relative
@@ -70,6 +70,7 @@
         rot = Quat.safeEulerAngles(MyAvatar.getJointRotation("LeftLeg"));
         MyAvatar.setJointData("LeftLeg", Quat.fromPitchYawRollDegrees(LOWER_LEG_ANGLE, rot.y, rot.z), MyAvatar.getJointTranslation("LeftLeg"));
     }
+
     function leaveSitPose() {
          MyAvatar.clearJointData("RightUpLeg");
          MyAvatar.clearJointData("LeftUpLeg");
@@ -77,11 +78,14 @@
          MyAvatar.clearJointData("LeftLeg");
     }
 
-    function moveToSeat(position, rotation) {
+    function sitDown(position, rotation) {
         var eulers = Quat.safeEulerAngles(MyAvatar.orientation);
         eulers.y = Quat.safeEulerAngles(rotation).y;
         MyAvatar.position = Vec3.sum(position, Vec3.multiplyQbyV(props.rotation, sitTarget));
         MyAvatar.orientation = Quat.fromPitchYawRollDegrees(eulers.x, eulers.y, eulers.z);
+
+        enterSitPose();
+        state = SITTING;
     }
 
     this.preload = function(entityID) { 
@@ -89,23 +93,18 @@
         debugPrint("chair preload");
         entity = entityID;
         _this = this;
-        checkTimer = Script.setInterval(this.maybeSitOrStand, UPDATE_INTERVAL_MSECS);
         settingsTimer = Script.setInterval(this.checkSettings, SETTINGS_INTERVAL_MSECS);
     }; 
 
-    this.maybeSitOrStand = function() {
+    this.maybeStand = function() {
         props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
         // First, check if the entity is far enough away to not need to do anything with it
         var howFar = howFarAway(props.position);
-        if ((state === STANDING) && (howFar < sitDistance) && isSeatOpen(props.position, sitDistance)) {
-            moveToSeat(props.position, props.rotation);
-            enterSitPose();
-            state = SITTING;
-            debugPrint("Sitting from being close");
-        } else if ((state === SITTING) && (howFar > sitDistance * HYSTERESIS)) {
+        if ((state === SITTING) && (howFar > sitDistance * HYSTERESIS)) {
             leaveSitPose();
+            Script.clearInterval(checkTimer);
+            checkTimer = null;
             state = STANDING;
-            MyAvatar.characterControllerEnabled = true;
             debugPrint("Standing");
         } 
     }
@@ -114,9 +113,8 @@
         //  If entity is clicked, sit
         props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
         if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) {
-            moveToSeat(props.position, props.rotation);
-            enterSitPose();
-            state = SITTING;
+            sitDown(props.position, props.rotation);
+            checkTimer = Script.setInterval(this.maybeStand, CHECK_INTERVAL_MSECS);
             debugPrint("Sitting from mouse click");
         }
     }
@@ -125,9 +123,8 @@
         //  If entity is far clicked, sit
         props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
         if ((state === STANDING) && isSeatOpen(props.position, sitDistance)) {
-            moveToSeat(props.position, props.rotation);
-            enterSitPose();
-            state = SITTING;
+            sitDown(props.position, props.rotation);
+            checkTimer = Script.setInterval(this.maybeStand, CHECK_INTERVAL_MSECS);
             debugPrint("Sitting from far trigger");
         }
     }
@@ -159,8 +156,6 @@
         }
     }
 
-
-
     this.unload = function(entityID) { 
         debugPrint("chair unload");
         if (checkTimer) {

From 9bd858c1ab8695ce3c1433529ae34f03bf29b881 Mon Sep 17 00:00:00 2001
From: Philip Rosedale <philip@highfidelity.io>
Date: Thu, 5 Jan 2017 13:49:38 -0800
Subject: [PATCH 7/7] no log spam

---
 scripts/tutorials/entity_scripts/chair.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/tutorials/entity_scripts/chair.js b/scripts/tutorials/entity_scripts/chair.js
index f94f3d76e4..e28e253657 100644
--- a/scripts/tutorials/entity_scripts/chair.js
+++ b/scripts/tutorials/entity_scripts/chair.js
@@ -34,7 +34,7 @@
 
     var _this;
     
-    var WANT_DEBUG = true;
+    var WANT_DEBUG = false;
     function debugPrint(string) {
         if (WANT_DEBUG) {
             print(string);