L2Spawn.java 18 KB

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