SkillTreesData.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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 com.l2jserver.gameserver.datatables;
  16. import gnu.trove.map.hash.TIntObjectHashMap;
  17. import java.io.File;
  18. import java.util.Arrays;
  19. import java.util.Set;
  20. import java.util.logging.Logger;
  21. import javax.xml.parsers.DocumentBuilderFactory;
  22. import javolution.util.FastList;
  23. import javolution.util.FastMap;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.NamedNodeMap;
  26. import org.w3c.dom.Node;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.gameserver.model.L2Clan;
  29. import com.l2jserver.gameserver.model.L2Skill;
  30. import com.l2jserver.gameserver.model.L2SkillLearn;
  31. import com.l2jserver.gameserver.model.StatsSet;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.base.ClassId;
  34. import com.l2jserver.gameserver.model.base.Race;
  35. import com.l2jserver.gameserver.model.base.SubClass;
  36. import com.l2jserver.gameserver.util.Util;
  37. import com.l2jserver.util.file.filter.XMLFilter;
  38. /**
  39. * @author Zoey76
  40. */
  41. public final class SkillTreesData
  42. {
  43. private static final Logger _log = Logger.getLogger(SkillTreesData.class.getName());
  44. // ClassId, FastMap of Skill Hash Code, L2LearkSkill
  45. private static final FastMap<ClassId, FastMap<Integer, L2SkillLearn>> _classSkillTrees = new FastMap<ClassId, FastMap<Integer, L2SkillLearn>>();
  46. private static final FastMap<ClassId, FastMap<Integer, L2SkillLearn>> _transferSkillTrees = new FastMap<ClassId, FastMap<Integer, L2SkillLearn>>();
  47. // Skill Hash Code, L2LearkSkill
  48. private static final FastMap<Integer, L2SkillLearn> _collectSkillTree = new FastMap<Integer, L2SkillLearn>();
  49. private static final FastMap<Integer, L2SkillLearn> _fishingSkillTree = new FastMap<Integer, L2SkillLearn>();
  50. private static final FastMap<Integer, L2SkillLearn> _pledgeSkillTree = new FastMap<Integer, L2SkillLearn>();
  51. private static final FastMap<Integer, L2SkillLearn> _subClassSkillTree = new FastMap<Integer, L2SkillLearn>();
  52. private static final FastMap<Integer, L2SkillLearn> _subPledgeSkillTree = new FastMap<Integer, L2SkillLearn>();
  53. private static final FastMap<Integer, L2SkillLearn> _transformSkillTree = new FastMap<Integer, L2SkillLearn>();
  54. private static final FastMap<Integer, L2SkillLearn> _commonSkillTree = new FastMap<Integer, L2SkillLearn>();
  55. // Checker, sorted arrays of hash codes
  56. private TIntObjectHashMap<int[]> _skillsByClassIdHashCodes; // Occupation skills
  57. private TIntObjectHashMap<int[]> _skillsByRaceHashCodes; // Race-specific Transformations
  58. private int[] _allSkillsHashCodes; // Fishing, Collection, Transformations, Common Skills.
  59. private boolean _loading = true;
  60. /**
  61. * Parent class IDs are read from XML and stored in this map, to allow easy customization.
  62. */
  63. private static final FastMap<ClassId, ClassId> _parentClassMap = new FastMap<ClassId, ClassId>();
  64. private SkillTreesData()
  65. {
  66. load();
  67. }
  68. public void load()
  69. {
  70. _loading = true;
  71. _classSkillTrees.clear();
  72. _collectSkillTree.clear();
  73. _fishingSkillTree.clear();
  74. _pledgeSkillTree.clear();
  75. _subClassSkillTree.clear();
  76. _subPledgeSkillTree.clear();
  77. _transferSkillTrees.clear();
  78. _transformSkillTree.clear();
  79. // Load files.
  80. _loading = loadFiles();
  81. // Generate check arrays.
  82. generateCheckArrays();
  83. int classSkillTreeCount = 0;
  84. for (ClassId classId : _classSkillTrees.keySet())
  85. {
  86. classSkillTreeCount += _classSkillTrees.get(classId).size();
  87. }
  88. int trasferSkillTreeCount = 0;
  89. for (ClassId classId : _transferSkillTrees.keySet())
  90. {
  91. trasferSkillTreeCount += _transferSkillTrees.get(classId).size();
  92. }
  93. int fishingDwarvenSkillCount = 0;
  94. for (L2SkillLearn fishSkill : _fishingSkillTree.values())
  95. {
  96. if ((fishSkill.getRaces() != null) && Util.contains(fishSkill.getRaces(), 4))
  97. {
  98. fishingDwarvenSkillCount++;
  99. }
  100. }
  101. int residentialSkillCount = 0;
  102. for (L2SkillLearn pledgeSkill : _pledgeSkillTree.values())
  103. {
  104. if (pledgeSkill.isResidencialSkill())
  105. {
  106. residentialSkillCount++;
  107. }
  108. }
  109. _log.info(getClass().getSimpleName() + ": Loaded " + classSkillTreeCount + " Class Skills for " + _classSkillTrees.size() + " Class Skill Trees.");
  110. _log.info(getClass().getSimpleName() + ": Loaded " + _subClassSkillTree.size() + " Sub-Class Skills.");
  111. _log.info(getClass().getSimpleName() + ": Loaded " + trasferSkillTreeCount + " Transfer Skills for " + _transferSkillTrees.size() + " Transfer Skill Trees.");
  112. _log.info(getClass().getSimpleName() + ": Loaded " + _fishingSkillTree.size() + " Fishing Skills, " + fishingDwarvenSkillCount + " Dwarven only Fishing Skills.");
  113. _log.info(getClass().getSimpleName() + ": Loaded " + _collectSkillTree.size() + " Collect Skills.");
  114. _log.info(getClass().getSimpleName() + ": Loaded " + _pledgeSkillTree.size() + " Pledge Skills, " + (_pledgeSkillTree.size() - residentialSkillCount) + " for Pledge and " + residentialSkillCount + " Residential.");
  115. _log.info(getClass().getSimpleName() + ": Loaded " + _subPledgeSkillTree.size() + " Sub-Pledge Skills.");
  116. _log.info(getClass().getSimpleName() + ": Loaded " + _transformSkillTree.size() + " Transform Skills.");
  117. final int commonSkills = _commonSkillTree.size();
  118. if (commonSkills > 0)
  119. {
  120. _log.info(getClass().getSimpleName() + ": Loaded " + commonSkills + " Common Skills to all classes.");
  121. }
  122. }
  123. /**
  124. * Loads all files type xml from data/skillTrees/ and call the parser for each one of them.
  125. * @return {@code false} when the files are loaded.
  126. */
  127. private boolean loadFiles()
  128. {
  129. final File folder = new File(Config.DATAPACK_ROOT, "data/skillTrees/");
  130. if (!folder.exists())
  131. {
  132. _log.warning(getClass().getSimpleName() + ": Folder " + folder.getAbsolutePath() + " doesn't exist!");
  133. return false;
  134. }
  135. final File[] listOfFiles = folder.listFiles(new XMLFilter());
  136. for (File f : listOfFiles)
  137. {
  138. loadSkillTree(f);
  139. }
  140. return false;
  141. }
  142. /**
  143. * Parse a skill tree file and store it into the correct skill tree.
  144. * @param file the xml file to be parsed.
  145. */
  146. private void loadSkillTree(File file)
  147. {
  148. if (!file.exists())
  149. {
  150. _log.warning(getClass().getSimpleName() + ": Could not parse " + file.getName() + " file doesn't exist");
  151. return;
  152. }
  153. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  154. dbf.setValidating(false);
  155. dbf.setIgnoringComments(true);
  156. Document doc = null;
  157. try
  158. {
  159. doc = dbf.newDocumentBuilder().parse(file);
  160. }
  161. catch (Exception e)
  162. {
  163. _log.warning(getClass().getSimpleName() + ": Could not parse " + file.getName() + " file: " + e.getMessage());
  164. return;
  165. }
  166. NamedNodeMap attributes;
  167. Node attribute;
  168. String type = null;
  169. int cId = -1;
  170. int parentClassId = -1;
  171. ClassId classId = null;
  172. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  173. {
  174. if ("list".equalsIgnoreCase(n.getNodeName()))
  175. {
  176. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  177. {
  178. final FastMap<Integer, L2SkillLearn> classSkillTree = new FastMap<Integer, L2SkillLearn>();
  179. final FastMap<Integer, L2SkillLearn> trasferSkillTree = new FastMap<Integer, L2SkillLearn>();
  180. if ("skillTree".equalsIgnoreCase(d.getNodeName()))
  181. {
  182. attribute = d.getAttributes().getNamedItem("type");
  183. if (attribute == null)
  184. {
  185. _log.warning(getClass().getSimpleName() + ": Skill Tree without type!");
  186. continue;
  187. }
  188. type = attribute.getNodeValue();
  189. attribute = d.getAttributes().getNamedItem("classId");
  190. if (attribute != null)
  191. {
  192. try
  193. {
  194. cId = Integer.parseInt(attribute.getNodeValue());
  195. if (cId != -1)
  196. {
  197. classId = ClassId.values()[cId];
  198. }
  199. }
  200. catch (Exception e)
  201. {
  202. _log.warning(getClass().getSimpleName() + ": Invalid class Id " + attribute.getNodeValue() + " for Skill Tree type: " + type + "!");
  203. continue;
  204. }
  205. }
  206. attribute = d.getAttributes().getNamedItem("parentClassId");
  207. if (attribute != null)
  208. {
  209. try
  210. {
  211. parentClassId = Integer.parseInt(attribute.getNodeValue());
  212. if ((cId > -1) && (cId != parentClassId) && (parentClassId > -1))
  213. {
  214. _parentClassMap.putIfAbsent(classId, ClassId.values()[parentClassId]);
  215. }
  216. }
  217. catch (Exception e)
  218. {
  219. _log.warning(getClass().getSimpleName() + ": Invalid parent class Id " + attribute.getNodeValue() + " for Skill Tree type: " + type + "!");
  220. continue;
  221. }
  222. }
  223. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  224. {
  225. if ("skill".equalsIgnoreCase(c.getNodeName()))
  226. {
  227. final StatsSet learnSkillSet = new StatsSet();
  228. int skillId;
  229. int skillLvl;
  230. attributes = c.getAttributes();
  231. attribute = attributes.getNamedItem("skillName");
  232. if (attribute == null)
  233. {
  234. _log.severe(getClass().getSimpleName() + ": Missing skillName, skipping!");
  235. continue;
  236. }
  237. learnSkillSet.set("skillName", attribute.getNodeValue());
  238. attribute = attributes.getNamedItem("skillIdLvl");
  239. if (attribute == null)
  240. {
  241. _log.severe(getClass().getSimpleName() + ": Missing skillIdLvl, skipping!");
  242. continue;
  243. }
  244. try
  245. {
  246. skillId = Integer.parseInt(attribute.getNodeValue().split(",")[0]);
  247. skillLvl = Integer.parseInt(attribute.getNodeValue().split(",")[1]);
  248. learnSkillSet.set("skillId", skillId);
  249. learnSkillSet.set("skillLvl", skillLvl);
  250. }
  251. catch (Exception e)
  252. {
  253. _log.severe(getClass().getSimpleName() + ": Malformed skillIdLvl, skipping!");
  254. continue;
  255. }
  256. attribute = attributes.getNamedItem("getLevel");
  257. if (attribute != null)
  258. {
  259. learnSkillSet.set("getLevel", attribute.getNodeValue());
  260. }
  261. attribute = attributes.getNamedItem("autoGet");
  262. if (attribute != null)
  263. {
  264. learnSkillSet.set("autoGet", attribute.getNodeValue());
  265. }
  266. attribute = attributes.getNamedItem("levelUpSp");
  267. if (attribute != null)
  268. {
  269. learnSkillSet.set("levelUpSp", attribute.getNodeValue());
  270. }
  271. attribute = attributes.getNamedItem("itemsIdCount");
  272. if (attribute != null)
  273. {
  274. learnSkillSet.set("itemsIdCount", attribute.getNodeValue());
  275. }
  276. attribute = attributes.getNamedItem("race");
  277. if (attribute != null)
  278. {
  279. learnSkillSet.set("race", attribute.getNodeValue());
  280. }
  281. attribute = attributes.getNamedItem("preReqSkillIdLvl");
  282. if (attribute != null)
  283. {
  284. learnSkillSet.set("preReqSkillIdLvl", attribute.getNodeValue());
  285. }
  286. attribute = attributes.getNamedItem("socialClass");
  287. if (attribute != null)
  288. {
  289. learnSkillSet.set("socialClass", attribute.getNodeValue());
  290. }
  291. attribute = attributes.getNamedItem("subClassLvlNumber");
  292. if (attribute != null)
  293. {
  294. learnSkillSet.set("subClassLvlNumber", attribute.getNodeValue());
  295. }
  296. attribute = attributes.getNamedItem("residenceSkill");
  297. if (attribute != null)
  298. {
  299. learnSkillSet.set("residenceSkill", attribute.getNodeValue());
  300. }
  301. attribute = attributes.getNamedItem("residenceIds");
  302. if (attribute != null)
  303. {
  304. learnSkillSet.set("residenceIds", attribute.getNodeValue());
  305. }
  306. attribute = attributes.getNamedItem("learnedByNpc");
  307. if (attribute != null)
  308. {
  309. learnSkillSet.set("learnedByNpc", attribute.getNodeValue());
  310. }
  311. attribute = attributes.getNamedItem("learnedByFS");
  312. if (attribute != null)
  313. {
  314. learnSkillSet.set("learnedByFS", attribute.getNodeValue());
  315. }
  316. final L2SkillLearn skillLearn = new L2SkillLearn(learnSkillSet);
  317. final int skillHashCode = SkillTable.getSkillHashCode(skillId, skillLvl);
  318. if (type.equals("classSkillTree"))
  319. {
  320. if (cId != -1)
  321. {
  322. classSkillTree.put(skillHashCode, skillLearn);
  323. }
  324. else
  325. {
  326. _commonSkillTree.put(skillHashCode, skillLearn);
  327. }
  328. }
  329. else if (type.equals("transferSkillTree"))
  330. {
  331. trasferSkillTree.put(skillHashCode, skillLearn);
  332. }
  333. else
  334. {
  335. if (type.equals("collectSkillTree"))
  336. {
  337. _collectSkillTree.put(skillHashCode, skillLearn);
  338. }
  339. else if (type.equals("fishingSkillTree"))
  340. {
  341. _fishingSkillTree.put(skillHashCode, skillLearn);
  342. }
  343. else if (type.equals("pledgeSkillTree"))
  344. {
  345. _pledgeSkillTree.put(skillHashCode, skillLearn);
  346. }
  347. else if (type.equals("subClassSkillTree"))
  348. {
  349. _subClassSkillTree.put(skillHashCode, skillLearn);
  350. }
  351. else if (type.equals("subPledgeSkillTree"))
  352. {
  353. _subPledgeSkillTree.put(skillHashCode, skillLearn);
  354. }
  355. else if (type.equals("transformSkillTree"))
  356. {
  357. _transformSkillTree.put(skillHashCode, skillLearn);
  358. }
  359. }
  360. }
  361. }
  362. if (type.equals("classSkillTree"))
  363. {
  364. if (cId != -1)
  365. {
  366. if (_classSkillTrees.get(classId) == null)
  367. {
  368. _classSkillTrees.put(classId, classSkillTree);
  369. }
  370. else
  371. {
  372. _classSkillTrees.get(classId).putAll(classSkillTree);
  373. }
  374. }
  375. }
  376. else if (type.equals("transferSkillTree"))
  377. {
  378. _transferSkillTrees.put(classId, trasferSkillTree);
  379. }
  380. }
  381. }
  382. }
  383. }
  384. }
  385. /**
  386. * Wrapper for class skill trees.
  387. * @return the {@code _classSkillTrees}, if it's null allocate a new map and returns it.
  388. */
  389. private FastMap<ClassId, FastMap<Integer, L2SkillLearn>> getClassSkillTrees()
  390. {
  391. return _classSkillTrees;
  392. }
  393. /**
  394. * Method to get the complete skill tree for a given class id.<br>
  395. * Include all skills common to all classes.<br>
  396. * Includes all parent skill trees.
  397. * @param classId the class skill tree ID.
  398. * @return the complete Class Skill Tree including skill trees from parent class for a given {@code classId}.
  399. */
  400. public FastMap<Integer, L2SkillLearn> getCompleteClassSkillTree(ClassId classId)
  401. {
  402. final FastMap<Integer, L2SkillLearn> skillTree = new FastMap<Integer, L2SkillLearn>();
  403. // Add all skills that belong to all classes.
  404. skillTree.putAll(_commonSkillTree);
  405. while ((classId != null) && (getClassSkillTrees().get(classId) != null))
  406. {
  407. skillTree.putAll(getClassSkillTrees().get(classId));
  408. classId = _parentClassMap.get(classId);
  409. }
  410. return skillTree;
  411. }
  412. /**
  413. * @param classId the transfer skill tree ID.
  414. * @return the complete Transfer Skill Tree for a given {@code classId}.
  415. */
  416. public FastMap<Integer, L2SkillLearn> getTransferSkillTree(ClassId classId)
  417. {
  418. // If new classes are implemented over 3rd class, we use a recursive call.
  419. if (classId.level() >= 3)
  420. {
  421. classId = classId.getParent();
  422. return getTransferSkillTree(classId);
  423. }
  424. return _transferSkillTrees.get(classId);
  425. }
  426. /**
  427. * @return the complete Common Skill Tree.
  428. */
  429. public FastMap<Integer, L2SkillLearn> getCommonSkillTree()
  430. {
  431. return _commonSkillTree;
  432. }
  433. /**
  434. * @return the complete Collect Skill Tree.
  435. */
  436. public FastMap<Integer, L2SkillLearn> getCollectSkillTree()
  437. {
  438. return _collectSkillTree;
  439. }
  440. /**
  441. * @return the complete Fishing Skill Tree.
  442. */
  443. public FastMap<Integer, L2SkillLearn> getFishingSkillTree()
  444. {
  445. return _fishingSkillTree;
  446. }
  447. /**
  448. * @return the complete Pledge Skill Tree.
  449. */
  450. public FastMap<Integer, L2SkillLearn> getPledgeSkillTree()
  451. {
  452. return _pledgeSkillTree;
  453. }
  454. /**
  455. * @return the complete Sub-Class Skill Tree.
  456. */
  457. public FastMap<Integer, L2SkillLearn> getSubClassSkillTree()
  458. {
  459. return _subClassSkillTree;
  460. }
  461. /**
  462. * @return the complete Sub-Pledge Skill Tree.
  463. */
  464. public FastMap<Integer, L2SkillLearn> getSubPledgeSkillTree()
  465. {
  466. return _subPledgeSkillTree;
  467. }
  468. /**
  469. * @return the complete Transform Skill Tree.
  470. */
  471. public FastMap<Integer, L2SkillLearn> getTransformSkillTree()
  472. {
  473. return _transformSkillTree;
  474. }
  475. /**
  476. * @param player the learning skill player.
  477. * @param classId the learning skill class ID.
  478. * @param includeByFs if {@code true} skills from Forgotten Scroll will be included.
  479. * @param includeAutoGet if {@code true} Auto-Get skills will be included.
  480. * @return all available skills for a given {@code player}, {@code classId}, {@code includeByFs} and {@code includeAutoGet}.
  481. */
  482. public FastList<L2SkillLearn> getAvailableSkills(L2PcInstance player, ClassId classId, boolean includeByFs, boolean includeAutoGet)
  483. {
  484. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  485. final FastMap<Integer, L2SkillLearn> skills = getCompleteClassSkillTree(classId);
  486. if (skills.isEmpty())
  487. {
  488. // The Skill Tree for this class is undefined.
  489. _log.warning(getClass().getSimpleName() + ": Skilltree for class " + classId + " is not defined!");
  490. return result;
  491. }
  492. final L2Skill[] oldSkills = player.getAllSkills();
  493. for (L2SkillLearn temp : skills.values())
  494. {
  495. if (((includeAutoGet && temp.isAutoGet()) || temp.isLearnedByNpc() || (includeByFs && temp.isLearnedByFS())) && (player.getLevel() >= temp.getGetLevel()))
  496. {
  497. boolean knownSkill = false;
  498. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  499. {
  500. if (oldSkills[j].getId() == temp.getSkillId())
  501. {
  502. if (oldSkills[j].getLevel() == (temp.getSkillLevel() - 1))
  503. {
  504. // This is the next level of a known skill:
  505. result.add(temp);
  506. }
  507. knownSkill = true;
  508. }
  509. }
  510. if (!knownSkill && (temp.getSkillLevel() == 1))
  511. {
  512. // This is a new skill:
  513. result.add(temp);
  514. }
  515. }
  516. }
  517. return result;
  518. }
  519. /**
  520. * @param player the player requesting the Auto-Get skills.
  521. * @return all the available Auto-Get skills for a given {@code player}.
  522. */
  523. public FastList<L2SkillLearn> getAvailableAutoGetSkills(L2PcInstance player)
  524. {
  525. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  526. final FastMap<Integer, L2SkillLearn> skills = getCompleteClassSkillTree(player.getClassId());
  527. if (skills.size() < 1)
  528. {
  529. // The Skill Tree for this class is undefined, so we return an empty list.
  530. _log.warning(getClass().getSimpleName() + ": Skill Tree for this classId(" + player.getClassId() + ") is not defined!");
  531. return result;
  532. }
  533. final L2Skill[] oldSkills = player.getAllSkills();
  534. for (L2SkillLearn temp : skills.values())
  535. {
  536. if ((temp.getRaces() != null) && Util.contains(temp.getRaces(), 4) && !player.hasDwarvenCraft())
  537. {
  538. continue;
  539. }
  540. if (temp.isAutoGet() && (player.getLevel() >= temp.getGetLevel()))
  541. {
  542. boolean knownSkill = false;
  543. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  544. {
  545. if (oldSkills[j].getId() == temp.getSkillId())
  546. {
  547. if (oldSkills[j].getLevel() < temp.getSkillLevel())
  548. {
  549. result.add(temp);
  550. }
  551. knownSkill = true;
  552. }
  553. }
  554. if (!knownSkill)
  555. {
  556. result.add(temp);
  557. }
  558. }
  559. }
  560. return result;
  561. }
  562. /**
  563. * Dwarvens will get additional dwarven only fishing skills.
  564. * @param player
  565. * @return all the available Fishing skills for a given {@code player}.
  566. */
  567. public FastList<L2SkillLearn> getAvailableFishingSkills(L2PcInstance player)
  568. {
  569. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  570. final FastMap<Integer, L2SkillLearn> skills = new FastMap<Integer, L2SkillLearn>();
  571. skills.putAll(_fishingSkillTree);
  572. if (skills.size() < 1)
  573. {
  574. // The Skill Tree for fishing skills is undefined.
  575. _log.warning(getClass().getSimpleName() + ": Skilltree for fishing is not defined !");
  576. return result;
  577. }
  578. final L2Skill[] oldSkills = player.getAllSkills();
  579. final Race playerRace = player.getRace();
  580. for (L2SkillLearn temp : skills.values())
  581. {
  582. // If skill is Race specific and the player's race isn't allowed, skip it.
  583. if ((temp.getRaces() != null) && !Util.contains(temp.getRaces(), playerRace))
  584. {
  585. continue;
  586. }
  587. if (temp.isLearnedByNpc() && (player.getLevel() >= temp.getGetLevel()))
  588. {
  589. boolean knownSkill = false;
  590. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  591. {
  592. if (oldSkills[j].getId() == temp.getSkillId())
  593. {
  594. if (oldSkills[j].getLevel() == (temp.getSkillLevel() - 1))
  595. {
  596. // This is the next level of a known skill:
  597. result.add(temp);
  598. }
  599. knownSkill = true;
  600. }
  601. }
  602. if (!knownSkill && (temp.getSkillLevel() == 1))
  603. {
  604. // This is a new skill:
  605. result.add(temp);
  606. }
  607. }
  608. }
  609. return result;
  610. }
  611. /**
  612. * Used in Gracia continent.
  613. * @param player the collecting skill learning player.
  614. * @return all the available Collecting skills for a given {@code player}.
  615. */
  616. public FastList<L2SkillLearn> getAvailableCollectSkills(L2PcInstance player)
  617. {
  618. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  619. final FastMap<Integer, L2SkillLearn> skills = new FastMap<Integer, L2SkillLearn>();
  620. skills.putAll(_collectSkillTree);
  621. if (skills.size() < 1)
  622. {
  623. // The Skill Tree for Collecting skills is undefined.
  624. _log.warning(getClass().getSimpleName() + ": Skilltree for collecting skills is not defined !");
  625. return result;
  626. }
  627. final L2Skill[] oldSkills = player.getAllSkills();
  628. for (L2SkillLearn temp : skills.values())
  629. {
  630. boolean knownSkill = false;
  631. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  632. {
  633. if (oldSkills[j].getId() == temp.getSkillId())
  634. {
  635. if (oldSkills[j].getLevel() == (temp.getSkillLevel() - 1))
  636. {
  637. // This is the next level of a known skill:
  638. result.add(temp);
  639. }
  640. knownSkill = true;
  641. }
  642. }
  643. if (!knownSkill && (temp.getSkillLevel() == 1))
  644. {
  645. // This is a new skill:
  646. result.add(temp);
  647. }
  648. }
  649. return result;
  650. }
  651. /**
  652. * @param player the transfer skill learning player.
  653. * @return all the available Transfer skills for a given {@code player}.
  654. */
  655. public FastList<L2SkillLearn> getAvailableTransferSkills(L2PcInstance player)
  656. {
  657. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  658. ClassId classId = player.getClassId();
  659. // If new classes are implemented over 3rd class, a different way should be implemented.
  660. if (classId.level() == 3)
  661. {
  662. classId = classId.getParent();
  663. }
  664. if (_transferSkillTrees.get(classId) == null)
  665. {
  666. return result;
  667. }
  668. for (L2SkillLearn temp : _transferSkillTrees.get(classId).values())
  669. {
  670. // If player doesn't know this transfer skill:
  671. if (player.getKnownSkill(temp.getSkillId()) == null)
  672. {
  673. result.add(temp);
  674. }
  675. }
  676. return result;
  677. }
  678. /**
  679. * Some transformations are not available for some races.
  680. * @param player the transformation skill learning player.
  681. * @return all the available Transformation skills for a given {@code player}.
  682. */
  683. public FastList<L2SkillLearn> getAvailableTransformSkills(L2PcInstance player)
  684. {
  685. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  686. final FastMap<Integer, L2SkillLearn> skills = _transformSkillTree;
  687. if (skills == null)
  688. {
  689. // The Skill Tree for Transformation skills is undefined.
  690. _log.warning(getClass().getSimpleName() + ": No Transform skills defined!");
  691. return result;
  692. }
  693. final L2Skill[] oldSkills = player.getAllSkills();
  694. for (L2SkillLearn temp : skills.values())
  695. {
  696. if ((player.getLevel() >= temp.getGetLevel()) && ((temp.getRaces() == null) || Util.contains(temp.getRaces(), player.getRace().ordinal())))
  697. {
  698. boolean knownSkill = false;
  699. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  700. {
  701. if (oldSkills[j].getId() == temp.getSkillId())
  702. {
  703. if (oldSkills[j].getLevel() == (temp.getSkillLevel() - 1))
  704. {
  705. // This is the next level of a known skill:
  706. result.add(temp);
  707. }
  708. knownSkill = true;
  709. }
  710. }
  711. if (!knownSkill && (temp.getSkillLevel() == 1))
  712. {
  713. // This is a new skill:
  714. result.add(temp);
  715. }
  716. }
  717. }
  718. return result;
  719. }
  720. /**
  721. * @param clan the pledge skill learning clan.
  722. * @return all the available Pledge skills for a given {@code clan}.
  723. */
  724. public FastList<L2SkillLearn> getAvailablePledgeSkills(L2Clan clan)
  725. {
  726. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  727. final FastMap<Integer, L2SkillLearn> skills = _pledgeSkillTree;
  728. if (skills == null)
  729. {
  730. // The Skill Tree for Pledge skills is undefined.
  731. _log.warning(getClass().getSimpleName() + ": No clan skills defined!");
  732. return result;
  733. }
  734. final L2Skill[] oldSkills = clan.getAllSkills();
  735. for (L2SkillLearn temp : skills.values())
  736. {
  737. if (!temp.isResidencialSkill() && (clan.getLevel() >= temp.getGetLevel()))
  738. {
  739. boolean knownSkill = false;
  740. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  741. {
  742. if (oldSkills[j].getId() == temp.getSkillId())
  743. {
  744. if (oldSkills[j].getLevel() == (temp.getSkillLevel() - 1))
  745. {
  746. // This is the next level of a known skill:
  747. result.add(temp);
  748. }
  749. knownSkill = true;
  750. }
  751. }
  752. if (!knownSkill && (temp.getSkillLevel() == 1))
  753. {
  754. // This is a new skill:
  755. result.add(temp);
  756. }
  757. }
  758. }
  759. return result;
  760. }
  761. /**
  762. * @param clan the sub-pledge skill learning clan.
  763. * @return all the available Sub-Pledge skills for a given {@code clan}.
  764. */
  765. public FastList<L2SkillLearn> getAvailableSubPledgeSkills(L2Clan clan)
  766. {
  767. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  768. final FastMap<Integer, L2SkillLearn> skills = _subPledgeSkillTree;
  769. if (skills == null)
  770. {
  771. // The Skill Tree for Sub-Pledge skills is undefined.
  772. _log.warning(getClass().getSimpleName() + ": No sub-clan skills defined!");
  773. return result;
  774. }
  775. for (L2SkillLearn temp : skills.values())
  776. {
  777. if ((clan.getLevel() >= temp.getGetLevel()) && clan.isLearnableSubSkill(temp.getSkillId(), temp.getSkillLevel()))
  778. {
  779. result.add(temp);
  780. }
  781. }
  782. return result;
  783. }
  784. /**
  785. * @param player the sub-class skill learning player.
  786. * @return all the available Sub-Class skills for a given {@code player}.
  787. */
  788. public FastList<L2SkillLearn> getAvailableSubClassSkills(L2PcInstance player)
  789. {
  790. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  791. final FastMap<Integer, L2SkillLearn> skills = _subClassSkillTree;
  792. if (skills == null)
  793. {
  794. // The Skill Tree for Sub-Class skills is undefined.
  795. _log.warning(getClass().getSimpleName() + ": No Sub-Class skills defined!");
  796. return result;
  797. }
  798. final L2Skill[] oldSkills = player.getAllSkills();
  799. for (L2SkillLearn temp : skills.values())
  800. {
  801. if (player.getLevel() >= temp.getGetLevel())
  802. {
  803. int[][] subClassConds = null;
  804. for (SubClass subClass : player.getSubClasses().values())
  805. {
  806. subClassConds = temp.getSubClassConditions();
  807. if ((subClassConds != null) && (subClass.getClassIndex() <= subClassConds.length) && (subClass.getClassIndex() == subClassConds[subClass.getClassIndex() - 1][1]) && (subClassConds[subClass.getClassIndex() - 1][0] <= subClass.getLevel()))
  808. {
  809. boolean knownSkill = false;
  810. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  811. {
  812. if (oldSkills[j].getId() == temp.getSkillId())
  813. {
  814. if (oldSkills[j].getLevel() == (temp.getSkillLevel() - 1))
  815. {
  816. // This is the next level of a known skill:
  817. result.add(temp);
  818. }
  819. knownSkill = true;
  820. }
  821. }
  822. if (!knownSkill && (temp.getSkillLevel() == 1))
  823. {
  824. // This is a new skill:
  825. result.add(temp);
  826. }
  827. }
  828. }
  829. }
  830. }
  831. return result;
  832. }
  833. /**
  834. * @param residenceId the id of the Castle, Fort, Territory.
  835. * @return all the available Residential skills for a given {@code residenceId}.
  836. */
  837. public FastList<L2SkillLearn> getAvailableResidentialSkills(int residenceId)
  838. {
  839. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  840. final FastMap<Integer, L2SkillLearn> skills = _pledgeSkillTree;
  841. if (skills == null)
  842. {
  843. // The Skill Tree for Residential skills is undefined?
  844. _log.warning(getClass().getSimpleName() + ": No residential skills defined!");
  845. return result;
  846. }
  847. for (L2SkillLearn temp : skills.values())
  848. {
  849. if (temp.isResidencialSkill() && (temp.getRecidenceIds() != null) && Util.contains(temp.getRecidenceIds(), residenceId))
  850. {
  851. result.add(temp);
  852. }
  853. }
  854. return result;
  855. }
  856. /**
  857. * @param id the transformation skill ID.
  858. * @param lvl the transformation skill level.
  859. * @return the transform skill from the Transform Skill Tree for a given {@code id} and {@code lvl}.
  860. */
  861. public L2SkillLearn getTransformSkill(int id, int lvl)
  862. {
  863. return _transformSkillTree.get(SkillTable.getSkillHashCode(id, lvl));
  864. }
  865. /**
  866. * @param id the class skill ID.
  867. * @param lvl the class skill level.
  868. * @param classId the class skill tree ID.
  869. * @return the class skill from the Class Skill Trees for a given {@code classId}, {@code id} and {@code lvl}.
  870. */
  871. public L2SkillLearn getClassSkill(int id, int lvl, ClassId classId)
  872. {
  873. final FastMap<Integer, L2SkillLearn> skills = getCompleteClassSkillTree(classId);
  874. return skills.get(SkillTable.getSkillHashCode(id, lvl));
  875. }
  876. /**
  877. * @param id the fishing skill ID.
  878. * @param lvl the fishing skill level.
  879. * @return Fishing skill from the Fishing Skill Tree for a given {@code id} and {@code lvl}.
  880. */
  881. public L2SkillLearn getFishingSkill(int id, int lvl)
  882. {
  883. return _fishingSkillTree.get(SkillTable.getSkillHashCode(id, lvl));
  884. }
  885. /**
  886. * @param id the pledge skill ID.
  887. * @param lvl the pledge skill level.
  888. * @return the pledge skill from the Pledge Skill Tree for a given {@code id} and {@code lvl}.
  889. */
  890. public L2SkillLearn getPledgeSkill(int id, int lvl)
  891. {
  892. return _pledgeSkillTree.get(SkillTable.getSkillHashCode(id, lvl));
  893. }
  894. /**
  895. * @param id the sub-pledge skill ID.
  896. * @param lvl the sub-pledge skill level.
  897. * @return the sub-pledge skill from the Sub-Pledge Skill Tree for a given {@code id} and {@code lvl}.
  898. */
  899. public L2SkillLearn getSubPledgeSkill(int id, int lvl)
  900. {
  901. return _subPledgeSkillTree.get(SkillTable.getSkillHashCode(id, lvl));
  902. }
  903. /**
  904. * @param id the transfer skill ID.
  905. * @param lvl the transfer skill level.
  906. * @param classId the transfer skill tree ID.
  907. * @return the transfer skill from the Transfer Skill Trees for a given {@code classId}, {@code id} and {@code lvl}.
  908. */
  909. public L2SkillLearn getTransferSkill(int id, int lvl, ClassId classId)
  910. {
  911. if (classId.getParent() != null)
  912. {
  913. final ClassId parentId = classId.getParent();
  914. if (_transferSkillTrees.get(parentId) != null)
  915. {
  916. return _transferSkillTrees.get(parentId).get(SkillTable.getSkillHashCode(id, lvl));
  917. }
  918. }
  919. return null;
  920. }
  921. /**
  922. * @param id the sub-class skill ID.
  923. * @param lvl the sub-class skill level.
  924. * @return the sub-class skill from the Sub-Class Skill Tree for a given {@code id} and {@code lvl}.
  925. */
  926. public L2SkillLearn getSubClassSkill(int id, int lvl)
  927. {
  928. return _subClassSkillTree.get(SkillTable.getSkillHashCode(id, lvl));
  929. }
  930. /**
  931. * @param id the common skill Id.
  932. * @param lvl the common skill level.
  933. * @return the common skill from the Common Skill Tree for a given {@code id} and {@code lvl}.
  934. */
  935. public L2SkillLearn getCommonSkill(int id, int lvl)
  936. {
  937. return _commonSkillTree.get(SkillTable.getSkillHashCode(id, lvl));
  938. }
  939. /**
  940. * @param id the collect skill ID.
  941. * @param lvl the collect skill level.
  942. * @return the collect skill from the Collect Skill Tree for a given {@code id} and {@code lvl}.
  943. */
  944. public L2SkillLearn getCollectSkill(int id, int lvl)
  945. {
  946. return _collectSkillTree.get(SkillTable.getSkillHashCode(id, lvl));
  947. }
  948. /**
  949. * @param player the player that requires the minimum level.
  950. * @param skillTree the skill tree to search the minimum get level.
  951. * @return the minimum level for a new skill for a given {@code player} and {@code skillTree}.
  952. */
  953. public int getMinLevelForNewSkill(L2PcInstance player, FastMap<Integer, L2SkillLearn> skillTree)
  954. {
  955. int minLevel = 0;
  956. if (skillTree.isEmpty())
  957. {
  958. _log.warning(getClass().getSimpleName() + ": SkillTree is not defined for getMinLevelForNewSkill!");
  959. }
  960. else
  961. {
  962. for (L2SkillLearn s : skillTree.values())
  963. {
  964. if (s.isLearnedByNpc() && (player.getLevel() < s.getGetLevel()))
  965. {
  966. if ((minLevel == 0) || (minLevel > s.getGetLevel()))
  967. {
  968. minLevel = s.getGetLevel();
  969. }
  970. }
  971. }
  972. }
  973. return minLevel;
  974. }
  975. /**
  976. * Create and store hash values for skills for easy and fast checks.
  977. */
  978. private void generateCheckArrays()
  979. {
  980. int i;
  981. int[] array;
  982. // Class specific skills:
  983. FastMap<Integer, L2SkillLearn> tempMap;
  984. final Set<ClassId> keySet = getClassSkillTrees().keySet();
  985. _skillsByClassIdHashCodes = new TIntObjectHashMap<int[]>(keySet.size());
  986. for (ClassId cls : keySet)
  987. {
  988. i = 0;
  989. tempMap = getCompleteClassSkillTree(cls);
  990. array = new int[tempMap.size()];
  991. for (int h : tempMap.keySet())
  992. {
  993. array[i++] = h;
  994. }
  995. tempMap.clear();
  996. Arrays.sort(array);
  997. _skillsByClassIdHashCodes.put(cls.ordinal(), array);
  998. }
  999. // Race specific skills from Fishing and Transformation skill trees.
  1000. final FastList<Integer> list = new FastList<Integer>();
  1001. _skillsByRaceHashCodes = new TIntObjectHashMap<int[]>(Race.values().length);
  1002. for (Race r : Race.values())
  1003. {
  1004. for (L2SkillLearn s : _fishingSkillTree.values())
  1005. {
  1006. if ((s.getRaces() != null) && Util.contains(s.getRaces(), r))
  1007. {
  1008. list.add(SkillTable.getSkillHashCode(s.getSkillId(), s.getSkillLevel()));
  1009. }
  1010. }
  1011. for (L2SkillLearn s : _transformSkillTree.values())
  1012. {
  1013. if ((s.getRaces() != null) && Util.contains(s.getRaces(), r))
  1014. {
  1015. list.add(SkillTable.getSkillHashCode(s.getSkillId(), s.getSkillLevel()));
  1016. }
  1017. }
  1018. i = 0;
  1019. array = new int[list.size()];
  1020. for (int s : list)
  1021. {
  1022. array[i++] = s;
  1023. }
  1024. Arrays.sort(array);
  1025. _skillsByRaceHashCodes.put(r.ordinal(), array);
  1026. list.clear();
  1027. }
  1028. // Skills available for all classes and races
  1029. for (L2SkillLearn s : _commonSkillTree.values())
  1030. {
  1031. if (s.getRaces() == null)
  1032. {
  1033. list.add(SkillTable.getSkillHashCode(s.getSkillId(), s.getSkillLevel()));
  1034. }
  1035. }
  1036. for (L2SkillLearn s : _fishingSkillTree.values())
  1037. {
  1038. if (s.getRaces() == null)
  1039. {
  1040. list.add(SkillTable.getSkillHashCode(s.getSkillId(), s.getSkillLevel()));
  1041. }
  1042. }
  1043. for (L2SkillLearn s : _transformSkillTree.values())
  1044. {
  1045. if (s.getRaces() == null)
  1046. {
  1047. list.add(SkillTable.getSkillHashCode(s.getSkillId(), s.getSkillLevel()));
  1048. }
  1049. }
  1050. for (L2SkillLearn s : _collectSkillTree.values())
  1051. {
  1052. list.add(SkillTable.getSkillHashCode(s.getSkillId(), s.getSkillLevel()));
  1053. }
  1054. _allSkillsHashCodes = new int[list.size()];
  1055. int j = 0;
  1056. for (int hashcode : list)
  1057. {
  1058. _allSkillsHashCodes[j++] = hashcode;
  1059. }
  1060. Arrays.sort(_allSkillsHashCodes);
  1061. }
  1062. /**
  1063. * Verify if the give skill is valid for the given player.<br>
  1064. * GM's skills are excluded for GM players.
  1065. * @param player the player to verify the skill.
  1066. * @param skill the skill to be verified.
  1067. * @return {@code true} if the skill is allowed to the given player.
  1068. */
  1069. public boolean isSkillAllowed(L2PcInstance player, L2Skill skill)
  1070. {
  1071. if (skill.isExcludedFromCheck())
  1072. {
  1073. return true;
  1074. }
  1075. if (player.isGM() && skill.isGMSkill())
  1076. {
  1077. return true;
  1078. }
  1079. // Prevent accidental skill remove during reload
  1080. if (_loading)
  1081. {
  1082. return true;
  1083. }
  1084. final int maxLvl = SkillTable.getInstance().getMaxLevel(skill.getId());
  1085. final int hashCode = SkillTable.getSkillHashCode(skill.getId(), Math.min(skill.getLevel(), maxLvl));
  1086. if (Arrays.binarySearch(_skillsByClassIdHashCodes.get(player.getClassId().ordinal()), hashCode) >= 0)
  1087. {
  1088. return true;
  1089. }
  1090. if (Arrays.binarySearch(_skillsByRaceHashCodes.get(player.getRace().ordinal()), hashCode) >= 0)
  1091. {
  1092. return true;
  1093. }
  1094. if (Arrays.binarySearch(_allSkillsHashCodes, hashCode) >= 0)
  1095. {
  1096. return true;
  1097. }
  1098. // Exclude Transfer Skills from this check.
  1099. if (getTransferSkill(skill.getId(), Math.min(skill.getLevel(), maxLvl), player.getClassId()) != null)
  1100. {
  1101. return true;
  1102. }
  1103. return false;
  1104. }
  1105. public static SkillTreesData getInstance()
  1106. {
  1107. return SingletonHolder._instance;
  1108. }
  1109. /**
  1110. * Singleton holder for the SkillTreesData class.
  1111. */
  1112. @SuppressWarnings("synthetic-access")
  1113. private static class SingletonHolder
  1114. {
  1115. protected static final SkillTreesData _instance = new SkillTreesData();
  1116. }
  1117. }