HellboundManager.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. protected HellboundManager()
  48. {
  49. _population = new FastList<>();
  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. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  230. {
  231. final PreparedStatement statement = con.prepareStatement(LOAD_SPAWNS);
  232. final ResultSet rset = statement.executeQuery();
  233. HellboundSpawn spawnDat;
  234. L2NpcTemplate template;
  235. while (rset.next())
  236. {
  237. template = NpcTable.getInstance().getTemplate(rset.getInt("npc_templateid"));
  238. if (template != null)
  239. {
  240. spawnDat = new HellboundSpawn(template);
  241. spawnDat.setAmount(1);
  242. spawnDat.setLocx(rset.getInt("locx"));
  243. spawnDat.setLocy(rset.getInt("locy"));
  244. spawnDat.setLocz(rset.getInt("locz"));
  245. spawnDat.setHeading(rset.getInt("heading"));
  246. spawnDat.setRespawnDelay(rset.getInt("respawn_delay"));
  247. spawnDat.setRespawnMinDelay(0);
  248. spawnDat.setRespawnMaxDelay(0);
  249. int respawnRandom = (rset.getInt("respawn_random"));
  250. if (respawnRandom > 0) // Random respawn time, if needed
  251. {
  252. spawnDat.setRespawnMinDelay(Math.max(rset.getInt("respawn_delay") - respawnRandom, 1));
  253. spawnDat.setRespawnMaxDelay(rset.getInt("respawn_delay") + respawnRandom);
  254. }
  255. spawnDat.setMinLvl(rset.getInt("min_hellbound_level"));
  256. spawnDat.setMaxLvl(rset.getInt("max_hellbound_level"));
  257. // _population.put(spawnDat, null);
  258. _population.add(spawnDat);
  259. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  260. }
  261. else
  262. {
  263. _log.warning("HellboundManager: Data missing in NPC table for ID: " + rset.getInt("npc_templateid") + ".");
  264. }
  265. }
  266. rset.close();
  267. statement.close();
  268. }
  269. catch (Exception e)
  270. {
  271. _log.warning("HellboundManager: problem while loading spawns: " + e);
  272. }
  273. _log.config("HellboundManager: Loaded " + _population.size() + " npc spawn locations.");
  274. }
  275. public static final class HellboundSpawn extends L2Spawn
  276. {
  277. /** The delay between a L2NpcInstance remove and its re-spawn */
  278. private int _respawnDelay;
  279. private int _minLvl;
  280. private int _maxLvl;
  281. public HellboundSpawn(L2NpcTemplate mobTemplate) throws SecurityException, ClassNotFoundException, NoSuchMethodException
  282. {
  283. super(mobTemplate);
  284. }
  285. public final int getMinLvl()
  286. {
  287. return _minLvl;
  288. }
  289. public final void setMinLvl(int lvl)
  290. {
  291. _minLvl = lvl;
  292. }
  293. public final int getMaxLvl()
  294. {
  295. return _maxLvl;
  296. }
  297. public final void setMaxLvl(int lvl)
  298. {
  299. _maxLvl = lvl;
  300. }
  301. @Override
  302. public final void decreaseCount(L2Npc oldNpc)
  303. {
  304. if (getRespawnDelay() <= 0)
  305. {
  306. stopRespawn();
  307. }
  308. else if (getRespawnMaxDelay() > getRespawnMinDelay())
  309. {
  310. setRespawnDelay(Rnd.get(getRespawnMinDelay(), getRespawnMaxDelay()));
  311. }
  312. super.decreaseCount(oldNpc);
  313. }
  314. /**
  315. * @param i delay in seconds
  316. */
  317. @Override
  318. public void setRespawnDelay(int i)
  319. {
  320. _respawnDelay = i * 1000;
  321. super.setRespawnDelay(i);
  322. }
  323. @Override
  324. public int getRespawnDelay()
  325. {
  326. return _respawnDelay;
  327. }
  328. }
  329. public static final HellboundManager getInstance()
  330. {
  331. return SingletonHolder._instance;
  332. }
  333. private static class SingletonHolder
  334. {
  335. protected static final HellboundManager _instance = new HellboundManager();
  336. }
  337. }