ZoneManager.java 17 KB

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