L2Spawn.java 22 KB

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