L2Spawn.java 18 KB

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