QueenAnt.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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.List;
  17. import javolution.util.FastList;
  18. import ai.group_template.L2AttackableAIScript;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.ai.CtrlIntention;
  21. import com.l2jserver.gameserver.datatables.SkillTable;
  22. import com.l2jserver.gameserver.instancemanager.GrandBossManager;
  23. import com.l2jserver.gameserver.model.L2Skill;
  24. import com.l2jserver.gameserver.model.StatsSet;
  25. import com.l2jserver.gameserver.model.actor.L2Attackable;
  26. import com.l2jserver.gameserver.model.actor.L2Npc;
  27. import com.l2jserver.gameserver.model.actor.L2Playable;
  28. import com.l2jserver.gameserver.model.actor.instance.L2GrandBossInstance;
  29. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.zone.type.L2BossZone;
  32. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  33. import com.l2jserver.gameserver.network.serverpackets.PlaySound;
  34. import com.l2jserver.gameserver.skills.SkillHolder;
  35. import com.l2jserver.util.Rnd;
  36. /**
  37. * Queen Ant AI
  38. * @author Emperorc
  39. */
  40. public class QueenAnt extends L2AttackableAIScript
  41. {
  42. private static final int QUEEN = 29001;
  43. private static final int LARVA = 29002;
  44. private static final int NURSE = 29003;
  45. private static final int GUARD = 29004;
  46. private static final int ROYAL = 29005;
  47. private static final int[] MOBS =
  48. {
  49. QUEEN, LARVA, NURSE, GUARD, ROYAL
  50. };
  51. private static final int QUEEN_X = -21610;
  52. private static final int QUEEN_Y = 181594;
  53. private static final int QUEEN_Z = -5734;
  54. // QUEEN Status Tracking :
  55. private static final byte ALIVE = 0; // Queen Ant is spawned.
  56. private static final byte DEAD = 1; // Queen Ant has been killed.
  57. private static L2BossZone _zone;
  58. private static SkillHolder HEAL1 = new SkillHolder(4020, 1);
  59. private static SkillHolder HEAL2 = new SkillHolder(4024, 1);
  60. private L2MonsterInstance _queen = null;
  61. private L2MonsterInstance _larva = null;
  62. private final List<L2MonsterInstance> _nurses = new FastList<L2MonsterInstance>(5);
  63. public QueenAnt(int questId, String name, String descr)
  64. {
  65. super(questId, name, descr);
  66. registerMobs(MOBS, QuestEventType.ON_SPAWN, QuestEventType.ON_KILL, QuestEventType.ON_AGGRO_RANGE_ENTER);
  67. addFactionCallId(NURSE);
  68. _zone = GrandBossManager.getInstance().getZone(QUEEN_X, QUEEN_Y, QUEEN_Z);
  69. StatsSet info = GrandBossManager.getInstance().getStatsSet(QUEEN);
  70. int status = GrandBossManager.getInstance().getBossStatus(QUEEN);
  71. if (status == DEAD)
  72. {
  73. // load the unlock date and time for queen ant from DB
  74. long temp = info.getLong("respawn_time") - System.currentTimeMillis();
  75. // if queen ant is locked until a certain time, mark it so and start the unlock timer
  76. // the unlock time has not yet expired.
  77. if (temp > 0)
  78. startQuestTimer("queen_unlock", temp, null, null);
  79. else
  80. {
  81. // the time has already expired while the server was offline. Immediately spawn queen ant.
  82. L2GrandBossInstance queen = (L2GrandBossInstance) addSpawn(QUEEN, QUEEN_X, QUEEN_Y, QUEEN_Z, 0, false, 0);
  83. GrandBossManager.getInstance().setBossStatus(QUEEN, ALIVE);
  84. spawnBoss(queen);
  85. }
  86. }
  87. else
  88. {
  89. int loc_x = info.getInteger("loc_x");
  90. int loc_y = info.getInteger("loc_y");
  91. int loc_z = info.getInteger("loc_z");
  92. int heading = info.getInteger("heading");
  93. int hp = info.getInteger("currentHP");
  94. int mp = info.getInteger("currentMP");
  95. if (!_zone.isInsideZone(loc_x, loc_y, loc_z))
  96. {
  97. loc_x = QUEEN_X;
  98. loc_y = QUEEN_Y;
  99. loc_z = QUEEN_Z;
  100. }
  101. L2GrandBossInstance queen = (L2GrandBossInstance) addSpawn(QUEEN, loc_x, loc_y, loc_z, heading, false, 0);
  102. queen.setCurrentHpMp(hp, mp);
  103. spawnBoss(queen);
  104. }
  105. }
  106. private void spawnBoss(L2GrandBossInstance npc)
  107. {
  108. GrandBossManager.getInstance().addBoss(npc);
  109. if (Rnd.get(100) < 33)
  110. _zone.movePlayersTo(-19480, 187344, -5600);
  111. else if (Rnd.get(100) < 50)
  112. _zone.movePlayersTo(-17928, 180912, -5520);
  113. else
  114. _zone.movePlayersTo(-23808, 182368, -5600);
  115. GrandBossManager.getInstance().addBoss(npc);
  116. startQuestTimer("action", 10000, npc, null, true);
  117. startQuestTimer("heal", 1000, null, null, true);
  118. npc.broadcastPacket(new PlaySound(1, "BS02_D", 1, npc.getObjectId(), npc.getX(), npc.getY(), npc.getZ()));
  119. _queen = npc;
  120. _larva = (L2MonsterInstance) addSpawn(LARVA, -21600, 179482, -5846, Rnd.get(360), false, 0);
  121. }
  122. @Override
  123. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  124. {
  125. if (event.equalsIgnoreCase("heal"))
  126. {
  127. boolean notCasting;
  128. final boolean larvaNeedHeal = _larva != null && _larva.getCurrentHp() < _larva.getMaxHp();
  129. final boolean queenNeedHeal = _queen != null && _queen.getCurrentHp() < _queen.getMaxHp();
  130. for (L2MonsterInstance nurse : _nurses)
  131. {
  132. if (nurse == null || nurse.isDead() || nurse.isCastingNow())
  133. continue;
  134. notCasting = nurse.getAI().getIntention() != CtrlIntention.AI_INTENTION_CAST;
  135. if (larvaNeedHeal)
  136. {
  137. if (nurse.getTarget() != _larva || notCasting)
  138. {
  139. nurse.setTarget(_larva);
  140. nurse.useMagic(Rnd.nextBoolean() ? HEAL1.getSkill() : HEAL2.getSkill());
  141. }
  142. continue;
  143. }
  144. if (queenNeedHeal)
  145. {
  146. if (nurse.getLeader() == _larva) // skip larva's minions
  147. continue;
  148. if (nurse.getTarget() != _queen || notCasting)
  149. {
  150. nurse.setTarget(_queen);
  151. nurse.useMagic(HEAL1.getSkill());
  152. }
  153. continue;
  154. }
  155. // if nurse not casting - remove target
  156. if (notCasting && nurse.getTarget() != null)
  157. nurse.setTarget(null);
  158. }
  159. }
  160. else if (event.equalsIgnoreCase("action") && npc != null)
  161. {
  162. if (Rnd.get(3) == 0)
  163. {
  164. if (Rnd.get(2) == 0)
  165. {
  166. npc.broadcastSocialAction(3);
  167. }
  168. else
  169. {
  170. npc.broadcastSocialAction(4);
  171. }
  172. }
  173. }
  174. else if (event.equalsIgnoreCase("queen_unlock"))
  175. {
  176. L2GrandBossInstance queen = (L2GrandBossInstance) addSpawn(QUEEN, QUEEN_X, QUEEN_Y, QUEEN_Z, 0, false, 0);
  177. GrandBossManager.getInstance().setBossStatus(QUEEN, ALIVE);
  178. spawnBoss(queen);
  179. }
  180. return super.onAdvEvent(event, npc, player);
  181. }
  182. @Override
  183. public String onSpawn(L2Npc npc)
  184. {
  185. final L2MonsterInstance mob = (L2MonsterInstance) npc;
  186. switch (npc.getNpcId())
  187. {
  188. case LARVA:
  189. mob.setIsImmobilized(true);
  190. mob.setIsMortal(false);
  191. mob.setIsRaidMinion(true);
  192. break;
  193. case NURSE:
  194. mob.disableCoreAI(true);
  195. mob.setIsRaidMinion(true);
  196. _nurses.add(mob);
  197. break;
  198. case ROYAL:
  199. case GUARD:
  200. mob.setIsRaidMinion(true);
  201. break;
  202. }
  203. return super.onSpawn(npc);
  204. }
  205. @Override
  206. public String onFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isPet)
  207. {
  208. if (caller == null || npc == null)
  209. return super.onFactionCall(npc, caller, attacker, isPet);
  210. if (!npc.isCastingNow() && npc.getAI().getIntention() != CtrlIntention.AI_INTENTION_CAST)
  211. {
  212. if (caller.getCurrentHp() < caller.getMaxHp())
  213. {
  214. npc.setTarget(caller);
  215. ((L2Attackable) npc).useMagic(HEAL1.getSkill());
  216. }
  217. }
  218. return null;
  219. }
  220. @Override
  221. public String onAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isPet)
  222. {
  223. if (npc == null)
  224. return null;
  225. final boolean isMage;
  226. final L2Playable character;
  227. if (isPet)
  228. {
  229. isMage = false;
  230. character = player.getPet();
  231. }
  232. else
  233. {
  234. isMage = player.isMageClass();
  235. character = player;
  236. }
  237. if (character == null)
  238. return null;
  239. if (!Config.RAID_DISABLE_CURSE && character.getLevel() - npc.getLevel() > 8)
  240. {
  241. L2Skill curse = null;
  242. if (isMage)
  243. {
  244. if (!character.isMuted() && Rnd.get(4) == 0)
  245. curse = SkillTable.FrequentSkill.RAID_CURSE.getSkill();
  246. }
  247. else
  248. {
  249. if (!character.isParalyzed() && Rnd.get(4) == 0)
  250. curse = SkillTable.FrequentSkill.RAID_CURSE2.getSkill();
  251. }
  252. if (curse != null)
  253. {
  254. npc.broadcastPacket(new MagicSkillUse(npc, character, curse.getId(), curse.getLevel(), 300, 0));
  255. curse.getEffects(npc, character);
  256. }
  257. ((L2Attackable) npc).stopHating(character); // for calling again
  258. return null;
  259. }
  260. return super.onAggroRangeEnter(npc, player, isPet);
  261. }
  262. @Override
  263. public String onKill(L2Npc npc, L2PcInstance killer, boolean isPet)
  264. {
  265. int npcId = npc.getNpcId();
  266. if (npcId == QUEEN)
  267. {
  268. npc.broadcastPacket(new PlaySound(1, "BS02_D", 1, npc.getObjectId(), npc.getX(), npc.getY(), npc.getZ()));
  269. GrandBossManager.getInstance().setBossStatus(QUEEN, DEAD);
  270. // time is 36hour +/- 17hour
  271. long respawnTime = (long) Config.Interval_Of_QueenAnt_Spawn + Rnd.get(Config.Random_Of_QueenAnt_Spawn);
  272. startQuestTimer("queen_unlock", respawnTime, null, null);
  273. cancelQuestTimer("action", npc, null);
  274. cancelQuestTimer("heal", null, null);
  275. // also save the respawn time so that the info is maintained past reboots
  276. StatsSet info = GrandBossManager.getInstance().getStatsSet(QUEEN);
  277. info.set("respawn_time", System.currentTimeMillis() + respawnTime);
  278. GrandBossManager.getInstance().setStatsSet(QUEEN, info);
  279. _nurses.clear();
  280. _larva.deleteMe();
  281. _larva = null;
  282. _queen = null;
  283. }
  284. else if (_queen != null && !_queen.isAlikeDead())
  285. {
  286. if (npcId == ROYAL)
  287. {
  288. L2MonsterInstance mob = (L2MonsterInstance) npc;
  289. if (mob.getLeader() != null)
  290. mob.getLeader().getMinionList().onMinionDie(mob, (280 + Rnd.get(40)) * 1000);
  291. }
  292. else if (npcId == NURSE)
  293. {
  294. L2MonsterInstance mob = (L2MonsterInstance) npc;
  295. _nurses.remove(mob);
  296. if (mob.getLeader() != null)
  297. mob.getLeader().getMinionList().onMinionDie(mob, 10000);
  298. }
  299. }
  300. return super.onKill(npc, killer, isPet);
  301. }
  302. public static void main(String[] args)
  303. {
  304. // now call the constructor (starts up the ai)
  305. new QueenAnt(-1, "queen_ant", "ai");
  306. }
  307. }