QueenAnt.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (C) 2004-2013 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package ai.individual;
  20. import java.util.List;
  21. import javolution.util.FastList;
  22. import ai.npc.AbstractNpcAI;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.ai.CtrlIntention;
  25. import com.l2jserver.gameserver.datatables.SkillTable;
  26. import com.l2jserver.gameserver.instancemanager.GrandBossManager;
  27. import com.l2jserver.gameserver.model.StatsSet;
  28. import com.l2jserver.gameserver.model.actor.L2Attackable;
  29. import com.l2jserver.gameserver.model.actor.L2Npc;
  30. import com.l2jserver.gameserver.model.actor.L2Playable;
  31. import com.l2jserver.gameserver.model.actor.instance.L2GrandBossInstance;
  32. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.holders.SkillHolder;
  35. import com.l2jserver.gameserver.model.skills.L2Skill;
  36. import com.l2jserver.gameserver.model.zone.type.L2BossZone;
  37. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  38. import com.l2jserver.gameserver.network.serverpackets.PlaySound;
  39. import com.l2jserver.util.Rnd;
  40. /**
  41. * Queen Ant's AI
  42. * @author Emperorc
  43. */
  44. public class QueenAnt extends AbstractNpcAI
  45. {
  46. private static final int QUEEN = 29001;
  47. private static final int LARVA = 29002;
  48. private static final int NURSE = 29003;
  49. private static final int GUARD = 29004;
  50. private static final int ROYAL = 29005;
  51. private static final int[] MOBS =
  52. {
  53. QUEEN,
  54. LARVA,
  55. NURSE,
  56. GUARD,
  57. ROYAL
  58. };
  59. private static final int QUEEN_X = -21610;
  60. private static final int QUEEN_Y = 181594;
  61. private static final int QUEEN_Z = -5734;
  62. // QUEEN Status Tracking :
  63. private static final byte ALIVE = 0; // Queen Ant is spawned.
  64. private static final byte DEAD = 1; // Queen Ant has been killed.
  65. private static L2BossZone _zone;
  66. private static SkillHolder HEAL1 = new SkillHolder(4020, 1);
  67. private static SkillHolder HEAL2 = new SkillHolder(4024, 1);
  68. private L2MonsterInstance _queen = null;
  69. private L2MonsterInstance _larva = null;
  70. private final List<L2MonsterInstance> _nurses = new FastList<>(5);
  71. private QueenAnt(String name, String descr)
  72. {
  73. super(name, descr);
  74. registerMobs(MOBS, QuestEventType.ON_SPAWN, QuestEventType.ON_KILL, QuestEventType.ON_AGGRO_RANGE_ENTER);
  75. addFactionCallId(NURSE);
  76. _zone = GrandBossManager.getInstance().getZone(QUEEN_X, QUEEN_Y, QUEEN_Z);
  77. StatsSet info = GrandBossManager.getInstance().getStatsSet(QUEEN);
  78. int status = GrandBossManager.getInstance().getBossStatus(QUEEN);
  79. if (status == DEAD)
  80. {
  81. // load the unlock date and time for queen ant from DB
  82. long temp = info.getLong("respawn_time") - System.currentTimeMillis();
  83. // if queen ant is locked until a certain time, mark it so and start the unlock timer
  84. // the unlock time has not yet expired.
  85. if (temp > 0)
  86. {
  87. startQuestTimer("queen_unlock", temp, null, null);
  88. }
  89. else
  90. {
  91. // the time has already expired while the server was offline. Immediately spawn queen ant.
  92. L2GrandBossInstance queen = (L2GrandBossInstance) addSpawn(QUEEN, QUEEN_X, QUEEN_Y, QUEEN_Z, 0, false, 0);
  93. GrandBossManager.getInstance().setBossStatus(QUEEN, ALIVE);
  94. spawnBoss(queen);
  95. }
  96. }
  97. else
  98. {
  99. int loc_x = info.getInteger("loc_x");
  100. int loc_y = info.getInteger("loc_y");
  101. int loc_z = info.getInteger("loc_z");
  102. int heading = info.getInteger("heading");
  103. int hp = info.getInteger("currentHP");
  104. int mp = info.getInteger("currentMP");
  105. if (!_zone.isInsideZone(loc_x, loc_y, loc_z))
  106. {
  107. loc_x = QUEEN_X;
  108. loc_y = QUEEN_Y;
  109. loc_z = QUEEN_Z;
  110. }
  111. L2GrandBossInstance queen = (L2GrandBossInstance) addSpawn(QUEEN, loc_x, loc_y, loc_z, heading, false, 0);
  112. queen.setCurrentHpMp(hp, mp);
  113. spawnBoss(queen);
  114. }
  115. }
  116. private void spawnBoss(L2GrandBossInstance npc)
  117. {
  118. GrandBossManager.getInstance().addBoss(npc);
  119. if (getRandom(100) < 33)
  120. {
  121. _zone.movePlayersTo(-19480, 187344, -5600);
  122. }
  123. else if (getRandom(100) < 50)
  124. {
  125. _zone.movePlayersTo(-17928, 180912, -5520);
  126. }
  127. else
  128. {
  129. _zone.movePlayersTo(-23808, 182368, -5600);
  130. }
  131. GrandBossManager.getInstance().addBoss(npc);
  132. startQuestTimer("action", 10000, npc, null, true);
  133. startQuestTimer("heal", 1000, null, null, true);
  134. npc.broadcastPacket(new PlaySound(1, "BS02_D", 1, npc.getObjectId(), npc.getX(), npc.getY(), npc.getZ()));
  135. _queen = npc;
  136. _larva = (L2MonsterInstance) addSpawn(LARVA, -21600, 179482, -5846, getRandom(360), false, 0);
  137. }
  138. @Override
  139. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  140. {
  141. if (event.equalsIgnoreCase("heal"))
  142. {
  143. boolean notCasting;
  144. final boolean larvaNeedHeal = (_larva != null) && (_larva.getCurrentHp() < _larva.getMaxHp());
  145. final boolean queenNeedHeal = (_queen != null) && (_queen.getCurrentHp() < _queen.getMaxHp());
  146. for (L2MonsterInstance nurse : _nurses)
  147. {
  148. if ((nurse == null) || nurse.isDead() || nurse.isCastingNow())
  149. {
  150. continue;
  151. }
  152. notCasting = nurse.getAI().getIntention() != CtrlIntention.AI_INTENTION_CAST;
  153. if (larvaNeedHeal)
  154. {
  155. if ((nurse.getTarget() != _larva) || notCasting)
  156. {
  157. nurse.setTarget(_larva);
  158. nurse.useMagic(Rnd.nextBoolean() ? HEAL1.getSkill() : HEAL2.getSkill());
  159. }
  160. continue;
  161. }
  162. if (queenNeedHeal)
  163. {
  164. if (nurse.getLeader() == _larva)
  165. {
  166. continue;
  167. }
  168. if ((nurse.getTarget() != _queen) || notCasting)
  169. {
  170. nurse.setTarget(_queen);
  171. nurse.useMagic(HEAL1.getSkill());
  172. }
  173. continue;
  174. }
  175. // if nurse not casting - remove target
  176. if (notCasting && (nurse.getTarget() != null))
  177. {
  178. nurse.setTarget(null);
  179. }
  180. }
  181. }
  182. else if (event.equalsIgnoreCase("action") && (npc != null))
  183. {
  184. if (getRandom(3) == 0)
  185. {
  186. if (getRandom(2) == 0)
  187. {
  188. npc.broadcastSocialAction(3);
  189. }
  190. else
  191. {
  192. npc.broadcastSocialAction(4);
  193. }
  194. }
  195. }
  196. else if (event.equalsIgnoreCase("queen_unlock"))
  197. {
  198. L2GrandBossInstance queen = (L2GrandBossInstance) addSpawn(QUEEN, QUEEN_X, QUEEN_Y, QUEEN_Z, 0, false, 0);
  199. GrandBossManager.getInstance().setBossStatus(QUEEN, ALIVE);
  200. spawnBoss(queen);
  201. }
  202. return super.onAdvEvent(event, npc, player);
  203. }
  204. @Override
  205. public String onSpawn(L2Npc npc)
  206. {
  207. final L2MonsterInstance mob = (L2MonsterInstance) npc;
  208. switch (npc.getNpcId())
  209. {
  210. case LARVA:
  211. mob.setIsImmobilized(true);
  212. mob.setIsMortal(false);
  213. mob.setIsRaidMinion(true);
  214. break;
  215. case NURSE:
  216. mob.disableCoreAI(true);
  217. mob.setIsRaidMinion(true);
  218. _nurses.add(mob);
  219. break;
  220. case ROYAL:
  221. case GUARD:
  222. mob.setIsRaidMinion(true);
  223. break;
  224. }
  225. return super.onSpawn(npc);
  226. }
  227. @Override
  228. public String onFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isSummon)
  229. {
  230. if ((caller == null) || (npc == null))
  231. {
  232. return super.onFactionCall(npc, caller, attacker, isSummon);
  233. }
  234. if (!npc.isCastingNow() && (npc.getAI().getIntention() != CtrlIntention.AI_INTENTION_CAST))
  235. {
  236. if (caller.getCurrentHp() < caller.getMaxHp())
  237. {
  238. npc.setTarget(caller);
  239. ((L2Attackable) npc).useMagic(HEAL1.getSkill());
  240. }
  241. }
  242. return null;
  243. }
  244. @Override
  245. public String onAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isSummon)
  246. {
  247. if (npc == null)
  248. {
  249. return null;
  250. }
  251. final boolean isMage;
  252. final L2Playable character;
  253. if (isSummon)
  254. {
  255. isMage = false;
  256. character = player.getSummon();
  257. }
  258. else
  259. {
  260. isMage = player.isMageClass();
  261. character = player;
  262. }
  263. if (character == null)
  264. {
  265. return null;
  266. }
  267. if (!Config.RAID_DISABLE_CURSE && ((character.getLevel() - npc.getLevel()) > 8))
  268. {
  269. L2Skill curse = null;
  270. if (isMage)
  271. {
  272. if (!character.isMuted() && (getRandom(4) == 0))
  273. {
  274. curse = SkillTable.FrequentSkill.RAID_CURSE.getSkill();
  275. }
  276. }
  277. else
  278. {
  279. if (!character.isParalyzed() && (getRandom(4) == 0))
  280. {
  281. curse = SkillTable.FrequentSkill.RAID_CURSE2.getSkill();
  282. }
  283. }
  284. if (curse != null)
  285. {
  286. npc.broadcastPacket(new MagicSkillUse(npc, character, curse.getId(), curse.getLevel(), 300, 0));
  287. curse.getEffects(npc, character);
  288. }
  289. ((L2Attackable) npc).stopHating(character); // for calling again
  290. return null;
  291. }
  292. return super.onAggroRangeEnter(npc, player, isSummon);
  293. }
  294. @Override
  295. public String onKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  296. {
  297. int npcId = npc.getNpcId();
  298. if (npcId == QUEEN)
  299. {
  300. npc.broadcastPacket(new PlaySound(1, "BS02_D", 1, npc.getObjectId(), npc.getX(), npc.getY(), npc.getZ()));
  301. GrandBossManager.getInstance().setBossStatus(QUEEN, DEAD);
  302. // Calculate Min and Max respawn times randomly.
  303. long respawnTime = Config.QUEEN_ANT_SPAWN_INTERVAL + getRandom(-Config.QUEEN_ANT_SPAWN_RANDOM, Config.QUEEN_ANT_SPAWN_RANDOM);
  304. respawnTime *= 60 * 60 * 1000;
  305. startQuestTimer("queen_unlock", respawnTime, null, null);
  306. cancelQuestTimer("action", npc, null);
  307. cancelQuestTimer("heal", null, null);
  308. // also save the respawn time so that the info is maintained past reboots
  309. StatsSet info = GrandBossManager.getInstance().getStatsSet(QUEEN);
  310. info.set("respawn_time", System.currentTimeMillis() + respawnTime);
  311. GrandBossManager.getInstance().setStatsSet(QUEEN, info);
  312. _nurses.clear();
  313. _larva.deleteMe();
  314. _larva = null;
  315. _queen = null;
  316. }
  317. else if ((_queen != null) && !_queen.isAlikeDead())
  318. {
  319. if (npcId == ROYAL)
  320. {
  321. L2MonsterInstance mob = (L2MonsterInstance) npc;
  322. if (mob.getLeader() != null)
  323. {
  324. mob.getLeader().getMinionList().onMinionDie(mob, (280 + getRandom(40)) * 1000);
  325. }
  326. }
  327. else if (npcId == NURSE)
  328. {
  329. L2MonsterInstance mob = (L2MonsterInstance) npc;
  330. _nurses.remove(mob);
  331. if (mob.getLeader() != null)
  332. {
  333. mob.getLeader().getMinionList().onMinionDie(mob, 10000);
  334. }
  335. }
  336. }
  337. return super.onKill(npc, killer, isSummon);
  338. }
  339. public static void main(String[] args)
  340. {
  341. new QueenAnt(QueenAnt.class.getSimpleName(), "ai");
  342. }
  343. }