Engine.java 8.2 KB

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