MobGroup.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.model;
  20. import java.util.List;
  21. import javolution.util.FastList;
  22. import com.l2jserver.gameserver.ai.CtrlIntention;
  23. import com.l2jserver.gameserver.ai.L2ControllableMobAI;
  24. import com.l2jserver.gameserver.datatables.SpawnTable;
  25. import com.l2jserver.gameserver.model.actor.L2Character;
  26. import com.l2jserver.gameserver.model.actor.instance.L2ControllableMobInstance;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  29. import com.l2jserver.util.Rnd;
  30. /**
  31. * @author littlecrow
  32. */
  33. public final class MobGroup
  34. {
  35. private final L2NpcTemplate _npcTemplate;
  36. private final int _groupId;
  37. private final int _maxMobCount;
  38. private List<L2ControllableMobInstance> _mobs;
  39. public MobGroup(int groupId, L2NpcTemplate npcTemplate, int maxMobCount)
  40. {
  41. _groupId = groupId;
  42. _npcTemplate = npcTemplate;
  43. _maxMobCount = maxMobCount;
  44. }
  45. public int getActiveMobCount()
  46. {
  47. return getMobs().size();
  48. }
  49. public int getGroupId()
  50. {
  51. return _groupId;
  52. }
  53. public int getMaxMobCount()
  54. {
  55. return _maxMobCount;
  56. }
  57. public List<L2ControllableMobInstance> getMobs()
  58. {
  59. if (_mobs == null)
  60. {
  61. _mobs = new FastList<>();
  62. }
  63. return _mobs;
  64. }
  65. public String getStatus()
  66. {
  67. try
  68. {
  69. L2ControllableMobAI mobGroupAI = (L2ControllableMobAI) getMobs().get(0).getAI();
  70. switch (mobGroupAI.getAlternateAI())
  71. {
  72. case L2ControllableMobAI.AI_NORMAL:
  73. return "Idle";
  74. case L2ControllableMobAI.AI_FORCEATTACK:
  75. return "Force Attacking";
  76. case L2ControllableMobAI.AI_FOLLOW:
  77. return "Following";
  78. case L2ControllableMobAI.AI_CAST:
  79. return "Casting";
  80. case L2ControllableMobAI.AI_ATTACK_GROUP:
  81. return "Attacking Group";
  82. default:
  83. return "Idle";
  84. }
  85. }
  86. catch (Exception e)
  87. {
  88. return "Unspawned";
  89. }
  90. }
  91. public L2NpcTemplate getTemplate()
  92. {
  93. return _npcTemplate;
  94. }
  95. public boolean isGroupMember(L2ControllableMobInstance mobInst)
  96. {
  97. for (L2ControllableMobInstance groupMember : getMobs())
  98. {
  99. if (groupMember == null)
  100. {
  101. continue;
  102. }
  103. if (groupMember.getObjectId() == mobInst.getObjectId())
  104. {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. public void spawnGroup(int x, int y, int z)
  111. {
  112. if (getActiveMobCount() > 0)
  113. {
  114. return;
  115. }
  116. try
  117. {
  118. for (int i = 0; i < getMaxMobCount(); i++)
  119. {
  120. L2GroupSpawn spawn = new L2GroupSpawn(getTemplate());
  121. int signX = (Rnd.nextInt(2) == 0) ? -1 : 1;
  122. int signY = (Rnd.nextInt(2) == 0) ? -1 : 1;
  123. int randX = Rnd.nextInt(MobGroupTable.RANDOM_RANGE);
  124. int randY = Rnd.nextInt(MobGroupTable.RANDOM_RANGE);
  125. spawn.setX(x + (signX * randX));
  126. spawn.setY(y + (signY * randY));
  127. spawn.setZ(z);
  128. spawn.stopRespawn();
  129. SpawnTable.getInstance().addNewSpawn(spawn, false);
  130. getMobs().add((L2ControllableMobInstance) spawn.doGroupSpawn());
  131. }
  132. }
  133. catch (ClassNotFoundException e)
  134. {
  135. }
  136. catch (NoSuchMethodException e2)
  137. {
  138. }
  139. }
  140. public void spawnGroup(L2PcInstance activeChar)
  141. {
  142. spawnGroup(activeChar.getX(), activeChar.getY(), activeChar.getZ());
  143. }
  144. public void teleportGroup(L2PcInstance player)
  145. {
  146. removeDead();
  147. for (L2ControllableMobInstance mobInst : getMobs())
  148. {
  149. if (mobInst == null)
  150. {
  151. continue;
  152. }
  153. if (!mobInst.isDead())
  154. {
  155. int x = player.getX() + Rnd.nextInt(50);
  156. int y = player.getY() + Rnd.nextInt(50);
  157. mobInst.teleToLocation(x, y, player.getZ(), true);
  158. L2ControllableMobAI ai = (L2ControllableMobAI) mobInst.getAI();
  159. ai.follow(player);
  160. }
  161. }
  162. }
  163. public L2ControllableMobInstance getRandomMob()
  164. {
  165. removeDead();
  166. if (getActiveMobCount() == 0)
  167. {
  168. return null;
  169. }
  170. int choice = Rnd.nextInt(getActiveMobCount());
  171. return getMobs().get(choice);
  172. }
  173. public void unspawnGroup()
  174. {
  175. removeDead();
  176. if (getActiveMobCount() == 0)
  177. {
  178. return;
  179. }
  180. for (L2ControllableMobInstance mobInst : getMobs())
  181. {
  182. if (mobInst == null)
  183. {
  184. continue;
  185. }
  186. if (!mobInst.isDead())
  187. {
  188. mobInst.deleteMe();
  189. }
  190. SpawnTable.getInstance().deleteSpawn(mobInst.getSpawn(), false);
  191. }
  192. getMobs().clear();
  193. }
  194. public void killGroup(L2PcInstance activeChar)
  195. {
  196. removeDead();
  197. for (L2ControllableMobInstance mobInst : getMobs())
  198. {
  199. if (mobInst == null)
  200. {
  201. continue;
  202. }
  203. if (!mobInst.isDead())
  204. {
  205. mobInst.reduceCurrentHp(mobInst.getMaxHp() + 1, activeChar, null);
  206. }
  207. SpawnTable.getInstance().deleteSpawn(mobInst.getSpawn(), false);
  208. }
  209. getMobs().clear();
  210. }
  211. public void setAttackRandom()
  212. {
  213. removeDead();
  214. for (L2ControllableMobInstance mobInst : getMobs())
  215. {
  216. if (mobInst == null)
  217. {
  218. continue;
  219. }
  220. L2ControllableMobAI ai = (L2ControllableMobAI) mobInst.getAI();
  221. ai.setAlternateAI(L2ControllableMobAI.AI_NORMAL);
  222. ai.setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  223. }
  224. }
  225. public void setAttackTarget(L2Character target)
  226. {
  227. removeDead();
  228. for (L2ControllableMobInstance mobInst : getMobs())
  229. {
  230. if (mobInst == null)
  231. {
  232. continue;
  233. }
  234. L2ControllableMobAI ai = (L2ControllableMobAI) mobInst.getAI();
  235. ai.forceAttack(target);
  236. }
  237. }
  238. public void setIdleMode()
  239. {
  240. removeDead();
  241. for (L2ControllableMobInstance mobInst : getMobs())
  242. {
  243. if (mobInst == null)
  244. {
  245. continue;
  246. }
  247. L2ControllableMobAI ai = (L2ControllableMobAI) mobInst.getAI();
  248. ai.stop();
  249. }
  250. }
  251. public void returnGroup(L2Character activeChar)
  252. {
  253. setIdleMode();
  254. for (L2ControllableMobInstance mobInst : getMobs())
  255. {
  256. if (mobInst == null)
  257. {
  258. continue;
  259. }
  260. int signX = (Rnd.nextInt(2) == 0) ? -1 : 1;
  261. int signY = (Rnd.nextInt(2) == 0) ? -1 : 1;
  262. int randX = Rnd.nextInt(MobGroupTable.RANDOM_RANGE);
  263. int randY = Rnd.nextInt(MobGroupTable.RANDOM_RANGE);
  264. L2ControllableMobAI ai = (L2ControllableMobAI) mobInst.getAI();
  265. ai.move(activeChar.getX() + (signX * randX), activeChar.getY() + (signY * randY), activeChar.getZ());
  266. }
  267. }
  268. public void setFollowMode(L2Character character)
  269. {
  270. removeDead();
  271. for (L2ControllableMobInstance mobInst : getMobs())
  272. {
  273. if (mobInst == null)
  274. {
  275. continue;
  276. }
  277. L2ControllableMobAI ai = (L2ControllableMobAI) mobInst.getAI();
  278. ai.follow(character);
  279. }
  280. }
  281. public void setCastMode()
  282. {
  283. removeDead();
  284. for (L2ControllableMobInstance mobInst : getMobs())
  285. {
  286. if (mobInst == null)
  287. {
  288. continue;
  289. }
  290. L2ControllableMobAI ai = (L2ControllableMobAI) mobInst.getAI();
  291. ai.setAlternateAI(L2ControllableMobAI.AI_CAST);
  292. }
  293. }
  294. public void setNoMoveMode(boolean enabled)
  295. {
  296. removeDead();
  297. for (L2ControllableMobInstance mobInst : getMobs())
  298. {
  299. if (mobInst == null)
  300. {
  301. continue;
  302. }
  303. L2ControllableMobAI ai = (L2ControllableMobAI) mobInst.getAI();
  304. ai.setNotMoving(enabled);
  305. }
  306. }
  307. protected void removeDead()
  308. {
  309. List<L2ControllableMobInstance> deadMobs = new FastList<>();
  310. for (L2ControllableMobInstance mobInst : getMobs())
  311. {
  312. if ((mobInst != null) && mobInst.isDead())
  313. {
  314. deadMobs.add(mobInst);
  315. }
  316. }
  317. getMobs().removeAll(deadMobs);
  318. }
  319. public void setInvul(boolean invulState)
  320. {
  321. removeDead();
  322. for (L2ControllableMobInstance mobInst : getMobs())
  323. {
  324. if (mobInst != null)
  325. {
  326. mobInst.setInvul(invulState);
  327. }
  328. }
  329. }
  330. public void setAttackGroup(MobGroup otherGrp)
  331. {
  332. removeDead();
  333. for (L2ControllableMobInstance mobInst : getMobs())
  334. {
  335. if (mobInst == null)
  336. {
  337. continue;
  338. }
  339. L2ControllableMobAI ai = (L2ControllableMobAI) mobInst.getAI();
  340. ai.forceAttackGroup(otherGrp);
  341. ai.setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  342. }
  343. }
  344. }