Instance.java 18 KB

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