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.cs86
1 files changed, 68 insertions, 18 deletions
diff --git a/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs b/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs
index eeb74d9..c710723 100644
--- a/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs
+++ b/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs
@@ -26,12 +26,14 @@
26 */ 26 */
27 27
28using System.Collections.Generic; 28using System.Collections.Generic;
29using System.Threading;
29using OpenMetaverse; 30using OpenMetaverse;
30using Nini.Config; 31using Nini.Config;
31using OpenSim.Region.Framework.Interfaces; 32using OpenSim.Region.Framework.Interfaces;
32using OpenSim.Region.Framework.Scenes; 33using OpenSim.Region.Framework.Scenes;
33using OpenSim.Region.CoreModules.Avatar.NPC; 34using OpenSim.Region.CoreModules.Avatar.NPC;
34using OpenSim.Framework; 35using OpenSim.Framework;
36using Timer=System.Timers.Timer;
35 37
36namespace OpenSim.Region.OptionalModules.World.NPC 38namespace OpenSim.Region.OptionalModules.World.NPC
37{ 39{
@@ -39,10 +41,25 @@ namespace OpenSim.Region.OptionalModules.World.NPC
39 { 41 {
40 // private const bool m_enabled = false; 42 // private const bool m_enabled = false;
41 43
44 private Mutex m_createMutex = new Mutex(false);
45
46 private Timer m_timer = new Timer(500);
47
42 private Dictionary<UUID,NPCAvatar> m_avatars = new Dictionary<UUID, NPCAvatar>(); 48 private Dictionary<UUID,NPCAvatar> m_avatars = new Dictionary<UUID, NPCAvatar>();
43 49
44 private Dictionary<UUID,AvatarAppearance> m_appearanceCache = new Dictionary<UUID, AvatarAppearance>(); 50 private Dictionary<UUID,AvatarAppearance> m_appearanceCache = new Dictionary<UUID, AvatarAppearance>();
45 51
52 // Timer vars.
53 private bool p_inUse = false;
54 private readonly object p_lock = new object();
55 // Private Temporary Variables.
56 private string p_firstname;
57 private string p_lastname;
58 private Vector3 p_position;
59 private Scene p_scene;
60 private UUID p_cloneAppearanceFrom;
61 private UUID p_returnUuid;
62
46 private AvatarAppearance GetAppearance(UUID target, Scene scene) 63 private AvatarAppearance GetAppearance(UUID target, Scene scene)
47 { 64 {
48 if (m_appearanceCache.ContainsKey(target)) 65 if (m_appearanceCache.ContainsKey(target))
@@ -57,29 +74,23 @@ namespace OpenSim.Region.OptionalModules.World.NPC
57 74
58 public UUID CreateNPC(string firstname, string lastname,Vector3 position, Scene scene, UUID cloneAppearanceFrom) 75 public UUID CreateNPC(string firstname, string lastname,Vector3 position, Scene scene, UUID cloneAppearanceFrom)
59 { 76 {
60 NPCAvatar npcAvatar = new NPCAvatar(firstname, lastname, position, scene); 77 // Block.
61 npcAvatar.CircuitCode = (uint) Util.RandomClass.Next(0, int.MaxValue); 78 m_createMutex.WaitOne();
62
63 scene.ClientManager.Add(npcAvatar.CircuitCode, npcAvatar);
64 scene.AddNewClient(npcAvatar);
65 79
66 ScenePresence sp; 80 // Copy Temp Variables for Timer to pick up.
67 if(scene.TryGetAvatar(npcAvatar.AgentId, out sp)) 81 lock (p_lock)
68 { 82 {
69 AvatarAppearance x = GetAppearance(cloneAppearanceFrom, scene); 83 p_firstname = firstname;
70 84 p_lastname = lastname;
71 List<byte> wearbyte = new List<byte>(); 85 p_position = position;
72 for (int i = 0; i < x.VisualParams.Length; i++) 86 p_scene = scene;
73 { 87 p_cloneAppearanceFrom = cloneAppearanceFrom;
74 wearbyte.Add(x.VisualParams[i]); 88 p_inUse = true;
75 }
76
77 sp.SetAppearance(x.Texture.GetBytes(), wearbyte);
78 } 89 }
79 90
80 m_avatars.Add(npcAvatar.AgentId, npcAvatar); 91 m_createMutex.ReleaseMutex();
81 92
82 return npcAvatar.AgentId; 93 return p_returnUuid;
83 } 94 }
84 95
85 public void Autopilot(UUID agentID, Scene scene, Vector3 pos) 96 public void Autopilot(UUID agentID, Scene scene, Vector3 pos)
@@ -116,6 +127,45 @@ namespace OpenSim.Region.OptionalModules.World.NPC
116 public void Initialise(Scene scene, IConfigSource source) 127 public void Initialise(Scene scene, IConfigSource source)
117 { 128 {
118 scene.RegisterModuleInterface<INPCModule>(this); 129 scene.RegisterModuleInterface<INPCModule>(this);
130
131 m_timer.Elapsed += m_timer_Elapsed;
132 m_timer.Start();
133 }
134
135 void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
136 {
137 lock (p_lock)
138 {
139 if (p_inUse)
140 {
141 p_inUse = false;
142
143
144 NPCAvatar npcAvatar = new NPCAvatar(p_firstname, p_lastname, p_position, p_scene);
145 npcAvatar.CircuitCode = (uint) Util.RandomClass.Next(0, int.MaxValue);
146
147 p_scene.ClientManager.Add(npcAvatar.CircuitCode, npcAvatar);
148 p_scene.AddNewClient(npcAvatar);
149
150 ScenePresence sp;
151 if (p_scene.TryGetAvatar(npcAvatar.AgentId, out sp))
152 {
153 AvatarAppearance x = GetAppearance(p_cloneAppearanceFrom, p_scene);
154
155 List<byte> wearbyte = new List<byte>();
156 for (int i = 0; i < x.VisualParams.Length; i++)
157 {
158 wearbyte.Add(x.VisualParams[i]);
159 }
160
161 sp.SetAppearance(x.Texture.GetBytes(), wearbyte);
162 }
163
164 m_avatars.Add(npcAvatar.AgentId, npcAvatar);
165
166 p_returnUuid = npcAvatar.AgentId;
167 }
168 }
119 } 169 }
120 170
121 public void PostInitialise() 171 public void PostInitialise()