Instance.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. package net.sf.l2j.gameserver.model.entity;
  2. import gnu.trove.TIntHashSet;
  3. import gnu.trove.TIntProcedure;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.util.concurrent.ScheduledFuture;
  8. import java.util.logging.Logger;
  9. import javax.xml.parsers.DocumentBuilderFactory;
  10. import javolution.util.FastList;
  11. import org.w3c.dom.Document;
  12. import org.w3c.dom.Node;
  13. import net.sf.l2j.Config;
  14. import net.sf.l2j.gameserver.Announcements;
  15. import net.sf.l2j.gameserver.ThreadPoolManager;
  16. import net.sf.l2j.gameserver.datatables.DoorTable;
  17. import net.sf.l2j.gameserver.datatables.MapRegionTable;
  18. import net.sf.l2j.gameserver.datatables.NpcTable;
  19. import net.sf.l2j.gameserver.idfactory.IdFactory;
  20. import net.sf.l2j.gameserver.instancemanager.InstanceManager;
  21. import net.sf.l2j.gameserver.model.L2Object;
  22. import net.sf.l2j.gameserver.model.L2Spawn;
  23. import net.sf.l2j.gameserver.model.L2World;
  24. import net.sf.l2j.gameserver.model.L2WorldRegion;
  25. import net.sf.l2j.gameserver.model.actor.L2Npc;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  27. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  28. import net.sf.l2j.gameserver.network.SystemMessageId;
  29. import net.sf.l2j.gameserver.network.clientpackets.Say2;
  30. import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
  31. import net.sf.l2j.gameserver.network.serverpackets.L2GameServerPacket;
  32. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  33. import net.sf.l2j.gameserver.templates.chars.L2NpcTemplate;
  34. /**
  35. * @author evill33t, GodKratos
  36. *
  37. */
  38. public class Instance
  39. {
  40. private final static Logger _log = Logger.getLogger(Instance.class.getName());
  41. private int _id;
  42. private String _name;
  43. private TIntHashSet _players = new TIntHashSet();
  44. private final EjectPlayerProcedure _ejectProc;
  45. private FastList<L2Npc> _npcs = new FastList<L2Npc>();
  46. private FastList<L2DoorInstance> _doors = new FastList<L2DoorInstance>();
  47. private int[] _spawnLoc = new int[3];
  48. private boolean _allowSummon = true;
  49. private long _emptyDestroyTime = -1;
  50. private long _lastLeft = -1;
  51. private long _instanceEndTime = -1;
  52. private boolean _isPvPInstance = false;
  53. protected ScheduledFuture<?> _CheckTimeUpTask = null;
  54. public Instance(int id)
  55. {
  56. _id = id;
  57. _ejectProc = new EjectPlayerProcedure();
  58. }
  59. /**
  60. * Returns the ID of this instance.
  61. */
  62. public int getId()
  63. {
  64. return _id;
  65. }
  66. /**
  67. * Returns the name of this instance
  68. */
  69. public String getName()
  70. {
  71. return _name;
  72. }
  73. public void setName(String name)
  74. {
  75. _name = name;
  76. }
  77. /**
  78. * Returns whether summon friend type skills are allowed for this instance
  79. */
  80. public boolean isSummonAllowed()
  81. {
  82. return _allowSummon;
  83. }
  84. /**
  85. * Sets the status for the instance for summon friend type skills
  86. */
  87. public void setAllowSummon(boolean b)
  88. {
  89. _allowSummon = b;
  90. }
  91. /*
  92. * Returns true if entire instance is PvP zone
  93. */
  94. public boolean isPvPInstance()
  95. {
  96. return _isPvPInstance;
  97. }
  98. /*
  99. * Sets PvP zone status of the instance
  100. */
  101. public void setPvPInstance(boolean b)
  102. {
  103. _isPvPInstance = b;
  104. }
  105. /**
  106. * Set the instance duration task
  107. * @param duration in milliseconds
  108. */
  109. public void setDuration(int duration)
  110. {
  111. if (_CheckTimeUpTask != null)
  112. _CheckTimeUpTask.cancel(true);
  113. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(duration), 500);
  114. _instanceEndTime = System.currentTimeMillis() + duration + 500;
  115. }
  116. /**
  117. * Set time before empty instance will be removed
  118. * @param time in milliseconds
  119. */
  120. public void setEmptyDestroyTime(long time)
  121. {
  122. _emptyDestroyTime = time;
  123. }
  124. /**
  125. * Checks if the player exists within this instance
  126. * @param objectId
  127. * @return true if player exists in instance
  128. */
  129. public boolean containsPlayer(int objectId)
  130. {
  131. return _players.contains(objectId);
  132. }
  133. /**
  134. * Adds the specified player to the instance
  135. * @param objectId Players object ID
  136. */
  137. public void addPlayer(int objectId)
  138. {
  139. synchronized(_players)
  140. {
  141. _players.add(objectId);
  142. }
  143. }
  144. /**
  145. * Removes the specified player from the instance list
  146. * @param objectId Players object ID
  147. */
  148. public void removePlayer(int objectId)
  149. {
  150. synchronized(_players)
  151. {
  152. _players.remove(objectId);
  153. }
  154. if (_players.isEmpty() && _emptyDestroyTime >= 0)
  155. {
  156. _lastLeft = System.currentTimeMillis();
  157. setDuration((int) (_instanceEndTime - System.currentTimeMillis() - 1000));
  158. }
  159. }
  160. /**
  161. * Removes the player from the instance by setting InstanceId to 0 and teleporting to nearest town.
  162. * @param objectId
  163. */
  164. public void ejectPlayer(int objectId)
  165. {
  166. L2PcInstance player = (L2PcInstance) L2World.getInstance().findObject(objectId);
  167. if (player != null && player.getInstanceId() == this.getId())
  168. {
  169. player.setInstanceId(0);
  170. player.sendMessage("You were removed from the instance");
  171. if (getSpawnLoc()[0] != 0 && getSpawnLoc()[1] != 0 && getSpawnLoc()[2] != 0)
  172. player.teleToLocation(getSpawnLoc()[0], getSpawnLoc()[1], getSpawnLoc()[2]);
  173. else
  174. player.teleToLocation(MapRegionTable.TeleportWhereType.Town);
  175. }
  176. }
  177. public void addNpc(L2Npc npc)
  178. {
  179. _npcs.add(npc);
  180. }
  181. public void removeNpc(L2Spawn spawn)
  182. {
  183. _npcs.remove(spawn);
  184. }
  185. /**
  186. * Adds a door into the instance
  187. * @param doorId - from doors.csv
  188. * @param open - initial state of the door
  189. */
  190. public void addDoor(int doorId, boolean open)
  191. {
  192. for (L2DoorInstance door: _doors)
  193. {
  194. if (door.getDoorId() == doorId)
  195. {
  196. _log.warning("Door ID " + doorId + " already exists in instance " + this.getId());
  197. return;
  198. }
  199. }
  200. L2DoorInstance temp = DoorTable.getInstance().getDoor(doorId);
  201. L2DoorInstance newdoor = new L2DoorInstance(IdFactory.getInstance().getNextId(), temp.getTemplate(), temp.getDoorId(), temp.getName(), temp.isUnlockable());
  202. newdoor.setInstanceId(getId());
  203. newdoor.setRange(temp.getXMin(), temp.getYMin(), temp.getZMin(), temp.getXMax(), temp.getYMax(), temp.getZMax());
  204. try
  205. {
  206. newdoor.setMapRegion(MapRegionTable.getInstance().getMapRegion(temp.getX(), temp.getY()));
  207. }
  208. catch (Exception e)
  209. {
  210. _log.severe("Error in door data, ID:" + temp.getDoorId());
  211. }
  212. newdoor.getStatus().setCurrentHpMp(newdoor.getMaxHp(), newdoor.getMaxMp());
  213. newdoor.setOpen(open);
  214. newdoor.getPosition().setXYZInvisible(temp.getX(), temp.getY(), temp.getZ());
  215. newdoor.spawnMe(newdoor.getX(), newdoor.getY(), newdoor.getZ());
  216. _doors.add(newdoor);
  217. }
  218. public TIntHashSet getPlayers()
  219. {
  220. return _players;
  221. }
  222. public FastList<L2Npc> getNpcs()
  223. {
  224. return _npcs;
  225. }
  226. public FastList<L2DoorInstance> getDoors()
  227. {
  228. return _doors;
  229. }
  230. public L2DoorInstance getDoor(int id)
  231. {
  232. for (L2DoorInstance temp: getDoors())
  233. {
  234. if (temp.getDoorId() == id)
  235. return temp;
  236. }
  237. return null;
  238. }
  239. /**
  240. * Returns the spawn location for this instance to be used when leaving the instance
  241. * @return int[3]
  242. */
  243. public int[] getSpawnLoc()
  244. {
  245. return _spawnLoc;
  246. }
  247. /**
  248. * Sets the spawn location for this instance to be used when leaving the instance
  249. */
  250. public void setSpawnLoc(int[] loc)
  251. {
  252. if (loc == null || loc.length < 3)
  253. return;
  254. System.arraycopy(loc, 0, _spawnLoc, 0, 3);
  255. }
  256. public void removePlayers()
  257. {
  258. _players.forEach(_ejectProc);
  259. synchronized (_players)
  260. {
  261. _players.clear();
  262. }
  263. }
  264. public void removeNpcs()
  265. {
  266. for (L2Npc mob : _npcs)
  267. {
  268. if (mob != null)
  269. {
  270. if (mob.getSpawn() != null)
  271. mob.getSpawn().stopRespawn();
  272. mob.deleteMe();
  273. }
  274. }
  275. _npcs.clear();
  276. }
  277. public void removeDoors()
  278. {
  279. for (L2DoorInstance door: _doors)
  280. {
  281. if (door != null)
  282. {
  283. L2WorldRegion region = door.getWorldRegion();
  284. door.decayMe();
  285. if (region != null)
  286. region.removeVisibleObject(door);
  287. door.getKnownList().removeAllKnownObjects();
  288. L2World.getInstance().removeObject(door);
  289. }
  290. }
  291. _doors.clear();
  292. }
  293. public void loadInstanceTemplate(String filename) throws FileNotFoundException
  294. {
  295. Document doc = null;
  296. File xml = new File(Config.DATAPACK_ROOT, "data/instances/" + filename);
  297. try
  298. {
  299. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  300. factory.setValidating(false);
  301. factory.setIgnoringComments(true);
  302. doc = factory.newDocumentBuilder().parse(xml);
  303. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  304. {
  305. if ("instance".equalsIgnoreCase(n.getNodeName()))
  306. {
  307. parseInstance(n);
  308. }
  309. }
  310. }
  311. catch (IOException e)
  312. {
  313. _log.warning("Instance: can not find " + xml.getAbsolutePath() + " ! " + e);
  314. }
  315. catch (Exception e)
  316. {
  317. _log.warning("Instance: error while loading " + xml.getAbsolutePath() + " ! " + e);
  318. }
  319. }
  320. private void parseInstance(Node n) throws Exception
  321. {
  322. L2Spawn spawnDat;
  323. L2NpcTemplate npcTemplate;
  324. String name = null;
  325. name = n.getAttributes().getNamedItem("name").getNodeValue();
  326. setName(name);
  327. Node a;
  328. Node first = n.getFirstChild();
  329. for (n = first; n != null; n = n.getNextSibling())
  330. {
  331. if ("activityTime".equalsIgnoreCase(n.getNodeName()))
  332. {
  333. a = n.getAttributes().getNamedItem("val");
  334. if (a != null)
  335. {
  336. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(Integer.parseInt(a.getNodeValue()) * 60000), 15000);
  337. _instanceEndTime = System.currentTimeMillis() + Long.parseLong(a.getNodeValue()) * 60000 + 15000;
  338. }
  339. }
  340. /* else if ("timeDelay".equalsIgnoreCase(n.getNodeName()))
  341. {
  342. a = n.getAttributes().getNamedItem("val");
  343. if (a != null)
  344. instance.setTimeDelay(Integer.parseInt(a.getNodeValue()));
  345. }*/
  346. else if ("allowSummon".equalsIgnoreCase(n.getNodeName()))
  347. {
  348. a = n.getAttributes().getNamedItem("val");
  349. if (a != null)
  350. setAllowSummon(Boolean.parseBoolean(a.getNodeValue()));
  351. }
  352. else if ("emptyDestroyTime".equalsIgnoreCase(n.getNodeName()))
  353. {
  354. a = n.getAttributes().getNamedItem("val");
  355. if (a != null)
  356. _emptyDestroyTime = Long.parseLong(a.getNodeValue()) * 1000;
  357. }
  358. else if ("PvPInstance".equalsIgnoreCase(n.getNodeName()))
  359. {
  360. a = n.getAttributes().getNamedItem("val");
  361. if (a != null)
  362. setPvPInstance(Boolean.parseBoolean(a.getNodeValue()));
  363. }
  364. else if ("doorlist".equalsIgnoreCase(n.getNodeName()))
  365. {
  366. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  367. {
  368. int doorId = 0;
  369. boolean doorState = false;
  370. if ("door".equalsIgnoreCase(d.getNodeName()))
  371. {
  372. doorId = Integer.parseInt(d.getAttributes().getNamedItem("doorId").getNodeValue());
  373. if (d.getAttributes().getNamedItem("open") != null)
  374. doorState = Boolean.parseBoolean(d.getAttributes().getNamedItem("open").getNodeValue());
  375. addDoor(doorId, doorState);
  376. }
  377. }
  378. }
  379. else if ("spawnlist".equalsIgnoreCase(n.getNodeName()))
  380. {
  381. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  382. {
  383. int npcId = 0, x = 0, y = 0, z = 0, respawn = 0, heading = 0;
  384. if ("spawn".equalsIgnoreCase(d.getNodeName()))
  385. {
  386. npcId = Integer.parseInt(d.getAttributes().getNamedItem("npcId").getNodeValue());
  387. x = Integer.parseInt(d.getAttributes().getNamedItem("x").getNodeValue());
  388. y = Integer.parseInt(d.getAttributes().getNamedItem("y").getNodeValue());
  389. z = Integer.parseInt(d.getAttributes().getNamedItem("z").getNodeValue());
  390. heading = Integer.parseInt(d.getAttributes().getNamedItem("heading").getNodeValue());
  391. respawn = Integer.parseInt(d.getAttributes().getNamedItem("respawn").getNodeValue());
  392. npcTemplate = NpcTable.getInstance().getTemplate(npcId);
  393. if (npcTemplate != null)
  394. {
  395. spawnDat = new L2Spawn(npcTemplate);
  396. spawnDat.setLocx(x);
  397. spawnDat.setLocy(y);
  398. spawnDat.setLocz(z);
  399. spawnDat.setAmount(1);
  400. spawnDat.setHeading(heading);
  401. spawnDat.setRespawnDelay(respawn);
  402. if (respawn == 0)
  403. spawnDat.stopRespawn();
  404. else
  405. spawnDat.startRespawn();
  406. spawnDat.setInstanceId(getId());
  407. spawnDat.doSpawn();
  408. }
  409. else
  410. {
  411. _log.warning("Instance: Data missing in NPC table for ID: " + npcTemplate + " in Instance " + getId());
  412. }
  413. }
  414. }
  415. }
  416. else if ("spawnpoint".equalsIgnoreCase(n.getNodeName()))
  417. {
  418. try
  419. {
  420. _spawnLoc[0] = Integer.parseInt(n.getAttributes().getNamedItem("spawnX").getNodeValue());
  421. _spawnLoc[1] = Integer.parseInt(n.getAttributes().getNamedItem("spawnY").getNodeValue());
  422. _spawnLoc[2] = Integer.parseInt(n.getAttributes().getNamedItem("spawnZ").getNodeValue());
  423. }
  424. catch (Exception e)
  425. {
  426. _log.warning("Error parsing instance xml: " + e);
  427. _spawnLoc = new int[3];
  428. }
  429. }
  430. }
  431. if (Config.DEBUG)
  432. _log.info(name + " Instance Template for Instance " + getId() + " loaded");
  433. }
  434. protected void doCheckTimeUp(int remaining)
  435. {
  436. CreatureSay cs = null;
  437. int timeLeft;
  438. int interval;
  439. if (_players.isEmpty() && _emptyDestroyTime == 0)
  440. {
  441. remaining = 0;
  442. interval = 500;
  443. }
  444. else if (_players.isEmpty() && _emptyDestroyTime > 0)
  445. {
  446. Long emptyTimeLeft = _lastLeft + _emptyDestroyTime - System.currentTimeMillis();
  447. if (emptyTimeLeft <= 0)
  448. {
  449. interval = 0;
  450. remaining = 0;
  451. }
  452. else if (remaining > 300000 && emptyTimeLeft > 300000)
  453. {
  454. interval = 300000;
  455. remaining = remaining - 300000;
  456. }
  457. else if (remaining > 60000 && emptyTimeLeft > 60000)
  458. {
  459. interval = 60000;
  460. remaining = remaining - 60000;
  461. }
  462. else if (remaining > 30000 && emptyTimeLeft > 30000)
  463. {
  464. interval = 30000;
  465. remaining = remaining - 30000;
  466. }
  467. else
  468. {
  469. interval = 10000;
  470. remaining = remaining - 10000;
  471. }
  472. }
  473. else if (remaining > 300000)
  474. {
  475. timeLeft = remaining / 60000;
  476. interval = 300000;
  477. SystemMessage sm = new SystemMessage(SystemMessageId.DUNGEON_EXPIRES_IN_S1_MINUTES);
  478. sm.addString(Integer.toString(timeLeft));
  479. Announcements.getInstance().announceToInstance(sm, getId());
  480. remaining = remaining - 300000;
  481. }
  482. else if (remaining > 60000)
  483. {
  484. timeLeft = remaining / 60000;
  485. interval = 60000;
  486. SystemMessage sm = new SystemMessage(SystemMessageId.DUNGEON_EXPIRES_IN_S1_MINUTES);
  487. sm.addString(Integer.toString(timeLeft));
  488. Announcements.getInstance().announceToInstance(sm, getId());
  489. remaining = remaining - 60000;
  490. }
  491. else if (remaining > 30000)
  492. {
  493. timeLeft = remaining / 1000;
  494. interval = 30000;
  495. cs = new CreatureSay(0, Say2.ALLIANCE, "Notice", timeLeft + " seconds left.");
  496. remaining = remaining - 30000;
  497. }
  498. else
  499. {
  500. timeLeft = remaining / 1000;
  501. interval = 10000;
  502. cs = new CreatureSay(0, Say2.ALLIANCE, "Notice", timeLeft + " seconds left.");
  503. remaining = remaining - 10000;
  504. }
  505. if (cs != null)
  506. _players.forEach(new SendPacketToPlayerProcedure(cs));
  507. cancelTimer();
  508. if (remaining >= 10000)
  509. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(remaining), interval);
  510. else
  511. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new TimeUp(), interval);
  512. }
  513. public void cancelTimer()
  514. {
  515. if (_CheckTimeUpTask != null)
  516. _CheckTimeUpTask.cancel(true);
  517. }
  518. public class CheckTimeUp implements Runnable
  519. {
  520. private int _remaining;
  521. public CheckTimeUp(int remaining)
  522. {
  523. _remaining = remaining;
  524. }
  525. public void run()
  526. {
  527. doCheckTimeUp(_remaining);
  528. }
  529. }
  530. public class TimeUp implements Runnable
  531. {
  532. public void run()
  533. {
  534. InstanceManager.getInstance().destroyInstance(getId());
  535. }
  536. }
  537. private final class EjectPlayerProcedure implements TIntProcedure
  538. {
  539. EjectPlayerProcedure()
  540. {
  541. }
  542. @Override
  543. public final boolean execute(final int objId)
  544. {
  545. ejectPlayer(objId);
  546. return true;
  547. }
  548. }
  549. private final class SendPacketToPlayerProcedure implements TIntProcedure
  550. {
  551. private final L2GameServerPacket _packet;
  552. SendPacketToPlayerProcedure(final L2GameServerPacket packet)
  553. {
  554. _packet = packet;
  555. }
  556. @Override
  557. public final boolean execute(final int objId)
  558. {
  559. L2Object find = L2World.getInstance().findObject(objId);
  560. if (!(find instanceof L2PcInstance))
  561. return true;
  562. L2PcInstance player = (L2PcInstance)find;
  563. if (player != null && player.getInstanceId() == getId())
  564. {
  565. player.sendPacket(_packet);
  566. }
  567. return true;
  568. }
  569. }
  570. }