DimensionalRiftManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.io.File;
  21. import java.sql.Connection;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import javax.xml.parsers.DocumentBuilderFactory;
  29. import javolution.util.FastList;
  30. import org.w3c.dom.Document;
  31. import org.w3c.dom.NamedNodeMap;
  32. import org.w3c.dom.Node;
  33. import com.l2jserver.Config;
  34. import com.l2jserver.L2DatabaseFactory;
  35. import com.l2jserver.gameserver.datatables.SpawnTable;
  36. import com.l2jserver.gameserver.model.DimensionalRiftRoom;
  37. import com.l2jserver.gameserver.model.L2Spawn;
  38. import com.l2jserver.gameserver.model.actor.L2Npc;
  39. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  40. import com.l2jserver.gameserver.model.entity.DimensionalRift;
  41. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  42. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  43. import com.l2jserver.gameserver.util.Util;
  44. import com.l2jserver.util.Rnd;
  45. /**
  46. * Dimensional Rift manager.
  47. * @author kombat
  48. */
  49. public final class DimensionalRiftManager
  50. {
  51. private static Logger _log = Logger.getLogger(DimensionalRiftManager.class.getName());
  52. private final Map<Byte, Map<Byte, DimensionalRiftRoom>> _rooms = new HashMap<>(7);
  53. private static final int DIMENSIONAL_FRAGMENT_ITEM_ID = 7079;
  54. public static DimensionalRiftManager getInstance()
  55. {
  56. return SingletonHolder._instance;
  57. }
  58. protected DimensionalRiftManager()
  59. {
  60. loadRooms();
  61. loadSpawns();
  62. }
  63. public DimensionalRiftRoom getRoom(byte type, byte room)
  64. {
  65. return _rooms.get(type) == null ? null : _rooms.get(type).get(room);
  66. }
  67. private void loadRooms()
  68. {
  69. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  70. Statement s = con.createStatement();
  71. ResultSet rs = s.executeQuery("SELECT * FROM dimensional_rift"))
  72. {
  73. while (rs.next())
  74. {
  75. // 0 waiting room, 1 recruit, 2 soldier, 3 officer, 4 captain , 5 commander, 6 hero
  76. byte type = rs.getByte("type");
  77. byte room_id = rs.getByte("room_id");
  78. // coords related
  79. int xMin = rs.getInt("xMin");
  80. int xMax = rs.getInt("xMax");
  81. int yMin = rs.getInt("yMin");
  82. int yMax = rs.getInt("yMax");
  83. int z1 = rs.getInt("zMin");
  84. int z2 = rs.getInt("zMax");
  85. int xT = rs.getInt("xT");
  86. int yT = rs.getInt("yT");
  87. int zT = rs.getInt("zT");
  88. boolean isBossRoom = rs.getByte("boss") > 0;
  89. if (!_rooms.containsKey(type))
  90. {
  91. _rooms.put(type, new HashMap<Byte, DimensionalRiftRoom>(9));
  92. }
  93. _rooms.get(type).put(room_id, new DimensionalRiftRoom(type, room_id, xMin, xMax, yMin, yMax, z1, z2, xT, yT, zT, isBossRoom));
  94. }
  95. }
  96. catch (Exception e)
  97. {
  98. _log.log(Level.WARNING, "Can't load Dimension Rift zones. " + e.getMessage(), e);
  99. }
  100. int typeSize = _rooms.keySet().size();
  101. int roomSize = 0;
  102. for (byte b : _rooms.keySet())
  103. {
  104. roomSize += _rooms.get(b).keySet().size();
  105. }
  106. _log.info(getClass().getSimpleName() + ": Loaded " + typeSize + " room types with " + roomSize + " rooms.");
  107. }
  108. public void loadSpawns()
  109. {
  110. int countGood = 0, countBad = 0;
  111. try
  112. {
  113. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  114. factory.setValidating(false);
  115. factory.setIgnoringComments(true);
  116. File file = new File(Config.DATAPACK_ROOT, "data/dimensionalRift.xml");
  117. if (!file.exists())
  118. {
  119. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't find data/" + file.getName());
  120. return;
  121. }
  122. Document doc = factory.newDocumentBuilder().parse(file);
  123. NamedNodeMap attrs;
  124. byte type, roomId;
  125. int mobId, x, y, z, delay, count;
  126. for (Node rift = doc.getFirstChild(); rift != null; rift = rift.getNextSibling())
  127. {
  128. if ("rift".equalsIgnoreCase(rift.getNodeName()))
  129. {
  130. for (Node area = rift.getFirstChild(); area != null; area = area.getNextSibling())
  131. {
  132. if ("area".equalsIgnoreCase(area.getNodeName()))
  133. {
  134. attrs = area.getAttributes();
  135. type = Byte.parseByte(attrs.getNamedItem("type").getNodeValue());
  136. for (Node room = area.getFirstChild(); room != null; room = room.getNextSibling())
  137. {
  138. if ("room".equalsIgnoreCase(room.getNodeName()))
  139. {
  140. attrs = room.getAttributes();
  141. roomId = Byte.parseByte(attrs.getNamedItem("id").getNodeValue());
  142. for (Node spawn = room.getFirstChild(); spawn != null; spawn = spawn.getNextSibling())
  143. {
  144. if ("spawn".equalsIgnoreCase(spawn.getNodeName()))
  145. {
  146. attrs = spawn.getAttributes();
  147. mobId = Integer.parseInt(attrs.getNamedItem("mobId").getNodeValue());
  148. delay = Integer.parseInt(attrs.getNamedItem("delay").getNodeValue());
  149. count = Integer.parseInt(attrs.getNamedItem("count").getNodeValue());
  150. if (!_rooms.containsKey(type))
  151. {
  152. _log.warning("Type " + type + " not found!");
  153. }
  154. else if (!_rooms.get(type).containsKey(roomId))
  155. {
  156. _log.warning("Room " + roomId + " in Type " + type + " not found!");
  157. }
  158. for (int i = 0; i < count; i++)
  159. {
  160. final DimensionalRiftRoom riftRoom = _rooms.get(type).get(roomId);
  161. x = riftRoom.getRandomX();
  162. y = riftRoom.getRandomY();
  163. z = riftRoom.getTeleportCoorinates().getZ();
  164. if (_rooms.containsKey(type) && _rooms.get(type).containsKey(roomId))
  165. {
  166. final L2Spawn spawnDat = new L2Spawn(mobId);
  167. spawnDat.setAmount(1);
  168. spawnDat.setX(x);
  169. spawnDat.setY(y);
  170. spawnDat.setZ(z);
  171. spawnDat.setHeading(-1);
  172. spawnDat.setRespawnDelay(delay);
  173. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  174. _rooms.get(type).get(roomId).getSpawns().add(spawnDat);
  175. countGood++;
  176. }
  177. else
  178. {
  179. countBad++;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. catch (Exception e)
  192. {
  193. _log.log(Level.WARNING, "Error on loading dimensional rift spawns: " + e.getMessage(), e);
  194. }
  195. _log.info(getClass().getSimpleName() + ": Loaded " + countGood + " dimensional rift spawns, " + countBad + " errors.");
  196. }
  197. public void reload()
  198. {
  199. for (byte b : _rooms.keySet())
  200. {
  201. for (byte i : _rooms.get(b).keySet())
  202. {
  203. _rooms.get(b).get(i).getSpawns().clear();
  204. }
  205. _rooms.get(b).clear();
  206. }
  207. _rooms.clear();
  208. loadRooms();
  209. loadSpawns();
  210. }
  211. public boolean checkIfInRiftZone(int x, int y, int z, boolean ignorePeaceZone)
  212. {
  213. if (ignorePeaceZone)
  214. {
  215. return _rooms.get((byte) 0).get((byte) 1).checkIfInZone(x, y, z);
  216. }
  217. return _rooms.get((byte) 0).get((byte) 1).checkIfInZone(x, y, z) && !_rooms.get((byte) 0).get((byte) 0).checkIfInZone(x, y, z);
  218. }
  219. public boolean checkIfInPeaceZone(int x, int y, int z)
  220. {
  221. return _rooms.get((byte) 0).get((byte) 0).checkIfInZone(x, y, z);
  222. }
  223. public void teleportToWaitingRoom(L2PcInstance player)
  224. {
  225. player.teleToLocation(getRoom((byte) 0, (byte) 0).getTeleportCoorinates());
  226. }
  227. public synchronized void start(L2PcInstance player, byte type, L2Npc npc)
  228. {
  229. boolean canPass = true;
  230. if (!player.isInParty())
  231. {
  232. showHtmlFile(player, "data/html/seven_signs/rift/NoParty.htm", npc);
  233. return;
  234. }
  235. if (player.getParty().getLeaderObjectId() != player.getObjectId())
  236. {
  237. showHtmlFile(player, "data/html/seven_signs/rift/NotPartyLeader.htm", npc);
  238. return;
  239. }
  240. if (player.getParty().isInDimensionalRift())
  241. {
  242. handleCheat(player, npc);
  243. return;
  244. }
  245. if (player.getParty().getMemberCount() < Config.RIFT_MIN_PARTY_SIZE)
  246. {
  247. final NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  248. html.setFile(player.getHtmlPrefix(), "data/html/seven_signs/rift/SmallParty.htm");
  249. html.replace("%npc_name%", npc.getName());
  250. html.replace("%count%", Integer.toString(Config.RIFT_MIN_PARTY_SIZE));
  251. player.sendPacket(html);
  252. return;
  253. }
  254. // max parties inside is rooms count - 1
  255. if (!isAllowedEnter(type))
  256. {
  257. player.sendMessage("Rift is full. Try later.");
  258. return;
  259. }
  260. for (L2PcInstance p : player.getParty().getMembers())
  261. {
  262. if (!checkIfInPeaceZone(p.getX(), p.getY(), p.getZ()))
  263. {
  264. canPass = false;
  265. break;
  266. }
  267. }
  268. if (!canPass)
  269. {
  270. showHtmlFile(player, "data/html/seven_signs/rift/NotInWaitingRoom.htm", npc);
  271. return;
  272. }
  273. L2ItemInstance i;
  274. int count = getNeededItems(type);
  275. for (L2PcInstance p : player.getParty().getMembers())
  276. {
  277. i = p.getInventory().getItemByItemId(DIMENSIONAL_FRAGMENT_ITEM_ID);
  278. if (i == null)
  279. {
  280. canPass = false;
  281. break;
  282. }
  283. if (i.getCount() > 0)
  284. {
  285. if (i.getCount() < getNeededItems(type))
  286. {
  287. canPass = false;
  288. break;
  289. }
  290. }
  291. else
  292. {
  293. canPass = false;
  294. break;
  295. }
  296. }
  297. if (!canPass)
  298. {
  299. final NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  300. html.setFile(player.getHtmlPrefix(), "data/html/seven_signs/rift/NoFragments.htm");
  301. html.replace("%npc_name%", npc.getName());
  302. html.replace("%count%", Integer.toString(count));
  303. player.sendPacket(html);
  304. return;
  305. }
  306. for (L2PcInstance p : player.getParty().getMembers())
  307. {
  308. i = p.getInventory().getItemByItemId(DIMENSIONAL_FRAGMENT_ITEM_ID);
  309. if (!p.destroyItem("RiftEntrance", i, count, null, false))
  310. {
  311. final NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  312. html.setFile(player.getHtmlPrefix(), "data/html/seven_signs/rift/NoFragments.htm");
  313. html.replace("%npc_name%", npc.getName());
  314. html.replace("%count%", Integer.toString(count));
  315. player.sendPacket(html);
  316. return;
  317. }
  318. }
  319. byte room;
  320. FastList<Byte> emptyRooms;
  321. do
  322. {
  323. emptyRooms = getFreeRooms(type);
  324. room = emptyRooms.get(Rnd.get(1, emptyRooms.size()) - 1);
  325. }
  326. // find empty room
  327. while (_rooms.get(type).get(room).isPartyInside());
  328. new DimensionalRift(player.getParty(), type, room);
  329. }
  330. public void killRift(DimensionalRift d)
  331. {
  332. if (d.getTeleportTimerTask() != null)
  333. {
  334. d.getTeleportTimerTask().cancel();
  335. }
  336. d.setTeleportTimerTask(null);
  337. if (d.getTeleportTimer() != null)
  338. {
  339. d.getTeleportTimer().cancel();
  340. }
  341. d.setTeleportTimer(null);
  342. if (d.getSpawnTimerTask() != null)
  343. {
  344. d.getSpawnTimerTask().cancel();
  345. }
  346. d.setSpawnTimerTask(null);
  347. if (d.getSpawnTimer() != null)
  348. {
  349. d.getSpawnTimer().cancel();
  350. }
  351. d.setSpawnTimer(null);
  352. }
  353. private int getNeededItems(byte type)
  354. {
  355. switch (type)
  356. {
  357. case 1:
  358. return Config.RIFT_ENTER_COST_RECRUIT;
  359. case 2:
  360. return Config.RIFT_ENTER_COST_SOLDIER;
  361. case 3:
  362. return Config.RIFT_ENTER_COST_OFFICER;
  363. case 4:
  364. return Config.RIFT_ENTER_COST_CAPTAIN;
  365. case 5:
  366. return Config.RIFT_ENTER_COST_COMMANDER;
  367. case 6:
  368. return Config.RIFT_ENTER_COST_HERO;
  369. default:
  370. throw new IndexOutOfBoundsException();
  371. }
  372. }
  373. public void showHtmlFile(L2PcInstance player, String file, L2Npc npc)
  374. {
  375. final NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  376. html.setFile(player.getHtmlPrefix(), file);
  377. html.replace("%npc_name%", npc.getName());
  378. player.sendPacket(html);
  379. }
  380. public void handleCheat(L2PcInstance player, L2Npc npc)
  381. {
  382. showHtmlFile(player, "data/html/seven_signs/rift/Cheater.htm", npc);
  383. if (!player.isGM())
  384. {
  385. _log.warning("Player " + player.getName() + "(" + player.getObjectId() + ") was cheating in dimension rift area!");
  386. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " tried to cheat in dimensional rift.", Config.DEFAULT_PUNISH);
  387. }
  388. }
  389. public boolean isAllowedEnter(byte type)
  390. {
  391. int count = 0;
  392. for (DimensionalRiftRoom room : _rooms.get(type).values())
  393. {
  394. if (room.isPartyInside())
  395. {
  396. count++;
  397. }
  398. }
  399. return (count < (_rooms.get(type).size() - 1));
  400. }
  401. public FastList<Byte> getFreeRooms(byte type)
  402. {
  403. FastList<Byte> list = new FastList<>();
  404. for (DimensionalRiftRoom room : _rooms.get(type).values())
  405. {
  406. if (!room.isPartyInside())
  407. {
  408. list.add(room.getRoom());
  409. }
  410. }
  411. return list;
  412. }
  413. private static class SingletonHolder
  414. {
  415. protected static final DimensionalRiftManager _instance = new DimensionalRiftManager();
  416. }
  417. }