2
0

ZoneManager.java 17 KB

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