MinionList.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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.util;
  20. import java.util.List;
  21. import java.util.concurrent.CopyOnWriteArrayList;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.ThreadPoolManager;
  25. import com.l2jserver.gameserver.data.xml.impl.NpcData;
  26. import com.l2jserver.gameserver.model.Location;
  27. import com.l2jserver.gameserver.model.actor.L2Character;
  28. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  29. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  30. import com.l2jserver.gameserver.model.holders.MinionHolder;
  31. import com.l2jserver.util.Rnd;
  32. /**
  33. * @author luisantonioa, DS
  34. */
  35. public class MinionList
  36. {
  37. private static final Logger _log = Logger.getLogger(MinionList.class.getName());
  38. protected final L2MonsterInstance _master;
  39. /** List containing the current spawned minions */
  40. private final List<L2MonsterInstance> _minionReferences = new CopyOnWriteArrayList<>();
  41. /** List containing the cached deleted minions for reuse */
  42. protected List<L2MonsterInstance> _reusedMinionReferences = null;
  43. public MinionList(L2MonsterInstance pMaster)
  44. {
  45. if (pMaster == null)
  46. {
  47. throw new NullPointerException("MinionList: master is null");
  48. }
  49. _master = pMaster;
  50. }
  51. /**
  52. * @return list of the spawned (alive) minions.
  53. */
  54. public List<L2MonsterInstance> getSpawnedMinions()
  55. {
  56. return _minionReferences;
  57. }
  58. /**
  59. * Manage the spawn of Minions.<BR>
  60. * <BR>
  61. * <B><U> Actions</U> :</B><BR>
  62. * <BR>
  63. * <li>Get the Minion data of all Minions that must be spawn</li> <li>For each Minion type, spawn the amount of Minion needed</li><BR>
  64. * <BR>
  65. * @param minions
  66. */
  67. public final void spawnMinions(final List<MinionHolder> minions)
  68. {
  69. if (_master.isAlikeDead())
  70. {
  71. return;
  72. }
  73. // List<MinionHolder> minions = _master.getTemplate().getParameters().getMinionList("Privates");
  74. if (minions == null)
  75. {
  76. return;
  77. }
  78. int minionCount, minionId;
  79. long minionsToSpawn;
  80. for (MinionHolder minion : minions)
  81. {
  82. minionCount = minion.getCount();
  83. minionId = minion.getId();
  84. minionsToSpawn = minionCount - countSpawnedMinionsById(minionId);
  85. if (minionsToSpawn > 0)
  86. {
  87. for (int i = 0; i < minionsToSpawn; i++)
  88. {
  89. spawnMinion(minionId);
  90. }
  91. }
  92. }
  93. // remove non-needed minions
  94. deleteReusedMinions();
  95. }
  96. /**
  97. * Delete all spawned minions and try to reuse them.
  98. */
  99. public void deleteSpawnedMinions()
  100. {
  101. if (!_minionReferences.isEmpty())
  102. {
  103. for (L2MonsterInstance minion : _minionReferences)
  104. {
  105. if (minion != null)
  106. {
  107. minion.setLeader(null);
  108. minion.deleteMe();
  109. if (_reusedMinionReferences != null)
  110. {
  111. _reusedMinionReferences.add(minion);
  112. }
  113. }
  114. }
  115. _minionReferences.clear();
  116. }
  117. }
  118. /**
  119. * Delete all reused minions to prevent memory leaks.
  120. */
  121. public void deleteReusedMinions()
  122. {
  123. if (_reusedMinionReferences != null)
  124. {
  125. _reusedMinionReferences.clear();
  126. }
  127. }
  128. // hooks
  129. /**
  130. * Called on the master spawn Old minions (from previous spawn) are deleted. If master can respawn - enabled reuse of the killed minions.
  131. */
  132. public void onMasterSpawn()
  133. {
  134. deleteSpawnedMinions();
  135. // if master has spawn and can respawn - try to reuse minions
  136. if ((_reusedMinionReferences == null) && (_master.getTemplate().getParameters().getSet().get("SummonPrivateRate") == null) && !_master.getTemplate().getParameters().getMinionList("Privates").isEmpty() && (_master.getSpawn() != null) && _master.getSpawn().isRespawnEnabled())
  137. {
  138. _reusedMinionReferences = new CopyOnWriteArrayList<>();
  139. }
  140. }
  141. /**
  142. * Called on the minion spawn and added them in the list of the spawned minions.
  143. * @param minion
  144. */
  145. public void onMinionSpawn(L2MonsterInstance minion)
  146. {
  147. _minionReferences.add(minion);
  148. }
  149. /**
  150. * Called on the master death/delete.
  151. * @param force if true - force delete of the spawned minions By default minions deleted only for raidbosses
  152. */
  153. public void onMasterDie(boolean force)
  154. {
  155. if (_master.isRaid() || force)
  156. {
  157. deleteSpawnedMinions();
  158. }
  159. }
  160. /**
  161. * Called on the minion death/delete. Removed minion from the list of the spawned minions and reuse if possible.
  162. * @param minion
  163. * @param respawnTime (ms) enable respawning of this minion while master is alive. -1 - use default value: 0 (disable) for mobs and config value for raids.
  164. */
  165. public void onMinionDie(L2MonsterInstance minion, int respawnTime)
  166. {
  167. minion.setLeader(null); // prevent memory leaks
  168. _minionReferences.remove(minion);
  169. if (_reusedMinionReferences != null)
  170. {
  171. _reusedMinionReferences.add(minion);
  172. }
  173. final int time = respawnTime < 0 ? _master.isRaid() ? (int) Config.RAID_MINION_RESPAWN_TIMER : 0 : respawnTime;
  174. if ((time > 0) && !_master.isAlikeDead())
  175. {
  176. ThreadPoolManager.getInstance().scheduleGeneral(new MinionRespawnTask(minion), time);
  177. }
  178. }
  179. /**
  180. * Called if master/minion was attacked. Master and all free minions receive aggro against attacker.
  181. * @param caller
  182. * @param attacker
  183. */
  184. public void onAssist(L2Character caller, L2Character attacker)
  185. {
  186. if (attacker == null)
  187. {
  188. return;
  189. }
  190. if (!_master.isAlikeDead() && !_master.isInCombat())
  191. {
  192. _master.addDamageHate(attacker, 0, 1);
  193. }
  194. final boolean callerIsMaster = caller == _master;
  195. int aggro = callerIsMaster ? 10 : 1;
  196. if (_master.isRaid())
  197. {
  198. aggro *= 10;
  199. }
  200. for (L2MonsterInstance minion : _minionReferences)
  201. {
  202. if ((minion != null) && !minion.isDead() && (callerIsMaster || !minion.isInCombat()))
  203. {
  204. minion.addDamageHate(attacker, 0, aggro);
  205. }
  206. }
  207. }
  208. /**
  209. * Called from onTeleported() of the master Alive and able to move minions teleported to master.
  210. */
  211. public void onMasterTeleported()
  212. {
  213. final int offset = 200;
  214. final int minRadius = (int) _master.getCollisionRadius() + 30;
  215. for (L2MonsterInstance minion : _minionReferences)
  216. {
  217. if ((minion != null) && !minion.isDead() && !minion.isMovementDisabled())
  218. {
  219. int newX = Rnd.get(minRadius * 2, offset * 2); // x
  220. int newY = Rnd.get(newX, offset * 2); // distance
  221. newY = (int) Math.sqrt((newY * newY) - (newX * newX)); // y
  222. if (newX > (offset + minRadius))
  223. {
  224. newX = (_master.getX() + newX) - offset;
  225. }
  226. else
  227. {
  228. newX = (_master.getX() - newX) + minRadius;
  229. }
  230. if (newY > (offset + minRadius))
  231. {
  232. newY = (_master.getY() + newY) - offset;
  233. }
  234. else
  235. {
  236. newY = (_master.getY() - newY) + minRadius;
  237. }
  238. minion.teleToLocation(new Location(newX, newY, _master.getZ()));
  239. }
  240. }
  241. }
  242. private final void spawnMinion(int minionId)
  243. {
  244. if (minionId == 0)
  245. {
  246. return;
  247. }
  248. // searching in reused minions
  249. if (_reusedMinionReferences != null)
  250. {
  251. final L2MonsterInstance minion = _reusedMinionReferences.stream().filter(m -> (m.getId() == minionId)).findFirst().orElse(null);
  252. if (minion != null)
  253. {
  254. _reusedMinionReferences.remove(minion);
  255. minion.refreshID();
  256. initializeNpcInstance(_master, minion);
  257. return;
  258. }
  259. }
  260. // not found in cache
  261. spawnMinion(_master, minionId);
  262. }
  263. private final class MinionRespawnTask implements Runnable
  264. {
  265. private final L2MonsterInstance _minion;
  266. public MinionRespawnTask(L2MonsterInstance minion)
  267. {
  268. _minion = minion;
  269. }
  270. @Override
  271. public void run()
  272. {
  273. if (!_master.isAlikeDead() && _master.isVisible())
  274. {
  275. // minion can be already spawned or deleted
  276. if (!_minion.isVisible())
  277. {
  278. if (_reusedMinionReferences != null)
  279. {
  280. _reusedMinionReferences.remove(_minion);
  281. }
  282. _minion.refreshID();
  283. initializeNpcInstance(_master, _minion);
  284. }
  285. }
  286. }
  287. }
  288. /**
  289. * Init a Minion and add it in the world as a visible object.<BR>
  290. * <BR>
  291. * <B><U> Actions</U> :</B><BR>
  292. * <BR>
  293. * <li>Get the template of the Minion to spawn</li> <li>Create and Init the Minion and generate its Identifier</li> <li>Set the Minion HP, MP and Heading</li> <li>Set the Minion leader to this RaidBoss</li> <li>Init the position of the Minion and add it in the world as a visible object</li><BR>
  294. * <BR>
  295. * @param master L2MonsterInstance used as master for this minion
  296. * @param minionId The L2NpcTemplate Identifier of the Minion to spawn
  297. * @return
  298. */
  299. public static final L2MonsterInstance spawnMinion(L2MonsterInstance master, int minionId)
  300. {
  301. // Get the template of the Minion to spawn
  302. L2NpcTemplate minionTemplate = NpcData.getInstance().getTemplate(minionId);
  303. if (minionTemplate == null)
  304. {
  305. return null;
  306. }
  307. return initializeNpcInstance(master, new L2MonsterInstance(minionTemplate));
  308. }
  309. protected static final L2MonsterInstance initializeNpcInstance(L2MonsterInstance master, L2MonsterInstance minion)
  310. {
  311. minion.stopAllEffects();
  312. minion.setIsDead(false);
  313. minion.setDecayed(false);
  314. // Set the Minion HP, MP and Heading
  315. minion.setCurrentHpMp(minion.getMaxHp(), minion.getMaxMp());
  316. minion.setHeading(master.getHeading());
  317. // Set the Minion leader to this RaidBoss
  318. minion.setLeader(master);
  319. // move monster to masters instance
  320. minion.setInstanceId(master.getInstanceId());
  321. // Init the position of the Minion and add it in the world as a visible object
  322. final int offset = 200;
  323. final int minRadius = (int) master.getCollisionRadius() + 30;
  324. int newX = Rnd.get(minRadius * 2, offset * 2); // x
  325. int newY = Rnd.get(newX, offset * 2); // distance
  326. newY = (int) Math.sqrt((newY * newY) - (newX * newX)); // y
  327. if (newX > (offset + minRadius))
  328. {
  329. newX = (master.getX() + newX) - offset;
  330. }
  331. else
  332. {
  333. newX = (master.getX() - newX) + minRadius;
  334. }
  335. if (newY > (offset + minRadius))
  336. {
  337. newY = (master.getY() + newY) - offset;
  338. }
  339. else
  340. {
  341. newY = (master.getY() - newY) + minRadius;
  342. }
  343. minion.spawnMe(newX, newY, master.getZ());
  344. if (Config.DEBUG)
  345. {
  346. _log.info("Spawned minion template " + minion.getId() + " with objid: " + minion.getObjectId() + " to boss " + master.getObjectId() + " ,at: " + minion.getX() + " x, " + minion.getY() + " y, " + minion.getZ() + " z");
  347. }
  348. return minion;
  349. }
  350. // Statistics part
  351. private final long countSpawnedMinionsById(int minionId)
  352. {
  353. return _minionReferences.stream().filter(npc -> npc.getId() == minionId).count();
  354. }
  355. public final int countSpawnedMinions()
  356. {
  357. return _minionReferences.size();
  358. }
  359. public final long lazyCountSpawnedMinionsGroups()
  360. {
  361. return _minionReferences.stream().map(L2MonsterInstance::getId).distinct().count();
  362. }
  363. }