L2Spawn.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * Copyright (C) 2004-2015 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.Map;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. import java.util.concurrent.CopyOnWriteArrayList;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.gameserver.GeoData;
  29. import com.l2jserver.gameserver.ThreadPoolManager;
  30. import com.l2jserver.gameserver.data.sql.impl.TerritoryTable;
  31. import com.l2jserver.gameserver.data.xml.impl.NpcData;
  32. import com.l2jserver.gameserver.datatables.NpcPersonalAIData;
  33. import com.l2jserver.gameserver.model.actor.L2Attackable;
  34. import com.l2jserver.gameserver.model.actor.L2Npc;
  35. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  36. import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
  37. import com.l2jserver.gameserver.model.interfaces.ILocational;
  38. import com.l2jserver.gameserver.model.interfaces.INamable;
  39. import com.l2jserver.gameserver.model.interfaces.IPositionable;
  40. import com.l2jserver.gameserver.model.zone.type.NpcSpawnTerritory;
  41. import com.l2jserver.util.Rnd;
  42. /**
  43. * This class manages the spawn and respawn of a group of L2NpcInstance that are in the same are and have the same type.<br>
  44. * <B><U>Concept</U>:</B><br>
  45. * 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>
  46. * The heading of the L2NpcInstance can be a random heading if not defined (value= -1) or an exact heading (ex : merchant...).
  47. * @author Nightmare
  48. */
  49. public class L2Spawn implements IPositionable, IIdentifiable, INamable
  50. {
  51. protected static final Logger _log = Logger.getLogger(L2Spawn.class.getName());
  52. /** String identifier of this spawn */
  53. private String _name;
  54. /** The link on the L2NpcTemplate object containing generic and static properties of this spawn (ex : RewardExp, RewardSP, AggroRange...) */
  55. private L2NpcTemplate _template;
  56. /** The maximum number of L2NpcInstance that can manage this L2Spawn */
  57. private int _maximumCount;
  58. /** The current number of L2NpcInstance managed by this L2Spawn */
  59. private int _currentCount;
  60. /** The current number of SpawnTask in progress or stand by of this L2Spawn */
  61. protected int _scheduledCount;
  62. /** The identifier of the location area where L2NpcInstance can be spwaned */
  63. private int _locationId;
  64. /** The Location of this NPC spawn. */
  65. private Location _location = new Location(0, 0, 0, 0, 0);
  66. /** Link to NPC spawn territory */
  67. private NpcSpawnTerritory _spawnTerritory = null;
  68. /** Minimum respawn delay */
  69. private int _respawnMinDelay;
  70. /** Maximum respawn delay */
  71. private int _respawnMaxDelay;
  72. /** The generic constructor of L2NpcInstance managed by this L2Spawn */
  73. private Constructor<? extends L2Npc> _constructor;
  74. /** If True a L2NpcInstance is respawned each time that another is killed */
  75. private boolean _doRespawn;
  76. /** If true then spawn is custom */
  77. private boolean _customSpawn;
  78. private static List<SpawnListener> _spawnListeners = new CopyOnWriteArrayList<>();
  79. private final List<L2Npc> _spawnedNpcs = new CopyOnWriteArrayList<>();
  80. private Map<Integer, Location> _lastSpawnPoints;
  81. private boolean _isNoRndWalk = false; // Is no random walk
  82. /** The task launching the function doSpawn() */
  83. class SpawnTask implements Runnable
  84. {
  85. private final L2Npc _oldNpc;
  86. public SpawnTask(L2Npc pOldNpc)
  87. {
  88. _oldNpc = pOldNpc;
  89. }
  90. @Override
  91. public void run()
  92. {
  93. try
  94. {
  95. // doSpawn();
  96. respawnNpc(_oldNpc);
  97. }
  98. catch (Exception e)
  99. {
  100. _log.log(Level.WARNING, "", e);
  101. }
  102. _scheduledCount--;
  103. }
  104. }
  105. /**
  106. * Constructor of L2Spawn.<br>
  107. * <B><U>Concept</U>:</B><br>
  108. * Each L2Spawn owns generic and static properties (ex : RewardExp, RewardSP, AggroRange...).<br>
  109. * 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>
  110. * When a new instance of L2Spawn is created, server just create a link between the instance and the template.<br>
  111. * This link is stored in <B>_template</B> Each L2NpcInstance is linked to a L2Spawn that manages its spawn and respawn (delay, location...).<br>
  112. * This link is stored in <B>_spawn</B> of the L2NpcInstance.<br>
  113. * <B><U> Actions</U>:</B><br>
  114. * <ul>
  115. * <li>Set the _template of the L2Spawn</li>
  116. * <li>Calculate the implementationName used to generate the generic constructor of L2NpcInstance managed by this L2Spawn</li>
  117. * <li>Create the generic constructor of L2NpcInstance managed by this L2Spawn</li>
  118. * </ul>
  119. * @param template The L2NpcTemplate to link to this L2Spawn
  120. * @throws SecurityException
  121. * @throws ClassNotFoundException
  122. * @throws NoSuchMethodException
  123. * @throws ClassCastException when template type is not subclass of L2Npc
  124. */
  125. public L2Spawn(L2NpcTemplate template) throws SecurityException, ClassNotFoundException, NoSuchMethodException, ClassCastException
  126. {
  127. // Set the _template of the L2Spawn
  128. _template = template;
  129. if (_template == null)
  130. {
  131. return;
  132. }
  133. String className = "com.l2jserver.gameserver.model.actor.instance." + _template.getType() + "Instance";
  134. // Create the generic constructor of L2Npc managed by this L2Spawn
  135. _constructor = Class.forName(className).asSubclass(L2Npc.class).getConstructor(L2NpcTemplate.class);
  136. }
  137. /**
  138. * Creates a spawn.
  139. * @param npcId the NPC ID
  140. * @throws ClassCastException
  141. * @throws NoSuchMethodException
  142. * @throws ClassNotFoundException
  143. * @throws SecurityException
  144. */
  145. public L2Spawn(int npcId) throws SecurityException, ClassNotFoundException, NoSuchMethodException, ClassCastException
  146. {
  147. this(NpcData.getInstance().getTemplate(npcId));
  148. }
  149. /**
  150. * @return the maximum number of L2NpcInstance that this L2Spawn can manage.
  151. */
  152. public int getAmount()
  153. {
  154. return _maximumCount;
  155. }
  156. /**
  157. * @return the String Identifier of this spawn.
  158. */
  159. @Override
  160. public String getName()
  161. {
  162. return _name;
  163. }
  164. /**
  165. * Set the String Identifier of this spawn.
  166. * @param name
  167. */
  168. public void setName(String name)
  169. {
  170. _name = name;
  171. }
  172. /**
  173. * @return the Identifier of the location area where L2NpcInstance can be spwaned.
  174. */
  175. public int getLocationId()
  176. {
  177. return _locationId;
  178. }
  179. @Override
  180. public Location getLocation()
  181. {
  182. return _location;
  183. }
  184. public Location getLocation(L2Object obj)
  185. {
  186. return ((_lastSpawnPoints == null) || (obj == null) || !_lastSpawnPoints.containsKey(obj.getObjectId())) ? _location : _lastSpawnPoints.get(obj.getObjectId());
  187. }
  188. @Override
  189. public int getX()
  190. {
  191. return _location.getX();
  192. }
  193. /**
  194. * @param obj object to check
  195. * @return the X position of the last spawn point of given NPC.
  196. */
  197. public int getX(L2Object obj)
  198. {
  199. return getLocation(obj).getX();
  200. }
  201. /**
  202. * Set the X position of the spawn point.
  203. * @param x the x coordinate
  204. */
  205. @Override
  206. public void setX(int x)
  207. {
  208. _location.setX(x);
  209. }
  210. @Override
  211. public int getY()
  212. {
  213. return _location.getY();
  214. }
  215. /**
  216. * @param obj object to check
  217. * @return the Y position of the last spawn point of given NPC.
  218. */
  219. public int getY(L2Object obj)
  220. {
  221. return getLocation(obj).getY();
  222. }
  223. /**
  224. * Set the Y position of the spawn point.
  225. * @param y the y coordinate
  226. */
  227. @Override
  228. public void setY(int y)
  229. {
  230. _location.setY(y);
  231. }
  232. @Override
  233. public int getZ()
  234. {
  235. return _location.getZ();
  236. }
  237. /**
  238. * @param obj object to check
  239. * @return the Z position of the last spawn point of given NPC.
  240. */
  241. public int getZ(L2Object obj)
  242. {
  243. return getLocation(obj).getZ();
  244. }
  245. /**
  246. * Set the Z position of the spawn point.
  247. * @param z the z coordinate
  248. */
  249. @Override
  250. public void setZ(int z)
  251. {
  252. _location.setZ(z);
  253. }
  254. /**
  255. * Set the x, y, z position of the spawn point.
  256. * @param x The x coordinate.
  257. * @param y The y coordinate.
  258. * @param z The z coordinate.
  259. */
  260. @Override
  261. public void setXYZ(int x, int y, int z)
  262. {
  263. setX(x);
  264. setY(y);
  265. setZ(z);
  266. }
  267. /**
  268. * Set the x, y, z position of the spawn point.
  269. * @param loc The location.
  270. */
  271. @Override
  272. public void setXYZ(ILocational loc)
  273. {
  274. setXYZ(loc.getX(), loc.getY(), loc.getZ());
  275. }
  276. /**
  277. * @return the heading of L2NpcInstance when they are spawned.
  278. */
  279. @Override
  280. public int getHeading()
  281. {
  282. return _location.getHeading();
  283. }
  284. /**
  285. * Set the heading of L2NpcInstance when they are spawned.
  286. * @param heading
  287. */
  288. @Override
  289. public void setHeading(int heading)
  290. {
  291. _location.setHeading(heading);
  292. }
  293. /**
  294. * Set the XYZ position of the spawn point.
  295. * @param loc
  296. */
  297. @Override
  298. public void setLocation(Location loc)
  299. {
  300. _location = loc;
  301. }
  302. /**
  303. * Gets the NPC ID.
  304. * @return the NPC ID
  305. */
  306. @Override
  307. public int getId()
  308. {
  309. return _template.getId();
  310. }
  311. /**
  312. * @return min respawn delay.
  313. */
  314. public int getRespawnMinDelay()
  315. {
  316. return _respawnMinDelay;
  317. }
  318. /**
  319. * @return max respawn delay.
  320. */
  321. public int getRespawnMaxDelay()
  322. {
  323. return _respawnMaxDelay;
  324. }
  325. /**
  326. * Set the maximum number of L2NpcInstance that this L2Spawn can manage.
  327. * @param amount
  328. */
  329. public void setAmount(int amount)
  330. {
  331. _maximumCount = amount;
  332. }
  333. /**
  334. * Set the Identifier of the location area where L2NpcInstance can be spawned.
  335. * @param id
  336. */
  337. public void setLocationId(int id)
  338. {
  339. _locationId = id;
  340. }
  341. /**
  342. * Set Minimum Respawn Delay.
  343. * @param date
  344. */
  345. public void setRespawnMinDelay(int date)
  346. {
  347. _respawnMinDelay = date;
  348. }
  349. /**
  350. * Set Maximum Respawn Delay.
  351. * @param date
  352. */
  353. public void setRespawnMaxDelay(int date)
  354. {
  355. _respawnMaxDelay = date;
  356. }
  357. /**
  358. * Set the spawn as custom.<BR>
  359. * @param custom
  360. */
  361. public void setCustom(boolean custom)
  362. {
  363. _customSpawn = custom;
  364. }
  365. /**
  366. * @return type of spawn.
  367. */
  368. public boolean isCustom()
  369. {
  370. return _customSpawn;
  371. }
  372. /**
  373. * 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
  374. * 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 +
  375. * _currentCount < _maximumCount</B></FONT>
  376. * @param oldNpc
  377. */
  378. public void decreaseCount(L2Npc oldNpc)
  379. {
  380. // sanity check
  381. if (_currentCount <= 0)
  382. {
  383. return;
  384. }
  385. // Decrease the current number of L2NpcInstance of this L2Spawn
  386. _currentCount--;
  387. // Remove this NPC from list of spawned
  388. _spawnedNpcs.remove(oldNpc);
  389. // Remove spawn point for old NPC
  390. if (_lastSpawnPoints != null)
  391. {
  392. _lastSpawnPoints.remove(oldNpc.getObjectId());
  393. }
  394. // Check if respawn is possible to prevent multiple respawning caused by lag
  395. if (_doRespawn && ((_scheduledCount + _currentCount) < _maximumCount))
  396. {
  397. // Update the current number of SpawnTask in progress or stand by of this L2Spawn
  398. _scheduledCount++;
  399. // Create a new SpawnTask to launch after the respawn Delay
  400. // ClientScheduler.getInstance().scheduleLow(new SpawnTask(npcId), _respawnDelay);
  401. ThreadPoolManager.getInstance().scheduleGeneral(new SpawnTask(oldNpc), hasRespawnRandom() ? Rnd.get(_respawnMinDelay, _respawnMaxDelay) : _respawnMinDelay);
  402. }
  403. }
  404. /**
  405. * Create the initial spawning and set _doRespawn to False, if respawn time set to 0, or set it to True otherwise.
  406. * @return The number of L2NpcInstance that were spawned
  407. */
  408. public int init()
  409. {
  410. while (_currentCount < _maximumCount)
  411. {
  412. doSpawn();
  413. }
  414. _doRespawn = _respawnMinDelay != 0;
  415. return _currentCount;
  416. }
  417. /**
  418. * Create a L2NpcInstance in this L2Spawn.
  419. * @param val
  420. * @return
  421. */
  422. public L2Npc spawnOne(boolean val)
  423. {
  424. return doSpawn(val);
  425. }
  426. /**
  427. * @return true if respawn enabled
  428. */
  429. public boolean isRespawnEnabled()
  430. {
  431. return _doRespawn;
  432. }
  433. /**
  434. * Set _doRespawn to False to stop respawn in this L2Spawn.
  435. */
  436. public void stopRespawn()
  437. {
  438. _doRespawn = false;
  439. }
  440. /**
  441. * Set _doRespawn to True to start or restart respawn in this L2Spawn.
  442. */
  443. public void startRespawn()
  444. {
  445. _doRespawn = true;
  446. }
  447. public L2Npc doSpawn()
  448. {
  449. return doSpawn(false);
  450. }
  451. /**
  452. * Create the L2NpcInstance, add it to the world and lauch its OnSpawn action.<br>
  453. * <B><U>Concept</U>:</B><br>
  454. * 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>
  455. * The heading of the L2NpcInstance can be a random heading if not defined (value= -1) or an exact heading (ex : merchant...).<br>
  456. * <B><U>Actions for an random spawn into location area</U>:<I> (if Locx=0 and Locy=0)</I></B>
  457. * <ul>
  458. * <li>Get L2NpcInstance Init parameters and its generate an Identifier</li>
  459. * <li>Call the constructor of the L2NpcInstance</li>
  460. * <li>Calculate the random position in the location area (if Locx=0 and Locy=0) or get its exact position from the L2Spawn</li>
  461. * <li>Set the position of the L2NpcInstance</li>
  462. * <li>Set the HP and MP of the L2NpcInstance to the max</li>
  463. * <li>Set the heading of the L2NpcInstance (random heading if not defined : value=-1)</li>
  464. * <li>Link the L2NpcInstance to this L2Spawn</li>
  465. * <li>Init other values of the L2NpcInstance (ex : from its L2CharTemplate for INT, STR, DEX...) and add it in the world</li>
  466. * <li>Launch the action OnSpawn fo the L2NpcInstance</li>
  467. * <li>Increase the current number of L2NpcInstance managed by this L2Spawn</li>
  468. * </ul>
  469. * @param isSummonSpawn
  470. * @return
  471. */
  472. public L2Npc doSpawn(boolean isSummonSpawn)
  473. {
  474. try
  475. {
  476. // Check if the L2Spawn is not a L2Pet or L2Minion or L2Decoy spawn
  477. if (_template.isType("L2Pet") || _template.isType("L2Decoy") || _template.isType("L2Trap"))
  478. {
  479. _currentCount++;
  480. return null;
  481. }
  482. // Call the constructor of the L2Npc
  483. L2Npc npc = _constructor.newInstance(_template);
  484. npc.setInstanceId(getInstanceId()); // Must be done before object is spawned into visible world
  485. if (isSummonSpawn)
  486. {
  487. npc.setShowSummonAnimation(isSummonSpawn);
  488. }
  489. // Check for certain AI data, overriden in spawnlist
  490. if (_name != null)
  491. {
  492. NpcPersonalAIData.getInstance().initializeNpcParameters(npc, this, _name);
  493. }
  494. return initializeNpcInstance(npc);
  495. }
  496. catch (Exception e)
  497. {
  498. _log.log(Level.WARNING, "NPC " + _template.getId() + " class not found", e);
  499. }
  500. return null;
  501. }
  502. /**
  503. * @param mob
  504. * @return
  505. */
  506. private L2Npc initializeNpcInstance(L2Npc mob)
  507. {
  508. int newlocx = 0;
  509. int newlocy = 0;
  510. int newlocz = 0;
  511. // If Locx and Locy are not defined, the L2NpcInstance must be spawned in an area defined by location or spawn territory
  512. // New method
  513. if (isTerritoryBased())
  514. {
  515. int[] p = _spawnTerritory.getRandomPoint();
  516. newlocx = p[0];
  517. newlocy = p[1];
  518. newlocz = p[2];
  519. }
  520. // Old method (for backward compatibility)
  521. else if ((getX() == 0) && (getY() == 0))
  522. {
  523. if (getLocationId() == 0)
  524. {
  525. return mob;
  526. }
  527. // Calculate the random position in the location area
  528. final Location location = TerritoryTable.getInstance().getRandomPoint(getLocationId());
  529. // Set the calculated position of the L2NpcInstance
  530. if (location != null)
  531. {
  532. newlocx = location.getX();
  533. newlocy = location.getY();
  534. newlocz = location.getZ();
  535. }
  536. }
  537. else
  538. {
  539. // The L2NpcInstance is spawned at the exact position (Lox, Locy, Locz)
  540. newlocx = getX();
  541. newlocy = getY();
  542. newlocz = getZ();
  543. }
  544. // don't correct z of flying npc's
  545. if (!mob.isFlying())
  546. {
  547. newlocz = GeoData.getInstance().getSpawnHeight(newlocx, newlocy, newlocz);
  548. }
  549. mob.stopAllEffects();
  550. mob.setIsDead(false);
  551. // Reset decay info
  552. mob.setDecayed(false);
  553. // Set the HP and MP of the L2NpcInstance to the max
  554. mob.setCurrentHpMp(mob.getMaxHp(), mob.getMaxMp());
  555. // Clear script variables
  556. if (mob.hasVariables())
  557. {
  558. mob.getVariables().getSet().clear();
  559. }
  560. // Set is not random walk default value
  561. mob.setIsNoRndWalk(isNoRndWalk());
  562. // Set the heading of the L2NpcInstance (random heading if not defined)
  563. if (getHeading() == -1)
  564. {
  565. mob.setHeading(Rnd.nextInt(61794));
  566. }
  567. else
  568. {
  569. mob.setHeading(getHeading());
  570. }
  571. if (mob instanceof L2Attackable)
  572. {
  573. ((L2Attackable) mob).setChampion(false);
  574. }
  575. if (Config.L2JMOD_CHAMPION_ENABLE)
  576. {
  577. // Set champion on next spawn
  578. if (mob.isMonster() && !getTemplate().isUndying() && !mob.isRaid() && !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)))
  579. {
  580. if (Rnd.get(100) < Config.L2JMOD_CHAMPION_FREQUENCY)
  581. {
  582. ((L2Attackable) mob).setChampion(true);
  583. }
  584. }
  585. }
  586. // Reset summoner
  587. mob.setSummoner(null);
  588. // Reset summoned list
  589. mob.resetSummonedNpcs();
  590. // Link the L2NpcInstance to this L2Spawn
  591. mob.setSpawn(this);
  592. // Spawn NPC
  593. mob.spawnMe(newlocx, newlocy, newlocz);
  594. notifyNpcSpawned(mob);
  595. _spawnedNpcs.add(mob);
  596. if (_lastSpawnPoints != null)
  597. {
  598. _lastSpawnPoints.put(mob.getObjectId(), new Location(newlocx, newlocy, newlocz));
  599. }
  600. if (Config.DEBUG)
  601. {
  602. _log.finest("Spawned Mob Id: " + _template.getId() + " , at: X: " + mob.getX() + " Y: " + mob.getY() + " Z: " + mob.getZ());
  603. }
  604. // Increase the current number of L2NpcInstance managed by this L2Spawn
  605. _currentCount++;
  606. return mob;
  607. }
  608. public static void addSpawnListener(SpawnListener listener)
  609. {
  610. _spawnListeners.add(listener);
  611. }
  612. public static void removeSpawnListener(SpawnListener listener)
  613. {
  614. _spawnListeners.remove(listener);
  615. }
  616. public static void notifyNpcSpawned(L2Npc npc)
  617. {
  618. for (SpawnListener listener : _spawnListeners)
  619. {
  620. listener.npcSpawned(npc);
  621. }
  622. }
  623. /**
  624. * Set bounds for random calculation and delay for respawn
  625. * @param delay delay in seconds
  626. * @param randomInterval random interval in seconds
  627. */
  628. public void setRespawnDelay(int delay, int randomInterval)
  629. {
  630. if (delay != 0)
  631. {
  632. if (delay < 0)
  633. {
  634. _log.warning("respawn delay is negative for spawn:" + this);
  635. }
  636. int minDelay = delay - randomInterval;
  637. int maxDelay = delay + randomInterval;
  638. _respawnMinDelay = Math.max(10, minDelay) * 1000;
  639. _respawnMaxDelay = Math.max(10, maxDelay) * 1000;
  640. }
  641. else
  642. {
  643. _respawnMinDelay = 0;
  644. _respawnMaxDelay = 0;
  645. }
  646. }
  647. public void setRespawnDelay(int delay)
  648. {
  649. setRespawnDelay(delay, 0);
  650. }
  651. public int getRespawnDelay()
  652. {
  653. return (_respawnMinDelay + _respawnMaxDelay) / 2;
  654. }
  655. public boolean hasRespawnRandom()
  656. {
  657. return _respawnMinDelay != _respawnMaxDelay;
  658. }
  659. public void setSpawnTerritory(NpcSpawnTerritory territory)
  660. {
  661. _spawnTerritory = territory;
  662. _lastSpawnPoints = new ConcurrentHashMap<>();
  663. }
  664. public NpcSpawnTerritory getSpawnTerritory()
  665. {
  666. return _spawnTerritory;
  667. }
  668. public boolean isTerritoryBased()
  669. {
  670. return (_spawnTerritory != null) && (_location.getX() == 0) && (_location.getY() == 0);
  671. }
  672. public L2Npc getLastSpawn()
  673. {
  674. return _spawnedNpcs.get(_spawnedNpcs.size() - 1);
  675. }
  676. public final List<L2Npc> getSpawnedNpcs()
  677. {
  678. return _spawnedNpcs;
  679. }
  680. /**
  681. * @param oldNpc
  682. */
  683. public void respawnNpc(L2Npc oldNpc)
  684. {
  685. if (_doRespawn)
  686. {
  687. oldNpc.refreshID();
  688. initializeNpcInstance(oldNpc);
  689. }
  690. }
  691. public L2NpcTemplate getTemplate()
  692. {
  693. return _template;
  694. }
  695. @Override
  696. public int getInstanceId()
  697. {
  698. return _location.getInstanceId();
  699. }
  700. @Override
  701. public void setInstanceId(int instanceId)
  702. {
  703. _location.setInstanceId(instanceId);
  704. }
  705. @Override
  706. public String toString()
  707. {
  708. return "L2Spawn ID: " + getId() + " " + getLocation();
  709. }
  710. public final boolean isNoRndWalk()
  711. {
  712. return _isNoRndWalk;
  713. }
  714. public final void setIsNoRndWalk(boolean value)
  715. {
  716. _isNoRndWalk = value;
  717. }
  718. }