DocumentBase.java 31 KB

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