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