DimensionalRiftManager.java 16 KB

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