L2Spawn.java 18 KB

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