MapRegionManager.java 15 KB

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