CombatModule.cs 7.9 KB

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