DocumentBase.java 41 KB

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