1
0

NPCModule.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. private NPCOptionsFlags m_NPCOptionFlags;
  50. public NPCOptionsFlags NPCOptionFlags {get {return m_NPCOptionFlags;}}
  51. public bool Enabled { get; private set; }
  52. public void Initialise(IConfigSource source)
  53. {
  54. IConfig config = source.Configs["NPC"];
  55. Enabled = (config != null && config.GetBoolean("Enabled", false));
  56. m_NPCOptionFlags = NPCOptionsFlags.None;
  57. if(Enabled)
  58. {
  59. if(config.GetBoolean("AllowNotOwned", true))
  60. m_NPCOptionFlags |= NPCOptionsFlags.AllowNotOwned;
  61. if(config.GetBoolean("AllowSenseAsAvatar", true))
  62. m_NPCOptionFlags |= NPCOptionsFlags.AllowSenseAsAvatar;
  63. if(config.GetBoolean("AllowCloneOtherAvatars", true))
  64. m_NPCOptionFlags |= NPCOptionsFlags.AllowCloneOtherAvatars;
  65. if(config.GetBoolean("NoNPCGroup", true))
  66. m_NPCOptionFlags |= NPCOptionsFlags.NoNPCGroup;
  67. }
  68. }
  69. public void AddRegion(Scene scene)
  70. {
  71. if (Enabled)
  72. scene.RegisterModuleInterface<INPCModule>(this);
  73. }
  74. public void RegionLoaded(Scene scene)
  75. {
  76. }
  77. public void PostInitialise()
  78. {
  79. }
  80. public void RemoveRegion(Scene scene)
  81. {
  82. scene.UnregisterModuleInterface<INPCModule>(this);
  83. }
  84. public void Close()
  85. {
  86. }
  87. public string Name
  88. {
  89. get { return "NPCModule"; }
  90. }
  91. public Type ReplaceableInterface { get { return null; } }
  92. public bool IsNPC(UUID agentId, Scene scene)
  93. {
  94. // FIXME: This implementation could not just use the
  95. // ScenePresence.PresenceType (and callers could inspect that
  96. // directly).
  97. ScenePresence sp = scene.GetScenePresence(agentId);
  98. if (sp == null || sp.IsChildAgent)
  99. return false;
  100. lock (m_avatars)
  101. return m_avatars.ContainsKey(agentId);
  102. }
  103. public bool SetNPCAppearance(UUID agentId,
  104. AvatarAppearance appearance, Scene scene)
  105. {
  106. ScenePresence npc = scene.GetScenePresence(agentId);
  107. if (npc == null || npc.IsChildAgent)
  108. return false;
  109. lock (m_avatars)
  110. if (!m_avatars.ContainsKey(agentId))
  111. return false;
  112. // Delete existing npc attachments
  113. if(scene.AttachmentsModule != null)
  114. scene.AttachmentsModule.DeleteAttachmentsFromScene(npc, false);
  115. // XXX: We can't just use IAvatarFactoryModule.SetAppearance() yet
  116. // since it doesn't transfer attachments
  117. AvatarAppearance npcAppearance = new AvatarAppearance(appearance,
  118. true);
  119. npc.Appearance = npcAppearance;
  120. // Rez needed npc attachments
  121. if (scene.AttachmentsModule != null)
  122. scene.AttachmentsModule.RezAttachments(npc);
  123. IAvatarFactoryModule module =
  124. scene.RequestModuleInterface<IAvatarFactoryModule>();
  125. module.SendAppearance(npc.UUID);
  126. return true;
  127. }
  128. public UUID CreateNPC(string firstname, string lastname,
  129. Vector3 position, UUID owner, bool senseAsAgent, Scene scene,
  130. AvatarAppearance appearance)
  131. {
  132. return CreateNPC(firstname, lastname, position, UUID.Zero, owner, "", UUID.Zero, senseAsAgent, scene, appearance);
  133. }
  134. public UUID CreateNPC(string firstname, string lastname,
  135. Vector3 position, UUID agentID, UUID owner, string groupTitle, UUID groupID, bool senseAsAgent, Scene scene,
  136. AvatarAppearance appearance)
  137. {
  138. NPCAvatar npcAvatar = null;
  139. string born = DateTime.UtcNow.ToString();
  140. try
  141. {
  142. if (agentID == UUID.Zero)
  143. npcAvatar = new NPCAvatar(firstname, lastname, position,
  144. owner, senseAsAgent, scene);
  145. else
  146. npcAvatar = new NPCAvatar(firstname, lastname, agentID, position,
  147. owner, senseAsAgent, scene);
  148. }
  149. catch (Exception e)
  150. {
  151. m_log.Info("[NPC MODULE]: exception creating NPC avatar: " + e.ToString());
  152. return UUID.Zero;
  153. }
  154. npcAvatar.CircuitCode = (uint)Util.RandomClass.Next(0,
  155. int.MaxValue);
  156. // m_log.DebugFormat(
  157. // "[NPC MODULE]: Creating NPC {0} {1} {2}, owner={3}, senseAsAgent={4} at {5} in {6}",
  158. // firstname, lastname, npcAvatar.AgentId, owner, senseAsAgent, position, scene.RegionInfo.RegionName);
  159. AgentCircuitData acd = new AgentCircuitData();
  160. acd.AgentID = npcAvatar.AgentId;
  161. acd.firstname = firstname;
  162. acd.lastname = lastname;
  163. acd.ServiceURLs = new Dictionary<string, object>();
  164. AvatarAppearance npcAppearance = new AvatarAppearance(appearance, true);
  165. acd.Appearance = npcAppearance;
  166. /*
  167. for (int i = 0;
  168. i < acd.Appearance.Texture.FaceTextures.Length; i++)
  169. {
  170. m_log.DebugFormat(
  171. "[NPC MODULE]: NPC avatar {0} has texture id {1} : {2}",
  172. acd.AgentID, i,
  173. acd.Appearance.Texture.FaceTextures[i]);
  174. }
  175. */
  176. // ManualResetEvent ev = new ManualResetEvent(false);
  177. // Util.FireAndForget(delegate(object x) {
  178. lock (m_avatars)
  179. {
  180. scene.AuthenticateHandler.AddNewCircuit(npcAvatar.CircuitCode, acd);
  181. scene.AddNewAgent(npcAvatar, PresenceType.Npc);
  182. if (scene.TryGetScenePresence(npcAvatar.AgentId, out ScenePresence sp))
  183. {
  184. npcAvatar.Born = born;
  185. npcAvatar.ActiveGroupId = groupID;
  186. sp.CompleteMovement(npcAvatar, false);
  187. sp.Grouptitle = groupTitle;
  188. m_avatars.Add(npcAvatar.AgentId, npcAvatar);
  189. // m_log.DebugFormat("[NPC MODULE]: Created NPC {0} {1}", npcAvatar.AgentId, sp.Name);
  190. }
  191. }
  192. // ev.Set();
  193. // });
  194. // ev.WaitOne();
  195. // m_log.DebugFormat("[NPC MODULE]: Created NPC with id {0}", npcAvatar.AgentId);
  196. return npcAvatar.AgentId;
  197. }
  198. public bool MoveToTarget(UUID agentID, Scene scene, Vector3 pos,
  199. bool noFly, bool landAtTarget, bool running)
  200. {
  201. lock (m_avatars)
  202. {
  203. if (m_avatars.ContainsKey(agentID))
  204. {
  205. ScenePresence sp;
  206. if (scene.TryGetScenePresence(agentID, out sp))
  207. {
  208. if (sp.IsSatOnObject || sp.SitGround)
  209. return false;
  210. // m_log.DebugFormat(
  211. // "[NPC MODULE]: Moving {0} to {1} in {2}, noFly {3}, landAtTarget {4}",
  212. // sp.Name, pos, scene.RegionInfo.RegionName,
  213. // noFly, landAtTarget);
  214. sp.MoveToTarget(pos, noFly, landAtTarget, running);
  215. return true;
  216. }
  217. }
  218. }
  219. return false;
  220. }
  221. public bool StopMoveToTarget(UUID agentID, Scene scene)
  222. {
  223. lock (m_avatars)
  224. {
  225. if (m_avatars.ContainsKey(agentID))
  226. {
  227. ScenePresence sp;
  228. if (scene.TryGetScenePresence(agentID, out sp))
  229. {
  230. sp.Velocity = Vector3.Zero;
  231. sp.ResetMoveToTarget();
  232. return true;
  233. }
  234. }
  235. }
  236. return false;
  237. }
  238. public bool Say(UUID agentID, Scene scene, string text)
  239. {
  240. return Say(agentID, scene, text, 0);
  241. }
  242. public bool Say(UUID agentID, Scene scene, string text, int channel)
  243. {
  244. lock (m_avatars)
  245. {
  246. if (m_avatars.ContainsKey(agentID))
  247. {
  248. m_avatars[agentID].Say(channel, text);
  249. return true;
  250. }
  251. }
  252. return false;
  253. }
  254. public bool Shout(UUID agentID, Scene scene, string text, int channel)
  255. {
  256. lock (m_avatars)
  257. {
  258. if (m_avatars.ContainsKey(agentID))
  259. {
  260. m_avatars[agentID].Shout(channel, text);
  261. return true;
  262. }
  263. }
  264. return false;
  265. }
  266. public bool Sit(UUID agentID, UUID partID, Scene scene)
  267. {
  268. lock (m_avatars)
  269. {
  270. if (m_avatars.ContainsKey(agentID))
  271. {
  272. ScenePresence sp;
  273. if (scene.TryGetScenePresence(agentID, out sp))
  274. {
  275. sp.HandleAgentRequestSit(m_avatars[agentID], agentID, partID, Vector3.Zero);
  276. return true;
  277. }
  278. }
  279. }
  280. return false;
  281. }
  282. public bool Whisper(UUID agentID, Scene scene, string text,
  283. int channel)
  284. {
  285. lock (m_avatars)
  286. {
  287. if (m_avatars.ContainsKey(agentID))
  288. {
  289. m_avatars[agentID].Whisper(channel, text);
  290. return true;
  291. }
  292. }
  293. return false;
  294. }
  295. public bool Stand(UUID agentID, Scene scene)
  296. {
  297. lock (m_avatars)
  298. {
  299. if (m_avatars.ContainsKey(agentID))
  300. {
  301. ScenePresence sp;
  302. if (scene.TryGetScenePresence(agentID, out sp))
  303. {
  304. sp.StandUp();
  305. return true;
  306. }
  307. }
  308. }
  309. return false;
  310. }
  311. public bool Touch(UUID agentID, UUID objectID)
  312. {
  313. lock (m_avatars)
  314. {
  315. if (m_avatars.ContainsKey(agentID))
  316. return m_avatars[agentID].Touch(objectID);
  317. return false;
  318. }
  319. }
  320. public UUID GetOwner(UUID agentID)
  321. {
  322. lock (m_avatars)
  323. {
  324. NPCAvatar av;
  325. if (m_avatars.TryGetValue(agentID, out av))
  326. return av.OwnerID;
  327. }
  328. return UUID.Zero;
  329. }
  330. public INPC GetNPC(UUID agentID, Scene scene)
  331. {
  332. lock (m_avatars)
  333. {
  334. if (m_avatars.ContainsKey(agentID))
  335. return m_avatars[agentID];
  336. else
  337. return null;
  338. }
  339. }
  340. public bool DeleteNPC(UUID agentID, Scene scene)
  341. {
  342. bool doRemove = false;
  343. NPCAvatar av;
  344. lock (m_avatars)
  345. {
  346. if (m_avatars.TryGetValue(agentID, out av))
  347. {
  348. /*
  349. m_log.DebugFormat("[NPC MODULE]: Found {0} {1} to remove",
  350. agentID, av.Name);
  351. */
  352. doRemove = true;
  353. }
  354. }
  355. if (doRemove)
  356. {
  357. scene.CloseAgent(agentID, false);
  358. lock (m_avatars)
  359. {
  360. m_avatars.Remove(agentID);
  361. }
  362. m_log.DebugFormat("[NPC MODULE]: Removed NPC {0} {1}",
  363. agentID, av.Name);
  364. return true;
  365. }
  366. /*
  367. m_log.DebugFormat("[NPC MODULE]: Could not find {0} to remove",
  368. agentID);
  369. */
  370. return false;
  371. }
  372. public bool CheckPermissions(UUID npcID, UUID callerID)
  373. {
  374. lock (m_avatars)
  375. {
  376. NPCAvatar av;
  377. if (m_avatars.TryGetValue(npcID, out av))
  378. {
  379. if (npcID == callerID)
  380. return true;
  381. return CheckPermissions(av, callerID);
  382. }
  383. else
  384. {
  385. return false;
  386. }
  387. }
  388. }
  389. /// <summary>
  390. /// Check if the caller has permission to manipulate the given NPC.
  391. /// </summary>
  392. /// <remarks>
  393. /// A caller has permission if
  394. /// * The caller UUID given is UUID.Zero.
  395. /// * The avatar is unowned (owner is UUID.Zero).
  396. /// * The avatar is owned and the owner and callerID match.
  397. /// * The avatar is owned and the callerID matches its agentID.
  398. /// </remarks>
  399. /// <param name="av"></param>
  400. /// <param name="callerID"></param>
  401. /// <returns>true if they do, false if they don't.</returns>
  402. private bool CheckPermissions(NPCAvatar av, UUID callerID)
  403. {
  404. return callerID == UUID.Zero || av.OwnerID == UUID.Zero ||
  405. av.OwnerID == callerID || av.AgentId == callerID;
  406. }
  407. }
  408. }