L2Spawn.java 21 KB

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