DocumentBase.java 42 KB

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