MapRegionManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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.io.File;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.gameserver.SevenSigns;
  28. import com.l2jserver.gameserver.engines.DocumentParser;
  29. import com.l2jserver.gameserver.model.L2MapRegion;
  30. import com.l2jserver.gameserver.model.L2Object;
  31. import com.l2jserver.gameserver.model.Location;
  32. import com.l2jserver.gameserver.model.actor.L2Character;
  33. import com.l2jserver.gameserver.model.actor.L2Npc;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  35. import com.l2jserver.gameserver.model.actor.instance.L2SiegeFlagInstance;
  36. import com.l2jserver.gameserver.model.entity.Castle;
  37. import com.l2jserver.gameserver.model.entity.ClanHall;
  38. import com.l2jserver.gameserver.model.entity.Fort;
  39. import com.l2jserver.gameserver.model.entity.Instance;
  40. import com.l2jserver.gameserver.model.entity.clanhall.SiegableHall;
  41. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  42. import com.l2jserver.gameserver.model.zone.type.L2RespawnZone;
  43. /**
  44. * @author Nyaran
  45. */
  46. public class MapRegionManager extends DocumentParser
  47. {
  48. private static final Map<String, L2MapRegion> _regions = new HashMap<>();
  49. private static final String defaultRespawn = "talking_island_town";
  50. public static enum TeleportWhereType
  51. {
  52. Castle,
  53. Castle_banish,
  54. ClanHall,
  55. ClanHall_banish,
  56. SiegeFlag,
  57. Town,
  58. Fortress,
  59. Fortress_banish,
  60. Territory,
  61. Territory_banish
  62. }
  63. protected MapRegionManager()
  64. {
  65. load();
  66. }
  67. @Override
  68. public void load()
  69. {
  70. _regions.clear();
  71. parseDirectory(new File(Config.DATAPACK_ROOT, "data/mapregion/"));
  72. _log.info(getClass().getSimpleName() + ": Loaded " + _regions.size() + " map regions.");
  73. }
  74. @Override
  75. protected void parseDocument()
  76. {
  77. NamedNodeMap attrs;
  78. String name;
  79. String town;
  80. int locId;
  81. int castle;
  82. int bbs;
  83. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  84. {
  85. if ("list".equalsIgnoreCase(n.getNodeName()))
  86. {
  87. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  88. {
  89. if ("region".equalsIgnoreCase(d.getNodeName()))
  90. {
  91. attrs = d.getAttributes();
  92. name = attrs.getNamedItem("name").getNodeValue();
  93. town = attrs.getNamedItem("town").getNodeValue();
  94. locId = parseInt(attrs, "locId");
  95. castle = parseInt(attrs, "castle");
  96. bbs = parseInt(attrs, "bbs");
  97. L2MapRegion region = new L2MapRegion(name, town, locId, castle, bbs);
  98. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  99. {
  100. attrs = c.getAttributes();
  101. if ("respawnPoint".equalsIgnoreCase(c.getNodeName()))
  102. {
  103. int spawnX = parseInt(attrs, "X");
  104. int spawnY = parseInt(attrs, "Y");
  105. int spawnZ = parseInt(attrs, "Z");
  106. boolean other = parseBoolean(attrs, "isOther");
  107. boolean chaotic = parseBoolean(attrs, "isChaotic");
  108. boolean banish = parseBoolean(attrs, "isBanish");
  109. if (other)
  110. {
  111. region.addOtherSpawn(spawnX, spawnY, spawnZ);
  112. }
  113. else if (chaotic)
  114. {
  115. region.addChaoticSpawn(spawnX, spawnY, spawnZ);
  116. }
  117. else if (banish)
  118. {
  119. region.addBanishSpawn(spawnX, spawnY, spawnZ);
  120. }
  121. else
  122. {
  123. region.addSpawn(spawnX, spawnY, spawnZ);
  124. }
  125. }
  126. else if ("map".equalsIgnoreCase(c.getNodeName()))
  127. {
  128. region.addMap(parseInt(attrs, "X"), parseInt(attrs, "Y"));
  129. }
  130. else if ("banned".equalsIgnoreCase(c.getNodeName()))
  131. {
  132. region.addBannedRace(attrs.getNamedItem("race").getNodeValue(), attrs.getNamedItem("point").getNodeValue());
  133. }
  134. }
  135. _regions.put(name, region);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * @param locX
  143. * @param locY
  144. * @return
  145. */
  146. public final L2MapRegion getMapRegion(int locX, int locY)
  147. {
  148. for (L2MapRegion region : _regions.values())
  149. {
  150. if (region.isZoneInRegion(getMapRegionX(locX), getMapRegionY(locY)))
  151. {
  152. return region;
  153. }
  154. }
  155. return null;
  156. }
  157. /**
  158. * @param locX
  159. * @param locY
  160. * @return
  161. */
  162. public final int getMapRegionLocId(int locX, int locY)
  163. {
  164. L2MapRegion region = getMapRegion(locX, locY);
  165. if (region != null)
  166. {
  167. return region.getLocId();
  168. }
  169. return 0;
  170. }
  171. /**
  172. * @param obj
  173. * @return
  174. */
  175. public final L2MapRegion getMapRegion(L2Object obj)
  176. {
  177. return getMapRegion(obj.getX(), obj.getY());
  178. }
  179. /**
  180. * @param obj
  181. * @return
  182. */
  183. public final int getMapRegionLocId(L2Object obj)
  184. {
  185. return getMapRegionLocId(obj.getX(), obj.getY());
  186. }
  187. /**
  188. * @param posX
  189. * @return
  190. */
  191. public final int getMapRegionX(int posX)
  192. {
  193. return (posX >> 15) + 9 + 11;// + centerTileX;
  194. }
  195. /**
  196. * @param posY
  197. * @return
  198. */
  199. public final int getMapRegionY(int posY)
  200. {
  201. return (posY >> 15) + 10 + 8;// + centerTileX;
  202. }
  203. /**
  204. * Get town name by character position
  205. * @param activeChar
  206. * @return
  207. */
  208. public String getClosestTownName(L2Character activeChar)
  209. {
  210. L2MapRegion region = getMapRegion(activeChar);
  211. if (region == null)
  212. {
  213. return "Aden Castle Town";
  214. }
  215. return region.getTown();
  216. }
  217. /**
  218. * @param activeChar
  219. * @return
  220. */
  221. public int getAreaCastle(L2Character activeChar)
  222. {
  223. L2MapRegion region = getMapRegion(activeChar);
  224. if (region == null)
  225. {
  226. return 0;
  227. }
  228. return region.getCastle();
  229. }
  230. /**
  231. * @param activeChar
  232. * @param teleportWhere
  233. * @return
  234. */
  235. public Location getTeleToLocation(L2Character activeChar, TeleportWhereType teleportWhere)
  236. {
  237. Location loc;
  238. if (activeChar instanceof L2PcInstance)
  239. {
  240. L2PcInstance player = ((L2PcInstance) activeChar);
  241. Castle castle = null;
  242. Fort fort = null;
  243. ClanHall clanhall = null;
  244. if ((player.getClan() != null) && !player.isFlyingMounted() && !player.isFlying()) // flying players in gracia cant use teleports to aden continent
  245. {
  246. // If teleport to clan hall
  247. if (teleportWhere == TeleportWhereType.ClanHall)
  248. {
  249. clanhall = ClanHallManager.getInstance().getAbstractHallByOwner(player.getClan());
  250. if (clanhall != null)
  251. {
  252. L2ClanHallZone zone = clanhall.getZone();
  253. if ((zone != null) && !player.isFlyingMounted())
  254. {
  255. if (player.getKarma() > 0)
  256. {
  257. return zone.getChaoticSpawnLoc();
  258. }
  259. return zone.getSpawnLoc();
  260. }
  261. }
  262. }
  263. // If teleport to castle
  264. if (teleportWhere == TeleportWhereType.Castle)
  265. {
  266. castle = CastleManager.getInstance().getCastleByOwner(player.getClan());
  267. // Otherwise check if player is on castle or fortress ground
  268. // and player's clan is defender
  269. if (castle == null)
  270. {
  271. castle = CastleManager.getInstance().getCastle(player);
  272. if (!((castle != null) && castle.getSiege().getIsInProgress() && (castle.getSiege().getDefenderClan(player.getClan()) != null)))
  273. {
  274. castle = null;
  275. }
  276. }
  277. if ((castle != null) && (castle.getCastleId() > 0))
  278. {
  279. if (player.getKarma() > 0)
  280. {
  281. return castle.getCastleZone().getChaoticSpawnLoc();
  282. }
  283. return castle.getCastleZone().getSpawnLoc();
  284. }
  285. }
  286. // If teleport to fortress
  287. if (teleportWhere == TeleportWhereType.Fortress)
  288. {
  289. fort = FortManager.getInstance().getFortByOwner(player.getClan());
  290. // Otherwise check if player is on castle or fortress ground
  291. // and player's clan is defender
  292. if (fort == null)
  293. {
  294. fort = FortManager.getInstance().getFort(player);
  295. if (!((fort != null) && fort.getSiege().getIsInProgress() && (fort.getOwnerClan() == player.getClan())))
  296. {
  297. fort = null;
  298. }
  299. }
  300. if ((fort != null) && (fort.getFortId() > 0))
  301. {
  302. if (player.getKarma() > 0)
  303. {
  304. return fort.getFortZone().getChaoticSpawnLoc();
  305. }
  306. return fort.getFortZone().getSpawnLoc();
  307. }
  308. }
  309. // If teleport to SiegeHQ
  310. if (teleportWhere == TeleportWhereType.SiegeFlag)
  311. {
  312. castle = CastleManager.getInstance().getCastle(player);
  313. fort = FortManager.getInstance().getFort(player);
  314. clanhall = ClanHallManager.getInstance().getNearbyAbstractHall(activeChar.getX(), activeChar.getY(), 10000);
  315. L2SiegeFlagInstance tw_flag = TerritoryWarManager.getInstance().getFlagForClan(player.getClan());
  316. if (tw_flag != null)
  317. {
  318. return tw_flag.getLocation();
  319. }
  320. else if (castle != null)
  321. {
  322. if (castle.getSiege().getIsInProgress())
  323. {
  324. // Check if player's clan is attacker
  325. List<L2Npc> flags = castle.getSiege().getFlag(player.getClan());
  326. if ((flags != null) && !flags.isEmpty())
  327. {
  328. // Spawn to flag - Need more work to get player to the nearest flag
  329. return flags.get(0).getLocation();
  330. }
  331. }
  332. }
  333. else if (fort != null)
  334. {
  335. if (fort.getSiege().getIsInProgress())
  336. {
  337. // Check if player's clan is attacker
  338. List<L2Npc> flags = fort.getSiege().getFlag(player.getClan());
  339. if ((flags != null) && !flags.isEmpty())
  340. {
  341. // Spawn to flag - Need more work to get player to the nearest flag
  342. return flags.get(0).getLocation();
  343. }
  344. }
  345. }
  346. else if ((clanhall != null) && clanhall.isSiegableHall())
  347. {
  348. SiegableHall sHall = (SiegableHall) clanhall;
  349. List<L2Npc> flags = sHall.getSiege().getFlag(player.getClan());
  350. if ((flags != null) && !flags.isEmpty())
  351. {
  352. return flags.get(0).getLocation();
  353. }
  354. }
  355. }
  356. }
  357. if (teleportWhere == TeleportWhereType.Castle_banish)
  358. {
  359. castle = CastleManager.getInstance().getCastle(player);
  360. if (castle != null)
  361. {
  362. return castle.getCastleZone().getBanishSpawnLoc();
  363. }
  364. }
  365. else if (teleportWhere == TeleportWhereType.Fortress_banish)
  366. {
  367. fort = FortManager.getInstance().getFort(activeChar);
  368. if (fort != null)
  369. {
  370. return fort.getFortZone().getBanishSpawnLoc();
  371. }
  372. }
  373. else if (teleportWhere == TeleportWhereType.ClanHall_banish)
  374. {
  375. clanhall = ClanHallManager.getInstance().getClanHall(activeChar);
  376. if (clanhall != null)
  377. {
  378. return clanhall.getZone().getBanishSpawnLoc();
  379. }
  380. }
  381. // Karma player land out of city
  382. if (player.getKarma() > 0)
  383. {
  384. try
  385. {
  386. L2RespawnZone zone = ZoneManager.getInstance().getZone(player, L2RespawnZone.class);
  387. if (zone != null)
  388. {
  389. return getRestartRegion(activeChar, zone.getRespawnPoint((L2PcInstance) activeChar)).getChaoticSpawnLoc();
  390. }
  391. return getMapRegion(activeChar).getChaoticSpawnLoc();
  392. }
  393. catch (Exception e)
  394. {
  395. if (player.isFlyingMounted())
  396. {
  397. return _regions.get("union_base_of_kserth").getChaoticSpawnLoc();
  398. }
  399. return _regions.get(defaultRespawn).getChaoticSpawnLoc();
  400. }
  401. }
  402. // Checking if needed to be respawned in "far" town from the castle;
  403. castle = CastleManager.getInstance().getCastle(player);
  404. if (castle != null)
  405. {
  406. if (castle.getSiege().getIsInProgress())
  407. {
  408. // Check if player's clan is participating
  409. if ((castle.getSiege().checkIsDefender(player.getClan()) || castle.getSiege().checkIsAttacker(player.getClan())) && (SevenSigns.getInstance().getSealOwner(SevenSigns.SEAL_STRIFE) == SevenSigns.CABAL_DAWN))
  410. {
  411. return castle.getCastleZone().getOtherSpawnLoc();
  412. }
  413. }
  414. }
  415. // Checking if in an instance
  416. if (player.getInstanceId() > 0)
  417. {
  418. Instance inst = InstanceManager.getInstance().getInstance(player.getInstanceId());
  419. if (inst != null)
  420. {
  421. loc = inst.getSpawnLoc();
  422. if (loc != null)
  423. {
  424. return loc;
  425. }
  426. }
  427. }
  428. }
  429. // Get the nearest town
  430. try
  431. {
  432. L2RespawnZone zone = ZoneManager.getInstance().getZone(activeChar, L2RespawnZone.class);
  433. if (zone != null)
  434. {
  435. return getRestartRegion(activeChar, zone.getRespawnPoint((L2PcInstance) activeChar)).getSpawnLoc();
  436. }
  437. return getMapRegion(activeChar).getSpawnLoc();
  438. }
  439. catch (Exception e)
  440. {
  441. // Port to the default respawn if no closest town found.
  442. return _regions.get(defaultRespawn).getSpawnLoc();
  443. }
  444. }
  445. /**
  446. * @param activeChar
  447. * @param point
  448. * @return
  449. */
  450. public L2MapRegion getRestartRegion(L2Character activeChar, String point)
  451. {
  452. try
  453. {
  454. L2PcInstance player = ((L2PcInstance) activeChar);
  455. L2MapRegion region = _regions.get(point);
  456. if (region.getBannedRace().containsKey(player.getRace()))
  457. {
  458. getRestartRegion(player, region.getBannedRace().get(player.getRace()));
  459. }
  460. return region;
  461. }
  462. catch (Exception e)
  463. {
  464. return _regions.get(defaultRespawn);
  465. }
  466. }
  467. /**
  468. * @param regionName the map region name.
  469. * @return if exists the map region identified by that name, null otherwise.
  470. */
  471. public L2MapRegion getMapRegionByName(String regionName)
  472. {
  473. return _regions.get(regionName);
  474. }
  475. public static MapRegionManager getInstance()
  476. {
  477. return SingletonHolder._instance;
  478. }
  479. private static class SingletonHolder
  480. {
  481. protected static final MapRegionManager _instance = new MapRegionManager();
  482. }
  483. }