NpcData.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.EnumMap;
  26. import java.util.HashMap;
  27. import java.util.HashSet;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import java.util.concurrent.ConcurrentHashMap;
  32. import java.util.function.Predicate;
  33. import java.util.logging.Level;
  34. import java.util.stream.Collectors;
  35. import org.w3c.dom.NamedNodeMap;
  36. import org.w3c.dom.Node;
  37. import com.l2jserver.Config;
  38. import com.l2jserver.L2DatabaseFactory;
  39. import com.l2jserver.gameserver.engines.DocumentParser;
  40. import com.l2jserver.gameserver.enums.AISkillScope;
  41. import com.l2jserver.gameserver.model.L2MinionData;
  42. import com.l2jserver.gameserver.model.StatsSet;
  43. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  44. import com.l2jserver.gameserver.model.base.ClassId;
  45. import com.l2jserver.gameserver.model.drops.DropListScope;
  46. import com.l2jserver.gameserver.model.drops.GeneralDropItem;
  47. import com.l2jserver.gameserver.model.drops.GroupedGeneralDropItem;
  48. import com.l2jserver.gameserver.model.drops.IDropItem;
  49. import com.l2jserver.gameserver.model.effects.L2EffectType;
  50. import com.l2jserver.gameserver.model.holders.SkillHolder;
  51. import com.l2jserver.gameserver.model.skills.Skill;
  52. import com.l2jserver.gameserver.util.Util;
  53. /**
  54. * NPC data parser.
  55. * @author Nos
  56. */
  57. public class NpcData extends DocumentParser
  58. {
  59. private final Map<Integer, L2NpcTemplate> _npcs = new ConcurrentHashMap<>();
  60. private final Map<String, Integer> _clans = new ConcurrentHashMap<>();
  61. // SQL Queries
  62. private static final String SELECT_MINION_ALL = "SELECT * FROM minions ORDER BY boss_id";
  63. protected NpcData()
  64. {
  65. load();
  66. }
  67. @Override
  68. public synchronized void load()
  69. {
  70. parseDatapackDirectory("data/stats/npcs", false);
  71. _log.info(getClass().getSimpleName() + ": Loaded " + _npcs.size() + " NPCs.");
  72. if (Config.CUSTOM_NPC_DATA)
  73. {
  74. final int npcCount = _npcs.size();
  75. parseDatapackDirectory("data/stats/npcs/custom", true);
  76. _log.info(getClass().getSimpleName() + ": Loaded " + (_npcs.size() - npcCount) + " Custom NPCs.");
  77. }
  78. loadMinions();
  79. loadNpcsSkillLearn();
  80. }
  81. @Override
  82. protected void parseDocument()
  83. {
  84. for (Node node = getCurrentDocument().getFirstChild(); node != null; node = node.getNextSibling())
  85. {
  86. if ("list".equalsIgnoreCase(node.getNodeName()))
  87. {
  88. for (Node list_node = node.getFirstChild(); list_node != null; list_node = list_node.getNextSibling())
  89. {
  90. if ("npc".equalsIgnoreCase(list_node.getNodeName()))
  91. {
  92. NamedNodeMap attrs = list_node.getAttributes();
  93. final StatsSet set = new StatsSet();
  94. final int npcId = parseInteger(attrs, "id");
  95. Map<String, Object> parameters = null;
  96. Map<Integer, Skill> skills = null;
  97. Set<Integer> clans = null;
  98. Set<Integer> enemyClans = null;
  99. Map<DropListScope, List<IDropItem>> dropLists = null;
  100. set.set("id", npcId);
  101. set.set("displayId", parseInteger(attrs, "displayId"));
  102. set.set("level", parseByte(attrs, "level"));
  103. set.set("type", parseString(attrs, "type"));
  104. set.set("name", parseString(attrs, "name"));
  105. set.set("usingServerSideName", parseBoolean(attrs, "usingServerSideName"));
  106. set.set("title", parseString(attrs, "title"));
  107. set.set("usingServerSideTitle", parseBoolean(attrs, "usingServerSideTitle"));
  108. for (Node npc_node = list_node.getFirstChild(); npc_node != null; npc_node = npc_node.getNextSibling())
  109. {
  110. attrs = npc_node.getAttributes();
  111. switch (npc_node.getNodeName().toLowerCase())
  112. {
  113. case "parameters":
  114. {
  115. if (parameters == null)
  116. {
  117. parameters = new HashMap<>();
  118. }
  119. for (Node parameters_node = npc_node.getFirstChild(); parameters_node != null; parameters_node = parameters_node.getNextSibling())
  120. {
  121. attrs = parameters_node.getAttributes();
  122. switch (parameters_node.getNodeName().toLowerCase())
  123. {
  124. case "param":
  125. {
  126. parameters.put(parseString(attrs, "name"), parseString(attrs, "value"));
  127. break;
  128. }
  129. case "skill":
  130. {
  131. parameters.put(parseString(attrs, "name"), new SkillHolder(parseInteger(attrs, "id"), parseInteger(attrs, "level")));
  132. break;
  133. }
  134. case "minions":
  135. {
  136. // TODO: Implement me
  137. break;
  138. }
  139. }
  140. }
  141. break;
  142. }
  143. case "race":
  144. case "sex":
  145. set.set(npc_node.getNodeName(), npc_node.getTextContent().toUpperCase());
  146. break;
  147. case "equipment":
  148. {
  149. set.set("chestId", parseInteger(attrs, "chest"));
  150. set.set("rhandId", parseInteger(attrs, "rhand"));
  151. set.set("lhandId", parseInteger(attrs, "lhand"));
  152. set.set("weaponEnchant", parseInteger(attrs, "weaponEnchant"));
  153. break;
  154. }
  155. case "acquire":
  156. {
  157. set.set("expRate", parseDouble(attrs, "expRate"));
  158. set.set("sp", parseDouble(attrs, "sp"));
  159. set.set("raidPoints", parseDouble(attrs, "raidPoints"));
  160. break;
  161. }
  162. case "stats":
  163. {
  164. set.set("baseSTR", parseInteger(attrs, "str"));
  165. set.set("baseINT", parseInteger(attrs, "int"));
  166. set.set("baseDEX", parseInteger(attrs, "dex"));
  167. set.set("baseWIT", parseInteger(attrs, "wit"));
  168. set.set("baseCON", parseInteger(attrs, "con"));
  169. set.set("baseMEN", parseInteger(attrs, "men"));
  170. for (Node stats_node = npc_node.getFirstChild(); stats_node != null; stats_node = stats_node.getNextSibling())
  171. {
  172. attrs = stats_node.getAttributes();
  173. switch (stats_node.getNodeName().toLowerCase())
  174. {
  175. case "vitals":
  176. {
  177. set.set("baseHpMax", parseDouble(attrs, "hp"));
  178. set.set("baseHpReg", parseDouble(attrs, "hpRegen"));
  179. set.set("baseMpMax", parseDouble(attrs, "mp"));
  180. set.set("baseMpReg", parseDouble(attrs, "mpRegen"));
  181. break;
  182. }
  183. case "attack":
  184. {
  185. set.set("basePAtk", parseDouble(attrs, "physical"));
  186. set.set("baseMAtk", parseDouble(attrs, "magical"));
  187. set.set("baseRndDam", parseInteger(attrs, "random"));
  188. set.set("baseCritRate", parseInteger(attrs, "critical"));
  189. set.set("accuracy", parseDouble(attrs, "accuracy"));// TODO: Implement me
  190. set.set("basePAtkSpd", parseInteger(attrs, "attackSpeed"));
  191. set.set("reuseDelay", parseInteger(attrs, "reuseDelay"));// TODO: Implement me
  192. set.set("baseAtkType", parseString(attrs, "type"));
  193. set.set("baseAtkRange", parseInteger(attrs, "range"));
  194. set.set("distance", parseInteger(attrs, "distance"));// TODO: Implement me
  195. set.set("width", parseInteger(attrs, "width"));// TODO: Implement me
  196. break;
  197. }
  198. case "defence":
  199. {
  200. set.set("basePDef", parseDouble(attrs, "physical"));
  201. set.set("baseMDef", parseDouble(attrs, "magical"));
  202. set.set("evasion", parseInteger(attrs, "evasion"));// TODO: Implement me
  203. set.set("baseShldDef", parseInteger(attrs, "shield"));
  204. set.set("baseShldRate", parseInteger(attrs, "shieldRate"));
  205. break;
  206. }
  207. case "attribute":
  208. {
  209. for (Node attribute_node = stats_node.getFirstChild(); attribute_node != null; attribute_node = attribute_node.getNextSibling())
  210. {
  211. attrs = attribute_node.getAttributes();
  212. switch (attribute_node.getNodeName().toLowerCase())
  213. {
  214. case "attack":
  215. {
  216. String attackAttributeType = parseString(attrs, "type");
  217. switch (attackAttributeType.toUpperCase())
  218. {
  219. case "FIRE":
  220. set.set("baseFire", parseInteger(attrs, "value"));
  221. break;
  222. case "WATER":
  223. set.set("baseWater", parseInteger(attrs, "value"));
  224. break;
  225. case "WIND":
  226. set.set("baseWind", parseInteger(attrs, "value"));
  227. break;
  228. case "EARTH":
  229. set.set("baseEarth", parseInteger(attrs, "value"));
  230. break;
  231. case "DARK":
  232. set.set("baseDark", parseInteger(attrs, "value"));
  233. break;
  234. case "HOLY":
  235. set.set("baseHoly", parseInteger(attrs, "value"));
  236. break;
  237. }
  238. break;
  239. }
  240. case "defence":
  241. {
  242. set.set("baseFireRes", parseInteger(attrs, "fire"));
  243. set.set("baseWaterRes", parseInteger(attrs, "water"));
  244. set.set("baseWindRes", parseInteger(attrs, "wind"));
  245. set.set("baseEarthRes", parseInteger(attrs, "earth"));
  246. set.set("baseHolyRes", parseInteger(attrs, "holy"));
  247. set.set("baseDarkRes", parseInteger(attrs, "dark"));
  248. set.set("baseElementRes", parseInteger(attrs, "default"));
  249. break;
  250. }
  251. }
  252. }
  253. break;
  254. }
  255. case "speed":
  256. {
  257. for (Node speed_node = stats_node.getFirstChild(); speed_node != null; speed_node = speed_node.getNextSibling())
  258. {
  259. attrs = speed_node.getAttributes();
  260. switch (speed_node.getNodeName().toLowerCase())
  261. {
  262. case "walk":
  263. {
  264. set.set("baseWalkSpd", parseFloat(attrs, "ground"));
  265. set.set("baseSwimWalkSpd", parseFloat(attrs, "swim"));
  266. set.set("baseFlyWalkSpd", parseFloat(attrs, "fly"));
  267. break;
  268. }
  269. case "run":
  270. {
  271. set.set("baseRunSpd", parseFloat(attrs, "ground"));
  272. set.set("baseSwimRunSpd", parseFloat(attrs, "swim"));
  273. set.set("baseFlyRunSpd", parseFloat(attrs, "fly"));
  274. break;
  275. }
  276. }
  277. }
  278. break;
  279. }
  280. case "hit_time":
  281. set.set("hit_time", npc_node.getTextContent());// TODO: Implement me default 600 (value in ms)
  282. break;
  283. }
  284. }
  285. break;
  286. }
  287. case "status":
  288. {
  289. set.set("unique", parseBoolean(attrs, "unique"));
  290. set.set("attackable", parseBoolean(attrs, "attackable"));
  291. set.set("targetable", parseBoolean(attrs, "targetable"));
  292. set.set("undying", parseBoolean(attrs, "undying"));
  293. set.set("showName", parseBoolean(attrs, "showName"));
  294. set.set("flying", parseBoolean(attrs, "flying"));
  295. set.set("canMove", parseBoolean(attrs, "canMove"));
  296. set.set("noSleepMode", parseBoolean(attrs, "noSleepMode"));
  297. set.set("passableDoor", parseBoolean(attrs, "passableDoor"));
  298. set.set("hasSummoner", parseBoolean(attrs, "hasSummoner"));
  299. set.set("canBeSown", parseBoolean(attrs, "canBeSown"));
  300. break;
  301. }
  302. case "skill_list":
  303. {
  304. skills = new HashMap<>();
  305. for (Node skill_list_node = npc_node.getFirstChild(); skill_list_node != null; skill_list_node = skill_list_node.getNextSibling())
  306. {
  307. if ("skill".equalsIgnoreCase(skill_list_node.getNodeName()))
  308. {
  309. attrs = skill_list_node.getAttributes();
  310. final int skillId = parseInteger(attrs, "id");
  311. final int skillLevel = parseInteger(attrs, "level");
  312. final Skill skill = SkillData.getInstance().getSkill(skillId, skillLevel);
  313. if (skill != null)
  314. {
  315. skills.put(skill.getId(), skill);
  316. }
  317. else
  318. {
  319. _log.warning("[" + getCurrentFile().getName() + "] skill not found. NPC ID: " + npcId + " Skill ID:" + skillId + " Skill Level: " + skillLevel);
  320. }
  321. }
  322. }
  323. break;
  324. }
  325. case "shots":
  326. {
  327. set.set("soulShot", parseInteger(attrs, "soul"));
  328. set.set("spiritShot", parseInteger(attrs, "spirit"));
  329. set.set("shotShotChance", parseInteger(attrs, "shotChance"));
  330. set.set("spiritShotChance", parseInteger(attrs, "spiritChance"));
  331. break;
  332. }
  333. case "corpse_time":
  334. set.set("corpseTime", npc_node.getTextContent());
  335. break;
  336. case "ex_crt_effect":
  337. set.set("ex_crt_effect", npc_node.getTextContent()); // TODO: Implement me default ? type boolean
  338. break;
  339. case "s_npc_prop_hp_rate":
  340. set.set("s_npc_prop_hp_rate", npc_node.getTextContent()); // TODO: Implement me default 1 type double
  341. break;
  342. case "ai":
  343. {
  344. set.set("aiType", parseString(attrs, "type"));
  345. set.set("aggroRange", parseInteger(attrs, "aggroRange"));
  346. set.set("clanHelpRange", parseInteger(attrs, "clanHelpRange"));
  347. set.set("dodge", parseInteger(attrs, "dodge"));
  348. set.set("isChaos", parseBoolean(attrs, "isChaos"));
  349. set.set("isAggressive", parseBoolean(attrs, "isAggressive"));
  350. for (Node ai_node = npc_node.getFirstChild(); ai_node != null; ai_node = ai_node.getNextSibling())
  351. {
  352. attrs = ai_node.getAttributes();
  353. switch (ai_node.getNodeName().toLowerCase())
  354. {
  355. case "skill":
  356. {
  357. set.set("minSkillChance", parseInteger(attrs, "minChance"));
  358. set.set("maxSkillChance", parseInteger(attrs, "maxChance"));
  359. set.set("primarySkillId", parseInteger(attrs, "primaryId"));
  360. set.set("shortRangeSkillId", parseInteger(attrs, "shortRangeId"));
  361. set.set("shortRangeSkillChance", parseInteger(attrs, "shortRangeChance"));
  362. set.set("longRangeSkillId", parseInteger(attrs, "longRangeId"));
  363. set.set("longRangeSkillChance", parseInteger(attrs, "longRangeChance"));
  364. break;
  365. }
  366. case "clan_list":
  367. {
  368. for (Node clan_list_node = ai_node.getFirstChild(); clan_list_node != null; clan_list_node = clan_list_node.getNextSibling())
  369. {
  370. attrs = clan_list_node.getAttributes();
  371. switch (clan_list_node.getNodeName().toLowerCase())
  372. {
  373. case "clan":
  374. {
  375. if (clans == null)
  376. {
  377. clans = new HashSet<>(1);
  378. }
  379. clans.add(getOrCreateClanId(clan_list_node.getTextContent()));
  380. break;
  381. }
  382. case "enemy_clan":
  383. {
  384. if (enemyClans == null)
  385. {
  386. enemyClans = new HashSet<>(1);
  387. }
  388. enemyClans.add(getOrCreateClanId(clan_list_node.getTextContent()));
  389. break;
  390. }
  391. }
  392. }
  393. break;
  394. }
  395. }
  396. }
  397. break;
  398. }
  399. case "drop_lists":
  400. {
  401. for (Node drop_lists_node = npc_node.getFirstChild(); drop_lists_node != null; drop_lists_node = drop_lists_node.getNextSibling())
  402. {
  403. DropListScope dropListScope = null;
  404. try
  405. {
  406. dropListScope = Enum.valueOf(DropListScope.class, drop_lists_node.getNodeName().toUpperCase());
  407. }
  408. catch (Exception e)
  409. {
  410. }
  411. if (dropListScope != null)
  412. {
  413. if (dropLists == null)
  414. {
  415. dropLists = new EnumMap<>(DropListScope.class);
  416. }
  417. List<IDropItem> dropList = new ArrayList<>();
  418. parseDropList(drop_lists_node, dropListScope, dropList);
  419. dropLists.put(dropListScope, Collections.unmodifiableList(dropList));
  420. }
  421. }
  422. break;
  423. }
  424. case "collision":
  425. {
  426. for (Node collision_node = npc_node.getFirstChild(); collision_node != null; collision_node = collision_node.getNextSibling())
  427. {
  428. attrs = collision_node.getAttributes();
  429. switch (collision_node.getNodeName().toLowerCase())
  430. {
  431. case "radius":
  432. {
  433. set.set("collision_radius", parseDouble(attrs, "normal"));
  434. set.set("collisionRadiusGrown", parseDouble(attrs, "grown"));
  435. break;
  436. }
  437. case "height":
  438. {
  439. set.set("collision_height", parseDouble(attrs, "normal"));
  440. set.set("collisionHeightGrown", parseDouble(attrs, "grown"));
  441. break;
  442. }
  443. }
  444. }
  445. break;
  446. }
  447. }
  448. }
  449. L2NpcTemplate template = _npcs.get(npcId);
  450. if (template == null)
  451. {
  452. template = new L2NpcTemplate(set);
  453. _npcs.put(template.getId(), template);
  454. }
  455. else
  456. {
  457. template.set(set);
  458. }
  459. if (parameters != null)
  460. {
  461. // Using unmodifiable map parameters of template are not meant to be changed at runtime.
  462. template.setParameters(new StatsSet(Collections.unmodifiableMap(parameters)));
  463. }
  464. else
  465. {
  466. template.setParameters(null);
  467. }
  468. if (skills != null)
  469. {
  470. Map<AISkillScope, List<Skill>> aiSkillLists = null;
  471. for (Skill skill : skills.values())
  472. {
  473. if (!skill.isPassive())
  474. {
  475. if (aiSkillLists == null)
  476. {
  477. aiSkillLists = new EnumMap<>(AISkillScope.class);
  478. }
  479. List<AISkillScope> aiSkillScopes = new ArrayList<>();
  480. final AISkillScope shortOrLongRangeScope = skill.getCastRange() <= 150 ? AISkillScope.SHORT_RANGE : AISkillScope.SHORT_RANGE;
  481. if (skill.isSuicideAttack())
  482. {
  483. aiSkillScopes.add(AISkillScope.SUICIDE);
  484. }
  485. else
  486. {
  487. aiSkillScopes.add(AISkillScope.GENERAL);
  488. if (skill.isContinuous())
  489. {
  490. if (!skill.isDebuff())
  491. {
  492. aiSkillScopes.add(AISkillScope.BUFF);
  493. }
  494. else
  495. {
  496. aiSkillScopes.add(AISkillScope.DEBUFF);
  497. aiSkillScopes.add(AISkillScope.COT);
  498. aiSkillScopes.add(shortOrLongRangeScope);
  499. }
  500. }
  501. else
  502. {
  503. if (skill.hasEffectType(L2EffectType.DISPEL, L2EffectType.DISPEL_BY_SLOT))
  504. {
  505. aiSkillScopes.add(AISkillScope.NEGATIVE);
  506. aiSkillScopes.add(shortOrLongRangeScope);
  507. }
  508. else if (skill.hasEffectType(L2EffectType.HEAL))
  509. {
  510. aiSkillScopes.add(AISkillScope.HEAL);
  511. }
  512. else if (skill.hasEffectType(L2EffectType.PHYSICAL_ATTACK, L2EffectType.PHYSICAL_ATTACK_HP_LINK, L2EffectType.MAGICAL_ATTACK, L2EffectType.DEATH_LINK, L2EffectType.HP_DRAIN))
  513. {
  514. aiSkillScopes.add(AISkillScope.ATTACK);
  515. aiSkillScopes.add(AISkillScope.UNIVERSAL);
  516. aiSkillScopes.add(shortOrLongRangeScope);
  517. }
  518. else if (skill.hasEffectType(L2EffectType.SLEEP))
  519. {
  520. aiSkillScopes.add(AISkillScope.IMMOBILIZE);
  521. }
  522. else if (skill.hasEffectType(L2EffectType.STUN, L2EffectType.ROOT))
  523. {
  524. aiSkillScopes.add(AISkillScope.IMMOBILIZE);
  525. aiSkillScopes.add(shortOrLongRangeScope);
  526. }
  527. else if (skill.hasEffectType(L2EffectType.MUTE, L2EffectType.FEAR))
  528. {
  529. aiSkillScopes.add(AISkillScope.COT);
  530. aiSkillScopes.add(shortOrLongRangeScope);
  531. }
  532. else if (skill.hasEffectType(L2EffectType.PARALYZE))
  533. {
  534. aiSkillScopes.add(AISkillScope.IMMOBILIZE);
  535. aiSkillScopes.add(shortOrLongRangeScope);
  536. }
  537. else if (skill.hasEffectType(L2EffectType.DMG_OVER_TIME, L2EffectType.DMG_OVER_TIME_PERCENT))
  538. {
  539. aiSkillScopes.add(shortOrLongRangeScope);
  540. }
  541. else if (skill.hasEffectType(L2EffectType.RESURRECTION))
  542. {
  543. aiSkillScopes.add(AISkillScope.RES);
  544. }
  545. else
  546. {
  547. aiSkillScopes.add(AISkillScope.UNIVERSAL);
  548. }
  549. }
  550. }
  551. for (AISkillScope aiSkillScope : aiSkillScopes)
  552. {
  553. List<Skill> aiSkills = aiSkillLists.get(aiSkillScope);
  554. if (aiSkills == null)
  555. {
  556. aiSkills = new ArrayList<>();
  557. aiSkillLists.put(aiSkillScope, aiSkills);
  558. }
  559. aiSkills.add(skill);
  560. }
  561. }
  562. }
  563. template.setSkills(skills);
  564. template.setAISkillLists(aiSkillLists);
  565. }
  566. else
  567. {
  568. template.setSkills(null);
  569. template.setAISkillLists(null);
  570. }
  571. template.setClans(clans);
  572. template.setEnemyClans(enemyClans);
  573. template.setDropLists(dropLists);
  574. }
  575. }
  576. }
  577. }
  578. }
  579. private void parseDropList(Node drop_list_node, DropListScope dropListScope, List<IDropItem> drops)
  580. {
  581. for (Node drop_node = drop_list_node.getFirstChild(); drop_node != null; drop_node = drop_node.getNextSibling())
  582. {
  583. NamedNodeMap attrs = drop_node.getAttributes();
  584. switch (drop_node.getNodeName().toLowerCase())
  585. {
  586. case "group":
  587. {
  588. GroupedGeneralDropItem dropItem = dropListScope.newGroupedDropItem(parseDouble(attrs, "chance"));
  589. List<IDropItem> groupedDropList = new ArrayList<>(2);
  590. for (Node group_node = drop_node.getFirstChild(); group_node != null; group_node = group_node.getNextSibling())
  591. {
  592. parseDropListItem(group_node, dropListScope, groupedDropList);
  593. }
  594. List<GeneralDropItem> items = new ArrayList<>(groupedDropList.size());
  595. for (IDropItem item : groupedDropList)
  596. {
  597. if (item instanceof GeneralDropItem)
  598. {
  599. items.add((GeneralDropItem) item);
  600. }
  601. else
  602. {
  603. _log.warning("[" + getCurrentFile() + "] grouped general drop item supports only general drop item.");
  604. }
  605. }
  606. dropItem.setItems(items);
  607. drops.add(dropItem);
  608. break;
  609. }
  610. default:
  611. {
  612. parseDropListItem(drop_node, dropListScope, drops);
  613. break;
  614. }
  615. }
  616. }
  617. }
  618. private void parseDropListItem(Node drop_list_item, DropListScope dropListScope, List<IDropItem> drops)
  619. {
  620. NamedNodeMap attrs = drop_list_item.getAttributes();
  621. switch (drop_list_item.getNodeName().toLowerCase())
  622. {
  623. case "item":
  624. {
  625. final IDropItem dropItem = dropListScope.newDropItem(parseInteger(attrs, "id"), parseLong(attrs, "min"), parseLong(attrs, "max"), parseDouble(attrs, "chance"));
  626. if (dropItem != null)
  627. {
  628. drops.add(dropItem);
  629. }
  630. break;
  631. }
  632. }
  633. }
  634. /**
  635. * Gets or creates a clan id if it doesnt exists.
  636. * @param clanName the clan name to get or create its id
  637. * @return the clan id for the given clan name
  638. */
  639. private int getOrCreateClanId(String clanName)
  640. {
  641. Integer id = _clans.get(clanName.toUpperCase());
  642. if (id == null)
  643. {
  644. id = _clans.size();
  645. _clans.put(clanName.toUpperCase(), id);
  646. }
  647. return id;
  648. }
  649. /**
  650. * Gets the clan id
  651. * @param clanName the clan name to get its id
  652. * @return the clan id for the given clan name if it exists, -1 otherwise
  653. */
  654. public int getClanId(String clanName)
  655. {
  656. Integer id = _clans.get(clanName.toUpperCase());
  657. return id != null ? id : -1;
  658. }
  659. /**
  660. * Gets the template.
  661. * @param id the template Id to get.
  662. * @return the template for the given id.
  663. */
  664. public L2NpcTemplate getTemplate(int id)
  665. {
  666. return _npcs.get(id);
  667. }
  668. /**
  669. * Gets the template by name.
  670. * @param name of the template to get.
  671. * @return the template for the given name.
  672. */
  673. public L2NpcTemplate getTemplateByName(String name)
  674. {
  675. for (L2NpcTemplate npcTemplate : _npcs.values())
  676. {
  677. if (npcTemplate.getName().equalsIgnoreCase(name))
  678. {
  679. return npcTemplate;
  680. }
  681. }
  682. return null;
  683. }
  684. /**
  685. * Gets all templates matching the filter.
  686. * @param filter
  687. * @return the template list for the given filter
  688. */
  689. public List<L2NpcTemplate> getTemplates(Predicate<L2NpcTemplate> filter)
  690. {
  691. //@formatter:off
  692. return _npcs.values().stream()
  693. .filter(filter)
  694. .collect(Collectors.toList());
  695. //@formatter:on
  696. }
  697. /**
  698. * Gets the all of level.
  699. * @param lvls of all the templates to get.
  700. * @return the template list for the given level.
  701. */
  702. public List<L2NpcTemplate> getAllOfLevel(int... lvls)
  703. {
  704. return getTemplates(template -> Util.contains(lvls, template.getLevel()));
  705. }
  706. /**
  707. * Gets the all monsters of level.
  708. * @param lvls of all the monster templates to get.
  709. * @return the template list for the given level.
  710. */
  711. public List<L2NpcTemplate> getAllMonstersOfLevel(int... lvls)
  712. {
  713. return getTemplates(template -> Util.contains(lvls, template.getLevel()) && template.isType("L2Monster"));
  714. }
  715. /**
  716. * Gets the all npc starting with.
  717. * @param text of all the NPC templates which its name start with.
  718. * @return the template list for the given letter.
  719. */
  720. public List<L2NpcTemplate> getAllNpcStartingWith(String text)
  721. {
  722. return getTemplates(template -> template.isType("L2Npc") && template.getName().startsWith(text));
  723. }
  724. /**
  725. * Gets the all npc of class type.
  726. * @param classTypes of all the templates to get.
  727. * @return the template list for the given class type.
  728. */
  729. public List<L2NpcTemplate> getAllNpcOfClassType(String... classTypes)
  730. {
  731. return getTemplates(template -> Util.contains(classTypes, template.getType(), true));
  732. }
  733. public void loadNpcsSkillLearn()
  734. {
  735. _npcs.values().forEach(template ->
  736. {
  737. final List<ClassId> teachInfo = SkillLearnData.getInstance().getSkillLearnData(template.getId());
  738. if (teachInfo != null)
  739. {
  740. template.addTeachInfo(teachInfo);
  741. }
  742. });
  743. }
  744. public void loadMinions()
  745. {
  746. final String query = SELECT_MINION_ALL;
  747. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  748. Statement statement = con.createStatement())
  749. {
  750. int count = 0;
  751. try (ResultSet rset = statement.executeQuery(query))
  752. {
  753. L2MinionData minionDat = null;
  754. L2NpcTemplate npcDat = null;
  755. int raidId;
  756. while (rset.next())
  757. {
  758. raidId = rset.getInt("boss_id");
  759. npcDat = _npcs.get(raidId);
  760. if (npcDat == null)
  761. {
  762. _log.warning(getClass().getSimpleName() + ": Minion references undefined boss NPC. Boss NpcId: " + raidId);
  763. continue;
  764. }
  765. minionDat = new L2MinionData();
  766. minionDat.setMinionId(rset.getInt("minion_id"));
  767. minionDat.setAmountMin(rset.getInt("amount_min"));
  768. minionDat.setAmountMax(rset.getInt("amount_max"));
  769. npcDat.addMinionData(minionDat);
  770. count++;
  771. }
  772. }
  773. _log.info(getClass().getSimpleName() + ": Loaded " + count + " Minions.");
  774. }
  775. catch (Exception e)
  776. {
  777. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error loading minion data.", e);
  778. }
  779. }
  780. /**
  781. * Gets the single instance of NpcData.
  782. * @return single instance of NpcData
  783. */
  784. public static NpcData getInstance()
  785. {
  786. return SingletonHolder._instance;
  787. }
  788. private static class SingletonHolder
  789. {
  790. protected static final NpcData _instance = new NpcData();
  791. }
  792. }