RaidBossSpawnManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 com.l2jserver.gameserver.instancemanager;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.Calendar;
  21. import java.util.Map;
  22. import java.util.concurrent.ScheduledFuture;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javolution.util.FastMap;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.ThreadPoolManager;
  29. import com.l2jserver.gameserver.datatables.NpcTable;
  30. import com.l2jserver.gameserver.datatables.SpawnTable;
  31. import com.l2jserver.gameserver.model.L2Spawn;
  32. import com.l2jserver.gameserver.model.StatsSet;
  33. import com.l2jserver.gameserver.model.actor.instance.L2RaidBossInstance;
  34. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  35. import com.l2jserver.util.Rnd;
  36. /**
  37. * Raid Boss spawn manager.
  38. * @author godson
  39. */
  40. public class RaidBossSpawnManager
  41. {
  42. private static final Logger _log = Logger.getLogger(RaidBossSpawnManager.class.getName());
  43. protected static final Map<Integer, L2RaidBossInstance> _bosses = new FastMap<>();
  44. protected static final Map<Integer, L2Spawn> _spawns = new FastMap<>();
  45. protected static final Map<Integer, StatsSet> _storedInfo = new FastMap<>();
  46. protected static final Map<Integer, ScheduledFuture<?>> _schedules = new FastMap<>();
  47. public static enum StatusEnum
  48. {
  49. ALIVE,
  50. DEAD,
  51. UNDEFINED
  52. }
  53. /**
  54. * Instantiates a new raid boss spawn manager.
  55. */
  56. protected RaidBossSpawnManager()
  57. {
  58. load();
  59. }
  60. /**
  61. * Load.
  62. */
  63. public void load()
  64. {
  65. _bosses.clear();
  66. _spawns.clear();
  67. _storedInfo.clear();
  68. _schedules.clear();
  69. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  70. PreparedStatement statement = con.prepareStatement("SELECT * FROM raidboss_spawnlist ORDER BY boss_id");
  71. ResultSet rset = statement.executeQuery())
  72. {
  73. L2Spawn spawnDat;
  74. L2NpcTemplate template;
  75. long respawnTime;
  76. while (rset.next())
  77. {
  78. template = getValidTemplate(rset.getInt("boss_id"));
  79. if (template != null)
  80. {
  81. spawnDat = new L2Spawn(template);
  82. spawnDat.setLocx(rset.getInt("loc_x"));
  83. spawnDat.setLocy(rset.getInt("loc_y"));
  84. spawnDat.setLocz(rset.getInt("loc_z"));
  85. spawnDat.setAmount(rset.getInt("amount"));
  86. spawnDat.setHeading(rset.getInt("heading"));
  87. spawnDat.setRespawnMinDelay(rset.getInt("respawn_min_delay"));
  88. spawnDat.setRespawnMaxDelay(rset.getInt("respawn_max_delay"));
  89. respawnTime = rset.getLong("respawn_time");
  90. addNewSpawn(spawnDat, respawnTime, rset.getDouble("currentHP"), rset.getDouble("currentMP"), false);
  91. }
  92. else
  93. {
  94. _log.warning(getClass().getSimpleName() + ": Could not load raidboss #" + rset.getInt("boss_id") + " from DB");
  95. }
  96. }
  97. _log.info(getClass().getSimpleName() + ": Loaded " + _bosses.size() + " Instances");
  98. _log.info(getClass().getSimpleName() + ": Scheduled " + _schedules.size() + " Instances");
  99. }
  100. catch (SQLException e)
  101. {
  102. _log.warning(getClass().getSimpleName() + ": Couldnt load raidboss_spawnlist table");
  103. }
  104. catch (Exception e)
  105. {
  106. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error while initializing RaidBossSpawnManager: " + e.getMessage(), e);
  107. }
  108. }
  109. private static class SpawnSchedule implements Runnable
  110. {
  111. private static final Logger _log = Logger.getLogger(SpawnSchedule.class.getName());
  112. private final int bossId;
  113. /**
  114. * Instantiates a new spawn schedule.
  115. * @param npcId the npc id
  116. */
  117. public SpawnSchedule(int npcId)
  118. {
  119. bossId = npcId;
  120. }
  121. @Override
  122. public void run()
  123. {
  124. L2RaidBossInstance raidboss = null;
  125. if (bossId == 25328)
  126. {
  127. raidboss = DayNightSpawnManager.getInstance().handleBoss(_spawns.get(bossId));
  128. }
  129. else
  130. {
  131. raidboss = (L2RaidBossInstance) _spawns.get(bossId).doSpawn();
  132. }
  133. if (raidboss != null)
  134. {
  135. raidboss.setRaidStatus(StatusEnum.ALIVE);
  136. final StatsSet info = new StatsSet();
  137. info.set("currentHP", raidboss.getCurrentHp());
  138. info.set("currentMP", raidboss.getCurrentMp());
  139. info.set("respawnTime", 0L);
  140. _storedInfo.put(bossId, info);
  141. _log.info(getClass().getSimpleName() + ": Spawning Raid Boss " + raidboss.getName());
  142. _bosses.put(bossId, raidboss);
  143. }
  144. _schedules.remove(bossId);
  145. }
  146. }
  147. /**
  148. * Update status.
  149. * @param boss the boss
  150. * @param isBossDead the is boss dead
  151. */
  152. public void updateStatus(L2RaidBossInstance boss, boolean isBossDead)
  153. {
  154. if (!_storedInfo.containsKey(boss.getNpcId()))
  155. {
  156. return;
  157. }
  158. final StatsSet info = _storedInfo.get(boss.getNpcId());
  159. if (isBossDead)
  160. {
  161. boss.setRaidStatus(StatusEnum.DEAD);
  162. final int respawnMinDelay = boss.getSpawn().getRespawnMinDelay();
  163. final int respawnMaxDelay = boss.getSpawn().getRespawnMaxDelay();
  164. final long respawnDelay = Rnd.get((int) (respawnMinDelay * 1000 * Config.RAID_MIN_RESPAWN_MULTIPLIER), (int) (respawnMaxDelay * 1000 * Config.RAID_MAX_RESPAWN_MULTIPLIER));
  165. final long respawnTime = Calendar.getInstance().getTimeInMillis() + respawnDelay;
  166. info.set("currentHP", boss.getMaxHp());
  167. info.set("currentMP", boss.getMaxMp());
  168. info.set("respawnTime", respawnTime);
  169. if (!_schedules.containsKey(boss.getNpcId()) && ((respawnMinDelay > 0) || (respawnMaxDelay > 0)))
  170. {
  171. final Calendar time = Calendar.getInstance();
  172. time.setTimeInMillis(respawnTime);
  173. _log.info(getClass().getSimpleName() + ": Updated " + boss.getName() + " respawn time to " + time.getTime());
  174. _schedules.put(boss.getNpcId(), ThreadPoolManager.getInstance().scheduleGeneral(new SpawnSchedule(boss.getNpcId()), respawnDelay));
  175. updateDb();
  176. }
  177. }
  178. else
  179. {
  180. boss.setRaidStatus(StatusEnum.ALIVE);
  181. info.set("currentHP", boss.getCurrentHp());
  182. info.set("currentMP", boss.getCurrentMp());
  183. info.set("respawnTime", 0L);
  184. }
  185. _storedInfo.put(boss.getNpcId(), info);
  186. }
  187. /**
  188. * Adds the new spawn.
  189. * @param spawnDat the spawn dat
  190. * @param respawnTime the respawn time
  191. * @param currentHP the current hp
  192. * @param currentMP the current mp
  193. * @param storeInDb the store in db
  194. */
  195. public void addNewSpawn(L2Spawn spawnDat, long respawnTime, double currentHP, double currentMP, boolean storeInDb)
  196. {
  197. if (spawnDat == null)
  198. {
  199. return;
  200. }
  201. if (_spawns.containsKey(spawnDat.getNpcid()))
  202. {
  203. return;
  204. }
  205. final int bossId = spawnDat.getNpcid();
  206. final long time = Calendar.getInstance().getTimeInMillis();
  207. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  208. if ((respawnTime == 0L) || (time > respawnTime))
  209. {
  210. L2RaidBossInstance raidboss = null;
  211. if (bossId == 25328)
  212. {
  213. raidboss = DayNightSpawnManager.getInstance().handleBoss(spawnDat);
  214. }
  215. else
  216. {
  217. raidboss = (L2RaidBossInstance) spawnDat.doSpawn();
  218. }
  219. if (raidboss != null)
  220. {
  221. raidboss.setCurrentHp(currentHP);
  222. raidboss.setCurrentMp(currentMP);
  223. raidboss.setRaidStatus(StatusEnum.ALIVE);
  224. _bosses.put(bossId, raidboss);
  225. final StatsSet info = new StatsSet();
  226. info.set("currentHP", currentHP);
  227. info.set("currentMP", currentMP);
  228. info.set("respawnTime", 0L);
  229. _storedInfo.put(bossId, info);
  230. }
  231. }
  232. else
  233. {
  234. final long spawnTime = respawnTime - Calendar.getInstance().getTimeInMillis();
  235. _schedules.put(bossId, ThreadPoolManager.getInstance().scheduleGeneral(new SpawnSchedule(bossId), spawnTime));
  236. }
  237. _spawns.put(bossId, spawnDat);
  238. if (storeInDb)
  239. {
  240. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  241. PreparedStatement statement = con.prepareStatement("INSERT INTO raidboss_spawnlist (boss_id,amount,loc_x,loc_y,loc_z,heading,respawn_time,currentHp,currentMp) VALUES(?,?,?,?,?,?,?,?,?)"))
  242. {
  243. statement.setInt(1, spawnDat.getNpcid());
  244. statement.setInt(2, spawnDat.getAmount());
  245. statement.setInt(3, spawnDat.getLocx());
  246. statement.setInt(4, spawnDat.getLocy());
  247. statement.setInt(5, spawnDat.getLocz());
  248. statement.setInt(6, spawnDat.getHeading());
  249. statement.setLong(7, respawnTime);
  250. statement.setDouble(8, currentHP);
  251. statement.setDouble(9, currentMP);
  252. statement.execute();
  253. }
  254. catch (Exception e)
  255. {
  256. // problem with storing spawn
  257. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not store raidboss #" + bossId + " in the DB:" + e.getMessage(), e);
  258. }
  259. }
  260. }
  261. /**
  262. * Delete spawn.
  263. * @param spawnDat the spawn dat
  264. * @param updateDb the update db
  265. */
  266. public void deleteSpawn(L2Spawn spawnDat, boolean updateDb)
  267. {
  268. if (spawnDat == null)
  269. {
  270. return;
  271. }
  272. final int bossId = spawnDat.getNpcid();
  273. if (!_spawns.containsKey(bossId))
  274. {
  275. return;
  276. }
  277. SpawnTable.getInstance().deleteSpawn(spawnDat, false);
  278. _spawns.remove(bossId);
  279. if (_bosses.containsKey(bossId))
  280. {
  281. _bosses.remove(bossId);
  282. }
  283. if (_schedules.containsKey(bossId))
  284. {
  285. final ScheduledFuture<?> f = _schedules.remove(bossId);
  286. f.cancel(true);
  287. }
  288. if (_storedInfo.containsKey(bossId))
  289. {
  290. _storedInfo.remove(bossId);
  291. }
  292. if (updateDb)
  293. {
  294. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  295. PreparedStatement statement = con.prepareStatement("DELETE FROM raidboss_spawnlist WHERE boss_id=?"))
  296. {
  297. statement.setInt(1, bossId);
  298. statement.execute();
  299. }
  300. catch (Exception e)
  301. {
  302. // problem with deleting spawn
  303. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not remove raidboss #" + bossId + " from DB: " + e.getMessage(), e);
  304. }
  305. }
  306. }
  307. /**
  308. * Update database.
  309. */
  310. private void updateDb()
  311. {
  312. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  313. PreparedStatement statement = con.prepareStatement("UPDATE raidboss_spawnlist SET respawn_time = ?, currentHP = ?, currentMP = ? WHERE boss_id = ?"))
  314. {
  315. for (Integer bossId : _storedInfo.keySet())
  316. {
  317. if (bossId == null)
  318. {
  319. continue;
  320. }
  321. L2RaidBossInstance boss = _bosses.get(bossId);
  322. if (boss == null)
  323. {
  324. continue;
  325. }
  326. if (boss.getRaidStatus().equals(StatusEnum.ALIVE))
  327. {
  328. updateStatus(boss, false);
  329. }
  330. StatsSet info = _storedInfo.get(bossId);
  331. if (info == null)
  332. {
  333. continue;
  334. }
  335. try
  336. {
  337. statement.setLong(1, info.getLong("respawnTime"));
  338. statement.setDouble(2, info.getDouble("currentHP"));
  339. statement.setDouble(3, info.getDouble("currentMP"));
  340. statement.setInt(4, bossId);
  341. statement.executeUpdate();
  342. statement.clearParameters();
  343. }
  344. catch (SQLException e)
  345. {
  346. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldnt update raidboss_spawnlist table " + e.getMessage(), e);
  347. }
  348. }
  349. }
  350. catch (SQLException e)
  351. {
  352. _log.log(Level.WARNING, getClass().getSimpleName() + ": SQL error while updating RaidBoss spawn to database: " + e.getMessage(), e);
  353. }
  354. }
  355. /**
  356. * Gets the all raid boss status.
  357. * @return the all raid boss status
  358. */
  359. public String[] getAllRaidBossStatus()
  360. {
  361. final String[] msg = new String[(_bosses == null) ? 0 : _bosses.size()];
  362. if (_bosses == null)
  363. {
  364. msg[0] = "None";
  365. return msg;
  366. }
  367. int index = 0;
  368. for (int i : _bosses.keySet())
  369. {
  370. L2RaidBossInstance boss = _bosses.get(i);
  371. msg[index++] = boss.getName() + ": " + boss.getRaidStatus().name();
  372. }
  373. return msg;
  374. }
  375. /**
  376. * Gets the raid boss status.
  377. * @param bossId the boss id
  378. * @return the raid boss status
  379. */
  380. public String getRaidBossStatus(int bossId)
  381. {
  382. String msg = "RaidBoss Status..." + Config.EOL;
  383. if (_bosses == null)
  384. {
  385. msg += "None";
  386. return msg;
  387. }
  388. if (_bosses.containsKey(bossId))
  389. {
  390. final L2RaidBossInstance boss = _bosses.get(bossId);
  391. msg += boss.getName() + ": " + boss.getRaidStatus().name();
  392. }
  393. return msg;
  394. }
  395. /**
  396. * Gets the raid boss status id.
  397. * @param bossId the boss id
  398. * @return the raid boss status id
  399. */
  400. public StatusEnum getRaidBossStatusId(int bossId)
  401. {
  402. if (_bosses.containsKey(bossId))
  403. {
  404. return _bosses.get(bossId).getRaidStatus();
  405. }
  406. else if (_schedules.containsKey(bossId))
  407. {
  408. return StatusEnum.DEAD;
  409. }
  410. else
  411. {
  412. return StatusEnum.UNDEFINED;
  413. }
  414. }
  415. /**
  416. * Gets the valid template.
  417. * @param bossId the boss id
  418. * @return the valid template
  419. */
  420. public L2NpcTemplate getValidTemplate(int bossId)
  421. {
  422. final L2NpcTemplate template = NpcTable.getInstance().getTemplate(bossId);
  423. if (template == null)
  424. {
  425. return null;
  426. }
  427. if (!template.isType("L2RaidBoss"))
  428. {
  429. return null;
  430. }
  431. return template;
  432. }
  433. /**
  434. * Notify spawn night boss.
  435. * @param raidboss the raidboss
  436. */
  437. public void notifySpawnNightBoss(L2RaidBossInstance raidboss)
  438. {
  439. final StatsSet info = new StatsSet();
  440. info.set("currentHP", raidboss.getCurrentHp());
  441. info.set("currentMP", raidboss.getCurrentMp());
  442. info.set("respawnTime", 0L);
  443. raidboss.setRaidStatus(StatusEnum.ALIVE);
  444. _storedInfo.put(raidboss.getNpcId(), info);
  445. _log.info(getClass().getSimpleName() + ": Spawning Night Raid Boss " + raidboss.getName());
  446. _bosses.put(raidboss.getNpcId(), raidboss);
  447. }
  448. /**
  449. * Checks if the boss is defined.
  450. * @param bossId the boss id
  451. * @return {@code true} if is defined
  452. */
  453. public boolean isDefined(int bossId)
  454. {
  455. return _spawns.containsKey(bossId);
  456. }
  457. /**
  458. * Gets the bosses.
  459. * @return the bosses
  460. */
  461. public Map<Integer, L2RaidBossInstance> getBosses()
  462. {
  463. return _bosses;
  464. }
  465. /**
  466. * Gets the spawns.
  467. * @return the spawns
  468. */
  469. public Map<Integer, L2Spawn> getSpawns()
  470. {
  471. return _spawns;
  472. }
  473. /**
  474. * Gets the stored info.
  475. * @return the stored info
  476. */
  477. public Map<Integer, StatsSet> getStoredInfo()
  478. {
  479. return _storedInfo;
  480. }
  481. /**
  482. * Saves and clears the raid bosses status, including all schedules.
  483. */
  484. public void cleanUp()
  485. {
  486. updateDb();
  487. _bosses.clear();
  488. if (_schedules != null)
  489. {
  490. for (Integer bossId : _schedules.keySet())
  491. {
  492. ScheduledFuture<?> f = _schedules.get(bossId);
  493. f.cancel(true);
  494. }
  495. _schedules.clear();
  496. }
  497. _storedInfo.clear();
  498. _spawns.clear();
  499. }
  500. /**
  501. * Gets the single instance of RaidBossSpawnManager.
  502. * @return single instance of RaidBossSpawnManager
  503. */
  504. public static RaidBossSpawnManager getInstance()
  505. {
  506. return SingletonHolder._instance;
  507. }
  508. private static class SingletonHolder
  509. {
  510. protected static final RaidBossSpawnManager _instance = new RaidBossSpawnManager();
  511. }
  512. }