MobGroup.java 8.7 KB

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