GrandBossManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 gnu.trove.TIntIntHashMap;
  17. import gnu.trove.TIntObjectHashMap;
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.util.Date;
  23. import java.util.Map;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastMap;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.datatables.NpcTable;
  29. import com.l2jserver.gameserver.model.L2Object;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.actor.instance.L2GrandBossInstance;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.zone.type.L2BossZone;
  34. import com.l2jserver.gameserver.templates.StatsSet;
  35. import com.l2jserver.util.L2FastList;
  36. /**
  37. * @author DaRkRaGe
  38. * Revised by Emperorc
  39. */
  40. public class GrandBossManager
  41. {
  42. /* =========================================================
  43. * This class handles all Grand Bosses:
  44. * <ul>
  45. * <li>25333-25338 Anakazel</li>
  46. * <li>29001 Queen Ant</li>
  47. * <li>29006 Core</li>
  48. * <li>29014 Orfen</li>
  49. * <li>29019 Antharas</li>
  50. * <li>29020 Baium</li>
  51. * <li>29022 Zaken</li>
  52. * <li>29028 Valakas</li>
  53. * <li>29045 Frintezza</li>
  54. * <li>29046-29047 Scarlet van Halisha</li>
  55. * </ul>
  56. *
  57. * It handles the saving of hp, mp, location, and status
  58. * of all Grand Bosses. It also manages the zones associated
  59. * with the Grand Bosses.
  60. * NOTE: The current version does NOT spawn the Grand Bosses,
  61. * it just stores and retrieves the values on reboot/startup,
  62. * for AI scripts to utilize as needed.
  63. */
  64. /**
  65. * DELETE FROM grandboss_list
  66. */
  67. private static final String DELETE_GRAND_BOSS_LIST = "DELETE FROM grandboss_list";
  68. /**
  69. * INSERT INTO grandboss_list (player_id,zone) VALUES (?,?)
  70. */
  71. private static final String INSERT_GRAND_BOSS_LIST = "INSERT INTO grandboss_list (player_id,zone) VALUES (?,?)";
  72. /**
  73. * UPDATE grandboss_data set loc_x = ?, loc_y = ?, loc_z = ?, heading = ?, respawn_time = ?, currentHP = ?, currentMP = ?, status = ? where boss_id = ?
  74. */
  75. private static final String UPDATE_GRAND_BOSS_DATA = "UPDATE grandboss_data set loc_x = ?, loc_y = ?, loc_z = ?, heading = ?, respawn_time = ?, currentHP = ?, currentMP = ?, status = ? where boss_id = ?";
  76. private static final String UPDATE_GRAND_BOSS_DATA2 = "UPDATE grandboss_data set status = ? where boss_id = ?";
  77. protected static Logger _log = Logger.getLogger(GrandBossManager.class.getName());
  78. protected static Map<Integer, L2GrandBossInstance> _bosses;
  79. protected static TIntObjectHashMap<StatsSet> _storedInfo;
  80. private TIntIntHashMap _bossStatus;
  81. private L2FastList<L2BossZone> _zones;
  82. public static GrandBossManager getInstance()
  83. {
  84. return SingletonHolder._instance;
  85. }
  86. private GrandBossManager()
  87. {
  88. _log.info("Initializing GrandBossManager");
  89. init();
  90. }
  91. private void init()
  92. {
  93. _zones = new L2FastList<L2BossZone>();
  94. _bosses = new FastMap<Integer, L2GrandBossInstance>();
  95. _storedInfo = new TIntObjectHashMap<StatsSet>();
  96. _bossStatus = new TIntIntHashMap();
  97. Connection con = null;
  98. try
  99. {
  100. con = L2DatabaseFactory.getInstance().getConnection();
  101. PreparedStatement statement = con.prepareStatement("SELECT * from grandboss_data ORDER BY boss_id");
  102. ResultSet rset = statement.executeQuery();
  103. while (rset.next())
  104. {
  105. //Read all info from DB, and store it for AI to read and decide what to do
  106. //faster than accessing DB in real time
  107. StatsSet info = new StatsSet();
  108. int bossId = rset.getInt("boss_id");
  109. info.set("loc_x", rset.getInt("loc_x"));
  110. info.set("loc_y", rset.getInt("loc_y"));
  111. info.set("loc_z", rset.getInt("loc_z"));
  112. info.set("heading", rset.getInt("heading"));
  113. info.set("respawn_time", rset.getLong("respawn_time"));
  114. double HP = rset.getDouble("currentHP"); //jython doesn't recognize doubles
  115. int true_HP = (int) HP; //so use java's ability to type cast
  116. info.set("currentHP", true_HP); //to convert double to int
  117. double MP = rset.getDouble("currentMP");
  118. int true_MP = (int) MP;
  119. info.set("currentMP", true_MP);
  120. int status = rset.getInt("status");
  121. _bossStatus.put(bossId, status);
  122. _storedInfo.put(bossId, info);
  123. _log.info("GrandBossManager: " +NpcTable.getInstance().getTemplate(bossId).getName()+"(" +bossId+ ") status is "+ status+".");
  124. if (status > 0)
  125. _log.info("GrandBossManager: Next spawn date of " +NpcTable.getInstance().getTemplate(bossId).getName()+" is "+ new Date(info.getLong("respawn_time"))+".");
  126. info = null;
  127. }
  128. _log.info("GrandBossManager: Loaded " + _storedInfo.size() + " Instances");
  129. rset.close();
  130. statement.close();
  131. }
  132. catch (SQLException e)
  133. {
  134. _log.log(Level.WARNING, "GrandBossManager: Could not load grandboss_data table: " + e.getMessage(), e);
  135. }
  136. catch (Exception e)
  137. {
  138. _log.log(Level.WARNING, "Error while initializing GrandBossManager: " + e.getMessage(), e);
  139. }
  140. finally
  141. {
  142. L2DatabaseFactory.close(con);
  143. }
  144. }
  145. /**
  146. * Zone Functions
  147. */
  148. public void initZones()
  149. {
  150. Connection con = null;
  151. FastMap<Integer, L2FastList<Integer>> zones = new FastMap<Integer, L2FastList<Integer>>();
  152. if (_zones == null)
  153. {
  154. _log.warning("GrandBossManager: Could not read Grand Boss zone data");
  155. return;
  156. }
  157. for (L2BossZone zone : _zones)
  158. {
  159. if (zone == null)
  160. continue;
  161. zones.put(zone.getId(), new L2FastList<Integer>());
  162. }
  163. try
  164. {
  165. con = L2DatabaseFactory.getInstance().getConnection();
  166. PreparedStatement statement = con.prepareStatement("SELECT * from grandboss_list ORDER BY player_id");
  167. ResultSet rset = statement.executeQuery();
  168. while (rset.next())
  169. {
  170. int id = rset.getInt("player_id");
  171. int zone_id = rset.getInt("zone");
  172. zones.get(zone_id).add(id);
  173. }
  174. rset.close();
  175. statement.close();
  176. _log.info("GrandBossManager: Initialized " + _zones.size() + " Grand Boss Zones");
  177. }
  178. catch (SQLException e)
  179. {
  180. _log.log(Level.WARNING, "GrandBossManager: Could not load grandboss_list table: " + e.getMessage(), e);
  181. }
  182. catch (Exception e)
  183. {
  184. _log.log(Level.WARNING, "Error while initializing GrandBoss zones: " + e.getMessage(), e);
  185. }
  186. finally
  187. {
  188. L2DatabaseFactory.close(con);
  189. }
  190. for (L2BossZone zone : _zones)
  191. {
  192. if (zone == null)
  193. continue;
  194. zone.setAllowedPlayers(zones.get(zone.getId()));
  195. }
  196. zones.clear();
  197. }
  198. public void addZone(L2BossZone zone)
  199. {
  200. if (_zones != null)
  201. {
  202. _zones.add(zone);
  203. }
  204. }
  205. public final L2BossZone getZone(L2Character character)
  206. {
  207. if (_zones != null)
  208. {
  209. for (L2BossZone temp : _zones)
  210. {
  211. if (temp.isCharacterInZone(character))
  212. return temp;
  213. }
  214. }
  215. return null;
  216. }
  217. public final L2BossZone getZone(int x, int y, int z)
  218. {
  219. if (_zones != null)
  220. {
  221. for (L2BossZone temp : _zones)
  222. {
  223. if (temp.isInsideZone(x, y, z))
  224. return temp;
  225. }
  226. }
  227. return null;
  228. }
  229. public boolean checkIfInZone(String zoneType, L2Object obj)
  230. {
  231. L2BossZone temp = getZone(obj.getX(), obj.getY(), obj.getZ());
  232. if (temp == null)
  233. return false;
  234. return temp.getName().equalsIgnoreCase(zoneType);
  235. }
  236. public boolean checkIfInZone(L2PcInstance player)
  237. {
  238. if (player == null)
  239. return false;
  240. L2BossZone temp = getZone(player.getX(), player.getY(), player.getZ());
  241. if (temp == null)
  242. return false;
  243. return true;
  244. }
  245. public int getBossStatus(int bossId)
  246. {
  247. return _bossStatus.get(bossId);
  248. }
  249. public void setBossStatus(int bossId, int status)
  250. {
  251. _bossStatus.put(bossId, status);
  252. _log.info(getClass().getSimpleName()+": Updated "+NpcTable.getInstance().getTemplate(bossId).getName()+"(" +bossId+ ") status to " +status);
  253. updateDb(bossId, true);
  254. }
  255. /**
  256. * Adds a L2GrandBossInstance to the list of bosses.
  257. */
  258. public void addBoss(L2GrandBossInstance boss)
  259. {
  260. if (boss != null)
  261. {
  262. _bosses.put(boss.getNpcId(), boss);
  263. }
  264. }
  265. public L2GrandBossInstance getBoss(int bossId)
  266. {
  267. return _bosses.get(bossId);
  268. }
  269. public StatsSet getStatsSet(int bossId)
  270. {
  271. return _storedInfo.get(bossId);
  272. }
  273. public void setStatsSet(int bossId, StatsSet info)
  274. {
  275. _storedInfo.put(bossId, info);
  276. updateDb(bossId, false);
  277. }
  278. private void storeToDb()
  279. {
  280. Connection con = null;
  281. try
  282. {
  283. con = L2DatabaseFactory.getInstance().getConnection();
  284. PreparedStatement statement = con.prepareStatement(DELETE_GRAND_BOSS_LIST);
  285. statement.executeUpdate();
  286. statement.close();
  287. statement = con.prepareStatement(INSERT_GRAND_BOSS_LIST);
  288. for (L2BossZone zone : _zones)
  289. {
  290. if (zone == null)
  291. continue;
  292. Integer id = zone.getId();
  293. L2FastList<Integer> list = zone.getAllowedPlayers();
  294. if (list == null || list.isEmpty())
  295. continue;
  296. for (Integer player : list)
  297. {
  298. statement.setInt(1, player);
  299. statement.setInt(2, id);
  300. statement.executeUpdate();
  301. statement.clearParameters();
  302. }
  303. }
  304. statement.close();
  305. for (Integer bossId : _storedInfo.keys())
  306. {
  307. final L2GrandBossInstance boss = _bosses.get(bossId);
  308. StatsSet info = _storedInfo.get(bossId);
  309. if (boss == null || info == null)
  310. {
  311. statement = con.prepareStatement(UPDATE_GRAND_BOSS_DATA2);
  312. statement.setInt(1, _bossStatus.get(bossId));
  313. statement.setInt(2, bossId);
  314. }
  315. else
  316. {
  317. statement = con.prepareStatement(UPDATE_GRAND_BOSS_DATA);
  318. statement.setInt(1, boss.getX());
  319. statement.setInt(2, boss.getY());
  320. statement.setInt(3, boss.getZ());
  321. statement.setInt(4, boss.getHeading());
  322. statement.setLong(5, info.getLong("respawn_time"));
  323. double hp = boss.getCurrentHp();
  324. double mp = boss.getCurrentMp();
  325. if (boss.isDead())
  326. {
  327. hp = boss.getMaxHp();
  328. mp = boss.getMaxMp();
  329. }
  330. statement.setDouble(6, hp);
  331. statement.setDouble(7, mp);
  332. statement.setInt(8, _bossStatus.get(bossId));
  333. statement.setInt(9, bossId);
  334. }
  335. statement.executeUpdate();
  336. statement.clearParameters();
  337. statement.close();
  338. }
  339. }
  340. catch (SQLException e)
  341. {
  342. _log.log(Level.WARNING, "GrandBossManager: Couldn't store grandbosses to database:" + e.getMessage(), e);
  343. }
  344. finally
  345. {
  346. L2DatabaseFactory.close(con);
  347. }
  348. }
  349. private void updateDb(int bossId, boolean statusOnly)
  350. {
  351. Connection con = null;
  352. try
  353. {
  354. con = L2DatabaseFactory.getInstance().getConnection();
  355. PreparedStatement statement = null;
  356. L2GrandBossInstance boss = _bosses.get(bossId);
  357. StatsSet info = _storedInfo.get(bossId);
  358. if (statusOnly || boss == null || info == null)
  359. {
  360. statement = con.prepareStatement(UPDATE_GRAND_BOSS_DATA2);
  361. statement.setInt(1, _bossStatus.get(bossId));
  362. statement.setInt(2, bossId);
  363. }
  364. else
  365. {
  366. statement = con.prepareStatement(UPDATE_GRAND_BOSS_DATA);
  367. statement.setInt(1, boss.getX());
  368. statement.setInt(2, boss.getY());
  369. statement.setInt(3, boss.getZ());
  370. statement.setInt(4, boss.getHeading());
  371. statement.setLong(5, info.getLong("respawn_time"));
  372. double hp = boss.getCurrentHp();
  373. double mp = boss.getCurrentMp();
  374. if (boss.isDead())
  375. {
  376. hp = boss.getMaxHp();
  377. mp = boss.getMaxMp();
  378. }
  379. statement.setDouble(6, hp);
  380. statement.setDouble(7, mp);
  381. statement.setInt(8, _bossStatus.get(bossId));
  382. statement.setInt(9, bossId);
  383. }
  384. statement.executeUpdate();
  385. statement.close();
  386. }
  387. catch (SQLException e)
  388. {
  389. _log.log(Level.WARNING, "GrandBossManager: Couldn't update grandbosses to database:" + e.getMessage(), e);
  390. }
  391. finally
  392. {
  393. L2DatabaseFactory.close(con);
  394. }
  395. }
  396. /**
  397. * Saves all Grand Boss info and then clears all info from memory,
  398. * including all schedules.
  399. */
  400. public void cleanUp()
  401. {
  402. storeToDb();
  403. _bosses.clear();
  404. _storedInfo.clear();
  405. _bossStatus.clear();
  406. _zones.clear();
  407. }
  408. public L2FastList<L2BossZone> getZones()
  409. {
  410. return _zones;
  411. }
  412. @SuppressWarnings("synthetic-access")
  413. private static class SingletonHolder
  414. {
  415. protected static final GrandBossManager _instance = new GrandBossManager();
  416. }
  417. }