NPCModule.cs 6.6 KB

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