AutoSpawnHandler.java 22 KB

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