aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/OptionalModules/World
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/OptionalModules/World')
-rw-r--r--OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs148
1 files changed, 140 insertions, 8 deletions
diff --git a/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs b/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs
index 8c9717c..b3bfe07 100644
--- a/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs
+++ b/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs
@@ -25,26 +25,158 @@
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.Collections.Generic;
29using System.Threading;
28using OpenMetaverse; 30using OpenMetaverse;
29using Nini.Config; 31using Nini.Config;
30using OpenSim.Region.Framework.Interfaces; 32using OpenSim.Region.Framework.Interfaces;
31using OpenSim.Region.Framework.Scenes; 33using OpenSim.Region.Framework.Scenes;
34using OpenSim.Region.CoreModules.Avatar.NPC;
35using OpenSim.Framework;
36using Timer=System.Timers.Timer;
32 37
33namespace OpenSim.Region.OptionalModules.World.NPC 38namespace OpenSim.Region.OptionalModules.World.NPC
34{ 39{
35 public class NPCModule : IRegionModule 40 public class NPCModule : IRegionModule, INPCModule
36 { 41 {
37 // private const bool m_enabled = false; 42 // private const bool m_enabled = false;
38 43
44 private Mutex m_createMutex = new Mutex(false);
45
46 private Timer m_timer = new Timer(500);
47
48 private Dictionary<UUID,NPCAvatar> m_avatars = new Dictionary<UUID, NPCAvatar>();
49
50 private Dictionary<UUID,AvatarAppearance> m_appearanceCache = new Dictionary<UUID, AvatarAppearance>();
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
63 private AvatarAppearance GetAppearance(UUID target, Scene scene)
64 {
65 if (m_appearanceCache.ContainsKey(target))
66 return m_appearanceCache[target];
67
68 AvatarAppearance x = scene.CommsManager.AvatarService.GetUserAppearance(target);
69
70 m_appearanceCache.Add(target, x);
71
72 return x;
73 }
74
75 public UUID CreateNPC(string firstname, string lastname,Vector3 position, Scene scene, UUID cloneAppearanceFrom)
76 {
77 // Block.
78 m_createMutex.WaitOne();
79
80 // Copy Temp Variables for Timer to pick up.
81 lock (p_lock)
82 {
83 p_firstname = firstname;
84 p_lastname = lastname;
85 p_position = position;
86 p_scene = scene;
87 p_cloneAppearanceFrom = cloneAppearanceFrom;
88 p_inUse = true;
89 p_returnUuid = UUID.Zero;
90 }
91
92 while (p_returnUuid == UUID.Zero)
93 {
94 Thread.Sleep(250);
95 }
96
97 m_createMutex.ReleaseMutex();
98
99 return p_returnUuid;
100 }
101
102 public void Autopilot(UUID agentID, Scene scene, Vector3 pos)
103 {
104 lock (m_avatars)
105 {
106 if (m_avatars.ContainsKey(agentID))
107 {
108 ScenePresence sp;
109 scene.TryGetAvatar(agentID, out sp);
110 sp.DoAutoPilot(0, pos, m_avatars[agentID]);
111 }
112 }
113 }
114
115 public void Say(UUID agentID, Scene scene, string text)
116 {
117 lock (m_avatars)
118 {
119 if (m_avatars.ContainsKey(agentID))
120 {
121 m_avatars[agentID].Say(text);
122 }
123 }
124 }
125
126 public void DeleteNPC(UUID agentID, Scene scene)
127 {
128 lock (m_avatars)
129 {
130 if (m_avatars.ContainsKey(agentID))
131 {
132 scene.RemoveClient(agentID);
133 m_avatars.Remove(agentID);
134 }
135 }
136 }
137
138
39 public void Initialise(Scene scene, IConfigSource source) 139 public void Initialise(Scene scene, IConfigSource source)
40 { 140 {
41 // if (m_enabled) 141 scene.RegisterModuleInterface<INPCModule>(this);
42 // { 142
43 // NPCAvatar testAvatar = new NPCAvatar("Jack", "NPC", new Vector3(128, 128, 40), scene); 143 m_timer.Elapsed += m_timer_Elapsed;
44 // NPCAvatar testAvatar2 = new NPCAvatar("Jill", "NPC", new Vector3(136, 128, 40), scene); 144 m_timer.Start();
45 // scene.AddNewClient(testAvatar); 145 }
46 // scene.AddNewClient(testAvatar2); 146
47 // } 147 void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
148 {
149 lock (p_lock)
150 {
151 if (p_inUse)
152 {
153 p_inUse = false;
154
155 NPCAvatar npcAvatar = new NPCAvatar(p_firstname, p_lastname, p_position, p_scene);
156 npcAvatar.CircuitCode = (uint) Util.RandomClass.Next(0, int.MaxValue);
157
158 p_scene.ClientManager.Add(npcAvatar.CircuitCode, npcAvatar);
159 p_scene.AddNewClient(npcAvatar);
160
161 ScenePresence sp;
162 if (p_scene.TryGetAvatar(npcAvatar.AgentId, out sp))
163 {
164 AvatarAppearance x = GetAppearance(p_cloneAppearanceFrom, p_scene);
165
166 List<byte> wearbyte = new List<byte>();
167 for (int i = 0; i < x.VisualParams.Length; i++)
168 {
169 wearbyte.Add(x.VisualParams[i]);
170 }
171
172 sp.SetAppearance(x.Texture.GetBytes(), wearbyte);
173 }
174
175 m_avatars.Add(npcAvatar.AgentId, npcAvatar);
176
177 p_returnUuid = npcAvatar.AgentId;
178 }
179 }
48 } 180 }
49 181
50 public void PostInitialise() 182 public void PostInitialise()