MapRegionManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright © 2004-2020 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.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.w3c.dom.Document;
  26. import org.w3c.dom.NamedNodeMap;
  27. import org.w3c.dom.Node;
  28. import com.l2jserver.gameserver.SevenSigns;
  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.TeleportWhereType;
  33. import com.l2jserver.gameserver.model.actor.L2Character;
  34. import com.l2jserver.gameserver.model.actor.L2Npc;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. import com.l2jserver.gameserver.model.actor.instance.L2SiegeFlagInstance;
  37. import com.l2jserver.gameserver.model.entity.Castle;
  38. import com.l2jserver.gameserver.model.entity.ClanHall;
  39. import com.l2jserver.gameserver.model.entity.Fort;
  40. import com.l2jserver.gameserver.model.entity.Instance;
  41. import com.l2jserver.gameserver.model.entity.clanhall.SiegableHall;
  42. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  43. import com.l2jserver.gameserver.model.zone.type.L2RespawnZone;
  44. import com.l2jserver.gameserver.util.IXmlReader;
  45. /**
  46. * Map Region Manager.
  47. * @author Nyaran
  48. */
  49. public final class MapRegionManager implements IXmlReader {
  50. private static final Logger LOG = LoggerFactory.getLogger(MapRegionManager.class);
  51. private static final Map<String, L2MapRegion> REGIONS = new HashMap<>();
  52. private static final String DEFAULT_RESPAWN = "talking_island_town";
  53. protected MapRegionManager() {
  54. load();
  55. }
  56. @Override
  57. public void load() {
  58. REGIONS.clear();
  59. parseDatapackDirectory("data/mapregion", false);
  60. LOG.info("Loaded {} map regions.", REGIONS.size());
  61. }
  62. @Override
  63. public void parseDocument(Document doc) {
  64. NamedNodeMap attrs;
  65. String name;
  66. String town;
  67. int locId;
  68. int castle;
  69. int bbs;
  70. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling()) {
  71. if ("list".equalsIgnoreCase(n.getNodeName())) {
  72. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling()) {
  73. if ("region".equalsIgnoreCase(d.getNodeName())) {
  74. attrs = d.getAttributes();
  75. name = attrs.getNamedItem("name").getNodeValue();
  76. town = attrs.getNamedItem("town").getNodeValue();
  77. locId = parseInteger(attrs, "locId");
  78. castle = parseInteger(attrs, "castle");
  79. bbs = parseInteger(attrs, "bbs");
  80. L2MapRegion region = new L2MapRegion(name, town, locId, castle, bbs);
  81. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling()) {
  82. attrs = c.getAttributes();
  83. if ("respawnPoint".equalsIgnoreCase(c.getNodeName())) {
  84. int spawnX = parseInteger(attrs, "X");
  85. int spawnY = parseInteger(attrs, "Y");
  86. int spawnZ = parseInteger(attrs, "Z");
  87. boolean other = parseBoolean(attrs, "isOther", false);
  88. boolean chaotic = parseBoolean(attrs, "isChaotic", false);
  89. boolean banish = parseBoolean(attrs, "isBanish", false);
  90. if (other) {
  91. region.addOtherSpawn(spawnX, spawnY, spawnZ);
  92. } else if (chaotic) {
  93. region.addChaoticSpawn(spawnX, spawnY, spawnZ);
  94. } else if (banish) {
  95. region.addBanishSpawn(spawnX, spawnY, spawnZ);
  96. } else {
  97. region.addSpawn(spawnX, spawnY, spawnZ);
  98. }
  99. } else if ("map".equalsIgnoreCase(c.getNodeName())) {
  100. region.addMap(parseInteger(attrs, "X"), parseInteger(attrs, "Y"));
  101. } else if ("banned".equalsIgnoreCase(c.getNodeName())) {
  102. region.addBannedRace(attrs.getNamedItem("race").getNodeValue(), attrs.getNamedItem("point").getNodeValue());
  103. }
  104. }
  105. REGIONS.put(name, region);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. public L2MapRegion getMapRegion(int locX, int locY) {
  112. for (L2MapRegion region : REGIONS.values()) {
  113. if (region.isZoneInRegion(getMapRegionX(locX), getMapRegionY(locY))) {
  114. return region;
  115. }
  116. }
  117. return null;
  118. }
  119. public int getMapRegionLocId(int locX, int locY) {
  120. L2MapRegion region = getMapRegion(locX, locY);
  121. if (region != null) {
  122. return region.getLocId();
  123. }
  124. return 0;
  125. }
  126. public L2MapRegion getMapRegion(L2Object obj) {
  127. return getMapRegion(obj.getX(), obj.getY());
  128. }
  129. public int getMapRegionLocId(L2Object obj) {
  130. return getMapRegionLocId(obj.getX(), obj.getY());
  131. }
  132. public int getMapRegionX(int posX) {
  133. return (posX >> 15) + 9 + 11;// + centerTileX;
  134. }
  135. public int getMapRegionY(int posY) {
  136. return (posY >> 15) + 10 + 8;// + centerTileX;
  137. }
  138. /**
  139. * Get town name by character position
  140. * @param activeChar
  141. * @return
  142. */
  143. public String getClosestTownName(L2Character activeChar) {
  144. L2MapRegion region = getMapRegion(activeChar);
  145. if (region == null) {
  146. return "Aden Castle Town";
  147. }
  148. return region.getTown();
  149. }
  150. public int getAreaCastle(L2Character activeChar) {
  151. L2MapRegion region = getMapRegion(activeChar);
  152. if (region == null) {
  153. return 0;
  154. }
  155. return region.getCastle();
  156. }
  157. public Location getTeleToLocation(L2Character activeChar, TeleportWhereType teleportWhere) {
  158. Location loc;
  159. if (activeChar.isPlayer()) {
  160. final L2PcInstance player = activeChar.getActingPlayer();
  161. Castle castle = null;
  162. Fort fort = null;
  163. ClanHall clanhall = null;
  164. if ((player.getClan() != null) && !player.isFlyingMounted() && !player.isFlying()) // flying players in gracia cant use teleports to aden continent
  165. {
  166. // If teleport to clan hall
  167. if (teleportWhere == TeleportWhereType.CLANHALL) {
  168. clanhall = ClanHallManager.getInstance().getAbstractHallByOwner(player.getClan());
  169. if (clanhall != null) {
  170. L2ClanHallZone zone = clanhall.getZone();
  171. if ((zone != null) && !player.isFlyingMounted()) {
  172. if (player.getKarma() > 0) {
  173. return zone.getChaoticSpawnLoc();
  174. }
  175. return zone.getSpawnLoc();
  176. }
  177. }
  178. }
  179. // If teleport to castle
  180. if (teleportWhere == TeleportWhereType.CASTLE) {
  181. castle = CastleManager.getInstance().getCastleByOwner(player.getClan());
  182. // Otherwise check if player is on castle or fortress ground
  183. // and player's clan is defender
  184. if (castle == null) {
  185. castle = CastleManager.getInstance().getCastle(player);
  186. if (!((castle != null) && castle.getSiege().isInProgress() && (castle.getSiege().getDefenderClan(player.getClan()) != null))) {
  187. castle = null;
  188. }
  189. }
  190. if ((castle != null) && (castle.getResidenceId() > 0)) {
  191. if (player.getKarma() > 0) {
  192. return castle.getResidenceZone().getChaoticSpawnLoc();
  193. }
  194. return castle.getResidenceZone().getSpawnLoc();
  195. }
  196. }
  197. // If teleport to fortress
  198. if (teleportWhere == TeleportWhereType.FORTRESS) {
  199. fort = FortManager.getInstance().getFortByOwner(player.getClan());
  200. // Otherwise check if player is on castle or fortress ground
  201. // and player's clan is defender
  202. if (fort == null) {
  203. fort = FortManager.getInstance().getFort(player);
  204. if (!((fort != null) && fort.getSiege().isInProgress() && (fort.getOwnerClan() == player.getClan()))) {
  205. fort = null;
  206. }
  207. }
  208. if ((fort != null) && (fort.getResidenceId() > 0)) {
  209. if (player.getKarma() > 0) {
  210. return fort.getResidenceZone().getChaoticSpawnLoc();
  211. }
  212. return fort.getResidenceZone().getSpawnLoc();
  213. }
  214. }
  215. // If teleport to SiegeHQ
  216. if (teleportWhere == TeleportWhereType.SIEGEFLAG) {
  217. castle = CastleManager.getInstance().getCastle(player);
  218. fort = FortManager.getInstance().getFort(player);
  219. clanhall = ClanHallManager.getInstance().getNearbyAbstractHall(activeChar.getX(), activeChar.getY(), 10000);
  220. L2SiegeFlagInstance tw_flag = TerritoryWarManager.getInstance().getHQForClan(player.getClan());
  221. if (tw_flag != null) {
  222. return tw_flag.getLocation();
  223. } else if (castle != null) {
  224. if (castle.getSiege().isInProgress()) {
  225. // Check if player's clan is attacker
  226. List<L2Npc> flags = castle.getSiege().getFlag(player.getClan());
  227. if ((flags != null) && !flags.isEmpty()) {
  228. // Spawn to flag - Need more work to get player to the nearest flag
  229. return flags.get(0).getLocation();
  230. }
  231. }
  232. } else if (fort != null) {
  233. if (fort.getSiege().isInProgress()) {
  234. // Check if player's clan is attacker
  235. List<L2Npc> flags = fort.getSiege().getFlag(player.getClan());
  236. if ((flags != null) && !flags.isEmpty()) {
  237. // Spawn to flag - Need more work to get player to the nearest flag
  238. return flags.get(0).getLocation();
  239. }
  240. }
  241. } else if ((clanhall != null) && clanhall.isSiegableHall()) {
  242. SiegableHall sHall = (SiegableHall) clanhall;
  243. List<L2Npc> flags = sHall.getSiege().getFlag(player.getClan());
  244. if ((flags != null) && !flags.isEmpty()) {
  245. return flags.get(0).getLocation();
  246. }
  247. }
  248. }
  249. }
  250. // Karma player land out of city
  251. if (player.getKarma() > 0) {
  252. try {
  253. L2RespawnZone zone = ZoneManager.getInstance().getZone(player, L2RespawnZone.class);
  254. if (zone != null) {
  255. return getRestartRegion(activeChar, zone.getRespawnPoint((L2PcInstance) activeChar)).getChaoticSpawnLoc();
  256. }
  257. return getMapRegion(activeChar).getChaoticSpawnLoc();
  258. } catch (Exception e) {
  259. if (player.isFlyingMounted()) {
  260. return REGIONS.get("union_base_of_kserth").getChaoticSpawnLoc();
  261. }
  262. return REGIONS.get(DEFAULT_RESPAWN).getChaoticSpawnLoc();
  263. }
  264. }
  265. // Checking if needed to be respawned in "far" town from the castle;
  266. castle = CastleManager.getInstance().getCastle(player);
  267. if (castle != null) {
  268. if (castle.getSiege().isInProgress()) {
  269. // Check if player's clan is participating
  270. if ((castle.getSiege().checkIsDefender(player.getClan()) || castle.getSiege().checkIsAttacker(player.getClan())) && (SevenSigns.getInstance().getSealOwner(SevenSigns.SEAL_STRIFE) == SevenSigns.CABAL_DAWN)) {
  271. return castle.getResidenceZone().getOtherSpawnLoc();
  272. }
  273. }
  274. }
  275. // Checking if in an instance
  276. if (player.getInstanceId() > 0) {
  277. Instance inst = InstanceManager.getInstance().getInstance(player.getInstanceId());
  278. if (inst != null) {
  279. loc = inst.getExitLoc();
  280. if (loc != null) {
  281. return loc;
  282. }
  283. }
  284. }
  285. }
  286. // Get the nearest town
  287. try {
  288. L2RespawnZone zone = ZoneManager.getInstance().getZone(activeChar, L2RespawnZone.class);
  289. if (zone != null) {
  290. return getRestartRegion(activeChar, zone.getRespawnPoint((L2PcInstance) activeChar)).getSpawnLoc();
  291. }
  292. return getMapRegion(activeChar).getSpawnLoc();
  293. } catch (Exception e) {
  294. // Port to the default respawn if no closest town found.
  295. return REGIONS.get(DEFAULT_RESPAWN).getSpawnLoc();
  296. }
  297. }
  298. public L2MapRegion getRestartRegion(L2Character activeChar, String point) {
  299. try {
  300. L2PcInstance player = ((L2PcInstance) activeChar);
  301. L2MapRegion region = REGIONS.get(point);
  302. if (region.getBannedRace().containsKey(player.getRace())) {
  303. getRestartRegion(player, region.getBannedRace().get(player.getRace()));
  304. }
  305. return region;
  306. } catch (Exception e) {
  307. return REGIONS.get(DEFAULT_RESPAWN);
  308. }
  309. }
  310. /**
  311. * @param regionName the map region name.
  312. * @return if exists the map region identified by that name, null otherwise.
  313. */
  314. public L2MapRegion getMapRegionByName(String regionName) {
  315. return REGIONS.get(regionName);
  316. }
  317. public static MapRegionManager getInstance() {
  318. return SingletonHolder.INSTANCE;
  319. }
  320. private static class SingletonHolder {
  321. protected static final MapRegionManager INSTANCE = new MapRegionManager();
  322. }
  323. }