EnemyCharge.java 3.6 KB

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