opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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  
28 using System;
29 using System.Collections.Generic;
30 using System.Reflection;
31 using log4net;
32 using Nini.Config;
33 using NUnit.Framework;
34 using OpenMetaverse;
35 using OpenSim.Framework;
36 using OpenSim.Framework.Communications;
37 using OpenSim.Region.CoreModules.Avatar.Attachments;
38 using OpenSim.Region.CoreModules.Avatar.AvatarFactory;
39 using OpenSim.Region.CoreModules.Framework.InventoryAccess;
40 using OpenSim.Region.CoreModules.Framework.UserManagement;
41 using OpenSim.Region.CoreModules.ServiceConnectorsOut.Avatar;
42 using OpenSim.Region.Framework.Interfaces;
43 using OpenSim.Region.Framework.Scenes;
44 using OpenSim.Services.AvatarService;
45 using OpenSim.Tests.Common;
46 using OpenSim.Tests.Common.Mock;
47  
48 namespace OpenSim.Region.OptionalModules.World.NPC.Tests
49 {
50 [TestFixture]
51 public class NPCModuleTests : OpenSimTestCase
52 {
53 private TestScene m_scene;
54 private AvatarFactoryModule m_afMod;
55 private UserManagementModule m_umMod;
56 private AttachmentsModule m_attMod;
57 private NPCModule m_npcMod;
58  
59 [TestFixtureSetUp]
60 public void FixtureInit()
61 {
62 // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
63 Util.FireAndForgetMethod = FireAndForgetMethod.None;
64 }
65  
66 [TestFixtureTearDown]
67 public void TearDown()
68 {
69 // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
70 // threads. Possibly, later tests should be rewritten not to worry about such things.
71 Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
72 }
73  
74 [SetUp]
75 public void Init()
76 {
77 base.SetUp();
78  
79 IConfigSource config = new IniConfigSource();
80 config.AddConfig("NPC");
81 config.Configs["NPC"].Set("Enabled", "true");
82 config.AddConfig("Modules");
83 config.Configs["Modules"].Set("InventoryAccessModule", "BasicInventoryAccessModule");
84  
85 m_afMod = new AvatarFactoryModule();
86 m_umMod = new UserManagementModule();
87 m_attMod = new AttachmentsModule();
88 m_npcMod = new NPCModule();
89  
90 m_scene = new SceneHelpers().SetupScene();
91 SceneHelpers.SetupSceneModules(m_scene, config, m_afMod, m_umMod, m_attMod, m_npcMod, new BasicInventoryAccessModule());
92 }
93  
94 [Test]
95 public void TestCreate()
96 {
97 TestHelpers.InMethod();
98 // log4net.Config.XmlConfigurator.Configure();
99  
100 ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
101 // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
102  
103 // 8 is the index of the first baked texture in AvatarAppearance
104 UUID originalFace8TextureId = TestHelpers.ParseTail(0x10);
105 Primitive.TextureEntry originalTe = new Primitive.TextureEntry(UUID.Zero);
106 Primitive.TextureEntryFace originalTef = originalTe.CreateFace(8);
107 originalTef.TextureID = originalFace8TextureId;
108  
109 // We also need to add the texture to the asset service, otherwise the AvatarFactoryModule will tell
110 // ScenePresence.SendInitialData() to reset our entire appearance.
111 m_scene.AssetService.Store(AssetHelpers.CreateNotecardAsset(originalFace8TextureId));
112  
113 m_afMod.SetAppearance(sp, originalTe, null, null);
114  
115 UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
116  
117 ScenePresence npc = m_scene.GetScenePresence(npcId);
118  
119 Assert.That(npc, Is.Not.Null);
120 Assert.That(npc.Appearance.Texture.FaceTextures[8].TextureID, Is.EqualTo(originalFace8TextureId));
121 Assert.That(m_umMod.GetUserName(npc.UUID), Is.EqualTo(string.Format("{0} {1}", npc.Firstname, npc.Lastname)));
122  
123 IClientAPI client;
124 Assert.That(m_scene.TryGetClient(npcId, out client), Is.True);
125  
126 // Have to account for both SP and NPC.
127 Assert.That(m_scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(2));
128 }
129  
130 [Test]
131 public void TestRemove()
132 {
133 TestHelpers.InMethod();
134 // log4net.Config.XmlConfigurator.Configure();
135  
136 ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
137 // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
138  
139 Vector3 startPos = new Vector3(128, 128, 30);
140 UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
141  
142 m_npcMod.DeleteNPC(npcId, m_scene);
143  
144 ScenePresence deletedNpc = m_scene.GetScenePresence(npcId);
145  
146 Assert.That(deletedNpc, Is.Null);
147 IClientAPI client;
148 Assert.That(m_scene.TryGetClient(npcId, out client), Is.False);
149  
150 // Have to account for SP still present.
151 Assert.That(m_scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(1));
152 }
153  
154 [Test]
155 public void TestCreateWithAttachments()
156 {
157 TestHelpers.InMethod();
158 // TestHelpers.EnableLogging();
159  
160 UUID userId = TestHelpers.ParseTail(0x1);
161 UserAccountHelpers.CreateUserWithInventory(m_scene, userId);
162 ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
163  
164 UUID attItemId = TestHelpers.ParseTail(0x2);
165 UUID attAssetId = TestHelpers.ParseTail(0x3);
166 string attName = "att";
167  
168 UserInventoryHelpers.CreateInventoryItem(m_scene, attName, attItemId, attAssetId, sp.UUID, InventoryType.Object);
169  
170 m_attMod.RezSingleAttachmentFromInventory(sp, attItemId, (uint)AttachmentPoint.Chest);
171  
172 UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
173  
174 ScenePresence npc = m_scene.GetScenePresence(npcId);
175  
176 // Check scene presence status
177 Assert.That(npc.HasAttachments(), Is.True);
178 List<SceneObjectGroup> attachments = npc.GetAttachments();
179 Assert.That(attachments.Count, Is.EqualTo(1));
180 SceneObjectGroup attSo = attachments[0];
181  
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  
186 Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
187 Assert.That(attSo.IsAttachment);
188 Assert.That(attSo.UsesPhysics, Is.False);
189 Assert.That(attSo.IsTemporary, Is.False);
190 Assert.That(attSo.OwnerID, Is.EqualTo(npc.UUID));
191 }
192  
193 [Test]
194 public void TestLoadAppearance()
195 {
196 TestHelpers.InMethod();
197 // log4net.Config.XmlConfigurator.Configure();
198  
199 UUID userId = TestHelpers.ParseTail(0x1);
200 UserAccountHelpers.CreateUserWithInventory(m_scene, userId);
201 ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
202  
203 UUID npcId = m_npcMod.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, sp.Appearance);
204  
205 // Now add the attachment to the original avatar and use that to load a new appearance
206 // TODO: Could also run tests loading from a notecard though this isn't much different for our purposes here
207 UUID attItemId = TestHelpers.ParseTail(0x2);
208 UUID attAssetId = TestHelpers.ParseTail(0x3);
209 string attName = "att";
210  
211 UserInventoryHelpers.CreateInventoryItem(m_scene, attName, attItemId, attAssetId, sp.UUID, InventoryType.Object);
212  
213 m_attMod.RezSingleAttachmentFromInventory(sp, attItemId, (uint)AttachmentPoint.Chest);
214  
215 m_npcMod.SetNPCAppearance(npcId, sp.Appearance, m_scene);
216  
217 ScenePresence npc = m_scene.GetScenePresence(npcId);
218  
219 // Check scene presence status
220 Assert.That(npc.HasAttachments(), Is.True);
221 List<SceneObjectGroup> attachments = npc.GetAttachments();
222 Assert.That(attachments.Count, Is.EqualTo(1));
223 SceneObjectGroup attSo = attachments[0];
224  
225 // Just for now, we won't test the name since this is (wrongly) the asset part name rather than the item
226 // name. TODO: Do need to fix ultimately since the item may be renamed before being passed on to an NPC.
227 // Assert.That(attSo.Name, Is.EqualTo(attName));
228  
229 Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
230 Assert.That(attSo.IsAttachment);
231 Assert.That(attSo.UsesPhysics, Is.False);
232 Assert.That(attSo.IsTemporary, Is.False);
233 Assert.That(attSo.OwnerID, Is.EqualTo(npc.UUID));
234 }
235  
236 [Test]
237 public void TestMove()
238 {
239 TestHelpers.InMethod();
240 // log4net.Config.XmlConfigurator.Configure();
241  
242 ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
243 // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
244  
245 Vector3 startPos = new Vector3(128, 128, 30);
246 UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
247  
248 ScenePresence npc = m_scene.GetScenePresence(npcId);
249 Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
250  
251 // For now, we'll make the scene presence fly to simplify this test, but this needs to change.
252 npc.Flying = true;
253  
254 m_scene.Update(1);
255 Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
256  
257 Vector3 targetPos = startPos + new Vector3(0, 10, 0);
258 m_npcMod.MoveToTarget(npc.UUID, m_scene, targetPos, false, false, false);
259  
260 Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
261 //Assert.That(npc.Rotation, Is.EqualTo(new Quaternion(0, 0, 0.7071068f, 0.7071068f)));
262 Assert.That(
263 npc.Rotation, new QuaternionToleranceConstraint(new Quaternion(0, 0, 0.7071068f, 0.7071068f), 0.000001));
264  
265 m_scene.Update(1);
266  
267 // We should really check the exact figure.
268 Assert.That(npc.AbsolutePosition.X, Is.EqualTo(startPos.X));
269 Assert.That(npc.AbsolutePosition.Y, Is.GreaterThan(startPos.Y));
270 Assert.That(npc.AbsolutePosition.Z, Is.EqualTo(startPos.Z));
271 Assert.That(npc.AbsolutePosition.Z, Is.LessThan(targetPos.X));
272  
273 m_scene.Update(10);
274  
275 double distanceToTarget = Util.GetDistanceTo(npc.AbsolutePosition, targetPos);
276 Assert.That(distanceToTarget, Is.LessThan(1), "NPC not within 1 unit of target position on first move");
277 Assert.That(npc.AbsolutePosition, Is.EqualTo(targetPos));
278 Assert.That(npc.AgentControlFlags, Is.EqualTo((uint)AgentManager.ControlFlags.NONE));
279  
280 // Try a second movement
281 startPos = npc.AbsolutePosition;
282 targetPos = startPos + new Vector3(10, 0, 0);
283 m_npcMod.MoveToTarget(npc.UUID, m_scene, targetPos, false, false, false);
284  
285 Assert.That(npc.AbsolutePosition, Is.EqualTo(startPos));
286 // Assert.That(npc.Rotation, Is.EqualTo(new Quaternion(0, 0, 0, 1)));
287 Assert.That(
288 npc.Rotation, new QuaternionToleranceConstraint(new Quaternion(0, 0, 0, 1), 0.000001));
289  
290 m_scene.Update(1);
291  
292 // We should really check the exact figure.
293 Assert.That(npc.AbsolutePosition.X, Is.GreaterThan(startPos.X));
294 Assert.That(npc.AbsolutePosition.X, Is.LessThan(targetPos.X));
295 Assert.That(npc.AbsolutePosition.Y, Is.EqualTo(startPos.Y));
296 Assert.That(npc.AbsolutePosition.Z, Is.EqualTo(startPos.Z));
297  
298 m_scene.Update(10);
299  
300 distanceToTarget = Util.GetDistanceTo(npc.AbsolutePosition, targetPos);
301 Assert.That(distanceToTarget, Is.LessThan(1), "NPC not within 1 unit of target position on second move");
302 Assert.That(npc.AbsolutePosition, Is.EqualTo(targetPos));
303 }
304  
305 [Test]
306 public void TestSitAndStandWithSitTarget()
307 {
308 TestHelpers.InMethod();
309 // log4net.Config.XmlConfigurator.Configure();
310  
311 ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
312  
313 Vector3 startPos = new Vector3(128, 128, 30);
314 UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
315  
316 ScenePresence npc = m_scene.GetScenePresence(npcId);
317 SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
318  
319 part.SitTargetPosition = new Vector3(0, 0, 1);
320 m_npcMod.Sit(npc.UUID, part.UUID, m_scene);
321  
322 Assert.That(part.SitTargetAvatar, Is.EqualTo(npcId));
323 Assert.That(npc.ParentID, Is.EqualTo(part.LocalId));
324 // Assert.That(
325 // npc.AbsolutePosition,
326 // Is.EqualTo(part.AbsolutePosition + part.SitTargetPosition + ScenePresence.SIT_TARGET_ADJUSTMENT));
327  
328 m_npcMod.Stand(npc.UUID, m_scene);
329  
330 Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
331 Assert.That(npc.ParentID, Is.EqualTo(0));
332 }
333  
334 [Test]
335 public void TestSitAndStandWithNoSitTarget()
336 {
337 TestHelpers.InMethod();
338 // TestHelpers.EnableLogging();
339  
340 ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
341  
342 // FIXME: To get this to work for now, we are going to place the npc right next to the target so that
343 // the autopilot doesn't trigger
344 Vector3 startPos = new Vector3(1, 1, 1);
345  
346 UUID npcId = m_npcMod.CreateNPC("John", "Smith", startPos, UUID.Zero, true, m_scene, sp.Appearance);
347  
348 ScenePresence npc = m_scene.GetScenePresence(npcId);
349 SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
350  
351 m_npcMod.Sit(npc.UUID, part.UUID, m_scene);
352  
353 Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
354 Assert.That(npc.ParentID, Is.EqualTo(part.LocalId));
355  
356 // We should really be using the NPC size but this would mean preserving the physics actor since it is
357 // removed on sit.
358 Assert.That(
359 npc.AbsolutePosition,
360 Is.EqualTo(part.AbsolutePosition + new Vector3(0, 0, sp.PhysicsActor.Size.Z / 2)));
361  
362 m_npcMod.Stand(npc.UUID, m_scene);
363  
364 Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
365 Assert.That(npc.ParentID, Is.EqualTo(0));
366 }
367 }
368 }