From 0642000a12785bc4e17b334751f386bd1ec6c702 Mon Sep 17 00:00:00 2001
From: Dave SIMmONs <simon@lindenlab.com>
Date: Mon, 28 Mar 2011 10:27:52 -0700
Subject: [PATCH] Added optional 'position' property to 'requestSit'
 LLEventHost function, re-factored code into new findObjectClosestTo()
 function.  Reviewed by Kelly

---
 indra/newview/llagentlistener.cpp | 67 ++++++++++++++++++++-----------
 indra/newview/llagentlistener.h   |  4 ++
 2 files changed, 48 insertions(+), 23 deletions(-)

diff --git a/indra/newview/llagentlistener.cpp b/indra/newview/llagentlistener.cpp
index 0a13644911b..c1edcef4c09 100644
--- a/indra/newview/llagentlistener.cpp
+++ b/indra/newview/llagentlistener.cpp
@@ -52,7 +52,8 @@ LLAgentListener::LLAgentListener(LLAgent &agent)
         "If [\"skip_confirmation\"] is true, use LLURLDispatcher rather than LLCommandDispatcher.",
         &LLAgentListener::requestTeleport);
     add("requestSit",
-        "Ask to sit on the object specified in [\"obj_uuid\"]",
+		"[\"obj_uuid\"]: id of object to sit on, use this or [\"position\"] to indicate the sit target"
+		"[\"position\"]: region position {x, y, z} where to find closest object to sit on",
         &LLAgentListener::requestSit);
     add("requestStand",
         "Ask to stand up",
@@ -149,7 +150,17 @@ void LLAgentListener::requestSit(LLSD const & event_data) const
     //mAgent.getAvatarObject()->sitOnObject();
     // shamelessly ripped from llviewermenu.cpp:handle_sit_or_stand()
     // *TODO - find a permanent place to share this code properly.
-    LLViewerObject *object = gObjectList.findObject(event_data["obj_uuid"]);
+
+	LLViewerObject *object = NULL;
+	if (event_data.has("obj_uuid"))
+	{
+		object = gObjectList.findObject(event_data["obj_uuid"]);
+	}
+	else if (event_data.has("position"))
+	{
+		LLVector3 target_position = ll_vector3_from_sd(event_data["position"]);
+		object = findObjectClosestTo(target_position);
+	}
 
     if (object && object->getPCode() == LL_PCODE_VOLUME)
     {
@@ -165,8 +176,8 @@ void LLAgentListener::requestSit(LLSD const & event_data) const
     }
 	else
 	{
-		llwarns << "LLAgent requestSit could not find the sit target " 
-			<< event_data["obj_uuid"].asUUID() << llendl;
+		llwarns << "LLAgent requestSit could not find the sit target: " 
+			<< event_data << llendl;
 	}
 }
 
@@ -175,6 +186,34 @@ void LLAgentListener::requestStand(LLSD const & event_data) const
     mAgent.setControlFlags(AGENT_CONTROL_STAND_UP);
 }
 
+
+LLViewerObject * LLAgentListener::findObjectClosestTo( const LLVector3 & position ) const
+{
+	LLViewerObject *object = NULL;
+
+	// Find the object closest to that position
+	F32 min_distance = 10000.0f;		// Start big
+	S32 num_objects = gObjectList.getNumObjects();
+	S32 cur_index = 0;
+	while (cur_index < num_objects)
+	{
+		LLViewerObject * cur_object = gObjectList.getObject(cur_index++);
+		if (cur_object)
+		{	// Calculate distance from the target position
+			LLVector3 target_diff = cur_object->getPositionRegion() - position;
+			F32 distance_to_target = target_diff.length();
+			if (distance_to_target < min_distance)
+			{	// Found an object closer
+				min_distance = distance_to_target;
+				object = cur_object;
+			}
+		}
+	}
+
+	return object;
+}
+
+
 void LLAgentListener::requestTouch(LLSD const & event_data) const
 {
 	LLViewerObject *object = NULL;
@@ -186,25 +225,7 @@ void LLAgentListener::requestTouch(LLSD const & event_data) const
 	else if (event_data.has("position"))
 	{
 		LLVector3 target_position = ll_vector3_from_sd(event_data["position"]);
-
-		// Find the object closest to that position
-		F32 min_distance = 10000.0f;		// Start big
-		S32 num_objects = gObjectList.getNumObjects();
-		S32 cur_index = 0;
-		while (cur_index < num_objects)
-		{
-			LLViewerObject * cur_object = gObjectList.getObject(cur_index++);
-			if (cur_object)
-			{	// Calculate distance from the target position
-				LLVector3 target_diff = cur_object->getPositionRegion() - target_position;
-				F32 distance_to_target = target_diff.length();
-				if (distance_to_target < min_distance)
-				{	// Found an object closer
-					min_distance = distance_to_target;
-					object = cur_object;
-				}
-			}
-		}
+		object = findObjectClosestTo(target_position);
 	}
 
 	S32 face = 0;
diff --git a/indra/newview/llagentlistener.h b/indra/newview/llagentlistener.h
index 6aeda5f8db3..c917151b71b 100644
--- a/indra/newview/llagentlistener.h
+++ b/indra/newview/llagentlistener.h
@@ -34,6 +34,8 @@
 
 class LLAgent;
 class LLSD;
+class LLViewerObject; 
+class LLVector3d;
 
 class LLAgentListener : public LLEventAPI
 {
@@ -54,6 +56,8 @@ class LLAgentListener : public LLEventAPI
     void setAutoPilotTarget(const LLSD& event) const;
     void stopAutoPilot(const LLSD& event) const;
 
+	LLViewerObject * findObjectClosestTo( const LLVector3 & position ) const;
+
 private:
     LLAgent &	mAgent;
 	LLUUID		mFollowTarget;
-- 
GitLab