123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- /*
- * Copyright (C) 2004-2015 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * L2J Server is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.model;
- import java.util.List;
- import java.util.concurrent.CopyOnWriteArrayList;
- import java.util.logging.Logger;
- import com.l2jserver.util.Rnd;
- /**
- * @version 0.1, 2005-03-12
- * @author Balancer
- */
- public class L2Territory
- {
- private static Logger _log = Logger.getLogger(L2Territory.class.getName());
-
- protected static class Point
- {
- protected int _x, _y, _zmin, _zmax, _proc;
-
- Point(int x, int y, int zmin, int zmax, int proc)
- {
- _x = x;
- _y = y;
- _zmin = zmin;
- _zmax = zmax;
- _proc = proc;
- }
- }
-
- private final List<Point> _points = new CopyOnWriteArrayList<>();
- private final int _terr;
- private int _xMin;
- private int _xMax;
- private int _yMin;
- private int _yMax;
- private int _zMin;
- private int _zMax;
- private int _procMax;
-
- public L2Territory(int terr)
- {
- _terr = terr;
- _xMin = 999999;
- _xMax = -999999;
- _yMin = 999999;
- _yMax = -999999;
- _zMin = 999999;
- _zMax = -999999;
- _procMax = 0;
- }
-
- public void add(int x, int y, int zmin, int zmax, int proc)
- {
- _points.add(new Point(x, y, zmin, zmax, proc));
- if (x < _xMin)
- {
- _xMin = x;
- }
- if (y < _yMin)
- {
- _yMin = y;
- }
- if (x > _xMax)
- {
- _xMax = x;
- }
- if (y > _yMax)
- {
- _yMax = y;
- }
- if (zmin < _zMin)
- {
- _zMin = zmin;
- }
- if (zmax > _zMax)
- {
- _zMax = zmax;
- }
- _procMax += proc;
- }
-
- public boolean isIntersect(int x, int y, Point p1, Point p2)
- {
- double dy1 = p1._y - y;
- double dy2 = p2._y - y;
-
- if (Math.abs(Math.signum(dy1) - Math.signum(dy2)) <= 1e-6)
- {
- return false;
- }
-
- double dx1 = p1._x - x;
- double dx2 = p2._x - x;
-
- if ((dx1 >= 0) && (dx2 >= 0))
- {
- return true;
- }
-
- if ((dx1 < 0) && (dx2 < 0))
- {
- return false;
- }
-
- double dx0 = (dy1 * (p1._x - p2._x)) / (p1._y - p2._y);
-
- return dx0 <= dx1;
- }
-
- public boolean isInside(int x, int y)
- {
- int intersect_count = 0;
- for (int i = 0; i < _points.size(); i++)
- {
- Point p1 = _points.get(i > 0 ? i - 1 : _points.size() - 1);
- Point p2 = _points.get(i);
-
- if (isIntersect(x, y, p1, p2))
- {
- intersect_count++;
- }
- }
-
- return (intersect_count % 2) == 1;
- }
-
- public Location getRandomPoint()
- {
- if (_procMax > 0)
- {
- int pos = 0;
- int rnd = Rnd.nextInt(_procMax);
- for (Point p1 : _points)
- {
- pos += p1._proc;
- if (rnd <= pos)
- {
- return new Location(p1._x, p1._y, Rnd.get(p1._zmin, p1._zmax));
- }
- }
-
- }
- for (int i = 0; i < 100; i++)
- {
- int x = Rnd.get(_xMin, _xMax);
- int y = Rnd.get(_yMin, _yMax);
- if (isInside(x, y))
- {
- double curdistance = 0;
- int zmin = _zMin;
- for (Point p1 : _points)
- {
- double dx = p1._x - x;
- double dy = p1._y - y;
- double distance = Math.sqrt((dx * dx) + (dy * dy));
- if ((curdistance == 0) || (distance < curdistance))
- {
- curdistance = distance;
- zmin = p1._zmin;
- }
- }
- return new Location(x, y, Rnd.get(zmin, _zMax));
- }
- }
- _log.warning("Can't make point for territory " + _terr);
- return null;
- }
-
- public int getProcMax()
- {
- return _procMax;
- }
- }
|