NPCModuleTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 scene;
  52. private AvatarFactoryModule afm;
  53. private UserManagementModule umm;
  54. private AttachmentsModule am;
  55. [TestFixtureSetUp]
  56. public void FixtureInit()
  57. {
  58. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  59. Util.FireAndForgetMethod = FireAndForgetMethod.None;
  60. }
  61. [TestFixtureTearDown]
  62. public void TearDown()
  63. {
  64. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  65. // threads. Possibly, later tests should be rewritten not to worry about such things.
  66. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  67. }
  68. [SetUp]
  69. public void Init()
  70. {
  71. IConfigSource config = new IniConfigSource();
  72. config.AddConfig("NPC");
  73. config.Configs["NPC"].Set("Enabled", "true");
  74. config.AddConfig("Modules");
  75. config.Configs["Modules"].Set("InventoryAccessModule", "BasicInventoryAccessModule");
  76. afm = new AvatarFactoryModule();
  77. umm = new UserManagementModule();
  78. am = new AttachmentsModule();
  79. scene = SceneHelpers.SetupScene();
  80. SceneHelpers.SetupSceneModules(scene, config, afm, umm, am, new BasicInventoryAccessModule(), new NPCModule());
  81. }
  82. [Test]
  83. public void TestCreate()
  84. {
  85. TestHelpers.InMethod();
  86. // log4net.Config.XmlConfigurator.Configure();
  87. ScenePresence sp = SceneHelpers.AddScenePresence(scene, TestHelpers.ParseTail(0x1));
  88. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  89. // 8 is the index of the first baked texture in AvatarAppearance
  90. UUID originalFace8TextureId = TestHelpers.ParseTail(0x10);
  91. Primitive.TextureEntry originalTe = new Primitive.TextureEntry(UUID.Zero);
  92. Primitive.TextureEntryFace originalTef = originalTe.CreateFace(8);
  93. originalTef.TextureID = originalFace8TextureId;
  94. // We also need to add the texture to the asset service, otherwise the AvatarFactoryModule will tell
  95. // ScenePresence.SendInitialData() to reset our entire appearance.
  96. scene.AssetService.Store(AssetHelpers.CreateAsset(originalFace8TextureId));
  97. afm.SetAppearanceFromClient(sp.ControllingClient, originalTe, null);
  98. INPCModule npcModule = scene.RequestModuleInterface<INPCModule>();
  99. UUID npcId = npcModule.CreateNPC("John", "Smith", new Vector3(128, 128, 30), scene, sp.Appearance);
  100. ScenePresence npc = scene.GetScenePresence(npcId);
  101. Assert.That(npc, Is.Not.Null);
  102. Assert.That(npc.Appearance.Texture.FaceTextures[8].TextureID, Is.EqualTo(originalFace8TextureId));
  103. Assert.That(umm.GetUserName(npc.UUID), Is.EqualTo(string.Format("{0} {1}", npc.Firstname, npc.Lastname)));
  104. }
  105. [Test]
  106. public void TestAttachments()
  107. {
  108. TestHelpers.InMethod();
  109. // log4net.Config.XmlConfigurator.Configure();
  110. UUID userId = TestHelpers.ParseTail(0x1);
  111. UserAccountHelpers.CreateUserWithInventory(scene, userId);
  112. ScenePresence sp = SceneHelpers.AddScenePresence(scene, userId);
  113. UUID attItemId = TestHelpers.ParseTail(0x2);
  114. UUID attAssetId = TestHelpers.ParseTail(0x3);
  115. string attName = "att";
  116. UserInventoryHelpers.CreateInventoryItem(
  117. scene, attName, attItemId, attAssetId, sp.UUID, InventoryType.Object);
  118. am.RezSingleAttachmentFromInventory(
  119. sp.ControllingClient, attItemId, (uint)AttachmentPoint.Chest);
  120. INPCModule npcModule = scene.RequestModuleInterface<INPCModule>();
  121. UUID npcId = npcModule.CreateNPC("John", "Smith", new Vector3(128, 128, 30), scene, sp.Appearance);
  122. ScenePresence npc = scene.GetScenePresence(npcId);
  123. // Check scene presence status
  124. Assert.That(npc.HasAttachments(), Is.True);
  125. List<SceneObjectGroup> attachments = npc.GetAttachments();
  126. Assert.That(attachments.Count, Is.EqualTo(1));
  127. SceneObjectGroup attSo = attachments[0];
  128. // Just for now, we won't test the name since this is (wrongly) the asset part name rather than the item
  129. // name. TODO: Do need to fix ultimately since the item may be renamed before being passed on to an NPC.
  130. // Assert.That(attSo.Name, Is.EqualTo(attName));
  131. Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
  132. Assert.That(attSo.IsAttachment);
  133. Assert.That(attSo.UsesPhysics, Is.False);
  134. Assert.That(attSo.IsTemporary, Is.False);
  135. Assert.That(attSo.OwnerID, Is.EqualTo(npc.UUID));
  136. }
  137. [Test]
  138. public void TestMove()
  139. {
  140. TestHelpers.InMethod();
  141. // log4net.Config.XmlConfigurator.Configure();
  142. ScenePresence sp = SceneHelpers.AddScenePresence(scene, TestHelpers.ParseTail(0x1));
  143. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  144. Vector3 startPos = new Vector3(128, 128, 30);
  145. INPCModule npcModule = scene.RequestModuleInterface<INPCModule>();
  146. UUID npcId = npcModule.CreateNPC("John", "Smith", startPos, scene, sp.Appearance);
  147. ScenePresence npc = scene.GetScenePresence(npcId);
  148. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  149. // For now, we'll make the scene presence fly to simplify this test, but this needs to change.
  150. npc.PhysicsActor.Flying = true;
  151. scene.Update();
  152. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  153. Vector3 targetPos = startPos + new Vector3(0, 0, 10);
  154. npcModule.MoveToTarget(npc.UUID, scene, targetPos, false, false);
  155. Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
  156. scene.Update();
  157. // We should really check the exact figure.
  158. Assert.That(npc.AbsolutePosition.X, Is.EqualTo(startPos.X));
  159. Assert.That(npc.AbsolutePosition.Y, Is.EqualTo(startPos.Y));
  160. Assert.That(npc.AbsolutePosition.Z, Is.GreaterThan(startPos.Z));
  161. Assert.That(npc.AbsolutePosition.Z, Is.LessThan(targetPos.Z));
  162. for (int i = 0; i < 10; i++)
  163. scene.Update();
  164. double distanceToTarget = Util.GetDistanceTo(npc.AbsolutePosition, targetPos);
  165. Assert.That(distanceToTarget, Is.LessThan(1), "NPC not within 1 unit of target position on first move");
  166. Assert.That(npc.AbsolutePosition, Is.EqualTo(targetPos));
  167. Assert.That(npc.AgentControlFlags, Is.EqualTo((uint)AgentManager.ControlFlags.NONE));
  168. // Try a second movement
  169. startPos = npc.AbsolutePosition;
  170. targetPos = startPos + new Vector3(10, 0, 0);
  171. npcModule.MoveToTarget(npc.UUID, scene, targetPos, false, false);
  172. scene.Update();
  173. // We should really check the exact figure.
  174. Assert.That(npc.AbsolutePosition.X, Is.GreaterThan(startPos.X));
  175. Assert.That(npc.AbsolutePosition.X, Is.LessThan(targetPos.X));
  176. Assert.That(npc.AbsolutePosition.Y, Is.EqualTo(startPos.Y));
  177. Assert.That(npc.AbsolutePosition.Z, Is.EqualTo(startPos.Z));
  178. for (int i = 0; i < 10; i++)
  179. scene.Update();
  180. distanceToTarget = Util.GetDistanceTo(npc.AbsolutePosition, targetPos);
  181. Assert.That(distanceToTarget, Is.LessThan(1), "NPC not within 1 unit of target position on second move");
  182. Assert.That(npc.AbsolutePosition, Is.EqualTo(targetPos));
  183. }
  184. }
  185. }