DimensionalRiftManager.java 14 KB

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