L2Spawn.java 18 KB

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