GrandBossManager.java 13 KB

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