L2Spawn.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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.lang.reflect.Constructor;
  17. import java.util.List;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastList;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.GeoData;
  23. import com.l2jserver.gameserver.Territory;
  24. import com.l2jserver.gameserver.ThreadPoolManager;
  25. import com.l2jserver.gameserver.idfactory.IdFactory;
  26. import com.l2jserver.gameserver.model.actor.L2Attackable;
  27. import com.l2jserver.gameserver.model.actor.L2Character;
  28. import com.l2jserver.gameserver.model.actor.L2Npc;
  29. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  30. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  31. import com.l2jserver.util.Rnd;
  32. /**
  33. * This class manages the spawn and respawn of a group of L2NpcInstance that are in the same are and have the same type.
  34. *
  35. * <B><U> Concept</U> :</B><BR><BR>
  36. * L2NpcInstance can be spawned either in a random position into a location area (if Lox=0 and Locy=0), either at an exact position.
  37. * The heading of the L2NpcInstance can be a random heading if not defined (value= -1) or an exact heading (ex : merchant...).<BR><BR>
  38. *
  39. * @author Nightmare
  40. * @version $Revision: 1.9.2.3.2.8 $ $Date: 2005/03/27 15:29:32 $
  41. */
  42. public class L2Spawn
  43. {
  44. protected static final Logger _log = Logger.getLogger(L2Spawn.class.getName());
  45. /** The link on the L2NpcTemplate object containing generic and static properties of this spawn (ex : RewardExp, RewardSP, AggroRange...) */
  46. private L2NpcTemplate _template;
  47. // private String _location = DEFAULT_LOCATION;
  48. /** The identifier of the location area where L2NpcInstance can be spwaned */
  49. private int _location;
  50. /** The maximum number of L2NpcInstance that can manage this L2Spawn */
  51. private int _maximumCount;
  52. /** The current number of L2NpcInstance managed by this L2Spawn */
  53. private int _currentCount;
  54. /** The current number of SpawnTask in progress or stand by of this L2Spawn */
  55. protected int _scheduledCount;
  56. /** The X position of the spwan point */
  57. private int _locX;
  58. /** The Y position of the spwan point */
  59. private int _locY;
  60. /** The Z position of the spwan point */
  61. private int _locZ;
  62. /** The heading of L2NpcInstance when they are spawned */
  63. private int _heading;
  64. /** The delay between a L2NpcInstance remove and its re-spawn */
  65. private int _respawnDelay;
  66. /** Minimum delay RaidBoss */
  67. private int _respawnMinDelay;
  68. /** Maximum delay RaidBoss */
  69. private int _respawnMaxDelay;
  70. private int _instanceId = 0;
  71. /** The generic constructor of L2NpcInstance managed by this L2Spawn */
  72. private Constructor<?> _constructor;
  73. /** If True a L2NpcInstance is respawned each time that another is killed */
  74. private boolean _doRespawn;
  75. /** If true then spawn is custom */
  76. private boolean _customSpawn;
  77. private L2Npc _lastSpawn;
  78. private static List<SpawnListener> _spawnListeners = new FastList<SpawnListener>();
  79. /** The task launching the function doSpawn() */
  80. class SpawnTask implements Runnable
  81. {
  82. //L2NpcInstance _instance;
  83. //int _objId;
  84. private L2Npc _oldNpc;
  85. public SpawnTask(/*int objid*/L2Npc pOldNpc)
  86. {
  87. //_objId= objid;
  88. _oldNpc = pOldNpc;
  89. }
  90. @Override
  91. public void run()
  92. {
  93. try
  94. {
  95. //doSpawn();
  96. respawnNpc(_oldNpc);
  97. }
  98. catch (Exception e)
  99. {
  100. _log.log(Level.WARNING, "", e);
  101. }
  102. _scheduledCount--;
  103. }
  104. }
  105. /**
  106. * Constructor of L2Spawn.<BR><BR>
  107. *
  108. * <B><U> Concept</U> :</B><BR><BR>
  109. * Each L2Spawn owns generic and static properties (ex : RewardExp, RewardSP, AggroRange...).
  110. * All of those properties are stored in a different L2NpcTemplate for each type of L2Spawn.
  111. * Each template is loaded once in the server cache memory (reduce memory use).
  112. * When a new instance of L2Spawn is created, server just create a link between the instance and the template.
  113. * This link is stored in <B>_template</B><BR><BR>
  114. *
  115. * Each L2NpcInstance is linked to a L2Spawn that manages its spawn and respawn (delay, location...).
  116. * This link is stored in <B>_spawn</B> of the L2NpcInstance<BR><BR>
  117. *
  118. * <B><U> Actions</U> :</B><BR><BR>
  119. * <li>Set the _template of the L2Spawn </li>
  120. * <li>Calculate the implementationName used to generate the generic constructor of L2NpcInstance managed by this L2Spawn</li>
  121. * <li>Create the generic constructor of L2NpcInstance managed by this L2Spawn</li><BR><BR>
  122. *
  123. * @param mobTemplate The L2NpcTemplate to link to this L2Spawn
  124. * @throws SecurityException
  125. * @throws ClassNotFoundException
  126. * @throws NoSuchMethodException
  127. */
  128. public L2Spawn(L2NpcTemplate mobTemplate) throws SecurityException, ClassNotFoundException, NoSuchMethodException
  129. {
  130. // Set the _template of the L2Spawn
  131. _template = mobTemplate;
  132. if (_template == null)
  133. return;
  134. // Create the generic constructor of L2NpcInstance managed by this L2Spawn
  135. Class<?>[] parameters = {int.class, Class.forName("com.l2jserver.gameserver.templates.chars.L2NpcTemplate")};
  136. _constructor = Class.forName("com.l2jserver.gameserver.model.actor.instance." + _template.getType() + "Instance").getConstructor(parameters);
  137. }
  138. /**
  139. * @return the maximum number of L2NpcInstance that this L2Spawn can manage.
  140. */
  141. public int getAmount()
  142. {
  143. return _maximumCount;
  144. }
  145. /**
  146. * @return the Identifier of the location area where L2NpcInstance can be spwaned.
  147. */
  148. public int getLocation()
  149. {
  150. return _location;
  151. }
  152. /**
  153. * @return the X position of the spwan point.
  154. */
  155. public int getLocx()
  156. {
  157. return _locX;
  158. }
  159. /**
  160. * @return the Y position of the spwan point.
  161. */
  162. public int getLocy()
  163. {
  164. return _locY;
  165. }
  166. /**
  167. * @return the Z position of the spwan point.
  168. */
  169. public int getLocz()
  170. {
  171. return _locZ;
  172. }
  173. /**
  174. * @return the Identifier of the L2NpcInstance manage by this L2Spwan contained in the L2NpcTemplate.
  175. */
  176. public int getNpcid()
  177. {
  178. return _template.getNpcId();
  179. }
  180. /**
  181. * @return the heading of L2NpcInstance when they are spawned.
  182. */
  183. public int getHeading()
  184. {
  185. return _heading;
  186. }
  187. /**
  188. * @return the delay between a L2NpcInstance remove and its re-spawn.
  189. */
  190. public int getRespawnDelay()
  191. {
  192. return _respawnDelay;
  193. }
  194. /**
  195. * @return Min RaidBoss Spawn delay.
  196. */
  197. public int getRespawnMinDelay()
  198. {
  199. return _respawnMinDelay;
  200. }
  201. /**
  202. * @return Max RaidBoss Spawn delay.
  203. */
  204. public int getRespawnMaxDelay()
  205. {
  206. return _respawnMaxDelay;
  207. }
  208. /**
  209. * Set the maximum number of L2NpcInstance that this L2Spawn can manage.<BR><BR>
  210. * @param amount
  211. */
  212. public void setAmount(int amount)
  213. {
  214. _maximumCount = amount;
  215. }
  216. /**
  217. * Set the Identifier of the location area where L2NpcInstance can be spwaned.<BR><BR>
  218. * @param location
  219. */
  220. public void setLocation(int location)
  221. {
  222. _location = location;
  223. }
  224. /**
  225. * Set Minimum Respawn Delay.<BR><BR>
  226. * @param date
  227. */
  228. public void setRespawnMinDelay(int date)
  229. {
  230. _respawnMinDelay = date;
  231. }
  232. /**
  233. * Set Maximum Respawn Delay.<BR><BR>
  234. * @param date
  235. */
  236. public void setRespawnMaxDelay(int date)
  237. {
  238. _respawnMaxDelay = date;
  239. }
  240. /**
  241. * Set the X position of the spwan point.<BR><BR>
  242. * @param locx
  243. */
  244. public void setLocx(int locx)
  245. {
  246. _locX = locx;
  247. }
  248. /**
  249. * Set the Y position of the spwan point.<BR><BR>
  250. * @param locy
  251. */
  252. public void setLocy(int locy)
  253. {
  254. _locY = locy;
  255. }
  256. /**
  257. * Set the Z position of the spwan point.<BR><BR>
  258. * @param locz
  259. */
  260. public void setLocz(int locz)
  261. {
  262. _locZ = locz;
  263. }
  264. /**
  265. * Set the heading of L2NpcInstance when they are spawned.<BR><BR>
  266. * @param heading
  267. */
  268. public void setHeading(int heading)
  269. {
  270. _heading = heading;
  271. }
  272. /**
  273. * Set the spawn as custom.<BR>
  274. * @param custom
  275. */
  276. public void setCustom(boolean custom)
  277. {
  278. _customSpawn = custom;
  279. }
  280. /**
  281. * @return type of spawn.
  282. */
  283. public boolean isCustom()
  284. {
  285. return _customSpawn;
  286. }
  287. /**
  288. * Decrease the current number of L2NpcInstance of this L2Spawn and if necessary create a SpawnTask to launch after the respawn Delay.<BR><BR>
  289. *
  290. * <B><U> Actions</U> :</B><BR><BR>
  291. * <li>Decrease the current number of L2NpcInstance of this L2Spawn </li>
  292. * <li>Check if respawn is possible to prevent multiple respawning caused by lag </li>
  293. * <li>Update the current number of SpawnTask in progress or stand by of this L2Spawn </li>
  294. * <li>Create a new SpawnTask to launch after the respawn Delay </li><BR><BR>
  295. *
  296. * <FONT COLOR=#FF0000><B> <U>Caution</U> : A respawn is possible ONLY if _doRespawn=True and _scheduledCount + _currentCount < _maximumCount</B></FONT><BR><BR>
  297. * @param oldNpc
  298. */
  299. public void decreaseCount(/*int npcId*/L2Npc oldNpc)
  300. {
  301. // sanity check
  302. if (_currentCount <= 0)
  303. return;
  304. // Decrease the current number of L2NpcInstance of this L2Spawn
  305. _currentCount--;
  306. // Check if respawn is possible to prevent multiple respawning caused by lag
  307. if (_doRespawn && _scheduledCount + _currentCount < _maximumCount )
  308. {
  309. // Update the current number of SpawnTask in progress or stand by of this L2Spawn
  310. _scheduledCount++;
  311. // Create a new SpawnTask to launch after the respawn Delay
  312. //ClientScheduler.getInstance().scheduleLow(new SpawnTask(npcId), _respawnDelay);
  313. ThreadPoolManager.getInstance().scheduleGeneral(new SpawnTask(oldNpc), _respawnDelay);
  314. }
  315. }
  316. /**
  317. * Create the initial spawning and set _doRespawn to True.<BR><BR>
  318. *
  319. * @return The number of L2NpcInstance that were spawned
  320. */
  321. public int init()
  322. {
  323. while (_currentCount < _maximumCount)
  324. {
  325. doSpawn();
  326. }
  327. _doRespawn = true;
  328. return _currentCount;
  329. }
  330. /**
  331. * Create a L2NpcInstance in this L2Spawn.<BR><BR>
  332. * @param val
  333. * @return
  334. */
  335. public L2Npc spawnOne(boolean val)
  336. {
  337. return doSpawn(val);
  338. }
  339. /**
  340. * @return true if respawn enabled
  341. */
  342. public boolean isRespawnEnabled()
  343. {
  344. return _doRespawn;
  345. }
  346. /**
  347. * Set _doRespawn to False to stop respawn in this L2Spawn.<BR><BR>
  348. */
  349. public void stopRespawn()
  350. {
  351. _doRespawn = false;
  352. }
  353. /**
  354. * Set _doRespawn to True to start or restart respawn in this L2Spawn.<BR><BR>
  355. */
  356. public void startRespawn()
  357. {
  358. _doRespawn = true;
  359. }
  360. public L2Npc doSpawn()
  361. {
  362. return doSpawn(false);
  363. }
  364. /**
  365. * Create the L2NpcInstance, add it to the world and lauch its OnSpawn action.<BR><BR>
  366. *
  367. * <B><U> Concept</U> :</B><BR><BR>
  368. * L2NpcInstance can be spawned either in a random position into a location area (if Lox=0 and Locy=0), either at an exact position.
  369. * The heading of the L2NpcInstance can be a random heading if not defined (value= -1) or an exact heading (ex : merchant...).<BR><BR>
  370. *
  371. * <B><U> Actions for an random spawn into location area</U> : <I>(if Locx=0 and Locy=0)</I></B><BR><BR>
  372. * <li>Get L2NpcInstance Init parameters and its generate an Identifier </li>
  373. * <li>Call the constructor of the L2NpcInstance </li>
  374. * <li>Calculate the random position in the location area (if Locx=0 and Locy=0) or get its exact position from the L2Spawn </li>
  375. * <li>Set the position of the L2NpcInstance </li>
  376. * <li>Set the HP and MP of the L2NpcInstance to the max </li>
  377. * <li>Set the heading of the L2NpcInstance (random heading if not defined : value=-1) </li>
  378. * <li>Link the L2NpcInstance to this L2Spawn </li>
  379. * <li>Init other values of the L2NpcInstance (ex : from its L2CharTemplate for INT, STR, DEX...) and add it in the world </li>
  380. * <li>Lauch the action OnSpawn fo the L2NpcInstance </li><BR><BR>
  381. * <li>Increase the current number of L2NpcInstance managed by this L2Spawn </li><BR><BR>
  382. * @param isSummonSpawn
  383. * @return
  384. */
  385. public L2Npc doSpawn(boolean isSummonSpawn)
  386. {
  387. L2Npc mob = null;
  388. try
  389. {
  390. // Check if the L2Spawn is not a L2Pet or L2Minion or L2Decoy spawn
  391. if (_template.isType("L2Pet") || _template.isType("L2Decoy") || _template.isType("L2Trap") || _template.isType("L2EffectPoint"))
  392. {
  393. _currentCount++;
  394. return mob;
  395. }
  396. // Get L2NpcInstance Init parameters and its generate an Identifier
  397. Object[] parameters = {IdFactory.getInstance().getNextId(), _template};
  398. // Call the constructor of the L2NpcInstance
  399. // (can be a L2ArtefactInstance, L2FriendlyMobInstance, L2GuardInstance, L2MonsterInstance, L2SiegeGuardInstance, L2BoxInstance,
  400. // L2FeedableBeastInstance, L2TamedBeastInstance, L2FolkInstance or L2TvTEventNpcInstance)
  401. Object tmp = _constructor.newInstance(parameters);
  402. ((L2Object)tmp).setInstanceId(_instanceId); // Must be done before object is spawned into visible world
  403. if (isSummonSpawn && tmp instanceof L2Character)
  404. ((L2Character)tmp).setShowSummonAnimation(isSummonSpawn);
  405. // Check if the Instance is a L2NpcInstance
  406. if (!(tmp instanceof L2Npc))
  407. return mob;
  408. mob = (L2Npc)tmp;
  409. return initializeNpcInstance(mob);
  410. }
  411. catch (Exception e)
  412. {
  413. _log.log(Level.WARNING, "NPC " + _template.getNpcId() + " class not found", e);
  414. }
  415. return mob;
  416. }
  417. /**
  418. * @param mob
  419. * @return
  420. */
  421. private L2Npc initializeNpcInstance(L2Npc mob)
  422. {
  423. int newlocx, newlocy, newlocz;
  424. // If Locx=0 and Locy=0, the L2NpcInstance must be spawned in an area defined by location
  425. if (getLocx()==0 && getLocy()==0)
  426. {
  427. if (getLocation()==0)
  428. return mob;
  429. // Calculate the random position in the location area
  430. int p[] = Territory.getInstance().getRandomPoint(getLocation());
  431. // Set the calculated position of the L2NpcInstance
  432. newlocx = p[0];
  433. newlocy = p[1];
  434. newlocz = GeoData.getInstance().getSpawnHeight(newlocx, newlocy, p[2], p[3],this);
  435. }
  436. else
  437. {
  438. // The L2NpcInstance is spawned at the exact position (Lox, Locy, Locz)
  439. newlocx = getLocx();
  440. newlocy = getLocy();
  441. if (Config.GEODATA > 0)
  442. newlocz = GeoData.getInstance().getSpawnHeight(newlocx,newlocy,getLocz(),getLocz(),this);
  443. else newlocz = getLocz();
  444. }
  445. mob.stopAllEffects();
  446. mob.setIsDead(false);
  447. // Reset decay info
  448. mob.setDecayed(false);
  449. // Set the HP and MP of the L2NpcInstance to the max
  450. mob.setCurrentHpMp(mob.getMaxHp(), mob.getMaxMp());
  451. // Set the heading of the L2NpcInstance (random heading if not defined)
  452. if (getHeading() == -1)
  453. {
  454. mob.setHeading(Rnd.nextInt(61794));
  455. }
  456. else
  457. {
  458. mob.setHeading(getHeading());
  459. }
  460. if (mob instanceof L2Attackable)
  461. {
  462. ((L2Attackable) mob).setChampion(false);
  463. }
  464. if (Config.L2JMOD_CHAMPION_ENABLE)
  465. {
  466. // Set champion on next spawn
  467. if
  468. (
  469. mob instanceof L2MonsterInstance
  470. && !getTemplate().isQuestMonster()
  471. && !mob.isRaid()
  472. && !((L2MonsterInstance)mob).isRaidMinion()
  473. && Config.L2JMOD_CHAMPION_FREQUENCY > 0
  474. && mob.getLevel()>=Config.L2JMOD_CHAMP_MIN_LVL
  475. && mob.getLevel()<=Config.L2JMOD_CHAMP_MAX_LVL
  476. && (Config.L2JMOD_CHAMPION_ENABLE_IN_INSTANCES || getInstanceId() == 0)
  477. )
  478. {
  479. int random = Rnd.get(100);
  480. if (random < Config.L2JMOD_CHAMPION_FREQUENCY)
  481. ((L2Attackable) mob).setChampion(true);
  482. }
  483. }
  484. // Link the L2NpcInstance to this L2Spawn
  485. mob.setSpawn(this);
  486. // Init other values of the L2NpcInstance (ex : from its L2CharTemplate for INT, STR, DEX...) and add it in the world as a visible object
  487. mob.spawnMe(newlocx, newlocy, newlocz);
  488. L2Spawn.notifyNpcSpawned(mob);
  489. _lastSpawn = mob;
  490. if (Config.DEBUG)
  491. {
  492. _log.finest("Spawned Mob Id: " + _template.getNpcId() + " , at: X: " + mob.getX() + " Y: " + mob.getY() + " Z: " + mob.getZ());
  493. }
  494. // Increase the current number of L2NpcInstance managed by this L2Spawn
  495. _currentCount++;
  496. return mob;
  497. }
  498. public static void addSpawnListener(SpawnListener listener)
  499. {
  500. synchronized (_spawnListeners)
  501. {
  502. _spawnListeners.add(listener);
  503. }
  504. }
  505. public static void removeSpawnListener(SpawnListener listener)
  506. {
  507. synchronized (_spawnListeners)
  508. {
  509. _spawnListeners.remove(listener);
  510. }
  511. }
  512. public static void notifyNpcSpawned(L2Npc npc)
  513. {
  514. synchronized (_spawnListeners)
  515. {
  516. for (SpawnListener listener : _spawnListeners)
  517. {
  518. listener.npcSpawned(npc);
  519. }
  520. }
  521. }
  522. /**
  523. * @param i delay in seconds
  524. */
  525. public void setRespawnDelay(int i)
  526. {
  527. if (i < 0)
  528. _log.warning("respawn delay is negative for spawn:"+this);
  529. if (i < 10)
  530. i = 10;
  531. _respawnDelay = i * 1000;
  532. }
  533. public L2Npc getLastSpawn()
  534. {
  535. return _lastSpawn;
  536. }
  537. /**
  538. * @param oldNpc
  539. */
  540. public void respawnNpc(L2Npc oldNpc)
  541. {
  542. if (_doRespawn)
  543. {
  544. oldNpc.refreshID();
  545. /*L2NpcInstance instance = */initializeNpcInstance(oldNpc);
  546. }
  547. }
  548. public L2NpcTemplate getTemplate()
  549. {
  550. return _template;
  551. }
  552. public int getInstanceId()
  553. {
  554. return _instanceId;
  555. }
  556. public void setInstanceId(int instanceId)
  557. {
  558. _instanceId = instanceId;
  559. }
  560. /* (non-Javadoc)
  561. * @see java.lang.Object#toString()
  562. */
  563. @Override
  564. public String toString()
  565. {
  566. return "L2Spawn [_template=" + getNpcid() + ", _locX=" + _locX + ", _locY=" + _locY + ", _locZ=" + _locZ + ", _heading=" + _heading + "]";
  567. }
  568. }