1
0

NPCModule.cs 16 KB

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