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. * 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. float stackOrder = 0;
  225. String stackType = "none";
  226. if (attrs.getNamedItem("stackType") != null)
  227. {
  228. stackType = attrs.getNamedItem("stackType").getNodeValue();
  229. }
  230. if (attrs.getNamedItem("stackOrder") != null)
  231. {
  232. stackOrder = Float.parseFloat(getValue(attrs.getNamedItem("stackOrder").getNodeValue(), template));
  233. }
  234. double effectPower = -1;
  235. if (attrs.getNamedItem("effectPower") != null)
  236. effectPower = Double.parseDouble( getValue(attrs.getNamedItem("effectPower").getNodeValue(), template));
  237. L2SkillType type = null;
  238. if (attrs.getNamedItem("effectType") != null)
  239. {
  240. String typeName = getValue(attrs.getNamedItem("effectType").getNodeValue(), template);
  241. try
  242. {
  243. type = Enum.valueOf(L2SkillType.class, typeName);
  244. }
  245. catch (Exception e)
  246. {
  247. throw new IllegalArgumentException("Not skilltype found for: "+typeName);
  248. }
  249. }
  250. if (effectPower > -1 && type == null && _log.isLoggable(Level.WARNING))
  251. _log.log(Level.WARNING, "Missing effectType for effect: "+name);
  252. EffectTemplate lt;
  253. final boolean isChanceSkillTrigger = (name == EffectChanceSkillTrigger.class.getName());
  254. int trigId = 0;
  255. if (attrs.getNamedItem("triggeredId") != null)
  256. trigId = Integer.parseInt(getValue(attrs.getNamedItem("triggeredId").getNodeValue(), template));
  257. else if (isChanceSkillTrigger)
  258. throw new NoSuchElementException(name + " requires triggerId");
  259. int trigLvl = 1;
  260. if (attrs.getNamedItem("triggeredLevel") != null)
  261. trigLvl = Integer.parseInt(getValue(attrs.getNamedItem("triggeredLevel").getNodeValue(), template));
  262. String chanceCond = null;
  263. if (attrs.getNamedItem("chanceType") != null)
  264. chanceCond = getValue(attrs.getNamedItem("chanceType").getNodeValue(), template);
  265. else if (isChanceSkillTrigger)
  266. throw new NoSuchElementException(name + " requires chanceType");
  267. int activationChance = -1;
  268. if (attrs.getNamedItem("activationChance") != null)
  269. activationChance = Integer.parseInt(getValue(attrs.getNamedItem("activationChance").getNodeValue(), template));
  270. String activationElements = null;
  271. if (attrs.getNamedItem("activationElements") != null)
  272. activationElements = getValue(attrs.getNamedItem("activationElements").getNodeValue(), template);
  273. boolean pvpOnly = false;
  274. if (attrs.getNamedItem("pvpChanceOnly") != null)
  275. pvpOnly = Boolean.parseBoolean(getValue(attrs.getNamedItem("pvpChanceOnly").getNodeValue(), template));
  276. ChanceCondition chance = ChanceCondition.parse(chanceCond, activationChance, activationElements, pvpOnly);
  277. if (chance == null && isChanceSkillTrigger)
  278. throw new NoSuchElementException("Invalid chance condition: " + chanceCond + " "
  279. + activationChance);
  280. lt = new EffectTemplate(attachCond, applayCond, name, lambda, count, time, abnormal, special, stackType, stackOrder, icon, effectPower, type, trigId, trigLvl, chance);
  281. parseTemplate(n, lt);
  282. if (template instanceof L2Item)
  283. ((L2Item) template).attach(lt);
  284. else if (template instanceof L2Skill)
  285. {
  286. if (self)
  287. ((L2Skill) template).attachSelf(lt);
  288. else
  289. ((L2Skill) template).attach(lt);
  290. }
  291. }
  292. protected void attachSkill(Node n, Object template, Condition attachCond)
  293. {
  294. NamedNodeMap attrs = n.getAttributes();
  295. int id = 0, lvl = 1;
  296. if (attrs.getNamedItem("id") != null)
  297. {
  298. id = Integer.decode(getValue(attrs.getNamedItem("id").getNodeValue(), template));
  299. }
  300. if (attrs.getNamedItem("lvl") != null)
  301. {
  302. lvl = Integer.decode(getValue(attrs.getNamedItem("lvl").getNodeValue(), template));
  303. }
  304. L2Skill skill = SkillTable.getInstance().getInfo(id, lvl);
  305. if (attrs.getNamedItem("chance") != null)
  306. {
  307. if (template instanceof L2Weapon || template instanceof L2Item)
  308. {
  309. skill.attach(new ConditionGameChance(Integer.decode(getValue(attrs.getNamedItem("chance").getNodeValue(), template))), true);
  310. }
  311. else
  312. {
  313. skill.attach(new ConditionGameChance(Integer.decode(getValue(attrs.getNamedItem("chance").getNodeValue(), template))), false);
  314. }
  315. }
  316. if (template instanceof L2Weapon)
  317. {
  318. if (attrs.getNamedItem("onUse") != null
  319. || (attrs.getNamedItem("onCrit") == null && attrs.getNamedItem("onCast") == null))
  320. ((L2Weapon) template).attach(skill); // Attach as skill triggered on use
  321. //if (attrs.getNamedItem("onCrit") != null) ((L2Weapon) template).attachOnCrit(skill); // Attach as skill triggered on critical hit
  322. //if (attrs.getNamedItem("onCast") != null) ((L2Weapon) template).attachOnCast(skill); // Attach as skill triggered on cast
  323. }
  324. else if (template instanceof L2Item)
  325. {
  326. ((L2Item) template).attach(skill); // Attach as skill triggered on use
  327. }
  328. }
  329. protected Condition parseCondition(Node n, Object template)
  330. {
  331. while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
  332. n = n.getNextSibling();
  333. if (n == null) return null;
  334. if ("and".equalsIgnoreCase(n.getNodeName())) return parseLogicAnd(n, template);
  335. if ("or".equalsIgnoreCase(n.getNodeName())) return parseLogicOr(n, template);
  336. if ("not".equalsIgnoreCase(n.getNodeName())) return parseLogicNot(n, template);
  337. if ("player".equalsIgnoreCase(n.getNodeName())) return parsePlayerCondition(n, template);
  338. if ("target".equalsIgnoreCase(n.getNodeName())) return parseTargetCondition(n, template);
  339. if ("skill".equalsIgnoreCase(n.getNodeName())) return parseSkillCondition(n);
  340. if ("using".equalsIgnoreCase(n.getNodeName())) return parseUsingCondition(n);
  341. if ("game".equalsIgnoreCase(n.getNodeName())) return parseGameCondition(n);
  342. return null;
  343. }
  344. protected Condition parseLogicAnd(Node n, Object template)
  345. {
  346. ConditionLogicAnd cond = new ConditionLogicAnd();
  347. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  348. {
  349. if (n.getNodeType() == Node.ELEMENT_NODE) cond.add(parseCondition(n, template));
  350. }
  351. if (cond.conditions == null || cond.conditions.length == 0)
  352. _log.severe("Empty <and> condition in " + _file);
  353. return cond;
  354. }
  355. protected Condition parseLogicOr(Node n, Object template)
  356. {
  357. ConditionLogicOr cond = new ConditionLogicOr();
  358. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  359. {
  360. if (n.getNodeType() == Node.ELEMENT_NODE) cond.add(parseCondition(n, template));
  361. }
  362. if (cond.conditions == null || cond.conditions.length == 0)
  363. _log.severe("Empty <or> condition in " + _file);
  364. return cond;
  365. }
  366. protected Condition parseLogicNot(Node n, Object template)
  367. {
  368. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  369. {
  370. if (n.getNodeType() == Node.ELEMENT_NODE)
  371. {
  372. return new ConditionLogicNot(parseCondition(n, template));
  373. }
  374. }
  375. _log.severe("Empty <not> condition in " + _file);
  376. return null;
  377. }
  378. protected Condition parsePlayerCondition(Node n, Object template)
  379. {
  380. Condition cond = null;
  381. byte[] forces = new byte[2];
  382. NamedNodeMap attrs = n.getAttributes();
  383. for (int i = 0; i < attrs.getLength(); i++)
  384. {
  385. Node a = attrs.item(i);
  386. if ("race".equalsIgnoreCase(a.getNodeName()))
  387. {
  388. Race race = Race.valueOf(a.getNodeValue());
  389. cond = joinAnd(cond, new ConditionPlayerRace(race));
  390. }
  391. else if ("level".equalsIgnoreCase(a.getNodeName()))
  392. {
  393. int lvl = Integer.decode(getValue(a.getNodeValue(), template));
  394. cond = joinAnd(cond, new ConditionPlayerLevel(lvl));
  395. }
  396. else if ("resting".equalsIgnoreCase(a.getNodeName()))
  397. {
  398. boolean val = Boolean.valueOf(a.getNodeValue());
  399. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.RESTING, val));
  400. }
  401. else if ("flying".equalsIgnoreCase(a.getNodeName()))
  402. {
  403. boolean val = Boolean.valueOf(a.getNodeValue());
  404. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.FLYING, val));
  405. }
  406. else if ("moving".equalsIgnoreCase(a.getNodeName()))
  407. {
  408. boolean val = Boolean.valueOf(a.getNodeValue());
  409. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.MOVING, val));
  410. }
  411. else if ("running".equalsIgnoreCase(a.getNodeName()))
  412. {
  413. boolean val = Boolean.valueOf(a.getNodeValue());
  414. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.RUNNING, val));
  415. }
  416. else if ("behind".equalsIgnoreCase(a.getNodeName()))
  417. {
  418. boolean val = Boolean.valueOf(a.getNodeValue());
  419. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.BEHIND, val));
  420. }
  421. else if ("front".equalsIgnoreCase(a.getNodeName()))
  422. {
  423. boolean val = Boolean.valueOf(a.getNodeValue());
  424. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.FRONT, val));
  425. }
  426. else if ("chaotic".equalsIgnoreCase(a.getNodeName()))
  427. {
  428. boolean val = Boolean.valueOf(a.getNodeValue());
  429. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.CHAOTIC, val));
  430. }
  431. else if ("olympiad".equalsIgnoreCase(a.getNodeName()))
  432. {
  433. boolean val = Boolean.valueOf(a.getNodeValue());
  434. cond = joinAnd(cond, new ConditionPlayerState(PlayerState.OLYMPIAD, val));
  435. }
  436. else if ("ishero".equalsIgnoreCase(a.getNodeName()))
  437. {
  438. boolean val = Boolean.valueOf(a.getNodeValue());
  439. cond = joinAnd(cond, new ConditionPlayerIsHero(val));
  440. }
  441. else if ("hp".equalsIgnoreCase(a.getNodeName()))
  442. {
  443. int hp = Integer.decode(getValue(a.getNodeValue(), null));
  444. cond = joinAnd(cond, new ConditionPlayerHp(hp));
  445. }
  446. else if ("mp".equalsIgnoreCase(a.getNodeName()))
  447. {
  448. int hp = Integer.decode(getValue(a.getNodeValue(), null));
  449. cond = joinAnd(cond, new ConditionPlayerMp(hp));
  450. }
  451. else if ("cp".equalsIgnoreCase(a.getNodeName()))
  452. {
  453. int cp = Integer.decode(getValue(a.getNodeValue(), null));
  454. cond = joinAnd(cond, new ConditionPlayerCp(cp));
  455. }
  456. else if ("grade".equalsIgnoreCase(a.getNodeName()))
  457. {
  458. int expIndex = Integer.decode(getValue(a.getNodeValue(), template));
  459. cond = joinAnd(cond, new ConditionPlayerGrade(expIndex));
  460. }
  461. else if ("pkCount".equalsIgnoreCase(a.getNodeName()))
  462. {
  463. int expIndex = Integer.decode(getValue(a.getNodeValue(), template));
  464. cond = joinAnd(cond, new ConditionPlayerPkCount(expIndex));
  465. }
  466. else if ("siegezone".equalsIgnoreCase(a.getNodeName()))
  467. {
  468. int value = Integer.decode(getValue(a.getNodeValue(), null));
  469. cond = joinAnd(cond, new ConditionSiegeZone(value, true));
  470. }
  471. else if ("siegeside".equalsIgnoreCase(a.getNodeName()))
  472. {
  473. int value = Integer.decode(getValue(a.getNodeValue(), null));
  474. cond = joinAnd(cond, new ConditionPlayerSiegeSide(value));
  475. }
  476. else if ("battle_force".equalsIgnoreCase(a.getNodeName()))
  477. {
  478. forces[0] = Byte.decode(getValue(a.getNodeValue(), null));
  479. }
  480. else if ("spell_force".equalsIgnoreCase(a.getNodeName()))
  481. {
  482. forces[1] = Byte.decode(getValue(a.getNodeValue(), null));
  483. }
  484. else if ("charges".equalsIgnoreCase(a.getNodeName()))
  485. {
  486. int value = Integer.decode(getValue(a.getNodeValue(), template));
  487. cond = joinAnd(cond, new ConditionPlayerCharges(value));
  488. }
  489. else if ("souls".equalsIgnoreCase(a.getNodeName()))
  490. {
  491. int value = Integer.decode(getValue(a.getNodeValue(), template));
  492. cond = joinAnd(cond, new ConditionPlayerSouls(value));
  493. }
  494. else if ("weight".equalsIgnoreCase(a.getNodeName()))
  495. {
  496. int weight = Integer.decode(getValue(a.getNodeValue(), null));
  497. cond = joinAnd(cond, new ConditionPlayerWeight(weight));
  498. }
  499. else if ("invSize".equalsIgnoreCase(a.getNodeName()))
  500. {
  501. int size = Integer.decode(getValue(a.getNodeValue(), null));
  502. cond = joinAnd(cond, new ConditionPlayerInvSize(size));
  503. }
  504. else if ("isClanLeader".equalsIgnoreCase(a.getNodeName()))
  505. {
  506. boolean val = Boolean.valueOf(a.getNodeValue());
  507. cond = joinAnd(cond, new ConditionPlayerIsClanLeader(val));
  508. }
  509. else if ("onTvTEvent".equalsIgnoreCase(a.getNodeName()))
  510. {
  511. boolean val = Boolean.valueOf(a.getNodeValue());
  512. cond = joinAnd(cond, new ConditionPlayerTvTEvent(val));
  513. }
  514. else if ("pledgeClass".equalsIgnoreCase(a.getNodeName()))
  515. {
  516. int pledgeClass = Integer.decode(getValue(a.getNodeValue(), null));
  517. cond = joinAnd(cond, new ConditionPlayerPledgeClass(pledgeClass));
  518. }
  519. else if ("clanHall".equalsIgnoreCase(a.getNodeName()))
  520. {
  521. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  522. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  523. while (st.hasMoreTokens())
  524. {
  525. String item = st.nextToken().trim();
  526. array.add(Integer.decode(getValue(item, null)));
  527. }
  528. cond = joinAnd(cond, new ConditionPlayerHasClanHall(array));
  529. }
  530. else if ("fort".equalsIgnoreCase(a.getNodeName()))
  531. {
  532. int fort = Integer.decode(getValue(a.getNodeValue(), null));
  533. cond = joinAnd(cond, new ConditionPlayerHasFort(fort));
  534. }
  535. else if ("castle".equalsIgnoreCase(a.getNodeName()))
  536. {
  537. int castle = Integer.decode(getValue(a.getNodeValue(), null));
  538. cond = joinAnd(cond, new ConditionPlayerHasCastle(castle));
  539. }
  540. else if ("sex".equalsIgnoreCase(a.getNodeName()))
  541. {
  542. int sex = Integer.decode(getValue(a.getNodeValue(), null));
  543. cond = joinAnd(cond, new ConditionPlayerSex(sex));
  544. }
  545. else if ("flyMounted".equalsIgnoreCase(a.getNodeName()))
  546. {
  547. boolean val = Boolean.valueOf(a.getNodeValue());
  548. cond = joinAnd(cond, new ConditionPlayerFlyMounted(val));
  549. }
  550. else if ("landingZone".equalsIgnoreCase(a.getNodeName()))
  551. {
  552. boolean val = Boolean.valueOf(a.getNodeValue());
  553. cond = joinAnd(cond, new ConditionPlayerLandingZone(val));
  554. }
  555. else if ("active_effect_id".equalsIgnoreCase(a.getNodeName()))
  556. {
  557. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  558. cond = joinAnd(cond, new ConditionPlayerActiveEffectId(effect_id));
  559. }
  560. else if ("active_effect_id_lvl".equalsIgnoreCase(a.getNodeName()))
  561. {
  562. String val = getValue(a.getNodeValue(), template);
  563. int effect_id = Integer.decode(getValue(val.split(",")[0], template));
  564. int effect_lvl = Integer.decode(getValue(val.split(",")[1], template));
  565. cond = joinAnd(cond, new ConditionPlayerActiveEffectId(effect_id, effect_lvl));
  566. }
  567. else if ("active_skill_id".equalsIgnoreCase(a.getNodeName()))
  568. {
  569. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  570. cond = joinAnd(cond, new ConditionPlayerActiveSkillId(skill_id));
  571. }
  572. else if ("active_skill_id_lvl".equalsIgnoreCase(a.getNodeName()))
  573. {
  574. String val = getValue(a.getNodeValue(), template);
  575. int skill_id = Integer.decode(getValue(val.split(",")[0], template));
  576. int skill_lvl = Integer.decode(getValue(val.split(",")[1], template));
  577. cond = joinAnd(cond, new ConditionPlayerActiveSkillId(skill_id, skill_lvl));
  578. }
  579. else if ("class_id_restriction".equalsIgnoreCase(a.getNodeName()))
  580. {
  581. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  582. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  583. while (st.hasMoreTokens())
  584. {
  585. String item = st.nextToken().trim();
  586. array.add(Integer.decode(getValue(item, null)));
  587. }
  588. cond = joinAnd(cond, new ConditionPlayerClassIdRestriction(array));
  589. }
  590. else if ("subclass".equalsIgnoreCase(a.getNodeName()))
  591. {
  592. boolean val = Boolean.valueOf(a.getNodeValue());
  593. cond = joinAnd(cond, new ConditionPlayerSubclass(val));
  594. }
  595. else if ("instanceId".equalsIgnoreCase(a.getNodeName()))
  596. {
  597. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  598. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  599. while (st.hasMoreTokens())
  600. {
  601. String item = st.nextToken().trim();
  602. array.add(Integer.decode(getValue(item, null)));
  603. }
  604. cond = joinAnd(cond, new ConditionPlayerInstanceId(array));
  605. }
  606. else if ("agathionId".equalsIgnoreCase(a.getNodeName()))
  607. {
  608. int agathionId = Integer.decode(a.getNodeValue());
  609. cond = joinAnd(cond, new ConditionPlayerAgathionId(agathionId));
  610. }
  611. else if ("cloakStatus".equalsIgnoreCase(a.getNodeName()))
  612. {
  613. int val = Integer.valueOf(a.getNodeValue());
  614. cond = joinAnd(cond, new ConditionPlayerCloakStatus(val));
  615. }
  616. else if ("hasPet".equalsIgnoreCase(a.getNodeName()))
  617. {
  618. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  619. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  620. while (st.hasMoreTokens())
  621. {
  622. String item = st.nextToken().trim();
  623. array.add(Integer.decode(getValue(item, null)));
  624. }
  625. cond = joinAnd(cond, new ConditionPlayerHasPet(array));
  626. }
  627. else if ("servitorNpcId".equalsIgnoreCase(a.getNodeName()))
  628. {
  629. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  630. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  631. while (st.hasMoreTokens())
  632. {
  633. String item = st.nextToken().trim();
  634. array.add(Integer.decode(getValue(item, null)));
  635. }
  636. cond = joinAnd(cond, new ConditionPlayerServitorNpcId(array));
  637. }
  638. }
  639. if (forces[0] + forces[1] > 0)
  640. cond = joinAnd(cond, new ConditionForceBuff(forces));
  641. if (cond == null) _log.severe("Unrecognized <player> condition in " + _file);
  642. return cond;
  643. }
  644. protected Condition parseTargetCondition(Node n, Object template)
  645. {
  646. Condition cond = null;
  647. NamedNodeMap attrs = n.getAttributes();
  648. for (int i = 0; i < attrs.getLength(); i++)
  649. {
  650. Node a = attrs.item(i);
  651. if ("aggro".equalsIgnoreCase(a.getNodeName()))
  652. {
  653. boolean val = Boolean.valueOf(a.getNodeValue());
  654. cond = joinAnd(cond, new ConditionTargetAggro(val));
  655. }
  656. else if ("siegezone".equalsIgnoreCase(a.getNodeName()))
  657. {
  658. int value = Integer.decode(getValue(a.getNodeValue(), null));
  659. cond = joinAnd(cond, new ConditionSiegeZone(value, false));
  660. }
  661. else if ("level".equalsIgnoreCase(a.getNodeName()))
  662. {
  663. int lvl = Integer.decode(getValue(a.getNodeValue(), template));
  664. cond = joinAnd(cond, new ConditionTargetLevel(lvl));
  665. }
  666. else if ("class_id_restriction".equalsIgnoreCase(a.getNodeName()))
  667. {
  668. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  669. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  670. while (st.hasMoreTokens())
  671. {
  672. String item = st.nextToken().trim();
  673. array.add(Integer.decode(getValue(item, null)));
  674. }
  675. cond = joinAnd(cond, new ConditionTargetClassIdRestriction(array));
  676. }
  677. else if ("active_effect_id".equalsIgnoreCase(a.getNodeName()))
  678. {
  679. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  680. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id));
  681. }
  682. else if ("active_effect_id_lvl".equalsIgnoreCase(a.getNodeName()))
  683. {
  684. String val = getValue(a.getNodeValue(), template);
  685. int effect_id = Integer.decode(getValue(val.split(",")[0], template));
  686. int effect_lvl = Integer.decode(getValue(val.split(",")[1], template));
  687. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id, effect_lvl));
  688. }
  689. else if ("active_skill_id".equalsIgnoreCase(a.getNodeName()))
  690. {
  691. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  692. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id));
  693. }
  694. else if ("active_skill_id_lvl".equalsIgnoreCase(a.getNodeName()))
  695. {
  696. String val = getValue(a.getNodeValue(), template);
  697. int skill_id = Integer.decode(getValue(val.split(",")[0], template));
  698. int skill_lvl = Integer.decode(getValue(val.split(",")[1], template));
  699. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id, skill_lvl));
  700. }
  701. else if ("abnormal".equalsIgnoreCase(a.getNodeName()))
  702. {
  703. int abnormalId = Integer.decode(getValue(a.getNodeValue(), template));
  704. cond = joinAnd(cond, new ConditionTargetAbnormal(abnormalId));
  705. }
  706. else if("mindistance".equalsIgnoreCase(a.getNodeName()))
  707. {
  708. int distance = Integer.decode(getValue(a.getNodeValue(),null));
  709. cond = joinAnd(cond, new ConditionMinDistance(distance*distance));
  710. }
  711. // used for npc race
  712. else if ("race_id".equalsIgnoreCase(a.getNodeName()))
  713. {
  714. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  715. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  716. while (st.hasMoreTokens())
  717. {
  718. String item = st.nextToken().trim();
  719. array.add(Integer.decode(getValue(item, null)));
  720. }
  721. cond = joinAnd(cond, new ConditionTargetRaceId(array));
  722. }
  723. // used for pc race
  724. else if ("race".equalsIgnoreCase(a.getNodeName()))
  725. {
  726. Race race = Race.valueOf(a.getNodeValue());
  727. cond = joinAnd(cond, new ConditionTargetRace(race));
  728. }
  729. else if ("using".equalsIgnoreCase(a.getNodeName()))
  730. {
  731. int mask = 0;
  732. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  733. while (st.hasMoreTokens())
  734. {
  735. String item = st.nextToken().trim();
  736. for (L2WeaponType wt : L2WeaponType.values())
  737. {
  738. if (wt.toString().equals(item))
  739. {
  740. mask |= wt.mask();
  741. break;
  742. }
  743. }
  744. for (L2ArmorType at : L2ArmorType.values())
  745. {
  746. if (at.toString().equals(item))
  747. {
  748. mask |= at.mask();
  749. break;
  750. }
  751. }
  752. }
  753. cond = joinAnd(cond, new ConditionTargetUsesWeaponKind(mask));
  754. }
  755. else if ("npcId".equalsIgnoreCase(a.getNodeName()))
  756. {
  757. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  758. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  759. while (st.hasMoreTokens())
  760. {
  761. String item = st.nextToken().trim();
  762. array.add(Integer.decode(getValue(item, null)));
  763. }
  764. cond = joinAnd(cond, new ConditionTargetNpcId(array));
  765. }
  766. else if ("npcType".equalsIgnoreCase(a.getNodeName()))
  767. {
  768. String values = getValue(a.getNodeValue(), template).trim();
  769. String[] valuesSplit = values.split(" ");
  770. cond = joinAnd(cond, new ConditionTargetNpcType(valuesSplit));
  771. }
  772. }
  773. if (cond == null) _log.severe("Unrecognized <target> condition in " + _file);
  774. return cond;
  775. }
  776. protected Condition parseSkillCondition(Node n)
  777. {
  778. NamedNodeMap attrs = n.getAttributes();
  779. Stats stat = Stats.valueOfXml(attrs.getNamedItem("stat").getNodeValue());
  780. return new ConditionSkillStats(stat);
  781. }
  782. protected Condition parseUsingCondition(Node n)
  783. {
  784. Condition cond = null;
  785. NamedNodeMap attrs = n.getAttributes();
  786. for (int i = 0; i < attrs.getLength(); i++)
  787. {
  788. Node a = attrs.item(i);
  789. if ("kind".equalsIgnoreCase(a.getNodeName()))
  790. {
  791. int mask = 0;
  792. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  793. while (st.hasMoreTokens())
  794. {
  795. String item = st.nextToken().trim();
  796. for (L2WeaponType wt : L2WeaponType.values())
  797. {
  798. if (wt.toString().equals(item))
  799. {
  800. mask |= wt.mask();
  801. break;
  802. }
  803. }
  804. for (L2ArmorType at : L2ArmorType.values())
  805. {
  806. if (at.toString().equals(item))
  807. {
  808. mask |= at.mask();
  809. break;
  810. }
  811. }
  812. }
  813. cond = joinAnd(cond, new ConditionUsingItemType(mask));
  814. }
  815. else if ("skill".equalsIgnoreCase(a.getNodeName()))
  816. {
  817. int id = Integer.parseInt(a.getNodeValue());
  818. cond = joinAnd(cond, new ConditionUsingSkill(id));
  819. }
  820. else if ("slotitem".equalsIgnoreCase(a.getNodeName()))
  821. {
  822. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ";");
  823. int id = Integer.parseInt(st.nextToken().trim());
  824. int slot = Integer.parseInt(st.nextToken().trim());
  825. int enchant = 0;
  826. if (st.hasMoreTokens()) enchant = Integer.parseInt(st.nextToken().trim());
  827. cond = joinAnd(cond, new ConditionSlotItemId(slot, id, enchant));
  828. }
  829. else if ("weaponChange".equalsIgnoreCase(a.getNodeName()))
  830. {
  831. boolean val = Boolean.valueOf(a.getNodeValue());
  832. cond = joinAnd(cond, new ConditionChangeWeapon(val));
  833. }
  834. }
  835. if (cond == null) _log.severe("Unrecognized <using> condition in " + _file);
  836. return cond;
  837. }
  838. protected Condition parseGameCondition(Node n)
  839. {
  840. Condition cond = null;
  841. NamedNodeMap attrs = n.getAttributes();
  842. for (int i = 0; i < attrs.getLength(); i++)
  843. {
  844. Node a = attrs.item(i);
  845. if ("skill".equalsIgnoreCase(a.getNodeName()))
  846. {
  847. boolean val = Boolean.valueOf(a.getNodeValue());
  848. cond = joinAnd(cond, new ConditionWithSkill(val));
  849. }
  850. if ("night".equalsIgnoreCase(a.getNodeName()))
  851. {
  852. boolean val = Boolean.valueOf(a.getNodeValue());
  853. cond = joinAnd(cond, new ConditionGameTime(CheckGameTime.NIGHT, val));
  854. }
  855. if ("chance".equalsIgnoreCase(a.getNodeName()))
  856. {
  857. int val = Integer.decode(getValue(a.getNodeValue(), null));
  858. cond = joinAnd(cond, new ConditionGameChance(val));
  859. }
  860. }
  861. if (cond == null) _log.severe("Unrecognized <game> condition in " + _file);
  862. return cond;
  863. }
  864. protected void parseTable(Node n)
  865. {
  866. NamedNodeMap attrs = n.getAttributes();
  867. String name = attrs.getNamedItem("name").getNodeValue();
  868. if (name.charAt(0) != '#') throw new IllegalArgumentException("Table name must start with #");
  869. StringTokenizer data = new StringTokenizer(n.getFirstChild().getNodeValue());
  870. List<String> array = new ArrayList<String>(data.countTokens());
  871. while (data.hasMoreTokens())
  872. array.add(data.nextToken());
  873. setTable(name, array.toArray(new String[array.size()]));
  874. }
  875. protected void parseBeanSet(Node n, StatsSet set, Integer level)
  876. {
  877. String name = n.getAttributes().getNamedItem("name").getNodeValue().trim();
  878. String value = n.getAttributes().getNamedItem("val").getNodeValue().trim();
  879. char ch = value.length() == 0 ? ' ' : value.charAt(0);
  880. if (ch == '#' || ch == '-' || Character.isDigit(ch)) set.set(name,
  881. String.valueOf(getValue(value,
  882. level)));
  883. else set.set(name, value);
  884. }
  885. protected Lambda getLambda(Node n, Object template)
  886. {
  887. Node nval = n.getAttributes().getNamedItem("val");
  888. if (nval != null)
  889. {
  890. String val = nval.getNodeValue();
  891. if (val.charAt(0) == '#')
  892. { // table by level
  893. return new LambdaConst(Double.parseDouble(getTableValue(val)));
  894. }
  895. else if (val.charAt(0) == '$')
  896. {
  897. if (val.equalsIgnoreCase("$player_level"))
  898. return new LambdaStats(LambdaStats.StatsType.PLAYER_LEVEL);
  899. if (val.equalsIgnoreCase("$target_level"))
  900. return new LambdaStats(LambdaStats.StatsType.TARGET_LEVEL);
  901. if (val.equalsIgnoreCase("$player_max_hp"))
  902. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_HP);
  903. if (val.equalsIgnoreCase("$player_max_mp"))
  904. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_MP);
  905. // try to find value out of item fields
  906. StatsSet set = getStatsSet();
  907. String field = set.getString(val.substring(1));
  908. if (field != null)
  909. {
  910. return new LambdaConst(Double.parseDouble(getValue(field, template)));
  911. }
  912. // failed
  913. throw new IllegalArgumentException("Unknown value " + val);
  914. }
  915. else
  916. {
  917. return new LambdaConst(Double.parseDouble(val));
  918. }
  919. }
  920. LambdaCalc calc = new LambdaCalc();
  921. n = n.getFirstChild();
  922. while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
  923. n = n.getNextSibling();
  924. if (n == null || !"val".equals(n.getNodeName()))
  925. throw new IllegalArgumentException("Value not specified");
  926. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  927. {
  928. if (n.getNodeType() != Node.ELEMENT_NODE) continue;
  929. attachLambdaFunc(n, template, calc);
  930. }
  931. return calc;
  932. }
  933. protected String getValue(String value, Object template)
  934. {
  935. // is it a table?
  936. if (value.charAt(0) == '#')
  937. {
  938. if (template instanceof L2Skill) return getTableValue(value);
  939. else if (template instanceof Integer) return getTableValue(value, ((Integer) template).intValue());
  940. else throw new IllegalStateException();
  941. }
  942. return value;
  943. }
  944. protected Condition joinAnd(Condition cond, Condition c)
  945. {
  946. if (cond == null) return c;
  947. if (cond instanceof ConditionLogicAnd)
  948. {
  949. ((ConditionLogicAnd) cond).add(c);
  950. return cond;
  951. }
  952. ConditionLogicAnd and = new ConditionLogicAnd();
  953. and.add(cond);
  954. and.add(c);
  955. return and;
  956. }
  957. }