DimensionalRiftManager.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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.Level;
  25. import java.util.logging.Logger;
  26. import javax.xml.parsers.DocumentBuilderFactory;
  27. import javolution.util.FastList;
  28. import org.w3c.dom.Document;
  29. import org.w3c.dom.NamedNodeMap;
  30. import org.w3c.dom.Node;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.L2DatabaseFactory;
  33. import com.l2jserver.gameserver.datatables.NpcTable;
  34. import com.l2jserver.gameserver.datatables.SpawnTable;
  35. import com.l2jserver.gameserver.model.L2ItemInstance;
  36. import com.l2jserver.gameserver.model.L2Spawn;
  37. import com.l2jserver.gameserver.model.actor.L2Npc;
  38. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  39. import com.l2jserver.gameserver.model.entity.DimensionalRift;
  40. import com.l2jserver.gameserver.model.quest.Quest;
  41. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  42. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  43. import com.l2jserver.gameserver.util.Util;
  44. import com.l2jserver.util.Rnd;
  45. /**
  46. * Thanks to L2Fortress and balancer.ru - kombat
  47. */
  48. public class DimensionalRiftManager
  49. {
  50. private static Logger _log = Logger.getLogger(DimensionalRiftManager.class.getName());
  51. private TByteObjectHashMap<TByteObjectHashMap<DimensionalRiftRoom>> _rooms = new TByteObjectHashMap<TByteObjectHashMap<DimensionalRiftRoom>>(7);
  52. private final int DIMENSIONAL_FRAGMENT_ITEM_ID = 7079;
  53. public static DimensionalRiftManager getInstance()
  54. {
  55. return SingletonHolder._instance;
  56. }
  57. private DimensionalRiftManager()
  58. {
  59. loadRooms();
  60. loadSpawns();
  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. Connection con = null;
  70. try
  71. {
  72. con = L2DatabaseFactory.getInstance().getConnection();
  73. PreparedStatement statement = con.prepareStatement("SELECT * FROM dimensional_rift");
  74. ResultSet rs = statement.executeQuery();
  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. _rooms.put(type, new TByteObjectHashMap<DimensionalRiftRoom>(9));
  93. _rooms.get(type).put(room_id, new DimensionalRiftRoom(type, room_id, xMin, xMax, yMin, yMax, z1, z2, xT, yT, zT, isBossRoom));
  94. }
  95. rs.close();
  96. statement.close();
  97. }
  98. catch (Exception e)
  99. {
  100. _log.log(Level.WARNING, "Can't load Dimension Rift zones. " + e.getMessage(), e);
  101. }
  102. finally
  103. {
  104. L2DatabaseFactory.close(con);
  105. }
  106. int typeSize = _rooms.keys().length;
  107. int roomSize = 0;
  108. for (byte b : _rooms.keys())
  109. roomSize += _rooms.get(b).keys().length;
  110. _log.info("DimensionalRiftManager: Loaded " + typeSize + " room types with " + roomSize + " rooms.");
  111. }
  112. public void loadSpawns()
  113. {
  114. int countGood = 0, countBad = 0;
  115. try
  116. {
  117. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  118. factory.setValidating(false);
  119. factory.setIgnoringComments(true);
  120. File file = new File(Config.DATAPACK_ROOT + "/data/dimensionalRift.xml");
  121. if (!file.exists())
  122. throw new IOException();
  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("DimensionalRiftManager: 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. return _rooms.get((byte) 0).get((byte) 1).checkIfInZone(x, y, z);
  223. else
  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().getPartyLeaderOID() != 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().getPartyMembers())
  269. if (!checkIfInPeaceZone(p.getX(), p.getY(), p.getZ()))
  270. {
  271. canPass = false;
  272. break;
  273. }
  274. if (!canPass)
  275. {
  276. showHtmlFile(player, "data/html/seven_signs/rift/NotInWaitingRoom.htm", npc);
  277. return;
  278. }
  279. L2ItemInstance i;
  280. int count = getNeededItems(type);
  281. for (L2PcInstance p : player.getParty().getPartyMembers())
  282. {
  283. i = p.getInventory().getItemByItemId(DIMENSIONAL_FRAGMENT_ITEM_ID);
  284. if (i == null)
  285. {
  286. canPass = false;
  287. break;
  288. }
  289. if (i.getCount() > 0)
  290. {
  291. if (i.getCount() < getNeededItems(type))
  292. {
  293. canPass = false;
  294. break;
  295. }
  296. }
  297. else
  298. {
  299. canPass = false;
  300. break;
  301. }
  302. }
  303. if (!canPass)
  304. {
  305. NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  306. html.setFile(player.getHtmlPrefix(), "data/html/seven_signs/rift/NoFragments.htm");
  307. html.replace("%npc_name%", npc.getName());
  308. html.replace("%count%", Integer.toString(count));
  309. player.sendPacket(html);
  310. return;
  311. }
  312. for (L2PcInstance p : player.getParty().getPartyMembers())
  313. {
  314. i = p.getInventory().getItemByItemId(DIMENSIONAL_FRAGMENT_ITEM_ID);
  315. if (!p.destroyItem("RiftEntrance", i, count, null, false))
  316. {
  317. NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  318. html.setFile(player.getHtmlPrefix(), "data/html/seven_signs/rift/NoFragments.htm");
  319. html.replace("%npc_name%", npc.getName());
  320. html.replace("%count%", Integer.toString(count));
  321. player.sendPacket(html);
  322. return;
  323. }
  324. }
  325. byte room;
  326. FastList<Byte> emptyRooms;
  327. do
  328. {
  329. emptyRooms = getFreeRooms(type);
  330. room = emptyRooms.get(Rnd.get(1, emptyRooms.size())-1);
  331. }
  332. // find empty room
  333. while (_rooms.get(type).get(room).ispartyInside());
  334. new DimensionalRift(player.getParty(), type, room);
  335. }
  336. public void killRift(DimensionalRift d)
  337. {
  338. if (d.getTeleportTimerTask() != null)
  339. d.getTeleportTimerTask().cancel();
  340. d.setTeleportTimerTask(null);
  341. if (d.getTeleportTimer() != null)
  342. d.getTeleportTimer().cancel();
  343. d.setTeleportTimer(null);
  344. if (d.getSpawnTimerTask() != null)
  345. d.getSpawnTimerTask().cancel();
  346. d.setSpawnTimerTask(null);
  347. if (d.getSpawnTimer() != null)
  348. d.getSpawnTimer().cancel();
  349. d.setSpawnTimer(null);
  350. }
  351. public static class DimensionalRiftRoom
  352. {
  353. protected final byte _type;
  354. protected final byte _room;
  355. private final int _xMin;
  356. private final int _xMax;
  357. private final int _yMin;
  358. private final int _yMax;
  359. private final int _zMin;
  360. private final int _zMax;
  361. private final int[] _teleportCoords;
  362. private final Shape _s;
  363. private final boolean _isBossRoom;
  364. private final FastList<L2Spawn> _roomSpawns;
  365. protected final FastList<L2Npc> _roomMobs;
  366. private boolean _partyInside = false;
  367. public DimensionalRiftRoom(byte type, byte room, int xMin, int xMax, int yMin, int yMax, int zMin, int zMax, int xT, int yT,
  368. int zT, boolean isBossRoom)
  369. {
  370. _type = type;
  371. _room = room;
  372. _xMin = (xMin + 128);
  373. _xMax = (xMax - 128);
  374. _yMin = (yMin + 128);
  375. _yMax = (yMax - 128);
  376. _zMin = zMin;
  377. _zMax = zMax;
  378. _teleportCoords = new int[] { xT, yT, zT };
  379. _isBossRoom = isBossRoom;
  380. _roomSpawns = new FastList<L2Spawn>();
  381. _roomMobs = new FastList<L2Npc>();
  382. _s = new Polygon(new int[] { xMin, xMax, xMax, xMin }, new int[] { yMin, yMin, yMax, yMax }, 4);
  383. }
  384. public int getRandomX()
  385. {
  386. return Rnd.get(_xMin, _xMax);
  387. }
  388. public int getRandomY()
  389. {
  390. return Rnd.get(_yMin, _yMax);
  391. }
  392. public int[] getTeleportCoords()
  393. {
  394. return _teleportCoords;
  395. }
  396. public boolean checkIfInZone(int x, int y, int z)
  397. {
  398. return _s.contains(x, y) && z >= _zMin && z <= _zMax;
  399. }
  400. public boolean isBossRoom()
  401. {
  402. return _isBossRoom;
  403. }
  404. public FastList<L2Spawn> getSpawns()
  405. {
  406. return _roomSpawns;
  407. }
  408. public void spawn()
  409. {
  410. for (L2Spawn spawn : _roomSpawns)
  411. {
  412. spawn.doSpawn();
  413. spawn.startRespawn();
  414. }
  415. }
  416. public DimensionalRiftRoom unspawn()
  417. {
  418. for (L2Spawn spawn : _roomSpawns)
  419. {
  420. spawn.stopRespawn();
  421. if (spawn.getLastSpawn() != null)
  422. spawn.getLastSpawn().deleteMe();
  423. }
  424. return this;
  425. }
  426. /**
  427. * @return the _partyInside
  428. */
  429. public boolean ispartyInside()
  430. {
  431. return _partyInside;
  432. }
  433. public void setPartyInside(boolean partyInside)
  434. {
  435. _partyInside = partyInside;
  436. }
  437. }
  438. private int getNeededItems(byte type)
  439. {
  440. switch (type)
  441. {
  442. case 1:
  443. return Config.RIFT_ENTER_COST_RECRUIT;
  444. case 2:
  445. return Config.RIFT_ENTER_COST_SOLDIER;
  446. case 3:
  447. return Config.RIFT_ENTER_COST_OFFICER;
  448. case 4:
  449. return Config.RIFT_ENTER_COST_CAPTAIN;
  450. case 5:
  451. return Config.RIFT_ENTER_COST_COMMANDER;
  452. case 6:
  453. return Config.RIFT_ENTER_COST_HERO;
  454. default:
  455. throw new IndexOutOfBoundsException();
  456. }
  457. }
  458. public void showHtmlFile(L2PcInstance player, String file, L2Npc npc)
  459. {
  460. NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  461. html.setFile(player.getHtmlPrefix(), file);
  462. html.replace("%npc_name%", npc.getName());
  463. player.sendPacket(html);
  464. }
  465. public void handleCheat(L2PcInstance player, L2Npc npc)
  466. {
  467. showHtmlFile(player, "data/html/seven_signs/rift/Cheater.htm", npc);
  468. if (!player.isGM())
  469. {
  470. _log.warning("Player " + player.getName() + "(" + player.getObjectId() + ") was cheating in dimension rift area!");
  471. Util.handleIllegalPlayerAction(player, "Warning!! Character " + player.getName() + " tried to cheat in dimensional rift.", Config.DEFAULT_PUNISH);
  472. }
  473. }
  474. public boolean isAllowedEnter(byte type)
  475. {
  476. int count = 0;
  477. for (Object room : _rooms.get(type).getValues())
  478. {
  479. if (((DimensionalRiftRoom) room).ispartyInside())
  480. count++;
  481. }
  482. return (count < (_rooms.get(type).size() - 1));
  483. }
  484. public FastList<Byte> getFreeRooms(byte type)
  485. {
  486. FastList<Byte> list = new FastList<Byte>();
  487. for (Object room : _rooms.get(type).getValues())
  488. {
  489. if (!((DimensionalRiftRoom) room).ispartyInside())
  490. list.add(((DimensionalRiftRoom) room)._room);
  491. }
  492. return list;
  493. }
  494. @SuppressWarnings("synthetic-access")
  495. private static class SingletonHolder
  496. {
  497. protected static final DimensionalRiftManager _instance = new DimensionalRiftManager();
  498. }
  499. }