diff options
Diffstat (limited to 'OpenSim/Region/OptionalModules/World')
-rw-r--r-- | OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs | 125 | ||||
-rw-r--r-- | OpenSim/Region/OptionalModules/World/NPC/Tests/NPCModuleTests.cs | 71 |
2 files changed, 121 insertions, 75 deletions
diff --git a/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs b/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs index c471636..3cdd06d 100644 --- a/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs +++ b/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs | |||
@@ -25,13 +25,15 @@ | |||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ | 26 | */ |
27 | 27 | ||
28 | using System; | ||
28 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
30 | using System.Reflection; | ||
29 | using System.Threading; | 31 | using System.Threading; |
30 | using OpenMetaverse; | 32 | using log4net; |
31 | using Nini.Config; | 33 | using Nini.Config; |
34 | using OpenMetaverse; | ||
32 | using OpenSim.Region.Framework.Interfaces; | 35 | using OpenSim.Region.Framework.Interfaces; |
33 | using OpenSim.Region.Framework.Scenes; | 36 | using OpenSim.Region.Framework.Scenes; |
34 | using OpenSim.Region.CoreModules.Avatar.NPC; | ||
35 | using OpenSim.Framework; | 37 | using OpenSim.Framework; |
36 | using Timer=System.Timers.Timer; | 38 | using Timer=System.Timers.Timer; |
37 | using OpenSim.Services.Interfaces; | 39 | using OpenSim.Services.Interfaces; |
@@ -40,24 +42,17 @@ namespace OpenSim.Region.OptionalModules.World.NPC | |||
40 | { | 42 | { |
41 | public class NPCModule : IRegionModule, INPCModule | 43 | public class NPCModule : IRegionModule, INPCModule |
42 | { | 44 | { |
43 | // private const bool m_enabled = false; | 45 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
44 | 46 | ||
45 | private Mutex m_createMutex; | 47 | // private const bool m_enabled = false; |
46 | private Timer m_timer; | ||
47 | 48 | ||
48 | private Dictionary<UUID,NPCAvatar> m_avatars = new Dictionary<UUID, NPCAvatar>(); | 49 | private Dictionary<UUID,NPCAvatar> m_avatars = new Dictionary<UUID, NPCAvatar>(); |
49 | private Dictionary<UUID,AvatarAppearance> m_appearanceCache = new Dictionary<UUID, AvatarAppearance>(); | 50 | private Dictionary<UUID,AvatarAppearance> m_appearanceCache = new Dictionary<UUID, AvatarAppearance>(); |
50 | 51 | ||
51 | // Timer vars. | 52 | public void Initialise(Scene scene, IConfigSource source) |
52 | private bool p_inUse = false; | 53 | { |
53 | private readonly object p_lock = new object(); | 54 | scene.RegisterModuleInterface<INPCModule>(this); |
54 | // Private Temporary Variables. | 55 | } |
55 | private string p_firstname; | ||
56 | private string p_lastname; | ||
57 | private Vector3 p_position; | ||
58 | private Scene p_scene; | ||
59 | private UUID p_cloneAppearanceFrom; | ||
60 | private UUID p_returnUuid; | ||
61 | 56 | ||
62 | private AvatarAppearance GetAppearance(UUID target, Scene scene) | 57 | private AvatarAppearance GetAppearance(UUID target, Scene scene) |
63 | { | 58 | { |
@@ -74,31 +69,53 @@ namespace OpenSim.Region.OptionalModules.World.NPC | |||
74 | return new AvatarAppearance(); | 69 | return new AvatarAppearance(); |
75 | } | 70 | } |
76 | 71 | ||
77 | public UUID CreateNPC(string firstname, string lastname,Vector3 position, Scene scene, UUID cloneAppearanceFrom) | 72 | public UUID CreateNPC(string firstname, string lastname, Vector3 position, Scene scene, UUID cloneAppearanceFrom) |
78 | { | 73 | { |
79 | // Block. | 74 | NPCAvatar npcAvatar = new NPCAvatar(firstname, lastname, position, scene); |
80 | m_createMutex.WaitOne(); | 75 | npcAvatar.CircuitCode = (uint)Util.RandomClass.Next(0, int.MaxValue); |
76 | |||
77 | m_log.DebugFormat( | ||
78 | "[NPC MODULE]: Creating NPC {0} {1} {2} at {3} in {4}", | ||
79 | firstname, lastname, npcAvatar.AgentId, position, scene.RegionInfo.RegionName); | ||
80 | |||
81 | AgentCircuitData acd = new AgentCircuitData(); | ||
82 | acd.AgentID = npcAvatar.AgentId; | ||
83 | acd.firstname = firstname; | ||
84 | acd.lastname = lastname; | ||
85 | acd.ServiceURLs = new Dictionary<string, object>(); | ||
81 | 86 | ||
82 | // Copy Temp Variables for Timer to pick up. | 87 | AvatarAppearance originalAppearance = GetAppearance(cloneAppearanceFrom, scene); |
83 | lock (p_lock) | 88 | AvatarAppearance npcAppearance = new AvatarAppearance(originalAppearance, true); |
89 | acd.Appearance = npcAppearance; | ||
90 | |||
91 | scene.AuthenticateHandler.AddNewCircuit(npcAvatar.CircuitCode, acd); | ||
92 | scene.AddNewClient(npcAvatar); | ||
93 | |||
94 | ScenePresence sp; | ||
95 | if (scene.TryGetScenePresence(npcAvatar.AgentId, out sp)) | ||
84 | { | 96 | { |
85 | p_firstname = firstname; | 97 | m_log.DebugFormat( |
86 | p_lastname = lastname; | 98 | "[NPC MODULE]: Successfully retrieved scene presence for NPC {0} {1}", sp.Name, sp.UUID); |
87 | p_position = position; | ||
88 | p_scene = scene; | ||
89 | p_cloneAppearanceFrom = cloneAppearanceFrom; | ||
90 | p_inUse = true; | ||
91 | p_returnUuid = UUID.Zero; | ||
92 | } | ||
93 | 99 | ||
94 | while (p_returnUuid == UUID.Zero) | 100 | // Shouldn't call this - temporary. |
101 | sp.CompleteMovement(npcAvatar); | ||
102 | |||
103 | // sp.SendAppearanceToAllOtherAgents(); | ||
104 | // | ||
105 | // // Send animations back to the avatar as well | ||
106 | // sp.Animator.SendAnimPack(); | ||
107 | } | ||
108 | else | ||
95 | { | 109 | { |
96 | Thread.Sleep(250); | 110 | m_log.WarnFormat("[NPC MODULE]: Could not find scene presence for NPC {0} {1}", sp.Name, sp.UUID); |
97 | } | 111 | } |
98 | 112 | ||
99 | m_createMutex.ReleaseMutex(); | 113 | lock (m_avatars) |
114 | m_avatars.Add(npcAvatar.AgentId, npcAvatar); | ||
115 | |||
116 | m_log.DebugFormat("[NPC MODULE]: Created NPC with id {0}", npcAvatar.AgentId); | ||
100 | 117 | ||
101 | return p_returnUuid; | 118 | return npcAvatar.AgentId; |
102 | } | 119 | } |
103 | 120 | ||
104 | public void Autopilot(UUID agentID, Scene scene, Vector3 pos) | 121 | public void Autopilot(UUID agentID, Scene scene, Vector3 pos) |
@@ -137,48 +154,6 @@ namespace OpenSim.Region.OptionalModules.World.NPC | |||
137 | } | 154 | } |
138 | } | 155 | } |
139 | 156 | ||
140 | |||
141 | public void Initialise(Scene scene, IConfigSource source) | ||
142 | { | ||
143 | m_createMutex = new Mutex(false); | ||
144 | |||
145 | m_timer = new Timer(500); | ||
146 | m_timer.Elapsed += m_timer_Elapsed; | ||
147 | m_timer.Start(); | ||
148 | |||
149 | scene.RegisterModuleInterface<INPCModule>(this); | ||
150 | } | ||
151 | |||
152 | void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) | ||
153 | { | ||
154 | lock (p_lock) | ||
155 | { | ||
156 | if (p_inUse) | ||
157 | { | ||
158 | p_inUse = false; | ||
159 | |||
160 | NPCAvatar npcAvatar = new NPCAvatar(p_firstname, p_lastname, p_position, p_scene); | ||
161 | npcAvatar.CircuitCode = (uint) Util.RandomClass.Next(0, int.MaxValue); | ||
162 | |||
163 | p_scene.AddNewClient(npcAvatar); | ||
164 | |||
165 | ScenePresence sp; | ||
166 | if (p_scene.TryGetScenePresence(npcAvatar.AgentId, out sp)) | ||
167 | { | ||
168 | AvatarAppearance x = GetAppearance(p_cloneAppearanceFrom, p_scene); | ||
169 | |||
170 | sp.Appearance.SetTextureEntries(x.Texture); | ||
171 | sp.Appearance.SetVisualParams((byte[])x.VisualParams.Clone()); | ||
172 | sp.SendAppearanceToAllOtherAgents(); | ||
173 | } | ||
174 | |||
175 | m_avatars.Add(npcAvatar.AgentId, npcAvatar); | ||
176 | |||
177 | p_returnUuid = npcAvatar.AgentId; | ||
178 | } | ||
179 | } | ||
180 | } | ||
181 | |||
182 | public void PostInitialise() | 157 | public void PostInitialise() |
183 | { | 158 | { |
184 | } | 159 | } |
@@ -197,4 +172,4 @@ namespace OpenSim.Region.OptionalModules.World.NPC | |||
197 | get { return true; } | 172 | get { return true; } |
198 | } | 173 | } |
199 | } | 174 | } |
200 | } | 175 | } \ No newline at end of file |
diff --git a/OpenSim/Region/OptionalModules/World/NPC/Tests/NPCModuleTests.cs b/OpenSim/Region/OptionalModules/World/NPC/Tests/NPCModuleTests.cs new file mode 100644 index 0000000..899e721 --- /dev/null +++ b/OpenSim/Region/OptionalModules/World/NPC/Tests/NPCModuleTests.cs | |||
@@ -0,0 +1,71 @@ | |||
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.Reflection; | ||
30 | using Nini.Config; | ||
31 | using NUnit.Framework; | ||
32 | using OpenMetaverse; | ||
33 | using OpenSim.Framework; | ||
34 | using OpenSim.Framework.Communications; | ||
35 | using OpenSim.Region.CoreModules.ServiceConnectorsOut.Avatar; | ||
36 | using OpenSim.Region.Framework.Interfaces; | ||
37 | using OpenSim.Region.Framework.Scenes; | ||
38 | using OpenSim.Services.AvatarService; | ||
39 | using OpenSim.Tests.Common; | ||
40 | using OpenSim.Tests.Common.Mock; | ||
41 | |||
42 | namespace OpenSim.Region.OptionalModules.World.NPC.Tests | ||
43 | { | ||
44 | [TestFixture] | ||
45 | public class NPCModuleTests | ||
46 | { | ||
47 | [Test] | ||
48 | public void TestCreate() | ||
49 | { | ||
50 | TestHelper.InMethod(); | ||
51 | // log4net.Config.XmlConfigurator.Configure(); | ||
52 | |||
53 | IConfigSource config = new IniConfigSource(); | ||
54 | |||
55 | config.AddConfig("Modules"); | ||
56 | config.Configs["Modules"].Set("AvatarServices", "LocalAvatarServicesConnector"); | ||
57 | config.AddConfig("AvatarService"); | ||
58 | config.Configs["AvatarService"].Set("LocalServiceModule", "OpenSim.Services.AvatarService.dll:AvatarService"); | ||
59 | config.Configs["AvatarService"].Set("StorageProvider", "OpenSim.Data.Null.dll"); | ||
60 | |||
61 | TestScene scene = SceneSetupHelpers.SetupScene(); | ||
62 | SceneSetupHelpers.SetupSceneModules(scene, config, new NPCModule(), new LocalAvatarServicesConnector()); | ||
63 | |||
64 | INPCModule npcModule = scene.RequestModuleInterface<INPCModule>(); | ||
65 | UUID npcId = npcModule.CreateNPC("John", "Smith", new Vector3(128, 128, 30), scene, UUID.Zero); | ||
66 | |||
67 | ScenePresence npc = scene.GetScenePresence(npcId); | ||
68 | Assert.That(npc, Is.Not.Null); | ||
69 | } | ||
70 | } | ||
71 | } \ No newline at end of file | ||