HellboundManager.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.concurrent.ScheduledFuture;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.ThreadPoolManager;
  30. import com.l2jserver.gameserver.datatables.NpcTable;
  31. import com.l2jserver.gameserver.datatables.SpawnTable;
  32. import com.l2jserver.gameserver.model.L2Spawn;
  33. import com.l2jserver.gameserver.model.actor.L2Npc;
  34. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  35. /**
  36. * @author _DS_, GKR
  37. */
  38. public class HellboundManager
  39. {
  40. private static final Logger _log = Logger.getLogger(HellboundManager.class.getName());
  41. 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";
  42. private int _level = 0;
  43. private int _trust = 0;
  44. private int _maxTrust = 0;
  45. private int _minTrust = 0;
  46. private ScheduledFuture<?> _engine = null;
  47. private final List<HellboundSpawn> _population = new ArrayList<>();
  48. protected HellboundManager()
  49. {
  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(getClass().getSimpleName() + ": Spawned " + added + " NPCs.");
  194. }
  195. if (deleted > 0)
  196. {
  197. _log.info(getClass().getSimpleName() + ": 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().hasVariable("HBLevel"))
  213. {
  214. _level = GlobalVariablesManager.getInstance().getInteger("HBLevel");
  215. _trust = GlobalVariablesManager.getInstance().getInteger("HBTrust");
  216. }
  217. else
  218. {
  219. saveData();
  220. }
  221. }
  222. public final void saveData()
  223. {
  224. GlobalVariablesManager.getInstance().set("HBLevel", _level);
  225. GlobalVariablesManager.getInstance().set("HBTrust", _trust);
  226. }
  227. private final void loadSpawns()
  228. {
  229. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  230. Statement s = con.createStatement();
  231. ResultSet rs = s.executeQuery(LOAD_SPAWNS))
  232. {
  233. HellboundSpawn spawnDat;
  234. L2NpcTemplate template;
  235. while (rs.next())
  236. {
  237. template = NpcTable.getInstance().getTemplate(rs.getInt("npc_templateid"));
  238. if (template != null)
  239. {
  240. spawnDat = new HellboundSpawn(template);
  241. spawnDat.setAmount(1);
  242. spawnDat.setX(rs.getInt("locx"));
  243. spawnDat.setY(rs.getInt("locy"));
  244. spawnDat.setZ(rs.getInt("locz"));
  245. spawnDat.setHeading(rs.getInt("heading"));
  246. spawnDat.setRespawnDelay(rs.getInt("respawn_delay"), rs.getInt("respawn_random"));
  247. spawnDat.setMinLvl(rs.getInt("min_hellbound_level"));
  248. spawnDat.setMaxLvl(rs.getInt("max_hellbound_level"));
  249. // _population.put(spawnDat, null);
  250. _population.add(spawnDat);
  251. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  252. }
  253. else
  254. {
  255. _log.warning(getClass().getSimpleName() + ": Data missing in NPC table for ID: " + rs.getInt("npc_templateid") + ".");
  256. }
  257. }
  258. rs.close();
  259. s.close();
  260. }
  261. catch (Exception e)
  262. {
  263. _log.warning(getClass().getSimpleName() + ": problem while loading spawns: " + e);
  264. }
  265. _log.info(getClass().getSimpleName() + ": Loaded " + _population.size() + " npc spawn locations.");
  266. }
  267. public static final class HellboundSpawn extends L2Spawn
  268. {
  269. private int _minLvl;
  270. private int _maxLvl;
  271. public HellboundSpawn(L2NpcTemplate mobTemplate) throws SecurityException, ClassNotFoundException, NoSuchMethodException
  272. {
  273. super(mobTemplate);
  274. }
  275. public final int getMinLvl()
  276. {
  277. return _minLvl;
  278. }
  279. public final void setMinLvl(int lvl)
  280. {
  281. _minLvl = lvl;
  282. }
  283. public final int getMaxLvl()
  284. {
  285. return _maxLvl;
  286. }
  287. public final void setMaxLvl(int lvl)
  288. {
  289. _maxLvl = lvl;
  290. }
  291. }
  292. public static final HellboundManager getInstance()
  293. {
  294. return SingletonHolder._instance;
  295. }
  296. private static class SingletonHolder
  297. {
  298. protected static final HellboundManager _instance = new HellboundManager();
  299. }
  300. }