EnemyCharge.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.BUFF;
  43. }
  44. @Override
  45. public boolean onStart()
  46. {
  47. if (getEffected().isMovementDisabled())
  48. {
  49. return false;
  50. }
  51. // Get current position of the L2Character
  52. final int curX = getEffector().getX();
  53. final int curY = getEffector().getY();
  54. final int curZ = getEffector().getZ();
  55. // Calculate distance (dx,dy) between current position and destination
  56. double dx = getEffected().getX() - curX;
  57. double dy = getEffected().getY() - curY;
  58. double dz = getEffected().getZ() - curZ;
  59. double distance = Math.sqrt((dx * dx) + (dy * dy));
  60. if (distance > 2000)
  61. {
  62. _log.info("EffectEnemyCharge was going to use invalid coordinates for characters, getEffector: " + curX + "," + curY + " and getEffected: " + getEffected().getX() + "," + getEffected().getY());
  63. return false;
  64. }
  65. int offset = Math.max((int) distance - getSkill().getFlyRadius(), 30);
  66. double cos;
  67. double sin;
  68. // approximation for moving closer when z coordinates are different
  69. // TODO: handle Z axis movement better
  70. offset -= Math.abs(dz);
  71. if (offset < 5)
  72. {
  73. offset = 5;
  74. }
  75. // If no distance
  76. if ((distance < 1) || ((distance - offset) <= 0))
  77. {
  78. return false;
  79. }
  80. // Calculate movement angles needed
  81. sin = dy / distance;
  82. cos = dx / distance;
  83. // Calculate the new destination with offset included
  84. _x = curX + (int) ((distance - offset) * cos);
  85. _y = curY + (int) ((distance - offset) * sin);
  86. _z = getEffected().getZ();
  87. if (Config.GEODATA > 0)
  88. {
  89. Location destiny = GeoData.getInstance().moveCheck(getEffector().getX(), getEffector().getY(), getEffector().getZ(), _x, _y, _z, getEffector().getInstanceId());
  90. _x = destiny.getX();
  91. _y = destiny.getY();
  92. }
  93. getEffector().broadcastPacket(new FlyToLocation(getEffector(), _x, _y, _z, FlyType.CHARGE));
  94. // maybe is need force set X,Y,Z
  95. getEffector().setXYZ(_x, _y, _z);
  96. getEffector().broadcastPacket(new ValidateLocation(getEffector()));
  97. return true;
  98. }
  99. }