CombatModule.cs 6.8 KB

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