ConditionSiegeZone.java 5.0 KB

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