Instance.java 18 KB

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