DocumentBase.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 com.l2jserver.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.NoSuchElementException;
  21. import java.util.StringTokenizer;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import javax.xml.parsers.DocumentBuilderFactory;
  25. import javolution.util.FastList;
  26. import javolution.util.FastMap;
  27. import org.w3c.dom.Document;
  28. import org.w3c.dom.NamedNodeMap;
  29. import org.w3c.dom.Node;
  30. import com.l2jserver.Config;
  31. import com.l2jserver.gameserver.datatables.SkillTable;
  32. import com.l2jserver.gameserver.model.ChanceCondition;
  33. import com.l2jserver.gameserver.model.L2Skill;
  34. import com.l2jserver.gameserver.model.base.PlayerState;
  35. import com.l2jserver.gameserver.model.base.Race;
  36. import com.l2jserver.gameserver.skills.conditions.*;
  37. import com.l2jserver.gameserver.skills.conditions.ConditionGameTime.CheckGameTime;
  38. import com.l2jserver.gameserver.skills.effects.EffectChanceSkillTrigger;
  39. import com.l2jserver.gameserver.skills.funcs.FuncTemplate;
  40. import com.l2jserver.gameserver.skills.funcs.Lambda;
  41. import com.l2jserver.gameserver.skills.funcs.LambdaCalc;
  42. import com.l2jserver.gameserver.skills.funcs.LambdaConst;
  43. import com.l2jserver.gameserver.skills.funcs.LambdaStats;
  44. import com.l2jserver.gameserver.templates.StatsSet;
  45. import com.l2jserver.gameserver.templates.effects.EffectTemplate;
  46. import com.l2jserver.gameserver.templates.item.L2ArmorType;
  47. import com.l2jserver.gameserver.templates.item.L2Item;
  48. import com.l2jserver.gameserver.templates.item.L2Weapon;
  49. import com.l2jserver.gameserver.templates.item.L2WeaponType;
  50. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  51. /**
  52. * @author mkizub
  53. *
  54. * TODO To change the template for this generated type comment go to
  55. * Window - Preferences - Java - Code Style - Code Templates
  56. */
  57. abstract class DocumentBase
  58. {
  59. static Logger _log = Logger.getLogger(DocumentBase.class.getName());
  60. private File _file;
  61. protected Map<String, String[]> _tables;
  62. DocumentBase(File pFile)
  63. {
  64. _file = pFile;
  65. _tables = new FastMap<String, String[]>();
  66. }
  67. Document parse()
  68. {
  69. Document doc;
  70. try
  71. {
  72. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  73. factory.setValidating(false);
  74. factory.setIgnoringComments(true);
  75. doc = factory.newDocumentBuilder().parse(_file);
  76. }
  77. catch (Exception e)
  78. {
  79. _log.log(Level.SEVERE, "Error loading file " + _file, e);
  80. return null;
  81. }
  82. try
  83. {
  84. parseDocument(doc);
  85. }
  86. catch (Exception e)
  87. {
  88. _log.log(Level.SEVERE, "Error in file " + _file, e);
  89. return null;
  90. }
  91. return doc;
  92. }
  93. protected abstract void parseDocument(Document doc);
  94. protected abstract StatsSet getStatsSet();
  95. protected abstract String getTableValue(String name);
  96. protected abstract String getTableValue(String name, int idx);
  97. protected void resetTable()
  98. {
  99. _tables = new FastMap<String, String[]>();
  100. }
  101. protected void setTable(String name, String[] table)
  102. {
  103. _tables.put(name, table);
  104. }
  105. protected void parseTemplate(Node n, Object template)
  106. {
  107. Condition condition = null;
  108. n = n.getFirstChild();
  109. if (n == null) return;
  110. if ("cond".equalsIgnoreCase(n.getNodeName()))
  111. {
  112. condition = parseCondition(n.getFirstChild(), template);
  113. Node msg = n.getAttributes().getNamedItem("msg");
  114. Node msgId = n.getAttributes().getNamedItem("msgId");
  115. if (condition != null && msg != null)
  116. condition.setMessage(msg.getNodeValue());
  117. else if (condition != null && msgId != null)
  118. {
  119. condition.setMessageId(Integer.decode(getValue(msgId.getNodeValue(), null)));
  120. Node addName = n.getAttributes().getNamedItem("addName");
  121. if (addName != null && Integer.decode(getValue(msgId.getNodeValue(), null)) > 0)
  122. condition.addName();
  123. }
  124. n = n.getNextSibling();
  125. }
  126. for (; n != null; n = n.getNextSibling())
  127. {
  128. if ("add".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Add", condition);
  129. else if ("sub".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Sub", condition);
  130. else if ("mul".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Mul", condition);
  131. else if ("basemul".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "BaseMul", condition);
  132. else if ("div".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Div", condition);
  133. else if ("set".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Set", condition);
  134. else if ("enchant".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "Enchant", condition);
  135. else if ("enchanthp".equalsIgnoreCase(n.getNodeName())) attachFunc(n, template, "EnchantHp", condition);
  136. //else if ("skill".equalsIgnoreCase(n.getNodeName())) attachSkill(n, template, condition);
  137. else if ("effect".equalsIgnoreCase(n.getNodeName()))
  138. {
  139. if (template instanceof EffectTemplate) throw new RuntimeException("Nested effects");
  140. attachEffect(n, template, condition);
  141. }
  142. }
  143. }
  144. protected void attachFunc(Node n, Object template, String name, Condition attachCond)
  145. {
  146. Stats stat = Stats.valueOfXml(n.getAttributes().getNamedItem("stat").getNodeValue());
  147. String order = n.getAttributes().getNamedItem("order").getNodeValue();
  148. Lambda lambda = getLambda(n, template);
  149. int ord = Integer.decode(getValue(order, template));
  150. Condition applayCond = parseCondition(n.getFirstChild(), template);
  151. FuncTemplate ft = new FuncTemplate(attachCond, applayCond, name, stat, ord, lambda);
  152. if (template instanceof L2Item) ((L2Item) template).attach(ft);
  153. else if (template instanceof L2Skill) ((L2Skill) template).attach(ft);
  154. else if (template instanceof EffectTemplate) ((EffectTemplate) template).attach(ft);
  155. }
  156. protected void attachLambdaFunc(Node n, Object template, LambdaCalc calc)
  157. {
  158. String name = n.getNodeName();
  159. final StringBuilder sb = new StringBuilder(name);
  160. sb.setCharAt(0, Character.toUpperCase(name.charAt(0)));
  161. name = sb.toString();
  162. Lambda lambda = getLambda(n, template);
  163. FuncTemplate ft = new FuncTemplate(null, null, name, null, calc.funcs.length, lambda);
  164. calc.addFunc(ft.getFunc(new Env(), calc));
  165. }
  166. protected void attachEffect(Node n, Object template, Condition attachCond)
  167. {
  168. NamedNodeMap attrs = n.getAttributes();
  169. String name = attrs.getNamedItem("name").getNodeValue().intern();
  170. /**
  171. * Keep this values as default ones, DP needs it
  172. */
  173. int time = 1;
  174. int count = 1;
  175. if (attrs.getNamedItem("count") != null)
  176. {
  177. count = Integer.decode(getValue(attrs.getNamedItem("count").getNodeValue(), template));
  178. }
  179. if (attrs.getNamedItem("time") != null)
  180. {
  181. time = Integer.decode(getValue(attrs.getNamedItem("time").getNodeValue(),template));
  182. if (Config.ENABLE_MODIFY_SKILL_DURATION)
  183. {
  184. if (Config.SKILL_DURATION_LIST.containsKey(((L2Skill) template).getId()))
  185. {
  186. if (((L2Skill) template).getLevel() < 100)
  187. time = Config.SKILL_DURATION_LIST.get(((L2Skill) template).getId());
  188. else if ((((L2Skill) template).getLevel() >= 100) && (((L2Skill) template).getLevel() < 140))
  189. time += Config.SKILL_DURATION_LIST.get(((L2Skill) template).getId());
  190. else if (((L2Skill) template).getLevel() > 140)
  191. time = Config.SKILL_DURATION_LIST.get(((L2Skill) template).getId());
  192. if (Config.DEBUG)
  193. _log.info("*** Skill " + ((L2Skill) template).getName() + " (" + ((L2Skill) template).getLevel() + ") changed duration to " + time + " seconds.");
  194. }
  195. }
  196. }
  197. else if (((L2Skill) template).getBuffDuration() > 0)
  198. time = ((L2Skill) template).getBuffDuration() / 1000 / count;
  199. boolean self = false;
  200. if (attrs.getNamedItem("self") != null)
  201. {
  202. if (Integer.decode(getValue(attrs.getNamedItem("self").getNodeValue(),template)) == 1)
  203. self = true;
  204. }
  205. boolean icon = true;
  206. if (attrs.getNamedItem("noicon") !=null)
  207. {
  208. if (Integer.decode(getValue(attrs.getNamedItem("noicon").getNodeValue(),template)) == 1)
  209. icon = false;
  210. }
  211. Lambda lambda = getLambda(n, template);
  212. Condition applayCond = parseCondition(n.getFirstChild(), template);
  213. AbnormalEffect abnormal = AbnormalEffect.NULL;
  214. if (attrs.getNamedItem("abnormal") != null)
  215. {
  216. String abn = attrs.getNamedItem("abnormal").getNodeValue();
  217. abnormal = AbnormalEffect.getByName(abn);
  218. }
  219. AbnormalEffect special = AbnormalEffect.NULL;
  220. if (attrs.getNamedItem("special") != null)
  221. {
  222. String spc = attrs.getNamedItem("special").getNodeValue();
  223. special = AbnormalEffect.getByName(spc);
  224. }
  225. float stackOrder = 0;
  226. String stackType = "none";
  227. if (attrs.getNamedItem("stackType") != null)
  228. {
  229. stackType = attrs.getNamedItem("stackType").getNodeValue();
  230. }
  231. if (attrs.getNamedItem("stackOrder") != null)
  232. {
  233. stackOrder = Float.parseFloat(getValue(attrs.getNamedItem("stackOrder").getNodeValue(), template));
  234. }
  235. double effectPower = -1;
  236. if (attrs.getNamedItem("effectPower") != null)
  237. effectPower = Double.parseDouble( getValue(attrs.getNamedItem("effectPower").getNodeValue(), template));
  238. L2SkillType type = null;
  239. if (attrs.getNamedItem("effectType") != null)
  240. {
  241. String typeName = getValue(attrs.getNamedItem("effectType").getNodeValue(), template);
  242. try
  243. {
  244. type = Enum.valueOf(L2SkillType.class, typeName);
  245. }
  246. catch (Exception e)
  247. {
  248. throw new IllegalArgumentException("Not skilltype found for: "+typeName);
  249. }
  250. }
  251. if (effectPower > -1 && type == null && _log.isLoggable(Level.WARNING))
  252. _log.log(Level.WARNING, "Missing effectType for effect: "+name);
  253. EffectTemplate lt;
  254. final boolean isChanceSkillTrigger = (name == EffectChanceSkillTrigger.class.getName());
  255. int trigId = 0;
  256. if (attrs.getNamedItem("triggeredId") != null)
  257. trigId = Integer.parseInt(getValue(attrs.getNamedItem("triggeredId").getNodeValue(), template));
  258. else if (isChanceSkillTrigger)
  259. throw new NoSuchElementException(name + " requires triggerId");
  260. int trigLvl = 1;
  261. if (attrs.getNamedItem("triggeredLevel") != null)
  262. trigLvl = Integer.parseInt(getValue(attrs.getNamedItem("triggeredLevel").getNodeValue(), template));
  263. String chanceCond = null;
  264. if (attrs.getNamedItem("chanceType") != null)
  265. chanceCond = getValue(attrs.getNamedItem("chanceType").getNodeValue(), template);
  266. else if (isChanceSkillTrigger)
  267. throw new NoSuchElementException(name + " requires chanceType");
  268. int activationChance = 0;
  269. if (attrs.getNamedItem("activationChance") != null)
  270. activationChance = Integer.parseInt(getValue(attrs.getNamedItem("activationChance").getNodeValue(), template));
  271. else if (isChanceSkillTrigger)
  272. throw new NoSuchElementException(name + " requires activationChance");
  273. String activationElements = null;
  274. if (attrs.getNamedItem("activationElements") != null)
  275. activationElements = getValue(attrs.getNamedItem("activationElements").getNodeValue(), template);
  276. boolean pvpOnly = false;
  277. if (attrs.getNamedItem("pvpChanceOnly") != null)
  278. pvpOnly = Boolean.parseBoolean(getValue(attrs.getNamedItem("pvpChanceOnly").getNodeValue(), template));
  279. ChanceCondition chance = ChanceCondition.parse(chanceCond, activationChance, activationElements, pvpOnly);
  280. if (chance == null && isChanceSkillTrigger)
  281. throw new NoSuchElementException("Invalid chance condition: " + chanceCond + " "
  282. + activationChance);
  283. lt = new EffectTemplate(attachCond, applayCond, name, lambda, count, time, abnormal, special, stackType, stackOrder, icon, effectPower, type, trigId, trigLvl, chance);
  284. parseTemplate(n, lt);
  285. if (template instanceof L2Item)
  286. ((L2Item) template).attach(lt);
  287. else if (template instanceof L2Skill)
  288. {
  289. if (self)
  290. ((L2Skill) template).attachSelf(lt);
  291. else
  292. ((L2Skill) template).attach(lt);
  293. }
  294. }
  295. protected void attachSkill(Node n, Object template, Condition attachCond)
  296. {
  297. NamedNodeMap attrs = n.getAttributes();
  298. int id = 0, lvl = 1;
  299. if (attrs.getNamedItem("id") != null)
  300. {
  301. id = Integer.decode(getValue(attrs.getNamedItem("id").getNodeValue(), template));
  302. }
  303. if (attrs.getNamedItem("lvl") != null)
  304. {
  305. lvl = Integer.decode(getValue(attrs.getNamedItem("lvl").getNodeValue(), template));
  306. }
  307. L2Skill skill = SkillTable.getInstance().getInfo(id, lvl);
  308. if (attrs.getNamedItem("chance") != null)
  309. {
  310. if (template instanceof L2Weapon || template instanceof L2Item)
  311. {
  312. skill.attach(new ConditionGameChance(Integer.decode(getValue(attrs.getNamedItem("chance").getNodeValue(), template))), true);
  313. }
  314. else
  315. {
  316. skill.attach(new ConditionGameChance(Integer.decode(getValue(attrs.getNamedItem("chance").getNodeValue(), template))), false);
  317. }
  318. }
  319. if (template instanceof L2Weapon)
  320. {
  321. if (attrs.getNamedItem("onUse") != null
  322. || (attrs.getNamedItem("onCrit") == null && attrs.getNamedItem("onCast") == null))
  323. ((L2Weapon) template).attach(skill); // Attach as skill triggered on use
  324. //if (attrs.getNamedItem("onCrit") != null) ((L2Weapon) template).attachOnCrit(skill); // Attach as skill triggered on critical hit
  325. //if (attrs.getNamedItem("onCast") != null) ((L2Weapon) template).attachOnCast(skill); // Attach as skill triggered on cast
  326. }
  327. else if (template instanceof L2Item)
  328. {
  329. ((L2Item) template).attach(skill); // Attach as skill triggered on use
  330. }
  331. }
  332. protected Condition parseCondition(Node n, Object template)
  333. {
  334. while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
  335. n = n.getNextSibling();
  336. if (n == null) return null;
  337. if ("and".equalsIgnoreCase(n.getNodeName())) return parseLogicAnd(n, template);
  338. if ("or".equalsIgnoreCase(n.getNodeName())) return parseLogicOr(n, template);
  339. if ("not".equalsIgnoreCase(n.getNodeName())) return parseLogicNot(n, template);
  340. if ("player".equalsIgnoreCase(n.getNodeName())) return parsePlayerCondition(n, template);
  341. if ("target".equalsIgnoreCase(n.getNodeName())) return parseTargetCondition(n, template);
  342. if ("skill".equalsIgnoreCase(n.getNodeName())) return parseSkillCondition(n);
  343. if ("using".equalsIgnoreCase(n.getNodeName())) return parseUsingCondition(n);
  344. if ("game".equalsIgnoreCase(n.getNodeName())) return parseGameCondition(n);
  345. return null;
  346. }
  347. protected Condition parseLogicAnd(Node n, Object template)
  348. {
  349. ConditionLogicAnd cond = new ConditionLogicAnd();
  350. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  351. {
  352. if (n.getNodeType() == Node.ELEMENT_NODE) cond.add(parseCondition(n, template));
  353. }
  354. if (cond.conditions == null || cond.conditions.length == 0)
  355. _log.severe("Empty <and> condition in " + _file);
  356. return cond;
  357. }
  358. protected Condition parseLogicOr(Node n, Object template)
  359. {
  360. ConditionLogicOr cond = new ConditionLogicOr();
  361. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  362. {
  363. if (n.getNodeType() == Node.ELEMENT_NODE) cond.add(parseCondition(n, template));
  364. }
  365. if (cond.conditions == null || cond.conditions.length == 0)
  366. _log.severe("Empty <or> condition in " + _file);
  367. return cond;
  368. }
  369. protected Condition parseLogicNot(Node n, Object template)
  370. {
  371. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  372. {
  373. if (n.getNodeType() == Node.ELEMENT_NODE)
  374. {
  375. return new ConditionLogicNot(parseCondition(n, template));
  376. }
  377. }
  378. _log.severe("Empty <not> condition in " + _file);
  379. return null;
  380. }
  381. protected Condition parsePlayerCondition(Node n, Object template)
  382. {
  383. Condition cond = null;
  384. byte[] forces = new byte[2];
  385. NamedNodeMap attrs = n.getAttributes();
  386. for (int i = 0; i < attrs.getLength(); i++)
  387. {
  388. Node a = attrs.item(i);
  389. if ("race".equalsIgnoreCase(a.getNodeName()))
  390. {
  391. Race race = Race.valueOf(a.getNodeValue());
  392. cond = joinAnd(cond, new ConditionPlayerRace(race));
  393. }
  394. else if ("level".equalsIgnoreCase(a.getNodeName()))
  395. {
  396. int lvl = Integer.decode(getValue(a.getNodeValue(), template));
  397. cond = joinAnd(cond, new ConditionPlayerLevel(lvl));
  398. }
  399. else if ("resting".equalsIgnoreCase(a.getNodeName()))
  400. {
  401. boolean val = Boolean.valueOf(a.getNodeValue());
  402. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.RESTING, val));
  403. }
  404. else if ("flying".equalsIgnoreCase(a.getNodeName()))
  405. {
  406. boolean val = Boolean.valueOf(a.getNodeValue());
  407. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.FLYING, val));
  408. }
  409. else if ("moving".equalsIgnoreCase(a.getNodeName()))
  410. {
  411. boolean val = Boolean.valueOf(a.getNodeValue());
  412. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.MOVING, val));
  413. }
  414. else if ("running".equalsIgnoreCase(a.getNodeName()))
  415. {
  416. boolean val = Boolean.valueOf(a.getNodeValue());
  417. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.RUNNING, val));
  418. }
  419. else if ("behind".equalsIgnoreCase(a.getNodeName()))
  420. {
  421. boolean val = Boolean.valueOf(a.getNodeValue());
  422. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.BEHIND, val));
  423. }
  424. else if ("front".equalsIgnoreCase(a.getNodeName()))
  425. {
  426. boolean val = Boolean.valueOf(a.getNodeValue());
  427. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.FRONT, val));
  428. }
  429. else if ("chaotic".equalsIgnoreCase(a.getNodeName()))
  430. {
  431. boolean val = Boolean.valueOf(a.getNodeValue());
  432. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.CHAOTIC, val));
  433. }
  434. else if ("olympiad".equalsIgnoreCase(a.getNodeName()))
  435. {
  436. boolean val = Boolean.valueOf(a.getNodeValue());
  437. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.OLYMPIAD, val));
  438. }
  439. else if ("hp".equalsIgnoreCase(a.getNodeName()))
  440. {
  441. int hp = Integer.decode(getValue(a.getNodeValue(), null));
  442. cond = joinAnd(cond, new ConditionPlayerHp(hp));
  443. }
  444. else if ("hprate".equalsIgnoreCase(a.getNodeName()))
  445. {
  446. double rate = Double.parseDouble(getValue(a.getNodeValue(), null));
  447. cond = joinAnd(cond, new ConditionPlayerHpPercentage(rate));
  448. }
  449. else if ("mp".equalsIgnoreCase(a.getNodeName()))
  450. {
  451. int hp = Integer.decode(getValue(a.getNodeValue(), null));
  452. cond = joinAnd(cond, new ConditionPlayerMp(hp));
  453. }
  454. else if ("cp".equalsIgnoreCase(a.getNodeName()))
  455. {
  456. int cp = Integer.decode(getValue(a.getNodeValue(), null));
  457. cond = joinAnd(cond, new ConditionPlayerCp(cp));
  458. }
  459. else if ("grade".equalsIgnoreCase(a.getNodeName()))
  460. {
  461. int expIndex = Integer.decode(getValue(a.getNodeValue(), template));
  462. cond = joinAnd(cond, new ConditionPlayerGrade(expIndex));
  463. }
  464. else if ("siegezone".equalsIgnoreCase(a.getNodeName()))
  465. {
  466. int value = Integer.decode(getValue(a.getNodeValue(), null));
  467. cond = joinAnd(cond, new ConditionSiegeZone(value, true));
  468. }
  469. else if ("battle_force".equalsIgnoreCase(a.getNodeName()))
  470. {
  471. forces[0] = Byte.decode(getValue(a.getNodeValue(), null));
  472. }
  473. else if ("spell_force".equalsIgnoreCase(a.getNodeName()))
  474. {
  475. forces[1] = Byte.decode(getValue(a.getNodeValue(), null));
  476. }
  477. else if ("weight".equalsIgnoreCase(a.getNodeName()))
  478. {
  479. int weight = Integer.decode(getValue(a.getNodeValue(), null));
  480. cond = joinAnd(cond, new ConditionPlayerWeight(weight));
  481. }
  482. else if ("invSize".equalsIgnoreCase(a.getNodeName()))
  483. {
  484. int size = Integer.decode(getValue(a.getNodeValue(), null));
  485. cond = joinAnd(cond, new ConditionPlayerInvSize(size));
  486. }
  487. else if ("isClanLeader".equalsIgnoreCase(a.getNodeName()))
  488. {
  489. boolean val = Boolean.valueOf(a.getNodeValue());
  490. cond = joinAnd(cond, new ConditionPlayerIsClanLeader(val));
  491. }
  492. else if ("onTvTEvent".equalsIgnoreCase(a.getNodeName()))
  493. {
  494. boolean val = Boolean.valueOf(a.getNodeValue());
  495. cond = joinAnd(cond, new ConditionPlayerTvTEvent(val));
  496. }
  497. else if ("pledgeClass".equalsIgnoreCase(a.getNodeName()))
  498. {
  499. int pledgeClass = Integer.decode(getValue(a.getNodeValue(), null));
  500. cond = joinAnd(cond, new ConditionPlayerPledgeClass(pledgeClass));
  501. }
  502. else if ("clanHall".equalsIgnoreCase(a.getNodeName()))
  503. {
  504. FastList<Integer> array = new FastList<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 ConditionPlayerHasClanHall(array));
  512. }
  513. else if ("fort".equalsIgnoreCase(a.getNodeName()))
  514. {
  515. int fort = Integer.decode(getValue(a.getNodeValue(), null));
  516. cond = joinAnd(cond, new ConditionPlayerHasFort(fort));
  517. }
  518. else if ("castle".equalsIgnoreCase(a.getNodeName()))
  519. {
  520. int castle = Integer.decode(getValue(a.getNodeValue(), null));
  521. cond = joinAnd(cond, new ConditionPlayerHasCastle(castle));
  522. }
  523. else if ("sex".equalsIgnoreCase(a.getNodeName()))
  524. {
  525. int sex = Integer.decode(getValue(a.getNodeValue(), null));
  526. cond = joinAnd(cond, new ConditionPlayerSex(sex));
  527. }
  528. else if ("flyMounted".equalsIgnoreCase(a.getNodeName()))
  529. {
  530. boolean val = Boolean.valueOf(a.getNodeValue());
  531. cond = joinAnd(cond, new ConditionPlayerFlyMounted(val));
  532. }
  533. else if ("landingZone".equalsIgnoreCase(a.getNodeName()))
  534. {
  535. boolean val = Boolean.valueOf(a.getNodeValue());
  536. cond = joinAnd(cond, new ConditionPlayerLandingZone(val));
  537. }
  538. else if ("active_effect_id".equalsIgnoreCase(a.getNodeName()))
  539. {
  540. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  541. cond = joinAnd(cond, new ConditionPlayerActiveEffectId(effect_id));
  542. }
  543. else if ("active_effect_id_lvl".equalsIgnoreCase(a.getNodeName()))
  544. {
  545. String val = getValue(a.getNodeValue(), template);
  546. int effect_id = Integer.decode(getValue(val.split(",")[0], template));
  547. int effect_lvl = Integer.decode(getValue(val.split(",")[1], template));
  548. cond = joinAnd(cond, new ConditionPlayerActiveEffectId(effect_id, effect_lvl));
  549. }
  550. else if ("active_skill_id".equalsIgnoreCase(a.getNodeName()))
  551. {
  552. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  553. cond = joinAnd(cond, new ConditionPlayerActiveSkillId(skill_id));
  554. }
  555. else if ("active_skill_id_lvl".equalsIgnoreCase(a.getNodeName()))
  556. {
  557. String val = getValue(a.getNodeValue(), template);
  558. int skill_id = Integer.decode(getValue(val.split(",")[0], template));
  559. int skill_lvl = Integer.decode(getValue(val.split(",")[1], template));
  560. cond = joinAnd(cond, new ConditionPlayerActiveSkillId(skill_id, skill_lvl));
  561. }
  562. else if ("class_id_restriction".equalsIgnoreCase(a.getNodeName()))
  563. {
  564. FastList<Integer> array = new FastList<Integer>();
  565. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  566. while (st.hasMoreTokens())
  567. {
  568. String item = st.nextToken().trim();
  569. array.add(Integer.decode(getValue(item, null)));
  570. }
  571. cond = joinAnd(cond, new ConditionPlayerClassIdRestriction(array));
  572. }
  573. else if ("subclass".equalsIgnoreCase(a.getNodeName()))
  574. {
  575. boolean val = Boolean.valueOf(a.getNodeValue());
  576. cond = joinAnd(cond, new ConditionPlayerSubclass(val));
  577. }
  578. else if ("instanceid".equalsIgnoreCase(a.getNodeName()))
  579. {
  580. FastList<Integer> array = new FastList<Integer>();
  581. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  582. while (st.hasMoreTokens())
  583. {
  584. String item = st.nextToken().trim();
  585. array.add(Integer.decode(getValue(item, null)));
  586. }
  587. cond = joinAnd(cond, new ConditionPlayerInstanceId(array));
  588. }
  589. else if ("cloakStatus".equalsIgnoreCase(a.getNodeName()))
  590. {
  591. int val = Integer.valueOf(a.getNodeValue());
  592. cond = joinAnd(cond, new ConditionPlayerCloakStatus(val));
  593. }
  594. }
  595. if (forces[0] + forces[1] > 0)
  596. cond = joinAnd(cond, new ConditionForceBuff(forces));
  597. if (cond == null) _log.severe("Unrecognized <player> condition in " + _file);
  598. return cond;
  599. }
  600. protected Condition parseTargetCondition(Node n, Object template)
  601. {
  602. Condition cond = null;
  603. NamedNodeMap attrs = n.getAttributes();
  604. for (int i = 0; i < attrs.getLength(); i++)
  605. {
  606. Node a = attrs.item(i);
  607. if ("aggro".equalsIgnoreCase(a.getNodeName()))
  608. {
  609. boolean val = Boolean.valueOf(a.getNodeValue());
  610. cond = joinAnd(cond, new ConditionTargetAggro(val));
  611. }
  612. else if ("siegezone".equalsIgnoreCase(a.getNodeName()))
  613. {
  614. int value = Integer.decode(getValue(a.getNodeValue(), null));
  615. cond = joinAnd(cond, new ConditionSiegeZone(value, false));
  616. }
  617. else if ("level".equalsIgnoreCase(a.getNodeName()))
  618. {
  619. int lvl = Integer.decode(getValue(a.getNodeValue(), template));
  620. cond = joinAnd(cond, new ConditionTargetLevel(lvl));
  621. }
  622. else if ("class_id_restriction".equalsIgnoreCase(a.getNodeName()))
  623. {
  624. FastList<Integer> array = new FastList<Integer>();
  625. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  626. while (st.hasMoreTokens())
  627. {
  628. String item = st.nextToken().trim();
  629. array.add(Integer.decode(getValue(item, null)));
  630. }
  631. cond = joinAnd(cond, new ConditionTargetClassIdRestriction(array));
  632. }
  633. else if ("active_effect_id".equalsIgnoreCase(a.getNodeName()))
  634. {
  635. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  636. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id));
  637. }
  638. else if ("active_effect_id_lvl".equalsIgnoreCase(a.getNodeName()))
  639. {
  640. String val = getValue(a.getNodeValue(), template);
  641. int effect_id = Integer.decode(getValue(val.split(",")[0], template));
  642. int effect_lvl = Integer.decode(getValue(val.split(",")[1], template));
  643. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id, effect_lvl));
  644. }
  645. else if ("active_skill_id".equalsIgnoreCase(a.getNodeName()))
  646. {
  647. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  648. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id));
  649. }
  650. else if ("active_skill_id_lvl".equalsIgnoreCase(a.getNodeName()))
  651. {
  652. String val = getValue(a.getNodeValue(), template);
  653. int skill_id = Integer.decode(getValue(val.split(",")[0], template));
  654. int skill_lvl = Integer.decode(getValue(val.split(",")[1], template));
  655. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id, skill_lvl));
  656. }
  657. else if ("abnormal".equalsIgnoreCase(a.getNodeName()))
  658. {
  659. int abnormalId = Integer.decode(getValue(a.getNodeValue(), template));
  660. cond = joinAnd(cond, new ConditionTargetAbnormal(abnormalId));
  661. }
  662. else if("mindistance".equalsIgnoreCase(a.getNodeName()))
  663. {
  664. int distance = Integer.decode(getValue(a.getNodeValue(),null));
  665. cond = joinAnd(cond, new ConditionMinDistance(distance*distance));
  666. }
  667. // used for npc race
  668. else if ("race_id".equalsIgnoreCase(a.getNodeName()))
  669. {
  670. ArrayList<Integer> array = new ArrayList<Integer>();
  671. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  672. while (st.hasMoreTokens())
  673. {
  674. String item = st.nextToken().trim();
  675. array.add(Integer.decode(getValue(item, null)));
  676. }
  677. cond = joinAnd(cond, new ConditionTargetRaceId(array));
  678. }
  679. // used for pc race
  680. else if ("race".equalsIgnoreCase(a.getNodeName()))
  681. {
  682. Race race = Race.valueOf(a.getNodeValue());
  683. cond = joinAnd(cond, new ConditionTargetRace(race));
  684. }
  685. else if ("using".equalsIgnoreCase(a.getNodeName()))
  686. {
  687. int mask = 0;
  688. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  689. while (st.hasMoreTokens())
  690. {
  691. String item = st.nextToken().trim();
  692. for (L2WeaponType wt : L2WeaponType.values())
  693. {
  694. if (wt.toString().equals(item))
  695. {
  696. mask |= wt.mask();
  697. break;
  698. }
  699. }
  700. for (L2ArmorType at : L2ArmorType.values())
  701. {
  702. if (at.toString().equals(item))
  703. {
  704. mask |= at.mask();
  705. break;
  706. }
  707. }
  708. }
  709. cond = joinAnd(cond, new ConditionTargetUsesWeaponKind(mask));
  710. }
  711. else if ("npcId".equalsIgnoreCase(a.getNodeName()))
  712. {
  713. String values = getValue(a.getNodeValue(), template).trim();
  714. String[] valuesSplit = values.split(" ");
  715. cond = joinAnd(cond, new ConditionTargetNpcId(valuesSplit));
  716. }
  717. else if ("npcType".equalsIgnoreCase(a.getNodeName()))
  718. {
  719. String values = getValue(a.getNodeValue(), template).trim();
  720. String[] valuesSplit = values.split(" ");
  721. cond = joinAnd(cond, new ConditionTargetNpcType(valuesSplit));
  722. }
  723. }
  724. if (cond == null) _log.severe("Unrecognized <target> condition in " + _file);
  725. return cond;
  726. }
  727. protected Condition parseSkillCondition(Node n)
  728. {
  729. NamedNodeMap attrs = n.getAttributes();
  730. Stats stat = Stats.valueOfXml(attrs.getNamedItem("stat").getNodeValue());
  731. return new ConditionSkillStats(stat);
  732. }
  733. protected Condition parseUsingCondition(Node n)
  734. {
  735. Condition cond = null;
  736. NamedNodeMap attrs = n.getAttributes();
  737. for (int i = 0; i < attrs.getLength(); i++)
  738. {
  739. Node a = attrs.item(i);
  740. if ("kind".equalsIgnoreCase(a.getNodeName()))
  741. {
  742. int mask = 0;
  743. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  744. while (st.hasMoreTokens())
  745. {
  746. String item = st.nextToken().trim();
  747. for (L2WeaponType wt : L2WeaponType.values())
  748. {
  749. if (wt.toString().equals(item))
  750. {
  751. mask |= wt.mask();
  752. break;
  753. }
  754. }
  755. for (L2ArmorType at : L2ArmorType.values())
  756. {
  757. if (at.toString().equals(item))
  758. {
  759. mask |= at.mask();
  760. break;
  761. }
  762. }
  763. }
  764. cond = joinAnd(cond, new ConditionUsingItemType(mask));
  765. }
  766. else if ("skill".equalsIgnoreCase(a.getNodeName()))
  767. {
  768. int id = Integer.parseInt(a.getNodeValue());
  769. cond = joinAnd(cond, new ConditionUsingSkill(id));
  770. }
  771. else if ("slotitem".equalsIgnoreCase(a.getNodeName()))
  772. {
  773. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ";");
  774. int id = Integer.parseInt(st.nextToken().trim());
  775. int slot = Integer.parseInt(st.nextToken().trim());
  776. int enchant = 0;
  777. if (st.hasMoreTokens()) enchant = Integer.parseInt(st.nextToken().trim());
  778. cond = joinAnd(cond, new ConditionSlotItemId(slot, id, enchant));
  779. }
  780. else if ("weaponChange".equalsIgnoreCase(a.getNodeName()))
  781. {
  782. boolean val = Boolean.valueOf(a.getNodeValue());
  783. cond = joinAnd(cond, new ConditionChangeWeapon(val));
  784. }
  785. }
  786. if (cond == null) _log.severe("Unrecognized <using> condition in " + _file);
  787. return cond;
  788. }
  789. protected Condition parseGameCondition(Node n)
  790. {
  791. Condition cond = null;
  792. NamedNodeMap attrs = n.getAttributes();
  793. for (int i = 0; i < attrs.getLength(); i++)
  794. {
  795. Node a = attrs.item(i);
  796. if ("skill".equalsIgnoreCase(a.getNodeName()))
  797. {
  798. boolean val = Boolean.valueOf(a.getNodeValue());
  799. cond = joinAnd(cond, new ConditionWithSkill(val));
  800. }
  801. if ("night".equalsIgnoreCase(a.getNodeName()))
  802. {
  803. boolean val = Boolean.valueOf(a.getNodeValue());
  804. cond = joinAnd(cond, new ConditionGameTime(CheckGameTime.NIGHT, val));
  805. }
  806. if ("chance".equalsIgnoreCase(a.getNodeName()))
  807. {
  808. int val = Integer.decode(getValue(a.getNodeValue(), null));
  809. cond = joinAnd(cond, new ConditionGameChance(val));
  810. }
  811. }
  812. if (cond == null) _log.severe("Unrecognized <game> condition in " + _file);
  813. return cond;
  814. }
  815. protected void parseTable(Node n)
  816. {
  817. NamedNodeMap attrs = n.getAttributes();
  818. String name = attrs.getNamedItem("name").getNodeValue();
  819. if (name.charAt(0) != '#') throw new IllegalArgumentException("Table name must start with #");
  820. StringTokenizer data = new StringTokenizer(n.getFirstChild().getNodeValue());
  821. List<String> array = new FastList<String>();
  822. while (data.hasMoreTokens())
  823. array.add(data.nextToken());
  824. String[] res = new String[array.size()];
  825. int i = 0;
  826. for (String str : array)
  827. {
  828. res[i++] = str;
  829. }
  830. setTable(name, res);
  831. }
  832. protected void parseBeanSet(Node n, StatsSet set, Integer level)
  833. {
  834. String name = n.getAttributes().getNamedItem("name").getNodeValue().trim();
  835. String value = n.getAttributes().getNamedItem("val").getNodeValue().trim();
  836. char ch = value.length() == 0 ? ' ' : value.charAt(0);
  837. if (ch == '#' || ch == '-' || Character.isDigit(ch)) set.set(name,
  838. String.valueOf(getValue(value,
  839. level)));
  840. else set.set(name, value);
  841. }
  842. protected Lambda getLambda(Node n, Object template)
  843. {
  844. Node nval = n.getAttributes().getNamedItem("val");
  845. if (nval != null)
  846. {
  847. String val = nval.getNodeValue();
  848. if (val.charAt(0) == '#')
  849. { // table by level
  850. return new LambdaConst(Double.parseDouble(getTableValue(val)));
  851. }
  852. else if (val.charAt(0) == '$')
  853. {
  854. if (val.equalsIgnoreCase("$player_level"))
  855. return new LambdaStats(LambdaStats.StatsType.PLAYER_LEVEL);
  856. if (val.equalsIgnoreCase("$target_level"))
  857. return new LambdaStats(LambdaStats.StatsType.TARGET_LEVEL);
  858. if (val.equalsIgnoreCase("$player_max_hp"))
  859. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_HP);
  860. if (val.equalsIgnoreCase("$player_max_mp"))
  861. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_MP);
  862. // try to find value out of item fields
  863. StatsSet set = getStatsSet();
  864. String field = set.getString(val.substring(1));
  865. if (field != null)
  866. {
  867. return new LambdaConst(Double.parseDouble(getValue(field, template)));
  868. }
  869. // failed
  870. throw new IllegalArgumentException("Unknown value " + val);
  871. }
  872. else
  873. {
  874. return new LambdaConst(Double.parseDouble(val));
  875. }
  876. }
  877. LambdaCalc calc = new LambdaCalc();
  878. n = n.getFirstChild();
  879. while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
  880. n = n.getNextSibling();
  881. if (n == null || !"val".equals(n.getNodeName()))
  882. throw new IllegalArgumentException("Value not specified");
  883. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  884. {
  885. if (n.getNodeType() != Node.ELEMENT_NODE) continue;
  886. attachLambdaFunc(n, template, calc);
  887. }
  888. return calc;
  889. }
  890. protected String getValue(String value, Object template)
  891. {
  892. // is it a table?
  893. if (value.charAt(0) == '#')
  894. {
  895. if (template instanceof L2Skill) return getTableValue(value);
  896. else if (template instanceof Integer) return getTableValue(value, ((Integer) template).intValue());
  897. else throw new IllegalStateException();
  898. }
  899. return value;
  900. }
  901. protected Condition joinAnd(Condition cond, Condition c)
  902. {
  903. if (cond == null) return c;
  904. if (cond instanceof ConditionLogicAnd)
  905. {
  906. ((ConditionLogicAnd) cond).add(c);
  907. return cond;
  908. }
  909. ConditionLogicAnd and = new ConditionLogicAnd();
  910. and.add(cond);
  911. and.add(c);
  912. return and;
  913. }
  914. }