MinionList.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * $Header: MinionList.java, 25/10/2005 18:42:48 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $
  5. * $Date: 25/10/2005 18:42:48 $
  6. * $Revision: 1 $
  7. * $Log: MinionList.java,v $
  8. * Revision 1 25/10/2005 18:42:48 luisantonioa
  9. * Added copyright notice
  10. *
  11. *
  12. * This program is free software: you can redistribute it and/or modify it under
  13. * the terms of the GNU General Public License as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option) any later
  15. * version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. package com.l2jserver.gameserver.util;
  26. import java.util.List;
  27. import java.util.Set;
  28. import java.util.logging.Logger;
  29. import com.l2jserver.Config;
  30. import com.l2jserver.gameserver.datatables.NpcTable;
  31. import com.l2jserver.gameserver.idfactory.IdFactory;
  32. import com.l2jserver.gameserver.model.L2MinionData;
  33. import com.l2jserver.gameserver.model.actor.instance.L2MinionInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  35. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  36. import com.l2jserver.util.Rnd;
  37. import javolution.util.FastList;
  38. import javolution.util.FastMap;
  39. import javolution.util.FastSet;
  40. /**
  41. * This class ...
  42. *
  43. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  44. */
  45. public class MinionList
  46. {
  47. private static Logger _log = Logger.getLogger(L2MonsterInstance.class.getName());
  48. /** List containing the current spawned minions for this L2MonsterInstance */
  49. private final List<L2MinionInstance> minionReferences;
  50. protected FastMap<Long, Integer> _respawnTasks = new FastMap<Long, Integer>().shared();
  51. private final L2MonsterInstance master;
  52. public MinionList(L2MonsterInstance pMaster)
  53. {
  54. minionReferences = new FastList<L2MinionInstance>();
  55. master = pMaster;
  56. }
  57. public int countSpawnedMinions()
  58. {
  59. synchronized (minionReferences)
  60. {
  61. return minionReferences.size();
  62. }
  63. }
  64. public int countSpawnedMinionsById(int minionId)
  65. {
  66. int count = 0;
  67. synchronized (minionReferences)
  68. {
  69. for (L2MinionInstance minion : getSpawnedMinions())
  70. {
  71. if (minion.getNpcId() == minionId)
  72. {
  73. count++;
  74. }
  75. }
  76. }
  77. return count;
  78. }
  79. public boolean hasMinions()
  80. {
  81. return !getSpawnedMinions().isEmpty();
  82. }
  83. public List<L2MinionInstance> getSpawnedMinions()
  84. {
  85. return minionReferences;
  86. }
  87. public void addSpawnedMinion(L2MinionInstance minion)
  88. {
  89. synchronized (minionReferences)
  90. {
  91. minionReferences.add(minion);
  92. }
  93. }
  94. public int lazyCountSpawnedMinionsGroups()
  95. {
  96. Set<Integer> seenGroups = new FastSet<Integer>();
  97. for (L2MinionInstance minion : getSpawnedMinions())
  98. {
  99. if (minion == null)
  100. continue;
  101. seenGroups.add(minion.getNpcId());
  102. }
  103. return seenGroups.size();
  104. }
  105. public void removeSpawnedMinion(L2MinionInstance minion)
  106. {
  107. synchronized (minionReferences)
  108. {
  109. minionReferences.remove(minion);
  110. }
  111. }
  112. public void moveMinionToRespawnList(L2MinionInstance minion)
  113. {
  114. Long current = System.currentTimeMillis();
  115. synchronized (minionReferences)
  116. {
  117. minionReferences.remove(minion);
  118. if (_respawnTasks.get(current) == null)
  119. _respawnTasks.put(current, minion.getNpcId());
  120. else
  121. {
  122. // nice AoE
  123. for (int i = 1; i < 30; i++)
  124. {
  125. if (_respawnTasks.get(current + i) == null)
  126. {
  127. _respawnTasks.put(current + i, minion.getNpcId());
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. public void clearRespawnList()
  135. {
  136. _respawnTasks.clear();
  137. }
  138. /**
  139. * Manage respawning of minions for this RaidBoss.<BR><BR>
  140. */
  141. public void maintainMinions()
  142. {
  143. if (master == null || master.isAlikeDead())
  144. return;
  145. Long current = System.currentTimeMillis();
  146. if (_respawnTasks != null)
  147. for (long deathTime : _respawnTasks.keySet())
  148. {
  149. double delay = Config.RAID_MINION_RESPAWN_TIMER;
  150. if ((current - deathTime) > delay)
  151. {
  152. spawnSingleMinion(_respawnTasks.get(deathTime), master.getInstanceId());
  153. _respawnTasks.remove(deathTime);
  154. }
  155. }
  156. }
  157. /**
  158. * Manage the spawn of all Minions of this RaidBoss.<BR><BR>
  159. *
  160. * <B><U> Actions</U> :</B><BR><BR>
  161. * <li>Get the Minion data of all Minions that must be spawn </li>
  162. * <li>For each Minion type, spawn the amount of Minion needed </li><BR><BR>
  163. *
  164. * @param player The L2PcInstance to attack
  165. *
  166. */
  167. public void spawnMinions()
  168. {
  169. if (master == null || master.isAlikeDead())
  170. return;
  171. List<L2MinionData> minions = master.getTemplate().getMinionData();
  172. if (minions == null)
  173. return;
  174. synchronized (minionReferences)
  175. {
  176. int minionCount, minionId, minionsToSpawn;
  177. for (L2MinionData minion : minions)
  178. {
  179. minionCount = minion.getAmount();
  180. minionId = minion.getMinionId();
  181. minionsToSpawn = minionCount - countSpawnedMinionsById(minionId);
  182. for (int i = 0; i < minionsToSpawn; i++)
  183. {
  184. spawnSingleMinion(minionId, master.getInstanceId());
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Init a Minion and add it in the world as a visible object.<BR><BR>
  191. *
  192. * <B><U> Actions</U> :</B><BR><BR>
  193. * <li>Get the template of the Minion to spawn </li>
  194. * <li>Create and Init the Minion and generate its Identifier </li>
  195. * <li>Set the Minion HP, MP and Heading </li>
  196. * <li>Set the Minion leader to this RaidBoss </li>
  197. * <li>Init the position of the Minion and add it in the world as a visible object </li><BR><BR>
  198. *
  199. * @param minionid The I2NpcTemplate Identifier of the Minion to spawn
  200. *
  201. */
  202. public void spawnSingleMinion(int minionId, int instanceId)
  203. {
  204. // Get the template of the Minion to spawn
  205. L2NpcTemplate minionTemplate = NpcTable.getInstance().getTemplate(minionId);
  206. // Create and Init the Minion and generate its Identifier
  207. L2MinionInstance monster = new L2MinionInstance(IdFactory.getInstance().getNextId(), minionTemplate);
  208. // Set the Minion HP, MP and Heading
  209. monster.setCurrentHpMp(monster.getMaxHp(), monster.getMaxMp());
  210. monster.setHeading(master.getHeading());
  211. // Set the Minion leader to this RaidBoss
  212. monster.setLeader(master);
  213. //move monster to masters instance
  214. monster.setInstanceId(instanceId);
  215. // Init the position of the Minion and add it in the world as a visible object
  216. final int offset = 200;
  217. final int minRadius = 30;
  218. int newX = Rnd.get(minRadius * 2, offset * 2); // x
  219. int newY = Rnd.get(newX, offset * 2); // distance
  220. newY = (int)Math.sqrt(newY*newY - newX*newX); // y
  221. if (newX > offset + minRadius)
  222. newX = master.getX() + newX - offset;
  223. else
  224. newX = master.getX() - newX + minRadius;
  225. if (newY > offset + minRadius)
  226. newY = master.getY() + newY - offset;
  227. else
  228. newY = master.getY() - newY + minRadius;
  229. monster.spawnMe(newX, newY, master.getZ());
  230. if (Config.DEBUG)
  231. _log.fine("Spawned minion template " + minionTemplate.npcId + " with objid: " + monster.getObjectId() + " to boss " + master.getObjectId() + " ,at: " + monster.getX() + " x, " + monster.getY() + " y, " + monster.getZ() + " z");
  232. }
  233. }