2
0

MapRegionManager.java 16 KB

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