Instance.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. package net.sf.l2j.gameserver.model.entity;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.util.concurrent.ScheduledFuture;
  6. import java.util.logging.Logger;
  7. import javax.xml.parsers.DocumentBuilderFactory;
  8. import javolution.util.FastList;
  9. import javolution.util.FastSet;
  10. import org.w3c.dom.Document;
  11. import org.w3c.dom.Node;
  12. import net.sf.l2j.Config;
  13. import net.sf.l2j.gameserver.Announcements;
  14. import net.sf.l2j.gameserver.ThreadPoolManager;
  15. import net.sf.l2j.gameserver.datatables.DoorTable;
  16. import net.sf.l2j.gameserver.datatables.MapRegionTable;
  17. import net.sf.l2j.gameserver.datatables.NpcTable;
  18. import net.sf.l2j.gameserver.idfactory.IdFactory;
  19. import net.sf.l2j.gameserver.instancemanager.InstanceManager;
  20. import net.sf.l2j.gameserver.model.L2Spawn;
  21. import net.sf.l2j.gameserver.model.L2World;
  22. import net.sf.l2j.gameserver.model.L2WorldRegion;
  23. import net.sf.l2j.gameserver.model.actor.L2Npc;
  24. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  26. import net.sf.l2j.gameserver.network.SystemMessageId;
  27. import net.sf.l2j.gameserver.network.clientpackets.Say2;
  28. import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
  29. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  30. import net.sf.l2j.gameserver.templates.chars.L2NpcTemplate;
  31. /**
  32. * @author evill33t, GodKratos
  33. *
  34. */
  35. public class Instance
  36. {
  37. private final static Logger _log = Logger.getLogger(Instance.class.getName());
  38. private int _id;
  39. private String _name;
  40. private FastSet<Integer> _players = new FastSet<Integer>();
  41. private FastList<L2Npc> _npcs = new FastList<L2Npc>();
  42. private FastList<L2DoorInstance> _doors = new FastList<L2DoorInstance>();
  43. private int[] _spawnLoc = new int[3];
  44. private boolean _allowSummon = true;
  45. protected ScheduledFuture<?> _CheckTimeUpTask = null;
  46. public Instance(int id)
  47. {
  48. _id = id;
  49. }
  50. /**
  51. * Returns the ID of this instance.
  52. */
  53. public int getId()
  54. {
  55. return _id;
  56. }
  57. /**
  58. * Returns the name of this instance
  59. */
  60. public String getName()
  61. {
  62. return _name;
  63. }
  64. public void setName(String name)
  65. {
  66. _name = name;
  67. }
  68. /**
  69. * Returns whether summon friend type skills are allowed for this instance
  70. */
  71. public boolean isSummonAllowed()
  72. {
  73. return _allowSummon;
  74. }
  75. /**
  76. * Sets the status for the instance for summon friend type skills
  77. */
  78. public void setAllowSummon(boolean b)
  79. {
  80. _allowSummon = b;
  81. }
  82. /**
  83. * Set the instance duration task
  84. * @param duration in minutes
  85. */
  86. public void setDuration(int duration)
  87. {
  88. if (_CheckTimeUpTask != null)
  89. _CheckTimeUpTask.cancel(true);
  90. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(duration * 60000), 15000);
  91. }
  92. /**
  93. * Checks if the player exists within this instance
  94. * @param objectId
  95. * @return true if player exists in instance
  96. */
  97. public boolean containsPlayer(int objectId)
  98. {
  99. if (_players.contains(objectId))
  100. return true;
  101. return false;
  102. }
  103. /**
  104. * Adds the specified player to the instance
  105. * @param objectId Players object ID
  106. */
  107. public void addPlayer(int objectId)
  108. {
  109. if (!_players.contains(objectId))
  110. _players.add(objectId);
  111. }
  112. /**
  113. * Removes the specified player from the instance list
  114. * @param objectId Players object ID
  115. */
  116. public void removePlayer(int objectId)
  117. {
  118. if (_players.contains(objectId))
  119. _players.remove(objectId);
  120. }
  121. /**
  122. * Removes the player from the instance by setting InstanceId to 0 and teleporting to nearest town.
  123. * @param objectId
  124. */
  125. public void ejectPlayer(int objectId)
  126. {
  127. L2PcInstance player = (L2PcInstance) L2World.getInstance().findObject(objectId);
  128. if (player != null && player.getInstanceId() == this.getId())
  129. {
  130. player.setInstanceId(0);
  131. player.sendMessage("You were removed from the instance");
  132. if (getSpawnLoc()[0] != 0 && getSpawnLoc()[1] != 0 && getSpawnLoc()[2] != 0)
  133. player.teleToLocation(getSpawnLoc()[0], getSpawnLoc()[1], getSpawnLoc()[2]);
  134. else
  135. player.teleToLocation(MapRegionTable.TeleportWhereType.Town);
  136. }
  137. }
  138. public void removeNpc(L2Spawn spawn)
  139. {
  140. _npcs.remove(spawn);
  141. }
  142. /**
  143. * Adds a door into the instance
  144. * @param doorId - from doors.csv
  145. * @param open - initial state of the door
  146. */
  147. public void addDoor(int doorId, boolean open)
  148. {
  149. for (L2DoorInstance door: _doors)
  150. {
  151. if (door.getDoorId() == doorId)
  152. {
  153. _log.warning("Door ID " + doorId + " already exists in instance " + this.getId());
  154. return;
  155. }
  156. }
  157. L2DoorInstance temp = DoorTable.getInstance().getDoor(doorId);
  158. L2DoorInstance newdoor = new L2DoorInstance(IdFactory.getInstance().getNextId(), temp.getTemplate(), temp.getDoorId(), temp.getName(), temp.isUnlockable());
  159. newdoor.setInstanceId(getId());
  160. newdoor.setRange(temp.getXMin(), temp.getYMin(), temp.getZMin(), temp.getXMax(), temp.getYMax(), temp.getZMax());
  161. try
  162. {
  163. newdoor.setMapRegion(MapRegionTable.getInstance().getMapRegion(temp.getX(), temp.getY()));
  164. }
  165. catch (Exception e)
  166. {
  167. _log.severe("Error in door data, ID:" + temp.getDoorId());
  168. }
  169. newdoor.getStatus().setCurrentHpMp(newdoor.getMaxHp(), newdoor.getMaxMp());
  170. newdoor.setOpen(open);
  171. newdoor.getPosition().setXYZInvisible(temp.getX(), temp.getY(), temp.getZ());
  172. newdoor.spawnMe(newdoor.getX(), newdoor.getY(), newdoor.getZ());
  173. _doors.add(newdoor);
  174. }
  175. public FastSet<Integer> getPlayers()
  176. {
  177. return _players;
  178. }
  179. public FastList<L2Npc> getNpcs()
  180. {
  181. return _npcs;
  182. }
  183. public FastList<L2DoorInstance> getDoors()
  184. {
  185. return _doors;
  186. }
  187. public L2DoorInstance getDoor(int id)
  188. {
  189. for (L2DoorInstance temp: getDoors())
  190. {
  191. if (temp.getDoorId() == id)
  192. return temp;
  193. }
  194. return null;
  195. }
  196. /**
  197. * Returns the spawn location for this instance to be used when leaving the instance
  198. * @return int[3]
  199. */
  200. public int[] getSpawnLoc()
  201. {
  202. return _spawnLoc;
  203. }
  204. public void removePlayers()
  205. {
  206. for (int objectId : _players)
  207. {
  208. ejectPlayer(objectId);
  209. }
  210. _players.clear();
  211. }
  212. public void removeNpcs()
  213. {
  214. for (L2Npc mob : _npcs)
  215. {
  216. if (mob != null)
  217. {
  218. mob.getSpawn().stopRespawn();
  219. mob.deleteMe();
  220. }
  221. }
  222. _npcs.clear();
  223. }
  224. public void removeDoors()
  225. {
  226. for (L2DoorInstance door: _doors)
  227. {
  228. if (door != null)
  229. {
  230. L2WorldRegion region = door.getWorldRegion();
  231. door.decayMe();
  232. if (region != null)
  233. region.removeVisibleObject(door);
  234. door.getKnownList().removeAllKnownObjects();
  235. L2World.getInstance().removeObject(door);
  236. }
  237. }
  238. _doors.clear();
  239. }
  240. public void loadInstanceTemplate(String filename) throws FileNotFoundException
  241. {
  242. Document doc = null;
  243. File xml = new File(Config.DATAPACK_ROOT, "data/instances/" + filename);
  244. try
  245. {
  246. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  247. factory.setValidating(false);
  248. factory.setIgnoringComments(true);
  249. doc = factory.newDocumentBuilder().parse(xml);
  250. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  251. {
  252. if ("instance".equalsIgnoreCase(n.getNodeName()))
  253. {
  254. parseInstance(n);
  255. }
  256. }
  257. }
  258. catch (IOException e)
  259. {
  260. _log.warning("Instance: can not find " + xml.getAbsolutePath() + " ! " + e);
  261. }
  262. catch (Exception e)
  263. {
  264. _log.warning("Instance: error while loading " + xml.getAbsolutePath() + " ! " + e);
  265. }
  266. }
  267. private void parseInstance(Node n) throws Exception
  268. {
  269. L2Spawn spawnDat;
  270. L2NpcTemplate npcTemplate;
  271. String name = null;
  272. name = n.getAttributes().getNamedItem("name").getNodeValue();
  273. setName(name);
  274. Node a;
  275. Node first = n.getFirstChild();
  276. for (n = first; n != null; n = n.getNextSibling())
  277. {
  278. if ("activityTime".equalsIgnoreCase(n.getNodeName()))
  279. {
  280. a = n.getAttributes().getNamedItem("val");
  281. if (a != null)
  282. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(Integer.parseInt(a.getNodeValue()) * 60000), 15000);
  283. }
  284. /* else if ("timeDelay".equalsIgnoreCase(n.getNodeName()))
  285. {
  286. a = n.getAttributes().getNamedItem("val");
  287. if (a != null)
  288. instance.setTimeDelay(Integer.parseInt(a.getNodeValue()));
  289. }*/
  290. else if ("allowSummon".equalsIgnoreCase(n.getNodeName()))
  291. {
  292. a = n.getAttributes().getNamedItem("val");
  293. if (a != null)
  294. setAllowSummon(Boolean.parseBoolean(a.getNodeValue()));
  295. }
  296. else if ("doorlist".equalsIgnoreCase(n.getNodeName()))
  297. {
  298. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  299. {
  300. int doorId = 0;
  301. boolean doorState = false;
  302. if ("door".equalsIgnoreCase(d.getNodeName()))
  303. {
  304. doorId = Integer.parseInt(d.getAttributes().getNamedItem("doorId").getNodeValue());
  305. if (d.getAttributes().getNamedItem("open") != null)
  306. doorState = Boolean.parseBoolean(d.getAttributes().getNamedItem("open").getNodeValue());
  307. addDoor(doorId, doorState);
  308. }
  309. }
  310. }
  311. else if ("spawnlist".equalsIgnoreCase(n.getNodeName()))
  312. {
  313. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  314. {
  315. int npcId = 0, x = 0, y = 0, z = 0, respawn = 0, heading = 0;
  316. if ("spawn".equalsIgnoreCase(d.getNodeName()))
  317. {
  318. npcId = Integer.parseInt(d.getAttributes().getNamedItem("npcId").getNodeValue());
  319. x = Integer.parseInt(d.getAttributes().getNamedItem("x").getNodeValue());
  320. y = Integer.parseInt(d.getAttributes().getNamedItem("y").getNodeValue());
  321. z = Integer.parseInt(d.getAttributes().getNamedItem("z").getNodeValue());
  322. heading = Integer.parseInt(d.getAttributes().getNamedItem("heading").getNodeValue());
  323. respawn = Integer.parseInt(d.getAttributes().getNamedItem("respawn").getNodeValue());
  324. npcTemplate = NpcTable.getInstance().getTemplate(npcId);
  325. if (npcTemplate != null)
  326. {
  327. spawnDat = new L2Spawn(npcTemplate);
  328. spawnDat.setLocx(x);
  329. spawnDat.setLocy(y);
  330. spawnDat.setLocz(z);
  331. spawnDat.setAmount(1);
  332. spawnDat.setHeading(heading);
  333. spawnDat.setRespawnDelay(respawn);
  334. if (respawn == 0)
  335. spawnDat.stopRespawn();
  336. else
  337. spawnDat.startRespawn();
  338. spawnDat.setInstanceId(getId());
  339. L2Npc newmob = spawnDat.doSpawn();
  340. _npcs.add(newmob);
  341. }
  342. else
  343. {
  344. _log.warning("Instance: Data missing in NPC table for ID: " + npcTemplate + " in Instance " + getId());
  345. }
  346. }
  347. }
  348. }
  349. else if ("spawnpoint".equalsIgnoreCase(n.getNodeName()))
  350. {
  351. try
  352. {
  353. _spawnLoc[0] = Integer.parseInt(n.getAttributes().getNamedItem("spawnX").getNodeValue());
  354. _spawnLoc[1] = Integer.parseInt(n.getAttributes().getNamedItem("spawnY").getNodeValue());
  355. _spawnLoc[2] = Integer.parseInt(n.getAttributes().getNamedItem("spawnZ").getNodeValue());
  356. }
  357. catch (Exception e)
  358. {
  359. _log.warning("Error parsing instance xml: " + e);
  360. _spawnLoc = new int[3];
  361. }
  362. }
  363. }
  364. if (Config.DEBUG)
  365. _log.info(name + " Instance Template for Instance " + getId() + " loaded");
  366. }
  367. protected void doCheckTimeUp(int remaining)
  368. {
  369. CreatureSay cs = null;
  370. int timeLeft;
  371. int interval;
  372. if (remaining > 300000)
  373. {
  374. timeLeft = remaining / 60000;
  375. interval = 300000;
  376. SystemMessage sm = new SystemMessage(SystemMessageId.DUNGEON_EXPIRES_IN_S1_MINUTES);
  377. sm.addString(Integer.toString(timeLeft));
  378. Announcements.getInstance().announceToInstance(sm, getId());
  379. remaining = remaining - 300000;
  380. }
  381. else if (remaining > 60000)
  382. {
  383. timeLeft = remaining / 60000;
  384. interval = 60000;
  385. SystemMessage sm = new SystemMessage(SystemMessageId.DUNGEON_EXPIRES_IN_S1_MINUTES);
  386. sm.addString(Integer.toString(timeLeft));
  387. Announcements.getInstance().announceToInstance(sm, getId());
  388. remaining = remaining - 60000;
  389. }
  390. else if (remaining > 30000)
  391. {
  392. timeLeft = remaining / 1000;
  393. interval = 30000;
  394. cs = new CreatureSay(0, Say2.ALLIANCE, "Notice", timeLeft + " seconds left.");
  395. remaining = remaining - 30000;
  396. }
  397. else
  398. {
  399. timeLeft = remaining / 1000;
  400. interval = 10000;
  401. cs = new CreatureSay(0, Say2.ALLIANCE, "Notice", timeLeft + " seconds left.");
  402. remaining = remaining - 10000;
  403. }
  404. if (cs != null)
  405. {
  406. for (int objectId : _players)
  407. {
  408. L2PcInstance player = (L2PcInstance) L2World.getInstance().findObject(objectId);
  409. if (player != null && player.getInstanceId() == getId())
  410. {
  411. player.sendPacket(cs);
  412. }
  413. }
  414. }
  415. if (_CheckTimeUpTask != null)
  416. _CheckTimeUpTask.cancel(true);
  417. if (remaining >= 10000)
  418. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(remaining), interval);
  419. else
  420. _CheckTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new TimeUp(), interval);
  421. }
  422. public void cancelTimer()
  423. {
  424. _CheckTimeUpTask.cancel(true);
  425. }
  426. public class CheckTimeUp implements Runnable
  427. {
  428. private int _remaining;
  429. public CheckTimeUp(int remaining)
  430. {
  431. _remaining = remaining;
  432. }
  433. public void run()
  434. {
  435. doCheckTimeUp(_remaining);
  436. }
  437. }
  438. public class TimeUp implements Runnable
  439. {
  440. public void run()
  441. {
  442. InstanceManager.getInstance().destroyInstance(getId());
  443. }
  444. }
  445. }