SkillTreesData.java 36 KB

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