2
0

DimensionalRiftManager.java 18 KB

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