MapRegionTable.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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.Logger;
  20. import net.sf.l2j.Config;
  21. import net.sf.l2j.L2DatabaseFactory;
  22. import net.sf.l2j.gameserver.instancemanager.ArenaManager;
  23. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  24. import net.sf.l2j.gameserver.instancemanager.ClanHallManager;
  25. import net.sf.l2j.gameserver.instancemanager.FortManager;
  26. import net.sf.l2j.gameserver.instancemanager.TownManager;
  27. import net.sf.l2j.gameserver.model.L2Character;
  28. import net.sf.l2j.gameserver.model.Location;
  29. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  30. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  31. import net.sf.l2j.gameserver.model.entity.Castle;
  32. import net.sf.l2j.gameserver.model.entity.ClanHall;
  33. import net.sf.l2j.gameserver.model.entity.Fort;
  34. import net.sf.l2j.gameserver.model.zone.type.L2ArenaZone;
  35. import net.sf.l2j.gameserver.model.zone.type.L2ClanHallZone;
  36. /**
  37. * This class ...
  38. */
  39. public class MapRegionTable
  40. {
  41. private static Logger _log = Logger.getLogger(MapRegionTable.class.getName());
  42. private static MapRegionTable _instance;
  43. private final int[][] _regions = new int[19][21];
  44. private final int[][] _pointsWithKarmas;
  45. public static enum TeleportWhereType
  46. {
  47. Castle,
  48. ClanHall,
  49. SiegeFlag,
  50. Town,
  51. Fortress
  52. }
  53. public static MapRegionTable getInstance()
  54. {
  55. if (_instance == null)
  56. {
  57. _instance = new MapRegionTable();
  58. }
  59. return _instance;
  60. }
  61. private MapRegionTable()
  62. {
  63. int count2 = 0;
  64. //LineNumberReader lnr = null;
  65. java.sql.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 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<10; 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) _log.fine(count2 +" mapregion loaded");
  85. }
  86. catch (Exception e)
  87. {
  88. _log.warning("error while creating map region data: "+e);
  89. }
  90. finally
  91. {
  92. try { con.close(); } catch (Exception e) {}
  93. }
  94. _pointsWithKarmas = new int[21][3];
  95. //Talking Island
  96. _pointsWithKarmas[0][0] = -79077;
  97. _pointsWithKarmas[0][1] = 240355;
  98. _pointsWithKarmas[0][2] = -3440;
  99. //Elven
  100. _pointsWithKarmas[1][0] = 43503;
  101. _pointsWithKarmas[1][1] = 40398;
  102. _pointsWithKarmas[1][2] = -3450;
  103. //DarkElven
  104. _pointsWithKarmas[2][0] = 1675;
  105. _pointsWithKarmas[2][1] = 19581;
  106. _pointsWithKarmas[2][2] = -3110;
  107. //Orc
  108. _pointsWithKarmas[3][0] = -44413;
  109. _pointsWithKarmas[3][1] = -121762;
  110. _pointsWithKarmas[3][2] = -235;
  111. //Dwalf
  112. _pointsWithKarmas[4][0] = 12009;
  113. _pointsWithKarmas[4][1] = -187319;
  114. _pointsWithKarmas[4][2] = -3309;
  115. //Gludio
  116. _pointsWithKarmas[5][0] = -18872;
  117. _pointsWithKarmas[5][1] = 126216;
  118. _pointsWithKarmas[5][2] = -3280;
  119. //Gludin
  120. _pointsWithKarmas[6][0] = -85915;
  121. _pointsWithKarmas[6][1] = 150402;
  122. _pointsWithKarmas[6][2] = -3060;
  123. //Dion
  124. _pointsWithKarmas[7][0] = 23652;
  125. _pointsWithKarmas[7][1] = 144823;
  126. _pointsWithKarmas[7][2] = -3330;
  127. //Giran
  128. _pointsWithKarmas[8][0] = 79125;
  129. _pointsWithKarmas[8][1] = 154197;
  130. _pointsWithKarmas[8][2] = -3490;
  131. //Oren
  132. _pointsWithKarmas[9][0] = 73840;
  133. _pointsWithKarmas[9][1] = 58193;
  134. _pointsWithKarmas[9][2] = -2730;
  135. //Aden
  136. _pointsWithKarmas[10][0] = 44413;
  137. _pointsWithKarmas[10][1] = 22610;
  138. _pointsWithKarmas[10][2] = 235;
  139. //Hunters
  140. _pointsWithKarmas[11][0] = 114137;
  141. _pointsWithKarmas[11][1] = 72993;
  142. _pointsWithKarmas[11][2] = -2445;
  143. //Giran
  144. _pointsWithKarmas[12][0] = 79125;
  145. _pointsWithKarmas[12][1] = 154197;
  146. _pointsWithKarmas[12][2] = -3490;
  147. // heine
  148. _pointsWithKarmas[13][0] = 119536;
  149. _pointsWithKarmas[13][1] = 218558;
  150. _pointsWithKarmas[13][2] = -3495;
  151. // Rune Castle Town
  152. _pointsWithKarmas[14][0] = 42931;
  153. _pointsWithKarmas[14][1] = -44733;
  154. _pointsWithKarmas[14][2] = -1326;
  155. // Goddard
  156. _pointsWithKarmas[15][0] = 147419;
  157. _pointsWithKarmas[15][1] = -64980;
  158. _pointsWithKarmas[15][2] = -3457;
  159. // Schuttgart
  160. _pointsWithKarmas[16][0] = 85184;
  161. _pointsWithKarmas[16][1] = -138560;
  162. _pointsWithKarmas[16][2] = -2256;
  163. // Kamael Village
  164. _pointsWithKarmas[19][0] = -121425;
  165. _pointsWithKarmas[19][1] = 59778;
  166. _pointsWithKarmas[19][2] = -2264;
  167. }
  168. public final int getMapRegion(int posX, int posY)
  169. {
  170. return _regions[getMapRegionX(posX)][getMapRegionY(posY)];
  171. }
  172. public final int getMapRegionX(int posX)
  173. {
  174. return ( posX >> 15 ) + 4;// + centerTileX;
  175. }
  176. public final int getMapRegionY(int posY)
  177. {
  178. return ( posY >> 15 ) + 10;// + centerTileX;
  179. }
  180. public int getAreaCastle(L2Character activeChar)
  181. {
  182. int area = getClosestTownNumber(activeChar);
  183. int castle;
  184. switch (area)
  185. {
  186. case 0: castle = 1; break;//Talking Island Village
  187. case 1: castle = 4; break; //Elven Village
  188. case 2: castle = 4; break; //Dark Elven Village
  189. case 3: castle = 9; break; //Orc Village
  190. case 4: castle = 9; break; //Dwarven Village
  191. case 5: castle = 1; break; //Town of Gludio
  192. case 6: castle = 1; break; //Gludin Village
  193. case 7: castle = 2; break; //Town of Dion
  194. case 8: castle = 3; break; //Town of Giran
  195. case 9: castle = 4; break; //Town of Oren
  196. case 10: castle = 5; break; //Town of Aden
  197. case 11: castle = 5; break; //Hunters Village
  198. case 12: castle = 3; break; //Giran Harbor
  199. case 13: castle = 6; break; //Heine
  200. case 14: castle = 8; break; //Rune Township
  201. case 15: castle = 7; break; //Town of Goddard
  202. case 16: castle = 9; break; //Town of Shuttgart
  203. case 17: castle = 4; break; //Ivory Tower
  204. case 18: castle = 8; break; //Primeval Isle Wharf
  205. case 19: castle = 5; break; //Kamael Village
  206. case 20: castle = 6; break; //South of Wastelands Camp
  207. case 21: castle = 8; break; //Fantasy Island
  208. default: castle = 5; break; //Town of Aden
  209. }
  210. return castle;
  211. }
  212. public int getClosestTownNumber(L2Character activeChar)
  213. {
  214. return getMapRegion(activeChar.getX(), activeChar.getY());
  215. }
  216. public String getClosestTownName(L2Character activeChar)
  217. {
  218. int nearestTownId = getMapRegion(activeChar.getX(), activeChar.getY());
  219. String nearestTown;
  220. switch (nearestTownId)
  221. {
  222. case 0: nearestTown = "Talking Island Village"; break;
  223. case 1: nearestTown = "Elven Village"; break;
  224. case 2: nearestTown = "Dark Elven Village"; break;
  225. case 3: nearestTown = "Orc Village"; break;
  226. case 4: nearestTown = "Dwarven Village"; break;
  227. case 5: nearestTown = "Town of Gludio"; break;
  228. case 6: nearestTown = "Gludin Village"; break;
  229. case 7: nearestTown = "Town of Dion"; break;
  230. case 8: nearestTown = "Town of Giran"; break;
  231. case 9: nearestTown = "Town of Oren"; break;
  232. case 10: nearestTown = "Town of Aden"; break;
  233. case 11: nearestTown = "Hunters Village"; break;
  234. case 12: nearestTown = "Giran Harbor"; break;
  235. case 13: nearestTown = "Heine"; break;
  236. case 14: nearestTown = "Rune Township"; break;
  237. case 15: nearestTown = "Town of Goddard"; break;
  238. case 16: nearestTown = "Town of Shuttgart"; break; ////TODO@ (Check mapregion table)[Luno]
  239. case 18: nearestTown = "Primeval Isle";break;
  240. case 19: nearestTown = "Kamael Village";break;
  241. default: nearestTown = "Town of Aden"; break;
  242. }
  243. return nearestTown;
  244. }
  245. public Location getTeleToLocation(L2Character activeChar, TeleportWhereType teleportWhere)
  246. {
  247. int[] coord;
  248. if (activeChar instanceof L2PcInstance)
  249. {
  250. L2PcInstance player = ((L2PcInstance)activeChar);
  251. // If in Monster Derby Track
  252. if (player.isInsideZone(L2Character.ZONE_MONSTERTRACK))
  253. return new Location(12661, 181687, -3560);
  254. Castle castle = null;
  255. Fort fort = null;
  256. ClanHall clanhall = null;
  257. if (player.getClan() != null)
  258. {
  259. // If teleport to clan hall
  260. if (teleportWhere == TeleportWhereType.ClanHall)
  261. {
  262. clanhall = ClanHallManager.getInstance().getClanHallByOwner(player.getClan());
  263. if (clanhall != null)
  264. {
  265. L2ClanHallZone zone = clanhall.getZone();
  266. if (zone != null)
  267. {
  268. return zone.getSpawn();
  269. }
  270. }
  271. }
  272. // If teleport to castle
  273. if (teleportWhere == TeleportWhereType.Castle)
  274. {
  275. castle = CastleManager.getInstance().getCastleByOwner(player.getClan());
  276. // Check if player is on castle or fortress ground
  277. if (castle == null) castle = CastleManager.getInstance().getCastle(player);
  278. if (castle != null && castle.getCastleId() > 0)
  279. {
  280. // If Teleporting to castle or
  281. // If is on caslte with siege and player's clan is defender
  282. if (teleportWhere == TeleportWhereType.Castle || (teleportWhere == TeleportWhereType.Castle && castle.getSiege().getIsInProgress() && castle.getSiege().getDefenderClan(player.getClan()) != null))
  283. {
  284. coord = castle.getZone().getSpawn();
  285. return new Location(coord[0], coord[1], coord[2]);
  286. }
  287. }
  288. }
  289. // If teleport to fortress
  290. if (teleportWhere == TeleportWhereType.Fortress)
  291. {
  292. fort = FortManager.getInstance().getFortByOwner(player.getClan());
  293. if (fort == null) fort = FortManager.getInstance().getFort(player);
  294. if (fort != null && fort.getFortId() > 0)
  295. {
  296. // If Teleporting to castle or
  297. // If is on caslte with siege and player's clan is defender
  298. if (teleportWhere == TeleportWhereType.Fortress || (teleportWhere == TeleportWhereType.Fortress && fort.getSiege().getIsInProgress() && fort.getSiege().getDefenderClan(player.getClan()) != null))
  299. {
  300. coord = fort.getZone().getSpawn();
  301. return new Location(coord[0], coord[1], coord[2]);
  302. }
  303. }
  304. }
  305. // If teleport to SiegeHQ
  306. if (teleportWhere == TeleportWhereType.SiegeFlag)
  307. {
  308. castle = CastleManager.getInstance().getCastle(player);
  309. fort = FortManager.getInstance().getFort(player);
  310. if ( castle != null)
  311. {
  312. if (castle.getSiege().getIsInProgress())
  313. {
  314. // Check if player's clan is attacker
  315. List<L2NpcInstance> flags = castle.getSiege().getFlag(player.getClan());
  316. if (flags != null && !flags.isEmpty())
  317. {
  318. // Spawn to flag - Need more work to get player to the nearest flag
  319. L2NpcInstance flag = flags.get(0);
  320. return new Location(flag.getX(), flag.getY(), flag.getZ());
  321. }
  322. }
  323. }
  324. else if ( fort != null)
  325. {
  326. if (fort.getSiege().getIsInProgress())
  327. {
  328. // Check if player's clan is attacker
  329. List<L2NpcInstance> flags = fort.getSiege().getFlag(player.getClan());
  330. if (flags != null && !flags.isEmpty())
  331. {
  332. // Spawn to flag - Need more work to get player to the nearest flag
  333. L2NpcInstance flag = flags.get(0);
  334. return new Location(flag.getX(), flag.getY(), flag.getZ());
  335. }
  336. }
  337. }
  338. }
  339. }
  340. // teleport RED PK 5+ to Floran Village
  341. if(player.getPkKills() > 5 && player.getKarma() > 1) return new Location(17817, 170079, -3530);
  342. //Karma player land out of city
  343. if(player.getKarma() > 1)
  344. {
  345. int closest = getMapRegion(activeChar.getX(), activeChar.getY());
  346. if(closest >= 0 && closest < _pointsWithKarmas.length)
  347. return new Location(_pointsWithKarmas[closest][0], _pointsWithKarmas[closest][1], _pointsWithKarmas[closest][2]);
  348. else
  349. return new Location(17817, 170079, -3530);
  350. }
  351. // Checking if in arena
  352. L2ArenaZone arena = ArenaManager.getInstance().getArena(player);
  353. if (arena != null)
  354. {
  355. coord = arena.getSpawnLoc();
  356. return new Location(coord[0], coord[1], coord[2]);
  357. }
  358. }
  359. // Get the nearest town
  360. // TODO: Micht: Maybe we should add some checks to prevent exception here.
  361. coord = TownManager.getInstance().getClosestTown(activeChar).getSpawnLoc();
  362. return new Location(coord[0], coord[1], coord[2]);
  363. }
  364. }