Engine.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2004-2013 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 java.io.File;
  21. import java.util.Map;
  22. import java.util.logging.Level;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javolution.util.FastMap;
  25. import org.w3c.dom.Document;
  26. import org.w3c.dom.NamedNodeMap;
  27. import org.w3c.dom.Node;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.gameserver.Announcements;
  30. import com.l2jserver.gameserver.datatables.DoorTable;
  31. import com.l2jserver.gameserver.instancemanager.HellboundManager;
  32. import com.l2jserver.gameserver.model.actor.L2Npc;
  33. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  35. import com.l2jserver.gameserver.model.quest.Quest;
  36. public class Engine extends Quest implements Runnable
  37. {
  38. private static final String pointsInfoFile = "data/hellboundTrustPoints.xml";
  39. private static final int UPDATE_INTERVAL = 10000;
  40. private static final int[][] DOOR_LIST =
  41. {
  42. {
  43. 19250001,
  44. 5
  45. },
  46. {
  47. 19250002,
  48. 5
  49. },
  50. {
  51. 20250001,
  52. 9
  53. },
  54. {
  55. 20250002,
  56. 7
  57. }
  58. };
  59. private static final int[] MAX_TRUST =
  60. {
  61. 0,
  62. 300000,
  63. 600000,
  64. 1000000,
  65. 1010000,
  66. 1400000,
  67. 1490000,
  68. 2000000,
  69. 2000001,
  70. 2500000,
  71. 4000000,
  72. 0
  73. };
  74. private static final String ANNOUNCE = "Hellbound now has reached level: %lvl%";
  75. private int _cachedLevel = -1;
  76. private static Map<Integer, PointsInfoHolder> pointsInfo = new FastMap<>();
  77. // Holds info about points for mob killing
  78. private class PointsInfoHolder
  79. {
  80. protected int pointsAmount;
  81. protected int minHbLvl;
  82. protected int maxHbLvl;
  83. protected int lowestTrustLimit;
  84. protected PointsInfoHolder(int points, int min, int max, int trust)
  85. {
  86. pointsAmount = points;
  87. minHbLvl = min;
  88. maxHbLvl = max;
  89. lowestTrustLimit = trust;
  90. }
  91. }
  92. private final void onLevelChange(int newLevel)
  93. {
  94. try
  95. {
  96. HellboundManager.getInstance().setMaxTrust(MAX_TRUST[newLevel]);
  97. HellboundManager.getInstance().setMinTrust(MAX_TRUST[newLevel - 1]);
  98. }
  99. catch (ArrayIndexOutOfBoundsException e)
  100. {
  101. HellboundManager.getInstance().setMaxTrust(0);
  102. HellboundManager.getInstance().setMinTrust(0);
  103. }
  104. HellboundManager.getInstance().updateTrust(0, false);
  105. HellboundManager.getInstance().doSpawn();
  106. for (int[] doorData : DOOR_LIST)
  107. {
  108. try
  109. {
  110. L2DoorInstance door = DoorTable.getInstance().getDoor(doorData[0]);
  111. if (door.getOpen())
  112. {
  113. if (newLevel < doorData[1])
  114. {
  115. door.closeMe();
  116. }
  117. }
  118. else
  119. {
  120. if (newLevel >= doorData[1])
  121. {
  122. door.openMe();
  123. }
  124. }
  125. }
  126. catch (Exception e)
  127. {
  128. _log.log(Level.WARNING, "Hellbound doors problem!", e);
  129. }
  130. }
  131. if (_cachedLevel > 0)
  132. {
  133. Announcements.getInstance().announceToAll(ANNOUNCE.replace("%lvl%", String.valueOf(newLevel)));
  134. _log.info("HellboundEngine: New Level: " + newLevel);
  135. }
  136. _cachedLevel = newLevel;
  137. }
  138. private void loadPointsInfoData()
  139. {
  140. final File file = new File(Config.DATAPACK_ROOT, pointsInfoFile);
  141. if (!file.exists())
  142. {
  143. _log.warning("Cannot locate points info file: " + pointsInfoFile);
  144. return;
  145. }
  146. Document doc = null;
  147. try
  148. {
  149. final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  150. factory.setValidating(false);
  151. factory.setIgnoringComments(true);
  152. doc = factory.newDocumentBuilder().parse(file);
  153. }
  154. catch (Exception e)
  155. {
  156. _log.log(Level.WARNING, "Could not parse " + pointsInfoFile + " file: " + e.getMessage(), e);
  157. return;
  158. }
  159. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  160. {
  161. if ("list".equalsIgnoreCase(n.getNodeName()))
  162. {
  163. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  164. {
  165. if ("npc".equalsIgnoreCase(d.getNodeName()))
  166. {
  167. NamedNodeMap attrs = d.getAttributes();
  168. Node att;
  169. att = attrs.getNamedItem("id");
  170. if (att == null)
  171. {
  172. _log.severe("[Hellbound Trust Points Info] Missing NPC ID, skipping record");
  173. continue;
  174. }
  175. int npcId = Integer.parseInt(att.getNodeValue());
  176. att = attrs.getNamedItem("points");
  177. if (att == null)
  178. {
  179. _log.severe("[Hellbound Trust Points Info] Missing reward point info for NPC ID " + npcId + ", skipping record");
  180. continue;
  181. }
  182. int points = Integer.parseInt(att.getNodeValue());
  183. att = attrs.getNamedItem("minHellboundLvl");
  184. if (att == null)
  185. {
  186. _log.severe("[Hellbound Trust Points Info] Missing minHellboundLvl info for NPC ID " + npcId + ", skipping record");
  187. continue;
  188. }
  189. int minHbLvl = Integer.parseInt(att.getNodeValue());
  190. att = attrs.getNamedItem("maxHellboundLvl");
  191. if (att == null)
  192. {
  193. _log.severe("[Hellbound Trust Points Info] Missing maxHellboundLvl info for NPC ID " + npcId + ", skipping record");
  194. continue;
  195. }
  196. int maxHbLvl = Integer.parseInt(att.getNodeValue());
  197. att = attrs.getNamedItem("lowestTrustLimit");
  198. int lowestTrustLimit = 0;
  199. if (att != null)
  200. {
  201. lowestTrustLimit = Integer.parseInt(att.getNodeValue());
  202. }
  203. pointsInfo.put(npcId, new PointsInfoHolder(points, minHbLvl, maxHbLvl, lowestTrustLimit));
  204. }
  205. }
  206. }
  207. }
  208. _log.info("HellboundEngine: Loaded: " + pointsInfo.size() + " trust point reward data");
  209. }
  210. @Override
  211. public void run()
  212. {
  213. int level = HellboundManager.getInstance().getLevel();
  214. if ((level > 0) && (level == _cachedLevel))
  215. {
  216. if ((HellboundManager.getInstance().getTrust() == HellboundManager.getInstance().getMaxTrust()) && (level != 4)) // only exclusion is kill of Derek
  217. {
  218. level++;
  219. HellboundManager.getInstance().setLevel(level);
  220. onLevelChange(level);
  221. }
  222. }
  223. else
  224. {
  225. onLevelChange(level); // first run or changed by admin
  226. }
  227. }
  228. // Let's try to manage all trust changes for killing here
  229. @Override
  230. public String onKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  231. {
  232. int npcId = npc.getNpcId();
  233. if (pointsInfo.containsKey(npcId))
  234. {
  235. PointsInfoHolder npcInfo = pointsInfo.get(npcId);
  236. if ((HellboundManager.getInstance().getLevel() >= npcInfo.minHbLvl) && (HellboundManager.getInstance().getLevel() <= npcInfo.maxHbLvl) && ((npcInfo.lowestTrustLimit == 0) || (HellboundManager.getInstance().getTrust() > npcInfo.lowestTrustLimit)))
  237. {
  238. HellboundManager.getInstance().updateTrust(npcInfo.pointsAmount, true);
  239. }
  240. if ((npc.getNpcId() == 18465) && (HellboundManager.getInstance().getLevel() == 4))
  241. {
  242. HellboundManager.getInstance().setLevel(5);
  243. }
  244. }
  245. return super.onKill(npc, killer, isSummon);
  246. }
  247. public Engine(int questId, String name, String descr)
  248. {
  249. super(questId, name, descr);
  250. HellboundManager.getInstance().registerEngine(this, UPDATE_INTERVAL);
  251. loadPointsInfoData();
  252. // Register onKill for all rewardable monsters
  253. for (int npcId : pointsInfo.keySet())
  254. {
  255. addKillId(npcId);
  256. }
  257. _log.info("HellboundEngine: Mode: levels 0-3");
  258. _log.info("HellboundEngine: Level: " + HellboundManager.getInstance().getLevel());
  259. _log.info("HellboundEngine: Trust: " + HellboundManager.getInstance().getTrust());
  260. if (HellboundManager.getInstance().isLocked())
  261. {
  262. _log.info("HellboundEngine: State: locked");
  263. }
  264. else
  265. {
  266. _log.info("HellboundEngine: State: unlocked");
  267. }
  268. }
  269. public static void main(String[] args)
  270. {
  271. new Engine(-1, Engine.class.getSimpleName(), "hellbound");
  272. }
  273. }