DocumentBase.java 33 KB

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