RaidBossSpawnManager.java 15 KB

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