L2Territory.java 3.8 KB

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