L2Spawn.java 18 KB

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