GrandBossManager.java 11 KB

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