MapRegionManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 new Location(tw_flag.getX(), tw_flag.getY(), tw_flag.getZ());
  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. L2Npc flag = flags.get(0);
  330. return new Location(flag.getX(), flag.getY(), flag.getZ());
  331. }
  332. }
  333. }
  334. else if (fort != null)
  335. {
  336. if (fort.getSiege().getIsInProgress())
  337. {
  338. // Check if player's clan is attacker
  339. List<L2Npc> flags = fort.getSiege().getFlag(player.getClan());
  340. if ((flags != null) && !flags.isEmpty())
  341. {
  342. // Spawn to flag - Need more work to get player to the nearest flag
  343. L2Npc flag = flags.get(0);
  344. return new Location(flag.getX(), flag.getY(), flag.getZ());
  345. }
  346. }
  347. }
  348. else if ((clanhall != null) && clanhall.isSiegableHall())
  349. {
  350. SiegableHall sHall = (SiegableHall) clanhall;
  351. List<L2Npc> flags = sHall.getSiege().getFlag(player.getClan());
  352. if ((flags != null) && !flags.isEmpty())
  353. {
  354. L2Npc flag = flags.get(0);
  355. return new Location(flag.getX(), flag.getY(), flag.getZ());
  356. }
  357. }
  358. }
  359. }
  360. if (teleportWhere == TeleportWhereType.Castle_banish)
  361. {
  362. castle = CastleManager.getInstance().getCastle(player);
  363. if (castle != null)
  364. {
  365. return castle.getCastleZone().getBanishSpawnLoc();
  366. }
  367. }
  368. else if (teleportWhere == TeleportWhereType.Fortress_banish)
  369. {
  370. fort = FortManager.getInstance().getFort(activeChar);
  371. if (fort != null)
  372. {
  373. return fort.getFortZone().getBanishSpawnLoc();
  374. }
  375. }
  376. else if (teleportWhere == TeleportWhereType.ClanHall_banish)
  377. {
  378. clanhall = ClanHallManager.getInstance().getClanHall(activeChar);
  379. if (clanhall != null)
  380. {
  381. return clanhall.getZone().getBanishSpawnLoc();
  382. }
  383. }
  384. // Karma player land out of city
  385. if (player.getKarma() > 0)
  386. {
  387. try
  388. {
  389. L2RespawnZone zone = ZoneManager.getInstance().getZone(player, L2RespawnZone.class);
  390. if (zone != null)
  391. {
  392. return getRestartRegion(activeChar, zone.getRespawnPoint((L2PcInstance) activeChar)).getChaoticSpawnLoc();
  393. }
  394. return getMapRegion(activeChar).getChaoticSpawnLoc();
  395. }
  396. catch (Exception e)
  397. {
  398. if (player.isFlyingMounted())
  399. {
  400. return _regions.get("union_base_of_kserth").getChaoticSpawnLoc();
  401. }
  402. return _regions.get(defaultRespawn).getChaoticSpawnLoc();
  403. }
  404. }
  405. // Checking if needed to be respawned in "far" town from the castle;
  406. castle = CastleManager.getInstance().getCastle(player);
  407. if (castle != null)
  408. {
  409. if (castle.getSiege().getIsInProgress())
  410. {
  411. // Check if player's clan is participating
  412. if ((castle.getSiege().checkIsDefender(player.getClan()) || castle.getSiege().checkIsAttacker(player.getClan())) && (SevenSigns.getInstance().getSealOwner(SevenSigns.SEAL_STRIFE) == SevenSigns.CABAL_DAWN))
  413. {
  414. return castle.getCastleZone().getOtherSpawnLoc();
  415. }
  416. }
  417. }
  418. // Checking if in an instance
  419. if (player.getInstanceId() > 0)
  420. {
  421. Instance inst = InstanceManager.getInstance().getInstance(player.getInstanceId());
  422. if (inst != null)
  423. {
  424. loc = inst.getSpawnLoc();
  425. if (loc != null)
  426. {
  427. return loc;
  428. }
  429. }
  430. }
  431. }
  432. // Get the nearest town
  433. try
  434. {
  435. L2RespawnZone zone = ZoneManager.getInstance().getZone(activeChar, L2RespawnZone.class);
  436. if (zone != null)
  437. {
  438. return getRestartRegion(activeChar, zone.getRespawnPoint((L2PcInstance) activeChar)).getSpawnLoc();
  439. }
  440. return getMapRegion(activeChar).getSpawnLoc();
  441. }
  442. catch (Exception e)
  443. {
  444. // Port to the default respawn if no closest town found.
  445. return _regions.get(defaultRespawn).getSpawnLoc();
  446. }
  447. }
  448. /**
  449. * @param activeChar
  450. * @param point
  451. * @return
  452. */
  453. public L2MapRegion getRestartRegion(L2Character activeChar, String point)
  454. {
  455. try
  456. {
  457. L2PcInstance player = ((L2PcInstance) activeChar);
  458. L2MapRegion region = _regions.get(point);
  459. if (region.getBannedRace().containsKey(player.getRace()))
  460. {
  461. getRestartRegion(player, region.getBannedRace().get(player.getRace()));
  462. }
  463. return region;
  464. }
  465. catch (Exception e)
  466. {
  467. return _regions.get(defaultRespawn);
  468. }
  469. }
  470. /**
  471. * @param regionName the map region name.
  472. * @return if exists the map region identified by that name, null otherwise.
  473. */
  474. public L2MapRegion getMapRegionByName(String regionName)
  475. {
  476. return _regions.get(regionName);
  477. }
  478. public static MapRegionManager getInstance()
  479. {
  480. return SingletonHolder._instance;
  481. }
  482. private static class SingletonHolder
  483. {
  484. protected static final MapRegionManager _instance = new MapRegionManager();
  485. }
  486. }