MapRegionManager.java 14 KB

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