2
0

ZoneManager.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. package com.l2jserver.gameserver.instancemanager;
  16. import java.lang.reflect.Constructor;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.gameserver.engines.DocumentParser;
  27. import com.l2jserver.gameserver.model.L2Object;
  28. import com.l2jserver.gameserver.model.L2World;
  29. import com.l2jserver.gameserver.model.L2WorldRegion;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  32. import com.l2jserver.gameserver.model.zone.AbstractZoneSettings;
  33. import com.l2jserver.gameserver.model.zone.L2ZoneRespawn;
  34. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  35. import com.l2jserver.gameserver.model.zone.form.ZoneCuboid;
  36. import com.l2jserver.gameserver.model.zone.form.ZoneCylinder;
  37. import com.l2jserver.gameserver.model.zone.form.ZoneNPoly;
  38. import com.l2jserver.gameserver.model.zone.type.L2ArenaZone;
  39. import com.l2jserver.gameserver.model.zone.type.L2OlympiadStadiumZone;
  40. import com.l2jserver.gameserver.model.zone.type.L2RespawnZone;
  41. /**
  42. * This class manages the zones
  43. * @author durgus
  44. */
  45. public class ZoneManager extends DocumentParser
  46. {
  47. private static final Map<String, AbstractZoneSettings> _settings = new HashMap<>();
  48. private final Map<Class<? extends L2ZoneType>, Map<Integer, ? extends L2ZoneType>> _classZones = new HashMap<>();
  49. private int _lastDynamicId = 300000;
  50. private List<L2ItemInstance> _debugItems;
  51. /**
  52. * @return
  53. */
  54. public static final ZoneManager getInstance()
  55. {
  56. return SingletonHolder._instance;
  57. }
  58. protected ZoneManager()
  59. {
  60. load();
  61. }
  62. public void reload()
  63. {
  64. // Get the world regions
  65. int count = 0;
  66. L2WorldRegion[][] worldRegions = L2World.getInstance().getAllWorldRegions();
  67. // Backup old zone settings
  68. for (Map<Integer, ? extends L2ZoneType> map : _classZones.values())
  69. {
  70. for (L2ZoneType zone : map.values())
  71. {
  72. if (zone.getSettings() != null)
  73. {
  74. _settings.put(zone.getName(), zone.getSettings());
  75. }
  76. }
  77. }
  78. // Clear zones
  79. for (int x = 0; x < worldRegions.length; x++)
  80. {
  81. for (int y = 0; y < worldRegions[x].length; y++)
  82. {
  83. worldRegions[x][y].getZones().clear();
  84. count++;
  85. }
  86. }
  87. GrandBossManager.getInstance().getZones().clear();
  88. _log.info(getClass().getSimpleName() + ": Removed zones in " + count + " regions.");
  89. // Load the zones
  90. load();
  91. // Re-validate all characters in zones
  92. for (L2Object obj : L2World.getInstance().getAllVisibleObjectsArray())
  93. {
  94. if (obj instanceof L2Character)
  95. {
  96. ((L2Character) obj).revalidateZone(true);
  97. }
  98. }
  99. _settings.clear();
  100. }
  101. @Override
  102. protected void parseDocument()
  103. {
  104. // Get the world regions
  105. L2WorldRegion[][] worldRegions = L2World.getInstance().getAllWorldRegions();
  106. NamedNodeMap attrs;
  107. Node attribute;
  108. String zoneName;
  109. int[][] coords;
  110. int zoneId, minZ, maxZ;
  111. String zoneType, zoneShape;
  112. List<int[]> rs = new ArrayList<>();
  113. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  114. {
  115. if ("list".equalsIgnoreCase(n.getNodeName()))
  116. {
  117. attrs = n.getAttributes();
  118. attribute = attrs.getNamedItem("enabled");
  119. if (attribute != null && !Boolean.parseBoolean(attribute.getNodeValue()))
  120. continue;
  121. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  122. {
  123. if ("zone".equalsIgnoreCase(d.getNodeName()))
  124. {
  125. attrs = d.getAttributes();
  126. attribute = attrs.getNamedItem("id");
  127. if (attribute != null)
  128. zoneId = Integer.parseInt(attribute.getNodeValue());
  129. else
  130. zoneId = _lastDynamicId++;
  131. attribute = attrs.getNamedItem("name");
  132. if (attribute != null)
  133. zoneName = attribute.getNodeValue();
  134. else
  135. zoneName = null;
  136. minZ = parseInt(attrs, "minZ");
  137. maxZ = parseInt(attrs, "maxZ");
  138. zoneType = attrs.getNamedItem("type").getNodeValue();
  139. zoneShape = attrs.getNamedItem("shape").getNodeValue();
  140. // Create the zone
  141. Class<?> newZone = null;
  142. Constructor<?> zoneConstructor = null;
  143. L2ZoneType temp = null;
  144. try
  145. {
  146. newZone = Class.forName("com.l2jserver.gameserver.model.zone.type.L2" + zoneType);
  147. zoneConstructor = newZone.getConstructor(int.class);
  148. temp = (L2ZoneType) zoneConstructor.newInstance(zoneId);
  149. }
  150. catch (Exception e)
  151. {
  152. _log.warning(getClass().getSimpleName() + ": ZoneData: No such zone type: " + zoneType + " in file: " + getCurrentFile().getName());
  153. continue;
  154. }
  155. // Get the zone shape from xml
  156. try
  157. {
  158. coords = null;
  159. int[] point;
  160. rs.clear();
  161. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  162. {
  163. if ("node".equalsIgnoreCase(cd.getNodeName()))
  164. {
  165. attrs = cd.getAttributes();
  166. point = new int[2];
  167. point[0] = parseInt(attrs, "X");
  168. point[1] = parseInt(attrs, "Y");
  169. rs.add(point);
  170. }
  171. }
  172. coords = rs.toArray(new int[rs.size()][2]);
  173. if (coords == null || coords.length == 0)
  174. {
  175. _log.warning(getClass().getSimpleName() + ": ZoneData: missing data for zone: " + zoneId + " XML file: " + getCurrentFile().getName());
  176. continue;
  177. }
  178. // Create this zone. Parsing for cuboids is a
  179. // bit different than for other polygons
  180. // cuboids need exactly 2 points to be defined.
  181. // Other polygons need at least 3 (one per
  182. // vertex)
  183. if (zoneShape.equalsIgnoreCase("Cuboid"))
  184. {
  185. if (coords.length == 2)
  186. temp.setZone(new ZoneCuboid(coords[0][0], coords[1][0], coords[0][1], coords[1][1], minZ, maxZ));
  187. else
  188. {
  189. _log.warning(getClass().getSimpleName() + ": ZoneData: Missing cuboid vertex in sql data for zone: " + zoneId + " in file: " + getCurrentFile().getName());
  190. continue;
  191. }
  192. }
  193. else if (zoneShape.equalsIgnoreCase("NPoly"))
  194. {
  195. // nPoly needs to have at least 3 vertices
  196. if (coords.length > 2)
  197. {
  198. final int[] aX = new int[coords.length];
  199. final int[] aY = new int[coords.length];
  200. for (int i = 0; i < coords.length; i++)
  201. {
  202. aX[i] = coords[i][0];
  203. aY[i] = coords[i][1];
  204. }
  205. temp.setZone(new ZoneNPoly(aX, aY, minZ, maxZ));
  206. }
  207. else
  208. {
  209. _log.warning(getClass().getSimpleName() + ": ZoneData: Bad data for zone: " + zoneId + " in file: " + getCurrentFile().getName());
  210. continue;
  211. }
  212. }
  213. else if (zoneShape.equalsIgnoreCase("Cylinder"))
  214. {
  215. // A Cylinder zone requires a center point
  216. // at x,y and a radius
  217. attrs = d.getAttributes();
  218. final int zoneRad = Integer.parseInt(attrs.getNamedItem("rad").getNodeValue());
  219. if (coords.length == 1 && zoneRad > 0)
  220. temp.setZone(new ZoneCylinder(coords[0][0], coords[0][1], minZ, maxZ, zoneRad));
  221. else
  222. {
  223. _log.warning(getClass().getSimpleName() + ": ZoneData: Bad data for zone: " + zoneId + " in file: " + getCurrentFile().getName());
  224. continue;
  225. }
  226. }
  227. }
  228. catch (Exception e)
  229. {
  230. _log.log(Level.WARNING, getClass().getSimpleName() + ": ZoneData: Failed to load zone " + zoneId + " coordinates: " + e.getMessage(), e);
  231. }
  232. // Check for additional parameters
  233. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  234. {
  235. if ("stat".equalsIgnoreCase(cd.getNodeName()))
  236. {
  237. attrs = cd.getAttributes();
  238. String name = attrs.getNamedItem("name").getNodeValue();
  239. String val = attrs.getNamedItem("val").getNodeValue();
  240. temp.setParameter(name, val);
  241. }
  242. else if ("spawn".equalsIgnoreCase(cd.getNodeName()) && temp instanceof L2ZoneRespawn)
  243. {
  244. attrs = cd.getAttributes();
  245. int spawnX = Integer.parseInt(attrs.getNamedItem("X").getNodeValue());
  246. int spawnY = Integer.parseInt(attrs.getNamedItem("Y").getNodeValue());
  247. int spawnZ = Integer.parseInt(attrs.getNamedItem("Z").getNodeValue());
  248. Node val = attrs.getNamedItem("type");
  249. ((L2ZoneRespawn) temp).parseLoc(spawnX, spawnY, spawnZ, val == null ? null : val.getNodeValue());
  250. }
  251. else if ("race".equalsIgnoreCase(cd.getNodeName()) && temp instanceof L2RespawnZone)
  252. {
  253. attrs = cd.getAttributes();
  254. String race = attrs.getNamedItem("name").getNodeValue();
  255. String point = attrs.getNamedItem("point").getNodeValue();
  256. ((L2RespawnZone) temp).addRaceRespawnPoint(race, point);
  257. }
  258. }
  259. if (checkId(zoneId))
  260. _log.config(getClass().getSimpleName() + ": Caution: Zone (" + zoneId + ") from file: " + getCurrentFile().getName() + " overrides previos definition.");
  261. if (zoneName != null && !zoneName.isEmpty())
  262. temp.setName(zoneName);
  263. addZone(zoneId, temp);
  264. // Register the zone into any world region it
  265. // intersects with...
  266. // currently 11136 test for each zone :>
  267. int ax, ay, bx, by;
  268. for (int x = 0; x < worldRegions.length; x++)
  269. {
  270. for (int y = 0; y < worldRegions[x].length; y++)
  271. {
  272. ax = (x - L2World.OFFSET_X) << L2World.SHIFT_BY;
  273. bx = ((x + 1) - L2World.OFFSET_X) << L2World.SHIFT_BY;
  274. ay = (y - L2World.OFFSET_Y) << L2World.SHIFT_BY;
  275. by = ((y + 1) - L2World.OFFSET_Y) << L2World.SHIFT_BY;
  276. if (temp.getZone().intersectsRectangle(ax, bx, ay, by))
  277. {
  278. worldRegions[x][y].addZone(temp);
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }
  287. @Override
  288. public final void load()
  289. {
  290. _log.info(getClass().getSimpleName() + ": Loading zones...");
  291. _classZones.clear();
  292. long started = System.currentTimeMillis();
  293. parseDirectory("data/zones");
  294. started = System.currentTimeMillis() - started;
  295. _log.info(getClass().getSimpleName() + ": Loaded " + _classZones.size() + " zone classes and " + getSize() + " zones in " + (started / 1000) + " seconds.");
  296. }
  297. public int getSize()
  298. {
  299. int i = 0;
  300. for (Map<Integer, ? extends L2ZoneType> map : _classZones.values())
  301. {
  302. i += map.size();
  303. }
  304. return i;
  305. }
  306. public boolean checkId(int id)
  307. {
  308. for (Map<Integer, ? extends L2ZoneType> map : _classZones.values())
  309. {
  310. if (map.containsKey(id))
  311. return true;
  312. }
  313. return false;
  314. }
  315. /**
  316. * Add new zone
  317. * @param <T>
  318. * @param id
  319. * @param zone
  320. */
  321. @SuppressWarnings("unchecked")
  322. public <T extends L2ZoneType> void addZone(Integer id, T zone)
  323. {
  324. Map<Integer, T> map = (Map<Integer, T>) _classZones.get(zone.getClass());
  325. if (map == null)
  326. {
  327. map = new HashMap<>();
  328. map.put(id, zone);
  329. _classZones.put(zone.getClass(), map);
  330. }
  331. else
  332. map.put(id, zone);
  333. }
  334. /**
  335. * Returns all zones registered with the ZoneManager. To minimize iteration processing retrieve zones from L2WorldRegion for a specific location instead.
  336. * @return zones
  337. * @see #getAllZones(Class)
  338. */
  339. @Deprecated
  340. public Collection<L2ZoneType> getAllZones()
  341. {
  342. List<L2ZoneType> zones = new ArrayList<>();
  343. for (Map<Integer, ? extends L2ZoneType> map : _classZones.values())
  344. {
  345. zones.addAll(map.values());
  346. }
  347. return zones;
  348. }
  349. /**
  350. * Return all zones by class type
  351. * @param <T>
  352. * @param zoneType Zone class
  353. * @return Collection of zones
  354. */
  355. @SuppressWarnings("unchecked")
  356. public <T extends L2ZoneType> Collection<T> getAllZones(Class<T> zoneType)
  357. {
  358. return (Collection<T>) _classZones.get(zoneType).values();
  359. }
  360. /**
  361. * Get zone by ID
  362. * @param id
  363. * @return
  364. * @see #getZoneById(int, Class)
  365. */
  366. public L2ZoneType getZoneById(int id)
  367. {
  368. for (Map<Integer, ? extends L2ZoneType> map : _classZones.values())
  369. {
  370. if (map.containsKey(id))
  371. return map.get(id);
  372. }
  373. return null;
  374. }
  375. /**
  376. * Get zone by ID and zone class
  377. * @param <T>
  378. * @param id
  379. * @param zoneType
  380. * @return zone
  381. */
  382. @SuppressWarnings("unchecked")
  383. public <T extends L2ZoneType> T getZoneById(int id, Class<T> zoneType)
  384. {
  385. return (T) _classZones.get(zoneType).get(id);
  386. }
  387. /**
  388. * Returns all zones from where the object is located
  389. * @param object
  390. * @return zones
  391. */
  392. public List<L2ZoneType> getZones(L2Object object)
  393. {
  394. return getZones(object.getX(), object.getY(), object.getZ());
  395. }
  396. /**
  397. * @param <T>
  398. * @param object
  399. * @param type
  400. * @return zone from where the object is located by type
  401. */
  402. public <T extends L2ZoneType> T getZone(L2Object object, Class<T> type)
  403. {
  404. if (object == null)
  405. return null;
  406. return getZone(object.getX(), object.getY(), object.getZ(), type);
  407. }
  408. /**
  409. * Returns all zones from given coordinates (plane)
  410. * @param x
  411. * @param y
  412. * @return zones
  413. */
  414. public List<L2ZoneType> getZones(int x, int y)
  415. {
  416. L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  417. List<L2ZoneType> temp = new ArrayList<>();
  418. for (L2ZoneType zone : region.getZones())
  419. {
  420. if (zone.isInsideZone(x, y))
  421. temp.add(zone);
  422. }
  423. return temp;
  424. }
  425. /**
  426. * Returns all zones from given coordinates
  427. * @param x
  428. * @param y
  429. * @param z
  430. * @return zones
  431. */
  432. public List<L2ZoneType> getZones(int x, int y, int z)
  433. {
  434. L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  435. List<L2ZoneType> temp = new ArrayList<>();
  436. for (L2ZoneType zone : region.getZones())
  437. {
  438. if (zone.isInsideZone(x, y, z))
  439. temp.add(zone);
  440. }
  441. return temp;
  442. }
  443. /**
  444. * @param <T>
  445. * @param x
  446. * @param y
  447. * @param z
  448. * @param type
  449. * @return zone from given coordinates
  450. */
  451. @SuppressWarnings("unchecked")
  452. public <T extends L2ZoneType> T getZone(int x, int y, int z, Class<T> type)
  453. {
  454. L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  455. for (L2ZoneType zone : region.getZones())
  456. {
  457. if (zone.isInsideZone(x, y, z) && type.isInstance(zone))
  458. return (T) zone;
  459. }
  460. return null;
  461. }
  462. public final L2ArenaZone getArena(L2Character character)
  463. {
  464. if (character == null)
  465. return null;
  466. for (L2ZoneType temp : ZoneManager.getInstance().getZones(character.getX(), character.getY(), character.getZ()))
  467. {
  468. if (temp instanceof L2ArenaZone && temp.isCharacterInZone(character))
  469. return ((L2ArenaZone) temp);
  470. }
  471. return null;
  472. }
  473. public final L2OlympiadStadiumZone getOlympiadStadium(L2Character character)
  474. {
  475. if (character == null)
  476. return null;
  477. for (L2ZoneType temp : ZoneManager.getInstance().getZones(character.getX(), character.getY(), character.getZ()))
  478. {
  479. if (temp instanceof L2OlympiadStadiumZone && temp.isCharacterInZone(character))
  480. return ((L2OlympiadStadiumZone) temp);
  481. }
  482. return null;
  483. }
  484. /**
  485. * For testing purposes only
  486. * @param <T>
  487. * @param obj
  488. * @param type
  489. * @return
  490. */
  491. @SuppressWarnings("unchecked")
  492. public <T extends L2ZoneType> T getClosestZone(L2Object obj, Class<T> type)
  493. {
  494. T zone = getZone(obj, type);
  495. if (zone == null)
  496. {
  497. double closestdis = Double.MAX_VALUE;
  498. for (T temp : (Collection<T>) _classZones.get(type).values())
  499. {
  500. double distance = temp.getDistanceToZone(obj);
  501. if (distance < closestdis)
  502. {
  503. closestdis = distance;
  504. zone = temp;
  505. }
  506. }
  507. }
  508. return zone;
  509. }
  510. /**
  511. * General storage for debug items used for visualizing zones.
  512. * @return list of items
  513. */
  514. public List<L2ItemInstance> getDebugItems()
  515. {
  516. if (_debugItems == null)
  517. _debugItems = new ArrayList<>();
  518. return _debugItems;
  519. }
  520. /**
  521. * Remove all debug items from l2world
  522. */
  523. public void clearDebugItems()
  524. {
  525. if (_debugItems != null)
  526. {
  527. Iterator<L2ItemInstance> it = _debugItems.iterator();
  528. while (it.hasNext())
  529. {
  530. L2ItemInstance item = it.next();
  531. if (item != null)
  532. item.decayMe();
  533. it.remove();
  534. }
  535. }
  536. }
  537. public static AbstractZoneSettings getSettings(String name)
  538. {
  539. return _settings.get(name);
  540. }
  541. private static class SingletonHolder
  542. {
  543. protected static final ZoneManager _instance = new ZoneManager();
  544. }
  545. }