NPCModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Threading;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Framework;
  37. using Timer=System.Timers.Timer;
  38. using OpenSim.Services.Interfaces;
  39. namespace OpenSim.Region.OptionalModules.World.NPC
  40. {
  41. public class NPCModule : IRegionModule, INPCModule
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private Dictionary<UUID, NPCAvatar> m_avatars = new Dictionary<UUID, NPCAvatar>();
  45. public void Initialise(Scene scene, IConfigSource source)
  46. {
  47. IConfig config = source.Configs["NPC"];
  48. if (config != null && config.GetBoolean("Enabled", false))
  49. {
  50. scene.RegisterModuleInterface<INPCModule>(this);
  51. }
  52. }
  53. public void PostInitialise()
  54. {
  55. }
  56. public void Close()
  57. {
  58. }
  59. public string Name
  60. {
  61. get { return "NPCModule"; }
  62. }
  63. public bool IsSharedModule
  64. {
  65. get { return true; }
  66. }
  67. public bool IsNPC(UUID agentId, Scene scene)
  68. {
  69. // FIXME: This implementation could not just use the ScenePresence.PresenceType (and callers could inspect
  70. // that directly).
  71. ScenePresence sp = scene.GetScenePresence(agentId);
  72. if (sp == null || sp.IsChildAgent)
  73. return false;
  74. lock (m_avatars)
  75. return m_avatars.ContainsKey(agentId);
  76. }
  77. public bool SetNPCAppearance(UUID agentId, AvatarAppearance appearance, Scene scene)
  78. {
  79. ScenePresence npc = scene.GetScenePresence(agentId);
  80. if (npc == null || npc.IsChildAgent)
  81. return false;
  82. lock (m_avatars)
  83. if (!m_avatars.ContainsKey(agentId))
  84. return false;
  85. // Delete existing npc attachments
  86. scene.AttachmentsModule.DeleteAttachmentsFromScene(npc, false);
  87. // XXX: We can't just use IAvatarFactoryModule.SetAppearance() yet since it doesn't transfer attachments
  88. AvatarAppearance npcAppearance = new AvatarAppearance(appearance, true);
  89. npc.Appearance = npcAppearance;
  90. // Rez needed npc attachments
  91. scene.AttachmentsModule.RezAttachments(npc);
  92. IAvatarFactoryModule module = scene.RequestModuleInterface<IAvatarFactoryModule>();
  93. module.SendAppearance(npc.UUID);
  94. return true;
  95. }
  96. public UUID CreateNPC(
  97. string firstname,
  98. string lastname,
  99. Vector3 position,
  100. UUID owner,
  101. bool senseAsAgent,
  102. Scene scene,
  103. AvatarAppearance appearance)
  104. {
  105. NPCAvatar npcAvatar = new NPCAvatar(firstname, lastname, position, owner, senseAsAgent, scene);
  106. npcAvatar.CircuitCode = (uint)Util.RandomClass.Next(0, int.MaxValue);
  107. m_log.DebugFormat(
  108. "[NPC MODULE]: Creating NPC {0} {1} {2}, owner={3}, senseAsAgent={4} at {5} in {6}",
  109. firstname, lastname, npcAvatar.AgentId, owner, senseAsAgent, position, scene.RegionInfo.RegionName);
  110. AgentCircuitData acd = new AgentCircuitData();
  111. acd.AgentID = npcAvatar.AgentId;
  112. acd.firstname = firstname;
  113. acd.lastname = lastname;
  114. acd.ServiceURLs = new Dictionary<string, object>();
  115. AvatarAppearance npcAppearance = new AvatarAppearance(appearance, true);
  116. acd.Appearance = npcAppearance;
  117. // for (int i = 0; i < acd.Appearance.Texture.FaceTextures.Length; i++)
  118. // {
  119. // m_log.DebugFormat(
  120. // "[NPC MODULE]: NPC avatar {0} has texture id {1} : {2}",
  121. // acd.AgentID, i, acd.Appearance.Texture.FaceTextures[i]);
  122. // }
  123. lock (m_avatars)
  124. {
  125. scene.AuthenticateHandler.AddNewCircuit(npcAvatar.CircuitCode, acd);
  126. scene.AddNewClient(npcAvatar, PresenceType.Npc);
  127. ScenePresence sp;
  128. if (scene.TryGetScenePresence(npcAvatar.AgentId, out sp))
  129. {
  130. // m_log.DebugFormat(
  131. // "[NPC MODULE]: Successfully retrieved scene presence for NPC {0} {1}", sp.Name, sp.UUID);
  132. sp.CompleteMovement(npcAvatar, false);
  133. m_avatars.Add(npcAvatar.AgentId, npcAvatar);
  134. m_log.DebugFormat("[NPC MODULE]: Created NPC with id {0}", npcAvatar.AgentId);
  135. return npcAvatar.AgentId;
  136. }
  137. else
  138. {
  139. m_log.WarnFormat("[NPC MODULE]: Could not find scene presence for NPC {0} {1}", sp.Name, sp.UUID);
  140. return UUID.Zero;
  141. }
  142. }
  143. }
  144. public bool MoveToTarget(UUID agentID, Scene scene, Vector3 pos, bool noFly, bool landAtTarget)
  145. {
  146. lock (m_avatars)
  147. {
  148. if (m_avatars.ContainsKey(agentID))
  149. {
  150. ScenePresence sp;
  151. scene.TryGetScenePresence(agentID, out sp);
  152. m_log.DebugFormat(
  153. "[NPC MODULE]: Moving {0} to {1} in {2}, noFly {3}, landAtTarget {4}",
  154. sp.Name, pos, scene.RegionInfo.RegionName, noFly, landAtTarget);
  155. sp.MoveToTarget(pos, noFly, landAtTarget);
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. public bool StopMoveToTarget(UUID agentID, Scene scene)
  162. {
  163. lock (m_avatars)
  164. {
  165. if (m_avatars.ContainsKey(agentID))
  166. {
  167. ScenePresence sp;
  168. scene.TryGetScenePresence(agentID, out sp);
  169. sp.Velocity = Vector3.Zero;
  170. sp.ResetMoveToTarget();
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. public bool Say(UUID agentID, Scene scene, string text)
  177. {
  178. lock (m_avatars)
  179. {
  180. if (m_avatars.ContainsKey(agentID))
  181. {
  182. ScenePresence sp;
  183. scene.TryGetScenePresence(agentID, out sp);
  184. m_avatars[agentID].Say(text);
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190. public bool Sit(UUID agentID, UUID partID, Scene scene)
  191. {
  192. lock (m_avatars)
  193. {
  194. if (m_avatars.ContainsKey(agentID))
  195. {
  196. ScenePresence sp;
  197. scene.TryGetScenePresence(agentID, out sp);
  198. sp.HandleAgentRequestSit(m_avatars[agentID], agentID, partID, Vector3.Zero);
  199. // sp.HandleAgentSit(m_avatars[agentID], agentID);
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205. public bool Stand(UUID agentID, Scene scene)
  206. {
  207. lock (m_avatars)
  208. {
  209. if (m_avatars.ContainsKey(agentID))
  210. {
  211. ScenePresence sp;
  212. scene.TryGetScenePresence(agentID, out sp);
  213. sp.StandUp();
  214. return true;
  215. }
  216. }
  217. return false;
  218. }
  219. public UUID GetOwner(UUID agentID)
  220. {
  221. lock (m_avatars)
  222. {
  223. NPCAvatar av;
  224. if (m_avatars.TryGetValue(agentID, out av))
  225. {
  226. return av.OwnerID;
  227. }
  228. }
  229. return UUID.Zero;
  230. }
  231. public INPC GetNPC(UUID agentID, Scene scene)
  232. {
  233. lock (m_avatars)
  234. {
  235. if (m_avatars.ContainsKey(agentID))
  236. return m_avatars[agentID];
  237. else
  238. return null;
  239. }
  240. }
  241. public bool DeleteNPC(UUID agentID, Scene scene)
  242. {
  243. lock (m_avatars)
  244. {
  245. NPCAvatar av;
  246. if (m_avatars.TryGetValue(agentID, out av))
  247. {
  248. // m_log.DebugFormat("[NPC MODULE]: Found {0} {1} to remove", agentID, av.Name);
  249. scene.RemoveClient(agentID, false);
  250. m_avatars.Remove(agentID);
  251. // m_log.DebugFormat("[NPC MODULE]: Removed {0} {1}", agentID, av.Name);
  252. return true;
  253. }
  254. }
  255. // m_log.DebugFormat("[NPC MODULE]: Could not find {0} to remove", agentID);
  256. return false;
  257. }
  258. public bool CheckPermissions(UUID npcID, UUID callerID)
  259. {
  260. lock (m_avatars)
  261. {
  262. NPCAvatar av;
  263. if (m_avatars.TryGetValue(npcID, out av))
  264. return CheckPermissions(av, callerID);
  265. else
  266. return false;
  267. }
  268. }
  269. /// <summary>
  270. /// Check if the caller has permission to manipulate the given NPC.
  271. /// </summary>
  272. /// <param name="av"></param>
  273. /// <param name="callerID"></param>
  274. /// <returns>true if they do, false if they don't.</returns>
  275. private bool CheckPermissions(NPCAvatar av, UUID callerID)
  276. {
  277. return callerID == UUID.Zero || av.OwnerID == UUID.Zero || av.OwnerID == callerID;
  278. }
  279. }
  280. }