DocumentBase.java 44 KB

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