HellboundManager.java 8.9 KB

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