DimensionalRiftManager.java 18 KB

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