NPCModuleTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 : OpenSimTestCase
  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. base.SetUp();
  73. IConfigSource config = new IniConfigSource();
  74. config.AddConfig("NPC");
  75. config.Configs["NPC"].Set("Enabled", "true");
  76. config.AddConfig("Modules");
  77. config.Configs["Modules"].Set("InventoryAccessModule", "BasicInventoryAccessModule");
  78. m_afMod = new AvatarFactoryModule();
  79. m_umMod = new UserManagementModule();
  80. m_attMod = new AttachmentsModule();
  81. m_npcMod = new NPCModule();
  82. m_scene = new SceneHelpers().SetupScene();
  83. SceneHelpers.SetupSceneModules(m_scene, config, m_afMod, m_umMod, m_attMod, m_npcMod, new BasicInventoryAccessModule());
  84. }
  85. [Test]
  86. public void TestCreate()
  87. {
  88. TestHelpers.InMethod();
  89. // log4net.Config.XmlConfigurator.Configure();
  90. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  91. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  92. // 8 is the index of the first baked texture in AvatarAppearance
  93. UUID originalFace8TextureId = TestHelpers.ParseTail(0x10);
  94. Primitive.TextureEntry originalTe = new Primitive.TextureEntry(UUID.Zero);
  95. Primitive.TextureEntryFace originalTef = originalTe.CreateFace(8);
  96. originalTef.TextureID = originalFace8TextureId;
  97. // We also need to add the texture to the asset service, otherwise the AvatarFactoryModule will tell
  98. // ScenePresence.SendInitialData() to reset our entire appearance.
  99. m_scene.AssetService.Store(AssetHelpers.CreateNotecardAsset(originalFace8TextureId));
  100. m_afMod.SetAppearance(sp, originalTe, null);
  101. UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
  102. ScenePresence npc = m_scene.GetScenePresence(npcId);
  103. Assert.That(npc, Is.Not.Null);
  104. Assert.That(npc.Appearance.Texture.FaceTextures[8].TextureID, Is.EqualTo(originalFace8TextureId));
  105. Assert.That(m_umMod.GetUserName(npc.UUID), Is.EqualTo(string.Format("{0} {1}", npc.Firstname, npc.Lastname)));
  106. IClientAPI client;
  107. Assert.That(m_scene.TryGetClient(npcId, out client), Is.True);
  108. // Have to account for both SP and NPC.
  109. Assert.That(m_scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(2));
  110. }
  111. [Test]
  112. public void TestRemove()
  113. {
  114. TestHelpers.InMethod();
  115. // log4net.Config.XmlConfigurator.Configure();
  116. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  117. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  118. Vector3 startPos = new Vector3(128, 128, 30);
  119. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  120. m_npcMod.DeleteNPC(npcId, m_scene);
  121. ScenePresence deletedNpc = m_scene.GetScenePresence(npcId);
  122. Assert.That(deletedNpc, Is.Null);
  123. IClientAPI client;
  124. Assert.That(m_scene.TryGetClient(npcId, out client), Is.False);
  125. // Have to account for SP still present.
  126. Assert.That(m_scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(1));
  127. }
  128. [Test]
  129. public void TestCreateWithAttachments()
  130. {
  131. TestHelpers.InMethod();
  132. // log4net.Config.XmlConfigurator.Configure();
  133. UUID userId = TestHelpers.ParseTail(0x1);
  134. UserAccountHelpers.CreateUserWithInventory(m_scene, userId);
  135. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  136. UUID attItemId = TestHelpers.ParseTail(0x2);
  137. UUID attAssetId = TestHelpers.ParseTail(0x3);
  138. string attName = "att";
  139. UserInventoryHelpers.CreateInventoryItem(m_scene, attName, attItemId, attAssetId, sp.UUID, InventoryType.Object);
  140. m_attMod.RezSingleAttachmentFromInventory(sp, attItemId, (uint)AttachmentPoint.Chest);
  141. UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
  142. ScenePresence npc = m_scene.GetScenePresence(npcId);
  143. // Check scene presence status
  144. Assert.That(npc.HasAttachments(), Is.True);
  145. List<SceneObjectGroup> attachments = npc.GetAttachments();
  146. Assert.That(attachments.Count, Is.EqualTo(1));
  147. SceneObjectGroup attSo = attachments[0];
  148. // Just for now, we won't test the name since this is (wrongly) the asset part name rather than the item
  149. // name. TODO: Do need to fix ultimately since the item may be renamed before being passed on to an NPC.
  150. // Assert.That(attSo.Name, Is.EqualTo(attName));
  151. Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
  152. Assert.That(attSo.IsAttachment);
  153. Assert.That(attSo.UsesPhysics, Is.False);
  154. Assert.That(attSo.IsTemporary, Is.False);
  155. Assert.That(attSo.OwnerID, Is.EqualTo(npc.UUID));
  156. }
  157. [Test]
  158. public void TestLoadAppearance()
  159. {
  160. TestHelpers.InMethod();
  161. // log4net.Config.XmlConfigurator.Configure();
  162. UUID userId = TestHelpers.ParseTail(0x1);
  163. UserAccountHelpers.CreateUserWithInventory(m_scene, userId);
  164. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  165. UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
  166. // Now add the attachment to the original avatar and use that to load a new appearance
  167. // TODO: Could also run tests loading from a notecard though this isn't much different for our purposes here
  168. UUID attItemId = TestHelpers.ParseTail(0x2);
  169. UUID attAssetId = TestHelpers.ParseTail(0x3);
  170. string attName = "att";
  171. UserInventoryHelpers.CreateInventoryItem(m_scene, attName, attItemId, attAssetId, sp.UUID, InventoryType.Object);
  172. m_attMod.RezSingleAttachmentFromInventory(sp, attItemId, (uint)AttachmentPoint.Chest);
  173. m_npcMod.SetNPCAppearance(npcId, sp.Appearance, m_scene);
  174. ScenePresence npc = m_scene.GetScenePresence(npcId);
  175. // Check scene presence status
  176. Assert.That(npc.HasAttachments(), Is.True);
  177. List<SceneObjectGroup> attachments = npc.GetAttachments();
  178. Assert.That(attachments.Count, Is.EqualTo(1));
  179. SceneObjectGroup attSo = attachments[0];
  180. // Just for now, we won't test the name since this is (wrongly) the asset part name rather than the item
  181. // name. TODO: Do need to fix ultimately since the item may be renamed before being passed on to an NPC.
  182. // Assert.That(attSo.Name, Is.EqualTo(attName));
  183. Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
  184. Assert.That(attSo.IsAttachment);
  185. Assert.That(attSo.UsesPhysics, Is.False);
  186. Assert.That(attSo.IsTemporary, Is.False);
  187. Assert.That(attSo.OwnerID, Is.EqualTo(npc.UUID));
  188. }
  189. [Test]
  190. public void TestMove()
  191. {
  192. TestHelpers.InMethod();
  193. // log4net.Config.XmlConfigurator.Configure();
  194. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  195. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  196. Vector3 startPos = new Vector3(128, 128, 30);
  197. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  198. ScenePresence npc = m_scene.GetScenePresence(npcId);
  199. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  200. // For now, we'll make the scene presence fly to simplify this test, but this needs to change.
  201. npc.Flying = true;
  202. m_scene.Update(1);
  203. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  204. Vector3 targetPos = startPos + new Vector3(0, 10, 0);
  205. m_npcMod.MoveToTarget(npc.UUID, m_scene, targetPos, false, false, false);
  206. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  207. //Assert.That(npc.Rotation, Is.EqualTo(new Quaternion(0, 0, 0.7071068f, 0.7071068f)));
  208. Assert.That(
  209. npc.Rotation, new QuaternionToleranceConstraint(new Quaternion(0, 0, 0.7071068f, 0.7071068f), 0.000001));
  210. m_scene.Update(1);
  211. // We should really check the exact figure.
  212. Assert.That(npc.AbsolutePosition.X, Is.EqualTo(startPos.X));
  213. Assert.That(npc.AbsolutePosition.Y, Is.GreaterThan(startPos.Y));
  214. Assert.That(npc.AbsolutePosition.Z, Is.EqualTo(startPos.Z));
  215. Assert.That(npc.AbsolutePosition.Z, Is.LessThan(targetPos.X));
  216. m_scene.Update(10);
  217. double distanceToTarget = Util.GetDistanceTo(npc.AbsolutePosition, targetPos);
  218. Assert.That(distanceToTarget, Is.LessThan(1), "NPC not within 1 unit of target position on first move");
  219. Assert.That(npc.AbsolutePosition, Is.EqualTo(targetPos));
  220. Assert.That(npc.AgentControlFlags, Is.EqualTo((uint)AgentManager.ControlFlags.NONE));
  221. // Try a second movement
  222. startPos = npc.AbsolutePosition;
  223. targetPos = startPos + new Vector3(10, 0, 0);
  224. m_npcMod.MoveToTarget(npc.UUID, m_scene, targetPos, false, false, false);
  225. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  226. // Assert.That(npc.Rotation, Is.EqualTo(new Quaternion(0, 0, 0, 1)));
  227. Assert.That(
  228. npc.Rotation, new QuaternionToleranceConstraint(new Quaternion(0, 0, 0, 1), 0.000001));
  229. m_scene.Update(1);
  230. // We should really check the exact figure.
  231. Assert.That(npc.AbsolutePosition.X, Is.GreaterThan(startPos.X));
  232. Assert.That(npc.AbsolutePosition.X, Is.LessThan(targetPos.X));
  233. Assert.That(npc.AbsolutePosition.Y, Is.EqualTo(startPos.Y));
  234. Assert.That(npc.AbsolutePosition.Z, Is.EqualTo(startPos.Z));
  235. m_scene.Update(10);
  236. distanceToTarget = Util.GetDistanceTo(npc.AbsolutePosition, targetPos);
  237. Assert.That(distanceToTarget, Is.LessThan(1), "NPC not within 1 unit of target position on second move");
  238. Assert.That(npc.AbsolutePosition, Is.EqualTo(targetPos));
  239. }
  240. [Test]
  241. public void TestSitAndStandWithSitTarget()
  242. {
  243. TestHelpers.InMethod();
  244. // log4net.Config.XmlConfigurator.Configure();
  245. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  246. Vector3 startPos = new Vector3(128, 128, 30);
  247. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  248. ScenePresence npc = m_scene.GetScenePresence(npcId);
  249. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  250. part.SitTargetPosition = new Vector3(0, 0, 1);
  251. m_npcMod.Sit(npc.UUID, part.UUID, m_scene);
  252. Assert.That(part.SitTargetAvatar, Is.EqualTo(npcId));
  253. Assert.That(npc.ParentID, Is.EqualTo(part.LocalId));
  254. Assert.That(
  255. npc.AbsolutePosition,
  256. Is.EqualTo(part.AbsolutePosition + part.SitTargetPosition + ScenePresence.SIT_TARGET_ADJUSTMENT));
  257. m_npcMod.Stand(npc.UUID, m_scene);
  258. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  259. Assert.That(npc.ParentID, Is.EqualTo(0));
  260. }
  261. [Test]
  262. public void TestSitAndStandWithNoSitTarget()
  263. {
  264. TestHelpers.InMethod();
  265. // log4net.Config.XmlConfigurator.Configure();
  266. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  267. // FIXME: To get this to work for now, we are going to place the npc right next to the target so that
  268. // the autopilot doesn't trigger
  269. Vector3 startPos = new Vector3(1, 1, 1);
  270. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  271. ScenePresence npc = m_scene.GetScenePresence(npcId);
  272. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  273. m_npcMod.Sit(npc.UUID, part.UUID, m_scene);
  274. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  275. Assert.That(npc.ParentID, Is.EqualTo(part.LocalId));
  276. // FIXME: This is different for live avatars - z position is adjusted. This is half the height of the
  277. // default avatar.
  278. // Curiously, Vector3.ToString() will not display the last two places of the float. For example,
  279. // printing out npc.AbsolutePosition will give <0, 0, 0.8454993> not <0, 0, 0.845499337>
  280. Assert.That(
  281. npc.AbsolutePosition,
  282. Is.EqualTo(part.AbsolutePosition + new Vector3(0, 0, 0.845499337f)));
  283. m_npcMod.Stand(npc.UUID, m_scene);
  284. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  285. Assert.That(npc.ParentID, Is.EqualTo(0));
  286. }
  287. }
  288. }