NPCModule.cs 14 KB

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