Instance.java 17 KB

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