L2Spawn.java 18 KB

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