MapRegionTable.java 16 KB

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