Instance.java 17 KB

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