SkillTreesData.java 38 KB

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