MapRegionManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 org.w3c.dom.Document;
  23. import org.w3c.dom.NamedNodeMap;
  24. import org.w3c.dom.Node;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.gameserver.SevenSigns;
  27. import com.l2jserver.gameserver.model.L2MapRegion;
  28. import com.l2jserver.gameserver.model.L2Object;
  29. import com.l2jserver.gameserver.model.Location;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.actor.L2Npc;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.actor.instance.L2SiegeFlagInstance;
  34. import com.l2jserver.gameserver.model.entity.Castle;
  35. import com.l2jserver.gameserver.model.entity.ClanHall;
  36. import com.l2jserver.gameserver.model.entity.Fort;
  37. import com.l2jserver.gameserver.model.entity.Instance;
  38. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  39. import com.l2jserver.gameserver.model.zone.type.L2RespawnZone;
  40. import javolution.util.FastMap;
  41. /**
  42. * @author Nyaran
  43. *
  44. */
  45. public class MapRegionManager
  46. {
  47. protected static final Logger _log = Logger.getLogger(MapRegionManager.class.getName());
  48. private static Map<String, L2MapRegion> _regions;
  49. private static final String FILE = "MapRegion.xml";
  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. e.printStackTrace();
  77. _log.log(Level.SEVERE, "Failed loading MapRegion", e);
  78. }
  79. }
  80. private static void load() throws Exception
  81. {
  82. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  83. factory.setValidating(false);
  84. factory.setIgnoringComments(true);
  85. File file = new File(Config.DATAPACK_ROOT + "/data/" + FILE);
  86. if (file.exists())
  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. else
  185. _log.severe("MapRegion file (" + file.getAbsolutePath() + ") doesnt exists.");
  186. }
  187. public final L2MapRegion getMapRegion(int locX, int locY)
  188. {
  189. for (L2MapRegion region : _regions.values())
  190. {
  191. if (region.isZoneInRegion(getMapRegionX(locX), getMapRegionY(locY)))
  192. return region;
  193. }
  194. return null;
  195. }
  196. public final int getMapRegionLocId(int locX, int locY)
  197. {
  198. L2MapRegion region = getMapRegion(locX, locY);
  199. if (region != null)
  200. return region.getLocId();
  201. if (Config.DEBUG_PATH)
  202. _log.log(Level.WARNING, "MapRegionManager: Player outside map regions at X,Y=" + locX + "," + locY);
  203. return 0;
  204. }
  205. public final L2MapRegion getMapRegion(L2Object obj)
  206. {
  207. return getMapRegion(obj.getX(), obj.getY());
  208. }
  209. public final int getMapRegionLocId(L2Object obj)
  210. {
  211. return getMapRegionLocId(obj.getX(), obj.getY());
  212. }
  213. public final int getMapRegionX(int posX)
  214. {
  215. return (posX >> 15) + 9 + 11;// + centerTileX;
  216. }
  217. public final int getMapRegionY(int posY)
  218. {
  219. return (posY >> 15) + 10 + 8;// + centerTileX;
  220. }
  221. /**
  222. * Get town name by character position
  223. * @param townId
  224. * @return String
  225. */
  226. public String getClosestTownName(L2Character activeChar)
  227. {
  228. L2MapRegion region = getMapRegion(activeChar);
  229. if (region == null)
  230. return "Aden Castle Town";
  231. return region.getTown();
  232. }
  233. public int getAreaCastle(L2Character activeChar)
  234. {
  235. L2MapRegion region = getMapRegion(activeChar);
  236. if (region == null)
  237. return 0;
  238. return region.getCastle();
  239. }
  240. public Location getTeleToLocation(L2Character activeChar, TeleportWhereType teleportWhere)
  241. {
  242. int[] coord;
  243. if (activeChar instanceof L2PcInstance)
  244. {
  245. L2PcInstance player = ((L2PcInstance) activeChar);
  246. Castle castle = null;
  247. Fort fort = null;
  248. ClanHall clanhall = null;
  249. if (player.getClan() != null && !player.isFlyingMounted() && !player.isFlying()) // flying players in gracia cant use teleports to aden continent
  250. {
  251. // If teleport to clan hall
  252. if (teleportWhere == TeleportWhereType.ClanHall)
  253. {
  254. clanhall = ClanHallManager.getInstance().getClanHallByOwner(player.getClan());
  255. if (clanhall != null)
  256. {
  257. L2ClanHallZone zone = clanhall.getZone();
  258. if (zone != null && !player.isFlyingMounted())
  259. {
  260. if (player.getKarma() > 0)
  261. return zone.getChaoticSpawnLoc();
  262. else
  263. return zone.getSpawnLoc();
  264. }
  265. }
  266. }
  267. // If teleport to castle
  268. if (teleportWhere == TeleportWhereType.Castle)
  269. {
  270. castle = CastleManager.getInstance().getCastleByOwner(player.getClan());
  271. // Otherwise check if player is on castle or fortress ground
  272. // and player's clan is defender
  273. if (castle == null)
  274. {
  275. castle = CastleManager.getInstance().getCastle(player);
  276. if (!(castle != null && castle.getSiege().getIsInProgress() && castle.getSiege().getDefenderClan(player.getClan()) != null))
  277. castle = null;
  278. }
  279. if (castle != null && castle.getCastleId() > 0)
  280. {
  281. if (player.getKarma() > 0)
  282. return castle.getCastleZone().getChaoticSpawnLoc();
  283. else
  284. return castle.getCastleZone().getSpawnLoc();
  285. }
  286. }
  287. // If teleport to fortress
  288. if (teleportWhere == TeleportWhereType.Fortress)
  289. {
  290. fort = FortManager.getInstance().getFortByOwner(player.getClan());
  291. // Otherwise check if player is on castle or fortress ground
  292. // and player's clan is defender
  293. if (fort == null)
  294. {
  295. fort = FortManager.getInstance().getFort(player);
  296. if (!(fort != null && fort.getSiege().getIsInProgress() && fort.getOwnerClan() == player.getClan()))
  297. fort = null;
  298. }
  299. if (fort != null && fort.getFortId() > 0)
  300. {
  301. if (player.getKarma() > 0)
  302. return fort.getFortZone().getChaoticSpawnLoc();
  303. else
  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. L2SiegeFlagInstance tw_flag = TerritoryWarManager.getInstance().getFlagForClan(player.getClan());
  313. if (tw_flag != null)
  314. return new Location(tw_flag.getX(), tw_flag.getY(), tw_flag.getZ());
  315. else if (castle != null)
  316. {
  317. if (castle.getSiege().getIsInProgress())
  318. {
  319. // Check if player's clan is attacker
  320. List<L2Npc> flags = castle.getSiege().getFlag(player.getClan());
  321. if (flags != null && !flags.isEmpty())
  322. {
  323. // Spawn to flag - Need more work to get player to the nearest flag
  324. L2Npc flag = flags.get(0);
  325. return new Location(flag.getX(), flag.getY(), flag.getZ());
  326. }
  327. }
  328. }
  329. else if (fort != null)
  330. {
  331. if (fort.getSiege().getIsInProgress())
  332. {
  333. // Check if player's clan is attacker
  334. List<L2Npc> flags = fort.getSiege().getFlag(player.getClan());
  335. if (flags != null && !flags.isEmpty())
  336. {
  337. // Spawn to flag - Need more work to get player to the nearest flag
  338. L2Npc flag = flags.get(0);
  339. return new Location(flag.getX(), flag.getY(), flag.getZ());
  340. }
  341. }
  342. }
  343. }
  344. }
  345. if (teleportWhere == TeleportWhereType.Castle_banish)
  346. {
  347. castle = CastleManager.getInstance().getCastle(player);
  348. if (castle != null)
  349. return castle.getCastleZone().getBanishSpawnLoc();
  350. }
  351. else if (teleportWhere == TeleportWhereType.Fortress_banish)
  352. {
  353. fort = FortManager.getInstance().getFort(activeChar);
  354. if (fort != null)
  355. return fort.getFortZone().getBanishSpawnLoc();
  356. }
  357. else if (teleportWhere == TeleportWhereType.ClanHall_banish)
  358. {
  359. clanhall = ClanHallManager.getInstance().getClanHall(activeChar);
  360. if (clanhall != null)
  361. return clanhall.getZone().getBanishSpawnLoc();
  362. }
  363. //Karma player land out of city
  364. if (player.getKarma() > 0)
  365. {
  366. try
  367. {
  368. L2RespawnZone zone = ZoneManager.getInstance().getZone(player, L2RespawnZone.class);
  369. if (zone != null)
  370. return getRestartRegion(activeChar, zone.getRespawnPoint((L2PcInstance) activeChar)).getChaoticSpawnLoc();
  371. else
  372. return getMapRegion(activeChar).getChaoticSpawnLoc();
  373. }
  374. catch (Exception e)
  375. {
  376. if (player.isFlyingMounted()) // prevent flying players to teleport outside of gracia
  377. return _regions.get("union_base_of_kserth").getChaoticSpawnLoc();
  378. else
  379. return _regions.get("talking_island_town").getChaoticSpawnLoc();
  380. }
  381. }
  382. //Checking if needed to be respawned in "far" town from the castle;
  383. castle = CastleManager.getInstance().getCastle(player);
  384. if (castle != null)
  385. {
  386. if (castle.getSiege().getIsInProgress())
  387. {
  388. // Check if player's clan is participating
  389. if ((castle.getSiege().checkIsDefender(player.getClan()) || castle.getSiege().checkIsAttacker(player.getClan())) && SevenSigns.getInstance().getSealOwner(SevenSigns.SEAL_STRIFE) == SevenSigns.CABAL_DAWN)
  390. return castle.getCastleZone().getOtherSpawnLoc();
  391. }
  392. }
  393. // Checking if in an instance
  394. if (player.getInstanceId() > 0)
  395. {
  396. Instance inst = InstanceManager.getInstance().getInstance(player.getInstanceId());
  397. if (inst != null)
  398. {
  399. coord = inst.getSpawnLoc();
  400. if (coord[0] != 0 && coord[1] != 0 && coord[2] != 0)
  401. return new Location(coord[0], coord[1], coord[2]);
  402. }
  403. }
  404. }
  405. // Get the nearest town
  406. try
  407. {
  408. L2RespawnZone zone = ZoneManager.getInstance().getZone(activeChar, L2RespawnZone.class);
  409. if (zone != null)
  410. return getRestartRegion(activeChar, zone.getRespawnPoint((L2PcInstance) activeChar)).getSpawnLoc();
  411. else
  412. return getMapRegion(activeChar).getSpawnLoc();
  413. }
  414. catch (Exception e)
  415. {
  416. // port to the Talking Island if no closest town found
  417. if (Config.DEBUG)
  418. _log.log(Level.WARNING, "Not defined respawn point for coords loc X=" + activeChar.getX() + " Y=" + activeChar.getY() + " Z=" + activeChar.getZ());
  419. return _regions.get("talking_island_town").getSpawnLoc();
  420. }
  421. }
  422. public L2MapRegion getRestartRegion(L2Character activeChar, String point)
  423. {
  424. try
  425. {
  426. L2PcInstance player = ((L2PcInstance) activeChar);
  427. L2MapRegion region = _regions.get(point);
  428. if (region.getBannedRace().containsKey(player.getRace()))
  429. getRestartRegion(player, region.getBannedRace().get(player.getRace()));
  430. return region;
  431. }
  432. catch (Exception e)
  433. {
  434. return _regions.get("talking_island_town");
  435. }
  436. }
  437. @SuppressWarnings("synthetic-access")
  438. private static class SingletonHolder
  439. {
  440. protected static final MapRegionManager _instance = new MapRegionManager();
  441. }
  442. }