Instance.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. if (_players.contains(objectId))
  162. {
  163. _players.remove(_players.indexOf(objectId));
  164. }
  165. if (_players.isEmpty() && (_emptyDestroyTime >= 0))
  166. {
  167. _lastLeft = System.currentTimeMillis();
  168. setDuration((int) (_instanceEndTime - System.currentTimeMillis() - 500));
  169. }
  170. }
  171. public void addNpc(L2Npc npc)
  172. {
  173. _npcs.add(npc);
  174. }
  175. public void removeNpc(L2Npc npc)
  176. {
  177. if (npc.getSpawn() != null)
  178. {
  179. npc.getSpawn().stopRespawn();
  180. }
  181. _npcs.remove(npc);
  182. }
  183. /**
  184. * Adds a door into the instance
  185. * @param doorId - from doorData.xml
  186. * @param set - StatsSet for initializing door
  187. */
  188. private void addDoor(int doorId, StatsSet set)
  189. {
  190. if (_doors.containsKey(doorId))
  191. {
  192. _log.warning("Door ID " + doorId + " already exists in instance " + getId());
  193. return;
  194. }
  195. L2DoorTemplate temp = DoorTable.getInstance().getDoorTemplate(doorId);
  196. L2DoorInstance newdoor = new L2DoorInstance(IdFactory.getInstance().getNextId(), temp, set);
  197. newdoor.setInstanceId(getId());
  198. newdoor.setCurrentHp(newdoor.getMaxHp());
  199. newdoor.spawnMe(temp.posX, temp.posY, temp.posZ);
  200. _doors.put(doorId, newdoor);
  201. }
  202. public List<Integer> getPlayers()
  203. {
  204. return _players;
  205. }
  206. public List<L2Npc> getNpcs()
  207. {
  208. return _npcs;
  209. }
  210. public Collection<L2DoorInstance> getDoors()
  211. {
  212. return _doors.values();
  213. }
  214. public L2DoorInstance getDoor(int id)
  215. {
  216. return _doors.get(id);
  217. }
  218. public long getInstanceEndTime()
  219. {
  220. return _instanceEndTime;
  221. }
  222. public long getInstanceStartTime()
  223. {
  224. return _instanceStartTime;
  225. }
  226. public boolean isShowTimer()
  227. {
  228. return _showTimer;
  229. }
  230. public boolean isTimerIncrease()
  231. {
  232. return _isTimerIncrease;
  233. }
  234. public String getTimerText()
  235. {
  236. return _timerText;
  237. }
  238. /**
  239. * @return the spawn location for this instance to be used when leaving the instance
  240. */
  241. public Location getSpawnLoc()
  242. {
  243. return _spawnLoc;
  244. }
  245. /**
  246. * Sets the spawn location for this instance to be used when leaving the instance
  247. * @param loc
  248. */
  249. public void setSpawnLoc(Location loc)
  250. {
  251. _spawnLoc = loc;
  252. }
  253. public void removePlayers()
  254. {
  255. _players.executeForEach(new EjectProcedure());
  256. _players.clear();
  257. }
  258. public void removeNpcs()
  259. {
  260. for (L2Npc mob : _npcs)
  261. {
  262. if (mob != null)
  263. {
  264. if (mob.getSpawn() != null)
  265. {
  266. mob.getSpawn().stopRespawn();
  267. }
  268. mob.deleteMe();
  269. }
  270. }
  271. _npcs.clear();
  272. }
  273. public void removeDoors()
  274. {
  275. for (L2DoorInstance door : _doors.values())
  276. {
  277. if (door != null)
  278. {
  279. L2WorldRegion region = door.getWorldRegion();
  280. door.decayMe();
  281. if (region != null)
  282. {
  283. region.removeVisibleObject(door);
  284. }
  285. door.getKnownList().removeAllKnownObjects();
  286. L2World.getInstance().removeObject(door);
  287. }
  288. }
  289. _doors.clear();
  290. }
  291. public void loadInstanceTemplate(String filename)
  292. {
  293. Document doc = null;
  294. File xml = new File(Config.DATAPACK_ROOT, "data/instances/" + filename);
  295. try
  296. {
  297. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  298. factory.setValidating(false);
  299. factory.setIgnoringComments(true);
  300. doc = factory.newDocumentBuilder().parse(xml);
  301. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  302. {
  303. if ("instance".equalsIgnoreCase(n.getNodeName()))
  304. {
  305. parseInstance(n);
  306. }
  307. }
  308. }
  309. catch (IOException e)
  310. {
  311. _log.log(Level.WARNING, "Instance: can not find " + xml.getAbsolutePath() + " ! " + e.getMessage(), e);
  312. }
  313. catch (Exception e)
  314. {
  315. _log.log(Level.WARNING, "Instance: error while loading " + xml.getAbsolutePath() + " ! " + e.getMessage(), e);
  316. }
  317. }
  318. private void parseInstance(Node n) throws Exception
  319. {
  320. L2Spawn spawnDat;
  321. L2NpcTemplate npcTemplate;
  322. String name = null;
  323. name = n.getAttributes().getNamedItem("name").getNodeValue();
  324. setName(name);
  325. Node a;
  326. Node first = n.getFirstChild();
  327. for (n = first; n != null; n = n.getNextSibling())
  328. {
  329. if ("activityTime".equalsIgnoreCase(n.getNodeName()))
  330. {
  331. a = n.getAttributes().getNamedItem("val");
  332. if (a != null)
  333. {
  334. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(Integer.parseInt(a.getNodeValue()) * 60000), 15000);
  335. _instanceEndTime = System.currentTimeMillis() + (Long.parseLong(a.getNodeValue()) * 60000) + 15000;
  336. }
  337. }
  338. //@formatter:off
  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. */
  347. //@formatter:on
  348. else if ("allowSummon".equalsIgnoreCase(n.getNodeName()))
  349. {
  350. a = n.getAttributes().getNamedItem("val");
  351. if (a != null)
  352. {
  353. setAllowSummon(Boolean.parseBoolean(a.getNodeValue()));
  354. }
  355. }
  356. else if ("emptyDestroyTime".equalsIgnoreCase(n.getNodeName()))
  357. {
  358. a = n.getAttributes().getNamedItem("val");
  359. if (a != null)
  360. {
  361. _emptyDestroyTime = Long.parseLong(a.getNodeValue()) * 1000;
  362. }
  363. }
  364. else if ("showTimer".equalsIgnoreCase(n.getNodeName()))
  365. {
  366. a = n.getAttributes().getNamedItem("val");
  367. if (a != null)
  368. {
  369. _showTimer = Boolean.parseBoolean(a.getNodeValue());
  370. }
  371. a = n.getAttributes().getNamedItem("increase");
  372. if (a != null)
  373. {
  374. _isTimerIncrease = Boolean.parseBoolean(a.getNodeValue());
  375. }
  376. a = n.getAttributes().getNamedItem("text");
  377. if (a != null)
  378. {
  379. _timerText = a.getNodeValue();
  380. }
  381. }
  382. else if ("PvPInstance".equalsIgnoreCase(n.getNodeName()))
  383. {
  384. a = n.getAttributes().getNamedItem("val");
  385. if (a != null)
  386. {
  387. setPvPInstance(Boolean.parseBoolean(a.getNodeValue()));
  388. }
  389. }
  390. else if ("doorlist".equalsIgnoreCase(n.getNodeName()))
  391. {
  392. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  393. {
  394. int doorId = 0;
  395. if ("door".equalsIgnoreCase(d.getNodeName()))
  396. {
  397. doorId = Integer.parseInt(d.getAttributes().getNamedItem("doorId").getNodeValue());
  398. StatsSet set = new StatsSet();
  399. for (Node bean = d.getFirstChild(); bean != null; bean = bean.getNextSibling())
  400. {
  401. if ("set".equalsIgnoreCase(bean.getNodeName()))
  402. {
  403. NamedNodeMap attrs = bean.getAttributes();
  404. String setname = attrs.getNamedItem("name").getNodeValue();
  405. String value = attrs.getNamedItem("val").getNodeValue();
  406. set.set(setname, value);
  407. }
  408. }
  409. addDoor(doorId, set);
  410. }
  411. }
  412. }
  413. else if ("spawnlist".equalsIgnoreCase(n.getNodeName()))
  414. {
  415. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  416. {
  417. int npcId = 0, x = 0, y = 0, z = 0, respawn = 0, heading = 0, delay = -1;
  418. if ("spawn".equalsIgnoreCase(d.getNodeName()))
  419. {
  420. npcId = Integer.parseInt(d.getAttributes().getNamedItem("npcId").getNodeValue());
  421. x = Integer.parseInt(d.getAttributes().getNamedItem("x").getNodeValue());
  422. y = Integer.parseInt(d.getAttributes().getNamedItem("y").getNodeValue());
  423. z = Integer.parseInt(d.getAttributes().getNamedItem("z").getNodeValue());
  424. heading = Integer.parseInt(d.getAttributes().getNamedItem("heading").getNodeValue());
  425. respawn = Integer.parseInt(d.getAttributes().getNamedItem("respawn").getNodeValue());
  426. if (d.getAttributes().getNamedItem("onKillDelay") != null)
  427. {
  428. delay = Integer.parseInt(d.getAttributes().getNamedItem("onKillDelay").getNodeValue());
  429. }
  430. npcTemplate = NpcTable.getInstance().getTemplate(npcId);
  431. if (npcTemplate != null)
  432. {
  433. spawnDat = new L2Spawn(npcTemplate);
  434. spawnDat.setLocx(x);
  435. spawnDat.setLocy(y);
  436. spawnDat.setLocz(z);
  437. spawnDat.setAmount(1);
  438. spawnDat.setHeading(heading);
  439. spawnDat.setRespawnDelay(respawn);
  440. if (respawn == 0)
  441. {
  442. spawnDat.stopRespawn();
  443. }
  444. else
  445. {
  446. spawnDat.startRespawn();
  447. }
  448. spawnDat.setInstanceId(getId());
  449. L2Npc spawned = spawnDat.doSpawn();
  450. if ((delay >= 0) && (spawned instanceof L2Attackable))
  451. {
  452. ((L2Attackable) spawned).setOnKillDelay(delay);
  453. }
  454. }
  455. else
  456. {
  457. _log.warning("Instance: Data missing in NPC table for ID: " + npcId + " in Instance " + getId());
  458. }
  459. }
  460. }
  461. }
  462. else if ("spawnpoint".equalsIgnoreCase(n.getNodeName()))
  463. {
  464. try
  465. {
  466. int x = Integer.parseInt(n.getAttributes().getNamedItem("spawnX").getNodeValue());
  467. int y = Integer.parseInt(n.getAttributes().getNamedItem("spawnY").getNodeValue());
  468. int z = Integer.parseInt(n.getAttributes().getNamedItem("spawnZ").getNodeValue());
  469. _spawnLoc = new Location(x, y, z);
  470. }
  471. catch (Exception e)
  472. {
  473. _log.log(Level.WARNING, "Error parsing instance xml: " + e.getMessage(), e);
  474. _spawnLoc = null;
  475. }
  476. }
  477. }
  478. if (Config.DEBUG)
  479. {
  480. _log.info(name + " Instance Template for Instance " + getId() + " loaded");
  481. }
  482. }
  483. protected void doCheckTimeUp(int remaining)
  484. {
  485. CreatureSay cs = null;
  486. int timeLeft;
  487. int interval;
  488. if (_players.isEmpty() && (_emptyDestroyTime == 0))
  489. {
  490. remaining = 0;
  491. interval = 500;
  492. }
  493. else if (_players.isEmpty() && (_emptyDestroyTime > 0))
  494. {
  495. Long emptyTimeLeft = (_lastLeft + _emptyDestroyTime) - System.currentTimeMillis();
  496. if (emptyTimeLeft <= 0)
  497. {
  498. interval = 0;
  499. remaining = 0;
  500. }
  501. else if ((remaining > 300000) && (emptyTimeLeft > 300000))
  502. {
  503. interval = 300000;
  504. remaining = remaining - 300000;
  505. }
  506. else if ((remaining > 60000) && (emptyTimeLeft > 60000))
  507. {
  508. interval = 60000;
  509. remaining = remaining - 60000;
  510. }
  511. else if ((remaining > 30000) && (emptyTimeLeft > 30000))
  512. {
  513. interval = 30000;
  514. remaining = remaining - 30000;
  515. }
  516. else
  517. {
  518. interval = 10000;
  519. remaining = remaining - 10000;
  520. }
  521. }
  522. else if (remaining > 300000)
  523. {
  524. timeLeft = remaining / 60000;
  525. interval = 300000;
  526. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.DUNGEON_EXPIRES_IN_S1_MINUTES);
  527. sm.addString(Integer.toString(timeLeft));
  528. Announcements.getInstance().announceToInstance(sm, getId());
  529. remaining = remaining - 300000;
  530. }
  531. else if (remaining > 60000)
  532. {
  533. timeLeft = remaining / 60000;
  534. interval = 60000;
  535. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.DUNGEON_EXPIRES_IN_S1_MINUTES);
  536. sm.addString(Integer.toString(timeLeft));
  537. Announcements.getInstance().announceToInstance(sm, getId());
  538. remaining = remaining - 60000;
  539. }
  540. else if (remaining > 30000)
  541. {
  542. timeLeft = remaining / 1000;
  543. interval = 30000;
  544. cs = new CreatureSay(0, Say2.ALLIANCE, "Notice", timeLeft + " seconds left.");
  545. remaining = remaining - 30000;
  546. }
  547. else
  548. {
  549. timeLeft = remaining / 1000;
  550. interval = 10000;
  551. cs = new CreatureSay(0, Say2.ALLIANCE, "Notice", timeLeft + " seconds left.");
  552. remaining = remaining - 10000;
  553. }
  554. if (cs != null)
  555. {
  556. _players.executeForEach(new BroadcastPacket(cs));
  557. }
  558. cancelTimer();
  559. if (remaining >= 10000)
  560. {
  561. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(remaining), interval);
  562. }
  563. else
  564. {
  565. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new TimeUp(), interval);
  566. }
  567. }
  568. public void cancelTimer()
  569. {
  570. if (_CheckTimeUpTask != null)
  571. {
  572. _CheckTimeUpTask.cancel(true);
  573. }
  574. }
  575. public class CheckTimeUp implements Runnable
  576. {
  577. private final int _remaining;
  578. public CheckTimeUp(int remaining)
  579. {
  580. _remaining = remaining;
  581. }
  582. @Override
  583. public void run()
  584. {
  585. doCheckTimeUp(_remaining);
  586. }
  587. }
  588. public class TimeUp implements Runnable
  589. {
  590. @Override
  591. public void run()
  592. {
  593. InstanceManager.getInstance().destroyInstance(getId());
  594. }
  595. }
  596. public final class EjectProcedure implements IL2Procedure<Integer>
  597. {
  598. @Override
  599. public boolean execute(Integer objectId)
  600. {
  601. final L2PcInstance player = L2World.getInstance().getPlayer(objectId);
  602. if ((player != null) && (player.getInstanceId() == getId()))
  603. {
  604. player.setInstanceId(0);
  605. player.sendMessage("You were removed from the instance");
  606. if (getSpawnLoc() != null)
  607. {
  608. player.teleToLocation(getSpawnLoc(), true);
  609. }
  610. else
  611. {
  612. player.teleToLocation(MapRegionManager.TeleportWhereType.Town);
  613. }
  614. }
  615. return true;
  616. }
  617. }
  618. public final class BroadcastPacket implements IL2Procedure<Integer>
  619. {
  620. private final L2GameServerPacket _packet;
  621. public BroadcastPacket(L2GameServerPacket packet)
  622. {
  623. _packet = packet;
  624. }
  625. @Override
  626. public boolean execute(Integer objectId)
  627. {
  628. final L2PcInstance player = L2World.getInstance().getPlayer(objectId);
  629. if ((player != null) && (player.getInstanceId() == getId()))
  630. {
  631. player.sendPacket(_packet);
  632. }
  633. return true;
  634. }
  635. }
  636. }