GeoUtils.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2004-2015 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.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.network.serverpackets.ExServerPrimitive;
  24. import com.l2jserver.geodriver.Cell;
  25. /**
  26. * @author HorridoJoho
  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. private static Color getDirectionColor(int x, int y, int z, int nswe)
  80. {
  81. if (GeoData.getInstance().checkNearestNswe(x, y, z, nswe, 100))
  82. {
  83. return Color.GREEN;
  84. }
  85. return Color.RED;
  86. }
  87. public static void debugGrid(L2PcInstance player)
  88. {
  89. int geoRadius = 10;
  90. int blocksPerPacket = 49;
  91. if (geoRadius < 0)
  92. {
  93. throw new IllegalArgumentException("geoRadius < 0");
  94. }
  95. int iBlock = blocksPerPacket;
  96. int iPacket = 0;
  97. ExServerPrimitive exsp = null;
  98. GeoData gd = GeoData.getInstance();
  99. int playerGx = gd.getGeoX(player.getX());
  100. int playerGy = gd.getGeoY(player.getY());
  101. for (int dx = -geoRadius; dx <= geoRadius; ++dx)
  102. {
  103. for (int dy = -geoRadius; dy <= geoRadius; ++dy)
  104. {
  105. if (iBlock >= blocksPerPacket)
  106. {
  107. iBlock = 0;
  108. if (exsp != null)
  109. {
  110. ++iPacket;
  111. player.sendPacket(exsp);
  112. }
  113. exsp = new ExServerPrimitive("DebugGrid_" + iPacket, player.getX(), player.getY(), -16000);
  114. }
  115. if (exsp == null)
  116. {
  117. throw new IllegalStateException();
  118. }
  119. int gx = playerGx + dx;
  120. int gy = playerGy + dy;
  121. int x = gd.getWorldX(gx);
  122. int y = gd.getWorldY(gy);
  123. int z = gd.getNearestZ(gx, gy, player.getZ());
  124. // north arrow
  125. Color col = getDirectionColor(gx, gy, z, Cell.NSWE_NORTH);
  126. exsp.addLine(col, x - 1, y - 7, z, x + 1, y - 7, z);
  127. exsp.addLine(col, x - 2, y - 6, z, x + 2, y - 6, z);
  128. exsp.addLine(col, x - 3, y - 5, z, x + 3, y - 5, z);
  129. exsp.addLine(col, x - 4, y - 4, z, x + 4, y - 4, z);
  130. // east arrow
  131. col = getDirectionColor(gx, gy, z, Cell.NSWE_EAST);
  132. exsp.addLine(col, x + 7, y - 1, z, x + 7, y + 1, z);
  133. exsp.addLine(col, x + 6, y - 2, z, x + 6, y + 2, z);
  134. exsp.addLine(col, x + 5, y - 3, z, x + 5, y + 3, z);
  135. exsp.addLine(col, x + 4, y - 4, z, x + 4, y + 4, z);
  136. // south arrow
  137. col = getDirectionColor(gx, gy, z, Cell.NSWE_SOUTH);
  138. exsp.addLine(col, x - 1, y + 7, z, x + 1, y + 7, z);
  139. exsp.addLine(col, x - 2, y + 6, z, x + 2, y + 6, z);
  140. exsp.addLine(col, x - 3, y + 5, z, x + 3, y + 5, z);
  141. exsp.addLine(col, x - 4, y + 4, z, x + 4, y + 4, z);
  142. col = getDirectionColor(gx, gy, z, Cell.NSWE_WEST);
  143. exsp.addLine(col, x - 7, y - 1, z, x - 7, y + 1, z);
  144. exsp.addLine(col, x - 6, y - 2, z, x - 6, y + 2, z);
  145. exsp.addLine(col, x - 5, y - 3, z, x - 5, y + 3, z);
  146. exsp.addLine(col, x - 4, y - 4, z, x - 4, y + 4, z);
  147. ++iBlock;
  148. }
  149. }
  150. player.sendPacket(exsp);
  151. }
  152. /**
  153. * difference between x values: never above 1<br>
  154. * difference between y values: never above 1
  155. * @param lastX
  156. * @param lastY
  157. * @param x
  158. * @param y
  159. * @return
  160. */
  161. public static int computeNswe(int lastX, int lastY, int x, int y)
  162. {
  163. if (x > lastX) // east
  164. {
  165. if (y > lastY)
  166. {
  167. return Cell.NSWE_SOUTH_EAST;// Direction.SOUTH_EAST;
  168. }
  169. else if (y < lastY)
  170. {
  171. return Cell.NSWE_NORTH_EAST;// Direction.NORTH_EAST;
  172. }
  173. else
  174. {
  175. return Cell.NSWE_EAST;// Direction.EAST;
  176. }
  177. }
  178. else if (x < lastX) // west
  179. {
  180. if (y > lastY)
  181. {
  182. return Cell.NSWE_SOUTH_WEST;// Direction.SOUTH_WEST;
  183. }
  184. else if (y < lastY)
  185. {
  186. return Cell.NSWE_NORTH_WEST;// Direction.NORTH_WEST;
  187. }
  188. else
  189. {
  190. return Cell.NSWE_WEST;// Direction.WEST;
  191. }
  192. }
  193. else
  194. // unchanged x
  195. {
  196. if (y > lastY)
  197. {
  198. return Cell.NSWE_SOUTH;// Direction.SOUTH;
  199. }
  200. else if (y < lastY)
  201. {
  202. return Cell.NSWE_NORTH;// Direction.NORTH;
  203. }
  204. else
  205. {
  206. throw new RuntimeException();
  207. }
  208. }
  209. }
  210. }