Broadcast.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * $Header: Broadcast.java, 18/11/2005 15:33:35 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $
  5. * $Date: 18/11/2005 15:33:35 $
  6. * $Revision: 1 $
  7. * $Log: Broadcast.java,v $
  8. * Revision 1 18/11/2005 15:33:35 luisantonioa
  9. * Added copyright notice
  10. *
  11. *
  12. * This program is free software: you can redistribute it and/or modify it under
  13. * the terms of the GNU General Public License as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option) any later
  15. * version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. package net.sf.l2j.gameserver.util;
  26. import java.util.logging.Logger;
  27. import net.sf.l2j.Config;
  28. import net.sf.l2j.gameserver.model.L2Character;
  29. import net.sf.l2j.gameserver.model.L2World;
  30. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  31. import net.sf.l2j.gameserver.serverpackets.CharInfo;
  32. import net.sf.l2j.gameserver.serverpackets.L2GameServerPacket;
  33. import net.sf.l2j.gameserver.serverpackets.RelationChanged;
  34. /**
  35. * This class ...
  36. *
  37. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  38. */
  39. public final class Broadcast
  40. {
  41. private static Logger _log = Logger.getLogger(Broadcast.class.getName());
  42. /**
  43. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character that have the Character targetted.<BR><BR>
  44. *
  45. * <B><U> Concept</U> :</B><BR>
  46. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  47. * In order to inform other players of state modification on the L2Character, server just need to go through _knownPlayers to send Server->Client Packet<BR><BR>
  48. *
  49. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packet to this L2Character (to do this use method toSelfAndKnownPlayers)</B></FONT><BR><BR>
  50. *
  51. */
  52. public static void toPlayersTargettingMyself(L2Character character, L2GameServerPacket mov)
  53. {
  54. if (Config.DEBUG) _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:"+mov.getType());
  55. for (L2PcInstance player : character.getKnownList().getKnownPlayers().values())
  56. {
  57. if (player == null || player.getTarget() != character)
  58. continue;
  59. player.sendPacket(mov);
  60. }
  61. }
  62. /**
  63. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character.<BR><BR>
  64. *
  65. * <B><U> Concept</U> :</B><BR>
  66. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  67. * In order to inform other players of state modification on the L2Character, server just need to go through _knownPlayers to send Server->Client Packet<BR><BR>
  68. *
  69. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packet to this L2Character (to do this use method toSelfAndKnownPlayers)</B></FONT><BR><BR>
  70. *
  71. */
  72. public static void toKnownPlayers(L2Character character, L2GameServerPacket mov)
  73. {
  74. if (Config.DEBUG) _log.fine("players to notify:" + character.getKnownList().getKnownPlayers().size() + " packet:"+mov.getType());
  75. for (L2PcInstance player : character.getKnownList().getKnownPlayers().values())
  76. {
  77. try
  78. {
  79. player.sendPacket(mov);
  80. if (mov instanceof CharInfo && character instanceof L2PcInstance)
  81. {
  82. int relation = ((L2PcInstance)character).getRelation(player);
  83. if (character.getKnownList().getKnownRelations().get(player.getObjectId()) != null && character.getKnownList().getKnownRelations().get(player.getObjectId()) != relation)
  84. player.sendPacket(new RelationChanged((L2PcInstance)character, relation, player.isAutoAttackable(character)));
  85. }
  86. }
  87. catch (NullPointerException e) { }
  88. }
  89. }
  90. /**
  91. * Send a packet to all L2PcInstance in the _KnownPlayers (in the specified radius) of the L2Character.<BR><BR>
  92. *
  93. * <B><U> Concept</U> :</B><BR>
  94. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  95. * In order to inform other players of state modification on the L2Character, server just needs to go through _knownPlayers to send Server->Client Packet
  96. * and check the distance between the targets.<BR><BR>
  97. *
  98. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packet to this L2Character (to do this use method toSelfAndKnownPlayers)</B></FONT><BR><BR>
  99. *
  100. */
  101. public static void toKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, int radius)
  102. {
  103. if (radius < 0)
  104. radius = 1500;
  105. for (L2PcInstance player : character.getKnownList().getKnownPlayers().values())
  106. {
  107. if (player == null)
  108. continue;
  109. if (character.isInsideRadius(player, radius, false, false))
  110. player.sendPacket(mov);
  111. }
  112. }
  113. /**
  114. * Send a packet to all L2PcInstance in the _KnownPlayers of the L2Character and to the specified character.<BR><BR>
  115. *
  116. * <B><U> Concept</U> :</B><BR>
  117. * L2PcInstance in the detection area of the L2Character are identified in <B>_knownPlayers</B>.<BR>
  118. * In order to inform other players of state modification on the L2Character, server just need to go through _knownPlayers to send Server->Client Packet<BR><BR>
  119. *
  120. */
  121. public static void toSelfAndKnownPlayers(L2Character character, L2GameServerPacket mov)
  122. {
  123. if (character instanceof L2PcInstance)
  124. {
  125. character.sendPacket(mov);
  126. }
  127. toKnownPlayers(character, mov);
  128. }
  129. // To improve performance we are comparing values of radius^2 instead of calculating sqrt all the time
  130. public static void toSelfAndKnownPlayersInRadius(L2Character character, L2GameServerPacket mov, long radiusSq)
  131. {
  132. if (radiusSq < 0) radiusSq = 360000;
  133. if (character instanceof L2PcInstance) character.sendPacket(mov);
  134. for (L2PcInstance player : character.getKnownList().getKnownPlayers().values())
  135. {
  136. if (player != null && character.getDistanceSq(player) <= radiusSq) player.sendPacket(mov);
  137. }
  138. }
  139. /**
  140. * Send a packet to all L2PcInstance present in the world.<BR><BR>
  141. *
  142. * <B><U> Concept</U> :</B><BR>
  143. * In order to inform other players of state modification on the L2Character, server just need to go through _allPlayers to send Server->Client Packet<BR><BR>
  144. *
  145. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packet to this L2Character (to do this use method toSelfAndKnownPlayers)</B></FONT><BR><BR>
  146. *
  147. */
  148. public static void toAllOnlinePlayers(L2GameServerPacket mov)
  149. {
  150. if (Config.DEBUG) _log.fine("Players to notify: " + L2World.getInstance().getAllPlayersCount() + " (with packet " + mov.getType() + ")");
  151. for (L2PcInstance onlinePlayer : L2World.getInstance().getAllPlayers())
  152. {
  153. if (onlinePlayer == null)
  154. continue;
  155. onlinePlayer.sendPacket(mov);
  156. }
  157. }
  158. }