MapRegionTable.java 13 KB

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