AutoSpawnHandler.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.model;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.concurrent.ScheduledFuture;
  21. import java.util.concurrent.TimeUnit;
  22. import java.util.logging.Logger;
  23. import javolution.util.FastList;
  24. import javolution.util.FastMap;
  25. import net.sf.l2j.Config;
  26. import net.sf.l2j.L2DatabaseFactory;
  27. import net.sf.l2j.gameserver.Announcements;
  28. import net.sf.l2j.gameserver.ThreadPoolManager;
  29. import net.sf.l2j.gameserver.datatables.MapRegionTable;
  30. import net.sf.l2j.gameserver.datatables.NpcTable;
  31. import net.sf.l2j.gameserver.datatables.SpawnTable;
  32. import net.sf.l2j.gameserver.idfactory.IdFactory;
  33. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  34. import net.sf.l2j.gameserver.templates.L2NpcTemplate;
  35. import net.sf.l2j.util.Rnd;
  36. /**
  37. * Auto Spawn Handler
  38. *
  39. * Allows spawning of a NPC object based on a timer. (From the official idea
  40. * used for the Merchant and Blacksmith of Mammon)
  41. *
  42. * General Usage: - Call registerSpawn() with the parameters listed below. int
  43. * npcId int[][] spawnPoints or specify NULL to add points later. int
  44. * initialDelay (If < 0 = default value) int respawnDelay (If < 0 = default
  45. * value) int despawnDelay (If < 0 = default value or if = 0, function disabled)
  46. *
  47. * spawnPoints is a standard two-dimensional int array containing X,Y and Z
  48. * coordinates. The default respawn/despawn delays are currently every hour (as
  49. * for Mammon on official servers).
  50. * - The resulting AutoSpawnInstance object represents the newly added spawn
  51. * index. - The interal methods of this object can be used to adjust random
  52. * spawning, for instance a call to setRandomSpawn(1, true); would set the spawn
  53. * at index 1 to be randomly rather than sequentially-based. - Also they can be
  54. * used to specify the number of NPC instances to spawn using setSpawnCount(),
  55. * and broadcast a message to all users using setBroadcast().
  56. *
  57. * Random Spawning = OFF by default Broadcasting = OFF by default
  58. *
  59. * @author Tempy
  60. *
  61. */
  62. public class AutoSpawnHandler
  63. {
  64. protected static final Logger _log = Logger.getLogger(AutoSpawnHandler.class.getName());
  65. private static AutoSpawnHandler _instance;
  66. private static final int DEFAULT_INITIAL_SPAWN = 30000; // 30 seconds after registration
  67. private static final int DEFAULT_RESPAWN = 3600000; // 1 hour in millisecs
  68. private static final int DEFAULT_DESPAWN = 3600000; // 1 hour in millisecs
  69. protected Map<Integer, AutoSpawnInstance> _registeredSpawns;
  70. protected Map<Integer, ScheduledFuture<?>> _runningSpawns;
  71. protected boolean _activeState = true;
  72. private AutoSpawnHandler()
  73. {
  74. _registeredSpawns = new FastMap<Integer, AutoSpawnInstance>();
  75. _runningSpawns = new FastMap<Integer, ScheduledFuture<?>>();
  76. restoreSpawnData();
  77. }
  78. public static AutoSpawnHandler getInstance()
  79. {
  80. if (_instance == null)
  81. _instance = new AutoSpawnHandler();
  82. return _instance;
  83. }
  84. public final int size()
  85. {
  86. return _registeredSpawns.size();
  87. }
  88. private void restoreSpawnData()
  89. {
  90. int numLoaded = 0;
  91. java.sql.Connection con = null;
  92. try
  93. {
  94. PreparedStatement statement = null;
  95. PreparedStatement statement2 = null;
  96. ResultSet rs = null;
  97. ResultSet rs2 = null;
  98. con = L2DatabaseFactory.getInstance().getConnection();
  99. // Restore spawn group data, then the location data.
  100. statement = con.prepareStatement("SELECT * FROM random_spawn ORDER BY groupId ASC");
  101. rs = statement.executeQuery();
  102. while (rs.next())
  103. {
  104. // Register random spawn group, set various options on the
  105. // created spawn instance.
  106. AutoSpawnInstance spawnInst = registerSpawn(rs.getInt("npcId"), rs.getInt("initialDelay"),
  107. rs.getInt("respawnDelay"), rs.getInt("despawnDelay"));
  108. spawnInst.setSpawnCount(rs.getInt("count"));
  109. spawnInst.setBroadcast(rs.getBoolean("broadcastSpawn"));
  110. spawnInst.setRandomSpawn(rs.getBoolean("randomSpawn"));
  111. numLoaded++;
  112. // Restore the spawn locations for this spawn group/instance.
  113. statement2 = con.prepareStatement("SELECT * FROM random_spawn_loc WHERE groupId=?");
  114. statement2.setInt(1, rs.getInt("groupId"));
  115. rs2 = statement2.executeQuery();
  116. while (rs2.next())
  117. {
  118. // Add each location to the spawn group/instance.
  119. spawnInst.addSpawnLocation(rs2.getInt("x"),
  120. rs2.getInt("y"), rs2.getInt("z"),
  121. rs2.getInt("heading"));
  122. }
  123. statement2.close();
  124. }
  125. statement.close();
  126. if (Config.DEBUG)
  127. _log.config("AutoSpawnHandler: Loaded " + numLoaded
  128. + " spawn group(s) from the database.");
  129. } catch (Exception e)
  130. {
  131. _log.warning("AutoSpawnHandler: Could not restore spawn data: "
  132. + e);
  133. } finally
  134. {
  135. try
  136. {
  137. con.close();
  138. } catch (Exception e)
  139. {
  140. }
  141. }
  142. }
  143. /**
  144. * Registers a spawn with the given parameters with the spawner, and marks
  145. * it as active. Returns a AutoSpawnInstance containing info about the
  146. * spawn.
  147. *
  148. * @param int
  149. * npcId
  150. * @param int[][]
  151. * spawnPoints
  152. * @param int
  153. * initialDelay (If < 0 = default value)
  154. * @param int
  155. * respawnDelay (If < 0 = default value)
  156. * @param int
  157. * despawnDelay (If < 0 = default value or if = 0, function
  158. * disabled)
  159. * @return AutoSpawnInstance spawnInst
  160. */
  161. public AutoSpawnInstance registerSpawn(int npcId, int[][] spawnPoints, int initialDelay, int respawnDelay,
  162. int despawnDelay)
  163. {
  164. if (initialDelay < 0)
  165. initialDelay = DEFAULT_INITIAL_SPAWN;
  166. if (respawnDelay < 0)
  167. respawnDelay = DEFAULT_RESPAWN;
  168. if (despawnDelay < 0)
  169. despawnDelay = DEFAULT_DESPAWN;
  170. AutoSpawnInstance newSpawn = new AutoSpawnInstance(npcId, initialDelay, respawnDelay, despawnDelay);
  171. if (spawnPoints != null)
  172. for (int[] spawnPoint : spawnPoints)
  173. newSpawn.addSpawnLocation(spawnPoint);
  174. int newId = IdFactory.getInstance().getNextId();
  175. newSpawn._objectId = newId;
  176. _registeredSpawns.put(newId, newSpawn);
  177. setSpawnActive(newSpawn, true);
  178. if (Config.DEBUG)
  179. _log.config("AutoSpawnHandler: Registered auto spawn for NPC ID "+ npcId + " (Object ID = " + newId + ").");
  180. return newSpawn;
  181. }
  182. /**
  183. * Registers a spawn with the given parameters with the spawner, and marks
  184. * it as active. Returns a AutoSpawnInstance containing info about the
  185. * spawn. <BR>
  186. * <B>Warning:</B> Spawn locations must be specified separately using
  187. * addSpawnLocation().
  188. *
  189. * @param int
  190. * npcId
  191. * @param int
  192. * initialDelay (If < 0 = default value)
  193. * @param int
  194. * respawnDelay (If < 0 = default value)
  195. * @param int
  196. * despawnDelay (If < 0 = default value or if = 0, function
  197. * disabled)
  198. * @return AutoSpawnInstance spawnInst
  199. */
  200. public AutoSpawnInstance registerSpawn(int npcId, int initialDelay, int respawnDelay, int despawnDelay)
  201. {
  202. return registerSpawn(npcId, null, initialDelay, respawnDelay, despawnDelay);
  203. }
  204. /**
  205. * Remove a registered spawn from the list, specified by the given spawn
  206. * instance.
  207. *
  208. * @param AutoSpawnInstance
  209. * spawnInst
  210. * @return boolean removedSuccessfully
  211. */
  212. public boolean removeSpawn(AutoSpawnInstance spawnInst)
  213. {
  214. if (!isSpawnRegistered(spawnInst))
  215. return false;
  216. try
  217. {
  218. // Try to remove from the list of registered spawns if it exists.
  219. _registeredSpawns.remove(spawnInst.getNpcId());
  220. // Cancel the currently associated running scheduled task.
  221. ScheduledFuture<?> respawnTask = _runningSpawns.remove(spawnInst._objectId);
  222. respawnTask.cancel(false);
  223. if (Config.DEBUG)
  224. _log.config("AutoSpawnHandler: Removed auto spawn for NPC ID "
  225. + spawnInst._npcId + " (Object ID = "
  226. + spawnInst._objectId + ").");
  227. } catch (Exception e)
  228. {
  229. _log.warning("AutoSpawnHandler: Could not auto spawn for NPC ID "
  230. + spawnInst._npcId + " (Object ID = " + spawnInst._objectId
  231. + "): " + e);
  232. return false;
  233. }
  234. return true;
  235. }
  236. /**
  237. * Remove a registered spawn from the list, specified by the given spawn
  238. * object ID.
  239. *
  240. * @param int
  241. * objectId
  242. * @return boolean removedSuccessfully
  243. */
  244. public void removeSpawn(int objectId)
  245. {
  246. removeSpawn(_registeredSpawns.get(objectId));
  247. }
  248. /**
  249. * Sets the active state of the specified spawn.
  250. *
  251. * @param AutoSpawnInstance
  252. * spawnInst
  253. * @param boolean
  254. * isActive
  255. */
  256. public void setSpawnActive(AutoSpawnInstance spawnInst, boolean isActive)
  257. {
  258. if (spawnInst == null)
  259. return;
  260. int objectId = spawnInst._objectId;
  261. if (isSpawnRegistered(objectId))
  262. {
  263. ScheduledFuture<?> spawnTask = null;
  264. if (isActive)
  265. {
  266. AutoSpawner rs = new AutoSpawner(objectId);
  267. if (spawnInst._desDelay > 0)
  268. spawnTask = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(rs,
  269. spawnInst._initDelay, spawnInst._resDelay);
  270. else
  271. spawnTask = ThreadPoolManager.getInstance().scheduleEffect(rs, spawnInst._initDelay);
  272. _runningSpawns.put(objectId, spawnTask);
  273. } else
  274. {
  275. AutoDespawner rd = new AutoDespawner(objectId);
  276. spawnTask = _runningSpawns.remove(objectId);
  277. if (spawnTask != null)
  278. spawnTask.cancel(false);
  279. ThreadPoolManager.getInstance().scheduleEffect(rd, 0);
  280. }
  281. spawnInst.setSpawnActive(isActive);
  282. }
  283. }
  284. /**
  285. * Sets the active state of all auto spawn instances to that specified, and
  286. * cancels the scheduled spawn task if necessary.
  287. *
  288. * @param boolean
  289. * isActive
  290. */
  291. public void setAllActive(boolean isActive)
  292. {
  293. if (_activeState == isActive)
  294. return;
  295. for (AutoSpawnInstance spawnInst : _registeredSpawns.values())
  296. setSpawnActive(spawnInst, isActive);
  297. _activeState = isActive;
  298. }
  299. /**
  300. * Returns the number of milliseconds until the next occurrance of the given
  301. * spawn.
  302. *
  303. * @param AutoSpawnInstance
  304. * spawnInst
  305. * @param long
  306. * milliRemaining
  307. */
  308. public final long getTimeToNextSpawn(AutoSpawnInstance spawnInst)
  309. {
  310. int objectId = spawnInst.getObjectId();
  311. if (!isSpawnRegistered(objectId))
  312. return -1;
  313. return _runningSpawns.get(objectId).getDelay(TimeUnit.MILLISECONDS);
  314. }
  315. /**
  316. * Attempts to return the AutoSpawnInstance associated with the given NPC or
  317. * Object ID type. <BR>
  318. * Note: If isObjectId == false, returns first instance for the specified
  319. * NPC ID.
  320. *
  321. * @param int
  322. * id
  323. * @param boolean
  324. * isObjectId
  325. * @return AutoSpawnInstance spawnInst
  326. */
  327. public final AutoSpawnInstance getAutoSpawnInstance(int id, boolean isObjectId)
  328. {
  329. if (isObjectId)
  330. {
  331. if (isSpawnRegistered(id))
  332. return _registeredSpawns.get(id);
  333. }
  334. else
  335. {
  336. for (AutoSpawnInstance spawnInst : _registeredSpawns.values())
  337. if (spawnInst.getNpcId() == id)
  338. return spawnInst;
  339. }
  340. return null;
  341. }
  342. public Map<Integer, AutoSpawnInstance> getAutoSpawnInstances(int npcId)
  343. {
  344. Map<Integer, AutoSpawnInstance> spawnInstList = new FastMap<Integer, AutoSpawnInstance>();
  345. for (AutoSpawnInstance spawnInst : _registeredSpawns.values())
  346. if (spawnInst.getNpcId() == npcId)
  347. spawnInstList.put(spawnInst.getObjectId(), spawnInst);
  348. return spawnInstList;
  349. }
  350. /**
  351. * Tests if the specified object ID is assigned to an auto spawn.
  352. *
  353. * @param int
  354. * objectId
  355. * @return boolean isAssigned
  356. */
  357. public final boolean isSpawnRegistered(int objectId)
  358. {
  359. return _registeredSpawns.containsKey(objectId);
  360. }
  361. /**
  362. * Tests if the specified spawn instance is assigned to an auto spawn.
  363. *
  364. * @param AutoSpawnInstance
  365. * spawnInst
  366. * @return boolean isAssigned
  367. */
  368. public final boolean isSpawnRegistered(AutoSpawnInstance spawnInst)
  369. {
  370. return _registeredSpawns.containsValue(spawnInst);
  371. }
  372. /**
  373. * AutoSpawner Class <BR>
  374. * <BR>
  375. * This handles the main spawn task for an auto spawn instance, and
  376. * initializes a despawner if required.
  377. *
  378. * @author Tempy
  379. */
  380. private class AutoSpawner implements Runnable
  381. {
  382. private int _objectId;
  383. protected AutoSpawner(int objectId)
  384. {
  385. _objectId = objectId;
  386. }
  387. public void run()
  388. {
  389. try
  390. {
  391. // Retrieve the required spawn instance for this spawn task.
  392. AutoSpawnInstance spawnInst = _registeredSpawns.get(_objectId);
  393. // If the spawn is not scheduled to be active, cancel the spawn
  394. // task.
  395. if (!spawnInst.isSpawnActive())
  396. return;
  397. Location[] locationList = spawnInst.getLocationList();
  398. // If there are no set co-ordinates, cancel the spawn task.
  399. if (locationList.length == 0)
  400. {
  401. _log.info("AutoSpawnHandler: No location co-ords specified for spawn instance (Object ID = "
  402. + _objectId + ").");
  403. return;
  404. }
  405. int locationCount = locationList.length;
  406. int locationIndex = Rnd.nextInt(locationCount);
  407. /*
  408. * If random spawning is disabled, the spawn at the next set of
  409. * co-ordinates after the last. If the index is greater than the
  410. * number of possible spawns, reset the counter to zero.
  411. */
  412. if (!spawnInst.isRandomSpawn())
  413. {
  414. locationIndex = spawnInst._lastLocIndex;
  415. locationIndex++;
  416. if (locationIndex == locationCount)
  417. locationIndex = 0;
  418. spawnInst._lastLocIndex = locationIndex;
  419. }
  420. // Set the X, Y and Z co-ordinates, where this spawn will take
  421. // place.
  422. final int x = locationList[locationIndex].getX();
  423. final int y = locationList[locationIndex].getY();
  424. final int z = locationList[locationIndex].getZ();
  425. final int heading = locationList[locationIndex].getHeading();
  426. // Fetch the template for this NPC ID and create a new spawn.
  427. L2NpcTemplate npcTemp = NpcTable.getInstance().getTemplate(spawnInst.getNpcId());
  428. if (npcTemp == null)
  429. {
  430. _log.warning("Couldnt find NPC id" + spawnInst.getNpcId()+ " Try to update your DP");
  431. return;
  432. }
  433. L2Spawn newSpawn = new L2Spawn(npcTemp);
  434. newSpawn.setLocx(x);
  435. newSpawn.setLocy(y);
  436. newSpawn.setLocz(z);
  437. if (heading != -1)
  438. newSpawn.setHeading(heading);
  439. newSpawn.setAmount(spawnInst.getSpawnCount());
  440. if (spawnInst._desDelay == 0)
  441. {
  442. newSpawn.setRespawnDelay(spawnInst._resDelay);
  443. }
  444. // Add the new spawn information to the spawn table, but do not
  445. // store it.
  446. SpawnTable.getInstance().addNewSpawn(newSpawn, false);
  447. L2NpcInstance npcInst = null;
  448. if (spawnInst._spawnCount == 1)
  449. {
  450. npcInst = newSpawn.doSpawn();
  451. npcInst.setXYZ(npcInst.getX(), npcInst.getY(), npcInst.getZ());
  452. spawnInst.addNpcInstance(npcInst);
  453. } else
  454. {
  455. for (int i = 0; i < spawnInst._spawnCount; i++)
  456. {
  457. npcInst = newSpawn.doSpawn();
  458. // To prevent spawning of more than one NPC in the exact
  459. // same spot,
  460. // move it slightly by a small random offset.
  461. npcInst.setXYZ(npcInst.getX() + Rnd.nextInt(50), npcInst.getY() + Rnd.nextInt(50),
  462. npcInst.getZ());
  463. // Add the NPC instance to the list of managed
  464. // instances.
  465. spawnInst.addNpcInstance(npcInst);
  466. }
  467. }
  468. String nearestTown = MapRegionTable.getInstance().getClosestTownName(npcInst);
  469. // Announce to all players that the spawn has taken place, with
  470. // the nearest town location.
  471. if (spawnInst.isBroadcasting())
  472. Announcements.getInstance().announceToAll("The " + npcInst.getName() + " has spawned near "
  473. + nearestTown + "!");
  474. if (Config.DEBUG)
  475. _log.info("AutoSpawnHandler: Spawned NPC ID "
  476. + spawnInst.getNpcId() + " at " + x + ", " + y
  477. + ", " + z + " (Near " + nearestTown + ") for "
  478. + (spawnInst.getRespawnDelay() / 60000)
  479. + " minute(s).");
  480. // If there is no despawn time, do not create a despawn task.
  481. if (spawnInst.getDespawnDelay() > 0)
  482. {
  483. AutoDespawner rd = new AutoDespawner(_objectId);
  484. ThreadPoolManager.getInstance().scheduleAi(rd, spawnInst.getDespawnDelay() - 1000);
  485. }
  486. } catch (Exception e)
  487. {
  488. _log.warning("AutoSpawnHandler: An error occurred while initializing spawn instance (Object ID = "
  489. + _objectId + "): " + e);
  490. e.printStackTrace();
  491. }
  492. }
  493. }
  494. /**
  495. * AutoDespawner Class <BR>
  496. * <BR>
  497. * Simply used as a secondary class for despawning an auto spawn instance.
  498. *
  499. * @author Tempy
  500. */
  501. private class AutoDespawner implements Runnable
  502. {
  503. private int _objectId;
  504. protected AutoDespawner(int objectId)
  505. {
  506. _objectId = objectId;
  507. }
  508. public void run()
  509. {
  510. try
  511. {
  512. AutoSpawnInstance spawnInst = _registeredSpawns.get(_objectId);
  513. if (spawnInst == null)
  514. {
  515. _log.info("AutoSpawnHandler: No spawn registered for object ID = "+ _objectId + ".");
  516. return;
  517. }
  518. for (L2NpcInstance npcInst : spawnInst.getNPCInstanceList())
  519. {
  520. if (npcInst == null)
  521. continue;
  522. npcInst.deleteMe();
  523. spawnInst.removeNpcInstance(npcInst);
  524. if (Config.DEBUG)
  525. _log.info("AutoSpawnHandler: Spawns removed for spawn instance (Object ID = "+ _objectId + ").");
  526. }
  527. } catch (Exception e)
  528. {
  529. _log.warning("AutoSpawnHandler: An error occurred while despawning spawn (Object ID = "
  530. + _objectId + "): " + e);
  531. }
  532. }
  533. }
  534. /**
  535. * AutoSpawnInstance Class <BR>
  536. * <BR>
  537. * Stores information about a registered auto spawn.
  538. *
  539. * @author Tempy
  540. */
  541. public class AutoSpawnInstance
  542. {
  543. protected int _objectId;
  544. protected int _spawnIndex;
  545. protected int _npcId;
  546. protected int _initDelay;
  547. protected int _resDelay;
  548. protected int _desDelay;
  549. protected int _spawnCount = 1;
  550. protected int _lastLocIndex = -1;
  551. private List<L2NpcInstance> _npcList = new FastList<L2NpcInstance>();
  552. private List<Location> _locList = new FastList<Location>();
  553. private boolean _spawnActive;
  554. private boolean _randomSpawn = false;
  555. private boolean _broadcastAnnouncement = false;
  556. protected AutoSpawnInstance(int npcId, int initDelay, int respawnDelay, int despawnDelay)
  557. {
  558. _npcId = npcId;
  559. _initDelay = initDelay;
  560. _resDelay = respawnDelay;
  561. _desDelay = despawnDelay;
  562. }
  563. protected void setSpawnActive(boolean activeValue)
  564. {
  565. _spawnActive = activeValue;
  566. }
  567. protected boolean addNpcInstance(L2NpcInstance npcInst)
  568. {
  569. return _npcList.add(npcInst);
  570. }
  571. protected boolean removeNpcInstance(L2NpcInstance npcInst)
  572. {
  573. return _npcList.remove(npcInst);
  574. }
  575. public int getObjectId()
  576. {
  577. return _objectId;
  578. }
  579. public int getInitialDelay()
  580. {
  581. return _initDelay;
  582. }
  583. public int getRespawnDelay()
  584. {
  585. return _resDelay;
  586. }
  587. public int getDespawnDelay()
  588. {
  589. return _desDelay;
  590. }
  591. public int getNpcId()
  592. {
  593. return _npcId;
  594. }
  595. public int getSpawnCount()
  596. {
  597. return _spawnCount;
  598. }
  599. public Location[] getLocationList()
  600. {
  601. return _locList.toArray(new Location[_locList.size()]);
  602. }
  603. public L2NpcInstance[] getNPCInstanceList()
  604. {
  605. L2NpcInstance[] ret;
  606. synchronized (_npcList)
  607. {
  608. ret = new L2NpcInstance[_npcList.size()];
  609. _npcList.toArray(ret);
  610. }
  611. return ret;
  612. }
  613. public L2Spawn[] getSpawns()
  614. {
  615. List<L2Spawn> npcSpawns = new FastList<L2Spawn>();
  616. for (L2NpcInstance npcInst : _npcList)
  617. npcSpawns.add(npcInst.getSpawn());
  618. return npcSpawns.toArray(new L2Spawn[npcSpawns.size()]);
  619. }
  620. public void setSpawnCount(int spawnCount)
  621. {
  622. _spawnCount = spawnCount;
  623. }
  624. public void setRandomSpawn(boolean randValue)
  625. {
  626. _randomSpawn = randValue;
  627. }
  628. public void setBroadcast(boolean broadcastValue)
  629. {
  630. _broadcastAnnouncement = broadcastValue;
  631. }
  632. public boolean isSpawnActive()
  633. {
  634. return _spawnActive;
  635. }
  636. public boolean isRandomSpawn()
  637. {
  638. return _randomSpawn;
  639. }
  640. public boolean isBroadcasting()
  641. {
  642. return _broadcastAnnouncement;
  643. }
  644. public boolean addSpawnLocation(int x, int y, int z, int heading)
  645. {
  646. return _locList.add(new Location(x, y, z, heading));
  647. }
  648. public boolean addSpawnLocation(int[] spawnLoc)
  649. {
  650. if (spawnLoc.length != 3)
  651. return false;
  652. return addSpawnLocation(spawnLoc[0], spawnLoc[1], spawnLoc[2], -1);
  653. }
  654. public Location removeSpawnLocation(int locIndex)
  655. {
  656. try
  657. {
  658. return _locList.remove(locIndex);
  659. } catch (IndexOutOfBoundsException e)
  660. {
  661. return null;
  662. }
  663. }
  664. }
  665. }