Engine.java 8.2 KB

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