GrandBossManager.java 13 KB

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