L2Spawn.java 21 KB

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