NPCModuleTests.cs 16 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. /*
  101. m_afMod.SetAppearance(sp, originalTe, null);
  102. UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
  103. ScenePresence npc = m_scene.GetScenePresence(npcId);
  104. Assert.That(npc, Is.Not.Null);
  105. Assert.That(npc.Appearance.Texture.FaceTextures[8].TextureID, Is.EqualTo(originalFace8TextureId));
  106. Assert.That(m_umMod.GetUserName(npc.UUID), Is.EqualTo(string.Format("{0} {1}", npc.Firstname, npc.Lastname)));
  107. IClientAPI client;
  108. Assert.That(m_scene.TryGetClient(npcId, out client), Is.True);
  109. // Have to account for both SP and NPC.
  110. Assert.That(m_scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(2));
  111. */
  112. }
  113. [Test]
  114. public void TestRemove()
  115. {
  116. TestHelpers.InMethod();
  117. // log4net.Config.XmlConfigurator.Configure();
  118. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  119. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  120. Vector3 startPos = new Vector3(128, 128, 30);
  121. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  122. m_npcMod.DeleteNPC(npcId, m_scene);
  123. ScenePresence deletedNpc = m_scene.GetScenePresence(npcId);
  124. Assert.That(deletedNpc, Is.Null);
  125. IClientAPI client;
  126. Assert.That(m_scene.TryGetClient(npcId, out client), Is.False);
  127. // Have to account for SP still present.
  128. Assert.That(m_scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(1));
  129. }
  130. [Test]
  131. public void TestCreateWithAttachments()
  132. {
  133. TestHelpers.InMethod();
  134. // TestHelpers.EnableLogging();
  135. UUID userId = TestHelpers.ParseTail(0x1);
  136. UserAccountHelpers.CreateUserWithInventory(m_scene, userId);
  137. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  138. UUID attItemId = TestHelpers.ParseTail(0x2);
  139. UUID attAssetId = TestHelpers.ParseTail(0x3);
  140. string attName = "att";
  141. UserInventoryHelpers.CreateInventoryItem(m_scene, attName, attItemId, attAssetId, sp.UUID, InventoryType.Object);
  142. m_attMod.RezSingleAttachmentFromInventory(sp, attItemId, (uint)AttachmentPoint.Chest);
  143. UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
  144. ScenePresence npc = m_scene.GetScenePresence(npcId);
  145. // Check scene presence status
  146. Assert.That(npc.HasAttachments(), Is.True);
  147. List<SceneObjectGroup> attachments = npc.GetAttachments();
  148. Assert.That(attachments.Count, Is.EqualTo(1));
  149. SceneObjectGroup attSo = attachments[0];
  150. // Just for now, we won't test the name since this is (wrongly) the asset part name rather than the item
  151. // name. TODO: Do need to fix ultimately since the item may be renamed before being passed on to an NPC.
  152. // Assert.That(attSo.Name, Is.EqualTo(attName));
  153. Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
  154. Assert.That(attSo.IsAttachment);
  155. Assert.That(attSo.UsesPhysics, Is.False);
  156. Assert.That(attSo.IsTemporary, Is.False);
  157. Assert.That(attSo.OwnerID, Is.EqualTo(npc.UUID));
  158. }
  159. [Test]
  160. public void TestLoadAppearance()
  161. {
  162. TestHelpers.InMethod();
  163. // log4net.Config.XmlConfigurator.Configure();
  164. UUID userId = TestHelpers.ParseTail(0x1);
  165. UserAccountHelpers.CreateUserWithInventory(m_scene, userId);
  166. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  167. UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
  168. // Now add the attachment to the original avatar and use that to load a new appearance
  169. // TODO: Could also run tests loading from a notecard though this isn't much different for our purposes here
  170. UUID attItemId = TestHelpers.ParseTail(0x2);
  171. UUID attAssetId = TestHelpers.ParseTail(0x3);
  172. string attName = "att";
  173. UserInventoryHelpers.CreateInventoryItem(m_scene, attName, attItemId, attAssetId, sp.UUID, InventoryType.Object);
  174. m_attMod.RezSingleAttachmentFromInventory(sp, attItemId, (uint)AttachmentPoint.Chest);
  175. m_npcMod.SetNPCAppearance(npcId, sp.Appearance, m_scene);
  176. ScenePresence npc = m_scene.GetScenePresence(npcId);
  177. // Check scene presence status
  178. Assert.That(npc.HasAttachments(), Is.True);
  179. List<SceneObjectGroup> attachments = npc.GetAttachments();
  180. Assert.That(attachments.Count, Is.EqualTo(1));
  181. SceneObjectGroup attSo = attachments[0];
  182. // Just for now, we won't test the name since this is (wrongly) the asset part name rather than the item
  183. // name. TODO: Do need to fix ultimately since the item may be renamed before being passed on to an NPC.
  184. // Assert.That(attSo.Name, Is.EqualTo(attName));
  185. Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
  186. Assert.That(attSo.IsAttachment);
  187. Assert.That(attSo.UsesPhysics, Is.False);
  188. Assert.That(attSo.IsTemporary, Is.False);
  189. Assert.That(attSo.OwnerID, Is.EqualTo(npc.UUID));
  190. }
  191. [Test]
  192. public void TestMove()
  193. {
  194. TestHelpers.InMethod();
  195. // log4net.Config.XmlConfigurator.Configure();
  196. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  197. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  198. Vector3 startPos = new Vector3(128, 128, 30);
  199. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  200. ScenePresence npc = m_scene.GetScenePresence(npcId);
  201. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  202. // For now, we'll make the scene presence fly to simplify this test, but this needs to change.
  203. npc.Flying = true;
  204. m_scene.Update(1);
  205. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  206. Vector3 targetPos = startPos + new Vector3(0, 10, 0);
  207. m_npcMod.MoveToTarget(npc.UUID, m_scene, targetPos, false, false, false);
  208. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  209. //Assert.That(npc.Rotation, Is.EqualTo(new Quaternion(0, 0, 0.7071068f, 0.7071068f)));
  210. Assert.That(
  211. npc.Rotation, new QuaternionToleranceConstraint(new Quaternion(0, 0, 0.7071068f, 0.7071068f), 0.000001));
  212. m_scene.Update(1);
  213. // We should really check the exact figure.
  214. Assert.That(npc.AbsolutePosition.X, Is.EqualTo(startPos.X));
  215. Assert.That(npc.AbsolutePosition.Y, Is.GreaterThan(startPos.Y));
  216. Assert.That(npc.AbsolutePosition.Z, Is.EqualTo(startPos.Z));
  217. Assert.That(npc.AbsolutePosition.Z, Is.LessThan(targetPos.X));
  218. m_scene.Update(10);
  219. double distanceToTarget = Util.GetDistanceTo(npc.AbsolutePosition, targetPos);
  220. Assert.That(distanceToTarget, Is.LessThan(1), "NPC not within 1 unit of target position on first move");
  221. Assert.That(npc.AbsolutePosition, Is.EqualTo(targetPos));
  222. Assert.That(npc.AgentControlFlags, Is.EqualTo((uint)AgentManager.ControlFlags.NONE));
  223. // Try a second movement
  224. startPos = npc.AbsolutePosition;
  225. targetPos = startPos + new Vector3(10, 0, 0);
  226. m_npcMod.MoveToTarget(npc.UUID, m_scene, targetPos, false, false, false);
  227. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  228. // Assert.That(npc.Rotation, Is.EqualTo(new Quaternion(0, 0, 0, 1)));
  229. Assert.That(
  230. npc.Rotation, new QuaternionToleranceConstraint(new Quaternion(0, 0, 0, 1), 0.000001));
  231. m_scene.Update(1);
  232. // We should really check the exact figure.
  233. Assert.That(npc.AbsolutePosition.X, Is.GreaterThan(startPos.X));
  234. Assert.That(npc.AbsolutePosition.X, Is.LessThan(targetPos.X));
  235. Assert.That(npc.AbsolutePosition.Y, Is.EqualTo(startPos.Y));
  236. Assert.That(npc.AbsolutePosition.Z, Is.EqualTo(startPos.Z));
  237. m_scene.Update(10);
  238. distanceToTarget = Util.GetDistanceTo(npc.AbsolutePosition, targetPos);
  239. Assert.That(distanceToTarget, Is.LessThan(1), "NPC not within 1 unit of target position on second move");
  240. Assert.That(npc.AbsolutePosition, Is.EqualTo(targetPos));
  241. }
  242. [Test]
  243. public void TestSitAndStandWithSitTarget()
  244. {
  245. TestHelpers.InMethod();
  246. // log4net.Config.XmlConfigurator.Configure();
  247. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  248. Vector3 startPos = new Vector3(128, 128, 30);
  249. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  250. ScenePresence npc = m_scene.GetScenePresence(npcId);
  251. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  252. part.SitTargetPosition = new Vector3(0, 0, 1);
  253. m_npcMod.Sit(npc.UUID, part.UUID, m_scene);
  254. Assert.That(part.SitTargetAvatar, Is.EqualTo(npcId));
  255. Assert.That(npc.ParentID, Is.EqualTo(part.LocalId));
  256. // Assert.That(
  257. // npc.AbsolutePosition,
  258. // Is.EqualTo(part.AbsolutePosition + part.SitTargetPosition + ScenePresence.SIT_TARGET_ADJUSTMENT));
  259. m_npcMod.Stand(npc.UUID, m_scene);
  260. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  261. Assert.That(npc.ParentID, Is.EqualTo(0));
  262. }
  263. [Test]
  264. public void TestSitAndStandWithNoSitTarget()
  265. {
  266. TestHelpers.InMethod();
  267. // TestHelpers.EnableLogging();
  268. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  269. // FIXME: To get this to work for now, we are going to place the npc right next to the target so that
  270. // the autopilot doesn't trigger
  271. Vector3 startPos = new Vector3(1, 1, 1);
  272. UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
  273. ScenePresence npc = m_scene.GetScenePresence(npcId);
  274. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  275. m_npcMod.Sit(npc.UUID, part.UUID, m_scene);
  276. Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
  277. Assert.That(npc.ParentID, Is.EqualTo(part.LocalId));
  278. // We should really be using the NPC size but this would mean preserving the physics actor since it is
  279. // removed on sit.
  280. Assert.That(
  281. npc.AbsolutePosition,
  282. Is.EqualTo(part.AbsolutePosition + new Vector3(0, 0, sp.PhysicsActor.Size.Z / 2)));
  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. }