NPCModule.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. using System.Collections.Generic;
  28. using System.Threading;
  29. using OpenMetaverse;
  30. using Nini.Config;
  31. using OpenSim.Region.Framework.Interfaces;
  32. using OpenSim.Region.Framework.Scenes;
  33. using OpenSim.Region.CoreModules.Avatar.NPC;
  34. using OpenSim.Framework;
  35. using Timer=System.Timers.Timer;
  36. namespace OpenSim.Region.OptionalModules.World.NPC
  37. {
  38. public class NPCModule : IRegionModule, INPCModule
  39. {
  40. // private const bool m_enabled = false;
  41. private Mutex m_createMutex;
  42. private Timer m_timer;
  43. private Dictionary<UUID,NPCAvatar> m_avatars = new Dictionary<UUID, NPCAvatar>();
  44. private Dictionary<UUID,AvatarAppearance> m_appearanceCache = new Dictionary<UUID, AvatarAppearance>();
  45. // Timer vars.
  46. private bool p_inUse = false;
  47. private readonly object p_lock = new object();
  48. // Private Temporary Variables.
  49. private string p_firstname;
  50. private string p_lastname;
  51. private Vector3 p_position;
  52. private Scene p_scene;
  53. private UUID p_cloneAppearanceFrom;
  54. private UUID p_returnUuid;
  55. private AvatarAppearance GetAppearance(UUID target, Scene scene)
  56. {
  57. if (m_appearanceCache.ContainsKey(target))
  58. return m_appearanceCache[target];
  59. AvatarAppearance x = scene.CommsManager.AvatarService.GetUserAppearance(target);
  60. m_appearanceCache.Add(target, x);
  61. return x;
  62. }
  63. public UUID CreateNPC(string firstname, string lastname,Vector3 position, Scene scene, UUID cloneAppearanceFrom)
  64. {
  65. // Block.
  66. m_createMutex.WaitOne();
  67. // Copy Temp Variables for Timer to pick up.
  68. lock (p_lock)
  69. {
  70. p_firstname = firstname;
  71. p_lastname = lastname;
  72. p_position = position;
  73. p_scene = scene;
  74. p_cloneAppearanceFrom = cloneAppearanceFrom;
  75. p_inUse = true;
  76. p_returnUuid = UUID.Zero;
  77. }
  78. while (p_returnUuid == UUID.Zero)
  79. {
  80. Thread.Sleep(250);
  81. }
  82. m_createMutex.ReleaseMutex();
  83. return p_returnUuid;
  84. }
  85. public void Autopilot(UUID agentID, Scene scene, Vector3 pos)
  86. {
  87. lock (m_avatars)
  88. {
  89. if (m_avatars.ContainsKey(agentID))
  90. {
  91. ScenePresence sp;
  92. scene.TryGetAvatar(agentID, out sp);
  93. sp.DoAutoPilot(0, pos, m_avatars[agentID]);
  94. }
  95. }
  96. }
  97. public void Say(UUID agentID, Scene scene, string text)
  98. {
  99. lock (m_avatars)
  100. {
  101. if (m_avatars.ContainsKey(agentID))
  102. {
  103. m_avatars[agentID].Say(text);
  104. }
  105. }
  106. }
  107. public void DeleteNPC(UUID agentID, Scene scene)
  108. {
  109. lock (m_avatars)
  110. {
  111. if (m_avatars.ContainsKey(agentID))
  112. {
  113. scene.RemoveClient(agentID);
  114. m_avatars.Remove(agentID);
  115. }
  116. }
  117. }
  118. public void Initialise(Scene scene, IConfigSource source)
  119. {
  120. m_createMutex = new Mutex(false);
  121. m_timer = new Timer(500);
  122. m_timer.Elapsed += m_timer_Elapsed;
  123. m_timer.Start();
  124. scene.RegisterModuleInterface<INPCModule>(this);
  125. }
  126. void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  127. {
  128. lock (p_lock)
  129. {
  130. if (p_inUse)
  131. {
  132. p_inUse = false;
  133. NPCAvatar npcAvatar = new NPCAvatar(p_firstname, p_lastname, p_position, p_scene);
  134. npcAvatar.CircuitCode = (uint) Util.RandomClass.Next(0, int.MaxValue);
  135. p_scene.AddNewClient(npcAvatar);
  136. ScenePresence sp;
  137. if (p_scene.TryGetAvatar(npcAvatar.AgentId, out sp))
  138. {
  139. AvatarAppearance x = GetAppearance(p_cloneAppearanceFrom, p_scene);
  140. sp.SetAppearance(x.Texture, (byte[])x.VisualParams.Clone());
  141. }
  142. m_avatars.Add(npcAvatar.AgentId, npcAvatar);
  143. p_returnUuid = npcAvatar.AgentId;
  144. }
  145. }
  146. }
  147. public void PostInitialise()
  148. {
  149. }
  150. public void Close()
  151. {
  152. }
  153. public string Name
  154. {
  155. get { return "NPCModule"; }
  156. }
  157. public bool IsSharedModule
  158. {
  159. get { return true; }
  160. }
  161. }
  162. }