L2Spawn.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. public void setX(int x)
  161. {
  162. _location.setX(x);
  163. }
  164. @Override
  165. public int getY()
  166. {
  167. return _location.getY();
  168. }
  169. /**
  170. * Set the Y position of the spawn point.
  171. * @param y the y coordinate
  172. */
  173. public void setY(int y)
  174. {
  175. _location.setY(y);
  176. }
  177. @Override
  178. public int getZ()
  179. {
  180. return _location.getZ();
  181. }
  182. /**
  183. * Set the Z position of the spawn point.
  184. * @param z the z coordinate
  185. */
  186. public void setZ(int z)
  187. {
  188. _location.setZ(z);
  189. }
  190. /**
  191. * @return the heading of L2NpcInstance when they are spawned.
  192. */
  193. public int getHeading()
  194. {
  195. return _location.getHeading();
  196. }
  197. /**
  198. * Set the heading of L2NpcInstance when they are spawned.
  199. * @param heading
  200. */
  201. public void setHeading(int heading)
  202. {
  203. _location.setHeading(heading);
  204. }
  205. /**
  206. * Set the XYZ position of the spawn point.
  207. * @param loc
  208. */
  209. public void setLocation(Location loc)
  210. {
  211. _location = loc;
  212. }
  213. /**
  214. * Gets the NPC ID.
  215. * @return the NPC ID
  216. */
  217. @Override
  218. public int getId()
  219. {
  220. return _template.getId();
  221. }
  222. /**
  223. * @return min respawn delay.
  224. */
  225. public int getRespawnMinDelay()
  226. {
  227. return _respawnMinDelay;
  228. }
  229. /**
  230. * @return max respawn delay.
  231. */
  232. public int getRespawnMaxDelay()
  233. {
  234. return _respawnMaxDelay;
  235. }
  236. /**
  237. * Set the maximum number of L2NpcInstance that this L2Spawn can manage.
  238. * @param amount
  239. */
  240. public void setAmount(int amount)
  241. {
  242. _maximumCount = amount;
  243. }
  244. /**
  245. * Set the Identifier of the location area where L2NpcInstance can be spawned.
  246. * @param id
  247. */
  248. public void setLocationId(int id)
  249. {
  250. _locationId = id;
  251. }
  252. /**
  253. * Set Minimum Respawn Delay.
  254. * @param date
  255. */
  256. public void setRespawnMinDelay(int date)
  257. {
  258. _respawnMinDelay = date;
  259. }
  260. /**
  261. * Set Maximum Respawn Delay.
  262. * @param date
  263. */
  264. public void setRespawnMaxDelay(int date)
  265. {
  266. _respawnMaxDelay = date;
  267. }
  268. /**
  269. * Set the spawn as custom.<BR>
  270. * @param custom
  271. */
  272. public void setCustom(boolean custom)
  273. {
  274. _customSpawn = custom;
  275. }
  276. /**
  277. * @return type of spawn.
  278. */
  279. public boolean isCustom()
  280. {
  281. return _customSpawn;
  282. }
  283. /**
  284. * 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
  285. * 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 +
  286. * _currentCount < _maximumCount</B></FONT>
  287. * @param oldNpc
  288. */
  289. public void decreaseCount(L2Npc oldNpc)
  290. {
  291. // sanity check
  292. if (_currentCount <= 0)
  293. {
  294. return;
  295. }
  296. // Decrease the current number of L2NpcInstance of this L2Spawn
  297. _currentCount--;
  298. // Check if respawn is possible to prevent multiple respawning caused by lag
  299. if (_doRespawn && ((_scheduledCount + _currentCount) < _maximumCount))
  300. {
  301. // Update the current number of SpawnTask in progress or stand by of this L2Spawn
  302. _scheduledCount++;
  303. // Create a new SpawnTask to launch after the respawn Delay
  304. // ClientScheduler.getInstance().scheduleLow(new SpawnTask(npcId), _respawnDelay);
  305. ThreadPoolManager.getInstance().scheduleGeneral(new SpawnTask(oldNpc), hasRespawnRandom() ? Rnd.get(_respawnMinDelay, _respawnMaxDelay) : _respawnMinDelay);
  306. }
  307. }
  308. /**
  309. * Create the initial spawning and set _doRespawn to False, if respawn time set to 0, or set it to True otherwise.
  310. * @return The number of L2NpcInstance that were spawned
  311. */
  312. public int init()
  313. {
  314. while (_currentCount < _maximumCount)
  315. {
  316. doSpawn();
  317. }
  318. _doRespawn = _respawnMinDelay != 0;
  319. return _currentCount;
  320. }
  321. /**
  322. * Create a L2NpcInstance in this L2Spawn.
  323. * @param val
  324. * @return
  325. */
  326. public L2Npc spawnOne(boolean val)
  327. {
  328. return doSpawn(val);
  329. }
  330. /**
  331. * @return true if respawn enabled
  332. */
  333. public boolean isRespawnEnabled()
  334. {
  335. return _doRespawn;
  336. }
  337. /**
  338. * Set _doRespawn to False to stop respawn in this L2Spawn.
  339. */
  340. public void stopRespawn()
  341. {
  342. _doRespawn = false;
  343. }
  344. /**
  345. * Set _doRespawn to True to start or restart respawn in this L2Spawn.
  346. */
  347. public void startRespawn()
  348. {
  349. _doRespawn = true;
  350. }
  351. public L2Npc doSpawn()
  352. {
  353. return doSpawn(false);
  354. }
  355. /**
  356. * Create the L2NpcInstance, add it to the world and lauch its OnSpawn action.<br>
  357. * <B><U>Concept</U>:</B><br>
  358. * 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>
  359. * The heading of the L2NpcInstance can be a random heading if not defined (value= -1) or an exact heading (ex : merchant...).<br>
  360. * <B><U>Actions for an random spawn into location area</U>:<I> (if Locx=0 and Locy=0)</I></B>
  361. * <ul>
  362. * <li>Get L2NpcInstance Init parameters and its generate an Identifier</li>
  363. * <li>Call the constructor of the L2NpcInstance</li>
  364. * <li>Calculate the random position in the location area (if Locx=0 and Locy=0) or get its exact position from the L2Spawn</li>
  365. * <li>Set the position of the L2NpcInstance</li>
  366. * <li>Set the HP and MP of the L2NpcInstance to the max</li>
  367. * <li>Set the heading of the L2NpcInstance (random heading if not defined : value=-1)</li>
  368. * <li>Link the L2NpcInstance to this L2Spawn</li>
  369. * <li>Init other values of the L2NpcInstance (ex : from its L2CharTemplate for INT, STR, DEX...) and add it in the world</li>
  370. * <li>Launch the action OnSpawn fo the L2NpcInstance</li>
  371. * <li>Increase the current number of L2NpcInstance managed by this L2Spawn</li>
  372. * </ul>
  373. * @param isSummonSpawn
  374. * @return
  375. */
  376. public L2Npc doSpawn(boolean isSummonSpawn)
  377. {
  378. L2Npc mob = null;
  379. try
  380. {
  381. // Check if the L2Spawn is not a L2Pet or L2Minion or L2Decoy spawn
  382. if (_template.isType("L2Pet") || _template.isType("L2Decoy") || _template.isType("L2Trap") || _template.isType("L2EffectPoint"))
  383. {
  384. _currentCount++;
  385. return mob;
  386. }
  387. // Get L2NpcInstance Init parameters and its generate an Identifier
  388. Object[] parameters =
  389. {
  390. IdFactory.getInstance().getNextId(),
  391. _template
  392. };
  393. // Call the constructor of the L2NpcInstance
  394. // (can be a L2ArtefactInstance, L2FriendlyMobInstance, L2GuardInstance, L2MonsterInstance, L2SiegeGuardInstance, L2BoxInstance,
  395. // L2FeedableBeastInstance, L2TamedBeastInstance, L2FolkInstance or L2TvTEventNpcInstance)
  396. Object tmp = _constructor.newInstance(parameters);
  397. ((L2Object) tmp).setInstanceId(_instanceId); // Must be done before object is spawned into visible world
  398. if (isSummonSpawn && (tmp instanceof L2Character))
  399. {
  400. ((L2Character) tmp).setShowSummonAnimation(isSummonSpawn);
  401. }
  402. // Check if the Instance is a L2NpcInstance
  403. if (!(tmp instanceof L2Npc))
  404. {
  405. return mob;
  406. }
  407. mob = (L2Npc) tmp;
  408. return initializeNpcInstance(mob);
  409. }
  410. catch (Exception e)
  411. {
  412. _log.log(Level.WARNING, "NPC " + _template.getId() + " class not found", e);
  413. }
  414. return mob;
  415. }
  416. /**
  417. * @param mob
  418. * @return
  419. */
  420. private L2Npc initializeNpcInstance(L2Npc mob)
  421. {
  422. int newlocx, newlocy, newlocz;
  423. // If Locx=0 and Locy=0, the L2NpcInstance must be spawned in an area defined by location
  424. if ((getX() == 0) && (getY() == 0))
  425. {
  426. if (getLocationId() == 0)
  427. {
  428. return mob;
  429. }
  430. // Calculate the random position in the location area
  431. int p[] = TerritoryTable.getInstance().getRandomPoint(getLocationId());
  432. // Set the calculated position of the L2NpcInstance
  433. newlocx = p[0];
  434. newlocy = p[1];
  435. newlocz = GeoData.getInstance().getSpawnHeight(newlocx, newlocy, p[2], p[3], this);
  436. }
  437. else
  438. {
  439. // The L2NpcInstance is spawned at the exact position (Lox, Locy, Locz)
  440. newlocx = getX();
  441. newlocy = getY();
  442. if (Config.GEODATA > 0)
  443. {
  444. newlocz = GeoData.getInstance().getSpawnHeight(newlocx, newlocy, getZ(), getZ(), this);
  445. }
  446. else
  447. {
  448. newlocz = getZ();
  449. }
  450. }
  451. mob.stopAllEffects();
  452. mob.setIsDead(false);
  453. // Reset decay info
  454. mob.setDecayed(false);
  455. // Set the HP and MP of the L2NpcInstance to the max
  456. mob.setCurrentHpMp(mob.getMaxHp(), mob.getMaxMp());
  457. // Set default value
  458. mob.setScriptValue(0);
  459. // Set is not random walk default value
  460. mob.setIsNoRndWalk(isNoRndWalk());
  461. // Set the heading of the L2NpcInstance (random heading if not defined)
  462. if (getHeading() == -1)
  463. {
  464. mob.setHeading(Rnd.nextInt(61794));
  465. }
  466. else
  467. {
  468. mob.setHeading(getHeading());
  469. }
  470. if (mob instanceof L2Attackable)
  471. {
  472. ((L2Attackable) mob).setChampion(false);
  473. }
  474. if (Config.L2JMOD_CHAMPION_ENABLE)
  475. {
  476. // Set champion on next spawn
  477. 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)))
  478. {
  479. int random = Rnd.get(100);
  480. if (random < Config.L2JMOD_CHAMPION_FREQUENCY)
  481. {
  482. ((L2Attackable) mob).setChampion(true);
  483. }
  484. }
  485. }
  486. // Link the L2NpcInstance to this L2Spawn
  487. mob.setSpawn(this);
  488. // Init other values of the L2NpcInstance (ex : from its L2CharTemplate for INT, STR, DEX...) and add it in the world as a visible object
  489. mob.spawnMe(newlocx, newlocy, newlocz);
  490. L2Spawn.notifyNpcSpawned(mob);
  491. _lastSpawn = mob;
  492. if (Config.DEBUG)
  493. {
  494. _log.finest("Spawned Mob Id: " + _template.getId() + " , at: X: " + mob.getX() + " Y: " + mob.getY() + " Z: " + mob.getZ());
  495. }
  496. // Increase the current number of L2NpcInstance managed by this L2Spawn
  497. _currentCount++;
  498. return mob;
  499. }
  500. public static void addSpawnListener(SpawnListener listener)
  501. {
  502. synchronized (_spawnListeners)
  503. {
  504. _spawnListeners.add(listener);
  505. }
  506. }
  507. public static void removeSpawnListener(SpawnListener listener)
  508. {
  509. synchronized (_spawnListeners)
  510. {
  511. _spawnListeners.remove(listener);
  512. }
  513. }
  514. public static void notifyNpcSpawned(L2Npc npc)
  515. {
  516. synchronized (_spawnListeners)
  517. {
  518. for (SpawnListener listener : _spawnListeners)
  519. {
  520. listener.npcSpawned(npc);
  521. }
  522. }
  523. }
  524. /**
  525. * Set bounds for random calculation and delay for respawn
  526. * @param delay delay in seconds
  527. * @param randomInterval random interval in seconds
  528. */
  529. public void setRespawnDelay(int delay, int randomInterval)
  530. {
  531. if (delay != 0)
  532. {
  533. if (delay < 0)
  534. {
  535. _log.warning("respawn delay is negative for spawn:" + this);
  536. }
  537. int minDelay = delay - randomInterval;
  538. int maxDelay = delay + randomInterval;
  539. _respawnMinDelay = Math.max(10, minDelay) * 1000;
  540. _respawnMaxDelay = Math.max(10, maxDelay) * 1000;
  541. }
  542. else
  543. {
  544. _respawnMinDelay = 0;
  545. _respawnMaxDelay = 0;
  546. }
  547. }
  548. public void setRespawnDelay(int delay)
  549. {
  550. setRespawnDelay(delay, 0);
  551. }
  552. public int getRespawnDelay()
  553. {
  554. return (_respawnMinDelay + _respawnMaxDelay) / 2;
  555. }
  556. public boolean hasRespawnRandom()
  557. {
  558. return _respawnMinDelay != _respawnMaxDelay;
  559. }
  560. public L2Npc getLastSpawn()
  561. {
  562. return _lastSpawn;
  563. }
  564. /**
  565. * @param oldNpc
  566. */
  567. public void respawnNpc(L2Npc oldNpc)
  568. {
  569. if (_doRespawn)
  570. {
  571. oldNpc.refreshID();
  572. initializeNpcInstance(oldNpc);
  573. }
  574. }
  575. public L2NpcTemplate getTemplate()
  576. {
  577. return _template;
  578. }
  579. public int getInstanceId()
  580. {
  581. return _instanceId;
  582. }
  583. public void setInstanceId(int instanceId)
  584. {
  585. _instanceId = instanceId;
  586. }
  587. @Override
  588. public String toString()
  589. {
  590. return "L2Spawn [_template=" + getId() + ", _locX=" + getX() + ", _locY=" + getY() + ", _locZ=" + getZ() + ", _heading=" + getHeading() + "]";
  591. }
  592. public final boolean isNoRndWalk()
  593. {
  594. return _isNoRndWalk;
  595. }
  596. public final void setIsNoRndWalk(boolean value)
  597. {
  598. _isNoRndWalk = value;
  599. }
  600. }