2
0

DimensionalRiftManager.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Copyright (C) 2004-2013 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.awt.Polygon;
  21. import java.awt.Shape;
  22. import java.io.File;
  23. import java.sql.Connection;
  24. import java.sql.ResultSet;
  25. import java.sql.Statement;
  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.NpcTable;
  36. import com.l2jserver.gameserver.datatables.SpawnTable;
  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.actor.templates.L2NpcTemplate;
  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. import gnu.trove.map.hash.TByteObjectHashMap;
  47. /**
  48. * @author kombat
  49. */
  50. public class DimensionalRiftManager
  51. {
  52. private static Logger _log = Logger.getLogger(DimensionalRiftManager.class.getName());
  53. private final TByteObjectHashMap<TByteObjectHashMap<DimensionalRiftRoom>> _rooms = new TByteObjectHashMap<>(7);
  54. private 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 = L2DatabaseFactory.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 TByteObjectHashMap<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.keys().length;
  102. int roomSize = 0;
  103. for (byte b : _rooms.keys())
  104. {
  105. roomSize += _rooms.get(b).keys().length;
  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. L2Spawn spawnDat;
  128. L2NpcTemplate template;
  129. for (Node rift = doc.getFirstChild(); rift != null; rift = rift.getNextSibling())
  130. {
  131. if ("rift".equalsIgnoreCase(rift.getNodeName()))
  132. {
  133. for (Node area = rift.getFirstChild(); area != null; area = area.getNextSibling())
  134. {
  135. if ("area".equalsIgnoreCase(area.getNodeName()))
  136. {
  137. attrs = area.getAttributes();
  138. type = Byte.parseByte(attrs.getNamedItem("type").getNodeValue());
  139. for (Node room = area.getFirstChild(); room != null; room = room.getNextSibling())
  140. {
  141. if ("room".equalsIgnoreCase(room.getNodeName()))
  142. {
  143. attrs = room.getAttributes();
  144. roomId = Byte.parseByte(attrs.getNamedItem("id").getNodeValue());
  145. for (Node spawn = room.getFirstChild(); spawn != null; spawn = spawn.getNextSibling())
  146. {
  147. if ("spawn".equalsIgnoreCase(spawn.getNodeName()))
  148. {
  149. attrs = spawn.getAttributes();
  150. mobId = Integer.parseInt(attrs.getNamedItem("mobId").getNodeValue());
  151. delay = Integer.parseInt(attrs.getNamedItem("delay").getNodeValue());
  152. count = Integer.parseInt(attrs.getNamedItem("count").getNodeValue());
  153. template = NpcTable.getInstance().getTemplate(mobId);
  154. if (template == null)
  155. {
  156. _log.warning("Template " + mobId + " not found!");
  157. }
  158. if (!_rooms.containsKey(type))
  159. {
  160. _log.warning("Type " + type + " not found!");
  161. }
  162. else if (!_rooms.get(type).containsKey(roomId))
  163. {
  164. _log.warning("Room " + roomId + " in Type " + type + " not found!");
  165. }
  166. for (int i = 0; i < count; i++)
  167. {
  168. DimensionalRiftRoom riftRoom = _rooms.get(type).get(roomId);
  169. x = riftRoom.getRandomX();
  170. y = riftRoom.getRandomY();
  171. z = riftRoom.getTeleportCoords()[2];
  172. if ((template != null) && _rooms.containsKey(type) && _rooms.get(type).containsKey(roomId))
  173. {
  174. spawnDat = new L2Spawn(template);
  175. spawnDat.setAmount(1);
  176. spawnDat.setLocx(x);
  177. spawnDat.setLocy(y);
  178. spawnDat.setLocz(z);
  179. spawnDat.setHeading(-1);
  180. spawnDat.setRespawnDelay(delay);
  181. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  182. _rooms.get(type).get(roomId).getSpawns().add(spawnDat);
  183. countGood++;
  184. }
  185. else
  186. {
  187. countBad++;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. catch (Exception e)
  200. {
  201. _log.log(Level.WARNING, "Error on loading dimensional rift spawns: " + e.getMessage(), e);
  202. }
  203. _log.info(getClass().getSimpleName() + ": Loaded " + countGood + " dimensional rift spawns, " + countBad + " errors.");
  204. }
  205. public void reload()
  206. {
  207. for (byte b : _rooms.keys())
  208. {
  209. for (byte i : _rooms.get(b).keys())
  210. {
  211. _rooms.get(b).get(i).getSpawns().clear();
  212. }
  213. _rooms.get(b).clear();
  214. }
  215. _rooms.clear();
  216. loadRooms();
  217. loadSpawns();
  218. }
  219. public boolean checkIfInRiftZone(int x, int y, int z, boolean ignorePeaceZone)
  220. {
  221. if (ignorePeaceZone)
  222. {
  223. return _rooms.get((byte) 0).get((byte) 1).checkIfInZone(x, y, z);
  224. }
  225. return _rooms.get((byte) 0).get((byte) 1).checkIfInZone(x, y, z) && !_rooms.get((byte) 0).get((byte) 0).checkIfInZone(x, y, z);
  226. }
  227. public boolean checkIfInPeaceZone(int x, int y, int z)
  228. {
  229. return _rooms.get((byte) 0).get((byte) 0).checkIfInZone(x, y, z);
  230. }
  231. public void teleportToWaitingRoom(L2PcInstance player)
  232. {
  233. int[] coords = getRoom((byte) 0, (byte) 0).getTeleportCoords();
  234. player.teleToLocation(coords[0], coords[1], coords[2]);
  235. }
  236. public synchronized void start(L2PcInstance player, byte type, L2Npc npc)
  237. {
  238. boolean canPass = true;
  239. if (!player.isInParty())
  240. {
  241. showHtmlFile(player, "data/html/seven_signs/rift/NoParty.htm", npc);
  242. return;
  243. }
  244. if (player.getParty().getLeaderObjectId() != player.getObjectId())
  245. {
  246. showHtmlFile(player, "data/html/seven_signs/rift/NotPartyLeader.htm", npc);
  247. return;
  248. }
  249. if (player.getParty().isInDimensionalRift())
  250. {
  251. handleCheat(player, npc);
  252. return;
  253. }
  254. if (player.getParty().getMemberCount() < Config.RIFT_MIN_PARTY_SIZE)
  255. {
  256. NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  257. html.setFile(player.getHtmlPrefix(), "data/html/seven_signs/rift/SmallParty.htm");
  258. html.replace("%npc_name%", npc.getName());
  259. html.replace("%count%", Integer.toString(Config.RIFT_MIN_PARTY_SIZE));
  260. player.sendPacket(html);
  261. return;
  262. }
  263. // max parties inside is rooms count - 1
  264. if (!isAllowedEnter(type))
  265. {
  266. player.sendMessage("Rift is full. Try later.");
  267. return;
  268. }
  269. for (L2PcInstance p : player.getParty().getMembers())
  270. {
  271. if (!checkIfInPeaceZone(p.getX(), p.getY(), p.getZ()))
  272. {
  273. canPass = false;
  274. break;
  275. }
  276. }
  277. if (!canPass)
  278. {
  279. showHtmlFile(player, "data/html/seven_signs/rift/NotInWaitingRoom.htm", npc);
  280. return;
  281. }
  282. L2ItemInstance i;
  283. int count = getNeededItems(type);
  284. for (L2PcInstance p : player.getParty().getMembers())
  285. {
  286. i = p.getInventory().getItemByItemId(DIMENSIONAL_FRAGMENT_ITEM_ID);
  287. if (i == null)
  288. {
  289. canPass = false;
  290. break;
  291. }
  292. if (i.getCount() > 0)
  293. {
  294. if (i.getCount() < getNeededItems(type))
  295. {
  296. canPass = false;
  297. break;
  298. }
  299. }
  300. else
  301. {
  302. canPass = false;
  303. break;
  304. }
  305. }
  306. if (!canPass)
  307. {
  308. NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  309. html.setFile(player.getHtmlPrefix(), "data/html/seven_signs/rift/NoFragments.htm");
  310. html.replace("%npc_name%", npc.getName());
  311. html.replace("%count%", Integer.toString(count));
  312. player.sendPacket(html);
  313. return;
  314. }
  315. for (L2PcInstance p : player.getParty().getMembers())
  316. {
  317. i = p.getInventory().getItemByItemId(DIMENSIONAL_FRAGMENT_ITEM_ID);
  318. if (!p.destroyItem("RiftEntrance", i, count, null, false))
  319. {
  320. NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  321. html.setFile(player.getHtmlPrefix(), "data/html/seven_signs/rift/NoFragments.htm");
  322. html.replace("%npc_name%", npc.getName());
  323. html.replace("%count%", Integer.toString(count));
  324. player.sendPacket(html);
  325. return;
  326. }
  327. }
  328. byte room;
  329. FastList<Byte> emptyRooms;
  330. do
  331. {
  332. emptyRooms = getFreeRooms(type);
  333. room = emptyRooms.get(Rnd.get(1, emptyRooms.size()) - 1);
  334. }
  335. // find empty room
  336. while (_rooms.get(type).get(room).ispartyInside());
  337. new DimensionalRift(player.getParty(), type, room);
  338. }
  339. public void killRift(DimensionalRift d)
  340. {
  341. if (d.getTeleportTimerTask() != null)
  342. {
  343. d.getTeleportTimerTask().cancel();
  344. }
  345. d.setTeleportTimerTask(null);
  346. if (d.getTeleportTimer() != null)
  347. {
  348. d.getTeleportTimer().cancel();
  349. }
  350. d.setTeleportTimer(null);
  351. if (d.getSpawnTimerTask() != null)
  352. {
  353. d.getSpawnTimerTask().cancel();
  354. }
  355. d.setSpawnTimerTask(null);
  356. if (d.getSpawnTimer() != null)
  357. {
  358. d.getSpawnTimer().cancel();
  359. }
  360. d.setSpawnTimer(null);
  361. }
  362. public static class DimensionalRiftRoom
  363. {
  364. protected final byte _type;
  365. protected final byte _room;
  366. private final int _xMin;
  367. private final int _xMax;
  368. private final int _yMin;
  369. private final int _yMax;
  370. private final int _zMin;
  371. private final int _zMax;
  372. private final int[] _teleportCoords;
  373. private final Shape _s;
  374. private final boolean _isBossRoom;
  375. private final FastList<L2Spawn> _roomSpawns;
  376. protected final FastList<L2Npc> _roomMobs;
  377. private boolean _partyInside = false;
  378. public DimensionalRiftRoom(byte type, byte room, int xMin, int xMax, int yMin, int yMax, int zMin, int zMax, int xT, int yT, int zT, boolean isBossRoom)
  379. {
  380. _type = type;
  381. _room = room;
  382. _xMin = (xMin + 128);
  383. _xMax = (xMax - 128);
  384. _yMin = (yMin + 128);
  385. _yMax = (yMax - 128);
  386. _zMin = zMin;
  387. _zMax = zMax;
  388. _teleportCoords = new int[]
  389. {
  390. xT,
  391. yT,
  392. zT
  393. };
  394. _isBossRoom = isBossRoom;
  395. _roomSpawns = new FastList<>();
  396. _roomMobs = new FastList<>();
  397. _s = new Polygon(new int[]
  398. {
  399. xMin,
  400. xMax,
  401. xMax,
  402. xMin
  403. }, new int[]
  404. {
  405. yMin,
  406. yMin,
  407. yMax,
  408. yMax
  409. }, 4);
  410. }
  411. public int getRandomX()
  412. {
  413. return Rnd.get(_xMin, _xMax);
  414. }
  415. public int getRandomY()
  416. {
  417. return Rnd.get(_yMin, _yMax);
  418. }
  419. public int[] getTeleportCoords()
  420. {
  421. return _teleportCoords;
  422. }
  423. public boolean checkIfInZone(int x, int y, int z)
  424. {
  425. return _s.contains(x, y) && (z >= _zMin) && (z <= _zMax);
  426. }
  427. public boolean isBossRoom()
  428. {
  429. return _isBossRoom;
  430. }
  431. public FastList<L2Spawn> getSpawns()
  432. {
  433. return _roomSpawns;
  434. }
  435. public void spawn()
  436. {
  437. for (L2Spawn spawn : _roomSpawns)
  438. {
  439. spawn.doSpawn();
  440. spawn.startRespawn();
  441. }
  442. }
  443. public DimensionalRiftRoom unspawn()
  444. {
  445. for (L2Spawn spawn : _roomSpawns)
  446. {
  447. spawn.stopRespawn();
  448. if (spawn.getLastSpawn() != null)
  449. {
  450. spawn.getLastSpawn().deleteMe();
  451. }
  452. }
  453. return this;
  454. }
  455. /**
  456. * @return the _partyInside
  457. */
  458. public boolean ispartyInside()
  459. {
  460. return _partyInside;
  461. }
  462. public void setPartyInside(boolean partyInside)
  463. {
  464. _partyInside = partyInside;
  465. }
  466. }
  467. private int getNeededItems(byte type)
  468. {
  469. switch (type)
  470. {
  471. case 1:
  472. return Config.RIFT_ENTER_COST_RECRUIT;
  473. case 2:
  474. return Config.RIFT_ENTER_COST_SOLDIER;
  475. case 3:
  476. return Config.RIFT_ENTER_COST_OFFICER;
  477. case 4:
  478. return Config.RIFT_ENTER_COST_CAPTAIN;
  479. case 5:
  480. return Config.RIFT_ENTER_COST_COMMANDER;
  481. case 6:
  482. return Config.RIFT_ENTER_COST_HERO;
  483. default:
  484. throw new IndexOutOfBoundsException();
  485. }
  486. }
  487. public void showHtmlFile(L2PcInstance player, String file, L2Npc npc)
  488. {
  489. NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  490. html.setFile(player.getHtmlPrefix(), file);
  491. html.replace("%npc_name%", npc.getName());
  492. player.sendPacket(html);
  493. }
  494. public void handleCheat(L2PcInstance player, L2Npc npc)
  495. {
  496. showHtmlFile(player, "data/html/seven_signs/rift/Cheater.htm", npc);
  497. if (!player.isGM())
  498. {
  499. _log.warning("Player " + player.getName() + "(" + player.getObjectId() + ") was cheating in dimension rift area!");
  500. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " tried to cheat in dimensional rift.", Config.DEFAULT_PUNISH);
  501. }
  502. }
  503. public boolean isAllowedEnter(byte type)
  504. {
  505. int count = 0;
  506. for (DimensionalRiftRoom room : _rooms.get(type).valueCollection())
  507. {
  508. if (room.ispartyInside())
  509. {
  510. count++;
  511. }
  512. }
  513. return (count < (_rooms.get(type).size() - 1));
  514. }
  515. public FastList<Byte> getFreeRooms(byte type)
  516. {
  517. FastList<Byte> list = new FastList<>();
  518. for (DimensionalRiftRoom room : _rooms.get(type).valueCollection())
  519. {
  520. if (!room.ispartyInside())
  521. {
  522. list.add(room._room);
  523. }
  524. }
  525. return list;
  526. }
  527. private static class SingletonHolder
  528. {
  529. protected static final DimensionalRiftManager _instance = new DimensionalRiftManager();
  530. }
  531. }