DocumentBase.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 net.sf.l2j.gameserver.skills;
  16. import java.io.File;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.StringTokenizer;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javolution.text.TextBuilder;
  25. import javolution.util.FastList;
  26. import javolution.util.FastMap;
  27. import net.sf.l2j.Config;
  28. import net.sf.l2j.gameserver.datatables.SkillTable;
  29. import net.sf.l2j.gameserver.model.L2Character;
  30. import net.sf.l2j.gameserver.model.L2Skill;
  31. import net.sf.l2j.gameserver.model.base.Race;
  32. import net.sf.l2j.gameserver.skills.conditions.Condition;
  33. import net.sf.l2j.gameserver.skills.conditions.ConditionChangeWeapon;
  34. import net.sf.l2j.gameserver.skills.conditions.ConditionForceBuff;
  35. import net.sf.l2j.gameserver.skills.conditions.ConditionGameChance;
  36. import net.sf.l2j.gameserver.skills.conditions.ConditionGameTime;
  37. import net.sf.l2j.gameserver.skills.conditions.ConditionLogicAnd;
  38. import net.sf.l2j.gameserver.skills.conditions.ConditionLogicNot;
  39. import net.sf.l2j.gameserver.skills.conditions.ConditionLogicOr;
  40. import net.sf.l2j.gameserver.skills.conditions.ConditionPlayerHp;
  41. import net.sf.l2j.gameserver.skills.conditions.ConditionPlayerHpPercentage;
  42. import net.sf.l2j.gameserver.skills.conditions.ConditionPlayerLevel;
  43. import net.sf.l2j.gameserver.skills.conditions.ConditionPlayerMp;
  44. import net.sf.l2j.gameserver.skills.conditions.ConditionPlayerRace;
  45. import net.sf.l2j.gameserver.skills.conditions.ConditionPlayerState;
  46. import net.sf.l2j.gameserver.skills.conditions.ConditionSkillStats;
  47. import net.sf.l2j.gameserver.skills.conditions.ConditionSlotItemId;
  48. import net.sf.l2j.gameserver.skills.conditions.ConditionTargetActiveEffectId;
  49. import net.sf.l2j.gameserver.skills.conditions.ConditionTargetActiveSkillId;
  50. import net.sf.l2j.gameserver.skills.conditions.ConditionTargetAggro;
  51. import net.sf.l2j.gameserver.skills.conditions.ConditionTargetClassIdRestriction;
  52. import net.sf.l2j.gameserver.skills.conditions.ConditionTargetLevel;
  53. import net.sf.l2j.gameserver.skills.conditions.ConditionTargetRaceId;
  54. import net.sf.l2j.gameserver.skills.conditions.ConditionTargetUsesWeaponKind;
  55. import net.sf.l2j.gameserver.skills.conditions.ConditionUsingItemType;
  56. import net.sf.l2j.gameserver.skills.conditions.ConditionUsingSkill;
  57. import net.sf.l2j.gameserver.skills.conditions.ConditionWithSkill;
  58. import net.sf.l2j.gameserver.skills.conditions.ConditionGameTime.CheckGameTime;
  59. import net.sf.l2j.gameserver.skills.conditions.ConditionPlayerState.CheckPlayerState;
  60. import net.sf.l2j.gameserver.skills.effects.EffectTemplate;
  61. import net.sf.l2j.gameserver.skills.funcs.FuncTemplate;
  62. import net.sf.l2j.gameserver.skills.funcs.Lambda;
  63. import net.sf.l2j.gameserver.skills.funcs.LambdaCalc;
  64. import net.sf.l2j.gameserver.skills.funcs.LambdaConst;
  65. import net.sf.l2j.gameserver.skills.funcs.LambdaStats;
  66. import net.sf.l2j.gameserver.templates.L2ArmorType;
  67. import net.sf.l2j.gameserver.templates.L2Item;
  68. import net.sf.l2j.gameserver.templates.L2Weapon;
  69. import net.sf.l2j.gameserver.templates.L2WeaponType;
  70. import net.sf.l2j.gameserver.templates.StatsSet;
  71. import org.w3c.dom.Document;
  72. import org.w3c.dom.NamedNodeMap;
  73. import org.w3c.dom.Node;
  74. /**
  75. * @author mkizub
  76. *
  77. * TODO To change the template for this generated type comment go to
  78. * Window - Preferences - Java - Code Style - Code Templates
  79. */
  80. abstract class DocumentBase
  81. {
  82. static Logger _log = Logger.getLogger(DocumentBase.class.getName());
  83. private File _file;
  84. protected Map<String, String[]> _tables;
  85. DocumentBase(File pFile)
  86. {
  87. _file = pFile;
  88. _tables = new FastMap<String, String[]>();
  89. }
  90. Document parse()
  91. {
  92. Document doc;
  93. try
  94. {
  95. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  96. factory.setValidating(false);
  97. factory.setIgnoringComments(true);
  98. doc = factory.newDocumentBuilder().parse(_file);
  99. }
  100. catch (Exception e)
  101. {
  102. _log.log(Level.SEVERE, "Error loading file " + _file, e);
  103. return null;
  104. }
  105. try
  106. {
  107. parseDocument(doc);
  108. }
  109. catch (Exception e)
  110. {
  111. _log.log(Level.SEVERE, "Error in file " + _file, e);
  112. return null;
  113. }
  114. return doc;
  115. }
  116. protected abstract void parseDocument(Document doc);
  117. protected abstract StatsSet getStatsSet();
  118. protected abstract String getTableValue(String name);
  119. protected abstract String getTableValue(String name, int idx);
  120. protected void resetTable()
  121. {
  122. _tables = new FastMap<String, String[]>();
  123. }
  124. protected void setTable(String name, String[] table)
  125. {
  126. _tables.put(name, table);
  127. }
  128. protected void parseTemplate(Node n, Object template)
  129. {
  130. Condition condition = null;
  131. n = n.getFirstChild();
  132. if (n == null) return;
  133. if ("cond".equalsIgnoreCase(n.getNodeName()))
  134. {
  135. condition = parseCondition(n.getFirstChild(), template);
  136. Node msg = n.getAttributes().getNamedItem("msg");
  137. if (condition != null && msg != null) condition.setMessage(msg.getNodeValue());
  138. n = n.getNextSibling();
  139. }
  140. for (; n != null; n = n.getNextSibling())
  141. {
  142. if ("add".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Add", condition);
  143. else if ("sub".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Sub", condition);
  144. else if ("mul".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Mul", condition);
  145. else if ("div".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Div", condition);
  146. else if ("set".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Set", condition);
  147. else if ("enchant".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Enchant",
  148. condition);
  149. else if ("skill".equalsIgnoreCase(n.getNodeName())) attachSkill(n, template, condition);
  150. else if ("effect".equalsIgnoreCase(n.getNodeName()))
  151. {
  152. if (template instanceof EffectTemplate) throw new RuntimeException("Nested effects");
  153. attachEffect(n, template, condition);
  154. }
  155. }
  156. }
  157. protected void attachFunc(Node n, Object template, String name, Condition attachCond)
  158. {
  159. Stats stat = Stats.valueOfXml(n.getAttributes().getNamedItem("stat").getNodeValue());
  160. String order = n.getAttributes().getNamedItem("order").getNodeValue();
  161. Lambda lambda = getLambda(n, template);
  162. int ord = Integer.decode(getValue(order, template));
  163. Condition applayCond = parseCondition(n.getFirstChild(), template);
  164. FuncTemplate ft = new FuncTemplate(attachCond, applayCond, name, stat, ord, lambda);
  165. if (template instanceof L2Item) ((L2Item) template).attach(ft);
  166. else if (template instanceof L2Skill) ((L2Skill) template).attach(ft);
  167. else if (template instanceof EffectTemplate) ((EffectTemplate) template).attach(ft);
  168. }
  169. protected void attachLambdaFunc(Node n, Object template, LambdaCalc calc)
  170. {
  171. String name = n.getNodeName();
  172. TextBuilder sb = new TextBuilder(name);
  173. sb.setCharAt(0, Character.toUpperCase(name.charAt(0)));
  174. name = sb.toString();
  175. Lambda lambda = getLambda(n, template);
  176. FuncTemplate ft = new FuncTemplate(null, null, name, null, calc.funcs.length, lambda);
  177. calc.addFunc(ft.getFunc(new Env(), calc));
  178. }
  179. protected void attachEffect(Node n, Object template, Condition attachCond)
  180. {
  181. NamedNodeMap attrs = n.getAttributes();
  182. String name = attrs.getNamedItem("name").getNodeValue();
  183. int time, count = 1;
  184. if (attrs.getNamedItem("count") != null)
  185. {
  186. count = Integer.decode(getValue(attrs.getNamedItem("count").getNodeValue(), template));
  187. }
  188. if (attrs.getNamedItem("time") != null)
  189. {
  190. time = Integer.decode(getValue(attrs.getNamedItem("time").getNodeValue(),template));
  191. if (Config.ENABLE_MODIFY_SKILL_DURATION)
  192. {
  193. if (Config.SKILL_DURATION_LIST.containsKey(((L2Skill) template).getId()))
  194. {
  195. if (((L2Skill) template).getLevel() < 100)
  196. time = Config.SKILL_DURATION_LIST.get(((L2Skill) template).getId());
  197. else if ((((L2Skill) template).getLevel() >= 100) && (((L2Skill) template).getLevel() < 140))
  198. time += Config.SKILL_DURATION_LIST.get(((L2Skill) template).getId());
  199. else if (((L2Skill) template).getLevel() > 140)
  200. time = Config.SKILL_DURATION_LIST.get(((L2Skill) template).getId());
  201. if (Config.DEBUG)
  202. _log.info("*** Skill " + ((L2Skill) template).getName() + " (" + ((L2Skill) template).getLevel() + ") changed duration to " + time + " seconds.");
  203. }
  204. }
  205. }
  206. else time = ((L2Skill) template).getBuffDuration() / 1000 / count;
  207. boolean self = false;
  208. if (attrs.getNamedItem("self") != null)
  209. {
  210. if (Integer.decode(getValue(attrs.getNamedItem("self").getNodeValue(),template)) == 1)
  211. self = true;
  212. }
  213. boolean icon = true;
  214. if (attrs.getNamedItem("noicon") !=null)
  215. {
  216. if (Integer.decode(getValue(attrs.getNamedItem("noicon").getNodeValue(),template)) == 1)
  217. icon = false;
  218. }
  219. Lambda lambda = getLambda(n, template);
  220. Condition applayCond = parseCondition(n.getFirstChild(), template);
  221. int abnormal = 0;
  222. if (attrs.getNamedItem("abnormal") != null)
  223. {
  224. String abn = attrs.getNamedItem("abnormal").getNodeValue();
  225. if (abn.equals("poison")) abnormal = L2Character.ABNORMAL_EFFECT_POISON;
  226. else if (abn.equals("bleeding")) abnormal = L2Character.ABNORMAL_EFFECT_BLEEDING;
  227. else if (abn.equals("flame")) abnormal = L2Character.ABNORMAL_EFFECT_FLAME;
  228. else if (abn.equals("bighead")) abnormal = L2Character.ABNORMAL_EFFECT_BIG_HEAD;
  229. else if (abn.equals("stealth")) abnormal = L2Character.ABNORMAL_EFFECT_STEALTH;
  230. }
  231. float stackOrder = 0;
  232. String stackType = "none";
  233. if (attrs.getNamedItem("stackType") != null)
  234. {
  235. stackType = attrs.getNamedItem("stackType").getNodeValue();
  236. }
  237. if (attrs.getNamedItem("stackOrder") != null)
  238. {
  239. stackOrder = Float.parseFloat(getValue(attrs.getNamedItem("stackOrder").getNodeValue(), template));
  240. }
  241. EffectTemplate lt = new EffectTemplate(attachCond, applayCond, name, lambda, count, time,
  242. abnormal, stackType, stackOrder, icon);
  243. parseTemplate(n, lt);
  244. if (template instanceof L2Item) ((L2Item) template).attach(lt);
  245. else if (template instanceof L2Skill && !self) ((L2Skill) template).attach(lt);
  246. else if (template instanceof L2Skill && self) ((L2Skill) template).attachSelf(lt);
  247. }
  248. protected void attachSkill(Node n, Object template, Condition attachCond)
  249. {
  250. NamedNodeMap attrs = n.getAttributes();
  251. int id = 0, lvl = 1;
  252. if (attrs.getNamedItem("id") != null)
  253. {
  254. id = Integer.decode(getValue(attrs.getNamedItem("id").getNodeValue(), template));
  255. }
  256. if (attrs.getNamedItem("lvl") != null)
  257. {
  258. lvl = Integer.decode(getValue(attrs.getNamedItem("lvl").getNodeValue(), template));
  259. }
  260. L2Skill skill = SkillTable.getInstance().getInfo(id, lvl);
  261. if (attrs.getNamedItem("chance") != null)
  262. {
  263. if (template instanceof L2Weapon || template instanceof L2Item)
  264. {
  265. skill.attach(new ConditionGameChance(Integer.decode(getValue(attrs.getNamedItem("chance").getNodeValue(), template))), true);
  266. }
  267. else
  268. {
  269. skill.attach(new ConditionGameChance(Integer.decode(getValue(attrs.getNamedItem("chance").getNodeValue(), template))), false);
  270. }
  271. }
  272. if (template instanceof L2Weapon)
  273. {
  274. if (attrs.getNamedItem("onUse") != null
  275. || (attrs.getNamedItem("onCrit") == null && attrs.getNamedItem("onCast") == null))
  276. ((L2Weapon) template).attach(skill); // Attach as skill triggered on use
  277. if (attrs.getNamedItem("onCrit") != null) ((L2Weapon) template).attachOnCrit(skill); // Attach as skill triggered on critical hit
  278. if (attrs.getNamedItem("onCast") != null) ((L2Weapon) template).attachOnCast(skill); // Attach as skill triggered on cast
  279. }
  280. else if (template instanceof L2Item)
  281. {
  282. ((L2Item) template).attach(skill); // Attach as skill triggered on use
  283. }
  284. }
  285. protected Condition parseCondition(Node n, Object template)
  286. {
  287. while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
  288. n = n.getNextSibling();
  289. if (n == null) return null;
  290. if ("and".equalsIgnoreCase(n.getNodeName())) return parseLogicAnd(n, template);
  291. if ("or".equalsIgnoreCase(n.getNodeName())) return parseLogicOr(n, template);
  292. if ("not".equalsIgnoreCase(n.getNodeName())) return parseLogicNot(n, template);
  293. if ("player".equalsIgnoreCase(n.getNodeName())) return parsePlayerCondition(n);
  294. if ("target".equalsIgnoreCase(n.getNodeName())) return parseTargetCondition(n, template);
  295. if ("skill".equalsIgnoreCase(n.getNodeName())) return parseSkillCondition(n);
  296. if ("using".equalsIgnoreCase(n.getNodeName())) return parseUsingCondition(n);
  297. if ("game".equalsIgnoreCase(n.getNodeName())) return parseGameCondition(n);
  298. return null;
  299. }
  300. protected Condition parseLogicAnd(Node n, Object template)
  301. {
  302. ConditionLogicAnd cond = new ConditionLogicAnd();
  303. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  304. {
  305. if (n.getNodeType() == Node.ELEMENT_NODE) cond.add(parseCondition(n, template));
  306. }
  307. if (cond.conditions == null || cond.conditions.length == 0)
  308. _log.severe("Empty <and> condition in " + _file);
  309. return cond;
  310. }
  311. protected Condition parseLogicOr(Node n, Object template)
  312. {
  313. ConditionLogicOr cond = new ConditionLogicOr();
  314. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  315. {
  316. if (n.getNodeType() == Node.ELEMENT_NODE) cond.add(parseCondition(n, template));
  317. }
  318. if (cond.conditions == null || cond.conditions.length == 0)
  319. _log.severe("Empty <or> condition in " + _file);
  320. return cond;
  321. }
  322. protected Condition parseLogicNot(Node n, Object template)
  323. {
  324. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  325. {
  326. if (n.getNodeType() == Node.ELEMENT_NODE)
  327. {
  328. return new ConditionLogicNot(parseCondition(n, template));
  329. }
  330. }
  331. _log.severe("Empty <not> condition in " + _file);
  332. return null;
  333. }
  334. protected Condition parsePlayerCondition(Node n)
  335. {
  336. Condition cond = null;
  337. byte[] forces = new byte[2];
  338. NamedNodeMap attrs = n.getAttributes();
  339. for (int i = 0; i < attrs.getLength(); i++)
  340. {
  341. Node a = attrs.item(i);
  342. if ("race".equalsIgnoreCase(a.getNodeName()))
  343. {
  344. Race race = Race.valueOf(a.getNodeValue());
  345. cond = joinAnd(cond, new ConditionPlayerRace(race));
  346. }
  347. else if ("level".equalsIgnoreCase(a.getNodeName()))
  348. {
  349. int lvl = Integer.decode(getValue(a.getNodeValue(), null));
  350. cond = joinAnd(cond, new ConditionPlayerLevel(lvl));
  351. }
  352. else if ("resting".equalsIgnoreCase(a.getNodeName()))
  353. {
  354. boolean val = Boolean.valueOf(a.getNodeValue());
  355. cond = joinAnd(cond, new ConditionPlayerState(CheckPlayerState.RESTING, val));
  356. }
  357. else if ("flying".equalsIgnoreCase(a.getNodeName()))
  358. {
  359. boolean val = Boolean.valueOf(a.getNodeValue());
  360. cond = joinAnd(cond, new ConditionPlayerState(CheckPlayerState.FLYING, val));
  361. }
  362. else if ("moving".equalsIgnoreCase(a.getNodeName()))
  363. {
  364. boolean val = Boolean.valueOf(a.getNodeValue());
  365. cond = joinAnd(cond, new ConditionPlayerState(CheckPlayerState.MOVING, val));
  366. }
  367. else if ("running".equalsIgnoreCase(a.getNodeName()))
  368. {
  369. boolean val = Boolean.valueOf(a.getNodeValue());
  370. cond = joinAnd(cond, new ConditionPlayerState(CheckPlayerState.RUNNING, val));
  371. }
  372. else if ("behind".equalsIgnoreCase(a.getNodeName()))
  373. {
  374. boolean val = Boolean.valueOf(a.getNodeValue());
  375. cond = joinAnd(cond, new ConditionPlayerState(CheckPlayerState.BEHIND, val));
  376. }
  377. else if ("front".equalsIgnoreCase(a.getNodeName()))
  378. {
  379. boolean val = Boolean.valueOf(a.getNodeValue());
  380. cond = joinAnd(cond, new ConditionPlayerState(CheckPlayerState.FRONT, val));
  381. }
  382. else if ("hp".equalsIgnoreCase(a.getNodeName()))
  383. {
  384. int hp = Integer.decode(getValue(a.getNodeValue(), null));
  385. cond = joinAnd(cond, new ConditionPlayerHp(hp));
  386. }
  387. else if ("hprate".equalsIgnoreCase(a.getNodeName()))
  388. {
  389. double rate = Double.parseDouble(getValue(a.getNodeValue(), null));
  390. cond = joinAnd(cond, new ConditionPlayerHpPercentage(rate));
  391. }
  392. else if ("mp".equalsIgnoreCase(a.getNodeName()))
  393. {
  394. int hp = Integer.decode(getValue(a.getNodeValue(), null));
  395. cond = joinAnd(cond, new ConditionPlayerMp(hp));
  396. }
  397. else if ("battle_force".equalsIgnoreCase(a.getNodeName()))
  398. {
  399. forces[0] = Byte.decode(getValue(a.getNodeValue(), null));
  400. }
  401. else if ("spell_force".equalsIgnoreCase(a.getNodeName()))
  402. {
  403. forces[1] = Byte.decode(getValue(a.getNodeValue(), null));
  404. }
  405. }
  406. if(forces[0] + forces[1] > 0)
  407. {
  408. cond = joinAnd(cond, new ConditionForceBuff(forces));
  409. }
  410. if (cond == null) _log.severe("Unrecognized <player> condition in " + _file);
  411. return cond;
  412. }
  413. protected Condition parseTargetCondition(Node n, Object template)
  414. {
  415. Condition cond = null;
  416. NamedNodeMap attrs = n.getAttributes();
  417. for (int i = 0; i < attrs.getLength(); i++)
  418. {
  419. Node a = attrs.item(i);
  420. if ("aggro".equalsIgnoreCase(a.getNodeName()))
  421. {
  422. boolean val = Boolean.valueOf(a.getNodeValue());
  423. cond = joinAnd(cond, new ConditionTargetAggro(val));
  424. }
  425. else if ("level".equalsIgnoreCase(a.getNodeName()))
  426. {
  427. int lvl = Integer.decode(getValue(a.getNodeValue(), template));
  428. cond = joinAnd(cond, new ConditionTargetLevel(lvl));
  429. }
  430. else if ("class_id_restriction".equalsIgnoreCase(a.getNodeName()))
  431. {
  432. FastList<Integer> array = new FastList<Integer>();
  433. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  434. while (st.hasMoreTokens())
  435. {
  436. String item = st.nextToken().trim();
  437. array.add(Integer.decode(getValue(item, null)));
  438. }
  439. cond = joinAnd(cond, new ConditionTargetClassIdRestriction(array));
  440. }
  441. else if ("active_effect_id".equalsIgnoreCase(a.getNodeName()))
  442. {
  443. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  444. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id));
  445. }
  446. else if ("active_skill_id".equalsIgnoreCase(a.getNodeName()))
  447. {
  448. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  449. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id));
  450. }
  451. else if ("race_id".equalsIgnoreCase(a.getNodeName()))
  452. {
  453. ArrayList<Integer> array = new ArrayList<Integer>();
  454. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  455. while (st.hasMoreTokens())
  456. {
  457. String item = st.nextToken().trim();
  458. array.add(Integer.decode(getValue(item, null)));
  459. }
  460. cond = joinAnd(cond, new ConditionTargetRaceId(array));
  461. }
  462. else if ("using".equalsIgnoreCase(a.getNodeName()))
  463. {
  464. int mask = 0;
  465. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  466. while (st.hasMoreTokens())
  467. {
  468. String item = st.nextToken().trim();
  469. for (L2WeaponType wt : L2WeaponType.values())
  470. {
  471. if (wt.toString().equals(item))
  472. {
  473. mask |= wt.mask();
  474. break;
  475. }
  476. }
  477. for (L2ArmorType at : L2ArmorType.values())
  478. {
  479. if (at.toString().equals(item))
  480. {
  481. mask |= at.mask();
  482. break;
  483. }
  484. }
  485. }
  486. cond = joinAnd(cond, new ConditionTargetUsesWeaponKind(mask));
  487. }
  488. }
  489. if (cond == null) _log.severe("Unrecognized <target> condition in " + _file);
  490. return cond;
  491. }
  492. protected Condition parseSkillCondition(Node n)
  493. {
  494. NamedNodeMap attrs = n.getAttributes();
  495. Stats stat = Stats.valueOfXml(attrs.getNamedItem("stat").getNodeValue());
  496. return new ConditionSkillStats(stat);
  497. }
  498. protected Condition parseUsingCondition(Node n)
  499. {
  500. Condition cond = null;
  501. NamedNodeMap attrs = n.getAttributes();
  502. for (int i = 0; i < attrs.getLength(); i++)
  503. {
  504. Node a = attrs.item(i);
  505. if ("kind".equalsIgnoreCase(a.getNodeName()))
  506. {
  507. int mask = 0;
  508. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  509. while (st.hasMoreTokens())
  510. {
  511. String item = st.nextToken().trim();
  512. for (L2WeaponType wt : L2WeaponType.values())
  513. {
  514. if (wt.toString().equals(item))
  515. {
  516. mask |= wt.mask();
  517. break;
  518. }
  519. }
  520. for (L2ArmorType at : L2ArmorType.values())
  521. {
  522. if (at.toString().equals(item))
  523. {
  524. mask |= at.mask();
  525. break;
  526. }
  527. }
  528. }
  529. cond = joinAnd(cond, new ConditionUsingItemType(mask));
  530. }
  531. else if ("skill".equalsIgnoreCase(a.getNodeName()))
  532. {
  533. int id = Integer.parseInt(a.getNodeValue());
  534. cond = joinAnd(cond, new ConditionUsingSkill(id));
  535. }
  536. else if ("slotitem".equalsIgnoreCase(a.getNodeName()))
  537. {
  538. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ";");
  539. int id = Integer.parseInt(st.nextToken().trim());
  540. int slot = Integer.parseInt(st.nextToken().trim());
  541. int enchant = 0;
  542. if (st.hasMoreTokens()) enchant = Integer.parseInt(st.nextToken().trim());
  543. cond = joinAnd(cond, new ConditionSlotItemId(slot, id, enchant));
  544. }
  545. else if ("weaponChange".equalsIgnoreCase(a.getNodeName()))
  546. {
  547. boolean val = Boolean.valueOf(a.getNodeValue());
  548. cond = joinAnd(cond, new ConditionChangeWeapon(val));
  549. }
  550. }
  551. if (cond == null) _log.severe("Unrecognized <using> condition in " + _file);
  552. return cond;
  553. }
  554. protected Condition parseGameCondition(Node n)
  555. {
  556. Condition cond = null;
  557. NamedNodeMap attrs = n.getAttributes();
  558. for (int i = 0; i < attrs.getLength(); i++)
  559. {
  560. Node a = attrs.item(i);
  561. if ("skill".equalsIgnoreCase(a.getNodeName()))
  562. {
  563. boolean val = Boolean.valueOf(a.getNodeValue());
  564. cond = joinAnd(cond, new ConditionWithSkill(val));
  565. }
  566. if ("night".equalsIgnoreCase(a.getNodeName()))
  567. {
  568. boolean val = Boolean.valueOf(a.getNodeValue());
  569. cond = joinAnd(cond, new ConditionGameTime(CheckGameTime.NIGHT, val));
  570. }
  571. if ("chance".equalsIgnoreCase(a.getNodeName()))
  572. {
  573. int val = Integer.decode(getValue(a.getNodeValue(), null));
  574. cond = joinAnd(cond, new ConditionGameChance(val));
  575. }
  576. }
  577. if (cond == null) _log.severe("Unrecognized <game> condition in " + _file);
  578. return cond;
  579. }
  580. protected void parseTable(Node n)
  581. {
  582. NamedNodeMap attrs = n.getAttributes();
  583. String name = attrs.getNamedItem("name").getNodeValue();
  584. if (name.charAt(0) != '#') throw new IllegalArgumentException("Table name must start with #");
  585. StringTokenizer data = new StringTokenizer(n.getFirstChild().getNodeValue());
  586. List<String> array = new FastList<String>();
  587. while (data.hasMoreTokens())
  588. array.add(data.nextToken());
  589. String[] res = new String[array.size()];
  590. int i = 0;
  591. for (String str : array)
  592. {
  593. res[i++] = str;
  594. }
  595. setTable(name, res);
  596. }
  597. protected void parseBeanSet(Node n, StatsSet set, Integer level)
  598. {
  599. String name = n.getAttributes().getNamedItem("name").getNodeValue().trim();
  600. String value = n.getAttributes().getNamedItem("val").getNodeValue().trim();
  601. char ch = value.length() == 0 ? ' ' : value.charAt(0);
  602. if (ch == '#' || ch == '-' || Character.isDigit(ch)) set.set(name,
  603. String.valueOf(getValue(value,
  604. level)));
  605. else set.set(name, value);
  606. }
  607. protected Lambda getLambda(Node n, Object template)
  608. {
  609. Node nval = n.getAttributes().getNamedItem("val");
  610. if (nval != null)
  611. {
  612. String val = nval.getNodeValue();
  613. if (val.charAt(0) == '#')
  614. { // table by level
  615. return new LambdaConst(Double.parseDouble(getTableValue(val)));
  616. }
  617. else if (val.charAt(0) == '$')
  618. {
  619. if (val.equalsIgnoreCase("$player_level"))
  620. return new LambdaStats(LambdaStats.StatsType.PLAYER_LEVEL);
  621. if (val.equalsIgnoreCase("$target_level"))
  622. return new LambdaStats(LambdaStats.StatsType.TARGET_LEVEL);
  623. if (val.equalsIgnoreCase("$player_max_hp"))
  624. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_HP);
  625. if (val.equalsIgnoreCase("$player_max_mp"))
  626. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_MP);
  627. // try to find value out of item fields
  628. StatsSet set = getStatsSet();
  629. String field = set.getString(val.substring(1));
  630. if (field != null)
  631. {
  632. return new LambdaConst(Double.parseDouble(getValue(field, template)));
  633. }
  634. // failed
  635. throw new IllegalArgumentException("Unknown value " + val);
  636. }
  637. else
  638. {
  639. return new LambdaConst(Double.parseDouble(val));
  640. }
  641. }
  642. LambdaCalc calc = new LambdaCalc();
  643. n = n.getFirstChild();
  644. while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
  645. n = n.getNextSibling();
  646. if (n == null || !"val".equals(n.getNodeName()))
  647. throw new IllegalArgumentException("Value not specified");
  648. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  649. {
  650. if (n.getNodeType() != Node.ELEMENT_NODE) continue;
  651. attachLambdaFunc(n, template, calc);
  652. }
  653. return calc;
  654. }
  655. protected String getValue(String value, Object template)
  656. {
  657. // is it a table?
  658. if (value.charAt(0) == '#')
  659. {
  660. if (template instanceof L2Skill) return getTableValue(value);
  661. else if (template instanceof Integer) return getTableValue(value, ((Integer) template).intValue());
  662. else throw new IllegalStateException();
  663. }
  664. return value;
  665. }
  666. protected Condition joinAnd(Condition cond, Condition c)
  667. {
  668. if (cond == null) return c;
  669. if (cond instanceof ConditionLogicAnd)
  670. {
  671. ((ConditionLogicAnd) cond).add(c);
  672. return cond;
  673. }
  674. ConditionLogicAnd and = new ConditionLogicAnd();
  675. and.add(cond);
  676. and.add(c);
  677. return and;
  678. }
  679. }