MapRegionManager.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.List;
  18. import java.util.Map;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javax.xml.parsers.DocumentBuilderFactory;
  22. import javolution.util.FastMap;
  23. import org.w3c.dom.Document;
  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.model.L2MapRegion;
  29. import com.l2jserver.gameserver.model.L2Object;
  30. import com.l2jserver.gameserver.model.Location;
  31. import com.l2jserver.gameserver.model.actor.L2Character;
  32. import com.l2jserver.gameserver.model.actor.L2Npc;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2SiegeFlagInstance;
  35. import com.l2jserver.gameserver.model.entity.Castle;
  36. import com.l2jserver.gameserver.model.entity.ClanHall;
  37. import com.l2jserver.gameserver.model.entity.Fort;
  38. import com.l2jserver.gameserver.model.entity.Instance;
  39. import com.l2jserver.gameserver.model.entity.clanhall.SiegableHall;
  40. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  41. import com.l2jserver.gameserver.model.zone.type.L2RespawnZone;
  42. import com.l2jserver.gameserver.util.Util;
  43. /**
  44. * @author Nyaran
  45. */
  46. public class MapRegionManager
  47. {
  48. protected static final Logger _log = Logger.getLogger(MapRegionManager.class.getName());
  49. private static Map<String, L2MapRegion> _regions;
  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. public static MapRegionManager getInstance()
  64. {
  65. return SingletonHolder._instance;
  66. }
  67. private MapRegionManager()
  68. {
  69. _regions = new FastMap<String, L2MapRegion>();
  70. try
  71. {
  72. load();
  73. }
  74. catch (Exception e)
  75. {
  76. _log.log(Level.SEVERE, "Failed loading MapRegion", e);
  77. }
  78. }
  79. private static void load()
  80. {
  81. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  82. factory.setValidating(false);
  83. factory.setIgnoringComments(true);
  84. for (File file : Util.getDatapackFiles("mapregion", ".xml"))
  85. {
  86. try
  87. {
  88. Document doc = factory.newDocumentBuilder().parse(file);
  89. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  90. {
  91. if ("list".equalsIgnoreCase(n.getNodeName()))
  92. {
  93. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  94. {
  95. if ("region".equalsIgnoreCase(d.getNodeName()))
  96. {
  97. NamedNodeMap attrs = d.getAttributes();
  98. Node att;
  99. String name = "";
  100. String town = "";
  101. int locId = -1;
  102. int castle = -1;
  103. int bbs = -1;
  104. att = attrs.getNamedItem("name");
  105. if (att == null)
  106. {
  107. _log.severe("Missing name for MapRegion, skipping");
  108. continue;
  109. }
  110. name = att.getNodeValue();
  111. att = attrs.getNamedItem("town");
  112. if (att == null)
  113. {
  114. _log.severe("Missing town for MapRegion name: " + name + ", skipping");
  115. continue;
  116. }
  117. town = att.getNodeValue();
  118. att = attrs.getNamedItem("locId");
  119. if (att == null)
  120. {
  121. _log.severe("Missing locId for MapRegion name: " + name + ", skipping");
  122. continue;
  123. }
  124. locId = Integer.parseInt(att.getNodeValue());
  125. att = attrs.getNamedItem("castle");
  126. if (att == null)
  127. {
  128. _log.severe("Missing castle for MapRegion name: " + name + ", skipping");
  129. continue;
  130. }
  131. castle = Integer.parseInt(att.getNodeValue());
  132. att = attrs.getNamedItem("bbs");
  133. if (att == null)
  134. {
  135. _log.severe("Missing bbs for MapRegion name: " + name + ", skipping");
  136. continue;
  137. }
  138. bbs = Integer.parseInt(att.getNodeValue());
  139. L2MapRegion region = new L2MapRegion(name, town, locId, castle, bbs);
  140. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  141. {
  142. if ("respawnPoint".equalsIgnoreCase(c.getNodeName()))
  143. {
  144. attrs = c.getAttributes();
  145. int spawnX = Integer.parseInt(attrs.getNamedItem("X").getNodeValue());
  146. int spawnY = Integer.parseInt(attrs.getNamedItem("Y").getNodeValue());
  147. int spawnZ = Integer.parseInt(attrs.getNamedItem("Z").getNodeValue());
  148. Node val = attrs.getNamedItem("isOther");
  149. boolean other = val != null && Boolean.parseBoolean(val.getNodeValue());
  150. val = attrs.getNamedItem("isChaotic");
  151. boolean chaotic = val != null && Boolean.parseBoolean(val.getNodeValue());
  152. val = attrs.getNamedItem("isBanish");
  153. boolean banish = val != null && Boolean.parseBoolean(val.getNodeValue());
  154. if (other)
  155. region.addOtherSpawn(spawnX, spawnY, spawnZ);
  156. else if (chaotic)
  157. region.addChaoticSpawn(spawnX, spawnY, spawnZ);
  158. else if (banish)
  159. region.addBanishSpawn(spawnX, spawnY, spawnZ);
  160. else
  161. region.addSpawn(spawnX, spawnY, spawnZ);
  162. }
  163. else if ("map".equalsIgnoreCase(c.getNodeName()))
  164. {
  165. attrs = c.getAttributes();
  166. int mapX = Integer.parseInt(attrs.getNamedItem("X").getNodeValue());
  167. int mapY = Integer.parseInt(attrs.getNamedItem("Y").getNodeValue());
  168. region.addMap(mapX, mapY);
  169. }
  170. else if ("banned".equalsIgnoreCase(c.getNodeName()))
  171. {
  172. attrs = c.getAttributes();
  173. String race = attrs.getNamedItem("race").getNodeValue();
  174. String point = attrs.getNamedItem("point").getNodeValue();
  175. region.addBannedRace(race, point);
  176. }
  177. }
  178. _regions.put(name, region);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. catch (Exception e)
  185. {
  186. _log.severe("MapRegion file (" + file.getAbsolutePath() + ") doesnt exists.");
  187. }
  188. }
  189. }
  190. public final L2MapRegion getMapRegion(int locX, int locY)
  191. {
  192. for (L2MapRegion region : _regions.values())
  193. {
  194. if (region.isZoneInRegion(getMapRegionX(locX), getMapRegionY(locY)))
  195. return region;
  196. }
  197. return null;
  198. }
  199. public final int getMapRegionLocId(int locX, int locY)
  200. {
  201. L2MapRegion region = getMapRegion(locX, locY);
  202. if (region != null)
  203. return region.getLocId();
  204. if (Config.DEBUG_PATH)
  205. _log.log(Level.WARNING, "MapRegionManager: Player outside map regions at X,Y=" + locX + "," + locY);
  206. return 0;
  207. }
  208. public final L2MapRegion getMapRegion(L2Object obj)
  209. {
  210. return getMapRegion(obj.getX(), obj.getY());
  211. }
  212. public final int getMapRegionLocId(L2Object obj)
  213. {
  214. return getMapRegionLocId(obj.getX(), obj.getY());
  215. }
  216. public final int getMapRegionX(int posX)
  217. {
  218. return (posX >> 15) + 9 + 11;// + centerTileX;
  219. }
  220. public final int getMapRegionY(int posY)
  221. {
  222. return (posY >> 15) + 10 + 8;// + centerTileX;
  223. }
  224. /**
  225. * Get town name by character position
  226. * @param activeChar
  227. * @return
  228. */
  229. public String getClosestTownName(L2Character activeChar)
  230. {
  231. L2MapRegion region = getMapRegion(activeChar);
  232. if (region == null)
  233. return "Aden Castle Town";
  234. return region.getTown();
  235. }
  236. public int getAreaCastle(L2Character activeChar)
  237. {
  238. L2MapRegion region = getMapRegion(activeChar);
  239. if (region == null)
  240. return 0;
  241. return region.getCastle();
  242. }
  243. public Location getTeleToLocation(L2Character activeChar, TeleportWhereType teleportWhere)
  244. {
  245. int[] coord;
  246. if (activeChar instanceof L2PcInstance)
  247. {
  248. L2PcInstance player = ((L2PcInstance) activeChar);
  249. Castle castle = null;
  250. Fort fort = null;
  251. ClanHall clanhall = null;
  252. if (player.getClan() != null && !player.isFlyingMounted() && !player.isFlying()) // flying players in gracia cant use teleports to aden continent
  253. {
  254. // If teleport to clan hall
  255. if (teleportWhere == TeleportWhereType.ClanHall)
  256. {
  257. clanhall = ClanHallManager.getInstance().getAbstractHallByOwner(player.getClan());
  258. if (clanhall != null)
  259. {
  260. L2ClanHallZone zone = clanhall.getZone();
  261. if (zone != null && !player.isFlyingMounted())
  262. {
  263. if (player.getKarma() > 0)
  264. return zone.getChaoticSpawnLoc();
  265. return zone.getSpawnLoc();
  266. }
  267. }
  268. }
  269. // If teleport to castle
  270. if (teleportWhere == TeleportWhereType.Castle)
  271. {
  272. castle = CastleManager.getInstance().getCastleByOwner(player.getClan());
  273. // Otherwise check if player is on castle or fortress ground
  274. // and player's clan is defender
  275. if (castle == null)
  276. {
  277. castle = CastleManager.getInstance().getCastle(player);
  278. if (!(castle != null && castle.getSiege().getIsInProgress() && castle.getSiege().getDefenderClan(player.getClan()) != null))
  279. castle = null;
  280. }
  281. if (castle != null && castle.getCastleId() > 0)
  282. {
  283. if (player.getKarma() > 0)
  284. return castle.getCastleZone().getChaoticSpawnLoc();
  285. return castle.getCastleZone().getSpawnLoc();
  286. }
  287. }
  288. // If teleport to fortress
  289. if (teleportWhere == TeleportWhereType.Fortress)
  290. {
  291. fort = FortManager.getInstance().getFortByOwner(player.getClan());
  292. // Otherwise check if player is on castle or fortress ground
  293. // and player's clan is defender
  294. if (fort == null)
  295. {
  296. fort = FortManager.getInstance().getFort(player);
  297. if (!(fort != null && fort.getSiege().getIsInProgress() && fort.getOwnerClan() == player.getClan()))
  298. fort = null;
  299. }
  300. if (fort != null && fort.getFortId() > 0)
  301. {
  302. if (player.getKarma() > 0)
  303. return fort.getFortZone().getChaoticSpawnLoc();
  304. return fort.getFortZone().getSpawnLoc();
  305. }
  306. }
  307. // If teleport to SiegeHQ
  308. if (teleportWhere == TeleportWhereType.SiegeFlag)
  309. {
  310. castle = CastleManager.getInstance().getCastle(player);
  311. fort = FortManager.getInstance().getFort(player);
  312. clanhall = ClanHallManager.getInstance().getNearbyAbstractHall(activeChar.getX(), activeChar.getY(), 10000);
  313. L2SiegeFlagInstance tw_flag = TerritoryWarManager.getInstance().getFlagForClan(player.getClan());
  314. if (tw_flag != null)
  315. return new Location(tw_flag.getX(), tw_flag.getY(), tw_flag.getZ());
  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. return castle.getCastleZone().getBanishSpawnLoc();
  361. }
  362. else if (teleportWhere == TeleportWhereType.Fortress_banish)
  363. {
  364. fort = FortManager.getInstance().getFort(activeChar);
  365. if (fort != null)
  366. return fort.getFortZone().getBanishSpawnLoc();
  367. }
  368. else if (teleportWhere == TeleportWhereType.ClanHall_banish)
  369. {
  370. clanhall = ClanHallManager.getInstance().getClanHall(activeChar);
  371. if (clanhall != null)
  372. return clanhall.getZone().getBanishSpawnLoc();
  373. }
  374. //Karma player land out of city
  375. if (player.getKarma() > 0)
  376. {
  377. try
  378. {
  379. L2RespawnZone zone = ZoneManager.getInstance().getZone(player, L2RespawnZone.class);
  380. if (zone != null)
  381. return getRestartRegion(activeChar, zone.getRespawnPoint((L2PcInstance) activeChar)).getChaoticSpawnLoc();
  382. return getMapRegion(activeChar).getChaoticSpawnLoc();
  383. }
  384. catch (Exception e)
  385. {
  386. if (player.isFlyingMounted()) // prevent flying players to teleport outside of gracia
  387. return _regions.get("union_base_of_kserth").getChaoticSpawnLoc();
  388. return _regions.get("talking_island_town").getChaoticSpawnLoc();
  389. }
  390. }
  391. //Checking if needed to be respawned in "far" town from the castle;
  392. castle = CastleManager.getInstance().getCastle(player);
  393. if (castle != null)
  394. {
  395. if (castle.getSiege().getIsInProgress())
  396. {
  397. // Check if player's clan is participating
  398. if ((castle.getSiege().checkIsDefender(player.getClan()) || castle.getSiege().checkIsAttacker(player.getClan())) && SevenSigns.getInstance().getSealOwner(SevenSigns.SEAL_STRIFE) == SevenSigns.CABAL_DAWN)
  399. return castle.getCastleZone().getOtherSpawnLoc();
  400. }
  401. }
  402. // Checking if in an instance
  403. if (player.getInstanceId() > 0)
  404. {
  405. Instance inst = InstanceManager.getInstance().getInstance(player.getInstanceId());
  406. if (inst != null)
  407. {
  408. coord = inst.getSpawnLoc();
  409. if (coord[0] != 0 && coord[1] != 0 && coord[2] != 0)
  410. return new Location(coord[0], coord[1], coord[2]);
  411. }
  412. }
  413. }
  414. // Get the nearest town
  415. try
  416. {
  417. L2RespawnZone zone = ZoneManager.getInstance().getZone(activeChar, L2RespawnZone.class);
  418. if (zone != null)
  419. return getRestartRegion(activeChar, zone.getRespawnPoint((L2PcInstance) activeChar)).getSpawnLoc();
  420. return getMapRegion(activeChar).getSpawnLoc();
  421. }
  422. catch (Exception e)
  423. {
  424. // port to the Talking Island if no closest town found
  425. if (Config.DEBUG)
  426. _log.log(Level.WARNING, "Not defined respawn point for coords loc X=" + activeChar.getX() + " Y=" + activeChar.getY() + " Z=" + activeChar.getZ());
  427. return _regions.get("talking_island_town").getSpawnLoc();
  428. }
  429. }
  430. public L2MapRegion getRestartRegion(L2Character activeChar, String point)
  431. {
  432. try
  433. {
  434. L2PcInstance player = ((L2PcInstance) activeChar);
  435. L2MapRegion region = _regions.get(point);
  436. if (region.getBannedRace().containsKey(player.getRace()))
  437. getRestartRegion(player, region.getBannedRace().get(player.getRace()));
  438. return region;
  439. }
  440. catch (Exception e)
  441. {
  442. return _regions.get("talking_island_town");
  443. }
  444. }
  445. /**
  446. * @param regionName the map region name.
  447. * @return if exists the map region identified by that name, null otherwise.
  448. */
  449. public L2MapRegion getMapRegionByName(String regionName)
  450. {
  451. return _regions.get(regionName);
  452. }
  453. @SuppressWarnings("synthetic-access")
  454. private static class SingletonHolder
  455. {
  456. protected static final MapRegionManager _instance = new MapRegionManager();
  457. }
  458. }