IceFairySirra.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package ai.individual;
  16. import java.util.concurrent.Future;
  17. import java.util.logging.Level;
  18. import javolution.util.FastList;
  19. import ai.group_template.L2AttackableAIScript;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.cache.HtmCache;
  22. import com.l2jserver.gameserver.datatables.DoorTable;
  23. import com.l2jserver.gameserver.datatables.NpcTable;
  24. import com.l2jserver.gameserver.datatables.SpawnTable;
  25. import com.l2jserver.gameserver.instancemanager.GrandBossManager;
  26. import com.l2jserver.gameserver.model.L2Spawn;
  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.L2NpcTemplate;
  31. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  32. import com.l2jserver.gameserver.model.zone.type.L2BossZone;
  33. import com.l2jserver.gameserver.network.NpcStringId;
  34. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  35. import com.l2jserver.gameserver.network.serverpackets.ExShowScreenMessage;
  36. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  37. /**
  38. * Ice Fairy Sirra AI
  39. * @author Kerberos
  40. */
  41. public class IceFairySirra extends L2AttackableAIScript
  42. {
  43. private static final int STEWARD = 32029;
  44. private static final int SILVER_HEMOCYTE = 8057;
  45. private static L2BossZone _sirrasZone;
  46. private static L2PcInstance _player = null;
  47. protected FastList<L2Npc> _allMobs = new FastList<L2Npc>();
  48. protected Future<?> _onDeadEventTask = null;
  49. public IceFairySirra(int id, String name, String descr)
  50. {
  51. super(id, name, descr);
  52. int[] mob =
  53. {
  54. 22100,
  55. 22102,
  56. 22104,
  57. 29056
  58. };
  59. registerMobs(mob);
  60. registerMobs(new int[]
  61. {
  62. STEWARD
  63. }, QuestEventType.QUEST_START, QuestEventType.ON_TALK, QuestEventType.ON_FIRST_TALK);
  64. String test = loadGlobalQuestVar("Sirra_Respawn");
  65. if (!test.equalsIgnoreCase(""))
  66. {
  67. long remain = Long.parseLong(test) - System.currentTimeMillis();
  68. if (remain <= 0)
  69. {
  70. init(false);
  71. }
  72. else
  73. {
  74. init(true);
  75. startQuestTimer("respawn", remain, null, null);
  76. }
  77. }
  78. else
  79. {
  80. init(false);
  81. }
  82. }
  83. public void init(boolean isBusy)
  84. {
  85. _sirrasZone = GrandBossManager.getInstance().getZone(105546, -127892, -2768);
  86. if (_sirrasZone == null)
  87. {
  88. _log.warning("IceFairySirraManager: Failed to load zone");
  89. return;
  90. }
  91. _sirrasZone.setZoneEnabled(false);
  92. L2Npc steward = findTemplate(STEWARD);
  93. if (steward != null)
  94. {
  95. steward.setBusy(isBusy);
  96. }
  97. openGates();
  98. }
  99. public void cleanUp()
  100. {
  101. init(false);
  102. cancelQuestTimer("30MinutesRemaining", null, _player);
  103. cancelQuestTimer("20MinutesRemaining", null, _player);
  104. cancelQuestTimer("10MinutesRemaining", null, _player);
  105. cancelQuestTimer("End", null, _player);
  106. for (L2Npc mob : _allMobs)
  107. {
  108. try
  109. {
  110. mob.getSpawn().stopRespawn();
  111. mob.deleteMe();
  112. }
  113. catch (Exception e)
  114. {
  115. _log.log(Level.SEVERE, "IceFairySirraManager: Failed deleting mob.", e);
  116. }
  117. }
  118. _allMobs.clear();
  119. }
  120. public L2Npc findTemplate(int npcId)
  121. {
  122. L2Npc npc = null;
  123. for (L2Spawn spawn : SpawnTable.getInstance().getSpawnTable())
  124. {
  125. if ((spawn != null) && (spawn.getNpcid() == npcId))
  126. {
  127. npc = spawn.getLastSpawn();
  128. break;
  129. }
  130. }
  131. return npc;
  132. }
  133. protected void openGates()
  134. {
  135. for (int i = 23140001; i < 23140003; i++)
  136. {
  137. try
  138. {
  139. L2DoorInstance door = DoorTable.getInstance().getDoor(i);
  140. if (door != null)
  141. {
  142. door.openMe();
  143. }
  144. else
  145. {
  146. _log.warning("IceFairySirraManager: Attempted to open undefined door. doorId: " + i);
  147. }
  148. }
  149. catch (Exception e)
  150. {
  151. _log.log(Level.SEVERE, "IceFairySirraManager: Failed closing door", e);
  152. }
  153. }
  154. }
  155. protected void closeGates()
  156. {
  157. for (int i = 23140001; i < 23140003; i++)
  158. {
  159. try
  160. {
  161. L2DoorInstance door = DoorTable.getInstance().getDoor(i);
  162. if (door != null)
  163. {
  164. door.closeMe();
  165. }
  166. else
  167. {
  168. _log.warning("IceFairySirraManager: Attempted to close undefined door. doorId: " + i);
  169. }
  170. }
  171. catch (Exception e)
  172. {
  173. _log.log(Level.SEVERE, "IceFairySirraManager: Failed closing door", e);
  174. }
  175. }
  176. }
  177. public boolean checkItems(L2PcInstance player)
  178. {
  179. if (player.getParty() != null)
  180. {
  181. for (L2PcInstance pc : player.getParty().getPartyMembers())
  182. {
  183. L2ItemInstance i = pc.getInventory().getItemByItemId(SILVER_HEMOCYTE);
  184. if ((i == null) || (i.getCount() < 10))
  185. {
  186. return false;
  187. }
  188. }
  189. }
  190. else
  191. {
  192. return false;
  193. }
  194. return true;
  195. }
  196. public void destroyItems(L2PcInstance player)
  197. {
  198. if (player.getParty() != null)
  199. {
  200. for (L2PcInstance pc : player.getParty().getPartyMembers())
  201. {
  202. L2ItemInstance i = pc.getInventory().getItemByItemId(SILVER_HEMOCYTE);
  203. pc.destroyItem("Hemocytes", i.getObjectId(), 10, null, false);
  204. }
  205. }
  206. else
  207. {
  208. cleanUp();
  209. }
  210. }
  211. public void teleportInside(L2PcInstance player)
  212. {
  213. if (player.getParty() != null)
  214. {
  215. for (L2PcInstance pc : player.getParty().getPartyMembers())
  216. {
  217. pc.teleToLocation(113533, -126159, -3488, false);
  218. if (_sirrasZone == null)
  219. {
  220. _log.warning("IceFairySirraManager: Failed to load zone");
  221. cleanUp();
  222. return;
  223. }
  224. _sirrasZone.allowPlayerEntry(pc, 2103);
  225. }
  226. }
  227. else
  228. {
  229. cleanUp();
  230. }
  231. }
  232. public void screenMessage(L2PcInstance player, NpcStringId npcString, int time)
  233. {
  234. if (player.getParty() != null)
  235. {
  236. for (L2PcInstance pc : player.getParty().getPartyMembers())
  237. {
  238. pc.sendPacket(new ExShowScreenMessage(npcString, 2, time));
  239. }
  240. }
  241. else
  242. {
  243. cleanUp();
  244. }
  245. }
  246. public void doSpawns()
  247. {
  248. int[][] mobs =
  249. {
  250. {
  251. 29060,
  252. 105546,
  253. -127892,
  254. -2768
  255. },
  256. {
  257. 29056,
  258. 102779,
  259. -125920,
  260. -2840
  261. },
  262. {
  263. 22100,
  264. 111719,
  265. -126646,
  266. -2992
  267. },
  268. {
  269. 22102,
  270. 109509,
  271. -128946,
  272. -3216
  273. },
  274. {
  275. 22104,
  276. 109680,
  277. -125756,
  278. -3136
  279. }
  280. };
  281. L2Spawn spawnDat;
  282. L2NpcTemplate template;
  283. try
  284. {
  285. for (int i = 0; i < 5; i++)
  286. {
  287. template = NpcTable.getInstance().getTemplate(mobs[i][0]);
  288. if (template != null)
  289. {
  290. spawnDat = new L2Spawn(template);
  291. spawnDat.setAmount(1);
  292. spawnDat.setLocx(mobs[i][1]);
  293. spawnDat.setLocy(mobs[i][2]);
  294. spawnDat.setLocz(mobs[i][3]);
  295. spawnDat.setHeading(0);
  296. spawnDat.setRespawnDelay(60);
  297. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  298. _allMobs.add(spawnDat.doSpawn());
  299. spawnDat.stopRespawn();
  300. }
  301. else
  302. {
  303. _log.warning("IceFairySirraManager: Data missing in NPC table for ID: " + mobs[i][0]);
  304. }
  305. }
  306. }
  307. catch (Exception e)
  308. {
  309. _log.warning("IceFairySirraManager: Spawns could not be initialized: " + e);
  310. }
  311. }
  312. public String getHtmlPath(int val)
  313. {
  314. String pom = "";
  315. pom = "32029-" + val;
  316. if (val == 0)
  317. {
  318. pom = "32029";
  319. }
  320. String temp = "data/html/default/" + pom + ".htm";
  321. if (!Config.LAZY_CACHE)
  322. {
  323. // If not running lazy cache the file must be in the cache or it doesnt exist
  324. if (HtmCache.getInstance().contains(temp))
  325. {
  326. return temp;
  327. }
  328. }
  329. else if (HtmCache.getInstance().isLoadable(temp))
  330. {
  331. return temp;
  332. }
  333. // If the file is not found, the standard message "I have nothing to say to you" is returned
  334. return "data/html/npcdefault.htm";
  335. }
  336. public void sendHtml(L2Npc npc, L2PcInstance player, String filename)
  337. {
  338. NpcHtmlMessage html = new NpcHtmlMessage(npc.getObjectId());
  339. html.setFile(player.getHtmlPrefix(), filename);
  340. html.replace("%objectId%", String.valueOf(npc.getObjectId()));
  341. player.sendPacket(html);
  342. player.sendPacket(ActionFailed.STATIC_PACKET);
  343. }
  344. @Override
  345. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  346. {
  347. if (player.getQuestState("IceFairySirra") == null)
  348. {
  349. newQuestState(player);
  350. }
  351. player.setLastQuestNpcObject(npc.getObjectId());
  352. String filename = "";
  353. if (npc.isBusy())
  354. {
  355. filename = getHtmlPath(10);
  356. }
  357. else
  358. {
  359. filename = getHtmlPath(0);
  360. }
  361. sendHtml(npc, player, filename);
  362. return null;
  363. }
  364. @Override
  365. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  366. {
  367. if (event.equalsIgnoreCase("check_condition"))
  368. {
  369. if (npc.isBusy())// should never happen
  370. {
  371. return super.onAdvEvent(event, npc, player);
  372. }
  373. String filename = "";
  374. if (player.isInParty() && (player.getParty().getPartyLeaderOID() == player.getObjectId()))
  375. {
  376. if (checkItems(player))
  377. {
  378. startQuestTimer("start", 100000, null, player);
  379. _player = player;
  380. destroyItems(player);
  381. player.getInventory().addItem("Scroll", 8379, 3, player, null);
  382. npc.setBusy(true);
  383. screenMessage(player, NpcStringId.STEWARD_PLEASE_WAIT_A_MOMENT, 100000);
  384. filename = getHtmlPath(3);
  385. }
  386. else
  387. {
  388. filename = getHtmlPath(2);
  389. }
  390. }
  391. else
  392. {
  393. filename = getHtmlPath(1);
  394. }
  395. sendHtml(npc, player, filename);
  396. }
  397. else if (event.equalsIgnoreCase("start"))
  398. {
  399. if (_sirrasZone == null)
  400. {
  401. _log.warning("IceFairySirraManager: Failed to load zone");
  402. cleanUp();
  403. return super.onAdvEvent(event, npc, player);
  404. }
  405. _sirrasZone.setZoneEnabled(true);
  406. closeGates();
  407. doSpawns();
  408. startQuestTimer("Party_Port", 2000, null, player);
  409. startQuestTimer("End", 1802000, null, player);
  410. }
  411. else if (event.equalsIgnoreCase("Party_Port"))
  412. {
  413. teleportInside(player);
  414. screenMessage(player, NpcStringId.STEWARD_PLEASE_RESTORE_THE_QUEENS_FORMER_APPEARANCE, 10000);
  415. startQuestTimer("30MinutesRemaining", 300000, null, player);
  416. }
  417. else if (event.equalsIgnoreCase("30MinutesRemaining"))
  418. {
  419. screenMessage(player, NpcStringId.N30_MINUTES_REMAIN, 10000);
  420. startQuestTimer("20minutesremaining", 600000, null, player);
  421. }
  422. else if (event.equalsIgnoreCase("20MinutesRemaining"))
  423. {
  424. screenMessage(player, NpcStringId.N20_MINUTES_REMAIN, 10000);
  425. startQuestTimer("10minutesremaining", 600000, null, player);
  426. }
  427. else if (event.equalsIgnoreCase("10MinutesRemaining"))
  428. {
  429. screenMessage(player, NpcStringId.STEWARD_WASTE_NO_TIME_PLEASE_HURRY, 10000);
  430. }
  431. else if (event.equalsIgnoreCase("End"))
  432. {
  433. screenMessage(player, NpcStringId.STEWARD_WAS_IT_INDEED_TOO_MUCH_TO_ASK, 10000);
  434. cleanUp();
  435. }
  436. else if (event.equalsIgnoreCase("respawn"))
  437. {
  438. cleanUp();
  439. }
  440. return super.onAdvEvent(event, npc, player);
  441. }
  442. @Override
  443. public String onKill(L2Npc npc, L2PcInstance killer, boolean isPet)
  444. {
  445. if (npc.getNpcId() == 29056)
  446. {
  447. cancelQuestTimer("30MinutesRemaining", null, _player);
  448. cancelQuestTimer("20MinutesRemaining", null, _player);
  449. cancelQuestTimer("10MinutesRemaining", null, _player);
  450. cancelQuestTimer("End", null, _player);
  451. for (L2Npc mob : _allMobs)
  452. {
  453. try
  454. {
  455. mob.getSpawn().stopRespawn();
  456. mob.deleteMe();
  457. }
  458. catch (Exception e)
  459. {
  460. _log.log(Level.SEVERE, "IceFairySirraManager: Failed deleting mob.", e);
  461. }
  462. }
  463. _allMobs.clear();
  464. init(true);
  465. int respawnMinDelay = 43200000 * (int) (Config.RAID_MIN_RESPAWN_MULTIPLIER);
  466. int respawnMaxDelay = 129600000 * (int) (Config.RAID_MAX_RESPAWN_MULTIPLIER);
  467. int respawn_delay = getRandom(respawnMinDelay, respawnMaxDelay);
  468. saveGlobalQuestVar("Sirra_Respawn", String.valueOf(System.currentTimeMillis() + respawn_delay));
  469. startQuestTimer("respawn", respawn_delay, null, null);
  470. screenMessage(killer, NpcStringId.STEWARD_PLEASE_RESTORE_THE_QUEENS_FORMER_APPEARANCE, 10000);
  471. }
  472. return super.onKill(npc, killer, isPet);
  473. }
  474. public static void main(String[] args)
  475. {
  476. new IceFairySirra(-1, "IceFairySirra", "ai");
  477. }
  478. }