GrandBossManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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.map.hash.TIntIntHashMap;
  17. import gnu.trove.map.hash.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.Location;
  31. import com.l2jserver.gameserver.model.StatsSet;
  32. import com.l2jserver.gameserver.model.actor.L2Character;
  33. import com.l2jserver.gameserver.model.actor.instance.L2GrandBossInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  35. import com.l2jserver.gameserver.model.zone.type.L2BossZone;
  36. import com.l2jserver.util.L2FastList;
  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>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. protected GrandBossManager()
  88. {
  89. init();
  90. }
  91. private void init()
  92. {
  93. _zones = new L2FastList<>();
  94. _bosses = new FastMap<>();
  95. _storedInfo = new TIntObjectHashMap<>();
  96. _bossStatus = new TIntIntHashMap();
  97. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  98. {
  99. PreparedStatement statement = con.prepareStatement("SELECT * from grandboss_data ORDER BY boss_id");
  100. ResultSet rset = statement.executeQuery();
  101. while (rset.next())
  102. {
  103. //Read all info from DB, and store it for AI to read and decide what to do
  104. //faster than accessing DB in real time
  105. StatsSet info = new StatsSet();
  106. int bossId = rset.getInt("boss_id");
  107. info.set("loc_x", rset.getInt("loc_x"));
  108. info.set("loc_y", rset.getInt("loc_y"));
  109. info.set("loc_z", rset.getInt("loc_z"));
  110. info.set("heading", rset.getInt("heading"));
  111. info.set("respawn_time", rset.getLong("respawn_time"));
  112. double HP = rset.getDouble("currentHP"); //jython doesn't recognize doubles
  113. int true_HP = (int) HP; //so use java's ability to type cast
  114. info.set("currentHP", true_HP); //to convert double to int
  115. double MP = rset.getDouble("currentMP");
  116. int true_MP = (int) MP;
  117. info.set("currentMP", true_MP);
  118. int status = rset.getInt("status");
  119. _bossStatus.put(bossId, status);
  120. _storedInfo.put(bossId, info);
  121. _log.info("GrandBossManager: " +NpcTable.getInstance().getTemplate(bossId).getName()+"(" +bossId+ ") status is "+ status+".");
  122. if (status > 0)
  123. _log.info("GrandBossManager: Next spawn date of " +NpcTable.getInstance().getTemplate(bossId).getName()+" is "+ new Date(info.getLong("respawn_time"))+".");
  124. info = null;
  125. }
  126. _log.info("GrandBossManager: Loaded " + _storedInfo.size() + " Instances");
  127. rset.close();
  128. statement.close();
  129. }
  130. catch (SQLException e)
  131. {
  132. _log.log(Level.WARNING, "GrandBossManager: Could not load grandboss_data table: " + e.getMessage(), e);
  133. }
  134. catch (Exception e)
  135. {
  136. _log.log(Level.WARNING, "Error while initializing GrandBossManager: " + e.getMessage(), e);
  137. }
  138. }
  139. /**
  140. * Zone Functions
  141. */
  142. public void initZones()
  143. {
  144. FastMap<Integer, L2FastList<Integer>> zones = new FastMap<>();
  145. if (_zones == null)
  146. {
  147. _log.warning("GrandBossManager: Could not read Grand Boss zone data");
  148. return;
  149. }
  150. for (L2BossZone zone : _zones)
  151. {
  152. if (zone == null)
  153. continue;
  154. zones.put(zone.getId(), new L2FastList<Integer>());
  155. }
  156. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  157. {
  158. PreparedStatement statement = con.prepareStatement("SELECT * from grandboss_list ORDER BY player_id");
  159. ResultSet rset = statement.executeQuery();
  160. while (rset.next())
  161. {
  162. int id = rset.getInt("player_id");
  163. int zone_id = rset.getInt("zone");
  164. zones.get(zone_id).add(id);
  165. }
  166. rset.close();
  167. statement.close();
  168. _log.info("GrandBossManager: Initialized " + _zones.size() + " Grand Boss Zones");
  169. }
  170. catch (SQLException e)
  171. {
  172. _log.log(Level.WARNING, "GrandBossManager: Could not load grandboss_list table: " + e.getMessage(), e);
  173. }
  174. catch (Exception e)
  175. {
  176. _log.log(Level.WARNING, "Error while initializing GrandBoss zones: " + e.getMessage(), e);
  177. }
  178. for (L2BossZone zone : _zones)
  179. {
  180. if (zone == null)
  181. continue;
  182. zone.setAllowedPlayers(zones.get(zone.getId()));
  183. }
  184. zones.clear();
  185. }
  186. public void addZone(L2BossZone zone)
  187. {
  188. if (_zones != null)
  189. {
  190. _zones.add(zone);
  191. }
  192. }
  193. public final L2BossZone getZone(L2Character character)
  194. {
  195. if (_zones != null)
  196. {
  197. for (L2BossZone temp : _zones)
  198. {
  199. if (temp.isCharacterInZone(character))
  200. return temp;
  201. }
  202. }
  203. return null;
  204. }
  205. public final L2BossZone getZone(Location loc)
  206. {
  207. return getZone(loc.getX(), loc.getY(), loc.getZ());
  208. }
  209. public final L2BossZone getZone(int x, int y, int z)
  210. {
  211. if (_zones != null)
  212. {
  213. for (L2BossZone temp : _zones)
  214. {
  215. if (temp.isInsideZone(x, y, z))
  216. return temp;
  217. }
  218. }
  219. return null;
  220. }
  221. public boolean checkIfInZone(String zoneType, L2Object obj)
  222. {
  223. L2BossZone temp = getZone(obj.getX(), obj.getY(), obj.getZ());
  224. if (temp == null)
  225. return false;
  226. return temp.getName().equalsIgnoreCase(zoneType);
  227. }
  228. public boolean checkIfInZone(L2PcInstance player)
  229. {
  230. if (player == null)
  231. return false;
  232. L2BossZone temp = getZone(player.getX(), player.getY(), player.getZ());
  233. if (temp == null)
  234. return false;
  235. return true;
  236. }
  237. public int getBossStatus(int bossId)
  238. {
  239. return _bossStatus.get(bossId);
  240. }
  241. public void setBossStatus(int bossId, int status)
  242. {
  243. _bossStatus.put(bossId, status);
  244. _log.info(getClass().getSimpleName()+": Updated "+NpcTable.getInstance().getTemplate(bossId).getName()+"(" +bossId+ ") status to " +status);
  245. updateDb(bossId, true);
  246. }
  247. /**
  248. * Adds a L2GrandBossInstance to the list of bosses.
  249. * @param boss
  250. */
  251. public void addBoss(L2GrandBossInstance boss)
  252. {
  253. if (boss != null)
  254. {
  255. _bosses.put(boss.getNpcId(), boss);
  256. }
  257. }
  258. public L2GrandBossInstance getBoss(int bossId)
  259. {
  260. return _bosses.get(bossId);
  261. }
  262. public StatsSet getStatsSet(int bossId)
  263. {
  264. return _storedInfo.get(bossId);
  265. }
  266. public void setStatsSet(int bossId, StatsSet info)
  267. {
  268. _storedInfo.put(bossId, info);
  269. updateDb(bossId, false);
  270. }
  271. private void storeToDb()
  272. {
  273. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  274. PreparedStatement delete = con.prepareStatement(DELETE_GRAND_BOSS_LIST))
  275. {
  276. delete.executeUpdate();
  277. try (PreparedStatement insert = con.prepareStatement(INSERT_GRAND_BOSS_LIST))
  278. {
  279. for (L2BossZone zone : _zones)
  280. {
  281. if (zone == null)
  282. continue;
  283. Integer id = zone.getId();
  284. L2FastList<Integer> list = zone.getAllowedPlayers();
  285. if (list == null || list.isEmpty())
  286. continue;
  287. for (Integer player : list)
  288. {
  289. insert.setInt(1, player);
  290. insert.setInt(2, id);
  291. insert.executeUpdate();
  292. insert.clearParameters();
  293. }
  294. }
  295. }
  296. for (Integer bossId : _storedInfo.keys())
  297. {
  298. final L2GrandBossInstance boss = _bosses.get(bossId);
  299. StatsSet info = _storedInfo.get(bossId);
  300. if (boss == null || info == null)
  301. {
  302. try (PreparedStatement update = con.prepareStatement(UPDATE_GRAND_BOSS_DATA2))
  303. {
  304. update.setInt(1, _bossStatus.get(bossId));
  305. update.setInt(2, bossId);
  306. update.executeUpdate();
  307. update.clearParameters();
  308. }
  309. }
  310. else
  311. {
  312. try (PreparedStatement update = con.prepareStatement(UPDATE_GRAND_BOSS_DATA))
  313. {
  314. update.setInt(1, boss.getX());
  315. update.setInt(2, boss.getY());
  316. update.setInt(3, boss.getZ());
  317. update.setInt(4, boss.getHeading());
  318. update.setLong(5, info.getLong("respawn_time"));
  319. double hp = boss.getCurrentHp();
  320. double mp = boss.getCurrentMp();
  321. if (boss.isDead())
  322. {
  323. hp = boss.getMaxHp();
  324. mp = boss.getMaxMp();
  325. }
  326. update.setDouble(6, hp);
  327. update.setDouble(7, mp);
  328. update.setInt(8, _bossStatus.get(bossId));
  329. update.setInt(9, bossId);
  330. update.executeUpdate();
  331. update.clearParameters();
  332. }
  333. }
  334. }
  335. }
  336. catch (SQLException e)
  337. {
  338. _log.log(Level.WARNING, "GrandBossManager: Couldn't store grandbosses to database:" + e.getMessage(), e);
  339. }
  340. }
  341. private void updateDb(int bossId, boolean statusOnly)
  342. {
  343. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  344. {
  345. L2GrandBossInstance boss = _bosses.get(bossId);
  346. StatsSet info = _storedInfo.get(bossId);
  347. if (statusOnly || boss == null || info == null)
  348. {
  349. try (PreparedStatement statement = con.prepareStatement(UPDATE_GRAND_BOSS_DATA2))
  350. {
  351. statement.setInt(1, _bossStatus.get(bossId));
  352. statement.setInt(2, bossId);
  353. statement.executeUpdate();
  354. }
  355. }
  356. else
  357. {
  358. try (PreparedStatement statement = con.prepareStatement(UPDATE_GRAND_BOSS_DATA))
  359. {
  360. statement.setInt(1, boss.getX());
  361. statement.setInt(2, boss.getY());
  362. statement.setInt(3, boss.getZ());
  363. statement.setInt(4, boss.getHeading());
  364. statement.setLong(5, info.getLong("respawn_time"));
  365. double hp = boss.getCurrentHp();
  366. double mp = boss.getCurrentMp();
  367. if (boss.isDead())
  368. {
  369. hp = boss.getMaxHp();
  370. mp = boss.getMaxMp();
  371. }
  372. statement.setDouble(6, hp);
  373. statement.setDouble(7, mp);
  374. statement.setInt(8, _bossStatus.get(bossId));
  375. statement.setInt(9, bossId);
  376. statement.executeUpdate();
  377. }
  378. }
  379. }
  380. catch (SQLException e)
  381. {
  382. _log.log(Level.WARNING, "GrandBossManager: Couldn't update grandbosses to database:" + e.getMessage(), e);
  383. }
  384. }
  385. /**
  386. * Saves all Grand Boss info and then clears all info from memory,
  387. * including all schedules.
  388. */
  389. public void cleanUp()
  390. {
  391. storeToDb();
  392. _bosses.clear();
  393. _storedInfo.clear();
  394. _bossStatus.clear();
  395. _zones.clear();
  396. }
  397. public L2FastList<L2BossZone> getZones()
  398. {
  399. return _zones;
  400. }
  401. private static class SingletonHolder
  402. {
  403. protected static final GrandBossManager _instance = new GrandBossManager();
  404. }
  405. }