2
0

GrandBossManager.java 11 KB

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