L2Spawn.java 21 KB

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