RaidBossSpawnManager.java 15 KB

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