HellboundManager.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.concurrent.ScheduledFuture;
  21. import java.util.logging.Logger;
  22. import javolution.util.FastList;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.ThreadPoolManager;
  26. import com.l2jserver.gameserver.datatables.NpcTable;
  27. import com.l2jserver.gameserver.datatables.SpawnTable;
  28. import com.l2jserver.gameserver.model.L2Spawn;
  29. import com.l2jserver.gameserver.model.actor.L2Npc;
  30. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  31. import com.l2jserver.util.Rnd;
  32. /**
  33. * @author _DS_, GKR
  34. */
  35. public class HellboundManager
  36. {
  37. private static final Logger _log = Logger.getLogger(HellboundManager.class.getName());
  38. private static final String LOAD_SPAWNS = "SELECT npc_templateid, locx, locy, locz, heading, " +
  39. "respawn_delay, respawn_random, min_hellbound_level, " +
  40. "max_hellbound_level FROM hellbound_spawnlist ORDER BY npc_templateid";
  41. private int _level = 0;
  42. private int _trust = 0;
  43. private int _maxTrust = 0;
  44. private int _minTrust = 0;
  45. private ScheduledFuture<?> _engine = null;
  46. private final List<HellboundSpawn> _population;
  47. private HellboundManager()
  48. {
  49. _population = new FastList<HellboundSpawn>();
  50. loadData();
  51. loadSpawns();
  52. }
  53. public final int getLevel()
  54. {
  55. return _level;
  56. }
  57. public final synchronized void updateTrust(int t, boolean useRates)
  58. {
  59. if (isLocked())
  60. {
  61. return;
  62. }
  63. int reward = t;
  64. if (useRates)
  65. {
  66. reward = (int) (t > 0 ? Config.RATE_HB_TRUST_INCREASE * t : Config.RATE_HB_TRUST_DECREASE * t);
  67. }
  68. final int trust = Math.max(_trust + reward, _minTrust);
  69. if (_maxTrust > 0)
  70. {
  71. _trust = Math.min(trust, _maxTrust);
  72. }
  73. else
  74. {
  75. _trust = trust;
  76. }
  77. }
  78. public final void setLevel(int lvl)
  79. {
  80. _level = lvl;
  81. }
  82. public final int getTrust()
  83. {
  84. return _trust;
  85. }
  86. public final int getMaxTrust()
  87. {
  88. return _maxTrust;
  89. }
  90. public final int getMinTrust()
  91. {
  92. return _minTrust;
  93. }
  94. public final void setMaxTrust(int trust)
  95. {
  96. _maxTrust = trust;
  97. if ((_maxTrust > 0) && (_trust > _maxTrust))
  98. {
  99. _trust = _maxTrust;
  100. }
  101. }
  102. public final void setMinTrust(int trust)
  103. {
  104. _minTrust = trust;
  105. if (_trust >= _maxTrust)
  106. {
  107. _trust = _minTrust;
  108. }
  109. }
  110. /**
  111. * @return true if Hellbound is locked
  112. */
  113. public final boolean isLocked()
  114. {
  115. return _level == 0;
  116. }
  117. public final void unlock()
  118. {
  119. if (_level == 0)
  120. {
  121. setLevel(1);
  122. }
  123. }
  124. public final void registerEngine(Runnable r, int interval)
  125. {
  126. if (_engine != null)
  127. {
  128. _engine.cancel(false);
  129. }
  130. _engine = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(r, interval, interval);
  131. }
  132. public final void doSpawn()
  133. {
  134. int added = 0;
  135. int deleted = 0;
  136. for (HellboundSpawn spawnDat : _population)
  137. {
  138. try
  139. {
  140. if (spawnDat == null)
  141. {
  142. continue;
  143. }
  144. L2Npc npc = spawnDat.getLastSpawn();
  145. if ((_level < spawnDat.getMinLvl()) || (_level > spawnDat.getMaxLvl()))
  146. {
  147. // npc should be removed
  148. spawnDat.stopRespawn();
  149. if ((npc != null) && npc.isVisible())
  150. {
  151. npc.deleteMe();
  152. deleted++;
  153. }
  154. }
  155. else
  156. {
  157. // npc should be added
  158. spawnDat.startRespawn();
  159. npc = spawnDat.getLastSpawn();
  160. if (npc == null)
  161. {
  162. npc = spawnDat.doSpawn();
  163. added++;
  164. }
  165. else
  166. {
  167. if (npc.isDecayed())
  168. {
  169. npc.setDecayed(false);
  170. }
  171. if (npc.isDead())
  172. {
  173. npc.doRevive();
  174. }
  175. if (!npc.isVisible())
  176. {
  177. added++;
  178. }
  179. npc.setCurrentHp(npc.getMaxHp());
  180. npc.setCurrentMp(npc.getMaxMp());
  181. // npc.spawnMe(spawnDat.getLocx(), spawnDat.getLocy(),
  182. // spawnDat.getLocz());
  183. }
  184. }
  185. }
  186. catch (Exception e)
  187. {
  188. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  189. }
  190. }
  191. if (added > 0)
  192. {
  193. _log.info("HellboundManager: Spawned " + added + " NPCs.");
  194. }
  195. if (deleted > 0)
  196. {
  197. _log.info("HellboundManager: Removed " + deleted + " NPCs.");
  198. }
  199. }
  200. public final void cleanUp()
  201. {
  202. saveData();
  203. if (_engine != null)
  204. {
  205. _engine.cancel(true);
  206. _engine = null;
  207. }
  208. _population.clear();
  209. }
  210. private final void loadData()
  211. {
  212. if (GlobalVariablesManager.getInstance().isVariableStored("HBLevel"))
  213. {
  214. _level = Integer.parseInt(GlobalVariablesManager.getInstance().getStoredVariable("HBLevel"));
  215. _trust = Integer.parseInt(GlobalVariablesManager.getInstance().getStoredVariable("HBTrust"));
  216. }
  217. else
  218. {
  219. saveData();
  220. }
  221. }
  222. public final void saveData()
  223. {
  224. GlobalVariablesManager.getInstance().storeVariable("HBLevel", String.valueOf(_level));
  225. GlobalVariablesManager.getInstance().storeVariable("HBTrust", String.valueOf(_trust));
  226. }
  227. private final void loadSpawns()
  228. {
  229. Connection con = null;
  230. try
  231. {
  232. con = L2DatabaseFactory.getInstance().getConnection();
  233. final PreparedStatement statement = con.prepareStatement(LOAD_SPAWNS);
  234. final ResultSet rset = statement.executeQuery();
  235. HellboundSpawn spawnDat;
  236. L2NpcTemplate template;
  237. while (rset.next())
  238. {
  239. template = NpcTable.getInstance().getTemplate(rset.getInt("npc_templateid"));
  240. if (template != null)
  241. {
  242. spawnDat = new HellboundSpawn(template);
  243. spawnDat.setAmount(1);
  244. spawnDat.setLocx(rset.getInt("locx"));
  245. spawnDat.setLocy(rset.getInt("locy"));
  246. spawnDat.setLocz(rset.getInt("locz"));
  247. spawnDat.setHeading(rset.getInt("heading"));
  248. spawnDat.setRespawnDelay(rset.getInt("respawn_delay"));
  249. spawnDat.setRespawnMinDelay(0);
  250. spawnDat.setRespawnMaxDelay(0);
  251. int respawnRandom = (rset.getInt("respawn_random"));
  252. if (respawnRandom > 0) // Random respawn time, if needed
  253. {
  254. spawnDat.setRespawnMinDelay(Math.max(rset.getInt("respawn_delay") - respawnRandom, 1));
  255. spawnDat.setRespawnMaxDelay(rset.getInt("respawn_delay") + respawnRandom);
  256. }
  257. spawnDat.setMinLvl(rset.getInt("min_hellbound_level"));
  258. spawnDat.setMaxLvl(rset.getInt("max_hellbound_level"));
  259. // _population.put(spawnDat, null);
  260. _population.add(spawnDat);
  261. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  262. }
  263. else
  264. {
  265. _log.warning("HellboundManager: Data missing in NPC table for ID: " + rset.getInt("npc_templateid") + ".");
  266. }
  267. }
  268. rset.close();
  269. statement.close();
  270. }
  271. catch (Exception e)
  272. {
  273. _log.warning("HellboundManager: problem while loading spawns: " + e);
  274. }
  275. finally
  276. {
  277. L2DatabaseFactory.close(con);
  278. }
  279. _log.config("HellboundManager: Loaded " + _population.size() + " npc spawn locations.");
  280. }
  281. public static final class HellboundSpawn extends L2Spawn
  282. {
  283. /** The delay between a L2NpcInstance remove and its re-spawn */
  284. private int _respawnDelay;
  285. private int _minLvl;
  286. private int _maxLvl;
  287. public HellboundSpawn(L2NpcTemplate mobTemplate) throws SecurityException, ClassNotFoundException, NoSuchMethodException
  288. {
  289. super(mobTemplate);
  290. }
  291. public final int getMinLvl()
  292. {
  293. return _minLvl;
  294. }
  295. public final void setMinLvl(int lvl)
  296. {
  297. _minLvl = lvl;
  298. }
  299. public final int getMaxLvl()
  300. {
  301. return _maxLvl;
  302. }
  303. public final void setMaxLvl(int lvl)
  304. {
  305. _maxLvl = lvl;
  306. }
  307. @Override
  308. public final void decreaseCount(L2Npc oldNpc)
  309. {
  310. if (getRespawnDelay() <= 0)
  311. {
  312. stopRespawn();
  313. }
  314. else if (getRespawnMaxDelay() > getRespawnMinDelay())
  315. {
  316. setRespawnDelay(Rnd.get(getRespawnMinDelay(), getRespawnMaxDelay()));
  317. }
  318. super.decreaseCount(oldNpc);
  319. }
  320. /**
  321. * @param i delay in seconds
  322. */
  323. @Override
  324. public void setRespawnDelay(int i)
  325. {
  326. _respawnDelay = i * 1000;
  327. super.setRespawnDelay(i);
  328. }
  329. @Override
  330. public int getRespawnDelay()
  331. {
  332. return _respawnDelay;
  333. }
  334. }
  335. public static final HellboundManager getInstance()
  336. {
  337. return SingletonHolder._instance;
  338. }
  339. @SuppressWarnings("synthetic-access")
  340. private static class SingletonHolder
  341. {
  342. protected static final HellboundManager _instance = new HellboundManager();
  343. }
  344. }