2
0

L2Territory.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2004-2013 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.model;
  20. import java.util.List;
  21. import java.util.logging.Logger;
  22. import javolution.util.FastList;
  23. import com.l2jserver.util.Rnd;
  24. /**
  25. * @version 0.1, 2005-03-12
  26. * @author Balancer
  27. */
  28. public class L2Territory
  29. {
  30. private static Logger _log = Logger.getLogger(L2Territory.class.getName());
  31. protected static class Point
  32. {
  33. protected int _x, _y, _zmin, _zmax, _proc;
  34. Point(int x, int y, int zmin, int zmax, int proc)
  35. {
  36. _x = x;
  37. _y = y;
  38. _zmin = zmin;
  39. _zmax = zmax;
  40. _proc = proc;
  41. }
  42. }
  43. private final List<Point> _points;
  44. private final int _terr;
  45. private int _xMin;
  46. private int _xMax;
  47. private int _yMin;
  48. private int _yMax;
  49. private int _zMin;
  50. private int _zMax;
  51. private int _procMax;
  52. public L2Territory(int terr)
  53. {
  54. _points = new FastList<>();
  55. _terr = terr;
  56. _xMin = 999999;
  57. _xMax = -999999;
  58. _yMin = 999999;
  59. _yMax = -999999;
  60. _zMin = 999999;
  61. _zMax = -999999;
  62. _procMax = 0;
  63. }
  64. public void add(int x, int y, int zmin, int zmax, int proc)
  65. {
  66. _points.add(new Point(x, y, zmin, zmax, proc));
  67. if (x < _xMin)
  68. {
  69. _xMin = x;
  70. }
  71. if (y < _yMin)
  72. {
  73. _yMin = y;
  74. }
  75. if (x > _xMax)
  76. {
  77. _xMax = x;
  78. }
  79. if (y > _yMax)
  80. {
  81. _yMax = y;
  82. }
  83. if (zmin < _zMin)
  84. {
  85. _zMin = zmin;
  86. }
  87. if (zmax > _zMax)
  88. {
  89. _zMax = zmax;
  90. }
  91. _procMax += proc;
  92. }
  93. public void print()
  94. {
  95. for (Point p : _points)
  96. {
  97. _log.info("(" + p._x + "," + p._y + ")");
  98. }
  99. }
  100. public boolean isIntersect(int x, int y, Point p1, Point p2)
  101. {
  102. double dy1 = p1._y - y;
  103. double dy2 = p2._y - y;
  104. if (Math.abs(Math.signum(dy1) - Math.signum(dy2)) <= 1e-6)
  105. {
  106. return false;
  107. }
  108. double dx1 = p1._x - x;
  109. double dx2 = p2._x - x;
  110. if ((dx1 >= 0) && (dx2 >= 0))
  111. {
  112. return true;
  113. }
  114. if ((dx1 < 0) && (dx2 < 0))
  115. {
  116. return false;
  117. }
  118. double dx0 = (dy1 * (p1._x - p2._x)) / (p1._y - p2._y);
  119. return dx0 <= dx1;
  120. }
  121. public boolean isInside(int x, int y)
  122. {
  123. int intersect_count = 0;
  124. for (int i = 0; i < _points.size(); i++)
  125. {
  126. Point p1 = _points.get(i > 0 ? i - 1 : _points.size() - 1);
  127. Point p2 = _points.get(i);
  128. if (isIntersect(x, y, p1, p2))
  129. {
  130. intersect_count++;
  131. }
  132. }
  133. return (intersect_count % 2) == 1;
  134. }
  135. public int[] getRandomPoint()
  136. {
  137. int[] p = new int[4];
  138. if (_procMax > 0)
  139. {
  140. int pos = 0;
  141. int rnd = Rnd.nextInt(_procMax);
  142. for (Point p1 : _points)
  143. {
  144. pos += p1._proc;
  145. if (rnd <= pos)
  146. {
  147. p[0] = p1._x;
  148. p[1] = p1._y;
  149. p[2] = p1._zmin;
  150. p[3] = p1._zmax;
  151. return p;
  152. }
  153. }
  154. }
  155. for (int i = 0; i < 100; i++)
  156. {
  157. p[0] = Rnd.get(_xMin, _xMax);
  158. p[1] = Rnd.get(_yMin, _yMax);
  159. if (isInside(p[0], p[1]))
  160. {
  161. double curdistance = 0;
  162. p[2] = _zMin + 100;
  163. p[3] = _zMax;
  164. for (Point p1 : _points)
  165. {
  166. double dx = p1._x - p[0];
  167. double dy = p1._y - p[1];
  168. double distance = Math.sqrt((dx * dx) + (dy * dy));
  169. if ((curdistance == 0) || (distance < curdistance))
  170. {
  171. curdistance = distance;
  172. p[2] = p1._zmin + 100;
  173. }
  174. }
  175. return p;
  176. }
  177. }
  178. _log.warning("Can't make point for territory " + _terr);
  179. return p;
  180. }
  181. public int getProcMax()
  182. {
  183. return _procMax;
  184. }
  185. }