ZoneManager.java 18 KB

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