Instance.java 17 KB

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