MobGroup.java 8.3 KB

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