MobGroup.java 9.6 KB

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