HellboundEngine.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (C) 2004-2014 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack 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 DataPack 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 hellbound;
  20. import ai.npc.AbstractNpcAI;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.Announcements;
  23. import com.l2jserver.gameserver.datatables.DoorTable;
  24. import com.l2jserver.gameserver.instancemanager.GlobalVariablesManager;
  25. import com.l2jserver.gameserver.model.L2Spawn;
  26. import com.l2jserver.gameserver.model.actor.L2Npc;
  27. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. /**
  30. * Hellbound Engine.
  31. * @author Zoey76
  32. */
  33. public final class HellboundEngine extends AbstractNpcAI
  34. {
  35. // @formatter:off
  36. private static final int[][] DOOR_LIST =
  37. {
  38. { 19250001, 5 },
  39. { 19250002, 5 },
  40. { 20250001, 9 },
  41. { 20250002, 7 }
  42. };
  43. private static final int[] MAX_TRUST =
  44. {
  45. 0, 300000, 600000, 1000000, 1010000, 1400000, 1490000, 2000000, 2000001, 2500000, 4000000, 0
  46. };
  47. // @formatter:on
  48. // Monsters
  49. private static final int DEREK = 18465;
  50. // Engine
  51. private static final String ANNOUNCEMENT = "Hellbound has reached level: %lvl%";
  52. private static final int UPDATE_INTERVAL = 60000; // 1 minute.
  53. private static final String UPDATE_EVENT = "UPDATE";
  54. private int _cachedLevel = -1;
  55. private int _maxTrust = 0;
  56. private int _minTrust = 0;
  57. public HellboundEngine()
  58. {
  59. super(HellboundEngine.class.getSimpleName(), "hellbound");
  60. addKillId(HellboundPointData.getInstance().getPointsInfo().keySet());
  61. startQuestTimer(UPDATE_EVENT, 1000, null, null);
  62. _log.info(HellboundEngine.class.getSimpleName() + ": Level: " + getLevel());
  63. _log.info(HellboundEngine.class.getSimpleName() + ": Trust: " + getTrust());
  64. _log.info(HellboundEngine.class.getSimpleName() + ": Status: " + (isLocked() ? "locked." : "unlocked."));
  65. }
  66. @Override
  67. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  68. {
  69. if (event.equals(UPDATE_EVENT))
  70. {
  71. int level = getLevel();
  72. if ((level > 0) && (level == getCachedLevel()))
  73. {
  74. if ((getTrust() == getMaxTrust()) && (level != 4)) // Only exclusion is kill of Derek.
  75. {
  76. level++;
  77. setLevel(level);
  78. onLevelChange(level);
  79. }
  80. }
  81. else
  82. {
  83. onLevelChange(level); // First run or changed by administrator.
  84. }
  85. startQuestTimer(UPDATE_EVENT, UPDATE_INTERVAL, null, null);
  86. }
  87. return super.onAdvEvent(event, npc, player);
  88. }
  89. /**
  90. * Adds and removes spawns for Hellbound given the conditions for spawn.
  91. */
  92. private void doSpawn()
  93. {
  94. int added = 0;
  95. int deleted = 0;
  96. final HellboundSpawns hellboundSpawns = HellboundSpawns.getInstance();
  97. for (L2Spawn spawn : hellboundSpawns.getSpawns())
  98. {
  99. final L2Npc npc = spawn.getLastSpawn();
  100. if ((getLevel() < hellboundSpawns.getSpawnMinLevel(spawn.getId())) || (getLevel() > hellboundSpawns.getSpawnMaxLevel(spawn.getId())))
  101. {
  102. spawn.stopRespawn();
  103. if ((npc != null) && npc.isVisible())
  104. {
  105. npc.deleteMe();
  106. deleted++;
  107. }
  108. }
  109. else
  110. {
  111. spawn.startRespawn();
  112. if (npc == null)
  113. {
  114. spawn.doSpawn();
  115. added++;
  116. }
  117. else
  118. {
  119. if (npc.isDecayed())
  120. {
  121. npc.setDecayed(false);
  122. }
  123. if (npc.isDead())
  124. {
  125. npc.doRevive();
  126. }
  127. if (!npc.isVisible())
  128. {
  129. npc.setIsVisible(true);
  130. added++;
  131. }
  132. npc.setCurrentHp(npc.getMaxHp());
  133. npc.setCurrentMp(npc.getMaxMp());
  134. }
  135. }
  136. }
  137. if (added > 0)
  138. {
  139. _log.info(getClass().getSimpleName() + ": Spawned " + added + " NPCs.");
  140. }
  141. if (deleted > 0)
  142. {
  143. _log.info(getClass().getSimpleName() + ": Removed " + deleted + " NPCs.");
  144. }
  145. }
  146. /**
  147. * Gets the Hellbound level.
  148. * @return the level
  149. */
  150. public int getLevel()
  151. {
  152. return GlobalVariablesManager.getInstance().getInt("HBLevel", 0);
  153. }
  154. /**
  155. * Sets the Hellbound level.
  156. * @param lvl the level to set
  157. */
  158. public void setLevel(int lvl)
  159. {
  160. if (lvl == getLevel())
  161. {
  162. return;
  163. }
  164. _log.info(HellboundEngine.class.getSimpleName() + ": Changing level from " + getLevel() + " to " + lvl + ".");
  165. GlobalVariablesManager.getInstance().set("HBLevel", lvl);
  166. }
  167. public int getCachedLevel()
  168. {
  169. return _cachedLevel;
  170. }
  171. public int getMaxTrust()
  172. {
  173. return _maxTrust;
  174. }
  175. public int getMinTrust()
  176. {
  177. return _minTrust;
  178. }
  179. /**
  180. * Gets the trust.
  181. * @return the trust
  182. */
  183. public int getTrust()
  184. {
  185. return GlobalVariablesManager.getInstance().getInt("HBTrust", 0);
  186. }
  187. /**
  188. * Sets the truest.
  189. * @param trust the trust to set
  190. */
  191. private void setTrust(int trust)
  192. {
  193. GlobalVariablesManager.getInstance().set("HBTrust", trust);
  194. }
  195. /**
  196. * Verifies if Hellbound is locked.
  197. * @return {@code true} if Hellbound is locked, {@code false} otherwise
  198. */
  199. public boolean isLocked()
  200. {
  201. return getLevel() <= 0;
  202. }
  203. @Override
  204. public String onKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  205. {
  206. final int npcId = npc.getId();
  207. final HellboundPointData hellboundPointData = HellboundPointData.getInstance();
  208. if (hellboundPointData.getPointsInfo().containsKey(npcId))
  209. {
  210. if ((getLevel() >= hellboundPointData.getMinHbLvl(npcId)) && (getLevel() <= hellboundPointData.getMaxHbLvl(npcId)) && ((hellboundPointData.getLowestTrustLimit(npcId) == 0) || (getTrust() > hellboundPointData.getLowestTrustLimit(npcId))))
  211. {
  212. updateTrust(hellboundPointData.getPointsAmount(npcId), true);
  213. }
  214. if ((npcId == DEREK) && (getLevel() == 4))
  215. {
  216. setLevel(5);
  217. }
  218. }
  219. return super.onKill(npc, killer, isSummon);
  220. }
  221. /**
  222. * Called on every level change.
  223. * @param newLevel the new level
  224. */
  225. public void onLevelChange(int newLevel)
  226. {
  227. try
  228. {
  229. setMaxTrust(MAX_TRUST[newLevel]);
  230. setMinTrust(MAX_TRUST[newLevel - 1]);
  231. }
  232. catch (Exception e)
  233. {
  234. setMaxTrust(0);
  235. setMinTrust(0);
  236. }
  237. updateTrust(0, false);
  238. doSpawn();
  239. for (int[] doorData : DOOR_LIST)
  240. {
  241. try
  242. {
  243. L2DoorInstance door = DoorTable.getInstance().getDoor(doorData[0]);
  244. if (door.getOpen())
  245. {
  246. if (newLevel < doorData[1])
  247. {
  248. door.closeMe();
  249. }
  250. }
  251. else
  252. {
  253. if (newLevel >= doorData[1])
  254. {
  255. door.openMe();
  256. }
  257. }
  258. }
  259. catch (Exception e)
  260. {
  261. _log.warning(getClass().getSimpleName() + " : Doors problem!" + e.getMessage());
  262. }
  263. }
  264. if (_cachedLevel > 0)
  265. {
  266. Announcements.getInstance().announceToAll(ANNOUNCEMENT.replace("%lvl%", String.valueOf(newLevel)));
  267. _log.info(HellboundEngine.class.getSimpleName() + ": New level: " + newLevel);
  268. }
  269. _cachedLevel = newLevel;
  270. }
  271. /**
  272. * Sets the maximum trust for the current level.
  273. * @param trust the maximum trust
  274. */
  275. private void setMaxTrust(int trust)
  276. {
  277. _maxTrust = trust;
  278. if ((_maxTrust > 0) && (getTrust() > _maxTrust))
  279. {
  280. setTrust(_maxTrust);
  281. }
  282. }
  283. /**
  284. * Sets the minimum trust for the current level.
  285. * @param trust the minimum trust
  286. */
  287. private void setMinTrust(int trust)
  288. {
  289. _minTrust = trust;
  290. if (getTrust() >= _maxTrust)
  291. {
  292. setTrust(_minTrust);
  293. }
  294. }
  295. @Override
  296. public boolean unload()
  297. {
  298. cancelQuestTimers(UPDATE_EVENT);
  299. return true;
  300. }
  301. /**
  302. * Updates the trust.
  303. * @param trust the trust
  304. * @param useRates if {@code true} it will use Hellbound trust rates
  305. */
  306. public synchronized void updateTrust(int trust, boolean useRates)
  307. {
  308. if (isLocked())
  309. {
  310. return;
  311. }
  312. int reward = trust;
  313. if (useRates)
  314. {
  315. reward = (int) (trust * (trust > 0 ? Config.RATE_HB_TRUST_INCREASE : Config.RATE_HB_TRUST_DECREASE));
  316. }
  317. final int finalTrust = Math.max(getTrust() + reward, _minTrust);
  318. if (_maxTrust > 0)
  319. {
  320. setTrust(Math.min(finalTrust, _maxTrust));
  321. }
  322. else
  323. {
  324. setTrust(finalTrust);
  325. }
  326. }
  327. public static HellboundEngine getInstance()
  328. {
  329. return SingletonHolder.INSTANCE;
  330. }
  331. private static class SingletonHolder
  332. {
  333. protected static final HellboundEngine INSTANCE = new HellboundEngine();
  334. }
  335. }