Fishing.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack 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 DataPack 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 handlers.effecthandlers;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.GeoData;
  22. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  23. import com.l2jserver.gameserver.model.PcCondOverride;
  24. import com.l2jserver.gameserver.model.StatsSet;
  25. import com.l2jserver.gameserver.model.actor.L2Character;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.model.conditions.Condition;
  28. import com.l2jserver.gameserver.model.effects.AbstractEffect;
  29. import com.l2jserver.gameserver.model.effects.L2EffectType;
  30. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  31. import com.l2jserver.gameserver.model.items.L2Weapon;
  32. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  33. import com.l2jserver.gameserver.model.items.type.EtcItemType;
  34. import com.l2jserver.gameserver.model.items.type.WeaponType;
  35. import com.l2jserver.gameserver.model.skills.BuffInfo;
  36. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  37. import com.l2jserver.gameserver.model.zone.ZoneId;
  38. import com.l2jserver.gameserver.model.zone.type.L2FishingZone;
  39. import com.l2jserver.gameserver.model.zone.type.L2WaterZone;
  40. import com.l2jserver.gameserver.network.SystemMessageId;
  41. import com.l2jserver.gameserver.util.Util;
  42. import com.l2jserver.util.Rnd;
  43. /**
  44. * Fishing effect implementation.
  45. * @author UnAfraid
  46. */
  47. public final class Fishing extends AbstractEffect
  48. {
  49. private static final int MIN_BAIT_DISTANCE = 90;
  50. private static final int MAX_BAIT_DISTANCE = 250;
  51. public Fishing(Condition attachCond, Condition applyCond, StatsSet set, StatsSet params)
  52. {
  53. super(attachCond, applyCond, set, params);
  54. }
  55. @Override
  56. public boolean isInstant()
  57. {
  58. return true;
  59. }
  60. @Override
  61. public L2EffectType getEffectType()
  62. {
  63. return L2EffectType.FISHING_START;
  64. }
  65. @Override
  66. public void onStart(BuffInfo info)
  67. {
  68. final L2Character activeChar = info.getEffector();
  69. if (!activeChar.isPlayer())
  70. {
  71. return;
  72. }
  73. final L2PcInstance player = activeChar.getActingPlayer();
  74. if (!Config.ALLOWFISHING && !player.canOverrideCond(PcCondOverride.SKILL_CONDITIONS))
  75. {
  76. player.sendMessage("Fishing is disabled!");
  77. return;
  78. }
  79. if (player.isFishing())
  80. {
  81. if (player.getFishCombat() != null)
  82. {
  83. player.getFishCombat().doDie(false);
  84. }
  85. else
  86. {
  87. player.endFishing(false);
  88. }
  89. player.sendPacket(SystemMessageId.FISHING_ATTEMPT_CANCELLED);
  90. return;
  91. }
  92. // check for equiped fishing rod
  93. L2Weapon equipedWeapon = player.getActiveWeaponItem();
  94. if (((equipedWeapon == null) || (equipedWeapon.getItemType() != WeaponType.FISHINGROD)))
  95. {
  96. player.sendPacket(SystemMessageId.FISHING_POLE_NOT_EQUIPPED);
  97. return;
  98. }
  99. // check for equiped lure
  100. L2ItemInstance equipedLeftHand = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LHAND);
  101. if ((equipedLeftHand == null) || (equipedLeftHand.getItemType() != EtcItemType.LURE))
  102. {
  103. player.sendPacket(SystemMessageId.BAIT_ON_HOOK_BEFORE_FISHING);
  104. return;
  105. }
  106. if (!player.isGM())
  107. {
  108. if (player.isInBoat())
  109. {
  110. player.sendPacket(SystemMessageId.CANNOT_FISH_ON_BOAT);
  111. return;
  112. }
  113. if (player.isInCraftMode() || player.isInStoreMode())
  114. {
  115. player.sendPacket(SystemMessageId.CANNOT_FISH_WHILE_USING_RECIPE_BOOK);
  116. return;
  117. }
  118. if (player.isInsideZone(ZoneId.WATER))
  119. {
  120. player.sendPacket(SystemMessageId.CANNOT_FISH_UNDER_WATER);
  121. return;
  122. }
  123. }
  124. // calculate a position in front of the player with a random distance
  125. int distance = Rnd.get(MIN_BAIT_DISTANCE, MAX_BAIT_DISTANCE);
  126. final double angle = Util.convertHeadingToDegree(player.getHeading());
  127. final double radian = Math.toRadians(angle);
  128. final double sin = Math.sin(radian);
  129. final double cos = Math.cos(radian);
  130. int baitX = (int) (player.getX() + (cos * distance));
  131. int baitY = (int) (player.getY() + (sin * distance));
  132. // search for fishing and water zone
  133. L2FishingZone fishingZone = null;
  134. L2WaterZone waterZone = null;
  135. for (final L2ZoneType zone : ZoneManager.getInstance().getZones(baitX, baitY))
  136. {
  137. if (zone instanceof L2FishingZone)
  138. {
  139. fishingZone = (L2FishingZone) zone;
  140. }
  141. else if (zone instanceof L2WaterZone)
  142. {
  143. waterZone = (L2WaterZone) zone;
  144. }
  145. if ((fishingZone != null) && (waterZone != null))
  146. {
  147. break;
  148. }
  149. }
  150. int baitZ = computeBaitZ(player, baitX, baitY, fishingZone, waterZone);
  151. if (baitZ == Integer.MIN_VALUE)
  152. {
  153. for (distance = MAX_BAIT_DISTANCE; distance >= MIN_BAIT_DISTANCE; --distance)
  154. {
  155. baitX = (int) (player.getX() + (cos * distance));
  156. baitY = (int) (player.getY() + (sin * distance));
  157. // search for fishing and water zone again
  158. fishingZone = null;
  159. waterZone = null;
  160. for (final L2ZoneType zone : ZoneManager.getInstance().getZones(baitX, baitY))
  161. {
  162. if (zone instanceof L2FishingZone)
  163. {
  164. fishingZone = (L2FishingZone) zone;
  165. }
  166. else if (zone instanceof L2WaterZone)
  167. {
  168. waterZone = (L2WaterZone) zone;
  169. }
  170. if ((fishingZone != null) && (waterZone != null))
  171. {
  172. break;
  173. }
  174. }
  175. baitZ = computeBaitZ(player, baitX, baitY, fishingZone, waterZone);
  176. if (baitZ != Integer.MIN_VALUE)
  177. {
  178. break;
  179. }
  180. }
  181. if (baitZ == Integer.MIN_VALUE)
  182. {
  183. if (player.isGM())
  184. {
  185. baitZ = player.getZ();
  186. }
  187. else
  188. {
  189. player.sendPacket(SystemMessageId.CANNOT_FISH_HERE);
  190. return;
  191. }
  192. }
  193. }
  194. if (!player.destroyItem("Fishing", equipedLeftHand, 1, null, false))
  195. {
  196. player.sendPacket(SystemMessageId.NOT_ENOUGH_BAIT);
  197. return;
  198. }
  199. player.setLure(equipedLeftHand);
  200. player.startFishing(baitX, baitY, baitZ);
  201. }
  202. /**
  203. * Computes the Z of the bait.
  204. * @param player the player
  205. * @param baitX the bait x
  206. * @param baitY the bait y
  207. * @param fishingZone the fishing zone
  208. * @param waterZone the water zone
  209. * @return the bait z or {@link Integer#MIN_VALUE} when you cannot fish here
  210. */
  211. private static int computeBaitZ(final L2PcInstance player, final int baitX, final int baitY, final L2FishingZone fishingZone, final L2WaterZone waterZone)
  212. {
  213. if ((fishingZone == null))
  214. {
  215. return Integer.MIN_VALUE;
  216. }
  217. if ((waterZone == null))
  218. {
  219. return Integer.MIN_VALUE;
  220. }
  221. // always use water zone, fishing zone high z is high in the air...
  222. int baitZ = waterZone.getWaterZ();
  223. if (!GeoData.getInstance().canSeeTarget(player.getX(), player.getY(), player.getZ(), baitX, baitY, baitZ))
  224. {
  225. return Integer.MIN_VALUE;
  226. }
  227. if (GeoData.getInstance().hasGeo(baitX, baitY))
  228. {
  229. if (GeoData.getInstance().getHeight(baitX, baitY, baitZ) > baitZ)
  230. {
  231. return Integer.MIN_VALUE;
  232. }
  233. if (GeoData.getInstance().getHeight(baitX, baitY, player.getZ()) > baitZ)
  234. {
  235. return Integer.MIN_VALUE;
  236. }
  237. }
  238. return baitZ;
  239. }
  240. }