Instance.java 18 KB

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