GrandBossManager.java 11 KB

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