DimensionalRiftManager.java 16 KB

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