ScenePresenceSitTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.Servers;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Simulation;
  37. using OpenSim.Tests.Common;
  38. using System.Threading;
  39. namespace OpenSim.Region.Framework.Scenes.Tests
  40. {
  41. [TestFixture]
  42. public class ScenePresenceSitTests : OpenSimTestCase
  43. {
  44. private TestScene m_scene;
  45. private ScenePresence m_sp;
  46. [SetUp]
  47. public void Init()
  48. {
  49. m_scene = new SceneHelpers().SetupScene();
  50. m_sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  51. }
  52. [Test]
  53. public void TestSitOutsideRangeNoTarget()
  54. {
  55. TestHelpers.InMethod();
  56. // log4net.Config.XmlConfigurator.Configure();
  57. // More than 10 meters away from 0, 0, 0 (default part position)
  58. Vector3 startPos = new Vector3(10.1f, 0, 0);
  59. m_sp.AbsolutePosition = startPos;
  60. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  61. m_sp.HandleAgentRequestSit(m_sp.ControllingClient, m_sp.UUID, part.UUID, Vector3.Zero);
  62. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  63. Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(0));
  64. Assert.That(part.GetSittingAvatars(), Is.Null);
  65. Assert.That(m_sp.ParentID, Is.EqualTo(0));
  66. Assert.AreEqual(startPos, m_sp.AbsolutePosition);
  67. }
  68. [Test]
  69. public void TestSitWithinRangeNoTarget()
  70. {
  71. TestHelpers.InMethod();
  72. // log4net.Config.XmlConfigurator.Configure();
  73. // Less than 10 meters away from 0, 0, 0 (default part position)
  74. Vector3 startPos = new Vector3(9.9f, 0, 0);
  75. m_sp.AbsolutePosition = startPos;
  76. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  77. // We need to preserve this here because phys actor is removed by the sit.
  78. Vector3 spPhysActorSize = m_sp.PhysicsActor.Size;
  79. m_sp.HandleAgentRequestSit(m_sp.ControllingClient, m_sp.UUID, part.UUID, Vector3.Zero);
  80. Assert.That(m_sp.PhysicsActor, Is.Null);
  81. Assert.That(
  82. m_sp.AbsolutePosition,
  83. Is.EqualTo(part.AbsolutePosition + new Vector3(0, 0, spPhysActorSize.Z / 2)));
  84. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  85. Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(1));
  86. HashSet<ScenePresence> sittingAvatars = part.GetSittingAvatars();
  87. Assert.That(sittingAvatars.Count, Is.EqualTo(1));
  88. Assert.That(sittingAvatars.Contains(m_sp));
  89. Assert.That(m_sp.ParentID, Is.EqualTo(part.LocalId));
  90. }
  91. [Test]
  92. public void TestSitAndStandWithNoSitTarget()
  93. {
  94. TestHelpers.InMethod();
  95. // log4net.Config.XmlConfigurator.Configure();
  96. // Make sure we're within range to sit
  97. Vector3 startPos = new Vector3(1, 1, 1);
  98. m_sp.AbsolutePosition = startPos;
  99. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  100. // We need to preserve this here because phys actor is removed by the sit.
  101. Vector3 spPhysActorSize = m_sp.PhysicsActor.Size;
  102. m_sp.HandleAgentRequestSit(m_sp.ControllingClient, m_sp.UUID, part.UUID, Vector3.Zero);
  103. Assert.That(
  104. m_sp.AbsolutePosition,
  105. Is.EqualTo(part.AbsolutePosition + new Vector3(0, 0, spPhysActorSize.Z / 2)));
  106. m_sp.StandUp();
  107. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  108. Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(0));
  109. Assert.That(part.GetSittingAvatars(), Is.Null);
  110. Assert.That(m_sp.ParentID, Is.EqualTo(0));
  111. Assert.That(m_sp.PhysicsActor, Is.Not.Null);
  112. }
  113. [Test]
  114. public void TestSitAndStandWithNoSitTargetChildPrim()
  115. {
  116. TestHelpers.InMethod();
  117. // log4net.Config.XmlConfigurator.Configure();
  118. // Make sure we're within range to sit
  119. Vector3 startPos = new Vector3(1, 1, 1);
  120. m_sp.AbsolutePosition = startPos;
  121. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene, 2, m_sp.UUID, "part", 0x10).Parts[1];
  122. part.OffsetPosition = new Vector3(2, 3, 4);
  123. // We need to preserve this here because phys actor is removed by the sit.
  124. Vector3 spPhysActorSize = m_sp.PhysicsActor.Size;
  125. m_sp.HandleAgentRequestSit(m_sp.ControllingClient, m_sp.UUID, part.UUID, Vector3.Zero);
  126. Assert.That(
  127. m_sp.AbsolutePosition,
  128. Is.EqualTo(part.AbsolutePosition + new Vector3(0, 0, spPhysActorSize.Z / 2)));
  129. m_sp.StandUp();
  130. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  131. Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(0));
  132. Assert.That(part.GetSittingAvatars(), Is.Null);
  133. Assert.That(m_sp.ParentID, Is.EqualTo(0));
  134. Assert.That(m_sp.PhysicsActor, Is.Not.Null);
  135. }
  136. [Test]
  137. public void TestSitAndStandWithSitTarget()
  138. {
  139. /* sit position math as changed, this needs to be fixed later
  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. }
  184. [Test]
  185. public void TestSitAndStandOnGround()
  186. {
  187. TestHelpers.InMethod();
  188. // log4net.Config.XmlConfigurator.Configure();
  189. // If a prim has a sit target then we can sit from any distance away
  190. // Vector3 startPos = new Vector3(128, 128, 30);
  191. // sp.AbsolutePosition = startPos;
  192. m_sp.HandleAgentSitOnGround();
  193. Assert.That(m_sp.SitGround, Is.True);
  194. Assert.That(m_sp.PhysicsActor, Is.Null);
  195. m_sp.StandUp();
  196. Assert.That(m_sp.SitGround, Is.False);
  197. Assert.That(m_sp.PhysicsActor, Is.Not.Null);
  198. }
  199. }
  200. }