MapRegionTable.java 14 KB

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