2
0

DocumentBase.java 44 KB

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