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