MapRegionManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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.Document;
  21. import org.w3c.dom.NamedNodeMap;
  22. import org.w3c.dom.Node;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.SevenSigns;
  25. import com.l2jserver.gameserver.engines.DocumentParser;
  26. import com.l2jserver.gameserver.model.L2MapRegion;
  27. import com.l2jserver.gameserver.model.L2Object;
  28. import com.l2jserver.gameserver.model.Location;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.actor.L2Npc;
  31. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  32. import com.l2jserver.gameserver.model.actor.instance.L2SiegeFlagInstance;
  33. import com.l2jserver.gameserver.model.entity.Castle;
  34. import com.l2jserver.gameserver.model.entity.ClanHall;
  35. import com.l2jserver.gameserver.model.entity.Fort;
  36. import com.l2jserver.gameserver.model.entity.Instance;
  37. import com.l2jserver.gameserver.model.entity.clanhall.SiegableHall;
  38. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  39. import com.l2jserver.gameserver.model.zone.type.L2RespawnZone;
  40. /**
  41. * @author Nyaran
  42. */
  43. public class MapRegionManager extends DocumentParser
  44. {
  45. private static final Map<String, L2MapRegion> _regions = new HashMap<>();
  46. private static final String defaultRespawn = "talking_island_town";
  47. public static enum TeleportWhereType
  48. {
  49. Castle,
  50. Castle_banish,
  51. ClanHall,
  52. ClanHall_banish,
  53. SiegeFlag,
  54. Town,
  55. Fortress,
  56. Fortress_banish,
  57. Territory,
  58. Territory_banish
  59. }
  60. private MapRegionManager()
  61. {
  62. _regions.clear();
  63. parseDirectory(new File(Config.DATAPACK_ROOT, "data/mapregion/"));
  64. _log.info(getClass().getSimpleName() + ": Loaded " + _regions.size() + " map regions.");
  65. }
  66. @Override
  67. protected void parseDocument(Document doc)
  68. {
  69. NamedNodeMap attrs;
  70. String name;
  71. String town;
  72. int locId;
  73. int castle;
  74. int bbs;
  75. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  76. {
  77. if ("list".equalsIgnoreCase(n.getNodeName()))
  78. {
  79. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  80. {
  81. if ("region".equalsIgnoreCase(d.getNodeName()))
  82. {
  83. attrs = d.getAttributes();
  84. name = attrs.getNamedItem("name").getNodeValue();
  85. town = attrs.getNamedItem("town").getNodeValue();
  86. locId = parseInt(attrs, "locId");
  87. castle = parseInt(attrs, "castle");
  88. bbs = parseInt(attrs, "bbs");
  89. L2MapRegion region = new L2MapRegion(name, town, locId, castle, bbs);
  90. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  91. {
  92. attrs = c.getAttributes();
  93. if ("respawnPoint".equalsIgnoreCase(c.getNodeName()))
  94. {
  95. int spawnX = parseInt(attrs, "X");
  96. int spawnY = parseInt(attrs, "Y");
  97. int spawnZ = parseInt(attrs, "Z");
  98. boolean other = parseBoolean(attrs, "isOther");
  99. boolean chaotic = parseBoolean(attrs, "isChaotic");
  100. boolean banish = parseBoolean(attrs, "isBanish");
  101. if (other)
  102. {
  103. region.addOtherSpawn(spawnX, spawnY, spawnZ);
  104. }
  105. else if (chaotic)
  106. {
  107. region.addChaoticSpawn(spawnX, spawnY, spawnZ);
  108. }
  109. else if (banish)
  110. {
  111. region.addBanishSpawn(spawnX, spawnY, spawnZ);
  112. }
  113. else
  114. {
  115. region.addSpawn(spawnX, spawnY, spawnZ);
  116. }
  117. }
  118. else if ("map".equalsIgnoreCase(c.getNodeName()))
  119. {
  120. region.addMap(parseInt(attrs, "X"), parseInt(attrs, "Y"));
  121. }
  122. else if ("banned".equalsIgnoreCase(c.getNodeName()))
  123. {
  124. region.addBannedRace(attrs.getNamedItem("race").getNodeValue(), attrs.getNamedItem("point").getNodeValue());
  125. }
  126. }
  127. _regions.put(name, region);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. /**
  134. * @param locX
  135. * @param locY
  136. * @return
  137. */
  138. public final L2MapRegion getMapRegion(int locX, int locY)
  139. {
  140. for (L2MapRegion region : _regions.values())
  141. {
  142. if (region.isZoneInRegion(getMapRegionX(locX), getMapRegionY(locY)))
  143. {
  144. return region;
  145. }
  146. }
  147. return null;
  148. }
  149. /**
  150. * @param locX
  151. * @param locY
  152. * @return
  153. */
  154. public final int getMapRegionLocId(int locX, int locY)
  155. {
  156. L2MapRegion region = getMapRegion(locX, locY);
  157. if (region != null)
  158. {
  159. return region.getLocId();
  160. }
  161. if (Config.DEBUG)
  162. {
  163. _log.warning(getClass().getSimpleName() + ": Player outside map regions at X,Y=" + locX + "," + locY);
  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. int[] coord;
  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. coord = inst.getSpawnLoc();
  421. if ((coord[0] != 0) && (coord[1] != 0) && (coord[2] != 0))
  422. {
  423. return new Location(coord[0], coord[1], coord[2]);
  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. if (Config.DEBUG)
  442. {
  443. _log.warning(getClass().getSimpleName() + ": Not defined respawn point for coords loc X=" + activeChar.getX() + " Y=" + activeChar.getY() + " Z=" + activeChar.getZ());
  444. }
  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. @SuppressWarnings("synthetic-access")
  483. private static class SingletonHolder
  484. {
  485. protected static final MapRegionManager _instance = new MapRegionManager();
  486. }
  487. }