CombatModule.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 OpenSim 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 Nini.Config;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Framework.Interfaces;
  32. using OpenSim.Region.Framework.Scenes;
  33. using OpenSim.Services.Interfaces;
  34. using OpenMetaverse;
  35. using Mono.Addins;
  36. namespace OpenSim.Region.CoreModules.Avatar.Combat.CombatModule
  37. {
  38. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "CombatModule")]
  39. public class CombatModule : ISharedRegionModule
  40. {
  41. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. /// <summary>
  43. /// Region UUIDS indexed by AgentID
  44. /// </summary>
  45. //private Dictionary<UUID, UUID> m_rootAgents = new Dictionary<UUID, UUID>();
  46. /// <summary>
  47. /// Scenes by Region Handle
  48. /// </summary>
  49. private Dictionary<ulong, Scene> m_scenel = new Dictionary<ulong, Scene>();
  50. /// <summary>
  51. /// Startup
  52. /// </summary>
  53. /// <param name="scene"></param>
  54. /// <param name="config"></param>
  55. public void Initialise(IConfigSource config)
  56. {
  57. }
  58. public void AddRegion(Scene scene)
  59. {
  60. lock (m_scenel)
  61. {
  62. if (m_scenel.ContainsKey(scene.RegionInfo.RegionHandle))
  63. {
  64. m_scenel[scene.RegionInfo.RegionHandle] = scene;
  65. }
  66. else
  67. {
  68. m_scenel.Add(scene.RegionInfo.RegionHandle, scene);
  69. }
  70. }
  71. scene.EventManager.OnAvatarKilled += KillAvatar;
  72. scene.EventManager.OnAvatarEnteringNewParcel += AvatarEnteringParcel;
  73. }
  74. public void RemoveRegion(Scene scene)
  75. {
  76. if (m_scenel.ContainsKey(scene.RegionInfo.RegionHandle))
  77. m_scenel.Remove(scene.RegionInfo.RegionHandle);
  78. scene.EventManager.OnAvatarKilled -= KillAvatar;
  79. scene.EventManager.OnAvatarEnteringNewParcel -= AvatarEnteringParcel;
  80. }
  81. public void RegionLoaded(Scene scene)
  82. {
  83. }
  84. public void PostInitialise()
  85. {
  86. }
  87. public void Close()
  88. {
  89. }
  90. public string Name
  91. {
  92. get { return "CombatModule"; }
  93. }
  94. public Type ReplaceableInterface
  95. {
  96. get { return null; }
  97. }
  98. private void KillAvatar(uint killerObjectLocalID, ScenePresence deadAvatar)
  99. {
  100. string deadAvatarMessage;
  101. ScenePresence killingAvatar = null;
  102. // string killingAvatarMessage;
  103. // check to see if it is an NPC and just remove it
  104. INPCModule NPCmodule = deadAvatar.Scene.RequestModuleInterface<INPCModule>();
  105. if (NPCmodule != null && NPCmodule.DeleteNPC(deadAvatar.UUID, deadAvatar.Scene))
  106. {
  107. return;
  108. }
  109. if (killerObjectLocalID == 0)
  110. deadAvatarMessage = "You committed suicide!";
  111. else
  112. {
  113. // Try to get the avatar responsible for the killing
  114. killingAvatar = deadAvatar.Scene.GetScenePresence(killerObjectLocalID);
  115. if (killingAvatar == null)
  116. {
  117. // Try to get the object which was responsible for the killing
  118. SceneObjectPart part = deadAvatar.Scene.GetSceneObjectPart(killerObjectLocalID);
  119. if (part == null)
  120. {
  121. // Cause of death: Unknown
  122. deadAvatarMessage = "You died!";
  123. }
  124. else
  125. {
  126. // Try to find the avatar wielding the killing object
  127. killingAvatar = deadAvatar.Scene.GetScenePresence(part.OwnerID);
  128. if (killingAvatar == null)
  129. {
  130. IUserManagement userManager = deadAvatar.Scene.RequestModuleInterface<IUserManagement>();
  131. string userName = "Unkown User";
  132. if (userManager != null)
  133. userName = userManager.GetUserName(part.OwnerID);
  134. deadAvatarMessage = String.Format("You impaled yourself on {0} owned by {1}!", part.Name, userName);
  135. }
  136. else
  137. {
  138. // killingAvatarMessage = String.Format("You fragged {0}!", deadAvatar.Name);
  139. deadAvatarMessage = String.Format("You got killed by {0}!", killingAvatar.Name);
  140. }
  141. }
  142. }
  143. else
  144. {
  145. // killingAvatarMessage = String.Format("You fragged {0}!", deadAvatar.Name);
  146. deadAvatarMessage = String.Format("You got killed by {0}!", killingAvatar.Name);
  147. }
  148. }
  149. try
  150. {
  151. deadAvatar.ControllingClient.SendAgentAlertMessage(deadAvatarMessage, true);
  152. if (killingAvatar != null)
  153. killingAvatar.ControllingClient.SendAlertMessage("You fragged " + deadAvatar.Firstname + " " + deadAvatar.Lastname);
  154. }
  155. catch (InvalidOperationException)
  156. { }
  157. deadAvatar.setHealthWithUpdate(100.0f);
  158. deadAvatar.Scene.TeleportClientHome(deadAvatar.UUID, deadAvatar.ControllingClient);
  159. }
  160. private void AvatarEnteringParcel(ScenePresence avatar, int localLandID, UUID regionID)
  161. {
  162. try
  163. {
  164. ILandObject obj = avatar.Scene.LandChannel.GetLandObject(avatar.AbsolutePosition.X, avatar.AbsolutePosition.Y);
  165. if (obj == null)
  166. return;
  167. if ((obj.LandData.Flags & (uint)ParcelFlags.AllowDamage) != 0
  168. || avatar.Scene.RegionInfo.RegionSettings.AllowDamage)
  169. {
  170. avatar.Invulnerable = false;
  171. }
  172. else
  173. {
  174. avatar.Invulnerable = true;
  175. if (avatar.Health < 100.0f)
  176. {
  177. avatar.setHealthWithUpdate(100.0f);
  178. }
  179. }
  180. }
  181. catch (Exception)
  182. {
  183. }
  184. }
  185. }
  186. }