HellboundManager.java 8.8 KB

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