MapRegionTable.java 14 KB

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