DocumentBase.java 31 KB

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