DocumentBase.java 43 KB

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