SkillTreesData.java 38 KB

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