SkillTreesData.java 38 KB

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