ConditionSiegeZone.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.conditions;
  20. import com.l2jserver.gameserver.instancemanager.CastleManager;
  21. import com.l2jserver.gameserver.instancemanager.FortManager;
  22. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager;
  23. import com.l2jserver.gameserver.model.actor.L2Character;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.model.entity.Castle;
  26. import com.l2jserver.gameserver.model.entity.Fort;
  27. import com.l2jserver.gameserver.model.stats.Env;
  28. /**
  29. * The Class ConditionSiegeZone.
  30. * @author Gigiikun
  31. */
  32. public final class ConditionSiegeZone extends Condition
  33. {
  34. // conditional values
  35. public static final int COND_NOT_ZONE = 0x0001;
  36. public static final int COND_CAST_ATTACK = 0x0002;
  37. public static final int COND_CAST_DEFEND = 0x0004;
  38. public static final int COND_CAST_NEUTRAL = 0x0008;
  39. public static final int COND_FORT_ATTACK = 0x0010;
  40. public static final int COND_FORT_DEFEND = 0x0020;
  41. public static final int COND_FORT_NEUTRAL = 0x0040;
  42. public static final int COND_TW_CHANNEL = 0x0080;
  43. public static final int COND_TW_PROGRESS = 0x0100;
  44. private final int _value;
  45. private final boolean _self;
  46. /**
  47. * Instantiates a new condition siege zone.
  48. * @param value the value
  49. * @param self the self
  50. */
  51. public ConditionSiegeZone(int value, boolean self)
  52. {
  53. _value = value;
  54. _self = self;
  55. }
  56. @Override
  57. public boolean testImpl(Env env)
  58. {
  59. L2Character target = _self ? env.getCharacter() : env.getTarget();
  60. Castle castle = CastleManager.getInstance().getCastle(target);
  61. Fort fort = FortManager.getInstance().getFort(target);
  62. if (((_value & COND_TW_PROGRESS) != 0) && !TerritoryWarManager.getInstance().isTWInProgress())
  63. {
  64. return false;
  65. }
  66. else if (((_value & COND_TW_CHANNEL) != 0) && !TerritoryWarManager.getInstance().isTWChannelOpen())
  67. {
  68. return false;
  69. }
  70. else if ((castle == null) && (fort == null))
  71. {
  72. return (_value & COND_NOT_ZONE) != 0;
  73. }
  74. if (castle != null)
  75. {
  76. return checkIfOk(target, castle, _value);
  77. }
  78. return checkIfOk(target, fort, _value);
  79. }
  80. /**
  81. * Check if ok.
  82. * @param activeChar the active char
  83. * @param castle the castle
  84. * @param value the value
  85. * @return true, if successful
  86. */
  87. public static boolean checkIfOk(L2Character activeChar, Castle castle, int value)
  88. {
  89. if ((activeChar == null) || !(activeChar instanceof L2PcInstance))
  90. {
  91. return false;
  92. }
  93. L2PcInstance player = (L2PcInstance) activeChar;
  94. if (((castle == null) || (castle.getResidenceId() <= 0)))
  95. {
  96. if ((value & COND_NOT_ZONE) != 0)
  97. {
  98. return true;
  99. }
  100. }
  101. else if (!castle.getZone().isActive())
  102. {
  103. if ((value & COND_NOT_ZONE) != 0)
  104. {
  105. return true;
  106. }
  107. }
  108. else if (((value & COND_CAST_ATTACK) != 0) && player.isRegisteredOnThisSiegeField(castle.getResidenceId()) && (player.getSiegeState() == 1))
  109. {
  110. return true;
  111. }
  112. else if (((value & COND_CAST_DEFEND) != 0) && player.isRegisteredOnThisSiegeField(castle.getResidenceId()) && (player.getSiegeState() == 2))
  113. {
  114. return true;
  115. }
  116. else if (((value & COND_CAST_NEUTRAL) != 0) && (player.getSiegeState() == 0))
  117. {
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * Check if ok.
  124. * @param activeChar the active char
  125. * @param fort the fort
  126. * @param value the value
  127. * @return true, if successful
  128. */
  129. public static boolean checkIfOk(L2Character activeChar, Fort fort, int value)
  130. {
  131. if ((activeChar == null) || !(activeChar instanceof L2PcInstance))
  132. {
  133. return false;
  134. }
  135. L2PcInstance player = (L2PcInstance) activeChar;
  136. if (((fort == null) || (fort.getResidenceId() <= 0)))
  137. {
  138. if ((value & COND_NOT_ZONE) != 0)
  139. {
  140. return true;
  141. }
  142. }
  143. else if (!fort.getZone().isActive())
  144. {
  145. if ((value & COND_NOT_ZONE) != 0)
  146. {
  147. return true;
  148. }
  149. }
  150. else if (((value & COND_FORT_ATTACK) != 0) && player.isRegisteredOnThisSiegeField(fort.getResidenceId()) && (player.getSiegeState() == 1))
  151. {
  152. return true;
  153. }
  154. else if (((value & COND_FORT_DEFEND) != 0) && player.isRegisteredOnThisSiegeField(fort.getResidenceId()) && (player.getSiegeState() == 2))
  155. {
  156. return true;
  157. }
  158. else if (((value & COND_FORT_NEUTRAL) != 0) && (player.getSiegeState() == 0))
  159. {
  160. return true;
  161. }
  162. return false;
  163. }
  164. }