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. import com.l2jserver.util.file.filter.XMLFilter;
  37. /**
  38. * @author Zoey76
  39. */
  40. public final class SkillTreesData
  41. {
  42. private static final Logger _log = Logger.getLogger(SkillTreesData.class.getName());
  43. //ClassId, FastMap of Skill Hash Code, L2LearkSkill
  44. private FastMap<ClassId, FastMap<Integer, L2SkillLearn>> _classSkillTrees;
  45. private FastMap<ClassId, FastMap<Integer, L2SkillLearn>> _transferSkillTrees;
  46. //Skill Hash Code, L2LearkSkill
  47. private FastMap<Integer, L2SkillLearn> _collectSkillTree;
  48. private FastMap<Integer, L2SkillLearn> _fishingSkillTree;
  49. private FastMap<Integer, L2SkillLearn> _pledgeSkillTree;
  50. private FastMap<Integer, L2SkillLearn> _subClassSkillTree;
  51. private FastMap<Integer, L2SkillLearn> _subPledgeSkillTree;
  52. private FastMap<Integer, L2SkillLearn> _transformSkillTree;
  53. //TODO: Unhardcode?
  54. //Checker, sorted arrays of hash codes
  55. private TIntObjectHashMap<int[]> _skillsByClassIdHashCodes; //Occupation skills
  56. private TIntObjectHashMap<int[]> _skillsByRaceHashCodes; // race-specific transformations
  57. private int[] _allSkillsHashCodes; // fishing, collection and all races transformations
  58. private boolean _loading = true;
  59. /**
  60. * Parent class IDs are read from XML and stored in this map, to allow easy customization.
  61. */
  62. private final FastMap<ClassId, ClassId> _parentClassMap = new FastMap<ClassId, ClassId>();
  63. private SkillTreesData()
  64. {
  65. load();
  66. }
  67. public void load()
  68. {
  69. _loading = true;
  70. _classSkillTrees = new FastMap<ClassId, FastMap<Integer, L2SkillLearn>>();
  71. _collectSkillTree = new FastMap<Integer, L2SkillLearn>();
  72. _fishingSkillTree = new FastMap<Integer, L2SkillLearn>();
  73. _pledgeSkillTree = new FastMap<Integer, L2SkillLearn>();
  74. _subClassSkillTree = new FastMap<Integer, L2SkillLearn>();
  75. _subPledgeSkillTree = new FastMap<Integer, L2SkillLearn>();
  76. _transferSkillTrees = new FastMap<ClassId, FastMap<Integer, L2SkillLearn>>();
  77. _transformSkillTree = new FastMap<Integer, L2SkillLearn>();
  78. //Load files.
  79. _loading = loadFiles();
  80. int classSkillTreeCount = 0;
  81. for (ClassId classId : _classSkillTrees.keySet())
  82. {
  83. classSkillTreeCount += _classSkillTrees.get(classId).size();
  84. }
  85. int trasferSkillTreeCount = 0;
  86. for (ClassId classId : _transferSkillTrees.keySet())
  87. {
  88. trasferSkillTreeCount += _transferSkillTrees.get(classId).size();
  89. }
  90. int fishingDwarvenSkillCount = 0;
  91. for (L2SkillLearn fishSkill : _fishingSkillTree.values())
  92. {
  93. if ((fishSkill.getRaces() != null) && Util.contains(fishSkill.getRaces(), 4))
  94. {
  95. fishingDwarvenSkillCount++;
  96. }
  97. }
  98. int residentialSkillCount = 0;
  99. for (L2SkillLearn pledgeSkill : _pledgeSkillTree.values())
  100. {
  101. if (pledgeSkill.isResidencialSkill())
  102. {
  103. residentialSkillCount++;
  104. }
  105. }
  106. _log.info(getClass().getSimpleName() + ": Loaded " + classSkillTreeCount + " Class Skills for " + _classSkillTrees.size() + " Class Skill Trees.");
  107. _log.info(getClass().getSimpleName() + ": Loaded " + _subClassSkillTree.size() + " Sub-Class Skills.");
  108. _log.info(getClass().getSimpleName() + ": Loaded " + trasferSkillTreeCount + " Transfer Skills for " + _transferSkillTrees.size() + " Transfer Skill Trees.");
  109. _log.info(getClass().getSimpleName() + ": Loaded " + _fishingSkillTree.size() + " Fishing Skills, " + fishingDwarvenSkillCount + " Dwarven only Fishing Skills.");
  110. _log.info(getClass().getSimpleName() + ": Loaded " + _collectSkillTree.size() + " Collect Skills.");
  111. _log.info(getClass().getSimpleName() + ": Loaded " + _pledgeSkillTree.size() + " Pledge Skills, " + (_pledgeSkillTree.size() - residentialSkillCount) + " for Pledge and " + residentialSkillCount + " Residential.");
  112. _log.info(getClass().getSimpleName() + ": Loaded " + _subPledgeSkillTree.size() + " Sub-Pledge Skills.");
  113. _log.info(getClass().getSimpleName() + ": Loaded " + _transformSkillTree.size() + " Transform Skills.");
  114. }
  115. /**
  116. * Loads all files type xml from data/skillTrees/ and call the parser for each one of them.
  117. * @return {@code false} when the files are loaded.
  118. */
  119. private boolean loadFiles()
  120. {
  121. File folder = new File(Config.DATAPACK_ROOT, "data/skillTrees/");
  122. File[] listOfFiles = folder.listFiles(new XMLFilter());
  123. for (File f : listOfFiles)
  124. {
  125. loadSkillTree(f);
  126. }
  127. return false;
  128. }
  129. /**
  130. * Parse a skill tree file and store it into the correct skill tree.
  131. * @param file the xml file to be parsed.
  132. */
  133. private void loadSkillTree(File file)
  134. {
  135. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  136. dbf.setValidating(false);
  137. dbf.setIgnoringComments(true);
  138. Document doc = null;
  139. if (file.exists())
  140. {
  141. try
  142. {
  143. doc = dbf.newDocumentBuilder().parse(file);
  144. }
  145. catch (Exception e)
  146. {
  147. _log.warning(getClass().getSimpleName() + ": Could not parse " + file.getName() + " file: " + e.getMessage());
  148. }
  149. NamedNodeMap attributes;
  150. Node attribute;
  151. String type = null;
  152. int cId = -1;
  153. int parentClassId = -1;
  154. ClassId classId = null;
  155. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  156. {
  157. if ("list".equalsIgnoreCase(n.getNodeName()))
  158. {
  159. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  160. {
  161. FastMap<Integer, L2SkillLearn> classSkillTree = new FastMap<Integer, L2SkillLearn>();
  162. FastMap<Integer, L2SkillLearn> trasferSkillTree = new FastMap<Integer, L2SkillLearn>();
  163. if ("skillTree".equalsIgnoreCase(d.getNodeName()))
  164. {
  165. attribute = d.getAttributes().getNamedItem("type");
  166. if (attribute == null)
  167. {
  168. _log.warning(getClass().getSimpleName() + ": Skill Tree without type!");
  169. continue;
  170. }
  171. type = attribute.getNodeValue();
  172. attribute = d.getAttributes().getNamedItem("classId");
  173. if (attribute != null)
  174. {
  175. cId = Integer.parseInt(attribute.getNodeValue());
  176. classId = ClassId.values()[cId];
  177. }
  178. attribute = d.getAttributes().getNamedItem("parentClassId");
  179. if (attribute != null)
  180. {
  181. parentClassId = Integer.parseInt(attribute.getNodeValue());
  182. if ((cId != parentClassId) && (parentClassId > -1))
  183. {
  184. _parentClassMap.putIfAbsent(classId, ClassId.values()[parentClassId]);
  185. }
  186. }
  187. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  188. {
  189. if ("skill".equalsIgnoreCase(c.getNodeName()))
  190. {
  191. final StatsSet learnSkillSet = new StatsSet();
  192. int skillId;
  193. int skillLvl;
  194. attributes = c.getAttributes();
  195. attribute = attributes.getNamedItem("skillName");
  196. if (attribute == null)
  197. {
  198. _log.severe(getClass().getSimpleName() + ": Missing skillName, skipping!");
  199. continue;
  200. }
  201. learnSkillSet.set("skillName", attribute.getNodeValue());
  202. attribute = attributes.getNamedItem("skillIdLvl");
  203. if (attribute == null)
  204. {
  205. _log.severe(getClass().getSimpleName() + ": Missing skillIdLvl, skipping!");
  206. continue;
  207. }
  208. try
  209. {
  210. skillId = Integer.parseInt(attribute.getNodeValue().split(",")[0]);
  211. skillLvl = Integer.parseInt(attribute.getNodeValue().split(",")[1]);
  212. learnSkillSet.set("skillId", skillId);
  213. learnSkillSet.set("skillLvl", skillLvl);
  214. }
  215. catch (Exception e)
  216. {
  217. _log.severe(getClass().getSimpleName() + ": Malformed skillIdLvl, skipping!");
  218. continue;
  219. }
  220. attribute = attributes.getNamedItem("getLevel");
  221. if (attribute != null)
  222. {
  223. learnSkillSet.set("getLevel", attribute.getNodeValue());
  224. }
  225. attribute = attributes.getNamedItem("autoGet");
  226. if (attribute != null)
  227. {
  228. learnSkillSet.set("autoGet", attribute.getNodeValue());
  229. }
  230. attribute = attributes.getNamedItem("levelUpSp");
  231. if (attribute != null)
  232. {
  233. learnSkillSet.set("levelUpSp", attribute.getNodeValue());
  234. }
  235. attribute = attributes.getNamedItem("itemsIdCount");
  236. if (attribute != null)
  237. {
  238. learnSkillSet.set("itemsIdCount", attribute.getNodeValue());
  239. }
  240. attribute = attributes.getNamedItem("race");
  241. if (attribute != null)
  242. {
  243. learnSkillSet.set("race", attribute.getNodeValue());
  244. }
  245. attribute = attributes.getNamedItem("preReqSkillIdLvl");
  246. if (attribute != null)
  247. {
  248. learnSkillSet.set("preReqSkillIdLvl", attribute.getNodeValue());
  249. }
  250. attribute = attributes.getNamedItem("socialClass");
  251. if (attribute != null)
  252. {
  253. learnSkillSet.set("socialClass", attribute.getNodeValue());
  254. }
  255. attribute = attributes.getNamedItem("subClassLvlNumber");
  256. if (attribute != null)
  257. {
  258. learnSkillSet.set("subClassLvlNumber", attribute.getNodeValue());
  259. }
  260. attribute = attributes.getNamedItem("residenceSkill");
  261. if (attribute != null)
  262. {
  263. learnSkillSet.set("residenceSkill", attribute.getNodeValue());
  264. }
  265. attribute = attributes.getNamedItem("residenceIds");
  266. if (attribute != null)
  267. {
  268. learnSkillSet.set("residenceIds", attribute.getNodeValue());
  269. }
  270. attribute = attributes.getNamedItem("learnedByNpc");
  271. if (attribute != null)
  272. {
  273. learnSkillSet.set("learnedByNpc", attribute.getNodeValue());
  274. }
  275. attribute = attributes.getNamedItem("learnedByFS");
  276. if (attribute != null)
  277. {
  278. learnSkillSet.set("learnedByFS", attribute.getNodeValue());
  279. }
  280. final L2SkillLearn skillLearn = new L2SkillLearn(learnSkillSet);
  281. if (type.equals("classSkillTree"))
  282. {
  283. classSkillTree.put(SkillTable.getSkillHashCode(skillId, skillLvl), skillLearn);
  284. }
  285. else if (type.equals("transferSkillTree"))
  286. {
  287. trasferSkillTree.put(SkillTable.getSkillHashCode(skillId, skillLvl), skillLearn);
  288. }
  289. else
  290. {
  291. if (type.equals("collectSkillTree"))
  292. {
  293. _collectSkillTree.put(SkillTable.getSkillHashCode(skillId, skillLvl), skillLearn);
  294. }
  295. else if (type.equals("fishingSkillTree"))
  296. {
  297. _fishingSkillTree.put(SkillTable.getSkillHashCode(skillId, skillLvl), skillLearn);
  298. }
  299. else if (type.equals("pledgeSkillTree"))
  300. {
  301. _pledgeSkillTree.put(SkillTable.getSkillHashCode(skillId, skillLvl), skillLearn);
  302. }
  303. else if (type.equals("subClassSkillTree"))
  304. {
  305. _subClassSkillTree.put(SkillTable.getSkillHashCode(skillId, skillLvl), skillLearn);
  306. }
  307. else if (type.equals("subPledgeSkillTree"))
  308. {
  309. _subPledgeSkillTree.put(SkillTable.getSkillHashCode(skillId, skillLvl), skillLearn);
  310. }
  311. else if (type.equals("transformSkillTree"))
  312. {
  313. _transformSkillTree.put(SkillTable.getSkillHashCode(skillId, skillLvl), skillLearn);
  314. }
  315. }
  316. }
  317. }
  318. if (type.equals("classSkillTree"))
  319. {
  320. if (_classSkillTrees.get(classId) == null)
  321. {
  322. _classSkillTrees.put(classId, classSkillTree);
  323. }
  324. else
  325. {
  326. _classSkillTrees.get(classId).putAll(classSkillTree);
  327. }
  328. }
  329. else if (type.equals("transferSkillTree"))
  330. {
  331. _transferSkillTrees.put(classId, trasferSkillTree);
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. generateCheckArrays();
  339. }
  340. /**
  341. * Wrapper for class skill trees.
  342. * @return the {@code _classSkillTrees}, if it's null allocate a new map and returns it.
  343. */
  344. private FastMap<ClassId, FastMap<Integer, L2SkillLearn>> getClassSkillTrees()
  345. {
  346. if (_classSkillTrees == null)
  347. {
  348. _classSkillTrees = new FastMap<ClassId, FastMap<Integer, L2SkillLearn>>();
  349. }
  350. return _classSkillTrees;
  351. }
  352. /**
  353. * Method to get the complete skill tree for a given class id.<br>
  354. * Includes all parent skill trees.
  355. * @param classId the class skill tree ID.
  356. * @return the complete Class Skill Tree including skill trees from parent class for a given {@code classId}.
  357. */
  358. public FastMap<Integer, L2SkillLearn> getCompleteClassSkillTree(ClassId classId)
  359. {
  360. final FastMap<Integer, L2SkillLearn> skillTree = new FastMap<Integer, L2SkillLearn>();
  361. while ((classId != null) && (getClassSkillTrees().get(classId) != null))
  362. {
  363. skillTree.putAll(getClassSkillTrees().get(classId));
  364. classId = _parentClassMap.get(classId);
  365. }
  366. return skillTree;
  367. }
  368. /**
  369. * @param classId the transfer skill tree ID.
  370. * @return the complete Transfer Skill Tree for a given {@code classId}.
  371. */
  372. public FastMap<Integer, L2SkillLearn> getTransferSkillTree(ClassId classId)
  373. {
  374. //If new classes are implemented over 3rd class, we use a recursive call.
  375. if (classId.level() >= 3)
  376. {
  377. classId = classId.getParent();
  378. return getTransferSkillTree(classId);
  379. }
  380. return _transferSkillTrees.get(classId);
  381. }
  382. /**
  383. * @return the complete Collect Skill Tree.
  384. */
  385. public FastMap<Integer, L2SkillLearn> getCollectSkillTree()
  386. {
  387. return _collectSkillTree;
  388. }
  389. /**
  390. * @return the complete Fishing Skill Tree.
  391. */
  392. public FastMap<Integer, L2SkillLearn> getFishingSkillTree()
  393. {
  394. return _fishingSkillTree;
  395. }
  396. /**
  397. * @return the complete Pledge Skill Tree.
  398. */
  399. public FastMap<Integer, L2SkillLearn> getPledgeSkillTree()
  400. {
  401. return _pledgeSkillTree;
  402. }
  403. /**
  404. * @return the complete Sub-Class Skill Tree.
  405. */
  406. public FastMap<Integer, L2SkillLearn> getSubClassSkillTree()
  407. {
  408. return _subClassSkillTree;
  409. }
  410. /**
  411. * @return the complete Sub-Pledge Skill Tree.
  412. */
  413. public FastMap<Integer, L2SkillLearn> getSubPledgeSkillTree()
  414. {
  415. return _subPledgeSkillTree;
  416. }
  417. /**
  418. * @return the complete Transform Skill Tree.
  419. */
  420. public FastMap<Integer, L2SkillLearn> getTransformSkillTree()
  421. {
  422. return _transformSkillTree;
  423. }
  424. /**
  425. * @param player the learning skill player.
  426. * @param classId the learning skill class ID.
  427. * @param includeByFs if {@code true} skills from Forgotten Scroll will be included.
  428. * @param includeAutoGet if {@code true} Auto-Get skills will be included.
  429. * @return all available skills for a given {@code player}, {@code classId}, {@code includeByFs} and {@code includeAutoGet}.
  430. */
  431. public FastList<L2SkillLearn> getAvailableSkills(L2PcInstance player, ClassId classId, boolean includeByFs, boolean includeAutoGet)
  432. {
  433. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  434. final FastMap<Integer, L2SkillLearn> skills = getCompleteClassSkillTree(classId);
  435. if (skills.isEmpty())
  436. {
  437. //The Skill Tree for this class is undefined.
  438. _log.warning(getClass().getSimpleName() + ": Skilltree for class " + classId + " is not defined!");
  439. return result;
  440. }
  441. final L2Skill[] oldSkills = player.getAllSkills();
  442. for (L2SkillLearn temp : skills.values())
  443. {
  444. if (((includeAutoGet && temp.isAutoGet()) || temp.isLearnedByNpc() || (includeByFs && temp.isLearnedByFS())) && (player.getLevel() >= temp.getGetLevel()))
  445. {
  446. boolean knownSkill = false;
  447. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  448. {
  449. if (oldSkills[j].getId() == temp.getSkillId())
  450. {
  451. if (oldSkills[j].getLevel() == (temp.getSkillLevel() - 1))
  452. {
  453. //This is the next level of a known skill:
  454. result.add(temp);
  455. }
  456. knownSkill = true;
  457. }
  458. }
  459. if (!knownSkill && (temp.getSkillLevel() == 1))
  460. {
  461. //This is a new skill:
  462. result.add(temp);
  463. }
  464. }
  465. }
  466. return result;
  467. }
  468. /**
  469. * @param player the player requesting the Auto-Get skills.
  470. * @return all the available Auto-Get skills for a given {@code player}.
  471. */
  472. public FastList<L2SkillLearn> getAvailableAutoGetSkills(L2PcInstance player)
  473. {
  474. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  475. final FastMap<Integer, L2SkillLearn> skills = getCompleteClassSkillTree(player.getClassId());
  476. if (skills.size() < 1)
  477. {
  478. //The Skill Tree for this class is undefined, so we return an empty list.
  479. _log.warning(getClass().getSimpleName() + ": Skill Tree for this classId(" + player.getClassId() + ") is not defined!");
  480. return new FastList<L2SkillLearn>();
  481. }
  482. final L2Skill[] oldSkills = player.getAllSkills();
  483. for (L2SkillLearn temp : skills.values())
  484. {
  485. if ((temp.getRaces() != null) && Util.contains(temp.getRaces(), 4) && !player.hasDwarvenCraft())
  486. {
  487. continue;
  488. }
  489. if (temp.isAutoGet() && (player.getLevel() >= temp.getGetLevel()))
  490. {
  491. boolean knownSkill = false;
  492. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  493. {
  494. if (oldSkills[j].getId() == temp.getSkillId())
  495. {
  496. if (oldSkills[j].getLevel() < temp.getSkillLevel())
  497. {
  498. result.add(temp);
  499. }
  500. knownSkill = true;
  501. }
  502. }
  503. if (!knownSkill)
  504. {
  505. result.add(temp);
  506. }
  507. }
  508. }
  509. return result;
  510. }
  511. /**
  512. * Dwarvens will get additional dwarven only fishing skills.
  513. * @param player
  514. * @return all the available Fishing skills for a given {@code player}.
  515. */
  516. public FastList<L2SkillLearn> getAvailableFishingSkills(L2PcInstance player)
  517. {
  518. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  519. final FastMap<Integer, L2SkillLearn> skills = new FastMap<Integer, L2SkillLearn>();
  520. skills.putAll(_fishingSkillTree);
  521. if (skills.size() < 1)
  522. {
  523. //The Skill Tree for fishing skills is undefined.
  524. _log.warning(getClass().getSimpleName() + ": Skilltree for fishing is not defined !");
  525. return new FastList<L2SkillLearn>();
  526. }
  527. final L2Skill[] oldSkills = player.getAllSkills();
  528. for (L2SkillLearn temp : skills.values())
  529. {
  530. //If skill is Dwarven only and player is not Dwarven.
  531. if ((temp.getRaces() != null) && Util.contains(temp.getRaces(), 4) && !player.hasDwarvenCraft())
  532. {
  533. continue;
  534. }
  535. if (temp.isLearnedByNpc() && (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() - 1))
  543. {
  544. //This is the next level of a known skill:
  545. result.add(temp);
  546. }
  547. knownSkill = true;
  548. }
  549. }
  550. if (!knownSkill && (temp.getSkillLevel() == 1))
  551. {
  552. //This is a new skill:
  553. result.add(temp);
  554. }
  555. }
  556. }
  557. return result;
  558. }
  559. /**
  560. * Used in Gracia continent.
  561. * @param player the collecting skill learning player.
  562. * @return all the available Collecting skills for a given {@code player}.
  563. */
  564. public FastList<L2SkillLearn> getAvailableCollectSkills(L2PcInstance player)
  565. {
  566. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  567. final FastMap<Integer, L2SkillLearn> skills = new FastMap<Integer, L2SkillLearn>();
  568. skills.putAll(_collectSkillTree);
  569. if (skills.size() < 1)
  570. {
  571. //The Skill Tree for Collecting skills is undefined.
  572. _log.warning(getClass().getSimpleName() + ": Skilltree for collecting skills is not defined !");
  573. return new FastList<L2SkillLearn>();
  574. }
  575. final L2Skill[] oldSkills = player.getAllSkills();
  576. for (L2SkillLearn temp : skills.values())
  577. {
  578. boolean knownSkill = false;
  579. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  580. {
  581. if (oldSkills[j].getId() == temp.getSkillId())
  582. {
  583. if (oldSkills[j].getLevel() == (temp.getSkillLevel() - 1))
  584. {
  585. //This is the next level of a known skill:
  586. result.add(temp);
  587. }
  588. knownSkill = true;
  589. }
  590. }
  591. if (!knownSkill && (temp.getSkillLevel() == 1))
  592. {
  593. //This is a new skill:
  594. result.add(temp);
  595. }
  596. }
  597. return result;
  598. }
  599. /**
  600. * @param player the transfer skill learning player.
  601. * @return all the available Transfer skills for a given {@code player}.
  602. */
  603. public FastList<L2SkillLearn> getAvailableTransferSkills(L2PcInstance player)
  604. {
  605. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  606. ClassId classId = player.getClassId();
  607. //If new classes are implemented over 3rd class, a different way should be implemented.
  608. if (classId.level() == 3)
  609. {
  610. classId = classId.getParent();
  611. }
  612. if (_transferSkillTrees.get(classId) == null)
  613. {
  614. return result;
  615. }
  616. for (L2SkillLearn temp : _transferSkillTrees.get(classId).values())
  617. {
  618. //If player doesn't know this transfer skill:
  619. if (player.getKnownSkill(temp.getSkillId()) == null)
  620. {
  621. result.add(temp);
  622. }
  623. }
  624. return result;
  625. }
  626. /**
  627. * Some transformations are not available for some races.
  628. * @param playerthe transformation skill learning player.
  629. * @return all the available Transformation skills for a given {@code player}.
  630. */
  631. public FastList<L2SkillLearn> getAvailableTransformSkills(L2PcInstance player)
  632. {
  633. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  634. final FastMap<Integer, L2SkillLearn> skills = _transformSkillTree;
  635. if (skills == null)
  636. {
  637. //The Skill Tree for Transformation skills is undefined.
  638. _log.warning(getClass().getSimpleName() + ": No Transform skills defined!");
  639. return new FastList<L2SkillLearn>();
  640. }
  641. final L2Skill[] oldSkills = player.getAllSkills();
  642. for (L2SkillLearn temp : skills.values())
  643. {
  644. if ((player.getLevel() >= temp.getGetLevel()) && ((temp.getRaces() == null) || Util.contains(temp.getRaces(), player.getRace().ordinal())))
  645. {
  646. boolean knownSkill = false;
  647. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  648. {
  649. if (oldSkills[j].getId() == temp.getSkillId())
  650. {
  651. if (oldSkills[j].getLevel() == (temp.getSkillLevel() - 1))
  652. {
  653. //This is the next level of a known skill:
  654. result.add(temp);
  655. }
  656. knownSkill = true;
  657. }
  658. }
  659. if (!knownSkill && (temp.getSkillLevel() == 1))
  660. {
  661. //This is a new skill:
  662. result.add(temp);
  663. }
  664. }
  665. }
  666. return result;
  667. }
  668. /**
  669. * @param clan the pledge skill learning clan.
  670. * @return all the available Pledge skills for a given {@code clan}.
  671. */
  672. public FastList<L2SkillLearn> getAvailablePledgeSkills(L2Clan clan)
  673. {
  674. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  675. final FastMap<Integer, L2SkillLearn> skills = _pledgeSkillTree;
  676. if (skills == null)
  677. {
  678. //The Skill Tree for Pledge skills is undefined.
  679. _log.warning(getClass().getSimpleName() + ": No clan skills defined!");
  680. return new FastList<L2SkillLearn>();
  681. }
  682. L2Skill[] oldSkills = clan.getAllSkills();
  683. for (L2SkillLearn temp : skills.values())
  684. {
  685. if (!temp.isResidencialSkill() && (clan.getLevel() >= temp.getGetLevel()))
  686. {
  687. boolean knownSkill = false;
  688. for (int j = 0; (j < oldSkills.length) && !knownSkill; j++)
  689. {
  690. if (oldSkills[j].getId() == temp.getSkillId())
  691. {
  692. if (oldSkills[j].getLevel() == (temp.getSkillLevel() - 1))
  693. {
  694. //This is the next level of a known skill:
  695. result.add(temp);
  696. }
  697. knownSkill = true;
  698. }
  699. }
  700. if (!knownSkill && (temp.getSkillLevel() == 1))
  701. {
  702. //This is a new skill:
  703. result.add(temp);
  704. }
  705. }
  706. }
  707. return result;
  708. }
  709. /**
  710. * @param clan the sub-pledge skill learning clan.
  711. * @return all the available Sub-Pledge skills for a given {@code clan}.
  712. */
  713. public FastList<L2SkillLearn> getAvailableSubPledgeSkills(L2Clan clan)
  714. {
  715. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  716. final FastMap<Integer, L2SkillLearn> skills = _subPledgeSkillTree;
  717. if (skills == null)
  718. {
  719. //The Skill Tree for Sub-Pledge skills is undefined.
  720. _log.warning(getClass().getSimpleName() + ": No sub-clan skills defined!");
  721. return new FastList<L2SkillLearn>();
  722. }
  723. for (L2SkillLearn temp : skills.values())
  724. {
  725. if ((clan.getLevel() >= temp.getGetLevel()) && clan.isLearnableSubSkill(temp.getSkillId(), temp.getSkillLevel()))
  726. {
  727. result.add(temp);
  728. }
  729. }
  730. return result;
  731. }
  732. /**
  733. * @param player the sub-class skill learning player.
  734. * @return all the available Sub-Class skills for a given {@code player}.
  735. */
  736. public FastList<L2SkillLearn> getAvailableSubClassSkills(L2PcInstance player)
  737. {
  738. final FastList<L2SkillLearn> result = new FastList<L2SkillLearn>();
  739. final FastMap<Integer, L2SkillLearn> skills = _subClassSkillTree;
  740. if (skills == null)
  741. {
  742. //The Skill Tree for Sub-Class skills is undefined.
  743. _log.warning(getClass().getSimpleName() + ": No Sub-Class skills defined!");
  744. return new FastList<L2SkillLearn>();
  745. }
  746. final L2Skill[] oldSkills = player.getAllSkills();
  747. for (L2SkillLearn temp : skills.values())
  748. {
  749. if (player.getLevel() >= temp.getGetLevel())
  750. {
  751. int[][] subClassConds = null;
  752. for (SubClass subClass : player.getSubClasses().values())
  753. {
  754. subClassConds = temp.getSubClassConditions();
  755. if ((subClassConds != null) && (subClass.getClassIndex() <= subClassConds.length) && (subClass.getClassIndex() == subClassConds[subClass.getClassIndex() - 1][1]) && (subClassConds[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. }