L2Spawn.java 18 KB

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