SelMahumDrill.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 2004-2014 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.group_template;
  20. import ai.npc.AbstractNpcAI;
  21. import com.l2jserver.gameserver.ai.CtrlIntention;
  22. import com.l2jserver.gameserver.datatables.SpawnTable;
  23. import com.l2jserver.gameserver.model.L2Object;
  24. import com.l2jserver.gameserver.model.L2Spawn;
  25. import com.l2jserver.gameserver.model.Location;
  26. import com.l2jserver.gameserver.model.actor.L2Attackable;
  27. import com.l2jserver.gameserver.model.actor.L2Npc;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.network.NpcStringId;
  30. import com.l2jserver.gameserver.network.clientpackets.Say2;
  31. import com.l2jserver.gameserver.util.Util;
  32. /**
  33. * Sel Mahum Training Ground AI for drill groups.
  34. * @author GKR
  35. */
  36. public final class SelMahumDrill extends AbstractNpcAI
  37. {
  38. private static final int[] MAHUM_CHIEFS =
  39. {
  40. 22775, // Sel Mahum Drill Sergeant
  41. 22776, // Sel Mahum Training Officer
  42. 22778, // Sel Mahum Drill Sergeant
  43. };
  44. private static final int[] MAHUM_SOLDIERS =
  45. {
  46. 22780, // Sel Mahum Recruit
  47. 22782, // Sel Mahum Recruit
  48. 22783, // Sel Mahum Soldier
  49. 22784, // Sel Mahum Recruit
  50. 22785, // Sel Mahum Soldier
  51. };
  52. private static final int[] CHIEF_SOCIAL_ACTIONS =
  53. {
  54. 1,
  55. 4,
  56. 5,
  57. 7
  58. };
  59. private static final Actions[] SOLDIER_SOCIAL_ACTIONS =
  60. {
  61. Actions.SCE_TRAINING_ACTION_A,
  62. Actions.SCE_TRAINING_ACTION_B,
  63. Actions.SCE_TRAINING_ACTION_C,
  64. Actions.SCE_TRAINING_ACTION_D
  65. };
  66. private static final NpcStringId[] CHIEF_FSTRINGS =
  67. {
  68. NpcStringId.HOW_DARE_YOU_ATTACK_MY_RECRUITS,
  69. NpcStringId.WHO_IS_DISRUPTING_THE_ORDER
  70. };
  71. private static final NpcStringId[] SOLDIER_FSTRINGS =
  72. {
  73. NpcStringId.THE_DRILLMASTER_IS_DEAD,
  74. NpcStringId.LINE_UP_THE_RANKS
  75. };
  76. // Chiefs event broadcast range
  77. private static final int TRAINING_RANGE = 1000;
  78. private static enum Actions
  79. {
  80. SCE_TRAINING_ACTION_A(4, -1, 2, 2333),
  81. SCE_TRAINING_ACTION_B(1, -1, 2, 4333),
  82. SCE_TRAINING_ACTION_C(6, 5, 4, 1000),
  83. SCE_TRAINING_ACTION_D(7, -1, 2, 1000);
  84. private final int _socialActionId;
  85. private final int _altSocialActionId;
  86. private final int _repeatCount;
  87. private final int _repeatInterval;
  88. private Actions(int socialActionId, int altSocialActionId, int repeatCount, int repeatInterval)
  89. {
  90. _socialActionId = socialActionId;
  91. _altSocialActionId = altSocialActionId;
  92. _repeatCount = repeatCount;
  93. _repeatInterval = repeatInterval;
  94. }
  95. protected int getSocialActionId()
  96. {
  97. return _socialActionId;
  98. }
  99. protected int getAltSocialActionId()
  100. {
  101. return _altSocialActionId;
  102. }
  103. protected int getRepeatCount()
  104. {
  105. return _repeatCount;
  106. }
  107. protected int getRepeatInterval()
  108. {
  109. return _repeatInterval;
  110. }
  111. }
  112. private SelMahumDrill()
  113. {
  114. super(SelMahumDrill.class.getSimpleName(), "ai/group_template");
  115. addAttackId(MAHUM_SOLDIERS);
  116. addKillId(MAHUM_CHIEFS);
  117. addEventReceivedId(MAHUM_CHIEFS);
  118. addEventReceivedId(MAHUM_SOLDIERS);
  119. addSpawnId(MAHUM_CHIEFS);
  120. addSpawnId(MAHUM_SOLDIERS);
  121. // Start global return home timer
  122. startQuestTimer("return_home", 120000, null, null, true);
  123. }
  124. @Override
  125. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  126. {
  127. switch (event)
  128. {
  129. case "do_social_action":
  130. {
  131. if ((npc != null) && !npc.isDead())
  132. {
  133. if (Util.contains(MAHUM_CHIEFS, npc.getId()))
  134. {
  135. if ((npc.getVariables().getInt("BUSY_STATE") == 0) && (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) && npc.staysInSpawnLoc())
  136. {
  137. final int idx = getRandom(6);
  138. if (idx <= (CHIEF_SOCIAL_ACTIONS.length - 1))
  139. {
  140. npc.broadcastSocialAction(CHIEF_SOCIAL_ACTIONS[idx]);
  141. npc.getVariables().set("SOCIAL_ACTION_NEXT_INDEX", idx); // Pass social action index to soldiers via script value
  142. npc.broadcastEvent("do_social_action", TRAINING_RANGE, null);
  143. }
  144. }
  145. startQuestTimer("do_social_action", 15000, npc, null);
  146. }
  147. else if (Util.contains(MAHUM_SOLDIERS, npc.getId()))
  148. {
  149. handleSocialAction(npc, SOLDIER_SOCIAL_ACTIONS[npc.getVariables().getInt("SOCIAL_ACTION_NEXT_INDEX")], false);
  150. }
  151. }
  152. break;
  153. }
  154. case "reset_busy_state":
  155. {
  156. if (npc != null)
  157. {
  158. npc.getVariables().remove("BUSY_STATE");
  159. npc.disableCoreAI(false);
  160. }
  161. break;
  162. }
  163. case "return_home":
  164. {
  165. for (int npcId : MAHUM_SOLDIERS)
  166. {
  167. for (L2Spawn npcSpawn : SpawnTable.getInstance().getSpawns(npcId))
  168. {
  169. final L2Npc soldier = npcSpawn.getLastSpawn();
  170. if ((soldier != null) && !soldier.isDead() && (npcSpawn.getName() != null) && npcSpawn.getName().startsWith("smtg_drill_group") && !soldier.staysInSpawnLoc() && ((soldier.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (soldier.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE)))
  171. {
  172. soldier.setHeading(npcSpawn.getHeading());
  173. soldier.teleToLocation(npcSpawn.getLocation(), false);
  174. }
  175. }
  176. }
  177. break;
  178. }
  179. }
  180. return null;
  181. }
  182. @Override
  183. public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isSummon)
  184. {
  185. if (getRandom(10) < 1)
  186. {
  187. npc.broadcastEvent("ATTACKED", 1000, null);
  188. }
  189. return super.onAttack(npc, attacker, damage, isSummon);
  190. }
  191. @Override
  192. public String onEventReceived(String eventName, L2Npc sender, L2Npc receiver, L2Object reference)
  193. {
  194. if ((receiver != null) && !receiver.isDead() && receiver.isInMySpawnGroup(sender))
  195. {
  196. switch (eventName)
  197. {
  198. case "do_social_action":
  199. {
  200. if (Util.contains(MAHUM_SOLDIERS, receiver.getId()))
  201. {
  202. final int actionIndex = sender.getVariables().getInt("SOCIAL_ACTION_NEXT_INDEX");
  203. receiver.getVariables().set("SOCIAL_ACTION_NEXT_INDEX", actionIndex);
  204. handleSocialAction(receiver, SOLDIER_SOCIAL_ACTIONS[actionIndex], true);
  205. }
  206. break;
  207. }
  208. case "CHIEF_DIED":
  209. {
  210. if (Util.contains(MAHUM_SOLDIERS, receiver.getId()))
  211. {
  212. if (getRandom(4) < 1)
  213. {
  214. broadcastNpcSay(receiver, Say2.NPC_ALL, SOLDIER_FSTRINGS[getRandom(2)]);
  215. }
  216. if (receiver.canBeAttacked())
  217. {
  218. ((L2Attackable) receiver).clearAggroList();
  219. }
  220. receiver.disableCoreAI(true);
  221. receiver.getVariables().set("BUSY_STATE", 1);
  222. receiver.setIsRunning(true);
  223. receiver.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new Location((receiver.getX() + getRandom(-800, 800)), (receiver.getY() + getRandom(-800, 800)), receiver.getZ(), receiver.getHeading()));
  224. startQuestTimer("reset_busy_state", 5000, receiver, null);
  225. }
  226. break;
  227. }
  228. case "ATTACKED":
  229. {
  230. if (Util.contains(MAHUM_CHIEFS, receiver.getId()))
  231. {
  232. broadcastNpcSay(receiver, Say2.NPC_ALL, CHIEF_FSTRINGS[getRandom(2)]);
  233. }
  234. break;
  235. }
  236. }
  237. }
  238. return null;
  239. }
  240. @Override
  241. public String onKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  242. {
  243. npc.broadcastEvent("CHIEF_DIED", TRAINING_RANGE, null);
  244. return null;
  245. }
  246. @Override
  247. public String onSpawn(L2Npc npc)
  248. {
  249. if (Util.contains(MAHUM_CHIEFS, npc.getId()))
  250. {
  251. startQuestTimer("do_social_action", 15000, npc, null);
  252. }
  253. else if ((getRandom(18) < 1) && Util.contains(MAHUM_SOLDIERS, npc.getId()))
  254. {
  255. npc.getVariables().set("SOCIAL_ACTION_ALT_BEHAVIOR", 1);
  256. }
  257. // Restore AI handling by core
  258. npc.disableCoreAI(false);
  259. return null;
  260. }
  261. private void handleSocialAction(L2Npc npc, Actions action, boolean firstCall)
  262. {
  263. if ((npc.getVariables().getInt("BUSY_STATE") != 0) || (npc.getAI().getIntention() != CtrlIntention.AI_INTENTION_ACTIVE) || !npc.staysInSpawnLoc())
  264. {
  265. return;
  266. }
  267. final int socialActionId = (npc.getVariables().getInt("SOCIAL_ACTION_ALT_BEHAVIOR") == 0) ? action.getSocialActionId() : action.getAltSocialActionId();
  268. if (socialActionId < 0)
  269. {
  270. return;
  271. }
  272. if (firstCall)
  273. {
  274. npc.getVariables().set("SOCIAL_ACTION_REMAINED_COUNT", action.getRepeatCount());
  275. }
  276. npc.broadcastSocialAction(socialActionId);
  277. final int remainedCount = npc.getVariables().getInt("SOCIAL_ACTION_REMAINED_COUNT");
  278. if (remainedCount > 0)
  279. {
  280. npc.getVariables().set("SOCIAL_ACTION_REMAINED_COUNT", (remainedCount - 1));
  281. startQuestTimer("do_social_action", action.getRepeatInterval(), npc, null);
  282. }
  283. }
  284. public static void main(String[] args)
  285. {
  286. new SelMahumDrill();
  287. }
  288. }