NPCModuleTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 log4net;
  31. using Nini.Config;
  32. using NUnit.Framework;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Communications;
  36. using OpenSim.Region.CoreModules.Avatar.Attachments;
  37. using OpenSim.Region.CoreModules.Avatar.AvatarFactory;
  38. using OpenSim.Region.CoreModules.Framework.InventoryAccess;
  39. using OpenSim.Region.CoreModules.Framework.UserManagement;
  40. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Avatar;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes;
  43. using OpenSim.Services.AvatarService;
  44. using OpenSim.Tests.Common;
  45. using OpenSim.Tests.Common.Mock;
  46. namespace OpenSim.Region.OptionalModules.World.NPC.Tests
  47. {
  48. [TestFixture]
  49. public class NPCModuleTests
  50. {
  51. private TestScene m_scene;
  52. private AvatarFactoryModule m_afMod;
  53. private UserManagementModule m_umMod;
  54. private AttachmentsModule m_attMod;
  55. private NPCModule m_npcMod;
  56. [TestFixtureSetUp]
  57. public void FixtureInit()
  58. {
  59. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  60. Util.FireAndForgetMethod = FireAndForgetMethod.None;
  61. }
  62. [TestFixtureTearDown]
  63. public void TearDown()
  64. {
  65. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  66. // threads. Possibly, later tests should be rewritten not to worry about such things.
  67. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  68. }
  69. [SetUp]
  70. public void Init()
  71. {
  72. IConfigSource config = new IniConfigSource();
  73. config.AddConfig("NPC");
  74. config.Configs["NPC"].Set("Enabled", "true");
  75. config.AddConfig("Modules");
  76. config.Configs["Modules"].Set("InventoryAccessModule", "BasicInventoryAccessModule");
  77. m_afMod = new AvatarFactoryModule();
  78. m_umMod = new UserManagementModule();
  79. m_attMod = new AttachmentsModule();
  80. m_npcMod = new NPCModule();
  81. m_scene = new SceneHelpers().SetupScene();
  82. SceneHelpers.SetupSceneModules(m_scene, config, m_afMod, m_umMod, m_attMod, m_npcMod, new BasicInventoryAccessModule());
  83. }
  84. [Test]
  85. public void TestCreate()
  86. {
  87. TestHelpers.InMethod();
  88. // log4net.Config.XmlConfigurator.Configure();
  89. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  90. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  91. // 8 is the index of the first baked texture in AvatarAppearance
  92. UUID originalFace8TextureId = TestHelpers.ParseTail(0x10);
  93. Primitive.TextureEntry originalTe = new Primitive.TextureEntry(UUID.Zero);
  94. Primitive.TextureEntryFace originalTef = originalTe.CreateFace(8);
  95. originalTef.TextureID = originalFace8TextureId;
  96. // We also need to add the texture to the asset service, otherwise the AvatarFactoryModule will tell
  97. // ScenePresence.SendInitialData() to reset our entire appearance.
  98. m_scene.AssetService.Store(AssetHelpers.CreateNotecardAsset(originalFace8TextureId));
  99. m_afMod.SetAppearance(sp, originalTe, null);
  100. UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
  101. ScenePresence npc = m_scene.GetScenePresence(npcId);
  102. Assert.That(npc, Is.Not.Null);
  103. Assert.That(npc.Appearance.Texture.FaceTextures[8].TextureID, Is.EqualTo(originalFace8TextureId));
  104. Assert.That(m_umMod.GetUserName(npc.UUID), Is.EqualTo(string.Format("{0} {1}", npc.Firstname, npc.Lastname)));
  105. }
  106. [Test]
  107. public void TestRemove()
  108. {
  109. TestHelpers.InMethod();
  110. // log4net.Config.XmlConfigurator.Configure();
  111. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  112. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  113. Vector3 startPos = new Vector3(128, 128, 30);
  114. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  115. m_npcMod.DeleteNPC(npcId, m_scene);
  116. ScenePresence deletedNpc = m_scene.GetScenePresence(npcId);
  117. Assert.That(deletedNpc, Is.Null);
  118. }
  119. [Test]
  120. public void TestCreateWithAttachments()
  121. {
  122. TestHelpers.InMethod();
  123. // log4net.Config.XmlConfigurator.Configure();
  124. UUID userId = TestHelpers.ParseTail(0x1);
  125. UserAccountHelpers.CreateUserWithInventory(m_scene, userId);
  126. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  127. UUID attItemId = TestHelpers.ParseTail(0x2);
  128. UUID attAssetId = TestHelpers.ParseTail(0x3);
  129. string attName = "att";
  130. UserInventoryHelpers.CreateInventoryItem(m_scene, attName, attItemId, attAssetId, sp.UUID, InventoryType.Object);
  131. m_attMod.RezSingleAttachmentFromInventory(sp, attItemId, (uint)AttachmentPoint.Chest);
  132. UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
  133. ScenePresence npc = m_scene.GetScenePresence(npcId);
  134. // Check scene presence status
  135. Assert.That(npc.HasAttachments(), Is.True);
  136. List<SceneObjectGroup> attachments = npc.GetAttachments();
  137. Assert.That(attachments.Count, Is.EqualTo(1));
  138. SceneObjectGroup attSo = attachments[0];
  139. // Just for now, we won't test the name since this is (wrongly) the asset part name rather than the item
  140. // name. TODO: Do need to fix ultimately since the item may be renamed before being passed on to an NPC.
  141. // Assert.That(attSo.Name, Is.EqualTo(attName));
  142. Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
  143. Assert.That(attSo.IsAttachment);
  144. Assert.That(attSo.UsesPhysics, Is.False);
  145. Assert.That(attSo.IsTemporary, Is.False);
  146. Assert.That(attSo.OwnerID, Is.EqualTo(npc.UUID));
  147. }
  148. [Test]
  149. public void TestLoadAppearance()
  150. {
  151. TestHelpers.InMethod();
  152. // log4net.Config.XmlConfigurator.Configure();
  153. UUID userId = TestHelpers.ParseTail(0x1);
  154. UserAccountHelpers.CreateUserWithInventory(m_scene, userId);
  155. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  156. UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
  157. // Now add the attachment to the original avatar and use that to load a new appearance
  158. // TODO: Could also run tests loading from a notecard though this isn't much different for our purposes here
  159. UUID attItemId = TestHelpers.ParseTail(0x2);
  160. UUID attAssetId = TestHelpers.ParseTail(0x3);
  161. string attName = "att";
  162. UserInventoryHelpers.CreateInventoryItem(m_scene, attName, attItemId, attAssetId, sp.UUID, InventoryType.Object);
  163. m_attMod.RezSingleAttachmentFromInventory(sp, attItemId, (uint)AttachmentPoint.Chest);
  164. m_npcMod.SetNPCAppearance(npcId, sp.Appearance, m_scene);
  165. ScenePresence npc = m_scene.GetScenePresence(npcId);
  166. // Check scene presence status
  167. Assert.That(npc.HasAttachments(), Is.True);
  168. List<SceneObjectGroup> attachments = npc.GetAttachments();
  169. Assert.That(attachments.Count, Is.EqualTo(1));
  170. SceneObjectGroup attSo = attachments[0];
  171. // Just for now, we won't test the name since this is (wrongly) the asset part name rather than the item
  172. // name. TODO: Do need to fix ultimately since the item may be renamed before being passed on to an NPC.
  173. // Assert.That(attSo.Name, Is.EqualTo(attName));
  174. Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
  175. Assert.That(attSo.IsAttachment);
  176. Assert.That(attSo.UsesPhysics, Is.False);
  177. Assert.That(attSo.IsTemporary, Is.False);
  178. Assert.That(attSo.OwnerID, Is.EqualTo(npc.UUID));
  179. }
  180. [Test]
  181. public void TestMove()
  182. {
  183. TestHelpers.InMethod();
  184. // log4net.Config.XmlConfigurator.Configure();
  185. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  186. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  187. Vector3 startPos = new Vector3(128, 128, 30);
  188. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  189. ScenePresence npc = m_scene.GetScenePresence(npcId);
  190. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  191. // For now, we'll make the scene presence fly to simplify this test, but this needs to change.
  192. npc.Flying = true;
  193. m_scene.Update(1);
  194. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  195. Vector3 targetPos = startPos + new Vector3(0, 10, 0);
  196. m_npcMod.MoveToTarget(npc.UUID, m_scene, targetPos, false, false);
  197. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  198. //Assert.That(npc.Rotation, Is.EqualTo(new Quaternion(0, 0, 0.7071068f, 0.7071068f)));
  199. Assert.That(
  200. npc.Rotation, new QuaternionToleranceConstraint(new Quaternion(0, 0, 0.7071068f, 0.7071068f), 0.000001));
  201. m_scene.Update(1);
  202. // We should really check the exact figure.
  203. Assert.That(npc.AbsolutePosition.X, Is.EqualTo(startPos.X));
  204. Assert.That(npc.AbsolutePosition.Y, Is.GreaterThan(startPos.Y));
  205. Assert.That(npc.AbsolutePosition.Z, Is.EqualTo(startPos.Z));
  206. Assert.That(npc.AbsolutePosition.Z, Is.LessThan(targetPos.X));
  207. m_scene.Update(10);
  208. double distanceToTarget = Util.GetDistanceTo(npc.AbsolutePosition, targetPos);
  209. Assert.That(distanceToTarget, Is.LessThan(1), "NPC not within 1 unit of target position on first move");
  210. Assert.That(npc.AbsolutePosition, Is.EqualTo(targetPos));
  211. Assert.That(npc.AgentControlFlags, Is.EqualTo((uint)AgentManager.ControlFlags.NONE));
  212. // Try a second movement
  213. startPos = npc.AbsolutePosition;
  214. targetPos = startPos + new Vector3(10, 0, 0);
  215. m_npcMod.MoveToTarget(npc.UUID, m_scene, targetPos, false, false);
  216. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  217. // Assert.That(npc.Rotation, Is.EqualTo(new Quaternion(0, 0, 0, 1)));
  218. Assert.That(
  219. npc.Rotation, new QuaternionToleranceConstraint(new Quaternion(0, 0, 0, 1), 0.000001));
  220. m_scene.Update(1);
  221. // We should really check the exact figure.
  222. Assert.That(npc.AbsolutePosition.X, Is.GreaterThan(startPos.X));
  223. Assert.That(npc.AbsolutePosition.X, Is.LessThan(targetPos.X));
  224. Assert.That(npc.AbsolutePosition.Y, Is.EqualTo(startPos.Y));
  225. Assert.That(npc.AbsolutePosition.Z, Is.EqualTo(startPos.Z));
  226. m_scene.Update(10);
  227. distanceToTarget = Util.GetDistanceTo(npc.AbsolutePosition, targetPos);
  228. Assert.That(distanceToTarget, Is.LessThan(1), "NPC not within 1 unit of target position on second move");
  229. Assert.That(npc.AbsolutePosition, Is.EqualTo(targetPos));
  230. }
  231. [Test]
  232. public void TestSitAndStandWithSitTarget()
  233. {
  234. TestHelpers.InMethod();
  235. // log4net.Config.XmlConfigurator.Configure();
  236. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  237. Vector3 startPos = new Vector3(128, 128, 30);
  238. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  239. ScenePresence npc = m_scene.GetScenePresence(npcId);
  240. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  241. part.SitTargetPosition = new Vector3(0, 0, 1);
  242. m_npcMod.Sit(npc.UUID, part.UUID, m_scene);
  243. Assert.That(part.SitTargetAvatar, Is.EqualTo(npcId));
  244. Assert.That(npc.ParentID, Is.EqualTo(part.LocalId));
  245. Assert.That(
  246. npc.AbsolutePosition,
  247. Is.EqualTo(part.AbsolutePosition + part.SitTargetPosition + ScenePresence.SIT_TARGET_ADJUSTMENT));
  248. m_npcMod.Stand(npc.UUID, m_scene);
  249. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  250. Assert.That(npc.ParentID, Is.EqualTo(0));
  251. }
  252. [Test]
  253. public void TestSitAndStandWithNoSitTarget()
  254. {
  255. TestHelpers.InMethod();
  256. // log4net.Config.XmlConfigurator.Configure();
  257. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  258. // FIXME: To get this to work for now, we are going to place the npc right next to the target so that
  259. // the autopilot doesn't trigger
  260. Vector3 startPos = new Vector3(1, 1, 1);
  261. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  262. ScenePresence npc = m_scene.GetScenePresence(npcId);
  263. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  264. m_npcMod.Sit(npc.UUID, part.UUID, m_scene);
  265. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  266. Assert.That(npc.ParentID, Is.EqualTo(part.LocalId));
  267. // FIXME: This is different for live avatars - z position is adjusted. This is half the height of the
  268. // default avatar.
  269. // Curiously, Vector3.ToString() will not display the last two places of the float. For example,
  270. // printing out npc.AbsolutePosition will give <0, 0, 0.8454993> not <0, 0, 0.845499337>
  271. Assert.That(
  272. npc.AbsolutePosition,
  273. Is.EqualTo(part.AbsolutePosition + new Vector3(0, 0, 0.845499337f)));
  274. m_npcMod.Stand(npc.UUID, m_scene);
  275. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  276. Assert.That(npc.ParentID, Is.EqualTo(0));
  277. }
  278. }
  279. }