ScenePresenceSitTests.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using Nini.Config;
  31. using NUnit.Framework;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Servers;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Simulation;
  38. using OpenSim.Tests.Common;
  39. using System.Threading;
  40. namespace OpenSim.Region.Framework.Scenes.Tests
  41. {
  42. [TestFixture]
  43. public class ScenePresenceSitTests : OpenSimTestCase
  44. {
  45. private TestScene m_scene;
  46. private ScenePresence m_sp;
  47. [SetUp]
  48. public void Init()
  49. {
  50. m_scene = new SceneHelpers().SetupScene();
  51. m_sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  52. }
  53. [Test]
  54. public void TestSitOutsideRangeNoTarget()
  55. {
  56. TestHelpers.InMethod();
  57. // log4net.Config.XmlConfigurator.Configure();
  58. // More than 10 meters away from 0, 0, 0 (default part position)
  59. Vector3 startPos = new Vector3(10.1f, 0, 0);
  60. m_sp.AbsolutePosition = startPos;
  61. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  62. m_sp.HandleAgentRequestSit(m_sp.ControllingClient, m_sp.UUID, part.UUID, Vector3.Zero);
  63. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  64. Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(0));
  65. Assert.That(part.GetSittingAvatars(), Is.Null);
  66. Assert.That(m_sp.ParentID, Is.EqualTo(0));
  67. Assert.AreEqual(startPos, m_sp.AbsolutePosition);
  68. }
  69. [Test]
  70. public void TestSitWithinRangeNoTarget()
  71. {
  72. TestHelpers.InMethod();
  73. // log4net.Config.XmlConfigurator.Configure();
  74. // Less than 10 meters away from 0, 0, 0 (default part position)
  75. Vector3 startPos = new Vector3(9.9f, 0, 0);
  76. m_sp.AbsolutePosition = startPos;
  77. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  78. // We need to preserve this here because phys actor is removed by the sit.
  79. Vector3 spPhysActorSize = m_sp.PhysicsActor.Size;
  80. m_sp.HandleAgentRequestSit(m_sp.ControllingClient, m_sp.UUID, part.UUID, Vector3.Zero);
  81. Assert.That(m_sp.PhysicsActor, Is.Null);
  82. Assert.That(
  83. m_sp.AbsolutePosition,
  84. Is.EqualTo(part.AbsolutePosition + new Vector3(0, 0, spPhysActorSize.Z / 2)));
  85. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  86. Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(1));
  87. HashSet<ScenePresence> sittingAvatars = part.GetSittingAvatars();
  88. Assert.That(sittingAvatars.Count, Is.EqualTo(1));
  89. Assert.That(sittingAvatars.Contains(m_sp));
  90. Assert.That(m_sp.ParentID, Is.EqualTo(part.LocalId));
  91. }
  92. [Test]
  93. public void TestSitAndStandWithNoSitTarget()
  94. {
  95. TestHelpers.InMethod();
  96. // log4net.Config.XmlConfigurator.Configure();
  97. // Make sure we're within range to sit
  98. Vector3 startPos = new Vector3(1, 1, 1);
  99. m_sp.AbsolutePosition = startPos;
  100. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  101. // We need to preserve this here because phys actor is removed by the sit.
  102. Vector3 spPhysActorSize = m_sp.PhysicsActor.Size;
  103. m_sp.HandleAgentRequestSit(m_sp.ControllingClient, m_sp.UUID, part.UUID, Vector3.Zero);
  104. Assert.That(
  105. m_sp.AbsolutePosition,
  106. Is.EqualTo(part.AbsolutePosition + new Vector3(0, 0, spPhysActorSize.Z / 2)));
  107. m_sp.StandUp();
  108. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  109. Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(0));
  110. Assert.That(part.GetSittingAvatars(), Is.Null);
  111. Assert.That(m_sp.ParentID, Is.EqualTo(0));
  112. Assert.That(m_sp.PhysicsActor, Is.Not.Null);
  113. }
  114. [Test]
  115. public void TestSitAndStandWithNoSitTargetChildPrim()
  116. {
  117. TestHelpers.InMethod();
  118. // log4net.Config.XmlConfigurator.Configure();
  119. // Make sure we're within range to sit
  120. Vector3 startPos = new Vector3(1, 1, 1);
  121. m_sp.AbsolutePosition = startPos;
  122. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene, 2, m_sp.UUID, "part", 0x10).Parts[1];
  123. part.OffsetPosition = new Vector3(2, 3, 4);
  124. // We need to preserve this here because phys actor is removed by the sit.
  125. Vector3 spPhysActorSize = m_sp.PhysicsActor.Size;
  126. m_sp.HandleAgentRequestSit(m_sp.ControllingClient, m_sp.UUID, part.UUID, Vector3.Zero);
  127. Assert.That(
  128. m_sp.AbsolutePosition,
  129. Is.EqualTo(part.AbsolutePosition + new Vector3(0, 0, spPhysActorSize.Z / 2)));
  130. m_sp.StandUp();
  131. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  132. Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(0));
  133. Assert.That(part.GetSittingAvatars(), Is.Null);
  134. Assert.That(m_sp.ParentID, Is.EqualTo(0));
  135. Assert.That(m_sp.PhysicsActor, Is.Not.Null);
  136. }
  137. [Test]
  138. public void TestSitAndStandWithSitTarget()
  139. {
  140. TestHelpers.InMethod();
  141. // log4net.Config.XmlConfigurator.Configure();
  142. // If a prim has a sit target then we can sit from any distance away
  143. Vector3 startPos = new Vector3(128, 128, 30);
  144. m_sp.AbsolutePosition = startPos;
  145. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  146. part.SitTargetPosition = new Vector3(0, 0, 1);
  147. m_sp.HandleAgentRequestSit(m_sp.ControllingClient, m_sp.UUID, part.UUID, Vector3.Zero);
  148. Assert.That(part.SitTargetAvatar, Is.EqualTo(m_sp.UUID));
  149. Assert.That(m_sp.ParentID, Is.EqualTo(part.LocalId));
  150. // This section is copied from ScenePresence.HandleAgentSit(). Correctness is not guaranteed.
  151. double x, y, z, m1, m2;
  152. Quaternion r = part.SitTargetOrientation;;
  153. m1 = r.X * r.X + r.Y * r.Y;
  154. m2 = r.Z * r.Z + r.W * r.W;
  155. // Rotate the vector <0, 0, 1>
  156. x = 2 * (r.X * r.Z + r.Y * r.W);
  157. y = 2 * (-r.X * r.W + r.Y * r.Z);
  158. z = m2 - m1;
  159. // Set m to be the square of the norm of r.
  160. double m = m1 + m2;
  161. // This constant is emperically determined to be what is used in SL.
  162. // See also http://opensimulator.org/mantis/view.php?id=7096
  163. double offset = 0.05;
  164. Vector3 up = new Vector3((float)x, (float)y, (float)z);
  165. Vector3 sitOffset = up * (float)offset;
  166. // End of copied section.
  167. Assert.That(
  168. m_sp.AbsolutePosition,
  169. Is.EqualTo(part.AbsolutePosition + part.SitTargetPosition - sitOffset + ScenePresence.SIT_TARGET_ADJUSTMENT));
  170. Assert.That(m_sp.PhysicsActor, Is.Null);
  171. Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(1));
  172. HashSet<ScenePresence> sittingAvatars = part.GetSittingAvatars();
  173. Assert.That(sittingAvatars.Count, Is.EqualTo(1));
  174. Assert.That(sittingAvatars.Contains(m_sp));
  175. m_sp.StandUp();
  176. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  177. Assert.That(m_sp.ParentID, Is.EqualTo(0));
  178. Assert.That(m_sp.PhysicsActor, Is.Not.Null);
  179. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  180. Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(0));
  181. Assert.That(part.GetSittingAvatars(), Is.Null);
  182. }
  183. [Test]
  184. public void TestSitAndStandOnGround()
  185. {
  186. TestHelpers.InMethod();
  187. // log4net.Config.XmlConfigurator.Configure();
  188. // If a prim has a sit target then we can sit from any distance away
  189. // Vector3 startPos = new Vector3(128, 128, 30);
  190. // sp.AbsolutePosition = startPos;
  191. m_sp.HandleAgentSitOnGround();
  192. Assert.That(m_sp.SitGround, Is.True);
  193. Assert.That(m_sp.PhysicsActor, Is.Null);
  194. m_sp.StandUp();
  195. Assert.That(m_sp.SitGround, Is.False);
  196. Assert.That(m_sp.PhysicsActor, Is.Not.Null);
  197. }
  198. }
  199. }