NPCModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. scene.EventManager.OnSignificantClientMovement += HandleOnSignificantClientMovement;
  52. }
  53. }
  54. public void HandleOnSignificantClientMovement(ScenePresence presence)
  55. {
  56. lock (m_avatars)
  57. {
  58. if (m_avatars.ContainsKey(presence.UUID) && presence.MovingToTarget)
  59. {
  60. double distanceToTarget = Util.GetDistanceTo(presence.AbsolutePosition, presence.MoveToPositionTarget);
  61. // m_log.DebugFormat(
  62. // "[NPC MODULE]: Abs pos of {0} is {1}, target {2}, distance {3}",
  63. // presence.Name, presence.AbsolutePosition, presence.MoveToPositionTarget, distanceToTarget);
  64. // Check the error term of the current position in relation to the target position
  65. if (distanceToTarget <= ScenePresence.SIGNIFICANT_MOVEMENT)
  66. {
  67. // We are close enough to the target
  68. m_log.DebugFormat("[NPC MODULE]: Stopping movement of npc {0}", presence.Name);
  69. presence.Velocity = Vector3.Zero;
  70. presence.AbsolutePosition = presence.MoveToPositionTarget;
  71. presence.ResetMoveToTarget();
  72. if (presence.PhysicsActor.Flying)
  73. {
  74. // A horrible hack to stop the NPC dead in its tracks rather than having them overshoot
  75. // the target if flying.
  76. // We really need to be more subtle (slow the avatar as it approaches the target) or at
  77. // least be able to set collision status once, rather than 5 times to give it enough
  78. // weighting so that that PhysicsActor thinks it really is colliding.
  79. for (int i = 0; i < 5; i++)
  80. presence.PhysicsActor.IsColliding = true;
  81. // Vector3 targetPos = presence.MoveToPositionTarget;
  82. if (m_avatars[presence.UUID].LandAtTarget)
  83. presence.PhysicsActor.Flying = false;
  84. // float terrainHeight = (float)presence.Scene.Heightmap[(int)targetPos.X, (int)targetPos.Y];
  85. // if (targetPos.Z - terrainHeight < 0.2)
  86. // {
  87. // presence.PhysicsActor.Flying = false;
  88. // }
  89. }
  90. // m_log.DebugFormat(
  91. // "[NPC MODULE]: AgentControlFlags {0}, MovementFlag {1} for {2}",
  92. // presence.AgentControlFlags, presence.MovementFlag, presence.Name);
  93. }
  94. else
  95. {
  96. // m_log.DebugFormat(
  97. // "[NPC MODULE]: Updating npc {0} at {1} for next movement to {2}",
  98. // presence.Name, presence.AbsolutePosition, presence.MoveToPositionTarget);
  99. Vector3 agent_control_v3 = new Vector3();
  100. presence.HandleMoveToTargetUpdate(ref agent_control_v3);
  101. presence.AddNewMovement(agent_control_v3);
  102. }
  103. //
  104. //// presence.DoMoveToPositionUpdate((0, presence.MoveToPositionTarget, null);
  105. //
  106. //
  107. }
  108. }
  109. }
  110. public bool IsNPC(UUID agentId, Scene scene)
  111. {
  112. ScenePresence sp = scene.GetScenePresence(agentId);
  113. if (sp == null || sp.IsChildAgent)
  114. return false;
  115. lock (m_avatars)
  116. return m_avatars.ContainsKey(agentId);
  117. }
  118. public bool SetNPCAppearance(UUID agentId, AvatarAppearance appearance, Scene scene)
  119. {
  120. ScenePresence sp = scene.GetScenePresence(agentId);
  121. if (sp == null || sp.IsChildAgent)
  122. return false;
  123. lock (m_avatars)
  124. if (!m_avatars.ContainsKey(agentId))
  125. return false;
  126. // FIXME: An extremely bad bit of code that reaches directly into the attachments list and manipulates it
  127. List<SceneObjectGroup> attachments = sp.Attachments;
  128. lock (attachments)
  129. {
  130. foreach (SceneObjectGroup att in attachments)
  131. scene.DeleteSceneObject(att, false);
  132. attachments.Clear();
  133. }
  134. AvatarAppearance npcAppearance = new AvatarAppearance(appearance, true);
  135. sp.Appearance = npcAppearance;
  136. sp.RezAttachments();
  137. IAvatarFactory module = scene.RequestModuleInterface<IAvatarFactory>();
  138. module.SendAppearance(sp.UUID);
  139. return true;
  140. }
  141. public UUID CreateNPC(
  142. string firstname, string lastname, Vector3 position, Scene scene, AvatarAppearance appearance)
  143. {
  144. NPCAvatar npcAvatar = new NPCAvatar(firstname, lastname, position, scene);
  145. npcAvatar.CircuitCode = (uint)Util.RandomClass.Next(0, int.MaxValue);
  146. m_log.DebugFormat(
  147. "[NPC MODULE]: Creating NPC {0} {1} {2} at {3} in {4}",
  148. firstname, lastname, npcAvatar.AgentId, position, scene.RegionInfo.RegionName);
  149. AgentCircuitData acd = new AgentCircuitData();
  150. acd.AgentID = npcAvatar.AgentId;
  151. acd.firstname = firstname;
  152. acd.lastname = lastname;
  153. acd.ServiceURLs = new Dictionary<string, object>();
  154. AvatarAppearance npcAppearance = new AvatarAppearance(appearance, true);
  155. acd.Appearance = npcAppearance;
  156. // for (int i = 0; i < acd.Appearance.Texture.FaceTextures.Length; i++)
  157. // {
  158. // m_log.DebugFormat(
  159. // "[NPC MODULE]: NPC avatar {0} has texture id {1} : {2}",
  160. // acd.AgentID, i, acd.Appearance.Texture.FaceTextures[i]);
  161. // }
  162. scene.AuthenticateHandler.AddNewCircuit(npcAvatar.CircuitCode, acd);
  163. scene.AddNewClient(npcAvatar, PresenceType.Npc);
  164. ScenePresence sp;
  165. if (scene.TryGetScenePresence(npcAvatar.AgentId, out sp))
  166. {
  167. m_log.DebugFormat(
  168. "[NPC MODULE]: Successfully retrieved scene presence for NPC {0} {1}", sp.Name, sp.UUID);
  169. sp.CompleteMovement(npcAvatar, false);
  170. }
  171. else
  172. {
  173. m_log.WarnFormat("[NPC MODULE]: Could not find scene presence for NPC {0} {1}", sp.Name, sp.UUID);
  174. }
  175. lock (m_avatars)
  176. m_avatars.Add(npcAvatar.AgentId, npcAvatar);
  177. m_log.DebugFormat("[NPC MODULE]: Created NPC with id {0}", npcAvatar.AgentId);
  178. return npcAvatar.AgentId;
  179. }
  180. public bool MoveToTarget(UUID agentID, Scene scene, Vector3 pos, bool noFly, bool landAtTarget)
  181. {
  182. lock (m_avatars)
  183. {
  184. if (m_avatars.ContainsKey(agentID))
  185. {
  186. ScenePresence sp;
  187. scene.TryGetScenePresence(agentID, out sp);
  188. m_log.DebugFormat(
  189. "[NPC MODULE]: Moving {0} to {1} in {2}, noFly {3}, landAtTarget {4}",
  190. sp.Name, pos, scene.RegionInfo.RegionName, noFly, landAtTarget);
  191. m_avatars[agentID].LandAtTarget = landAtTarget;
  192. sp.MoveToTarget(pos, noFly);
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. public bool StopMoveToTarget(UUID agentID, Scene scene)
  199. {
  200. lock (m_avatars)
  201. {
  202. if (m_avatars.ContainsKey(agentID))
  203. {
  204. ScenePresence sp;
  205. scene.TryGetScenePresence(agentID, out sp);
  206. sp.Velocity = Vector3.Zero;
  207. sp.ResetMoveToTarget();
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. public bool Say(UUID agentID, Scene scene, string text)
  214. {
  215. lock (m_avatars)
  216. {
  217. if (m_avatars.ContainsKey(agentID))
  218. {
  219. ScenePresence sp;
  220. scene.TryGetScenePresence(agentID, out sp);
  221. m_avatars[agentID].Say(text);
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. public bool DeleteNPC(UUID agentID, Scene scene)
  228. {
  229. lock (m_avatars)
  230. {
  231. if (m_avatars.ContainsKey(agentID))
  232. {
  233. scene.RemoveClient(agentID, false);
  234. m_avatars.Remove(agentID);
  235. return true;
  236. }
  237. }
  238. return false;
  239. }
  240. public void PostInitialise()
  241. {
  242. }
  243. public void Close()
  244. {
  245. }
  246. public string Name
  247. {
  248. get { return "NPCModule"; }
  249. }
  250. public bool IsSharedModule
  251. {
  252. get { return true; }
  253. }
  254. }
  255. }