aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs')
-rw-r--r--OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs125
1 files changed, 50 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
28using System;
28using System.Collections.Generic; 29using System.Collections.Generic;
30using System.Reflection;
29using System.Threading; 31using System.Threading;
30using OpenMetaverse; 32using log4net;
31using Nini.Config; 33using Nini.Config;
34using OpenMetaverse;
32using OpenSim.Region.Framework.Interfaces; 35using OpenSim.Region.Framework.Interfaces;
33using OpenSim.Region.Framework.Scenes; 36using OpenSim.Region.Framework.Scenes;
34using OpenSim.Region.CoreModules.Avatar.NPC;
35using OpenSim.Framework; 37using OpenSim.Framework;
36using Timer=System.Timers.Timer; 38using Timer=System.Timers.Timer;
37using OpenSim.Services.Interfaces; 39using 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