DocumentBase.java 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 ("battle_force".equalsIgnoreCase(a.getNodeName()))
  472. {
  473. forces[0] = Byte.decode(getValue(a.getNodeValue(), null));
  474. }
  475. else if ("spell_force".equalsIgnoreCase(a.getNodeName()))
  476. {
  477. forces[1] = Byte.decode(getValue(a.getNodeValue(), null));
  478. }
  479. else if ("charges".equalsIgnoreCase(a.getNodeName()))
  480. {
  481. int value = Integer.decode(getValue(a.getNodeValue(), template));
  482. cond = joinAnd(cond, new ConditionPlayerCharges(value));
  483. }
  484. else if ("souls".equalsIgnoreCase(a.getNodeName()))
  485. {
  486. int value = Integer.decode(getValue(a.getNodeValue(), template));
  487. cond = joinAnd(cond, new ConditionPlayerSouls(value));
  488. }
  489. else if ("weight".equalsIgnoreCase(a.getNodeName()))
  490. {
  491. int weight = Integer.decode(getValue(a.getNodeValue(), null));
  492. cond = joinAnd(cond, new ConditionPlayerWeight(weight));
  493. }
  494. else if ("invSize".equalsIgnoreCase(a.getNodeName()))
  495. {
  496. int size = Integer.decode(getValue(a.getNodeValue(), null));
  497. cond = joinAnd(cond, new ConditionPlayerInvSize(size));
  498. }
  499. else if ("isClanLeader".equalsIgnoreCase(a.getNodeName()))
  500. {
  501. boolean val = Boolean.valueOf(a.getNodeValue());
  502. cond = joinAnd(cond, new ConditionPlayerIsClanLeader(val));
  503. }
  504. else if ("onTvTEvent".equalsIgnoreCase(a.getNodeName()))
  505. {
  506. boolean val = Boolean.valueOf(a.getNodeValue());
  507. cond = joinAnd(cond, new ConditionPlayerTvTEvent(val));
  508. }
  509. else if ("pledgeClass".equalsIgnoreCase(a.getNodeName()))
  510. {
  511. int pledgeClass = Integer.decode(getValue(a.getNodeValue(), null));
  512. cond = joinAnd(cond, new ConditionPlayerPledgeClass(pledgeClass));
  513. }
  514. else if ("clanHall".equalsIgnoreCase(a.getNodeName()))
  515. {
  516. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  517. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  518. while (st.hasMoreTokens())
  519. {
  520. String item = st.nextToken().trim();
  521. array.add(Integer.decode(getValue(item, null)));
  522. }
  523. cond = joinAnd(cond, new ConditionPlayerHasClanHall(array));
  524. }
  525. else if ("fort".equalsIgnoreCase(a.getNodeName()))
  526. {
  527. int fort = Integer.decode(getValue(a.getNodeValue(), null));
  528. cond = joinAnd(cond, new ConditionPlayerHasFort(fort));
  529. }
  530. else if ("castle".equalsIgnoreCase(a.getNodeName()))
  531. {
  532. int castle = Integer.decode(getValue(a.getNodeValue(), null));
  533. cond = joinAnd(cond, new ConditionPlayerHasCastle(castle));
  534. }
  535. else if ("sex".equalsIgnoreCase(a.getNodeName()))
  536. {
  537. int sex = Integer.decode(getValue(a.getNodeValue(), null));
  538. cond = joinAnd(cond, new ConditionPlayerSex(sex));
  539. }
  540. else if ("flyMounted".equalsIgnoreCase(a.getNodeName()))
  541. {
  542. boolean val = Boolean.valueOf(a.getNodeValue());
  543. cond = joinAnd(cond, new ConditionPlayerFlyMounted(val));
  544. }
  545. else if ("landingZone".equalsIgnoreCase(a.getNodeName()))
  546. {
  547. boolean val = Boolean.valueOf(a.getNodeValue());
  548. cond = joinAnd(cond, new ConditionPlayerLandingZone(val));
  549. }
  550. else if ("active_effect_id".equalsIgnoreCase(a.getNodeName()))
  551. {
  552. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  553. cond = joinAnd(cond, new ConditionPlayerActiveEffectId(effect_id));
  554. }
  555. else if ("active_effect_id_lvl".equalsIgnoreCase(a.getNodeName()))
  556. {
  557. String val = getValue(a.getNodeValue(), template);
  558. int effect_id = Integer.decode(getValue(val.split(",")[0], template));
  559. int effect_lvl = Integer.decode(getValue(val.split(",")[1], template));
  560. cond = joinAnd(cond, new ConditionPlayerActiveEffectId(effect_id, effect_lvl));
  561. }
  562. else if ("active_skill_id".equalsIgnoreCase(a.getNodeName()))
  563. {
  564. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  565. cond = joinAnd(cond, new ConditionPlayerActiveSkillId(skill_id));
  566. }
  567. else if ("active_skill_id_lvl".equalsIgnoreCase(a.getNodeName()))
  568. {
  569. String val = getValue(a.getNodeValue(), template);
  570. int skill_id = Integer.decode(getValue(val.split(",")[0], template));
  571. int skill_lvl = Integer.decode(getValue(val.split(",")[1], template));
  572. cond = joinAnd(cond, new ConditionPlayerActiveSkillId(skill_id, skill_lvl));
  573. }
  574. else if ("class_id_restriction".equalsIgnoreCase(a.getNodeName()))
  575. {
  576. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  577. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  578. while (st.hasMoreTokens())
  579. {
  580. String item = st.nextToken().trim();
  581. array.add(Integer.decode(getValue(item, null)));
  582. }
  583. cond = joinAnd(cond, new ConditionPlayerClassIdRestriction(array));
  584. }
  585. else if ("subclass".equalsIgnoreCase(a.getNodeName()))
  586. {
  587. boolean val = Boolean.valueOf(a.getNodeValue());
  588. cond = joinAnd(cond, new ConditionPlayerSubclass(val));
  589. }
  590. else if ("instanceId".equalsIgnoreCase(a.getNodeName()))
  591. {
  592. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  593. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  594. while (st.hasMoreTokens())
  595. {
  596. String item = st.nextToken().trim();
  597. array.add(Integer.decode(getValue(item, null)));
  598. }
  599. cond = joinAnd(cond, new ConditionPlayerInstanceId(array));
  600. }
  601. else if ("agathionId".equalsIgnoreCase(a.getNodeName()))
  602. {
  603. int agathionId = Integer.decode(a.getNodeValue());
  604. cond = joinAnd(cond, new ConditionPlayerAgathionId(agathionId));
  605. }
  606. else if ("cloakStatus".equalsIgnoreCase(a.getNodeName()))
  607. {
  608. int val = Integer.valueOf(a.getNodeValue());
  609. cond = joinAnd(cond, new ConditionPlayerCloakStatus(val));
  610. }
  611. else if ("hasPet".equalsIgnoreCase(a.getNodeName()))
  612. {
  613. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  614. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  615. while (st.hasMoreTokens())
  616. {
  617. String item = st.nextToken().trim();
  618. array.add(Integer.decode(getValue(item, null)));
  619. }
  620. cond = joinAnd(cond, new ConditionPlayerHasPet(array));
  621. }
  622. else if ("servitorNpcId".equalsIgnoreCase(a.getNodeName()))
  623. {
  624. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  625. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  626. while (st.hasMoreTokens())
  627. {
  628. String item = st.nextToken().trim();
  629. array.add(Integer.decode(getValue(item, null)));
  630. }
  631. cond = joinAnd(cond, new ConditionPlayerServitorNpcId(array));
  632. }
  633. }
  634. if (forces[0] + forces[1] > 0)
  635. cond = joinAnd(cond, new ConditionForceBuff(forces));
  636. if (cond == null) _log.severe("Unrecognized <player> condition in " + _file);
  637. return cond;
  638. }
  639. protected Condition parseTargetCondition(Node n, Object template)
  640. {
  641. Condition cond = null;
  642. NamedNodeMap attrs = n.getAttributes();
  643. for (int i = 0; i < attrs.getLength(); i++)
  644. {
  645. Node a = attrs.item(i);
  646. if ("aggro".equalsIgnoreCase(a.getNodeName()))
  647. {
  648. boolean val = Boolean.valueOf(a.getNodeValue());
  649. cond = joinAnd(cond, new ConditionTargetAggro(val));
  650. }
  651. else if ("siegezone".equalsIgnoreCase(a.getNodeName()))
  652. {
  653. int value = Integer.decode(getValue(a.getNodeValue(), null));
  654. cond = joinAnd(cond, new ConditionSiegeZone(value, false));
  655. }
  656. else if ("level".equalsIgnoreCase(a.getNodeName()))
  657. {
  658. int lvl = Integer.decode(getValue(a.getNodeValue(), template));
  659. cond = joinAnd(cond, new ConditionTargetLevel(lvl));
  660. }
  661. else if ("class_id_restriction".equalsIgnoreCase(a.getNodeName()))
  662. {
  663. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  664. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  665. while (st.hasMoreTokens())
  666. {
  667. String item = st.nextToken().trim();
  668. array.add(Integer.decode(getValue(item, null)));
  669. }
  670. cond = joinAnd(cond, new ConditionTargetClassIdRestriction(array));
  671. }
  672. else if ("active_effect_id".equalsIgnoreCase(a.getNodeName()))
  673. {
  674. int effect_id = Integer.decode(getValue(a.getNodeValue(), template));
  675. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id));
  676. }
  677. else if ("active_effect_id_lvl".equalsIgnoreCase(a.getNodeName()))
  678. {
  679. String val = getValue(a.getNodeValue(), template);
  680. int effect_id = Integer.decode(getValue(val.split(",")[0], template));
  681. int effect_lvl = Integer.decode(getValue(val.split(",")[1], template));
  682. cond = joinAnd(cond, new ConditionTargetActiveEffectId(effect_id, effect_lvl));
  683. }
  684. else if ("active_skill_id".equalsIgnoreCase(a.getNodeName()))
  685. {
  686. int skill_id = Integer.decode(getValue(a.getNodeValue(), template));
  687. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id));
  688. }
  689. else if ("active_skill_id_lvl".equalsIgnoreCase(a.getNodeName()))
  690. {
  691. String val = getValue(a.getNodeValue(), template);
  692. int skill_id = Integer.decode(getValue(val.split(",")[0], template));
  693. int skill_lvl = Integer.decode(getValue(val.split(",")[1], template));
  694. cond = joinAnd(cond, new ConditionTargetActiveSkillId(skill_id, skill_lvl));
  695. }
  696. else if ("abnormal".equalsIgnoreCase(a.getNodeName()))
  697. {
  698. int abnormalId = Integer.decode(getValue(a.getNodeValue(), template));
  699. cond = joinAnd(cond, new ConditionTargetAbnormal(abnormalId));
  700. }
  701. else if("mindistance".equalsIgnoreCase(a.getNodeName()))
  702. {
  703. int distance = Integer.decode(getValue(a.getNodeValue(),null));
  704. cond = joinAnd(cond, new ConditionMinDistance(distance*distance));
  705. }
  706. // used for npc race
  707. else if ("race_id".equalsIgnoreCase(a.getNodeName()))
  708. {
  709. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  710. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  711. while (st.hasMoreTokens())
  712. {
  713. String item = st.nextToken().trim();
  714. array.add(Integer.decode(getValue(item, null)));
  715. }
  716. cond = joinAnd(cond, new ConditionTargetRaceId(array));
  717. }
  718. // used for pc race
  719. else if ("race".equalsIgnoreCase(a.getNodeName()))
  720. {
  721. Race race = Race.valueOf(a.getNodeValue());
  722. cond = joinAnd(cond, new ConditionTargetRace(race));
  723. }
  724. else if ("using".equalsIgnoreCase(a.getNodeName()))
  725. {
  726. int mask = 0;
  727. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  728. while (st.hasMoreTokens())
  729. {
  730. String item = st.nextToken().trim();
  731. for (L2WeaponType wt : L2WeaponType.values())
  732. {
  733. if (wt.toString().equals(item))
  734. {
  735. mask |= wt.mask();
  736. break;
  737. }
  738. }
  739. for (L2ArmorType at : L2ArmorType.values())
  740. {
  741. if (at.toString().equals(item))
  742. {
  743. mask |= at.mask();
  744. break;
  745. }
  746. }
  747. }
  748. cond = joinAnd(cond, new ConditionTargetUsesWeaponKind(mask));
  749. }
  750. else if ("npcId".equalsIgnoreCase(a.getNodeName()))
  751. {
  752. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  753. ArrayList<Integer> array = new ArrayList<Integer>(st.countTokens());
  754. while (st.hasMoreTokens())
  755. {
  756. String item = st.nextToken().trim();
  757. array.add(Integer.decode(getValue(item, null)));
  758. }
  759. cond = joinAnd(cond, new ConditionTargetNpcId(array));
  760. }
  761. else if ("npcType".equalsIgnoreCase(a.getNodeName()))
  762. {
  763. String values = getValue(a.getNodeValue(), template).trim();
  764. String[] valuesSplit = values.split(" ");
  765. cond = joinAnd(cond, new ConditionTargetNpcType(valuesSplit));
  766. }
  767. }
  768. if (cond == null) _log.severe("Unrecognized <target> condition in " + _file);
  769. return cond;
  770. }
  771. protected Condition parseSkillCondition(Node n)
  772. {
  773. NamedNodeMap attrs = n.getAttributes();
  774. Stats stat = Stats.valueOfXml(attrs.getNamedItem("stat").getNodeValue());
  775. return new ConditionSkillStats(stat);
  776. }
  777. protected Condition parseUsingCondition(Node n)
  778. {
  779. Condition cond = null;
  780. NamedNodeMap attrs = n.getAttributes();
  781. for (int i = 0; i < attrs.getLength(); i++)
  782. {
  783. Node a = attrs.item(i);
  784. if ("kind".equalsIgnoreCase(a.getNodeName()))
  785. {
  786. int mask = 0;
  787. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ",");
  788. while (st.hasMoreTokens())
  789. {
  790. String item = st.nextToken().trim();
  791. for (L2WeaponType wt : L2WeaponType.values())
  792. {
  793. if (wt.toString().equals(item))
  794. {
  795. mask |= wt.mask();
  796. break;
  797. }
  798. }
  799. for (L2ArmorType at : L2ArmorType.values())
  800. {
  801. if (at.toString().equals(item))
  802. {
  803. mask |= at.mask();
  804. break;
  805. }
  806. }
  807. }
  808. cond = joinAnd(cond, new ConditionUsingItemType(mask));
  809. }
  810. else if ("skill".equalsIgnoreCase(a.getNodeName()))
  811. {
  812. int id = Integer.parseInt(a.getNodeValue());
  813. cond = joinAnd(cond, new ConditionUsingSkill(id));
  814. }
  815. else if ("slotitem".equalsIgnoreCase(a.getNodeName()))
  816. {
  817. StringTokenizer st = new StringTokenizer(a.getNodeValue(), ";");
  818. int id = Integer.parseInt(st.nextToken().trim());
  819. int slot = Integer.parseInt(st.nextToken().trim());
  820. int enchant = 0;
  821. if (st.hasMoreTokens()) enchant = Integer.parseInt(st.nextToken().trim());
  822. cond = joinAnd(cond, new ConditionSlotItemId(slot, id, enchant));
  823. }
  824. else if ("weaponChange".equalsIgnoreCase(a.getNodeName()))
  825. {
  826. boolean val = Boolean.valueOf(a.getNodeValue());
  827. cond = joinAnd(cond, new ConditionChangeWeapon(val));
  828. }
  829. }
  830. if (cond == null) _log.severe("Unrecognized <using> condition in " + _file);
  831. return cond;
  832. }
  833. protected Condition parseGameCondition(Node n)
  834. {
  835. Condition cond = null;
  836. NamedNodeMap attrs = n.getAttributes();
  837. for (int i = 0; i < attrs.getLength(); i++)
  838. {
  839. Node a = attrs.item(i);
  840. if ("skill".equalsIgnoreCase(a.getNodeName()))
  841. {
  842. boolean val = Boolean.valueOf(a.getNodeValue());
  843. cond = joinAnd(cond, new ConditionWithSkill(val));
  844. }
  845. if ("night".equalsIgnoreCase(a.getNodeName()))
  846. {
  847. boolean val = Boolean.valueOf(a.getNodeValue());
  848. cond = joinAnd(cond, new ConditionGameTime(CheckGameTime.NIGHT, val));
  849. }
  850. if ("chance".equalsIgnoreCase(a.getNodeName()))
  851. {
  852. int val = Integer.decode(getValue(a.getNodeValue(), null));
  853. cond = joinAnd(cond, new ConditionGameChance(val));
  854. }
  855. }
  856. if (cond == null) _log.severe("Unrecognized <game> condition in " + _file);
  857. return cond;
  858. }
  859. protected void parseTable(Node n)
  860. {
  861. NamedNodeMap attrs = n.getAttributes();
  862. String name = attrs.getNamedItem("name").getNodeValue();
  863. if (name.charAt(0) != '#') throw new IllegalArgumentException("Table name must start with #");
  864. StringTokenizer data = new StringTokenizer(n.getFirstChild().getNodeValue());
  865. List<String> array = new ArrayList<String>(data.countTokens());
  866. while (data.hasMoreTokens())
  867. array.add(data.nextToken());
  868. setTable(name, array.toArray(new String[array.size()]));
  869. }
  870. protected void parseBeanSet(Node n, StatsSet set, Integer level)
  871. {
  872. String name = n.getAttributes().getNamedItem("name").getNodeValue().trim();
  873. String value = n.getAttributes().getNamedItem("val").getNodeValue().trim();
  874. char ch = value.length() == 0 ? ' ' : value.charAt(0);
  875. if (ch == '#' || ch == '-' || Character.isDigit(ch)) set.set(name,
  876. String.valueOf(getValue(value,
  877. level)));
  878. else set.set(name, value);
  879. }
  880. protected Lambda getLambda(Node n, Object template)
  881. {
  882. Node nval = n.getAttributes().getNamedItem("val");
  883. if (nval != null)
  884. {
  885. String val = nval.getNodeValue();
  886. if (val.charAt(0) == '#')
  887. { // table by level
  888. return new LambdaConst(Double.parseDouble(getTableValue(val)));
  889. }
  890. else if (val.charAt(0) == '$')
  891. {
  892. if (val.equalsIgnoreCase("$player_level"))
  893. return new LambdaStats(LambdaStats.StatsType.PLAYER_LEVEL);
  894. if (val.equalsIgnoreCase("$target_level"))
  895. return new LambdaStats(LambdaStats.StatsType.TARGET_LEVEL);
  896. if (val.equalsIgnoreCase("$player_max_hp"))
  897. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_HP);
  898. if (val.equalsIgnoreCase("$player_max_mp"))
  899. return new LambdaStats(LambdaStats.StatsType.PLAYER_MAX_MP);
  900. // try to find value out of item fields
  901. StatsSet set = getStatsSet();
  902. String field = set.getString(val.substring(1));
  903. if (field != null)
  904. {
  905. return new LambdaConst(Double.parseDouble(getValue(field, template)));
  906. }
  907. // failed
  908. throw new IllegalArgumentException("Unknown value " + val);
  909. }
  910. else
  911. {
  912. return new LambdaConst(Double.parseDouble(val));
  913. }
  914. }
  915. LambdaCalc calc = new LambdaCalc();
  916. n = n.getFirstChild();
  917. while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
  918. n = n.getNextSibling();
  919. if (n == null || !"val".equals(n.getNodeName()))
  920. throw new IllegalArgumentException("Value not specified");
  921. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  922. {
  923. if (n.getNodeType() != Node.ELEMENT_NODE) continue;
  924. attachLambdaFunc(n, template, calc);
  925. }
  926. return calc;
  927. }
  928. protected String getValue(String value, Object template)
  929. {
  930. // is it a table?
  931. if (value.charAt(0) == '#')
  932. {
  933. if (template instanceof L2Skill) return getTableValue(value);
  934. else if (template instanceof Integer) return getTableValue(value, ((Integer) template).intValue());
  935. else throw new IllegalStateException();
  936. }
  937. return value;
  938. }
  939. protected Condition joinAnd(Condition cond, Condition c)
  940. {
  941. if (cond == null) return c;
  942. if (cond instanceof ConditionLogicAnd)
  943. {
  944. ((ConditionLogicAnd) cond).add(c);
  945. return cond;
  946. }
  947. ConditionLogicAnd and = new ConditionLogicAnd();
  948. and.add(cond);
  949. and.add(c);
  950. return and;
  951. }
  952. }