GeoUtils.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.util;
  20. import java.awt.Color;
  21. import com.l2jserver.gameserver.GeoData;
  22. import com.l2jserver.gameserver.geoengine.Direction;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.network.serverpackets.ExServerPrimitive;
  25. /**
  26. * @author FBIagent
  27. */
  28. public final class GeoUtils
  29. {
  30. public static void debug2DLine(L2PcInstance player, int x, int y, int tx, int ty, int z)
  31. {
  32. int gx = GeoData.getInstance().getGeoX(x);
  33. int gy = GeoData.getInstance().getGeoY(y);
  34. int tgx = GeoData.getInstance().getGeoX(tx);
  35. int tgy = GeoData.getInstance().getGeoY(ty);
  36. ExServerPrimitive prim = new ExServerPrimitive("Debug2DLine", x, y, z);
  37. prim.addLine(Color.BLUE, GeoData.getInstance().getWorldX(gx), GeoData.getInstance().getWorldY(gy), z, GeoData.getInstance().getWorldX(tgx), GeoData.getInstance().getWorldY(tgy), z);
  38. LinePointIterator iter = new LinePointIterator(gx, gy, tgx, tgy);
  39. while (iter.next())
  40. {
  41. int wx = GeoData.getInstance().getWorldX(iter.x());
  42. int wy = GeoData.getInstance().getWorldY(iter.y());
  43. prim.addPoint(Color.RED, wx, wy, z);
  44. }
  45. player.sendPacket(prim);
  46. }
  47. public static void debug3DLine(L2PcInstance player, int x, int y, int z, int tx, int ty, int tz)
  48. {
  49. int gx = GeoData.getInstance().getGeoX(x);
  50. int gy = GeoData.getInstance().getGeoY(y);
  51. int tgx = GeoData.getInstance().getGeoX(tx);
  52. int tgy = GeoData.getInstance().getGeoY(ty);
  53. ExServerPrimitive prim = new ExServerPrimitive("Debug3DLine", x, y, z);
  54. prim.addLine(Color.BLUE, GeoData.getInstance().getWorldX(gx), GeoData.getInstance().getWorldY(gy), z, GeoData.getInstance().getWorldX(tgx), GeoData.getInstance().getWorldY(tgy), tz);
  55. LinePointIterator3D iter = new LinePointIterator3D(gx, gy, z, tgx, tgy, tz);
  56. iter.next();
  57. int prevX = iter.x();
  58. int prevY = iter.y();
  59. int wx = GeoData.getInstance().getWorldX(prevX);
  60. int wy = GeoData.getInstance().getWorldY(prevY);
  61. int wz = iter.z();
  62. prim.addPoint(Color.RED, wx, wy, wz);
  63. while (iter.next())
  64. {
  65. int curX = iter.x();
  66. int curY = iter.y();
  67. if ((curX != prevX) || (curY != prevY))
  68. {
  69. wx = GeoData.getInstance().getWorldX(curX);
  70. wy = GeoData.getInstance().getWorldY(curY);
  71. wz = iter.z();
  72. prim.addPoint(Color.RED, wx, wy, wz);
  73. prevX = curX;
  74. prevY = curY;
  75. }
  76. }
  77. player.sendPacket(prim);
  78. }
  79. /**
  80. * difference between x values: never abover 1<br>
  81. * difference between y values: never above 1
  82. * @param lastX
  83. * @param lastY
  84. * @param x
  85. * @param y
  86. * @return
  87. */
  88. public static Direction computeDirection(int lastX, int lastY, int x, int y)
  89. {
  90. if (x > lastX) // east
  91. {
  92. if (y > lastY)
  93. {
  94. return Direction.SOUTH_EAST;
  95. }
  96. else if (y < lastY)
  97. {
  98. return Direction.NORTH_EAST;
  99. }
  100. else
  101. {
  102. return Direction.EAST;
  103. }
  104. }
  105. else if (x < lastX) // west
  106. {
  107. if (y > lastY)
  108. {
  109. return Direction.SOUTH_WEST;
  110. }
  111. else if (y < lastY)
  112. {
  113. return Direction.NORTH_WEST;
  114. }
  115. else
  116. {
  117. return Direction.WEST;
  118. }
  119. }
  120. else
  121. // unchanged x
  122. {
  123. if (y > lastY)
  124. {
  125. return Direction.SOUTH;
  126. }
  127. else if (y < lastY)
  128. {
  129. return Direction.NORTH;
  130. }
  131. else
  132. {
  133. return null;// error, should never happen, TODO: Logging
  134. }
  135. }
  136. }
  137. }