RaidBossSpawnManager.java 14 KB

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