MapRegionTable.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 net.sf.l2j.gameserver.datatables;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import net.sf.l2j.Config;
  23. import net.sf.l2j.L2DatabaseFactory;
  24. import net.sf.l2j.gameserver.SevenSigns;
  25. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  26. import net.sf.l2j.gameserver.instancemanager.ClanHallManager;
  27. import net.sf.l2j.gameserver.instancemanager.FortManager;
  28. import net.sf.l2j.gameserver.instancemanager.InstanceManager;
  29. import net.sf.l2j.gameserver.instancemanager.TownManager;
  30. import net.sf.l2j.gameserver.instancemanager.ZoneManager;
  31. import net.sf.l2j.gameserver.model.Location;
  32. import net.sf.l2j.gameserver.model.actor.L2Character;
  33. import net.sf.l2j.gameserver.model.actor.L2Npc;
  34. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  35. import net.sf.l2j.gameserver.model.entity.Castle;
  36. import net.sf.l2j.gameserver.model.entity.ClanHall;
  37. import net.sf.l2j.gameserver.model.entity.Fort;
  38. import net.sf.l2j.gameserver.model.zone.type.L2ArenaZone;
  39. import net.sf.l2j.gameserver.model.zone.type.L2ClanHallZone;
  40. /**
  41. * This class ...
  42. */
  43. public class MapRegionTable
  44. {
  45. private static Logger _log = Logger.getLogger(MapRegionTable.class.getName());
  46. private final int[][] _regions = new int[16][18];
  47. private final int[][] _pointsWithKarmas;
  48. public static enum TeleportWhereType
  49. {
  50. Castle,
  51. ClanHall,
  52. SiegeFlag,
  53. Town,
  54. Fortress
  55. }
  56. public static MapRegionTable getInstance()
  57. {
  58. return SingletonHolder._instance;
  59. }
  60. private MapRegionTable()
  61. {
  62. int count2 = 0;
  63. //LineNumberReader lnr = null;
  64. Connection con = null;
  65. try
  66. {
  67. con = L2DatabaseFactory.getInstance().getConnection();
  68. PreparedStatement statement = con.prepareStatement("SELECT region, sec0, sec1, sec2, sec3, sec4, sec5, sec6, sec7, sec8, sec9, sec10,sec11,sec12,sec13,sec14,sec15 FROM mapregion");
  69. ResultSet rset = statement.executeQuery();
  70. int region;
  71. while (rset.next())
  72. {
  73. region = rset.getInt(1);
  74. for (int j = 0; j < 16; j++)
  75. {
  76. _regions[j][region] = rset.getInt(j + 2);
  77. count2++;
  78. //_log.fine(j+","+region+" -> "+rset.getInt(j+2));
  79. }
  80. }
  81. rset.close();
  82. statement.close();
  83. if (Config.DEBUG)
  84. _log.fine(count2 + " mapregion loaded");
  85. }
  86. catch (Exception e)
  87. {
  88. _log.log(Level.SEVERE, "Error loading Map Region Table.", e);
  89. }
  90. finally
  91. {
  92. try
  93. {
  94. con.close();
  95. }
  96. catch (Exception e)
  97. {
  98. }
  99. }
  100. _pointsWithKarmas = new int[21][3];
  101. //Talking Island
  102. _pointsWithKarmas[0][0] = -79077;
  103. _pointsWithKarmas[0][1] = 240355;
  104. _pointsWithKarmas[0][2] = -3440;
  105. //Elven
  106. _pointsWithKarmas[1][0] = 43503;
  107. _pointsWithKarmas[1][1] = 40398;
  108. _pointsWithKarmas[1][2] = -3450;
  109. //DarkElven
  110. _pointsWithKarmas[2][0] = 1675;
  111. _pointsWithKarmas[2][1] = 19581;
  112. _pointsWithKarmas[2][2] = -3110;
  113. //Orc
  114. _pointsWithKarmas[3][0] = -44413;
  115. _pointsWithKarmas[3][1] = -121762;
  116. _pointsWithKarmas[3][2] = -235;
  117. //Dwalf
  118. _pointsWithKarmas[4][0] = 12009;
  119. _pointsWithKarmas[4][1] = -187319;
  120. _pointsWithKarmas[4][2] = -3309;
  121. //Gludio
  122. _pointsWithKarmas[5][0] = -18872;
  123. _pointsWithKarmas[5][1] = 126216;
  124. _pointsWithKarmas[5][2] = -3280;
  125. //Gludin
  126. _pointsWithKarmas[6][0] = -85915;
  127. _pointsWithKarmas[6][1] = 150402;
  128. _pointsWithKarmas[6][2] = -3060;
  129. //Dion
  130. _pointsWithKarmas[7][0] = 23652;
  131. _pointsWithKarmas[7][1] = 144823;
  132. _pointsWithKarmas[7][2] = -3330;
  133. //Giran
  134. _pointsWithKarmas[8][0] = 79125;
  135. _pointsWithKarmas[8][1] = 154197;
  136. _pointsWithKarmas[8][2] = -3490;
  137. //Oren
  138. _pointsWithKarmas[9][0] = 73840;
  139. _pointsWithKarmas[9][1] = 58193;
  140. _pointsWithKarmas[9][2] = -2730;
  141. //Aden
  142. _pointsWithKarmas[10][0] = 44413;
  143. _pointsWithKarmas[10][1] = 22610;
  144. _pointsWithKarmas[10][2] = 235;
  145. //Hunters
  146. _pointsWithKarmas[11][0] = 114137;
  147. _pointsWithKarmas[11][1] = 72993;
  148. _pointsWithKarmas[11][2] = -2445;
  149. //Giran
  150. _pointsWithKarmas[12][0] = 79125;
  151. _pointsWithKarmas[12][1] = 154197;
  152. _pointsWithKarmas[12][2] = -3490;
  153. // heine
  154. _pointsWithKarmas[13][0] = 119536;
  155. _pointsWithKarmas[13][1] = 218558;
  156. _pointsWithKarmas[13][2] = -3495;
  157. // Rune Castle Town
  158. _pointsWithKarmas[14][0] = 42931;
  159. _pointsWithKarmas[14][1] = -44733;
  160. _pointsWithKarmas[14][2] = -1326;
  161. // Goddard
  162. _pointsWithKarmas[15][0] = 147419;
  163. _pointsWithKarmas[15][1] = -64980;
  164. _pointsWithKarmas[15][2] = -3457;
  165. // Schuttgart
  166. _pointsWithKarmas[16][0] = 85184;
  167. _pointsWithKarmas[16][1] = -138560;
  168. _pointsWithKarmas[16][2] = -2256;
  169. // Kamael Village
  170. _pointsWithKarmas[19][0] = -121425;
  171. _pointsWithKarmas[19][1] = 59778;
  172. _pointsWithKarmas[19][2] = -2264;
  173. }
  174. public final int getMapRegion(int posX, int posY)
  175. {
  176. try
  177. {
  178. return _regions[getMapRegionX(posX)][getMapRegionY(posY)];
  179. }
  180. catch (ArrayIndexOutOfBoundsException e)
  181. {
  182. // Position sent is outside MapRegionTable area.
  183. if (Config.DEBUG)
  184. _log.log(Level.WARNING, "MapRegionTable: Player outside map regions at X,Y=" + posX + "," + posY, e);
  185. return 0;
  186. }
  187. }
  188. public final int getMapRegionX(int posX)
  189. {
  190. return (posX >> 15) + 9;// + centerTileX;
  191. }
  192. public final int getMapRegionY(int posY)
  193. {
  194. return (posY >> 15) + 10;// + centerTileX;
  195. }
  196. public int getAreaCastle(L2Character activeChar)
  197. {
  198. int area = getClosestTownNumber(activeChar);
  199. int castle;
  200. switch (area)
  201. {
  202. case 0:
  203. castle = 1;
  204. break;//Talking Island Village
  205. case 1:
  206. castle = 4;
  207. break; //Elven Village
  208. case 2:
  209. castle = 4;
  210. break; //Dark Elven Village
  211. case 3:
  212. castle = 9;
  213. break; //Orc Village
  214. case 4:
  215. castle = 9;
  216. break; //Dwarven Village
  217. case 5:
  218. castle = 1;
  219. break; //Town of Gludio
  220. case 6:
  221. castle = 1;
  222. break; //Gludin Village
  223. case 7:
  224. castle = 2;
  225. break; //Town of Dion
  226. case 8:
  227. castle = 3;
  228. break; //Town of Giran
  229. case 9:
  230. castle = 4;
  231. break; //Town of Oren
  232. case 10:
  233. castle = 5;
  234. break; //Town of Aden
  235. case 11:
  236. castle = 5;
  237. break; //Hunters Village
  238. case 12:
  239. castle = 3;
  240. break; //Giran Harbor
  241. case 13:
  242. castle = 6;
  243. break; //Heine
  244. case 14:
  245. castle = 8;
  246. break; //Rune Township
  247. case 15:
  248. castle = 7;
  249. break; //Town of Goddard
  250. case 16:
  251. castle = 9;
  252. break; //Town of Shuttgart
  253. case 17:
  254. castle = 4;
  255. break; //Ivory Tower
  256. case 18:
  257. castle = 8;
  258. break; //Primeval Isle Wharf
  259. case 19:
  260. castle = 5;
  261. break; //Kamael Village
  262. case 20:
  263. castle = 6;
  264. break; //South of Wastelands Camp
  265. case 21:
  266. castle = 8;
  267. break; //Fantasy Island
  268. default:
  269. castle = 5;
  270. break; //Town of Aden
  271. }
  272. return castle;
  273. }
  274. public int getClosestTownNumber(L2Character activeChar)
  275. {
  276. return getMapRegion(activeChar.getX(), activeChar.getY());
  277. }
  278. public String getClosestTownName(L2Character activeChar)
  279. {
  280. int nearestTownId = getMapRegion(activeChar.getX(), activeChar.getY());
  281. String nearestTown;
  282. switch (nearestTownId)
  283. {
  284. case 0:
  285. nearestTown = "Talking Island Village";
  286. break;
  287. case 1:
  288. nearestTown = "Elven Village";
  289. break;
  290. case 2:
  291. nearestTown = "Dark Elven Village";
  292. break;
  293. case 3:
  294. nearestTown = "Orc Village";
  295. break;
  296. case 4:
  297. nearestTown = "Dwarven Village";
  298. break;
  299. case 5:
  300. nearestTown = "Town of Gludio";
  301. break;
  302. case 6:
  303. nearestTown = "Gludin Village";
  304. break;
  305. case 7:
  306. nearestTown = "Town of Dion";
  307. break;
  308. case 8:
  309. nearestTown = "Town of Giran";
  310. break;
  311. case 9:
  312. nearestTown = "Town of Oren";
  313. break;
  314. case 10:
  315. nearestTown = "Town of Aden";
  316. break;
  317. case 11:
  318. nearestTown = "Hunters Village";
  319. break;
  320. case 12:
  321. nearestTown = "Giran Harbor";
  322. break;
  323. case 13:
  324. nearestTown = "Heine";
  325. break;
  326. case 14:
  327. nearestTown = "Rune Township";
  328. break;
  329. case 15:
  330. nearestTown = "Town of Goddard";
  331. break;
  332. case 16:
  333. nearestTown = "Town of Shuttgart";
  334. break;
  335. case 18:
  336. nearestTown = "Primeval Isle";
  337. break;
  338. case 19:
  339. nearestTown = "Kamael Village";
  340. break;
  341. case 20:
  342. nearestTown = "South of Wastelands Camp";
  343. break;
  344. case 21:
  345. nearestTown = "Fantasy Island";
  346. break;
  347. case 22:
  348. nearestTown = "Neutral Zone";
  349. break;
  350. case 23:
  351. nearestTown = "Coliseum";
  352. break;
  353. case 24:
  354. nearestTown = "GM Consultation service";
  355. break;
  356. case 25:
  357. nearestTown = "Dimensional Gap";
  358. break;
  359. case 26:
  360. nearestTown = "Cemetery of the Empire";
  361. break;
  362. case 27:
  363. nearestTown = "inside the Steel Citadel";
  364. break;
  365. case 28:
  366. nearestTown = "Steel Citadel Resistance";
  367. break;
  368. case 29:
  369. nearestTown = "Inside Kamaloka";
  370. break;
  371. case 30:
  372. nearestTown = "Inside Nia Kamaloka";
  373. break;
  374. case 31:
  375. nearestTown = "Inside Rim Kamaloka";
  376. break;
  377. case 32:
  378. nearestTown = "Keucereus clan association";
  379. break;
  380. case 33:
  381. nearestTown = "inside the Seed of Infinity";
  382. break;
  383. case 34:
  384. nearestTown = "outside the Seed of Infinity";
  385. break;
  386. case 35:
  387. nearestTown = "Aerial Cleft";
  388. break;
  389. default:
  390. nearestTown = "Town of Aden";
  391. break;
  392. }
  393. return nearestTown;
  394. }
  395. public Location getTeleToLocation(L2Character activeChar, TeleportWhereType teleportWhere)
  396. {
  397. int[] coord;
  398. if (activeChar instanceof L2PcInstance)
  399. {
  400. L2PcInstance player = ((L2PcInstance) activeChar);
  401. // If in Monster Derby Track
  402. if (player.isInsideZone(L2Character.ZONE_MONSTERTRACK))
  403. return new Location(12661, 181687, -3560);
  404. Castle castle = null;
  405. Fort fort = null;
  406. ClanHall clanhall = null;
  407. if (player.getClan() != null && !player.isFlyingMounted()) // flying players in gracia cant use teleports to aden continent
  408. {
  409. // If teleport to clan hall
  410. if (teleportWhere == TeleportWhereType.ClanHall)
  411. {
  412. clanhall = ClanHallManager.getInstance().getClanHallByOwner(player.getClan());
  413. if (clanhall != null)
  414. {
  415. L2ClanHallZone zone = clanhall.getZone();
  416. if (zone != null && !player.isFlyingMounted())
  417. {
  418. return zone.getSpawn();
  419. }
  420. }
  421. }
  422. // If teleport to castle
  423. if (teleportWhere == TeleportWhereType.Castle)
  424. {
  425. castle = CastleManager.getInstance().getCastleByOwner(player.getClan());
  426. // Otherwise check if player is on castle or fortress ground
  427. // and player's clan is defender
  428. if (castle == null)
  429. {
  430. castle = CastleManager.getInstance().getCastle(player);
  431. if (!(castle != null && castle.getSiege().getIsInProgress() && castle.getSiege().getDefenderClan(player.getClan()) != null))
  432. castle = null;
  433. }
  434. if (castle != null && castle.getCastleId() > 0)
  435. {
  436. coord = castle.getZone().getSpawn();
  437. return new Location(coord[0], coord[1], coord[2]);
  438. }
  439. }
  440. // If teleport to fortress
  441. if (teleportWhere == TeleportWhereType.Fortress)
  442. {
  443. fort = FortManager.getInstance().getFortByOwner(player.getClan());
  444. // Otherwise check if player is on castle or fortress ground
  445. // and player's clan is defender
  446. if (fort == null)
  447. {
  448. fort = FortManager.getInstance().getFort(player);
  449. if (!(fort != null && fort.getSiege().getIsInProgress() && fort.getOwnerClan() == player.getClan()))
  450. fort = null;
  451. }
  452. if (fort != null && fort.getFortId() > 0)
  453. {
  454. coord = fort.getZone().getSpawn();
  455. return new Location(coord[0], coord[1], coord[2]);
  456. }
  457. }
  458. // If teleport to SiegeHQ
  459. if (teleportWhere == TeleportWhereType.SiegeFlag)
  460. {
  461. castle = CastleManager.getInstance().getCastle(player);
  462. fort = FortManager.getInstance().getFort(player);
  463. if (castle != null)
  464. {
  465. if (castle.getSiege().getIsInProgress())
  466. {
  467. // Check if player's clan is attacker
  468. List<L2Npc> flags = castle.getSiege().getFlag(player.getClan());
  469. if (flags != null && !flags.isEmpty())
  470. {
  471. // Spawn to flag - Need more work to get player to the nearest flag
  472. L2Npc flag = flags.get(0);
  473. return new Location(flag.getX(), flag.getY(), flag.getZ());
  474. }
  475. }
  476. }
  477. else if (fort != null)
  478. {
  479. if (fort.getSiege().getIsInProgress())
  480. {
  481. // Check if player's clan is attacker
  482. List<L2Npc> flags = fort.getSiege().getFlag(player.getClan());
  483. if (flags != null && !flags.isEmpty())
  484. {
  485. // Spawn to flag - Need more work to get player to the nearest flag
  486. L2Npc flag = flags.get(0);
  487. return new Location(flag.getX(), flag.getY(), flag.getZ());
  488. }
  489. }
  490. }
  491. }
  492. }
  493. //Karma player land out of city
  494. if (player.getKarma() > 1)
  495. {
  496. int closest = getMapRegion(activeChar.getX(), activeChar.getY());
  497. if (closest >= 0 && closest < _pointsWithKarmas.length)
  498. return new Location(_pointsWithKarmas[closest][0], _pointsWithKarmas[closest][1], _pointsWithKarmas[closest][2]);
  499. else if (player.isFlyingMounted()) // prevent flying players to teleport outside of gracia
  500. return new Location(-186330, 242944, 2544);
  501. else
  502. return new Location(17817, 170079, -3530);
  503. }
  504. // Checking if in arena
  505. L2ArenaZone arena = ZoneManager.getInstance().getArena(player);
  506. if (arena != null)
  507. {
  508. coord = arena.getSpawnLoc();
  509. return new Location(coord[0], coord[1], coord[2]);
  510. }
  511. //Checking if needed to be respawned in "far" town from the castle;
  512. castle = CastleManager.getInstance().getCastle(player);
  513. if (castle != null)
  514. {
  515. if (castle.getSiege().getIsInProgress())
  516. {
  517. // Check if player's clan is participating
  518. if ((castle.getSiege().checkIsDefender(player.getClan()) || castle.getSiege().checkIsAttacker(player.getClan()))
  519. && SevenSigns.getInstance().getSealOwner(SevenSigns.SEAL_STRIFE) == SevenSigns.CABAL_DAWN)
  520. {
  521. coord = TownManager.getSecondClosestTown(activeChar).getSpawnLoc();
  522. return new Location(coord[0], coord[1], coord[2]);
  523. }
  524. }
  525. }
  526. // Checking if in an instance
  527. if (player.getInstanceId() > 0)
  528. {
  529. coord = InstanceManager.getInstance().getInstance(player.getInstanceId()).getSpawnLoc();
  530. if (coord[0] != 0 && coord[1] != 0 && coord[2] != 0)
  531. {
  532. return new Location(coord[0], coord[1], coord[2]);
  533. }
  534. }
  535. }
  536. // Get the nearest town
  537. // TODO: Micht: Maybe we should add some checks to prevent exception here.
  538. coord = TownManager.getClosestTown(activeChar).getSpawnLoc();
  539. return new Location(coord[0], coord[1], coord[2]);
  540. }
  541. @SuppressWarnings("synthetic-access")
  542. private static class SingletonHolder
  543. {
  544. protected static final MapRegionTable _instance = new MapRegionTable();
  545. }
  546. }