1
0

NPCModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. scene.AttachmentsModule.DeleteAttachmentsFromScene(sp, false);
  127. AvatarAppearance npcAppearance = new AvatarAppearance(appearance, true);
  128. sp.Appearance = npcAppearance;
  129. scene.AttachmentsModule.RezAttachments(sp);
  130. IAvatarFactory module = scene.RequestModuleInterface<IAvatarFactory>();
  131. module.SendAppearance(sp.UUID);
  132. return true;
  133. }
  134. public UUID CreateNPC(
  135. string firstname, string lastname, Vector3 position, Scene scene, AvatarAppearance appearance)
  136. {
  137. NPCAvatar npcAvatar = new NPCAvatar(firstname, lastname, position, scene);
  138. npcAvatar.CircuitCode = (uint)Util.RandomClass.Next(0, int.MaxValue);
  139. m_log.DebugFormat(
  140. "[NPC MODULE]: Creating NPC {0} {1} {2} at {3} in {4}",
  141. firstname, lastname, npcAvatar.AgentId, position, scene.RegionInfo.RegionName);
  142. AgentCircuitData acd = new AgentCircuitData();
  143. acd.AgentID = npcAvatar.AgentId;
  144. acd.firstname = firstname;
  145. acd.lastname = lastname;
  146. acd.ServiceURLs = new Dictionary<string, object>();
  147. AvatarAppearance npcAppearance = new AvatarAppearance(appearance, true);
  148. acd.Appearance = npcAppearance;
  149. // for (int i = 0; i < acd.Appearance.Texture.FaceTextures.Length; i++)
  150. // {
  151. // m_log.DebugFormat(
  152. // "[NPC MODULE]: NPC avatar {0} has texture id {1} : {2}",
  153. // acd.AgentID, i, acd.Appearance.Texture.FaceTextures[i]);
  154. // }
  155. scene.AuthenticateHandler.AddNewCircuit(npcAvatar.CircuitCode, acd);
  156. scene.AddNewClient(npcAvatar, PresenceType.Npc);
  157. ScenePresence sp;
  158. if (scene.TryGetScenePresence(npcAvatar.AgentId, out sp))
  159. {
  160. m_log.DebugFormat(
  161. "[NPC MODULE]: Successfully retrieved scene presence for NPC {0} {1}", sp.Name, sp.UUID);
  162. sp.CompleteMovement(npcAvatar, false);
  163. }
  164. else
  165. {
  166. m_log.WarnFormat("[NPC MODULE]: Could not find scene presence for NPC {0} {1}", sp.Name, sp.UUID);
  167. }
  168. lock (m_avatars)
  169. m_avatars.Add(npcAvatar.AgentId, npcAvatar);
  170. m_log.DebugFormat("[NPC MODULE]: Created NPC with id {0}", npcAvatar.AgentId);
  171. return npcAvatar.AgentId;
  172. }
  173. public bool MoveToTarget(UUID agentID, Scene scene, Vector3 pos, bool noFly, bool landAtTarget)
  174. {
  175. lock (m_avatars)
  176. {
  177. if (m_avatars.ContainsKey(agentID))
  178. {
  179. ScenePresence sp;
  180. scene.TryGetScenePresence(agentID, out sp);
  181. m_log.DebugFormat(
  182. "[NPC MODULE]: Moving {0} to {1} in {2}, noFly {3}, landAtTarget {4}",
  183. sp.Name, pos, scene.RegionInfo.RegionName, noFly, landAtTarget);
  184. m_avatars[agentID].LandAtTarget = landAtTarget;
  185. sp.MoveToTarget(pos, noFly);
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. public bool StopMoveToTarget(UUID agentID, Scene scene)
  192. {
  193. lock (m_avatars)
  194. {
  195. if (m_avatars.ContainsKey(agentID))
  196. {
  197. ScenePresence sp;
  198. scene.TryGetScenePresence(agentID, out sp);
  199. sp.Velocity = Vector3.Zero;
  200. sp.ResetMoveToTarget();
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. public bool Say(UUID agentID, Scene scene, string text)
  207. {
  208. lock (m_avatars)
  209. {
  210. if (m_avatars.ContainsKey(agentID))
  211. {
  212. ScenePresence sp;
  213. scene.TryGetScenePresence(agentID, out sp);
  214. m_avatars[agentID].Say(text);
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. public bool DeleteNPC(UUID agentID, Scene scene)
  221. {
  222. lock (m_avatars)
  223. {
  224. if (m_avatars.ContainsKey(agentID))
  225. {
  226. scene.RemoveClient(agentID, false);
  227. m_avatars.Remove(agentID);
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. public void PostInitialise()
  234. {
  235. }
  236. public void Close()
  237. {
  238. }
  239. public string Name
  240. {
  241. get { return "NPCModule"; }
  242. }
  243. public bool IsSharedModule
  244. {
  245. get { return true; }
  246. }
  247. }
  248. }