EnemyCharge.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2004-2013 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 java.util.logging.Logger;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.GeoData;
  23. import com.l2jserver.gameserver.model.Location;
  24. import com.l2jserver.gameserver.model.effects.EffectTemplate;
  25. import com.l2jserver.gameserver.model.effects.L2Effect;
  26. import com.l2jserver.gameserver.model.effects.L2EffectType;
  27. import com.l2jserver.gameserver.model.stats.Env;
  28. import com.l2jserver.gameserver.network.serverpackets.FlyToLocation;
  29. import com.l2jserver.gameserver.network.serverpackets.FlyToLocation.FlyType;
  30. import com.l2jserver.gameserver.network.serverpackets.ValidateLocation;
  31. public class EnemyCharge extends L2Effect
  32. {
  33. static final Logger _log = Logger.getLogger(EnemyCharge.class.getName());
  34. private int _x, _y, _z;
  35. public EnemyCharge(Env env, EffectTemplate template)
  36. {
  37. super(env, template);
  38. }
  39. @Override
  40. public L2EffectType getEffectType()
  41. {
  42. return L2EffectType.NONE;
  43. }
  44. @Override
  45. public boolean isInstant()
  46. {
  47. return true;
  48. }
  49. @Override
  50. public boolean onStart()
  51. {
  52. if (getEffected().isMovementDisabled())
  53. {
  54. return false;
  55. }
  56. // Get current position of the L2Character
  57. final int curX = getEffector().getX();
  58. final int curY = getEffector().getY();
  59. final int curZ = getEffector().getZ();
  60. // Calculate distance (dx,dy) between current position and destination
  61. double dx = getEffected().getX() - curX;
  62. double dy = getEffected().getY() - curY;
  63. double dz = getEffected().getZ() - curZ;
  64. double distance = Math.sqrt((dx * dx) + (dy * dy));
  65. if (distance > 2000)
  66. {
  67. _log.info("EffectEnemyCharge was going to use invalid coordinates for characters, getEffector: " + curX + "," + curY + " and getEffected: " + getEffected().getX() + "," + getEffected().getY());
  68. return false;
  69. }
  70. int offset = Math.max((int) distance - getSkill().getFlyRadius(), 30);
  71. double cos;
  72. double sin;
  73. // approximation for moving closer when z coordinates are different
  74. // TODO: handle Z axis movement better
  75. offset -= Math.abs(dz);
  76. if (offset < 5)
  77. {
  78. offset = 5;
  79. }
  80. // If no distance
  81. if ((distance < 1) || ((distance - offset) <= 0))
  82. {
  83. return false;
  84. }
  85. // Calculate movement angles needed
  86. sin = dy / distance;
  87. cos = dx / distance;
  88. // Calculate the new destination with offset included
  89. _x = curX + (int) ((distance - offset) * cos);
  90. _y = curY + (int) ((distance - offset) * sin);
  91. _z = getEffected().getZ();
  92. if (Config.GEODATA > 0)
  93. {
  94. Location destiny = GeoData.getInstance().moveCheck(getEffector().getX(), getEffector().getY(), getEffector().getZ(), _x, _y, _z, getEffector().getInstanceId());
  95. _x = destiny.getX();
  96. _y = destiny.getY();
  97. }
  98. getEffector().broadcastPacket(new FlyToLocation(getEffector(), _x, _y, _z, FlyType.CHARGE));
  99. // maybe is need force set X,Y,Z
  100. getEffector().setXYZ(_x, _y, _z);
  101. getEffector().broadcastPacket(new ValidateLocation(getEffector()));
  102. return true;
  103. }
  104. }