DocumentBase.java 42 KB

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