ZoneManager.java 18 KB

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