RaidBossSpawnManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. /**
  17. @author godson
  18. **/
  19. import java.sql.Connection;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import java.sql.SQLException;
  23. import java.util.Calendar;
  24. import java.util.Map;
  25. import java.util.concurrent.ScheduledFuture;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.GmListTable;
  30. import com.l2jserver.gameserver.ThreadPoolManager;
  31. import com.l2jserver.gameserver.datatables.NpcTable;
  32. import com.l2jserver.gameserver.datatables.SpawnTable;
  33. import com.l2jserver.gameserver.model.L2Spawn;
  34. import com.l2jserver.gameserver.model.actor.instance.L2RaidBossInstance;
  35. import com.l2jserver.gameserver.templates.StatsSet;
  36. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  37. import com.l2jserver.util.Rnd;
  38. import javolution.util.FastMap;
  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. private 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<Integer, L2RaidBossInstance>();
  63. _schedules = new FastMap<Integer, ScheduledFuture<?>>();
  64. _storedInfo = new FastMap<Integer, StatsSet>();
  65. _spawns = new FastMap<Integer, L2Spawn>();
  66. Connection con = null;
  67. PreparedStatement statement = null;
  68. try
  69. {
  70. con = L2DatabaseFactory.getInstance().getConnection();
  71. statement = con.prepareStatement("SELECT * FROM raidboss_spawnlist ORDER BY boss_id");
  72. ResultSet rset = statement.executeQuery();
  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("RaidBossSpawnManager: Could not load raidboss #" + rset.getInt("boss_id") + " from DB");
  95. }
  96. }
  97. _log.info("RaidBossSpawnManager: Loaded " + _bosses.size() + " Instances");
  98. _log.info("RaidBossSpawnManager: Scheduled " + _schedules.size() + " Instances");
  99. rset.close();
  100. }
  101. catch (SQLException e)
  102. {
  103. _log.warning("RaidBossSpawnManager: Couldnt load raidboss_spawnlist table");
  104. }
  105. catch (Exception e)
  106. {
  107. e.printStackTrace();
  108. }
  109. finally
  110. {
  111. try
  112. {
  113. statement.close();
  114. }
  115. catch (Exception e)
  116. {
  117. }
  118. try
  119. {
  120. con.close();
  121. }
  122. catch (Exception e)
  123. {
  124. }
  125. }
  126. }
  127. private class spawnSchedule implements Runnable
  128. {
  129. private final int bossId;
  130. public spawnSchedule(int npcId)
  131. {
  132. bossId = npcId;
  133. }
  134. public void run()
  135. {
  136. L2RaidBossInstance raidboss = null;
  137. if (bossId == 25328)
  138. raidboss = DayNightSpawnManager.getInstance().handleBoss(_spawns.get(bossId));
  139. else
  140. raidboss = (L2RaidBossInstance) _spawns.get(bossId).doSpawn();
  141. if (raidboss != null)
  142. {
  143. raidboss.setRaidStatus(StatusEnum.ALIVE);
  144. StatsSet info = new StatsSet();
  145. info.set("currentHP", raidboss.getCurrentHp());
  146. info.set("currentMP", raidboss.getCurrentMp());
  147. info.set("respawnTime", 0L);
  148. _storedInfo.put(bossId, info);
  149. GmListTable.broadcastMessageToGMs("Spawning Raid Boss " + raidboss.getName());
  150. _bosses.put(bossId, raidboss);
  151. }
  152. _schedules.remove(bossId);
  153. }
  154. }
  155. public void updateStatus(L2RaidBossInstance boss, boolean isBossDead)
  156. {
  157. if (!_storedInfo.containsKey(boss.getNpcId()))
  158. return;
  159. StatsSet info = _storedInfo.get(boss.getNpcId());
  160. if (isBossDead)
  161. {
  162. boss.setRaidStatus(StatusEnum.DEAD);
  163. long respawnTime;
  164. int RespawnMinDelay = boss.getSpawn().getRespawnMinDelay();
  165. int RespawnMaxDelay = boss.getSpawn().getRespawnMaxDelay();
  166. long respawn_delay = Rnd.get((int) (RespawnMinDelay * 1000 * Config.RAID_MIN_RESPAWN_MULTIPLIER), (int) (RespawnMaxDelay * 1000 * Config.RAID_MAX_RESPAWN_MULTIPLIER));
  167. respawnTime = Calendar.getInstance().getTimeInMillis() + respawn_delay;
  168. info.set("currentHP", boss.getMaxHp());
  169. info.set("currentMP", boss.getMaxMp());
  170. info.set("respawnTime", respawnTime);
  171. Calendar time = Calendar.getInstance();
  172. time.setTimeInMillis(respawnTime);
  173. _log.info("RaidBossSpawnManager: Updated " + boss.getName() + " respawn time to " + time.getTime());
  174. if (!_schedules.containsKey(boss.getNpcId()))
  175. {
  176. ScheduledFuture<?> futureSpawn;
  177. futureSpawn = ThreadPoolManager.getInstance().scheduleGeneral(new spawnSchedule(boss.getNpcId()), respawn_delay);
  178. _schedules.put(boss.getNpcId(), futureSpawn);
  179. //To update immediately Database uncomment on the following line, to post the hour of respawn raid boss on your site for example or to envisage a crash landing of the waiter.
  180. updateDb();
  181. }
  182. }
  183. else
  184. {
  185. boss.setRaidStatus(StatusEnum.ALIVE);
  186. info.set("currentHP", boss.getCurrentHp());
  187. info.set("currentMP", boss.getCurrentMp());
  188. info.set("respawnTime", 0L);
  189. }
  190. _storedInfo.remove(boss.getNpcId());
  191. _storedInfo.put(boss.getNpcId(), info);
  192. }
  193. public void addNewSpawn(L2Spawn spawnDat, long respawnTime, double currentHP, double currentMP, boolean storeInDb)
  194. {
  195. if (spawnDat == null)
  196. return;
  197. if (_spawns.containsKey(spawnDat.getNpcid()))
  198. return;
  199. int bossId = spawnDat.getNpcid();
  200. long time = Calendar.getInstance().getTimeInMillis();
  201. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  202. if (respawnTime == 0L || (time > respawnTime))
  203. {
  204. L2RaidBossInstance raidboss = null;
  205. if (bossId == 25328)
  206. raidboss = DayNightSpawnManager.getInstance().handleBoss(spawnDat);
  207. else
  208. raidboss = (L2RaidBossInstance) spawnDat.doSpawn();
  209. if (raidboss != null)
  210. {
  211. raidboss.setCurrentHp(currentHP);
  212. raidboss.setCurrentMp(currentMP);
  213. raidboss.setRaidStatus(StatusEnum.ALIVE);
  214. _bosses.put(bossId, raidboss);
  215. StatsSet info = new StatsSet();
  216. info.set("currentHP", currentHP);
  217. info.set("currentMP", currentMP);
  218. info.set("respawnTime", 0L);
  219. _storedInfo.put(bossId, info);
  220. }
  221. }
  222. else
  223. {
  224. ScheduledFuture<?> futureSpawn;
  225. long spawnTime = respawnTime - Calendar.getInstance().getTimeInMillis();
  226. futureSpawn = ThreadPoolManager.getInstance().scheduleGeneral(new spawnSchedule(bossId), spawnTime);
  227. _schedules.put(bossId, futureSpawn);
  228. }
  229. _spawns.put(bossId, spawnDat);
  230. if (storeInDb)
  231. {
  232. Connection con = null;
  233. PreparedStatement statement = null;
  234. try
  235. {
  236. con = L2DatabaseFactory.getInstance().getConnection();
  237. statement = con.prepareStatement("INSERT INTO raidboss_spawnlist (boss_id,amount,loc_x,loc_y,loc_z,heading,respawn_time,currentHp,currentMp) VALUES(?,?,?,?,?,?,?,?,?)");
  238. statement.setInt(1, spawnDat.getNpcid());
  239. statement.setInt(2, spawnDat.getAmount());
  240. statement.setInt(3, spawnDat.getLocx());
  241. statement.setInt(4, spawnDat.getLocy());
  242. statement.setInt(5, spawnDat.getLocz());
  243. statement.setInt(6, spawnDat.getHeading());
  244. statement.setLong(7, respawnTime);
  245. statement.setDouble(8, currentHP);
  246. statement.setDouble(9, currentMP);
  247. statement.execute();
  248. }
  249. catch (Exception e)
  250. {
  251. // problem with storing spawn
  252. _log.warning("RaidBossSpawnManager: Could not store raidboss #" + bossId + " in the DB:" + e);
  253. }
  254. finally
  255. {
  256. try
  257. {
  258. statement.close();
  259. }
  260. catch (Exception e)
  261. {
  262. }
  263. try
  264. {
  265. con.close();
  266. }
  267. catch (Exception e)
  268. {
  269. }
  270. }
  271. }
  272. }
  273. public void deleteSpawn(L2Spawn spawnDat, boolean updateDb)
  274. {
  275. if (spawnDat == null)
  276. return;
  277. if (!_spawns.containsKey(spawnDat.getNpcid()))
  278. return;
  279. int bossId = spawnDat.getNpcid();
  280. SpawnTable.getInstance().deleteSpawn(spawnDat, false);
  281. _spawns.remove(bossId);
  282. if (_bosses.containsKey(bossId))
  283. _bosses.remove(bossId);
  284. if (_schedules.containsKey(bossId))
  285. {
  286. ScheduledFuture<?> f = _schedules.get(bossId);
  287. f.cancel(true);
  288. _schedules.remove(bossId);
  289. }
  290. if (_storedInfo.containsKey(bossId))
  291. _storedInfo.remove(bossId);
  292. if (updateDb)
  293. {
  294. Connection con = null;
  295. PreparedStatement statement = null;
  296. try
  297. {
  298. con = L2DatabaseFactory.getInstance().getConnection();
  299. statement = con.prepareStatement("DELETE FROM raidboss_spawnlist WHERE boss_id=?");
  300. statement.setInt(1, bossId);
  301. statement.execute();
  302. }
  303. catch (Exception e)
  304. {
  305. // problem with deleting spawn
  306. _log.warning("RaidBossSpawnManager: Could not remove raidboss #" + bossId + " from DB: " + e);
  307. }
  308. finally
  309. {
  310. try
  311. {
  312. statement.close();
  313. }
  314. catch (Exception e)
  315. {
  316. }
  317. try
  318. {
  319. con.close();
  320. }
  321. catch (Exception e)
  322. {
  323. }
  324. }
  325. }
  326. }
  327. private void updateDb()
  328. {
  329. for (Integer bossId : _storedInfo.keySet())
  330. {
  331. Connection con = null;
  332. PreparedStatement statement = null;
  333. try
  334. {
  335. con = L2DatabaseFactory.getInstance().getConnection();
  336. L2RaidBossInstance boss = _bosses.get(bossId);
  337. if (boss == null)
  338. continue;
  339. if (boss.getRaidStatus().equals(StatusEnum.ALIVE))
  340. updateStatus(boss, false);
  341. StatsSet info = _storedInfo.get(bossId);
  342. if (info == null)
  343. continue;
  344. statement = con.prepareStatement("UPDATE raidboss_spawnlist SET respawn_time = ?, currentHP = ?, currentMP = ? WHERE boss_id = ?");
  345. statement.setLong(1, info.getLong("respawnTime"));
  346. statement.setDouble(2, info.getDouble("currentHP"));
  347. statement.setDouble(3, info.getDouble("currentMP"));
  348. statement.setInt(4, bossId);
  349. statement.execute();
  350. }
  351. catch (SQLException e)
  352. {
  353. _log.warning("RaidBossSpawnManager: Couldnt update raidboss_spawnlist table " + e);
  354. }
  355. finally
  356. {
  357. try
  358. {
  359. statement.close();
  360. }
  361. catch (Exception e)
  362. {
  363. }
  364. try
  365. {
  366. con.close();
  367. }
  368. catch (Exception e)
  369. {
  370. }
  371. }
  372. }
  373. }
  374. public String[] getAllRaidBossStatus()
  375. {
  376. String[] msg = new String[_bosses == null ? 0 : _bosses.size()];
  377. if (_bosses == null)
  378. {
  379. msg[0] = "None";
  380. return msg;
  381. }
  382. int index = 0;
  383. for (int i : _bosses.keySet())
  384. {
  385. L2RaidBossInstance boss = _bosses.get(i);
  386. msg[index++] = boss.getName() + ": " + boss.getRaidStatus().name();
  387. }
  388. return msg;
  389. }
  390. public String getRaidBossStatus(int bossId)
  391. {
  392. String msg = "RaidBoss Status....\n";
  393. if (_bosses == null)
  394. {
  395. msg += "None";
  396. return msg;
  397. }
  398. if (_bosses.containsKey(bossId))
  399. {
  400. L2RaidBossInstance boss = _bosses.get(bossId);
  401. msg += boss.getName() + ": " + boss.getRaidStatus().name();
  402. }
  403. return msg;
  404. }
  405. public StatusEnum getRaidBossStatusId(int bossId)
  406. {
  407. if (_bosses.containsKey(bossId))
  408. return _bosses.get(bossId).getRaidStatus();
  409. else if (_schedules.containsKey(bossId))
  410. return StatusEnum.DEAD;
  411. else
  412. return StatusEnum.UNDEFINED;
  413. }
  414. public L2NpcTemplate getValidTemplate(int bossId)
  415. {
  416. L2NpcTemplate template = NpcTable.getInstance().getTemplate(bossId);
  417. if (template == null)
  418. return null;
  419. if (!template.type.equalsIgnoreCase("L2RaidBoss"))
  420. return null;
  421. return template;
  422. }
  423. public void notifySpawnNightBoss(L2RaidBossInstance raidboss)
  424. {
  425. StatsSet info = new StatsSet();
  426. info.set("currentHP", raidboss.getCurrentHp());
  427. info.set("currentMP", raidboss.getCurrentMp());
  428. info.set("respawnTime", 0L);
  429. raidboss.setRaidStatus(StatusEnum.ALIVE);
  430. _storedInfo.put(raidboss.getNpcId(), info);
  431. GmListTable.broadcastMessageToGMs("Spawning Raid Boss " + raidboss.getName());
  432. _bosses.put(raidboss.getNpcId(), raidboss);
  433. }
  434. public boolean isDefined(int bossId)
  435. {
  436. return _spawns.containsKey(bossId);
  437. }
  438. public Map<Integer, L2RaidBossInstance> getBosses()
  439. {
  440. return _bosses;
  441. }
  442. public Map<Integer, L2Spawn> getSpawns()
  443. {
  444. return _spawns;
  445. }
  446. public void reloadBosses()
  447. {
  448. init();
  449. }
  450. /**
  451. * Saves all raidboss status and then clears all info from memory,
  452. * including all schedules.
  453. */
  454. public void cleanUp()
  455. {
  456. updateDb();
  457. _bosses.clear();
  458. if (_schedules != null)
  459. {
  460. for (Integer bossId : _schedules.keySet())
  461. {
  462. ScheduledFuture<?> f = _schedules.get(bossId);
  463. f.cancel(true);
  464. }
  465. }
  466. _schedules.clear();
  467. _storedInfo.clear();
  468. _spawns.clear();
  469. }
  470. @SuppressWarnings("synthetic-access")
  471. private static class SingletonHolder
  472. {
  473. protected static final RaidBossSpawnManager _instance = new RaidBossSpawnManager();
  474. }
  475. }